Lecture03 Classes
Lecture03 Classes
Hybrid Modeling
Peter Fritzson
pelab
Acausal Modeling
The order of computations is not decided at modeling time
Acausal Causal
Equation Level
Peter Fritzson
pelab
Peter Fritzson
pelab
Multi-Domain Modeling Visual acausal hierarchical component modeling Typed declarative equation-based textual language Hybrid modeling and simulation
Peter Fritzson
pelab
Peter Fritzson
pelab
Peter Fritzson
pelab
axis6
tn
qd
axis5
l
qdRef
Kd
S
0.03
Jmotor=J rel joint=0
spring=c
axis4
+1
b(s)
340.8 S
0.3
+1
a(s)
fric=Rv0
qRef
pSum
Kv
sum
w Sum
rate2
rate3
iRef
S
gear=i
axis3
rate1 b(s) a(s) tacho2 b(s) a(s) tacho1 PT1
g5
axis2
Rp2 2=50 Ra=250 La=(250/(2*D*w m ))
qd
Rd1=100
C=0.004*D/w m
Rp1=200
Srel = n*transpose(n)+(identity(3)n*transpose(n))*cos(q)Rd2=100 Ri=10 skew(n)*sin(q); + + wrela = n*qd; + diff pow er OpI zrela = n*qdd; Sb = Sa*transpose(Srel); Rd4=100 r0b = r0a; vb = Srel*va; g3 wb = Srel*(wa + wrela); g1 ab = Srel*aa; zb = Srel*(za + zrela + cross(wa, wrela));
Rd3=100 Vs hall2
axis1
emf
y x inertial
w r
hall1
g2
qd
g4
pelab
Variable declarations
Differential equations
pelab
time
Hybrid Modeling
pelab
10
Peter Fritzson
pelab
0.5
1.5
11
Peter Fritzson
pelab
12
Peter Fritzson
pelab
1 -1
-2
13
Peter Fritzson
pelab
Start S OMNotebook O
Start->Programs->OpenModelica->OMNotebook Open File: Exercise01-classes-simple-textual.onb
Open Exercise01-classes-simple-textual.pdf
14
Peter Fritzson
pelab
15
Peter Fritzson
pelab
16
Peter Fritzson
pelab
Variables and Constants cont Names indicate meaning of constant Easier to maintain code Parameters are constant during simulation Two types of constants in Modelica
constant parameter
constant constant constant parameter Real String Integer Real PI=3.141592653589793; redcolor = "red" "red"; one = 1; mass = 22.5;
17
Peter Fritzson
pelab
Comments in Modelica
1) Declaration comments, e.g. Real x "state variable";
class VanDerPol "Van der Pol oscillator model" Real x(start = 1) "Descriptive Descriptive string for x x ; // Real y(start = 1) "y coordinate; // parameter Real lambda = 0.3; equation der(x) = y; // This is the der(y) = -x + lambda*(1 - x*x)*y; /* This is the end VanDerPol; x starts at 1 y starts at 1
2) Source code comments, disregarded by compiler 2a) C style, e.g. /* This is a C style comment */ 2b) C++ style, e.g. // Comment to the end of the line
pelab
18
Peter Fritzson
thrust mg
thrust mass gravity mass mass = massLossRate abs ( thrust ) acceleration = altitude = velocity velocity = acceleration
declaration comment
new model parameters (changeable before the simulation) floating point type
class Rocket "rocket class" parameter String name; Real mass(start=1038.358); Real altitude(start= 59404); Real velocity(start= -2003); Real acceleration; Real thrust; // Thrust force on rocket Real gravity; i // Gravity i forcefield i parameter Real massLossRate=0.000277; equation (thrust-mass*gravity)/mass = acceleration; der(mass) = -massLossRate * abs(thrust); der(altitude) = velocity; der(velocity) = acceleration; end Rocket;
start value
19
Peter Fritzson
pelab
An instance of the class can be declared by prefixing the type name to a variable name
The declaration states that moon is a variable containing an object of type CelestialBody
pelab
20
Peter Fritzson
10
Moon Landing
Rocket
apollo13
thrust mg altitude
apollo. gravity =
CelestialBody
class MoonLanding parameter Real force1 = 36350; parameter Real force2 = 1308; protected parameter Real thrustEndTime = 210; parameter Real thrustDecreaseTime = 43.2; public Rocket apollo(name "apollo13") apollo(name="apollo13"); 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;
only access inside the class access by dot t ti outside t id notation the class
21
Peter Fritzson
pelab
It starts at an altitude of 59404 (not shown in the diagram) at time zero, gradually reducing it until touchdown at the lunar surface when the altitude is zero
22
Peter Fritzson
The rocket initially has a high negative velocity when approaching the lunar surface. This is reduced to zero at touchdown, giving a smooth landing
pelab
11
pelab
Modelica Functions Modelica Functions can be viewed as a special kind of restricted class with some extensions A function can be called with arguments arguments, and is instantiated dynamically when called More on functions and algorithms later in Lecture 4
function sum input Real arg1; input Real arg2; output Real result; algorithm result := arg1+arg2; end sum;
24
Peter Fritzson
pelab
12
Inheritance
parent class to Color restricted kind of class without equations
record ColorData parameter Real red = 0.2; parameter Real blue = 0.6; Real green; end ColorData; class Color extends ColorData; equation red + blue + green = 1; end Color;
class l E ExpandedColor d dC l parameter Real red=0.2; parameter Real blue=0.6; Real green; equation red + blue + green = 1; end ExpandedColor;
Data and behavior: field declarations, equations, and certain other contents are copied into the subclass
25
Peter Fritzson
pelab
Inheriting definitions
Legal! Identical to the inherited field blue
record ColorData parameter Real red = 0.2; 0 2; parameter Real blue = 0.6; Real green; end ColorData; class ErrorColor extends ColorData; parameter Real blue = 0.6; parameter Real red = 0.3; equation q red + blue + green = 1; end ErrorColor;
26
Peter Fritzson
pelab
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;
class Color2 // OK! extends Color; equation red + blue + green = 1; end Color2;
Color is identical to Color2 Same equation twice leaves one copy when inheriting
class Color3 // Error! extends Color; equation red + blue + green = 1.0; // also inherited: red + blue + green = 1; end Color3;
27
Peter Fritzson
pelab
Multiple Inheritance
Multiple Inheritance is fine inheriting both geometry and color
class l Color l parameter Real red=0.2; parameter Real blue=0.6; Real green; equation red + blue + green = 1; end Color; class Point Real x; Real y,z; end Point; class ColoredPoint extends Point; extends Color; end ColoredPoint;
multiple inheritance
class ColoredPointWithoutInheritance Real x; Real y, z; parameter Real red = 0.2; parameter Real blue = 0.6; Real green; equation red + blue + green = 1; end ColoredPointWithoutInheritance;
Equivalent to
28
Peter Fritzson
pelab
14
Diamond Inheritance
29
Peter Fritzson
pelab
Equivalent to:
inheritance
30
Peter Fritzson
pelab
15
Inheritance Through Modification Modification is a concise way of combining inheritance with declaration of classes or instances A modifier modifies a declaration equation in the inherited class Example: The class Real is inherited, modified with a different start value equation, and instantiated as an altitude variable:
... Real altitude(start= 59404); ...
31
Peter Fritzson
pelab
thrust mg
model Rocket "generic rocket class" extends Body; parameter Real massLossRate=0.000277; Real altitude(start= 59404); Real velocity(start= -2003); Real acceleration; Real thrust; Real gravity; equation thrust-mass*gravity= mass*acceleration; der(mass)= -massLossRate*abs(thrust); der(altitude)= velocity; d (velocity)= der ( l it ) acceleration; l ti end Rocket;
altitude
CelestialBody
model Body "generic body" Real mass; String name; end Body;
model CelestialBody extends Body; constant Real g = 6.672e-11; parameter Real radius; end CelestialBody;
32
Peter Fritzson
pelab
16
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;
33
Peter Fritzson
pelab
Equivalent to
class ColoredPointWithoutInheritance Real x; Real y,z; protected Real red; protected Real blue; protected Real green; equation red + blue + green = 1; end ColoredPointWithoutInheritance;
The inherited fields from Point keep their protection status since that extends-clause is preceded by public A protected element cannot be accessed via dot notation!
34
Peter Fritzson
pelab
17
35
Peter Fritzson
pelab
Actual arguments to classes are modifiers, which when containing whole variable declarations or types are preceded by the prefix redeclare
class C replaceable class ColoredClass = GreenClass; ColoredClass obj1(p1=5); replaceable YellowClass obj2; ColoredClass obj3; R dCl RedClass obj4 bj4; equation end C; class C2 = C(redeclare class ColoredClass = BlueClass);
ColoredClass object
Equivalent to
class C2 BlueClass YellowClass BlueClass RedClass equation end C2; obj1(p1=5); obj2; obj3; obj4;
A red object
36
Peter Fritzson
pelab
18
AC
R2
L1
R3
The class ElectricalCircuit has been converted into a parameterized generic class GenericElectricalCircuit with three formal class parameters R1, R2, R3, marked by the keyword replaceable
class ElectricalCircuit Resistor R1(R=100); Resistor R2(R=200); Resistor R3(R=300); Inductor L1; SineVoltage AC; Groung G; equation ti connect(R1.n,R2.n); connect(R1.n,L1.n); connect(R1.n,R3.n); connect(R1.p,AC.p); ..... end ElectricalCircuit;
Class parameterization
class GenericElectricalCircuit replaceable Resistor R1(R=100); replaceable Resistor R2(R=200); replaceable Resistor R3(R=300); Inductor L1; SineVoltage AC; Groung G; equation connect(R1.n,R2.n); connect(R1.n,L1.n); connect(R1.n,R3.n); connect(R1.p,AC.p); ..... end GenericElectricalCircuit;
37
Peter Fritzson
pelab
A more specialized class TemperatureElectricalCircuit is created by changing the types of R1, R3, to TempResistor
R2 L1
2
AC
R3
38
Peter Fritzson
pelab
19
39
Peter Fritzson
pelab
Exercise 1.3 Model the System Below Model this Simple System of Equations in Modelica
40
Peter Fritzson
pelab
20