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

Unit consistency issue with Pyomo DAE #1790

Open
andrewlee94 opened this issue Jan 15, 2021 · 4 comments
Open

Unit consistency issue with Pyomo DAE #1790

andrewlee94 opened this issue Jan 15, 2021 · 4 comments

Comments

@andrewlee94
Copy link

andrewlee94 commented Jan 15, 2021

When using pyomo.dae in a model with units, there is a potential issue with unit consistency as the ContinuousSet does not have units. Consider the following case:

from pyomo.environ import ConcreteModel, Var, TransformationFactory, units
from pyomo.dae import ContinuousSet, DerivativeVar
from pyomo.util.check_units import assert_units_consistent

m = ConcreteModel()

m.time = ContinuousSet(initialize=[0, 100])  # Implicit units of seconds

m.disp = Var(m.time, initialize=10, units=units.m, doc="Displacement")
m.vel = DerivativeVar(m.disp, wrt=m.time, units=units.m/units.s, initialize=1, doc="Velocity")

discretizer = TransformationFactory("dae.finite_difference")
discretizer.apply_to(m,  wrt=m.time, nfe=200, scheme="BACKWARD")

assert_units_consistent(m)

>>> InconsistentUnitsError: Error in units found in expression: vel[0.5] - 2.0*(disp[0.5] - disp[0]): meter / second not compatible with meter.

If the DerivativeVar is defined such that it has the correct units (m/s), there ends up being a unit consistency issue in the discretization equations as Pyomo does not realize that the time domain has units, thus the "delta_t" term in the discretization is unitless.

Whilst there are ways around this (e.g. using normalized domains), I think it would make more sense (and be more obvious to the user) if the ContinuousSet were to have units and the numerical discretization to account for this.

@bpaul4
Copy link

bpaul4 commented Dec 7, 2021

@blnicho have there been any updates regarding this issue? For flowsheets where units are assigned to both the DerivativeVar and the differentiated variable, the ContinuousSet can be assigned appropriate units in the discretization method. Thanks!

@dallan-keylogic
Copy link

Unfortunately, I have once again rediscovered this issue.

@adam-a-a
Copy link

adam-a-a commented Jul 31, 2024

We are starting to incorporate dynamics in WaterTAP and this issue occurs while attempting to make our reverse osmosis model. In our tests, I threw in an assert_units_consistent before and after transformation, and this issue arises after transformation.

Here is the PR on WaterTAP: watertap-org/watertap#1471

@adowling2
Copy link
Member

I am starting to revamp my course materials that teach Pyomo.DAE; I would also like suggestions on best practices for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants