﻿ 20-sim webhelp > Editor > Compiling > Order of Execution

# Order of Execution

Equations within 20-sim may be entered in random form. During compilation, 20-sim will automatically try to rewrite equations into a correct order of execution. I.e. a form where all output variables are written as function of input variables and output variables of previous lines. Consider for example the following equations:

variables

real u,z;

equations

z = sin(u);

u = cos(time);

Here the (input) variable z is given as a function of u. Consequently u should be calculated first. This is exactly what 20-sim will try to do while compiling the model into simulation code. I.e. the equations will be executed as:

u = cos(time);

z = sin(u);

## Code Blocks

Equations in a code block are not reordered. A code block is a set of equations inside a statement. Suppose we have the following equations:

if condition then

code block 1

...

...

else

code block 2

...

...

end;

To prevent incorrect executions of the if-statement, the equations of the code blocks will not be separated. Inside a code-block, equations can are not rewritten into an new order of execution. E.g. the following equations:

if time > 10 then

z = sin(u);

a = z^2;

u = cos(time);

end;

Will be not be reordered and therefore not correctly executed! To get correct code, enter code blocks in a correct order, e.g.:

if time > 10 then

u = cos(time);

z = sin(u);

a = z^2;

end;

## Prevent Order Changes

To make all equations a code block you can use the code section. E.g.

parameters

real y = 8;

variables

real x1, x2, x3;

code

x1 = time;

x2 = sin(time);

x3 = y*x1;

## Integration Steps

Some integration algorithms do more calculations before generating the next output value. These calculations are called minor steps, the output generation is called a major step. During a minor step, all model equations are executed. In most cases you will not notice this because only the results of the major step are shown in the simulator. There are however, exceptions. The next topic will discuss this in more detail.