Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make physical units check trigger for _GeneralVarData.set_value more consistent #3100

Closed
shermanjasonaf opened this issue Jan 23, 2024 · 0 comments · Fixed by #3151
Closed
Labels

Comments

@shermanjasonaf
Copy link
Contributor

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

@shermanjasonaf shermanjasonaf changed the title Make physical units check trigger for Var.set_value more consistent Make physical units check trigger for _GeneralVarData.set_value more consistent Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant