Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.97k stars 505 forks source link

Pyomo+Casadi Error Cos + ContinuousSet #1612

Open Alves663 opened 4 years ago

Alves663 commented 4 years ago

I want to use Pyomo + Casadi to simulate an ODE system. The problem is that my ODE has a cosine of a continuous set.

m.x = ContinuousSet(bounds=(0,100))
m.y = Var(bounds=(0,100))
m.px = Var(m.x,bounds = (200000,500000))
m.dpdx = DerivativeVar(m.px,wrt=m.x,initialize=0)
m.y.fix(1)
m.px[0].fix(1)
def ODE_rule(m,x):
    return m.dpdx[x] == m.y*m.px[x] + cos(x) - m.y*sin(x)
m.drop_pressure = Constraint(m.x,rule=ODE_rule)

  sim = Simulator(m, package='casadi')
tsim, profiles = sim.simulate(numpoints=200,integrator = 'idas')

If I use Scipy the simulator works but if I use Casadi it gives me an error:

TemplateExpressionError: Evaluating uninitialized IndexTemplate ({x})

I need to use Casadi because I have algebraic equations in my model. Somebody could help me?

gabriel23cg commented 4 years ago

Here is the complete error message:

---------------------------------------------------------------------------
TemplateExpressionError                   Traceback (most recent call last)
<ipython-input-17-427db2c6e237> in <module>
----> 1 tsim, profiles = sim.simulate(numpoints=200,integrator = 'idas')

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\dae\simulator.py in simulate(self, numpoints, tstep, integrator, varying_inputs, initcon, integrator_options)
    910                     self._simulate_with_casadi_no_inputs(initcon, tsim,
    911                                                          integrator,
--> 912                                                          integrator_options)
    913
    914         self._tsim = tsim

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\dae\simulator.py in _simulate_with_casadi_no_inputs(self, initcon, tsim, integrator, integrator_options)
    954
    955         odealltemp = [convert_pyomo2casadi(self._rhsdict[i])
--> 956                       for i in self._derivlist]
    957         odeall = casadi.vertcat(*odealltemp)
    958         dae = {'x': xall, 'ode': odeall}

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\dae\simulator.py in <listcomp>(.0)
    954
    955         odealltemp = [convert_pyomo2casadi(self._rhsdict[i])
--> 956                       for i in self._derivlist]
    957         odeall = casadi.vertcat(*odealltemp)
    958         dae = {'x': xall, 'ode': odeall}

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\dae\simulator.py in convert_pyomo2casadi(expr)
    377                         "expression to a Casadi expression.")
    378     visitor = Convert_Pyomo2Casadi_Visitor()
--> 379     return visitor.dfs_postorder_stack(expr)
    380
    381

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\core\expr\visitor.py in dfs_postorder_stack(self, node)
    579                 _sub = _argList[_idx]
    580                 _idx += 1
--> 581                 flag, value = self.visiting_potential_leaf(_sub)
    582                 if flag:
    583                     _result.append( value )

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\dae\simulator.py in visiting_potential_leaf(self, node)
    331
    332         if not node.is_expression_type():
--> 333             return True, value(node)
    334
    335         return False, None

pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.value()

pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.value()

c:\users\kfed\venv_dtso\lib\site-packages\pyomo\core\expr\template_expr.py in __call__(self, exception)
    416                 raise TemplateExpressionError(
    417                     self, "Evaluating uninitialized IndexTemplate (%s)"
--> 418                     % (self,))
    419             return None
    420         else:

TemplateExpressionError: Evaluating uninitialized IndexTemplate ({x})
blnicho commented 4 years ago

Looks like a bug in the Simulator. I'm currently working on some other updates to the Simulator so I'll try to fix this as part of an upcoming PR.

damdaepark commented 3 years ago

Encountered the same problem. Waiting for the update.

adowling2 commented 3 years ago

@blnicho Found the same problem for another example. Happy to test if you have a solution. Edit: Or contribute a new Pyomo.dae test.