5

I'm working on my first Pyomo DAE project to teach myself how to use it for trajectory optimization. I have generated an atmosphere lookup table with density and speed of sound as a function of altitude. Pyomo will not accept the following code for my concrete model:

# create a model object
m = ConcreteModel()

define the independent variables

m.stage = RangeSet(0,1,bounds=(0,1)) m.tau = ContinuousSet(bounds=(0,1)) m.t = Var(m.stage, m.tau)

define the dependent variables

m.px = Var(m.stage, m.tau) m.py = Var(m.stage, m.tau) m.v = Var(m.stage, m.tau, bounds=(vmin, None)) m.g = Var(m.stage, m.tau, bounds=(gmin, gmax))

define internal function to calculate Mach number

def _Mach(alt,v): sos = np.interp(alt,altTab,sosTab) return v/sos

define algebraic auxiliary variables

m.Mach = Var(m.stage, m.tau) m.alg_Mach = Constraint(m.stage, m.tau, rule=lambda m, stage, tau:
m.Mach[stage,tau] == _Mach(m.py[stage,tau],m.v[stage,tau]) )

  • m.stage is a Pyomo RangeSet over [0,1] to represent the two phases of my problem (boost and coast)
  • m.tau is a ContinuousSet over [0,1] which is normalized time
  • m.py is the altitude variable
  • m.v is the airspeed variable
  • altTab and sosTab are 1D numpy.array variables defined using a previous call to pyatmos.coesa76, but they could be any arbitrary numpy.arrays for test purposes.

The error message is:

ERROR: Rule failed when generating expression for Constraint alg_Mach with
    index (0, 0): TypeError: Implicit conversion of Pyomo numeric value
    (py[0,0]) to float is disabled. This error is often the result of using
    Pyomo components as arguments to one of the Python built-in math module
    functions when defining expressions. Avoid this error by using Pyomo-
    provided math functions or explicitly resolving the numeric value using
    the Pyomo value() function.
ERROR: Constructing component 'alg_Mach' from data=None failed: TypeError:
    Implicit conversion of Pyomo numeric value (py[0,0]) to float is disabled.
    This error is often the result of using Pyomo components as arguments to
    one of the Python built-in math module functions when defining
    expressions. Avoid this error by using Pyomo-provided math functions or
    explicitly resolving the numeric value using the Pyomo value() function.

What's the acceptable way to perform linear interpolation on parameters of a Pyomo DAE problem?

machalot
  • 51
  • 1
  • 1
    Did you try the suggestions at the end of the error message? – Mark L. Stone Oct 01 '22 at 12:07
  • I tried it, but my impression was that it evaluates the variable immediately and creates a constant. It gave an error because py[] was not initialized. – machalot Oct 02 '22 at 05:35
  • When I change the rule to use value(m.py[stage,tau]),value(m.v[stage,tau])) it returns three errors: ERROR: evaluating object as numeric value: py[0,0] (object: <class 'pyomo.core.base.var._GeneralVarData'>) No value for uninitialized NumericValue object py[0,0] ERROR: Rule failed when generating expression for Constraint alg_Mach with index (0, 0): ValueError: No value for uninitialized NumericValue object py[0,0] ERROR: Constructing component 'alg_Mach' from data=None failed: ValueError: No value for uninitialized NumericValue object py[0,0] – machalot Oct 02 '22 at 05:42

0 Answers0