idaes-pse icon indicating copy to clipboard operation
idaes-pse copied to clipboard

First time point in dynamic models

Open dallan-keylogic opened this issue 3 years ago • 5 comments

Right now, some IDAES models obtain an initial condition for a model with dynamics by adding logic constraining the first time point to be at steady state. See, for example, the PID controller model:

@self.Constraint(time_set, doc="Error calculated by derivative of integral")
            def error_from_integral_eqn(b, t):
                if t == time_0:
                    if self.config.calculate_initial_integral:
                        if self.config.type == ControllerType.PI:
                            return (
                                self.integral_of_error[t]
                                == (
                                    b.manipulated_var[t]
                                    - b.mv_ref[t]
                                    - b.gain_p[t] * b.error[t]
                                )
                                / b.gain_i[t]
                            )
                        return (
                            self.integral_of_error[t]
                            == (
                                b.manipulated_var[t]
                                - b.mv_ref[t]
                                - b.gain_p[t] * b.error[t]
                                - b.gain_d[t] * b.derivative_of_error[t]
                            )
                            / b.gain_i[t]
                        )
                    return pyo.Constraint.Skip
                return b.error[t] == b.integral_of_error_dot[t]

However, in the documentation for Pyomo DAE simulators, we find:

Notice that the initial conditions are set by fixing the values of m.omega and m.theta at t=0 instead of being specified as extra equality constraints. Also notice that the differential equations are specified without using Constraint.Skip to skip enforcement at t=0. The Simulator cannot simulate any constraints that contain if-statements in their construction rules.

Previous conversations have specified that the prohibition on if-statements extends only to if-then statements conditioned on time.

Nevertheless, this is a potential problem for compatibility with Pyomo simulators, and we need to figure out the best way to address it. The method Pyomo would rather have us use is to deactivate constraints at the first time point and fix the corresponding capacity variables. Do we want to do that automatically for IDAES models with dynamic=True?

dallan-keylogic avatar Apr 07 '22 16:04 dallan-keylogic

@blnicho or @jsiirola I'm going to fix this, but want to make sure I understand the requirements. I think the answer here is to move this optional constraint for the initial condition of integral error to be a non-time indexed constraint. The initial condition for integral is either fixed or has a constraint, and it's all good. What's the right way to handle initial condition calculation? Do we break off initial conditions to be solved first like we do with the PETSc integrator? Then the integrator can go with the initial condition values in place? Maybe I can just fix the constraint and we can pin down the procedure later?

eslickj avatar Apr 07 '22 19:04 eslickj

I fixed the PID controller. I couldn't find other instances in the IDAES-PSE repo. I think we can close this?

eslickj avatar Apr 12 '22 14:04 eslickj

What's the right way to handle initial condition calculation? Do we break off initial conditions to be solved first like we do with the PETSc integrator? Then the integrator can go with the initial condition values in place? Maybe I can just fix the constraint and we can pin down the procedure later?

@eslickj for the Pyomo.DAE Simulator we only require initial conditions for the differential states, the DAE integrator in CasADi will solve for consistent initial conditions for the algebraic variables. But I have seen a case where it was unable to find consistent initial conditions and it was necessary to initialize the initial conditions for the algebraic variables away from zero in order to get the integrator to work. If you already have a routine for solving for the initial conditions for PETSc then I think we should use it in most cases to give the integrator the best chance. The Simulator in Pyomo.DAE reads the initial conditions from the variable values at time point 0 by default but there is also a way to pass in the initial conditions as a keyword argument. The Simulator will not identify initial conditions specified as constraints as documented in the note @dallan-keylogic pasted above.

blnicho avatar Apr 20 '22 15:04 blnicho

@blnicho, thanks. We can definitely break the initial condition calculation step out of the PETSc utility and use it as a more general method if it makes sense. At least it's good for consistency, so regardless of integrator choice the basic procedure can be the same for IDAES. If I can be of assistance, I'm happy to help.

eslickj avatar Apr 25 '22 18:04 eslickj

I'd also like to put in a request for a function to get the block which is solved for the initial condition problem. Doing an SVD on the Jacobian of that block can be very useful for figuring out when you've formulated your initial condition problem incorrectly.

dallan-keylogic avatar Apr 25 '22 19:04 dallan-keylogic

@dallan-keylogic Has this issue been fixed, or are there still lingering issues that need to be addressed (and if so should they become new, more targeted issues and this one closed)?

andrewlee94 avatar Oct 31 '22 17:10 andrewlee94

@andrewlee94 This particular issue has been addressed. There's still lingering confusion about how the initial timepoint should be treated in dynamic models, though. Nothing concrete enough to open an issue, though

dallan-keylogic avatar Oct 31 '22 18:10 dallan-keylogic