The simulation in Dymola does not run, stalls, is slow or even aborts? - A common cause: nonlinear systems of equations. Learn how to avoid them to save simulation time and make your simulation model numerically robust.

From our experience, we recommend that it is best to avoid nonlinear systems of equations in Dymola. This usually saves simulation time and makes the simulation model more numerically robust. Therefore, in this article we will show you what nonlinear systems of equations are in Dymola, how to recognize them, and how to solve or eliminate them.

Strictly speaking, the term "nonlinear systems of equations" used in Dymola is somewhat misleading; mathematically more correct is the term: "implicit algebraic systems of equations". To understand exactly how the "nonlinear" equations arise and why they are a problem, we should first look at what types of equation systems we consider in Dymola and how Dymola solves equation systems in general.

The easiest system of equations to solve is the system of ordinary differential equations (ODE):

$$\dot{x} = F(x)$$

Here \(\dot{x}\) is called the differential state. An ODE can always be solved explicitly for these differential states.

An ODE solver, such as an Euler algorithm, can then be used to solve the system of equations in Dymola. As simplified in Figure 1, starting from a known state \(x_0\) and the ODE system of equations, Dymola computes the time derivatives of all differential states and then performs integration to the state \(x_1\) at the next time step.

In general, when a system of equations contains algebraic equations, this system of algebraic differential equations (DAE) can no longer be solved explicitly for the differential states:

$$\dot{x} = F(x, y)$$

$$0 = G(x, y)$$

Here we distinguish between the differential states \(\dot{x}\) and the algebraic states \(y\).

These DAEs cannot usually be solved by the ODE solver alone. For this reason, Dymola uses an additional algebraic equation solver (AE solver) by default. This can consist of a Newton algorithm, for example. The step-by-step procedure for solving DAE systems in Dymola is shown in simplified form in Figure 2:

The solution of a DAE system also assumes a known initial state \(x_0\). In the first step, however, the algebraic systems of equations are solved using an AE solver (1). For example, the algebraic states can be computed by iterations using Newton's algorithm. In Dymola, the algebraic states to be iterated are also called iteration variables. Subsequently, the algebraic states \(y_0\) can be passed to the ODE solver together with the differential equation systems and solved (2).

The comparison between the solution of ODEs and DAEs in Dymola makes clear that nonlinear equations (or implicit algebraic systems of equations) cause additional computational effort and thus have a negative impact on the simulation time. Furthermore, the solution of the algebraic systems of equations can trigger additional problems such as the non-convergence of iterations.

Dymola offers the possibility to solve the DAE directly without the upstream AE solver using the flag **Advanced.Define.DAESolver**. For example, the Dassl algorithm (which is used by default in Dymola as a pure ODE solver) is suitable for this. When the DAE solver is enabled, the nonlinear equations are treated by the integrator as part of the sliding system, which the integrator solves at each time step. If the translated model contains large nonlinear systems of equations, DAE solvers can be more efficient.

Let us consider the following example for illustration (see Figure 3): The simulation model in the following figure consists of a series connection of a fan and a pipe as well as source and sink, which define the pressure. In the pipe, the pressure losses are calculated as a function of the mass flow and the fan defines the volume flow, which is varied in time with the help of a ramp.

After performing the translation, we analyze the "statistics" of the simulation model:

Here Dymola displays both the number of nonlinear equation systems before and after sorting and optimizing the equation system. By sorting and optimizing the system of equations, Dymola can already eliminate some nonlinear equations. For us, however, the remaining nonlinear equation systems are important.

The length of the vector describes the number of nonlinear equation systems and the vector entries the respective number of algebraic states. With the flag **Advanced.LogNonLinearIterationVariables** Dymola additionally specifies which algebraic states should be iterated in the log window. In our example, the mass flow is the iteration variable.

In order to solve the nonlinear system of equations, we must first understand where the nonlinear system appears in the simulation model. Here it is worth taking a look at the dsmodel.mof file. The dsmodel.mof file contains the complete, optimized and sorted sliding system of the simulation model. In the simulation setup we can activate the output of the file as follows:

The file dsmodel.mof is usually placed in the working directory after translation. At the end of the section "Nonlinear systems of equations" we find the following algebraic equation:

The mass flow is therefore calculated as a function of the volume flow and the density. This makes physical sense and is actually trivial. But why does this relationship lead to a nonlinear system of equations?

A nonlinear system of equations arises whenever two or more equations are coupled together. In our example, the density depends on the pressure \(p\) and thus on the pressure drop \(\Delta p\) in the pipe, which in turn depends on the mass flow:

$$\Delta p= f(\dot{m}) $$

Thus, with these two equations, we obtain a system of algebraic equations with \(\dot{m}\) as the iteration variable.

To eliminate algebraic systems of equations, there are several possible solutions. Which method makes the most sense depends strongly on the particular problem.

**specify the iteration variable explicitly**: Sometimes nonlinear systems of equations can be solved by explicitly specifying the iteration variable. In our example, we could define the mass flow rate in the fan instead of the volume flow rate. This makes the equation \(0=\dot{V}+\dot{m}/\rho\) redundant.**introduce additional differential states:**Nonlinear systems of equations can usually always be solved with an additional differential states. In our case, introducing a volume would help (see the figure below). The volume adds the pressure as a differential states to the system and the algebraic system of equations can be solved.

**define inverse:**In some cases, a nonlinear system of equations may also result because Modelica cannot invert equations. Here it helps to specify the inverted function directly.

Whether you are an experienced model developer or a Modelica beginner, nonlinear systems of equations inevitably occur and are always a problem for dynamic simulations. If you know how nonlinear systems of equations affect your simulations, how to identify and eliminate them, you are a big step closer to get the best out of your model. We wish you a lot of fun in the process!