pyomo icon indicating copy to clipboard operation
pyomo copied to clipboard

Support for variable units in Vars (and maybe Params)

Open andrewlee94 opened this issue 2 years ago • 4 comments

Summary

When working with empirical correlations involving power laws, we occasionally run into cases where the units of an empirical parameter depend on the value of another empirical parameter. Consider the following use case:

m = pyo.ConcreteModel()

m.v_1 = pyo.Var(units=pyo.units.m)
m.v_2 = pyo.Var(units=pyo.units.kg)

m.param_1 = Var()  # would like to assign units here
m.param_2 = Var(units=pyo.units.dimensionless)

@m.Constraint()
def correlation(m):
    return m.v_2 == m.param_1*m.v_1**m.param_2

Strictly speaking, the units of m.param_1 are kg/m**param_2 - i.e. the units of param_1 depend on the value of param_2. However, this behaviour is currently not supported by Pyomo (in part at least for performance reasons). In many cases, we would simply say that param_2 is an immutable parameter of known value and thus the units of param_1 are also known a priori, however if param_2 is to be estimated or is uncertain then this is not possible.

Currently, the best solution to cases like this is to introduce a third parameter with value 1 which will cancel the units in the power law term (i.e. return m.v_2 == m.param_1*(m.v_1/m.param_3)**m.param_2, similar to what would be done if a log were to appear in an empirical correlation. However, in this case the correct approach is to recognize that the original form is unit consistent but that the units of param_1 need to vary with param_2.

andrewlee94 avatar May 12 '22 15:05 andrewlee94

FWIW, I think the "correct" solution is the use of param_3 to make the base dimensionless. I think there is a fundamental conceptual problem if the meaning of a value (its units) is a function of another variable. It is a little like saying x is the number of floors in a building, unless y is 3, in which case it is the number of buildings on a block.

jsiirola avatar May 12 '22 17:05 jsiirola

@jsiirola Perhaps there is a more fundamental question: what does unit consistency even mean in an empirical correlation? Seeing as the correlation form is purely empirical and ignores any concept of units, perhaps that should extend to the correlation itself; i.e. we should have a way to flag a constraint as "empirical" and should be skipped for the purposes of asserting consistency.

andrewlee94 avatar May 12 '22 18:05 andrewlee94

I think you can think of an empirical correlation in much the same way as an external function: it is something that takes values with known units and produces a value in known units. If you buy that, then a correlation is something like y = A*f(x1, x2, ...). In that case, the output units are on A, and you enforce the input units by dividing them by the expected units and enforcing the result is dimensionless. That is basically the "param_3" solution.

jsiirola avatar May 13 '22 20:05 jsiirola

I agree with @jsiirola . I have seen many "constants" with units that are meaningless - they are just for unit consistency.

michaelbynum avatar May 15 '22 14:05 michaelbynum