5

I have got a specific question with regards to algebraic / implicit loops handling by Gekko.

I will give examples in the field of Chemical Engineering, as this is how I found the project and its other libraries.

For example, when it comes to multicomponent chemical equilibrium calculations, it is not possible to explicitly work out the equations, because the concentration of one specie may be present in many different equations.

I have been using other paid software in the past and it would automatically propose a resolution procedure based on how the system is solvable (by analyzing dependency and creating automatic algebraic loops).

My question would be:

Does Gekko do that automatically?

It is a little bit tricky because sometimes one needs to add tear variables and iterate from a good starting value.

I know this message may be a little bit abstract, but I am trying to decide which software to use for my work and this is a pragmatic bottle neck that I have happened to find.

Thanks in advance for your valuable insight.

1 Answers1

1

Python Gekko uses a simultaneous solution strategy so that all units are solved together instead of sequentially. Therefore, tear variables are not needed but large flowsheet problems with recycle can be difficult to converge to a feasible solution. Below are three methods that are in Python Gekko to assist in efficient solutions and initialization.

Method 1: Intermediate Variables

Intermediate variables are useful to decrease the complexity of the model. In many models, the temporary variables outnumber the regular variables. This model reduction often aides the solver in finding a solution by reducing the problem size. Intermediate variables are declared with m.Intermediates() in Python Gekko. The intermediate variables may be defined in one section or in multiple declarations throughout the model. Intermediate variables are parsed sequentially, from top to bottom. To avoid inadvertent overwrites, intermediate variable can be defined once. In the case of intermediate variables, the order of declaration is critical. If an intermediate is used before the definition, an error reports that there is an uninitialized value. Here is additional information on Intermediates with an example problem.

Method 2: Lower Block Triangular Decomposition

For large problems that have trouble with initialization, there is a mode that is activated with the option m.options.COLDSTART=2. This mode performs a lower block triangular decomposition to automatically identify independent blocks that are then solved independently and sequentially.

Lower Block Triangular

This decomposition method for initialization is discussed in the PhD dissertation (chapter 2) of Mostafa Safdarnejad or also in Safdarnejad, S.M., Hedengren, J.D., Lewis, N.R., Haseltine, E., Initialization Strategies for Optimization of Dynamic Systems, Computers and Chemical Engineering, 2015, Vol. 78, pp. 39-50, DOI: 10.1016/j.compchemeng.2015.04.016.

Method 3: Automatic Model Reduction

Model reduction requires more pre-processing time but can help to significantly reduce the solver time. There is additional documentation on m.options.REDUCE.

Overall Strategy for Initialization

The overall strategy that we use for initializing hard problems, such as flowsheets with recycle, is shown in this flowchart.

Initialization Flowchart

Sometimes it does mean breaking recycles to get an initialized solution. Other times, the initialization strategies detailed above work well and no model rearrangement is necessary. The advantage of working with a simultaneous solution strategy is degree of freedom swapping such as downstream variables can be fixed and upstream variables calculated to meet that value.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • 1
    I now understand the answer you kindly gave me is oriented towards optimization. Now I have a way to go for finding the optimal solution or performing a regression. What would it happen in the case of pure simulation of a DAE system? For example, ODEINT integrates ordinary differential equations by applying a RK method. In the case of DAEs, both time dependent variables and implicit loops will have to be solved altogether. It is something mathematically complex and deep, that may be helpful to other users. Thanks again. – Enrique Garcia Franco Nov 16 '19 at 14:21
  • Gekko solves coupled differential together with algebraic equations because the differential equations are converted to algebraic equations with orthogonal collocation on finite elements. Once the differential equations are algebraic equations, all of them are solved together with a Nonlinear Programming or Mixed Integer Nonlinear Programming solver (if you have integer variables). More information is here: https://apmonitor.com/do/index.php/Main/OrthogonalCollocation – John Hedengren Nov 17 '19 at 13:24
  • 1
    I see. It is a smart way to work out the algebraic loops while performing integration. May I ask you what are the pros and cons of Gekko (Orthogonal Colllocation) as compared to integrating with CasADi (IDAS solver). Both of them seem to handle implicit loops, from what I could know. Gekko allows straightforward equation input, while CasADI seems to require complex syntax writing. CasADI would require an additional optimizer (for example IPOPT). Thanks again! – Enrique Garcia Franco Nov 19 '19 at 14:57
  • CasADi is a good package as well. Here are some of the latest developments that Joel discussed about CasADi on an APMonitor webinar: https://youtu.be/DvicSVRhVxQ Here are 18 demo applications with Gekko: https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization Both packages are good and provide many of the same features. – John Hedengren Nov 19 '19 at 15:00
  • 1
    I see. I guess I would rather have flexibility and implicit loops automatic handling. So, I will choose APM, as Gekko seems to only have ODEs. This thread was really insightful. Thanks. – Enrique Garcia Franco Nov 21 '19 at 09:17
  • Gekko should be able to do everything that APM Python does, but you are welcome to use either. – John Hedengren Nov 21 '19 at 18:29
  • According to the following link, Gekko's IMODES 1,2 and 3 only simulate ODEs systems: https://gekko.readthedocs.io/en/latest/imode.html I would prefer to implement the equations rather than working out to make them explicit. See link: https://apmonitor.com/wiki/index.php/Main/UsersGroup I am not sure I am using the right terminology. Maybe with these two links, I could be more precise. Thanks in advance for your time. Enrique Garcia-Franco – Enrique Garcia Franco Nov 24 '19 at 08:41
  • The documentation was incorrect for IMODES=1-3. DAEs of any index are supported. I updated the documentation: https://gekko.readthedocs.io/en/latest/imode.html Thanks for pointing that out. – John Hedengren Nov 24 '19 at 13:42