Use of Connectors VI – Dynamic System

This tutorial demonstrates how to use connectors for a dynamic system. This could, for example, be necessary because you want to use a dynamic sub-model of a colleague. Here, we want to use the sub-system of a PI controller. If we did not use a connector for the time variable, MOSAICmodeling would issue a warning because we had two different time variables. MOSAICmodeling would interpret this as a partial differential equation, which can currently not be solved.

Model description

This example is based on the article by Asprey and Macchietto (2002), DOI: 10.1016/S0959-1524(01)00020-8. It features two differential equations representing the concentrations of the components as well as an expression for the reaction rate:

   \frac{\mathrm{d}x^\mathrm{B}}{\mathrm{d}t} = \left( r - u - \theta_{i=4} \right) x^\mathrm{B} \\[2ex] \frac{\mathrm{d}x^\mathrm{S}}{\mathrm{d}t} = - \frac{r x^\mathrm{B}}{\theta_{i=3}} + u \left( x_\mathrm{F}^\mathrm{B} - x^\mathrm{S} \right) \\[2ex] r = \frac{\theta_{i=1} x^\mathrm{S}}{\theta_{i=2} + x^\mathrm{S}}.

In addition, we want to add a PI controller to this system. This PI controller consists of the following generic equations, which have to be matched to the system above via a connector:

   u = K_\mathrm{P} e_\mathrm{P} + K_\mathrm{I} e_\mathrm{I} + u^\mathrm{set} \\[2ex] e_\mathrm{P} = y^\mathrm{set} - y \\[2ex] \frac{\mathrm{d}e_\mathrm{I}}{\mathrm{d}t} = e_\mathrm{P}.

Note that the u in the PI equations is the same u in the model equations above.

Modeling workflow

In the following, we show the workflow for setting up the model with both open loop and closed loop. We start by defining the notation for this example. Then, we define the notation of the controller equations.

Notation of model

Set up a notation with the following base names and indices:

Base names
  • \theta, parameter in various units
  • r, reaction rate in 1/h
  • t, time in h
  • u, dilution factor in 1/h
  • x, molar concentration in g/L
Superscripts
  • B, biomass
  • S, substate
  • SP, setpoint
Subscripts
  • F, feed
Indices
  • i, parameter index 1…NI

The resulting notation has ID 2323.

Notation of controller

Base names
  • e, control deviation
  • K, gain factor
  • t, time in h
  • u, manipulated variable
  • y, control variable
Superscripts
  • set, setpoint
Subscripts
  • I, integral part
  • P, proportional part

The resulting notation has ID 186460.

Equations of the model

Go to the Equation tab and perform the following steps:

  1. Load the model notation set up above
  2. Enter the model equations

The model equations have IDs 2324, 2325, and 2326.

Equations of the controller

Go to the Equation tab and perform the following steps:

  1. Load the controller notation set up above
  2. Enter the model equations

The model equations have IDs 186459, 186461, and 186462.

Connectors

To connect these two equation systems, we will need a connector. One could suggest to simply use the naming policy integrate as this would create the same namespace for all variables and there would only be one variable t. However,  the two models could also use completely different variable names for time, e.g., t and z in a more generic case. In this case here, we also need to match the controlled variables: y in the generic model of the PI controller and x^\mathrm{B} in the model equations. For this purpose, go to the Connector tab and do the following:

  1. Add a helpful description for the connector
  2. Activate the tab Edit Matching and select the controller equation as Sub Notation and the model notation of this example as Super Notation
  3. Generate the missing variables for the Sub Notation and the Super Notation, i.e.,
    • Sub Notation: t, u, y, y^\mathrm{set}
    • Super Notation: t, u, x^\mathrm{B}, x^\mathrm{B,SP}
  4. Select the analogous variables and click on Match to achieve the following matching:
    • t \rightarrow t
    • u \rightarrow u
    • y \rightarrow x^\mathrm{B}
    • y^\mathrm{set} \rightarrow x^\mathrm{B,SP}
  5. Save the connector

The connector has ID 2351.

Equation system for open loop

To construct this equation systems, go to the Equation System tab and take the following steps.

  1. Load the model notation and add a reasonable description
  2. Click on “Add EQU/EQS” and add the three model equations
  3. Make sure that the Naming policy is integrate
  4. Save the equation system

Equation system for PI controller

To construct this equation systems, go to the Equation System tab and take the following steps.

  1. Load the controller notation and add a reasonable description
  2. Click on “Add EQU/EQS” and add the three controller equations
  3. Make sure that the Naming policy is integrate
  4. Save the equation system

Equation system for closed loop

We now combine the two previous equation systems:

  1. Load the model notation and add a reasonable description
  2. Click on “Add EQU/EQS” and add the equation system of the model
  3. Make sure that the Naming policy is integrate
  4. Click again on “Add EQU/EQS” and add the equation system of the controller; select encapsulate as naming policy and select the connector from above; then confirm
  5. Save the equation system

The naming policy encapsulate is chosen arbitrarily in this case and the policy integrate would also work. In general, we recommend encapsulate, though, as you avoid undesired matches between variables in your individual sub-models.

The equation system for open loop is available with ID 2334, the equation system of the controller has ID 186463, and the equation system for closed loop has ID 2352.

Simulation workflow

Equation system and Indexing

Equation system for open loop

Go to the “Simulation” section and do the following:

  1. Load your equation system for the open loop in the tab Equation System
  2. Set the maximum value NI to 4 in the tab Indexing
Equation system for closed loop

Go to the “Simulation” section and do the following:

  1. Load your equation system for the closed loop in the tab Equation System
  2. Set the maximum value NI to 4 in the tab Indexing

Initialization and results

Equation system for open loop

Go to the tab Specifications and open the Variables tab. To initialize and specify the model, take the following steps:

  1. Initialize this example with the design values and initial guesses given in Table 1
  2. Save the variable specification
  3. Save the simulation
  4. Go to the Evaluation tab and generate the code for your preferred environment
  5. Solve the system using the generated code

This simulation is available with ID 2335 with variable specification 2334.

Equation system for closed loop

Go to the tab Specifications and open the Variables tab. To initialize and specify the model, take the following steps:

  1. Initialize this example with the design values and initial guesses given in Table 2
  2. Save the variable specification
  3. Save the simulation
  4. Go to the Evaluation tab and generate the code for your preferred environment
  5. Solve the system using the generated code

This simulation is available with ID 2354 with variable specification 2353.

When you simulate the system with open loop and closed loop, you can observe the impact of the controller on the model performance.

NameDescriptionValue / Initial condition / Initial guess
\theta_{i=1}Parameter 10.325
\theta_{i=2}Parameter 20.267
\theta_{i=3}Parameter 30.559
\theta_{i=4}Parameter 40.055
rReaction rate0.01
tTime, independent variable0.0
uControl0.2
x^\mathrm{B}Mass concentration of biomass1.0
x^\mathrm{S}Mass concentration of substrate0.01
x_\mathrm{F}^\mathrm{S}Mass concentration of substrate in feed30.0
Table 1: Overview of design values, initial conditions, and initial guesses for the open loop.

NameDescriptionValue / Initial condition / Initial guess
\theta_{i=1}Parameter 10.325
\theta_{i=2}Parameter 20.267
\theta_{i=3}Parameter 30.559
\theta_{i=4}Parameter 40.055
K_\mathrm{I}Gain factor of integral controller0.001
K_\mathrm{P}Gain factor of proportional controller0.001
e_\mathrm{I}Control deviation for integral controller0.0
e_\mathrm{P}Control deviation for proportional controller0.0
rReaction rate0.01
tTime, independent variable0.0
uControl0.2
u^\mathrm{set}Feedforward control value0.0
x^\mathrm{B}Mass concentration of biomass1.0
x^\mathrm{B,SP}Controller setpoint12.0
x^\mathrm{S}Mass concentration of substrate0.01
x_\mathrm{F}^\mathrm{S}Mass concentration of substrate in feed30.0
Table 2: Overview of design values, initial conditions, and initial guesses for the closed loop.

Use of functions

If you prefer to formulate an ODE system in which certain algebraic variables are calculated via functions, you can do so. For this example, the reaction rate was also formulated as function. In this case, the parameters 1 and 2 must be added to a parameter list (ID: 189115). This parameter list is then used in the function because non-generic indices cannot appear in the function body (ID: 189116). Finally, the function replaces the algebraic equation for the reaction rate in a new equation system (ID: 189114). This equation system is used in a new simulation with ID 189119, using the variable specification with ID 189117 and the parameter specification with ID 189118. The values are the same as given in Table 1. The results with be the same, but the system is now used with an ODE solver instead of a DAE solver.