Expert Insights

Introduction to the model translation and symbolic processing - Claytex

Written by Garron Fish | Nov 21, 2017 10:00:00 AM

Introduction

This blog entry describes some of the model translation process, focusing on the symbolic processing that is performed.

The symbolic processing of equations is applied to the complete set of equations for the model. This "flat" form of the Modelica model is extracted in the first step of the translation process, an example of a simple electrical model is shown in Figure 1.

Figure 1: Simple electrical circuit with Modelica code extracted

In the translation procedure this flat code is translated into procedural code that serves to facilitate function evaluations to be called as the software works through each time step. This process is summarised below (see Dymola User Manual Volume 2 Section 8.8 and Section 8.9 for a more detailed description of this process). The process however does not limit itself to extracting the equations and putting them in a procedural form.

Assessment of problem structure by Dymola

"Structure Jacobian"

From a model's Modelica flat code (as in Figure 1) a matrix that describes the dependency of each equation on its variables & their derivatives (treated as further variables) is created. The matrix results from each equation being assigned a row and each variable being assigned a column. If an equation makes use of a certain variable then the relevant entry is assigned the value 1 (for example in Figure 2). The only possible values for entries are zero, and unity. This matrix is referred to as the "Structure Jacobian".

Figure 2: "Structure Jacobian" for a mechanical system. Each row is an equation and each column is a variable.

Image from Dymola User Manual Volume 2.

The "Structure Jacobian" describes the dependency of the equations on the variables; the name "Structure Jacobian" is misleading as it is not a Jacobian, see note at end of post for further details.

The aim is to rearrange the "Structure Jacobian" so that working downwards, each term can be solved for. This rearrangement essentially represents the automated writing of instructions for the computer.

Removing constants and alias reduction

To improve the computational efficiency of the solver, the system of equations is simplified as much as possible. Two simplification methods are applied, with a view to removing constants and "alias" (essentially "duplicate") variables by substituting at translation.

Firstly, from examining the "Structure Jacobian" it is possible to determine which variables are really constants. This automates the process a person might use to spot that some "variables" for a particular set of boundary conditions or in a particular model configuration, do not vary in time, simplifying the specific solution of the system for that set of parameters & initial conditions. Some software would waste time "calculating" these variables at each time-step. This doesn't necessarily take long, but it adds up, and blocks up spaces in the memory, which for large systems is very damaging. Dymola avoids it.

Secondly, by examining rows in the "Structure Jacobian" that contain only two variables it is possible to determine which variables may be alias or the negated alias (i.e. b = a or c = -a). The alias variables are removed, for example in Figure 3. This automates the process a person might use to spot quantities that are identically equal, so they don't end up solving for the same thing twice, needlessly increasing the work. By doing this algorithmically, Dymola lets it happen for potentially hundreds of thousands of equations, so that problems can use the computational speed & efficiency of a computer, while not losing the common-sense of a person.

Figure 3: "Structure Jacobian" example with alias equations removed. The number of unknowns is reduced from about 1200 to 330. Image from Dymola User Manual Volume 2.

A significant reduction in complexity is typically achieved by alias reduction.

DAE to ODE

The Modelica flat code can describe a mixed system of Differential-Algebraic Equations (DAEs - https://en.wikipedia.org/wiki/Differential_algebraic_equation). However DAE solvers are typically slow and not robust with regards to initialisation, so the system is generally transformed into a set of Ordinary Differential Equations (ODEs), to take advantage of a wider variety of numerical solvers.

This transformation is called Index Reduction and a key part of this process is selection of states. The process followed by Dymola supports "dynamic states", and is described in https://www.modelica.org/events/workshop2000/proceedings/old/Mattsson.pdf. This means that the variables that are considered states in the resulting set of ODEs can be selected dynamically during simulation.

The term "state" is used in state-space modelling, and in principle means "variables chosen to be the coordinates". For example, let's imagine you are calculating some dynamics in cartesian space, and then you notice it would be easier or better to use polar coordinates. By changing to doing the equations in a new set of coordinates you are changing the chosen "states" that (fully) describe the configuration of the dynamical system (consider looking at http://web.mit.edu/2.14/www/Handouts/StateSpace.pdf or similar material on state-space modelling).

When Dymola uses static states, Dymola takes a system where the cartesian and polar coordinates are both defined, and chooses which set of variables (polar or cartesian) should be used as the coordinates, and which should just be back-calculated after the fact. This can have a huge effect on the computational efficiency, and is a powerful capability that aids analysis.

The use of dynamic states means that Dymola not only chooses states at the beginning, but continues to re-evaluate whether it should stick to the initial choice, or change, during the analysis! This is similar to how you might solve a motion problem piecemeal, with cartesian coordinates for one part, then switch to polar for another segment which lends itself better to that.

Note that dynamic state selection is typically turned off & requires careful management by a skilled user. Discussion of how these can be used for MultiBody systems can be found here: Understanding State Selection in Multibody Simulation.

Block lower triangle partitioning

In the "ideal" case the Structure Jacobian can be rearranged into a Lower Triangular Matrix. Variables can then be solved for one by one. In general, this is not the case however, and "algebraic loops" (i.e. "coupled equations") are present in the model. In this case, the Structure Jacobian cannot be restructured as a Lower Triangular Matrix but can be made to approach that state, laid out as in the example in Figure 4.

https://resources.technia.com/hubfs/Imported_Blog_Media/StructureJacobianBLT.png" width="447" height="432" alt="Structure Jacobian arranged as block lower triangular matrix" style="max-width:100%; height:auto;">

Figure 4: "structure" Jacobian restructured as Block Lower Triangular Matrix

The blocks along the diagonal of the BLT structure represent algebraic loops...

Written by: Garron Fish - Chief Engineer & Sas Harrison - Systems Engineer