pyomo icon indicating copy to clipboard operation
pyomo copied to clipboard

Bug when converting units of expression with value 0

Open andrewlee94 opened this issue 4 years ago • 7 comments

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.

andrewlee94 avatar Sep 15 '20 19:09 andrewlee94

@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.

andrewlee94 avatar Jun 10 '21 17:06 andrewlee94

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?

Robbybp avatar Jul 08 '21 17:07 Robbybp

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.

dallan-keylogic avatar Nov 30 '21 18:11 dallan-keylogic

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.

dallan-keylogic avatar Jun 15 '22 14:06 dallan-keylogic

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.

adam-a-a avatar Jul 12 '22 16:07 adam-a-a

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.

tarnold17 avatar Sep 07 '22 20:09 tarnold17

A workaround that works in some cases is creating a constant Param with the correct units and the value zero.

dallan-keylogic avatar Sep 07 '22 20:09 dallan-keylogic