Module2 - mtrl - Modeling and Simulation

--- Suggestion on material ---

Motivation

All of you have probably had some experience from modeling and simulating some dynamical systems, perhaps using a tool such as Simulink on a small toy-example in a course or in a small project (say with less than 100 states, with models that can be described on a couple of computer screens or so).  But when you start working on larger systems, perhaps including models from different domains, and when experts from different fields, and different companies, are cooperating and exchanging dynamical simulation components, you will quickly see the need for a proper language and good tools.

Much time and effort can be needed to produce reliable and good model libraries describing dynamical properties of components from different fields. These component models should be possible to connect to a larger system in a flexible way. This is why acausal equation-based modeling is needed.

We will here describe the Modelica language and let you try the tool OpenModelica

Note: There is a recent effort within Matlab/Simulink also in this direction, called SimScape. But we prefer to teach you this using Modelica. It is a real language, tools are open source,...

Read Download this presentation

(thank you Knut Åkesson, Chalmers) for more detailed motivation and explanation.

Still not convinced? Well then do the mental exercise of taking the models on p.14, describing an electrical system with two dynamical states and compare:

  • Where would you attach a measurement probe to measure the current out from the voltage source? What does the output of the block sum2 in Simulink represent physically?
  • How much work is needed to put C or L equal to zero (the system gets fewer dynamical states). Or put R1 = 0.
  • How much work is needed to exchange the capacitor C to an inductor L2?

Sure you can probably do it reasonably fast in this simple example, for instance putting the value on C or L to some small number close to zero (how close?) might produce acceptable numerical solutions to the second question, at least after changing the choice of the numerical simulation algorithm to a "stiff" solver. But it will be harder when you are faced with a large system, with hundreds of such sub-component models,  for which you lack domain knowledge and when you are uncertain what such numerical approximations give acceptable results.

 

You should definitely also watch this video Links to an external site.Modelica advantages illustrating advantages with the a-causal/equation-based approach compared to a causal/signal-flow approach, and which also gives you a glimpse of someone using the tool we will let you try below. (Do not try to follow the details about what happens in the tool at this moment).

Final note: For small toy-examples you don't really appreciate the difference. So for the work on the  Crazyflie examination exercises about control we will actually use matlab-simulink models, which were already available.

 

Learning Outcomes

You should get some knowledge of acausal, equation-based modeling and be able to explain its importance for model-reuse in modeling multidomain, large-scale systems.  You should be able to read Modelica code and setup and run a small example in a Modelica tool, such as OpenModelica (or JModelica, if you prefer to use that.)

 

Thanks to Martina Maggio for producing the following tutorial.

 

The Modelica language

Modelica® (website Links to an external site.) is a non-proprietary, object-oriented, equation based language to conveniently model complex physical systems containing, e.g., mechanical, electrical, electronic, hydraulic, thermal, control, electric power or process-oriented sub-components.

A Modelica modeling environment is needed to edit or to browse a Modelica model graphically in form of a composition diagram. A Modelica translator is needed to transform a Modelica model into a form (usually C-code) which can be simulated by standard tools. There are many alternatives Links to an external site. (both commercial and open source). In the following we use the open source OpenModelica Links to an external site. environment, compiler, and translator.

Modelica models

In Modelica one writes models, not programs. The Modelica translator takes the model, verifies its correctness, manipulates it, and generates some low level code (typically C or C++ code) to simulate the model. Models in Modelica are acausal, which means "not oriented", or written independently from their connections to other models.

Modelica models are based on four concepts: connectors, effort and flow variables, equation and their derivatives, and the connect construct. A connector corresponds to a physical terminal and contains effort and flow variables. When the connect construct is used to connect two terminals, two equations are generated: (1) the effort variables are set to have the same value, (2) the sum of the flow variables is set to zero.

Simple Modelica example of an electric Pin:

connector Pin
  Real v;      // pin voltage
  flow Real i; // current entering the pin
end Pin;

Pin a, b;
connect(a, b);

This generates these two equations.

  a.v = b.v;
  a.i + b.i = 0;

Using pins, we can model a resistor as follows.

model Resistor
  Pin r1, r2; 
  parameter Real Resistance = 1000;
equation 
  0 = r1.i + r2.i;
  0 = r1.v - r2.v - Resistance * r1.i;
end Resistor;

Parameters do not change during a simulation, they are assigned a fixed value that remains constant (used in this case to store the resistance of the resistor). In the equation section of the model, one writes equations and not assignments (the equations are valid during the entire execution of the model and are not only executed once).

Introducing dynamics

In Modelica it is possible to specify the dynamics of components. For example, let's write the model of a capacitor.

model Capacitor
  Pin a, b; 
  Real V (start = V_start);
    parameter Real C = 1e-6;
    parameter Real V_start = 0; 
equation
  0 = a.i + b.i; 
  0 = a.v - b.v - V; 
  0 = C*der(V) - a.i;
end Capacitor;

In this model, der(V) represents the derivative of the state variable V (real valued, with initial value V_start set to zero).

Algorithm and Equations

Together with the equation block, it is possible to specify an algorithm block. The main difference between an algorithm section and an equation section is that each equation in an equation section is used in simulating a model. With an algorithm section, you have assignment statements that are imperative. This means that you can overwrite the impact of a previous assignment.

equation
  a = b;
  a = c;
algorithm
  a := b;
  a := c;

In the first case (equation block), the two equations are always valid, forcing b and c to assume the same value during the entire simulation. In the second case, the statements are executed one after the other, and the value of a is overwritten.

Algorithms and equations can be mixed, and used to simulate events happening at precise times and their response, together with physical laws that are valid at any point in time.

When clause

A when clause can be used to indicate the reaction to specific conditions. This is for example useful in simulating hybrid and switching systems (systems that have both continuous dynamics and discrete transitions). To indicate the value that a variable has exactly prior to the event, it is possible to use pre(variable) in conjunction with statements in the when block. An example of the use of the when clause to simulate a bouncing ball can be found here Links to an external site..

 

Why using Modelica?

(If you are eager to try it out, you can postpone this material until later). The following give some further overviews tutorial about Modelica. 

The reason one should use a modern language supporting acausal, equation-based, object-oriented modeling and simulation is also described in this presentation.

The interested student might also want to take a look at this

OpenModelica

OpenModelica installation

The following instructions install OpenModelica on Ubuntu 16.04. You can follow them in the virtual machine that you have set up for Module 1. (If you want to set it up in another computer environment, feel free to do so, if you know how to.)

To install OpenModelica, open a terminal and type the following four commands. The first adds the OpenModelica repository to the list of software repositories. The second command adds the OpenModelica key and allows the download from the repository. The third command updates the information about the repositories and the last one installs the OpenModelica software.

for deb in deb deb-src; do echo "$deb http://build.openmodelica.org/apt `lsb_release -cs` nightly"; done | sudo tee /etc/apt/sources.list.d/openmodelica.list
wget -q http://build.openmodelica.org/apt/openmodelica.asc -O- | sudo apt-key add -
sudo apt-get update
sudo apt-get install openmodelica

The execution of the last command installs the OpenModelica Compiler (omc) and the OpenModelica Editor (OMEdit). Now start OMEdit.

OpenModelica usage

Create and simulate your first models by following the instructions given in the following videos.

Additional material