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

Units are not consistent with Pyomo DAE discretization #2970

Closed
dallan-keylogic opened this issue Aug 25, 2023 · 1 comment
Closed

Units are not consistent with Pyomo DAE discretization #2970

dallan-keylogic opened this issue Aug 25, 2023 · 1 comment
Labels

Comments

@dallan-keylogic
Copy link

Summary

Units aren't involved in pyomo.dae discretization transformations, which leads to inconsistent unit errors.

Steps to reproduce the issue

import pyomo.environ as pyo
from pyomo.dae import ContinuousSet, DerivativeVar
from pyomo.util.check_units import assert_units_consistent

m = pyo.ConcreteModel()
m.time = ContinuousSet(initialize=[0, 1, 2, 3, 4])

m.x = pyo.Var(m.time, units=pyo.units.mol, initialize=0)

m.dxdt = DerivativeVar(m.x, wrt=m.time, initialize=0, units=pyo.units.mol/pyo.units.s)

pyo.TransformationFactory("dae.finite_difference").apply_to(m, nfe=len(m.time)-1, wrt=m.time, scheme="BACKWARD")

assert_units_consistent(m)

Error Message

Exception has occurred: InconsistentUnitsError
Error in units found in expression: dxdt[1] - (x[1] - x[0]): mole / second not compatible with mole.
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\core\base\units_container.py", line 482, in _get_unit_for_equivalent_children
    raise InconsistentUnitsError(
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\core\base\units_container.py", line 929, in exitNode
    return node_func(self, node, data)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\core\expr\visitor.py", line 481, in _process_node_bx
    return self.exitNode(node, data)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\core\expr\visitor.py", line 268, in walk_expression
    result = self._process_node(root, RECURSION_LIMIT)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\core\base\units_container.py", line 1206, in _get_pint_units
    return self._pintUnitExtractionVisitor.walk_expression(expr=expr)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\util\check_units.py", line 165, in _assert_units_consistent_expression
    pint_unit = units._get_pint_units(expr)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\util\check_units.py", line 249, in assert_units_consistent
    _assert_units_consistent_expression(obj)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\util\check_units.py", line 121, in _assert_units_consistent_constraint_data
    assert_units_consistent(*args)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\util\check_units.py", line 270, in assert_units_consistent
    handler(cdata)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\util\check_units.py", line 193, in _assert_units_consistent_block
    assert_units_consistent(component)
  File "C:\Users\[REDACTED]\Repos\pyomo\pyomo\util\check_units.py", line 275, in assert_units_consistent
    handler(obj)
  File "C:\Users\[REDACTED]\Desktop\discretization_units.py", line 14, in <module>
    assert_units_consistent(m)
pyomo.core.base.units_container.InconsistentUnitsError: Error in units found in expression: dxdt[1] - (x[1] - x[0]): mole / second not compatible with mole.
@blnicho
Copy link
Member

blnicho commented Aug 25, 2023

This is a duplicate of #1790

@blnicho blnicho closed this as completed Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants