Wolfram Computation Meets Knowledge

Model Initialization

Over- and Underdetermined InitializationFixed Attribute
Start AttributeWhen Clauses and Initialization
Initial Equations and Algorithms

At the start time of a model simulation, the model needs to be initialized, which typically involves solving a larger problem than the integration problem in terms of the number of unknowns. For example, while the integration problem requires solving for the derivatives of all continuous-time state variables, the initialization problem also requires solving for the initial values of those states. Similarly for discrete-time variables, their pre values are also unknowns in the initialization problem, as are the values of parameters with fixed=false.

Some background with references to the Modelica Specification is given below. See Modelica specification for full details of how the initialization problem of a Modelica model is defined. See Troubleshooting Initialization for a tutorial-style presentation of how to deal with some common problems in model initialization.

Over- and Underdetermined Initialization

System Modeler detects three different kinds of initialization:

Start Attribute

The start attribute (an attribute of all predefined Modelica types; see Real, for instance) has two uses. It can be used to initialize a variable exactly to that value or it can be used as a guess valuefor example, when a variable is solved in a nonlinear system. You can see variable start attributes and how they are used in the Variables tab in Simulation Center.

If you do not specify the start attribute for a variable, it has a default value depending on the type (0.0 for Real, 0 for Integer and false for Boolean), but providing the attribute explicitly can have a stronger effect than relying on the default.

If your model has underdetermined initialization, there are too few equations and System Modeler has to pick some variables to be initialized from their start attributes. System Modeler does this by checking which variables can be initialized from the start, given the equation structure of the model, and then choosing the variables it considers most suitable. System Modeler considers variables that have an explicitly specified start value more suitable and considers states to be more suitable than other variables. In the model A, System Modeler will choose to initialize x to its start attribute.

model A
Real x(start = 1);
equation
der(x) = -x + 1;
end A;

When a start attribute is used as a guess value, it can help you get the solution you want. Consider the equation z2-6=z, which has two solutions, z=2 and z=3. In the models B1 and B2, you can see that different solutions are found, depending on your start setting.

model B1
Real z(start = 4);
equation
z^2 = 6 - z;
end B1;
model B2
Real z(start = -5);
equation
z^2 = 6 - z;
end B2;

Initial Equations and Algorithms

One of the fundamental ways of setting a variable's value at start time is to use an initial equation or an initial algorithm.

model C
Real x;
initial equation
x = 1;
equation
der(x) = -x + 1;
end C;

In the model C, x will always start with its value equal to 1, while in the model A, the start value of x could easily be changed by a modification or by changing the start value in the Variables tab in Simulation Center. It is not possible to change an initial equation section by a modification. An initial equation section is in some ways more powerful than the start attribute, since you can use full equations to express initial constraints. For example, in the model D, the variable x will start in steady state, since der(x)=0 is used as an initial equation.

model D
Real x;
Real y(start = 3);
initial equation
der(x) = 0;
equation
der(x) = -x + y + 1;
der(y) = -y + 2;
end D;

Fixed Attribute

The fixed attribute (an attribute of all predefined Modelica types; see Real, for instance) can be added to a variable declaration to say that the variable should be initialized exactly to its start attribute. Effectively, this is the same as adding an initial equation. However, if you use fixed=true and the start value is a literal constant, you can change it after the simulation has been built in the Variables tab in Simulation Center. The two models following, HelloWorld and HelloWorldInitialEquation, show the difference.

model HelloWorld
Real x(fixed = true, start = 1.0);
equation
der(x) = -x;
end HelloWorld;
model HelloWorldInitialEquation
Real x;
initial equation
x = 1.0;
equation
der(x) = -x;
end HelloWorldInitialEquation;

For parameters, you can set fixed=false to indicate that a parameter should be calculated from separately provided initial equations or initial algorithms. In the model HelloWorldParameter, the exponential decay constant is a parameter that is calculated at initialization; specifically, it will be two times the start value of x.

model HelloWorldParameter
Real x(start = 1.0, fixed = true);
parameter Real k(fixed = false);
initial equation
k = 2 * x;
equation
x = -k * der(x);
end HelloWorldParameter;

When Clauses and Initialization

When equations and statements have special rules when it comes to initialization. A when clause is only active during initialization if it has initial() as one of its conditions. Therefore, for when equations and statements without initial(), it can be necessary to add an initial equation for the variable assigned.

model WhenExample
Integer x;
equation
when sample(2.01, 5.0) then
x = 1;
elsewhen sample(0.5, 0.1) then
x = pre(x) + 1;
end when;
end WhenExample;

In the model WhenExample, System Modeler points out that initialization is underdetermined and that it has chosen x to be initialized from its start (having the default value of 0 for an Integer).

model WhenExampleInitial
Integer x;
equation
when {initial(), sample(2.01, 5.0)} then
x = 1;
elsewhen sample(0.5, 0.1) then
x = pre(x) + 1;
end when;
end WhenExampleInitial;

In the model WhenExampleInitial, the underdetermined initialization has been fixed by adding initial() to the first when condition list. It could also have been fixed by adding an initial equation or adding fixed=true to the declaration of x.