Make physical units check trigger for `_GeneralVarData.set_value` more consistent
shermanjasonaf opened this issue · 0 comments
shermanjasonaf commented
Summary
As of #3077 (and, in particular, cfa6ff49f
), invoking _GeneralVarData.set_value()
on an variable object with units to a dimensionless numpy type (such as numpy.float64
) may result in a UnitsError
, but this appears to depend on on whether or not numpy types were added to native_numeric_types
before or after set_value()
was invoked.
Steps to reproduce the issue
Before cfa6ff49f
>>> import numpy as np
>>> import pyomo.environ as pyo
>>> m = pyo.ConcreteModel()
>>> m.v = pyo.Var(units=pyo.units.m)
>>> m.v.set_value(np.float64(1))
>>> # (no error)
After cfa6ff49f
>>> import numpy as np
>>> import pyomo.environ as pyo
>>> m = pyo.ConcreteModel()
>>> m.v = pyo.Var(units=pyo.units.m)
>>> m.v.set_value(np.float64(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...pyomo/pyomo/core/base/var.py", line 397, in set_value
val = units.convert_value(
File "...pyomo/pyomo/core/base/units_container.py", line 1384,
in convert_value
raise UnitsError(
pyomo.core.base.units_container.UnitsError: Cannot convert dimensionless to m. Units are not compatible.
>>> m.v.set_value(np.float64(1))
>>> # (no error on second attempt; numpy numeric types registered before exception raised)
Error Message
See previous section.
Information on your system
Pyomo version: 6.7.1dev0 (before and after cfa6ff49f
)
Python version: 3.9.17
Operating system: Ubuntu 20.04
How Pyomo was installed (PyPI, conda, source): source
Solver (if applicable):
Additional information
Other dependencies: numpy 1.23.1, Pint 0.19.2