pyomo
pyomo copied to clipboard
Bug when converting units of expression with value 0
Using the units.convert
method incorrectly returns an exception if used on an expression with value 0, as shown in the example below.
from pyomo.environ import units
units.convert(0*units.Pa, to_units=units.kPa)
>>> InconsistentUnitsError: Error in convert: units not compatible.: None not compatible with kilopascal.
The above code works if the expression is given a small but non-zero value.
@carldlaird @jsiirola Would it be possible to get this issue fixed soon? Whilst I have been able to work around it in the past, I have just run into a case where it is harder to avoid.
Just hit this myself. Looks like the expression system automatically returns zero if either argument to __mul__
is zero. Maybe this should not be the case if the other argument is an NPV_Expression?
Just ran into this issue myself. It happens not just with conversion, but even just initial assignment of units:
def func_dk_dT(cname,param_data,i,j,T):
# Need to make the pyomo units match
return 0/T
returns the error
pyomo.core.base.units_container.InconsistentUnitsError: Error in units found in expression: 0.0 + (1 - 0.0)/2*... dimensionless not compatible with 1 / kelvin.
whereas
def func_dk_dT(cname,param_data,i,j,T):
# Need to make the pyomo units match
return 1E-15/T
works just fine.
Could we please get a fix for this sometime soon? I just ran into it with my SOEC model.
InconsistentUnitsError: Error in units found in expression: 0.0 - fs.soec_module.solid_oxide_cell.fuel_channel.material_flux_x1[0.0,1,H2]: dimensionless not compatible with mole / meter ** 2 / second.
We also noticed the same issue when setting a default initial value of zero for an expression (see WaterTAP Issue).
We end up getting the same InconsistentUnitsError
as others on this thread. It would be great if we could resolve this issue.
I'll add my name here to request a fix. I also have encountered this error:
pyomo.core.base.units_container.InconsistentUnitsError: Error in convert: units not compatible.: meter ** 3 / year not compatible with USD_2020 / year.
A workaround that works in some cases is creating a constant Param
with the correct units and the value zero.