Principles of Modelling Peter Fritzen PDF
Principles of Modelling Peter Fritzen PDF
Peter Fritzson
Linköping University, Dept. of Comp. & Inform. Science
SE 581-83, Linköping, Sweden
[email protected]
Reference Disturbance
2009-02-03 MODPROD’2009
Tutorial Aim
Peter Fritzson
Principles of Object Oriented
Modeling and Simulation with
Modelica 2.1
Wiley-IEEE Press
940 pages
Input current
pulse
Capacitor voltage - dynamic
time
Continuous
Discrete
time
• Composition lines represent the actual physical connections i.e. electrical line,
mechanical connection, heat flow
Component 1 Component 2
Connection
Component 3
i
axis6
qddRef cut joint
qdRef
1
qRef
1
k1 r3Control
r3Motor r3Drive1 tn
i 1
S S
qd axis5
qdRef Kd
S
0.03 rel
Jmotor=J joint=0
spring=c axis4
S
qRef pSum Kv sum w Sum rate2 rate3 iRef gear=i
fric=Rv0
+1 b(s) 340.8
0.3
- +1 - a(s) S
axis3
q
qd
Rp2=50
Ra=250 La=(250/(2*D*wm))
Rd1=100
C=0.004*D/w m Rp1=200 axis1
Rd2=100
Ri=10
- -
-
+ +
dif f + pow er
OpI
Vs
Rd4=100
Srel = n*transpose(n)+(identity(3)- n*transpose(n))*cos(q)- skew(n)*sin(q);
emf
y
wrela = n*qd;
Rd3=100
x
g3
zrela = n*qdd; inertial
Sb = Sa*transpose(Srel); g1
r0b = r0a;
hall2
vb = Srel*va; hall1 w r
wb = Srel*(wa + wrela);
ab = Srel*aa;
zb = Srel*(za + zrela + cross(wa, wrela)); qd g4
q g2
Developed by
Hello
MathCore for
Siemens
Courtesy of Siemens Industrial Turbomachinery AB
• Modelica Versions
• 1.0 released September 1997
• 2.0 released March 2002
• 2.2 released March 2005
• 3.0 released September 2007
Courtesy
MathCore
Engineering AB
The following equations are automatically derived from the Modelica model:
Interactive
Emacs session handler Textual
Editor/Browser Model Editor
DrModelica
OMNoteBook Execution Modelica
Model Editor Compiler
Modelica
Debugger
Parse
Client: Graphic
Model Editor
Server: Main Program
Corba
Including Compiler,
Interpreter, etc. Client: OMShell
Interactive
Session Handler
SCode Interactive
Client: Eclipse
Untyped API
Plugin MDT
Inst
Typed Checked Command API
system
plot
Ceval
etc.
• Compiler/interpreter – OMC
• Interactive session handler – OMShell
• Notebook with DrModelica – OMNotebook
• Eclipse plugin Modelica Development Tooling (MDT)
Code Assistance on
function calling.
mg altitude ′ = velocity
velocity ′ = acceleration
declaration
new model class Rocket "rocket class" comment
parameters (changeable parameter String name;
before the simulation) Real mass(start=1038.358);
Real altitude(start= 59404);
floating point Real velocity(start= -2003); start value
type Real acceleration;
Real thrust; // Thrust force on rocket
Real gravity; // Gravity forcefield
parameter Real massLossRate=0.000277; name + default value
equation
(thrust-mass*gravity)/mass = acceleration; mathematical
der(mass) = -massLossRate * abs(thrust); equation (acausal)
der(altitude) = velocity;
differentiation with der(velocity) = acceleration;
regards to time end Rocket;
altitude
CelestialBody
class MoonLanding
parameter Real force1 = 36350;
parameter Real force2 = 1308;
only access inside protected
the class parameter Real thrustEndTime = 210;
parameter Real thrustDecreaseTime = 43.2;
access by dot public
notation outside the Rocket apollo(name="apollo13");
class CelestialBody moon(name="moon",mass=7.382e22,radius=1.738e6);
equation
apollo.thrust = if (time < thrustDecreaseTime) then force1
else if (time < thrustEndTime) then force2
else 0;
apollo.gravity=moon.g*moon.mass/(apollo.altitude+moon.radius)^2;
end MoonLanding;
30000
50 100 150 200
25000
-100
20000
-200
15000
10000 -300
5000 -400
It starts at an altitude of 59404 (not The rocket initially has a high negative
shown in the diagram) at time zero, velocity when approaching the lunar
gradually reducing it until surface. This is reduced to zero at
touchdown at the lunar surface touchdown, giving a smooth landing
when the altitude is zero
• The class keyword can be replaced by other keywords, e.g.: model, record,
block, connector, function, ...
• Classes declared with such keywords have restrictions
• Restrictions apply to the contents of restricted classes
function sum
input Real arg1;
input Real arg2;
output Real result;
algorithm
result := arg1+arg2;
end sum;
class ColoredPointWithoutInheritance
Real x;
Real y, z;
parameter Real red = 0.2; Equivalent to
parameter Real blue = 0.6;
Real green;
equation
red + blue + green = 1;
end ColoredPointWithoutInheritance;
...
Real altitude(start= 59404);
...
inherited
parameters
model MoonLanding
parameter Real force1 = 36350;
parameter Real force2 = 1308;
parameter Real thrustEndTime = 210;
parameter Real thrustDecreaseTime = 43.2;
Rocket apollo(name="apollo13", mass(start=1038.358) );
CelestialBody moon(mass=7.382e22,radius=1.738e6,name="moon");
equation
apollo.thrust = if (time<thrustDecreaseTime) then force1
else if (time<thrustEndTime) then force2
else 0;
apollo.gravity =moon.g*moon.mass/(apollo.altitude+moon.radius)^2;
end Landing;
• Start OMNotebook
• Start->Programs->OpenModelica->OMNotebook
• Open File: Exercises-ModelicaTutorial.onb
• Open Exercises-ModelicaTutorial.pdf
0.4
0.2
0.5 1 1.5 2
simulate(VanDerPol,stopTime = 25)
1
plotParametric(x,y)
-2 -1 1 2
-1
-2
• Text Cells
• Input Cells
• Graphic Cells
Easy to follow
Exercises with
solutions hidden in
collapsed cells
+ v v +
pin1 pin2
i i
Pin pin1,pin2;
//A connect equation
//in Modelica:
connect(pin1,pin2);
Corresponds to pin1.v = pin2.v;
pin1.i + pin2.i =0;
Pin pin1,pin2;
//A connect equation
//in Modelica
connect(pin1,pin2);
Corresponds to pin1.v = pin2.v;
pin1.i + pin2.i =0;
v1 = v 2 = v 3 = … v n
Each primitive connection set of flow variables is used to generate sum-
to-zero equations of the form:
i1 + i 2 + … ( − i k ) + … i n = 0
model Source
extends TwoPin; v(t)
p.i n.i
parameter Real A,w;
+
equation
v = A*sin(w*time); p.v n.v
end Resistor;
model Ground
Pin p; p.v p.i
equation
p.v = 0;
end Ground;
i1 i2
n R1 p p R2 n
v1 v2
v3
p R3 n
i3
model ResistorCircuit
Resistor R1(R=100);
Resistor R2(R=200);
Resistor R3(R=300);
equation R1.p.v = R2.p.v;
connect(R1.p, R2.p); Corresponds to R1.p.v = R3.p.v;
connect(R1.p, R3.p); R1.p.i + R2.p.i + R3.p.i = 0;
end ResistorCircuit;
Example:
Examples:
V2
R2 R4
Gnd9
C2 Gnd3 Gnd6 C4
R1 Transistor1 Transistor2 R3
V1 C1 I1 C5 C3 Gnd4
ex ac iner vsen
model Generator
Modelica.Mechanics.Rotational.Accelerate ac;
Modelica.Mechanics.Rotational.Inertia iner;
Modelica.Electrical.Analog.Basic.EMF emf(k=-1);
Modelica.Electrical.Analog.Basic.Inductor ind(L=0.1);
Modelica.Electrical.Analog.Basic.Resistor R1,R2;
Modelica.Electrical.Analog.Basic.Ground G;
Modelica.Electrical.Analog.Sensors.VoltageSensor vsens;
Modelica.Blocks.Sources.Exponentials ex(riseTime={2},riseTimeConst={1});
equation
connect(ac.flange_b, iner.flange_a); connect(iner.flange_b, emf.flange_b);
connect(emf.p, ind.p); connect(ind.n, R1.p); connect(emf.n, G.p);
connect(emf.n, R2.n); connect(R1.n, R2.p); connect(R2.p, vsens.n);
connect(R2.n, vsens.p); connect(ex.outPort, ac.inPort);
end Generator;
model DCMotor
Resistor R(R=100);
Inductor L(L=100);
VsourceDC DC(f=10);
Ground G;
EMF emf(k=10,J=10, b=2);
Inertia load;
equation R L
connect(DC.p,R.n); emf
connect(R.p,L.n); DC
connect(L.p, emf.n);
connect(emf.p, DC.n); load
connect(DC.n,G.p);
connect(emf.flange,load.flange);
G
end DCMotor;
OpenModelica: www.openmodelica.org
class SumZ
parameter Integer n = 5;
Real[n] z(start = {10,20,30,40,50});
A simple for-loop
Real sum; summing the five elements
algorithm of the vector z, within the
sum := 0;
for i in 1:n loop // 1:5 is {1,2,3,4,5} class SumZ
sum := sum + z[i];
end for;
end SumZ;
class SumSeries
parameter Real eps = 1.E-6;
The example class SumSeries
Integer i; shows the while-loop construct
Real sum; used for summing a series of
Real delta;
algorithm exponential terms until the loop
i := 1; condition is violated , i.e., the terms
delta := exp(-0.01*i);
while delta>=eps loop become smaller than eps.
sum := sum + delta;
i := i+1;
delta := exp(-0.01*i);
end while;
end SumSeries;
...
algorithm
...
p:= polynomialEvaluator(A={1,2,3,4},x=21)
function polynomialMultiply
input Real a[:], b[:];
output Real c[:] := zeros(size(a,1)+size(b, 1) - 1);
external ”FORTRAN 77”
end polynomialMultiply;
time
event 1 event 2 event 3
initial()
true
false time
event at start
terminal()
true
false time
event at end
class SamplingClock
parameter Modelica.SIunits.Time first,interval;
Boolean clock;
equation
clock = sample(first,interval);
when clock then
...
end when;
end SamplingClock;
pre(y)
time
event
• The variable y has one of the basic types Boolean, Integer, Real, String,
or enumeration, a subtype of those, or an array type of one of those basic
types or subtypes
• The variable y is a discrete-time variable
• The pre operator can not be used within a function
v
4.5
4.1
3.2 delay(v,d)
4.5
4.1
3.2
time
model Sampler
parameter Real sample_interval = 0.1;
Real x(start=5);
Real y;
equation
der(x) = -x;
when sample(0, sample_interval) then
y = x;
end when;
simulate(Sampler, startTime = 0, stopTime = 10)
end Sampler;
plot({x,y})
t
2 4 6 8 10
inertia1 inertia2
signalV oltage
em f
1 elastoBacklash.w_rel
-0.5
t
We define a model with less mass in
5 10 15 20 25
inertia2(J=1), no damping d=0,
-0.2 inertia1.w and weaker string constant c=1e-5, to
-0.4 show even more dramatic backlash
-0.6
inertia2.w
phenomena
-0.8
The figure depicts the rotational
-1
-1.2
speeds for the two flanges of the
transmission with elastic backlash