-
I am working on a project involving natural gas and want to be able to convert between volume, mass and energy using provided densities. I have the following code python: import pint
ureg = pint.UnitRegistry()
ureg.define('Sm3 = m^3') # cubic meter of gas at given temperature and pressure
NG_DENS = 0.8 * ureg('kg/Sm3') # density
NG_SE = 50 * ureg('MJ/kg') # specific energy (energy per mass)
NG_ED = (NG_SE * NG_DENS).to_reduced_units() # energy density (energy per volume)
ng = pint.Context('ngas')
ng.add_transformation('[volume]', '[mass]', lambda ureg, x: x * NG_DENS)
ng.add_transformation('[mass]', '[energy]', lambda ureg, x: x * NG_SE)
ureg.add_context(ng)
print(f"{ureg('1 Sm3').to('kg', 'ngas') = }")
print(f"{ureg('1 Sm3').to('MJ', 'ngas') = }")
print(f"{ureg('1 Sm3/s').to('MJ/s', 'ngas') = }")
print(f"{ureg('1 Sm3/s').to('MW', 'ngas') = }") The first two However, for the last two lines to work I have to add one of the following transformations to the definition of context ng.add_transformation('[volume]/[time]', '[energy]/[time]', lambda ureg, x: x * NG_ED)
ng.add_transformation('[volume]/[time]', '[power]', lambda ureg, x: x * NG_ED) And if I wanted to convert Why doesn't pint figure this out? And, in general, what kind of relationships can I expect pint to deduce self and what has to be specified? Or is there a better way to achieve these transformations? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Right now, the context transformation only works by matching exact source and target dimensionality. That is why you need to define extra transformations for I would like to have something more clever in the future and PR are welcome. For example, it could be implement that if the simple transformation is not found, common dimensions in the source and target are ignored. |
Beta Was this translation helpful? Give feedback.
Right now, the context transformation only works by matching exact source and target dimensionality. That is why you need to define extra transformations for
volume]/[time] -> [energy]/[time]
.I would like to have something more clever in the future and PR are welcome. For example, it could be implement that if the simple transformation is not found, common dimensions in the source and target are ignored.