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 ():
: Modelica variables declared as parameter or constant, i.e., variables without any time-dependency.
: Modelica variable time, the independent (real) variable.
: Modelica variables of type Real, appearing differentiated.
: Continuous-time modelica variables of type Real that do not appear differentiated (= algebraic variables).
: Discrete-time modelica variables of type Real. These variables change their value only at event instants . pre(z) are the values of immediately before the current event occurred.
: Modelica variables of discrete-valued types (Boolean, Integer, etc) which are unknown. These variables change their value only at event instants . pre(m) are the values of 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 188.8.131.52.]
: The conditions of all if-expressions generated including when-clauses after conversion, see section 8.3.5).
: A relation containing variables , e.g., , .
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 and and 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 in the form of a sequence of assignments to each of the variables of in turn – there must be no cyclic dependencies between the equations used to solve for . 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 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 :
Another way that a discrete-time Real variable can end up becoming determined by a nonlinear equation is through coupling with other variables.
[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:
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 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:
The DAE (B.1a) is solved by a numerical integration method. In this phase the conditions of the if- and when-clauses, as well as the discrete-time variables and are kept constant. Therefore, (B.1a) is a continuous function of continuous variables and the most basic requirement of numerical integrators is fulfilled.
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.
At an event instant, (B.1d) is a mixed set of algebraic equations which is solved for the Real, Boolean and Integer unknowns.
After an event is processed, the integration is restarted at phase 1.
Note, that both the values of the conditions as well as the values of and (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 and , as well as of new initial values for the states , are determined. The change of discrete-time variables may characterize a new structure of a DAE where elements of the state vector 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:
Clocked variables are handled similarly as and (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 can be just computed by a forward evaluation sequence. In some cases, there remain systems of equations involving due to cyclic dependencies with and (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 and . Then, (B.1a) is a linear or nonlinear system of equations to compute the algebraic variables and the state derivatives and the model returns 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.