by Marco Bonvini on Saturday October 22, 2016
Ask any engineer, and they’ll tell you - “It’s tough to model the real world!” Engineers perform a delicate balancing act when they create complex models of physical systems, building detailed simulations while at the same time struggling against overwhelming complexity or irrelevant detail.
Fortunately, there’s help. In this post I give an overview of the Modelica modeling language and the features that make it the right choice for modeling complex engineering systems. Modelica is not some abstract academic toy disregarded by the modern industry. In fact, in the last few years Modelica has been adopted by some of the world’s biggest automotive companies – Audi, BMW, Daimler, Ford, Toyota, and VW. These companies use Modelica to design energy efficient vehicles, improve air conditioning systems, even (in the case of the Ferrari F1 team) design racing cars! And the automotive sector is just one of many that find this language effective.
As described on the Modelica association website, Modelica is a non-proprietary, object-oriented, equation based language to conveniently model complex physical systems including a variety of different aspects, including mechanical, electrical, electronic, hydraulic, thermal, control, electric power or process-oriented subcomponents.
The concept is simple. Modelica is a language for modelling systems that can be described with equations such as
The following diagram shows the big picture behind any Modelica model
The steps are the following:
Before diving into some of Modelica’s core concepts let’s start with an example, a kind of HelloWorld of differential equations. We’ll use the simplest differential equation you can imagine
with initial condition \( x(t_0) = 5 \)
model defines the beginning of a model named HelloWorld.
The model is divided in three sections: declarations, initializations and equations.
The model starts by declaring variables, constants and parameters. Variables represent
quantities that are expected to change over time. Constants, meanwhile, are fixed
quantities that will never change. And finally parameters are quantities
that do not change over time but may change between simulations.
In this case parameter
a and variable
x have the same type,
Real type meaning a real floating point number.
initial equation introduce the second part of our model.
Here we define the initial conditions of the system, for example the value of
the beginning of our model’s simulation time.
equation introduces the third and final part of our program,
where we finally get down to writing the equations of the system.
In this case there is one variable
x and one differential equation
der(x) = a*x.
One important thing to note is the
der(...) operator that indicates the
time derivative of the expression inside the brackets.
The ability to express derivatives in such a clean and concise way is very helpful
when dealing with mathematical models.
So we’ve completed a simple example to get familiar with Modelica. Now, let’s look at some of its most interesting features. As I mentioned at the beginning of the post, Modelica is an Object-Oriented equation-based modeling language. What exactly do those words mean? Let’s have a look, keeping in the context of mathematical modeling.
Computer programs are nothing but clever set of instructions to let a computer solve a particular problem for us (If this sounds familiar, bear with me. I’m going to make a distinction between assignments and equations that you might not be familiar with). Let’s consider the following problem: given two numbers compute their sum. The following code snippet shows a solution to the problem implemented in C
What happens here when the program is executed is that the value 4 is inserted in the memory space
associated to variable
a, the value 3 is inserted in the memory space
associated to variable
b and then their sum is assigned to the memory space associated to variable
Given the initial problem this program implements a particular solution to it. And, most of the time when we write algorithms we are writing specific solutions to certain problems. In this case the problem and its algorithmic solution were pretty straightforward!
Now here’s the kicker: when dealing with mathematical modeling the situation is quite different. You typically start with a set of equations that describe the general understanding of the system you want to model.
Then, you can collected data about that system, and feed it into those equations to start generating some interesting answers to your questions. Often there are multiple questions to answer, so the model should be as general as possible so that it can be be easily manipulated to answer all the questions.
Let’s get back to our problem: given two numbers compute their sum. The following is a Modelica model that solves it
The content of line 4 is the essence of equation-based modeling. It all boils down to two concepts:
c - (a + b) = 0is an equation and equations are not assignment like the ones we’re used to write in C, Java, Python, etc.
a = 4and
b - 3 = 0are located on lines 5 and 6. Both lines come after the first equation because equations are not assignment, their order does not matter. The only thing that matters is that there are the same number of variables and equations.
So it turns out that equation-based modeling is all about writing equations with code. The Modelica tool of choice will know how to symbolically analyze the model, extract equations, substitute variables in equations, sort them and apply the proper numerical methods to find the solution. The trick is knowing how the Modelica compiler works so that the model we create has better numerical properties and is therefore solved more efficiently.
The equation-based paradigm opens the door to an interesting concept called a-causality.
Before focusing on the meaning and implications of a-causality let’s have a look at an example based on Newton’s second law
The acceleration of an object as produced by a net force is directly proportional to the magnitude of the net force, in the same direction as the net force, and inversely proportional to the mass of the object.Sir Isaac Newton
Newton’s second law describes a relationship between the following quantities: the forces acting on a body, the mass of the body and its acceleration.
The example below shows a system where a body that can move only along one direction and two forces are acting on it
Given this system, we can identify a number of problems to solve. Identifying a problem and finding its solution requires us follow a specific mental process:
Here’s a small list of questions and their solutions presented with block diagrams; each diagram visually describes how the information flows from the inputs (i.e., known variables) to the output (i.e., answer to the question).
This process requires a person that looks at the problem and
What we’ve just seen is the so called causal approach: explicitly describe cause-effect relationships between variables and hard-code them in an algorithm that computes the solution. Even if the physical system is always the same, a slightly different question about the system might require a complete rewrite of the algorithm that solves the problem. The block diagrams above show this concept very well. Even if the underlying system is always the same, the type of operations performed on the data and the order of those operations continuously change. This is rather inconvenient, especially when dealing with systems that are more complicated than a single body and a couple of forces!
Moreover, the design process of any complex system is an iterative process. New questions come up as the process continues. Having an algorithm that answers only one question and that needs to be completely rewritten to answer a slightly different one is practically useless.
Modelica’s equation-based nature enables the so called a-causal modelling approach: build mathematical models without imposing a priori cause-effect relationships between variables. Let’s try to model the same single body system in Modelica and see what it looks like.
The equation section is divided in two parts. The first part describes Newton’s second law, i.e. the dynamic of the system. This part is not going to change unless the body starts moving at the speed of light!
The second part contains the boundary conditions. The boundary conditions are required to have a complete picture of the system. The boundary conditions can change from time to time depending on the question being asked.
When boundary conditions change the Modelica tool does all the hard work to finding causal relationships between the variables. Once the Modelica tools finally finds the relationships between inputs and outputs it generates an executable that solves the problem.
Such an approach leverages a computer for doing the boring and error prone job of rewriting and sorting equations. Computers are definitely better than humans at doing this!
A biproduct of A-causality is that models written in Modelica are more readable and maintainable. The model contains a description of the system and its boundary conditions rather than a solution to a specific problem. Most of the readers familiar with software engineering best practices know this is very important, especially when dealing with complex and large models.
After this brief discussion on a-causality I hope you’re convinced that writing ad-hoc solution to problems is almost as bad as writing assembly code without using a higher level language and a compiler.
In this post we just started scratching the surface. We learned a bit of Modelica syntax and we’ve seen that Modelica works directly with equations and that this leads to model that are a-causal. A-causal models are more readable and more flexible and are a good way to express complex engineering problems.
The next post SW engineering meets mathematical modeling - Types and Objects goes a little bit deeper and explains the meaning of Object-Oriented modeling as well some nice features related to Modelica’s type system.
If after reading this post you’re even more interested in Modelica I suggest you to look at the online book Modelica by examples by Michael Tiller, otherwise browse the website of the Modelica associations www.modelica.org.
If you’re interested in the work I’ve done with Modelica over the last years you can look here.
Special thanks to Daniel McQuillen for proof reading this post and providing helpful suggestions.