openPMD / openPMD-api

:floppy_disk: C++ & Python API for Scientific I/O
https://openpmd-api.readthedocs.io
GNU Lesser General Public License v3.0
134 stars 51 forks source link

set SI units #1622

Open rl3418 opened 1 month ago

rl3418 commented 1 month ago

I pip installed openpmd_api 0.15.2 and tried to do particle writing. I have attached the script I used down below. Setting Unit.si showed an error saying that 'openpmd_api.openpmd_api_cxx.Record' object has no attribute 'unit_SI'. Everything else works just fine.

boost along z.txt

rl3418 commented 1 month ago

It seems that set units should be applied to electrons["momentum"]['x'].unit_SI instead of electrons["momentum"].unit_SI. Just wanna check if that is correct.

franzpoeschel commented 1 month ago

Hello,

yes, the unit_SI should be applied to the last level as it is possible that it needs to be specified separately for each component.

I also see a comment "This section I don't know how to implement" down in the file. Do you have a specific question there? Otherwise, the workflow is the same as for the other data, except that you specify io.Record_Component.SCALAR] to indicate that the last level is not needed, i.e.:

weighting = electrons["weighting"][io.Record_Component.SCALAR]
weighting.reset_dataset(...)
weighting.store_chunk(w)
rl3418 commented 1 month ago

For weighting how do I set the SI unit? I thought [io.Record_Component.SCALAR] declares that the last level is not needed, so I just applied unit_SI to the top level. This is the code I used.

weighting = electrons["weighting"][io.Record_Component.SCALAR] electrons["weighting"].unit_dimension = {Unit_Dimension.L: 0} electrons["weighting"].unit_SI = 1.0

franzpoeschel commented 1 month ago

That will work in the next version of openPMD-api (or to be more precise: [io.Record_Component.SCALAR] is no longer necessary at all since it causes too much confusion). For now, you'll have to write electrons["weighting"][io.RecordComponent.SCALAR].unit_SI = 1.0.

rl3418 commented 1 month ago

Great, thanks for the clarification.