pyomo.dae.simulator.Simulator should use initial guess for algebraic variables

40 views
Skip to first unread message

N zudemK

unread,
Sep 12, 2023, 8:11:50 AM9/12/23
to Pyomo Forum

Hello everyone,

Currently, the Simulator reads only the initial guesses for the differential variables and provides them to the Casadi integrator instance. I recommend that we enhance this process by obtaining the initial guesses for the algebraic variables from the Pyomo Var, which represents algebraic variables, and passing them to the Casadi integrator using the keyword argument 'z0'.

This change is particularly crucial in my case because the current approach results in a failure during the initial condition calculation of IDAS. To address this issue, I had to implement a workaround in my simulator, which looks like this:


if len(self._algvars) != 0:
    zalltemp = [self._templatemap[i] for i in self._simalgvars]
    zall = casadi.vertcat(*zalltemp)

    algalltemp = [convert_pyomo2casadi(i) for i in self._alglist]
    algall = casadi.vertcat(*algalltemp)
    dae['z'] = zall
    dae['alg'] = algall

    z0 = []
    for v in self._simalgvars:
        for idx, i in enumerate(v._args):
            if type(i) is IndexTemplate:
                break
        initpoint = self._contset.first()
        vidx = tuple(v._args[0:idx]) + (initpoint,) + tuple(v._args[idx + 1 :])
        # This line will raise an error if no value was set
        z0.append(value(v._base[vidx]))

integrator_options['grid'] = tsim
integrator_options['output_t0'] = True
F = casadi.integrator('F', integrator, dae, integrator_options)
if len(self._algvars) != 0:
    sol = F(x0=initcon, z0=z0)
else:
    sol = F(x0=initcon)

I must emphasize that this solution may require further testing and might be specific to my use case. However, it was essential for me to ensure that IDAS integrates my DAE correctly.

If anyone has insights or would like to collaborate on implementing a more robust version of this workaround, I'm open to discussions and cooperation.

Reply all
Reply to author
Forward
0 new messages