Modelica® Language Specification version 3.6

Appendix B Modelica DAE Representation

In this appendix, the mapping of a Modelica model into an appropriate mathematical description form is discussed.

In a first step, a Modelica translator transforms a hierarchical Modelica simulation model into a “flat” set of Modelica “statements”, consisting of the equation and algorithm sections of all used components by:

  • Expanding all class definitions (flattening the inheritance tree) and adding the equations and assignment statements of the expanded classes for every instance of the model.

  • Replacing all connect-equations by the corresponding equations of the connection set (see section 9.2).

  • Mapping all algorithm sections to equation sets.

  • Mapping all when-clauses to equation sets (see section 8.3.5).

As a result of this transformation process, a set of equations is obtained consisting of differential, algebraic and discrete equations of the following form where (v:=[p;t;x˙;x;y;z;m;𝚙𝚛𝚎(z);𝚙𝚛𝚎(m)]):

0=fx(v,c) (B.1a)
z={fz(v,c)at events𝚙𝚛𝚎(z)otherwise (B.1b)
m:=fm(v,c) (B.1c)
c:=fc(𝑟𝑒𝑙𝑎𝑡𝑖𝑜𝑛(v)) (B.1d)

and where

  • p: Modelica variables declared as parameter or constant, i.e., variables without any time-dependency.

  • t: Modelica variable time, the independent (real) variable.

  • x(t): Modelica variables of type Real, appearing differentiated.

  • y(t): Continuous-time modelica variables of type Real that do not appear differentiated (= algebraic variables).

  • z(te): Discrete-time modelica variables of type Real. These variables change their value only at event instants te. pre(z) are the values of z immediately before the current event occurred.

  • m(te): Modelica variables of discrete-valued types (Boolean, Integer, etc) which are unknown. These variables change their value only at event instants te. pre(m) are the values of m immediately before the current event occurred.

    [For equations in when-clauses with discrete-valued variables on the left-hand side, the form (B.1c) relies upon the conceptual rewriting of equations described in section 8.3.5.1.]

  • c(te): The conditions of all if-expressions generated including when-clauses after conversion, see section 8.3.5).

  • 𝑟𝑒𝑙𝑎𝑡𝑖𝑜𝑛(v): A relation containing variables vi, e.g., v1>v2, v30.

For simplicity, the special cases of noEvent and reinit are not contained in the equations above and are not discussed below.

The key difference between the two groups of discrete-time variables z and and m here is how they are determined. The interpretation of the solved form of (B.1c) is that given values for everything else, there is a closed-form solution for m in the form of a sequence of assignments to each of the variables of m in turn – there must be no cyclic dependencies between the equations used to solve for m. Further, each of the original model equations behind (B.1c) must be given in solved form, at most requiring flipping sides of the equation to obtain the used assignment form. The interpretation of the non-solved form of (B.1b) at events, on the other hand, is that at events, the discrete-time Real variables z are solved together with the continuous-time variables using (B.1a) and (B.1b).

[Example: The following model demonstrates that equation B.1b does not imply that all discrete-time Real variables are given by equations in solved form, as also the discrete-time Real variables are included in z:

model M
  discrete Real x(start = 1.0, fixed = true);
equation
  when sample(1.0, 1.0) then
    x = 3 * pre(x) - x^2; // Valid equation for discrete-time Real variable x.
  end when;
end M;

Another way that a discrete-time Real variable can end up becoming determined by a nonlinear equation is through coupling with other variables.

model M
  discrete Real x(start = 1.0, fixed = true);
  discrete Real y(start = 0.0, fixed = true);
equation
  when sample(1.0, 1.0) then
    y = x ^ 2 + 2 * exp(-time);
    x = 3 * pre(x) - y; // OK, forming nonlinear equation system with y.
  end when;
end M;

]

[Example: The following model is illegal since there is no equation in solved form that can be used in (B.1c) to solve for the discrete-valued variable y:

model M
  Boolean x;
  Boolean y;
equation
  x = time >= 1.0;
  not y = x; /* Equation in solved form, but not with respect to y. */
end M;

]

The generated set of equations is used for simulation and other analysis activities. Simulation proceeds as follows. First, initialization takes place, during which initial values for the states x are found, section 8.6. Given those initial values the equations are simulated forward in time; this is the transient analysis. The equations define a DAE (Differential Algebraic Equations) which may have discontinuities, a variable structure and/or which are controlled by a discrete-event system. Such types of systems are called hybrid DAEs. After initialization, simulation proceeds with transient analysis in the following way:

  1. 1.

    The DAE (B.1a) is solved by a numerical integration method. In this phase the conditions c of the if- and when-clauses, as well as the discrete-time variables z and m are kept constant. Therefore, (B.1a) is a continuous function of continuous variables and the most basic requirement of numerical integrators is fulfilled.

  2. 2.

    During integration, all relations from (B.1d) are monitored. If one of the relations changes its value an event is triggered, i.e., the exact time instant of the change is determined and the integration is halted. As discussed in section 8.5, relations which depend only on time are usually treated in a special way, because this allows determining the time instant of the next event in advance.

  3. 3.

    At an event instant, (B.1d) is a mixed set of algebraic equations which is solved for the Real, Boolean and Integer unknowns.

  4. 4.

    After an event is processed, the integration is restarted at phase 1.

Note, that both the values of the conditions c as well as the values of z and m (all discrete-time Real, Boolean and Integer variables) are only changed at an event instant and that these variables remain constant during continuous integration. At every event instant, new values of the discrete-time variables z and m, as well as of new initial values for the states x, are determined. The change of discrete-time variables may characterize a new structure of a DAE where elements of the state vector x are disabled. In other words, the number of state variables, algebraic variables and residue equations of a DAE may change at event instants by disabling the appropriate part of the DAE. For clarity of the equations, this is not explicitly shown by an additional index in (B.1d).

At an event instant, including the initial event, the model equations are reinitialized according to the following iteration procedure:

known  variables: x, t, p
unkown variables: dx/dt, y, z, m, pre(z), pre(m), c
// pre(z) = value of z before event occured
// pre(m) = value of m before event occured
loop
  solve (1) for the unknowns, with pre(z) and pre(m) fixed
  if z == pre(z) and m == pre(m) then break
  pre(z) := z
  pre(m) := m
end loop

Clocked variables are handled similarly as z and m (depending on type), but using previous instead of pre and only solved in the first event iteration.

Solving (B.1d) for the unknowns is non-trivial, because this set of equations contains not only Real, but also discrete-valued unknowns. Usually, in a first step these equations are sorted and in many cases the discrete-valued unknowns m can be just computed by a forward evaluation sequence. In some cases, there remain systems of equations involving m due to cyclic dependencies with y and z (e.g., for ideal diodes, Coulomb friction elements), and specialized algorithms have to be used to solve them.

Due to the construction of the equations by flattening a Modelica model, the hybrid DAE (B.1d) contains a huge number of sparse equations. Therefore, direct simulation of (B.1d) requires sparse matrix methods. However, solving this initial set of equations directly with a numerical method is both unreliable and inefficient. One reason is that many Modelica models, like the mechanical ones, have a DAE index of 2 or 3, i.e., the overall number of states of the model is less than the sum of the states of the sub-components. In such a case, every direct numerical method has the difficulty that the numerical condition becomes worse, if the integrator step size is reduced and that a step size of zero leads to a singularity. Another problem is the handling of idealized elements, such as ideal diodes or Coulomb friction. These elements lead to mixed systems of equations having both Real and Boolean unknowns. Specialized algorithms are needed to solve such systems.

To summarize, symbolic transformation techniques are needed to transform (B.1d) into a set of equations which can be numerically solved reliably. Most important, the algorithm of Pantelides should to be applied to differentiate certain parts of the equations in order to reduce the index. Note, that also explicit integration methods, such as Runge-Kutta algorithms, can be used to solve (B.1a), after the index of (B.1a) has been reduced by the Pantelides algorithm: During continuous integration, the integrator provides x and t. Then, (B.1a) is a linear or nonlinear system of equations to compute the algebraic variables y and the state derivatives dxdt and the model returns dxdt to the integrator by solving these systems of equations. Often, (B.1a) is just a linear system of equations in these unknowns, so that the solution is straightforward. This procedure is especially useful for real-time simulation where usually explicit one-step methods are used.