modelica & dymola introduction - lunds...
TRANSCRIPT
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica & Dymola IntroductionFMNN05 – EMIL FREDRIKSSON & CLAUS FÜHRER
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Introduction
Modelica is a free, object-oriented, equation based language
for modelling of complex systems.
Initiated in 1996 by Hilding Elmqvist and now used in
industry all over the world.
Companies: Audi, BMW, Daimler,
Ford, Toyota, VW, ABB, Siemens
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Introduction
Modelica is a multi-domain language, not geared towards
any specific domain.
Easily couple models containing for example, mechanical
and electrical components.
Declarative, the order of the
equations does not matter.
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Introduction
• Object-oriented, models are classes and thus can easily
be extended using ordinary object-oriented features.
• Modelica allow acausal modelling, i.e. the
input-output causality is not fixed.
• Promotes model reuse!
• The language elements are mapped to
differential, algebraic and discrete
equations. NO support for partial
differential equations.
model FirstReal x;
equationder(x) = -1;
end First;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Introduction
Modelica is designed for fully implicit ordinary differential
equations,
𝟎 = 𝑭(𝒕, 𝒙, ሶ𝒙)
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Language VS Tool
Commercial Open-source
Dymola JModelica.org
SimulationX OpenModelica
MapleSim
Modelica → Compiler → Simulation Environment
Language Tool
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Standard Library (MSL)
Free library consisting of over 1200 models and about 900
functions in many engineering domains.
• Mechanics
• Electrical
• Fluid
• Thermal
• Etc...
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica, Hello World
How would the equation,
look in the Modelica language?
ሶ𝑥 = −𝑥, 𝑥 𝑡0 = 1
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica, Hello World
How would the equation,
look in the Modelica language?
ሶ𝑥 = −𝑥, 𝑥 𝑡0 = 1
model Basic
Real x(start=1);
equation
der(x) = -x;
end Basic;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Model
model Example
…
…
equation
…
end Example;
Model body
• Declaration of constants /
parameters / states / inputs
Equation section
• Declaration of model
equations
Name of the modelType of the object (i.e a model)
Start of equations
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics
• A key feature is that it is equation based, consider:
• x=y+1
• NOT an assignment in Modelica, a relation! The compiler
takes care of manipulating the equations.
• 0 = y+1-x is identical!
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica, Hello World (again)
How would the equation,
look in the Modelica language?
ሶ𝑥 = −𝑥, 𝑥 𝑡0 = 1
model Basic
Real x(start=1);
equation
0 = - der(x) - x ;
end Basic;
Note that this is identical
to the previous example!
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics (Syntax)
The important variable types are:
Real
Integer
Boolean
Additionally there is:
String
Enumeration
Real p;Integer y;Real x;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics (Syntax)
Constants
– Known by the compiler
and do not change values.
Parameters
– Known before the
simulation starts and
remain constant
throughout the simulation
Variability
• Constants
• Parameters
• Time-varyings
• Inputs
• Outputs
parameter Real p1 = 1;constant Integer y = 10;Real x;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics (Syntax)
To specify derivatives the keyword der() is to be used.
der(x) → 𝑑𝑥
𝑑𝑡
Only first order derivatives! der(der(x))
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Example, Pendulum
𝑚𝑑2𝑥
𝑑𝑡2=𝐹
𝐿𝑥
𝑚𝑑2𝑦
𝑑𝑡2=𝐹
𝐿𝑦 −𝑚𝑔
𝑥2 + 𝑦2 = 𝐿2
L is the length (parameter) of the Pendulum and g is the
gravity (constant), x and y are the coordinates and F is the
force.
Pendulum equations in Modelica code?
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics (Syntax)
Start attributes for time-varying variables may be set
using the modifier start.
Real x(start=1.0); x has (guess) start value 1.0!
NOTE, during initialization this value may change!
Real x(start=1.0, fixed=true); x must have start value 1.0!
Forces the initialization algorithm to use 1.0 as start value.
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Basics (Syntax)
model ChangedInitValuesReal x(start = 1);Real y(start = -1);
equationder(x) = -y;y = 1+x;
end ChangedInitValues;
• Consider the
following model,
• What happens
here?
•
If the guessed
values are
used:
• What actually happens is that ONE of the
guessed values are used to calculate the other.
𝑦 = 1 + 𝑥 → −1 = 1 + 1 → −1 ≠ 2
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Pendulum
model Pendulumconstant Real g=9.81 ”Gravity";
parameter Real L=1 ”Length";parameter Real m=1.0 ”Mass";
Real x(start=0.1);Real y(start=-0.9);Real vx;Real vy; Real F;
equationder(x) = vx;der(y) = vy;x^2 + y^2 = L^2;m*der(vx) = (F/L)*x;
m*der(vy) = (F/L)*y - m*g;
end Pendulum;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Example
The Van der Pol oscillator
• Parameter declaration
• State declaration
• Equations declaration
model VDP
// State start values
parameter Real x1_0 = 0;
parameter Real x2_0 = 1;
// The states
Real x1(start = x1_0);
Real x2(start = x2_0);
// The control signal
input Real u;
equation
der(x1) = (1 - x2^2) * x1 - x2 + u;
der(x2) = x1;
end VDP;NOTE: the equations may just as well
be specified as a residual!
ሷ𝑥 = 1 − 𝑥2 ሶ𝑥 − 𝑥 + 𝑢
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Features
In Modelica you can additionally specify:
• Functions
• Algorithms
• External functions (calling C or FORTRAN)
• Hybrid constructs (if and when clauses)
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica (connect statement)
In order to support model reuse, Modelica has a key
component, connect, for describing the interaction
between models.
connect(resistor2.n, resistor1.p)
In the electric case:
• The current should sum up to zero
• The voltage should be the same at
both connections
Different meaning in other domains.
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Electrical Circuit
Consider this Simple Circuit model:
model SimpleCircuit
parameter Real C = 0.001;
parameter Real R = 100;
parameter Real V0 = 5;
// The states
Real i;
Real uC(start=0);
equation
V0 – uC = R*i;
der(uC)*C = i;
end SimpleCircuit;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Electrical Circuit, Decomposed
• In order to model large systems, decomposing the system
into smaller components which are then connected is
essential.
• Trying this for our simple circuit:
model Resistor
parameter R;
Real i1, v1;
Real i2, v2;
Real i,u;
equationu = v2 - v1;
i1 + i2 = 0;
i = i2;
u = R*i;
end Resistor;
model Capacitator
parameter C;
Real i1, v1;
Real i2, v2;
Real i,u;
equationu = v2 - v1;
i1 + i2 = 0;
i = i2;
der(u)*C = i;
end Capacitator;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Electrical Circuit, Decomposed
model ConstVoltage
parameter V0;
Real i1, v1;
Real i2, v2;
Real i,u;
equationu = v2 - v1;
i1 + i2 = 0;
i = i2;
u = V0;
end ConstVoltage;
model Ground
Real i,u;
equationv = 0;
end Ground;
Voltage Source: Ground Model:
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Electrical Circuit, Reassembled
• The missing equations
are added in the top-level
model.
• This has to be done
manually and is thus error
prone.
• In order to simplify this,
Modelica introduces the
connector class.
model SimpleCircuit
Ground G;
Resistor R(R=100);
Capacitor C(C=0.001);
ConstVoltage S(V0=5);
equation
S.v2 = R.v1S.i2 + R.i1 = 0;R.v2 = C.v1R.i2 + C.i1 = 0;C.v2 = S.v1C.v2 = G.vC.i2 + S.i1 + G.i = 0;
end SimpleCircuit;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Connect Statement
In the electric case, we want a connector with the
properties:
• The current should sum up to zero
• The voltage should be the same at both connections
connector Pin
Real v;
flow Real i;
end Pin;
connect(pin1, pin2)pin1.v = pin2.v
pin1.i + pin2.i = 0
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Resistor Revisited
model Resistor
parameter R;
Real i1, v1;
Real i2, v2;
Real i,u;
equationu = v2 - v1;
i1 + i2 = 0;
i = i2;
u = R*i;
end Resistor;
model Resistor
parameter R;
Pin n, p;
Real i,u;
equationu = p.v – n.v;
p.i + n.i = 0;
i = p.i;
u = R*i;
end Resistor;
Using Pins
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Electrical Circuit, Reassembled (Again)
model SimpleCircuit
Ground G;
Resistor R(R=100);
Capacitor C(C=0.001);
ConstVoltage S(V0=5);
equation
connect(G.p, S.n);connect(S.p, R.n);connect(R.p, C.n);connect(C.p, G.p);
end SimpleCircuit;
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica Graphical Representation
Modelica is a textual language. However, there are language
features for specifying graphical representation of models.
Note, the equations in each model can always be viewed and
that the structure of the physical system is kept!
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modelica References
In charge of developing the Modelica language is the
non-profit organization Modelica Association
https://www.modelica.org/
Modelica by Example, free book about Modelica,
http://book.xogeny.com/
Cheat Sheet
http://modref.xogeny.com/
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola
State of the art modelling and simulation tool based on the
Modelica language.
“Dymola, Dynamic Modeling Laboratory, is a complete tool
for modeling and simulation of integrated and complex
systems for use within automotive, aerospace, robotics,
process and other applications.”
Developed since 1996 and now used in
industries such as, SCANIA, Toyota, BMW,
Ford and Tetra Pak.
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola (modelling layer)
• Modelica
standard library
• Toggle between
viewing textual or
objects layer
• Modelling area
• Toggle between
the modelling and
simulation tab
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola (modelling layer)
DEMO (MSL)
DEMO (Modelica Grammar)
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola (simulation layer)
• Translate the
model
• Simulate the model
• Simulation setup
• Browse the model
variables,
parameters and
constants
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola (simulation information)
Retrieving information
after a simulation:
• Goto, Simulation
• Choose, Show Log
A log showing the
simulation statistics will
appear.
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Dymola
DEMO (Simple dx)
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
More Information from Dymola
See under the
Translation tab
and the Debug
tab in Simulation
Setup.
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Integrators in Dymola
Integrators are found under
General in Simulation
Setup.
• Switch integrator
• Dassl
• Euler
• Radau
• more...
• Change tolerance
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Pendulum, revisited
𝑚𝑑2𝑥
𝑑𝑡2=𝐹
𝐿𝑥
𝑚𝑑2𝑦
𝑑𝑡2=𝐹
𝐿𝑦 −𝑚𝑔
𝑥2 + 𝑦2 = 𝐿2
L is the length (parameter) of the Pendulum and g is the
gravity (constant), x and y are the coordinates and F is the
force.
Write the Pendulum equations in Modelica code:
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Phase plots
In the Variable
Browser:
• Right click on a
variabel
• Choose to use as
independent
variable
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Installation
• Download and install Dymola
• Download and install Visual Studio Express
– https://www.microsoft.com/en-
us/download/details.aspx?id=44914
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Installation
• Goto simulation tab (bottom right corner)
– Goto Simulation menu (top, third from the left)
– Goto Setup
– Goto Compiler
– Choose Visual Studio and press OK.
• Next, goto Help menu (top, last from the left)
– Goto License
– Goto Setup
– Choose a local license file and point it to the provided license file.
– Press OK and restart!
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Modeling Issues
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Starting Point
Before starting on the projects:
• Check the many examples in the Modelica standard
library!
• Read the information about the different parts and objects!
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Initial Values
Real x(start = 0.9, fixed = true);
The initial guess If the initial guess is fixed or not
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Initial Values
model Pendulumconstant Real g=9.81 ”Gravity";
parameter Real L=1 ”Length";parameter Real m=1.0 ”Mass";
Real x(start=0.1);Real y(start=-0.9);Real vx;Real vy; Real F;
equationder(x) = vx;der(y) = vy;x^2 + y^2 = L^2;m*der(vx) = (F/L)*x;
m*der(vy) = (F/L)*y - m*g;
end Pendulum;
What happens if you specify
inconsistent initial values?
What happens if you do NOT
fully specify the initial values?
What happens if you force the
initial values?
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Initial Values
Lund University | Numerical Analysis | FMNN05 | 2016-11-08
Initial Values
Cases (Pendulum):
• Not fully specified (x=0.1)
– Dymola, tries to find a consistent set. May fail!
• Fixed inconsistent values (x=0.9, y=0.9, fixed=true)
– Error, overspecified system
• Guessed inconsistent values (x=0.9, y=0.9, fixed=false)
– Dymola, changes one of the values!
• Guessed inconsistent values (x=1.1, y=0.9, fixed=false)
– Error, cannot recover