Professional Documents
Culture Documents
Lecture03 Classes
Lecture03 Classes
Typed
Declarative Hybrid
Equation-based Modeling
Textual Language
Acausal Modeling
Visual
Component
Level
1
Typical Simulation Process
• Multi-Domain Modeling
• Visual acausal hierarchical component modeling
• Typed declarative equation-based textual
language
• Hybrid modeling and simulation
2
What is Special about Modelica?
Multi-Domain
Modeling
Acausal model
(Modelica)
Causal
block-based
model
(Simulink)
3
What is Special about Modelica?
Multi-Domain Hierarchical system Visual Acausal
Modeling modeling Hierarchical
k2
i
Component
qddRef
qdRef qRef k1 r3Control
r3Motor r3Drive1
cut joint
tn
axis6
Modeling
1 1
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
Srel = n*transpose(n)+(identity(3)-
Rd2=100 n*transpose(n))*cos(q)- axis1
Ri=10
skew(n)*sin(q); - -
-
wrela = n*qd; +
diff +
+
pow er
zrela = n*qdd; OpI
Sb = Sa*transpose(Srel);
Vs
Rd4=100
r0b = r0a; emf
Rd3=100
vb = Srel*va; g3
wb = Srel*(wa + wrela); y
ab = Srel*aa;
g1 x
inertial
zb = Srel*(za + zrela + cross(wa, wrela));
hall2
g2 qd
g4
q
7CourtesyPeter Fritzson
of Martin Otter Copyright © Open Source Modelica Consortium pelab
4
What is Special about Modelica?
Multi-Domain Visual Acausal
Modeling Component
Modeling
Hybrid modeling =
continuous-time + discrete-time modeling
Continuous-time
Discrete-time
Typed
Declarative time
Hybrid
Equation-based Modeling
Textual Language
5
Simplest Model – Hello World!
0.4
0.2
0.5 1 1.5 2
1.0 time
0.2 0.4 0.6 0.8 1
0.95
0.90
6
Example class: Van der Pol Oscillator
class VanDerPol "Van der Pol oscillator model"
Real x(start = 1) "Descriptive string for x"; // x starts at 1
Real y(start = 1) "y coordinate"; // y starts at 1
parameter Real lambda = 0.3;
equation
d ( ) = y;
der(x) // This
hi iis the
h 1st diff equation
i //
der(y) = -x + lambda*(1 - x*x)*y; /* This is the 2nd diff equation */
end VanDerPol;
simulate(VanDerPol,stopTime = 25)
1
plotParametric(x,y)
-2 -1 1 2
-1
-2
• Start
S OMNotebook
O
• Start->Programs->OpenModelica->OMNotebook
• Open File: Exercise01-classes-simple-textual.onb
• Open Exercise01-classes-simple-textual.pdf
7
Exercises 2.1 and 2.2
• Open the Exercise01-classes-simple-textual.onb
found in the Tutorial directory.
• Locate the VanDerPol model in DrModelica (link from
S ti 2
Section 2.1),
1) using
i OMN
OMNotebook!
t b k!
• Exercise 2.1: Simulate and plot VanDerPol. Do a slight
change in the model, re-simulate and re-plot.
• Exercise 2.2. Simulate and plot the HelloWorld example.
Do a slight change in the model, re-simulate and re-plot.
Try command-completion, val( ), etc.
class HelloWorld "A simple equation"
Real x(start=1);
equation simulate(HelloWorld, stopTime = 2)
der(x)= -x; plot(x)
end HelloWorld;
8
Variables and Constants cont’
Comments in Modelica
9
A Simple Rocket Model
thrust − mass ⋅ gravity
Rocket acceleration =
mass
apollo13 thrust
mass ′ = − massLossRate ⋅ abs ( thrust )
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;
i // Gravity
i forcefield
i
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;
10
Moon Landing
Rocket apollo13
thrust moon. g ⋅ moon.mass
apollo. gravity =
mg
(apollo.altitude + moon.radius )2
altitude
CelestialBody
class MoonLanding
parameter Real force1 = 36350;
parameter Real force2 = 1308;
only access protected
inside the class parameter Real thrustEndTime = 210;
parameter Real thrustDecreaseTime = 43.2;
access by dot public
t ti
notation outside
t id Rocket apollo(name "apollo13")
apollo(name="apollo13");
the 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
11
Restricted Class Keywords
Modelica Functions
12
Inheritance
parent class to Color
Inheriting definitions
Legal!
Identical to the
Inheriting multiple
record ColorData
inherited field blue identical
parameter Real red = 0.2;
0 2; definitions results
parameter Real blue = 0.6; in only one
Real green; definition
end ColorData;
class ErrorColor
extends ColorData;
parameter Real blue = 0.6;
parameter Real red = 0.3;
equation
q
red + blue + green = 1; Inheriting
Illegal!
end ErrorColor; Same name, but multiple different
different value definitions of the
same item is an
error
13
Inheritance of Equations
class Color
parameter Real red=0.2;
parameter Real blue=0.6;
Real green;
equation
red + blue + green = 1;
end Color;
Multiple Inheritance
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;
14
Multiple Inheritance cont’
class Rectangle
extends VerticalLine;
extends HorizontalLine;
end Rectangle;
class SameColor
inheritance extends Color; connector MyPin = Pin;
end SameColor;
15
Inheritance Through Modification
16
The Moon Landing
Example using Inheritance cont’
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
apo o.g a ty =moon.g*moon.mass/(apollo.altitude+moon.radius)^2;
oo .g oo . ass/(apo o.a t tude oo . ad us) ;
end Landing;
17
Advanced Topic
• Class parameterization
Colored- Colored-
Class Class
Equivalent to
object object
class C2
Green- BlueClass obj1(p1=5);
Class
YellowClass obj2;
BlueClass obj3;
A yellow A red
object object
RedClass obj4;
equation
end C2;
18
Class Parameterization when Class Parameters
are Components
R1
Class
class ElectricalCircuit class GenericElectricalCircuit
Resistor R1(R=100);
parameterization replaceable Resistor R1(R=100);
Resistor R2(R=200); replaceable Resistor R2(R=200);
Resistor R3(R=300); replaceable Resistor R3(R=300);
Inductor L1; Inductor L1;
SineVoltage AC; SineVoltage AC;
Groung G; Groung G;
equation
ti equation
connect(R1.n,R2.n); connect(R1.n,R2.n);
connect(R1.n,L1.n); connect(R1.n,L1.n);
connect(R1.n,R3.n); connect(R1.n,R3.n);
connect(R1.p,AC.p); connect(R1.p,AC.p);
..... .....
end ElectricalCircuit; end GenericElectricalCircuit;
AC R2 L1 2 R3
class TemperatureElectricalCircuit =
G GenericElectricalCircuit (redeclare TempResistor R1
redeclare TempResistor R3);
19
Exercises 1 Simple Textual Continued
• C
Continue exercises in Exercise01-classes-
simple-textual.onb
20