This blog post is a continuation from a previous post on the Modelica Language. This blog post focuses on the syntax used in equations and algorithms and the difference between the two.

## Calculations

#### Equation Sections

Models and blocks primarily use an equation section to define the calculations of the class. This section is started with a definition “**equation”** following which variables can be constrained. A calculation can be either side of the equals sign, as equations are used to constrain equality. Connections are also defined in this section. Every line of an equation must be ended with a **semicolon**.

```
equation
y = k1*u1 + k2*u2;
u1/u2 = z;
```

Unlike a procedural code, the order in which they are defined does not matter, as Dymola’s symbolic manipulation will rearrange the equations at compilation. As long as all variables have been constrained but not over constrained.

#### Algorithm Sections

**Algorithms** are procedural versions of equations, meaning the order is maintained when simulating. In **algorithm** sections the variables must be on the left of the equality sign, with the calculation on the right. Instead of a single equals sign a **colon** followed by an **equals** sign is used.

```
algorithm
y := k1*u1 + k2*u2;
z := u1/u2;
```

#### Initial Equation/Algorithm sections

Classes can have initial equation/algorithm sections. These sections follow the same rules as before but are run during initialisation.

#### Mathematical and Equality Operators

##### Real

These are the operators that are primarily used on Real or Integer variables

a+b |
– | Addition |

a-b |
– | Subtraction |

a*b |
– | Multiplication |

a/b |
– | Division |

a^b |
– | Raise to the power, i.e. 2^2=4 |

##### Boolean

These qualities produce a Boolean results

a==b |
– | Compare variables for equality. Cannot be used to compare Real variables. |

a-b |
– | Compare a to see if it is less than b. |

a=b |
– | Compare a to see if it is less than or equal to b. Cannot be used to compare Real variables. |

a>b |
– | Compare a to see if it is greater than b. |

a>=b |
– | Compare a to see if it is greater than or equal to b. Cannot be used to compare Real variables. |

a<>b |
– | Compare variables for inequality. Cannot be used to compare Real variables. |

A and B |
– | true if both A and B = true. A and B must be Boolean expressions. |

A or B |
– | true if either A and B = true. A and B must be Boolean expressions. |

not A |
– | true if A = false. A must be Boolean expression. |

#### Functions

Equations and algorithms can use functions to define relationships. They need to reference the path of the function, and the inputs put into a bracket, modifying the function. The syntax of the function inputs can reference specific function input variables to modify, otherwise the input order must match the order they are defined in the function. If a function has multiple outputs then they need to be within brackets in the correct order.

v := Modelica.Math.Vectors.length({v_x, v_y ,v_z}); (sortedVector, sizeOfVector) = Modelica.Math.Vectors.sort({1, 2, 3, 4, 5, 6, 7, 8, 9}, true);

There are some common functions that do not require a path, they are contained in the **Modelica Reference** and **Dymola Commands** libraries:

v = der(x); y = sqrt(x);

### Conditional Statements

Conditional statements are available in Modelica to be able to control the effects of equations and algorithms. They take the form of **if**, **for**, **when** and **while** statements that can be used to change behaviour of variables by changing the calculation. The condition needs to be in the form of a Boolean, either a variable or a calculation. In all cases the statement must be ended with an **end** declaration, coupled with the conditional designation. For instance, to end an **if** loop, the closing declaration is **end if** unlike some other codes.

**If** Statements

**if** statements define equations by operating conditions. They can define any number of equations but each condition must define the same number of relationships. There has to be an **else** statement used unless determining a constant or parameter. Different equations sets are separated either by **else** or **elseif** sections. The statement must be ended with an **end if;** declaration.

ifconditionthenexpressionelseifconditionthenexpressionelseexpressionend if;

for example:

if angle > 0 then y = 1; elseif angle < 0 then y = -1; else y = 0; end if;

If statements can also be used in equations:

variable= ifconditionthenexpressionelseifconditionthenexpressionelseexpression; y = if angle > 0 then 1 elseif angle < 0 then -1 else 0;

**When** Statements

When statements are used to define a set of equations that are valid at a particular event instant.

whenconditionthenexpressionelsewhenconditionthenexpressionend when;

for example:

when time > 1 then y = time; elsewhen time > 2 then y = 3; end when;

In this example **y** = 0 until **time** = 1, then from that point **y** = 1 until **time** = 2 then from that point onward **y** = 3.

**Written by: David Briant – Project Engineer**

**Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion.**