Introduction Modelica
Introduction Modelica
Francesco Casella
Dipartimento di Elettronica e Informazione
Politecnico di Milano
Course Programme
Basic concepts in
Object-Oriented Modelling
The interaction between the models is formalized in terms of input and output variables
Con: The model of a given system can only be re-used in the same context
(i.e. with the same prescribed variables at the system boundary):
low re-usability of basic models
Con: It is more difficult to go from the mathematical model to the numerical simulation
algorithm (the model is not oriented to the solution algorithm)
4
I
C
V =xRI
x =
(State-Space ODE)
xk 1 :=x k tf xk , u k
y k :=g x k , u k
S1
+
S2
S3
(SIMULINK)
S4
But: each sub-system model depends on the selection of input and output variables at
the system boundary (not re-usable).
Example: RC network
Prescribed Voltage
I
C
V
I
C
V =xRI
V x
x =
RC
Vx
I=
R
Prescribed Current
S1
x =
S2
The same physical model (same equations) requires different models depending on its
connection to the outside world
Example: RC network
R
I
x
C
xRI =V
C x=
I
V
Causality is only determined at the aggregate system level.
I0
R
V0
V
C
x
xRI =V
C x =I
V 0=u
V 0=V
I 0I =0
(RC network)
(voltage generator)
(Kirchoff's law - mesh)
(Kirchoff's law - node)
V 0x
RC
V =V 0
V 0 x
Symbolic
I=
R
manipulation
(simulation tool)
V x
I 0= 0
R
x =
sophisticated capabilities of symbolic analysis for large DAE systems (up to many
thousands of equations), to analyse and simplify the structure of the problem
numerical and symbolic algorithms to reduce the DAE system to an ODE system and
then integrate it
During this short course, the basic mathematics and algorithms to carry out this task
will be discussed.
8
Connectors / Ports
In an Object-Oriented context, models are connected together by means of
Connectors (or Ports)
The Port concepts originates from the modelling of physical systems exchanging
power (or energy): the connection between models always involves two coupled
variables:
Electrical systems:
W =V I
Mechanical systems:
Hydraulic systems:
W =sF
W =Pq
When N ports are connected together, the corresponding connection equations are:
Effort variables
Flow variables
e1=e2=...=e N
f j =0
Connectors can be generalised to cases with more than two variables on each port
(abandoning the idea that the product of the two must necessarily be a power)
Connectors are inherently a-causal: no variable is declared a-priori as input or output.
When needed, it is of course possible to declare causal connectors, to describe the
connection of input and output signals, such as in control systems.
Connectors - Example
Mechanical translational systems (1 d.o.f.)
Electrical systems
Thermal systems
Example:
Active suspension system
Control
system
Sensor
TM
TM
TM
TM
Act.
susp.
Ground
TM
Damp
Mass
TM
TM
Ground
Spring
TM
TM
TM
TM
10
All the models using the same standard connectors are interchangeable
Example: Resistor
Basic model
...
The choice is not trivial in some cases, e.g. 3D multibody mechanical systems,
thermo-hydraulic systems
11
TM
TM
TM
TM
TM
TM
A.Z
TM
A.Y
TM
A.X
TM
TM
12
The child class is a specialisation of the ancestor classes: it inherits all their
featurs (in our case, variables, parameters, sub-models, and equations), and
adds its own ones
Mammal
Pet
Is a
Is a
Feline
Is a
Cat
14
Classes
The basic construct of Modelica is the class (class), which defines how an object is
made.
As in most object-oriented language, in the code it is possible to:
Instantiate one or more instances of the classes, which will then be used to simulate some
real objects
type: Describes a variable type starting from the predefined ones (Real, Boolean,
Integer)
16
When two or more connector are connected, equality equations are generated for all
the corresponding effort variables; an equation sum()=0 is generated for all the
corresponding flow variables.
flow variables are always assumed positive when entering the object.
Variable types can be either predefined (Real, Boolean, Integer), or derived from
predefined types, e.g.
type Voltage = Real(unit=V);
type Current = Real(unit=A);
type Temperature = Real (unit=K, min=0);
17
Models
A model can contain variables, parameters, constants, other models, and equations
relating them.
Example: resistor and capacitor models
i
p
v
R
n
model Resistor
Pin p,n;
Voltage v;
Current i;
parameter Resistance R;
equation
v = p.v-n.v;
i = p.i;
0= p.i + n.i;
v = R*i;
end Resistor;
i
p
v
C
n
model Capacitor
Pin p,n;
Voltage v;
Current i;
parameter Capacitance C;
equation
v = p.v-n.v;
i = p.i;
0= p.i + n.i;
i = C*der(v);
end Capacitor;
Aggregate Models
It is possible to define models containing other models:
model RCNet
parameter Resistance Rnet;
parameter Capacitance Cnet;
Resistor R1(R=Rnet);
Capacitor C1(C=Cnet);
Pin p,n;
p
R1
C1
n
Modifiers
(parameter propagation)
equation
connect(R1.n, C1.p);
connect(R1.p, p);
connect(C1.n, n);
end RCNet;
Equivalent to:
R1.n.v = C1.p.v;
R1.n.i + C1.p.i = 0;
RC1
GND
GND
model SimpleCircuit
RCnet RC1(Rnet=100, Cnet=1e-6);
Vsource V0;
Ground GND;
equation
connect(RC1.n, GND.p);
connect(RC1.p, V0.p);
connect(V0.n, GND.p);
end SimpleCircuit;
19
//
//
//
//
//
3D position vector
10X5 matrix of real numbers
vector of 10 real numbers
vector of 5 real numbers
array of four 4 RCnet networks
Multiplication of vector and matrices follow the usual rules of matrix algebra
equation
w = A*v;
RC[2]
RC[3]
20
Blocks
A block is a model having only causal connectors.
Blocks usually represent signal-processing objects, rather than physical devices.
Example: state-space representation of a generic linear dynamical system:
block StateSpace
parameter Real A[:, :],
B[size(A,1), :],
C[:, size(A,2)],
D[size(C,1), size(B,2)]=zeros(size(C,1),size(B,2)];
input
Real u[size(B,2)];
output
Real y[size(C,1)];
protected
Real x[size(A,2)];
equation
assert(size(A,1)==size(A,2), Matrix A must be square.);
der(x) = A*x + B*u;
y
= C*x + D*u;
end StateSpace
block TestSS
StateSpace S(A=[1,2; 3,4], B=[0,1], C=[1,1]);
equation
S.u = sin(time);
end TestSS;
21
Functions
functions are particular models without state (memory), thus representing
functions in a strict mathematical sense (no side-effects). The input/output relationship
is described in a causal way (algorithm)
Examples: factorial function and Bessel function
n
n != k
k=1
function fact
input Integer n;
output Real y ;
algorithm
y := 1;
for k in 2:n loop
y := y*k;
end loop;
end fact;
1k
J m x = 2km
x 2k m
k ! mk !
k=0 2
function Bessel
input Integer m;
input Real x;
input N = 20 Defaults to 20;
output Real y ;
algorithm
y := 0;
for k in 0:N loop
y := y + (-1)^k * x^(2*k+m)/
(2^(2*k+m)*fact(k)*fact(m+k));
end loop
end Bessel;
a = Bessel(2,x);
b = Bessel(1,x,5);
22
model Resistor
extends OnePort;
parameter Resistance R;
equation
v = R*i;
end Resistor;
model Capacitor
extends OnePort;
parameter Capacitance C;
equation
C*der(v) = i;
end Capacitor;
In this way it is possible to factor out common parts in sets of similar models
It is also possible to extend models that are already fully functional by themselves
Modelica allows multiple inheritance: a given model can extend more than one parent
object.
23
Depending on the specific needs of the simulation, you might need converter models
having different degrees of accuracy, from the ideal transformer down to a detailed
representation of the switched capacitor circuit
All the models have something in common: the interface (four pins) and a parameter (the
nominal output voltage)
We can define an abstract (partial) model BaseDCDC with the common features and
the actual model implementations as derived objects:
model IdealDCDC
extends BaseDCDC
equation
pb.v nb.v = vout;
pa.i*(pa.v na.v) + pb.i*(pb.v-nb.v) = 0;
pb.i + nb.i = 0;
pa.i + pb.i = 0;
end SimpleDCDC
model DetailedDCDC
extends BaseDCDC
// detailed implementation
...
end DetailedDCDC;
24
Finally, it is possible to generate models of the circuit with different degrees of detail,
by replacing the base converter models with the desired implementations. Conv1 and
Conv2 can be described by any model inheriting from BaseDCDC, e.g:
model SimpleCircuit
extends BaseCircuit(
redeclare IdealDCDC Conv1,
redeclare IdealDCDC Conv2);
end MixedCircuit;
model MixedCircuit
extends BaseCircuit(
redeclare IdealDCDC Conv1,
redeclare DetailedDCDC Conv2);
end MixedCircuit;
This very powerful feature of the Modelica language allows to deal with model variants
in a clean, concise and consistent way, avoiding unnecessary code duplication.
25
26
Conditional Equations
Inside equations, it is possible to define expressions whose value depend on a
boolean expression (typically some inequality):
q_out = if level > 0 then K*sqrt(level) else 0 end if;
y = if
u < ymin then ymin
else if u > ymax then ymax
else
u
end if;
27
Example 3: Relais
block FlipFlop
parameter Real Thr;
input
Real Set;
input
Real Reset;
discrete output Real y;
equation
when (Set>Thr or Reset>Thr)
y = if Set>Thr then 1
else 0
end if;
end when;
end FlipFlop;
block Relais
parameter Real Thr;
input
Real Switch;
output
Boolean y;
equation
when (Switch > Thr)
if pre(y)==true then y = false
else y = true
end if;
end when;
end Relais;
29
Graphical annotations
The Modelica language is a textual language (each Modelica model is a plain-text
ASCII file); this favours the portability of models and the interoperability of the
modelling tools (no binary files in obscure, proprietary formats!).
To allow using GUIs, Modelica allows to define a graphical layer (icons) for models,
connectors, and connections.
The graphical appearence of each object is defined by an annotation() construct,
in terms of graphical primitives (lines, circles, rectangles, text, bitmaps).
It is then possible to build composite models by drag&drop, and to connect the models
by drawing lines between the corresponding connectors.
Most tools (e.g. Dymola) allow to work on the same model both in text and graphics
modes, which represent two different views on the same underlying object; the two
views are always kept consistent with each other
31
Libraries
To ease the re-use of previously developed models, it is possible to build
hierarchically-structured libraries, using the package construct.
package Modelica
package Electrical
...
end Electrical;
package Mechanics
...
end Mechanics;
...
end Modelica;
A plant model can be built by instantiating models from the installed libraries, either by
referencing to the full name, or using the import statement (or just by drag&drop, in
which case the tools takes care of the naming)
model Circuit
Modelica.Electrical.Analog.Basic.Resistor R1, R2;
Modelica.Electrical.Analog.Basic.Capacitor C1, C2;
...
end Circuit;
model Circuit2
import Modelica.Electrical.Analog.Basic.*;
Resistor R1, R2;
Capacitor C1, C2;
...
end Circuit2;
32
Standard Libraries
The Modelica language is accompanied by the Modelica library, a large collection of
standard libraries containing basic elements for the different fields of engineering:
Modelica
Blocks
Constants
Electrical
Icons
Math
Mechanics
Media
Fluid
SIunits
Thermal
...
Other libraries have been developed for specific application fields. Some of them are
open source, other are commercial.
The ThermoPower library is an open source library, developed at Politecnico di
Milano, aimed at power plant and energy conversion systems.
33
Dymola (Dynasim AB) is de-facto the only tool supporting the whole standard in
an industrial-strenght way.
2006
34
Versiopn 3.0 del linguaggio is developed with active contributions from Dynasim,
MathCore, Linkping Universitet, plus two new players: ITI GmbH (SimulationX)
and MapleSoft (MapleSim). The goal is full support of the language, including
advanced features for multibody and thermofluid systems.
MathCore joins OSMC e contributes to the front end. At the end of the year, a
version of OMC whose front-end fully supports Modelica 3.1. This front end is now
used by MathModelica.
36
References
Mattson, S.E., Elmqvist, H., Otter, M.: Physical system modeling with Modelica,
Control Engineering Practice, v. 6, pp. 501-510, 1998.
37