Modelica03 AdvancedTutorial
Modelica03 AdvancedTutorial
Contents
1. Overview
2. Modelica Libraries
3. Packages
4. Connectors
5. Replaceable Components
6. Datasheet Libraries
7. Component Arrays
8. Global Variables
9. Initialization
10.Version Handling
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 2
1. Overview
Learn how to use Modelica and Dymola for developing
your own Modelica libraries to model complex systems.
required:
2. Modelica Libraries
Use as much as possible from available libraries, see
Modelica library page: http://www.Modelica.org/libraries.shtml
3. Packages
Modelica models are structured in hierarchical libraries (package)
package Modelica
package Mechanics
package Rotational
model Inertia Modelica.Mechanics.Rotational.Inertia
...
end Inertia;
model Torque
...
end Torque;
...
end Rotational;
end Mechanics;
...
end Modelica;
package Modelica
package Mechanics
package Rotational
package Interfaces
connector Flange_a
... identical definitions
end Flange_a;
end Interfaces
model Inertia
Interfaces.Flange_a flange_a;
Modelica.Mechanics.Rotational.Interfaces.Flange_a a;
end Inertia;
...
end Rotational;
end Mechanics;
...
end Modelica;
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 11
file: Modelica.mo
package Modelica
package Mechanics
package Rotational
model Inertia
...
end Inertia;
model Torque
...
end Torque;
...
end Rotational;
end Mechanics;
...
end Modelica;
file: ...\Modelica\Mechanics\Rotational.mo
...\Modelica
\Blocks within Modelica.Mechanics;
\Electrical package Rotational
\Mechanics model Inertia
package.mo ...
Rotational.mo end Inertia;
Translational.mo
model Torque
...
end Torque;
...
end Rotational;
package Mechanics
end Mechanics; Every package directory must have a file
package.mo that contains information
file: directly belonging to the package
...\Modelica\Mechanics\package.mo
(e.g. annotations of the package)
Even when a package does not use annotations, a file package.mo must be
present on which the package name is defined. This file is used to uniquely
define the package name, since, e.g., directory names in Windows are not
case sensitive whereas in Modelica they are case sensitive.
File: ...\Modelica\Mechanics\package.mo
package Mechanics
end Mechanics;
MODELICAPATH
list of directory names, e.g.:
“D:\otter\work1; D:\otter\work2”
Define a package.
package ServoLib
model Motor
...
end Motor;
model Gear
end Gear;
model Load
end Load;
end ServoLib;
Seems simple, but one needs to generate, copy, remove, rename, resort etc.
models and sublibraries (important for convenience of "daily work").
On the following slides, it is shown how this is performed with Dymola.
Changing a package
package ServoLib
model Motor
end Motor;
model Gear
end Gear;
model Load
end Load;
end ServoLib;
electrical
electrical current Modelica.Electrical.Analog.Interfaces.PositivePin
potential
Medium.AbsolutePressure p;
flow Medium.MassFlowRate m_dot;
Medium.SpecificEnthalpy h;
flow Medium.EnthalpyFlowRate H_dot;
Medium.MassFraction X[Medium.nX];
flow Medium.MassFlowRate mX_dot[Medium.nX];
end FluidPort;
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 22
signal connectors
Modelica.Blocks.Interfaces
connector InPort
parameter Integer n=1;
replaceable type SignalType = Real;
input SignalType signal[n];
end InPort;
connector Pin
import SI=Modelica.SIunits; connector PlugExpanded
SI.Voltage v import SI=Modelica.SIunits;
flow SI.Current i; SI.Voltage phase.v
end Pin; is equivalent to: SI.Voltage ground.v
flow SI.Current phase.i;
connector Plug flow SI.Current ground.i;
Pin phase, ground; end PlugExpanded;
end Plug;
C1=0.01 C1=0.01
L1=0.1 L1=0.1
Example:
PowerTrain library
Bus, version 1:
connector Bus
import SI = Modelica.SIunits;
SI.Velocity vehicleSpeed;
SI.AngularVelocity engineSpeed;
Real desiredThrottle;
Integer desiredGear;
Boolean ignition;
...
end Bus;
Drawback:
It is not possible to connect to single variables, such as vehicleSpeed.
Every model needs to know the complete bus.
It is only possible to connect to connectors.
Therefore, we need single variable connectors.
Modelica.Blocks.Interfaces.
connector Bus
extends Modelica.Blocks.Interfaces.*;
RealPort vehicleSpeed;
RealPort engineSpeed; + Possible to connect directly to,
RealPort desiredThrottle;
e.g., vehicleSpeed
IntegerPort desiredGear;
BooleanPort ignition; - Variables have no units
...
end Bus;
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 29
connector Bus
import SI = Modelica.SIunits;
extends Modelica.Blocks.Interfaces.*;
RealPort vehicleSpeed(redeclare type SignalType
= SI.Velocity);
RealPort engineSpeed(redeclare type SignalType
= SI.AngularVelocity);
RealPort desiredThrottle;
IntegerPort desiredGear;
BooleanPort ignition;
...
end Bus;
Hierarchical buses
Example: Robot with 6 axes.
connect(axis3.bus, bus.axis[3]);
connector AxisBus
BooleanPort motion_ref;
RealPort angle_ref(..);
connector Bus RealPort speed_ref(..);
parameter Integer naxis=6; BooleanPort motorOn;
AxisBus axis[naxis]; ...
end Bus end AxisBus;
model C2 =
C(redeclare RedModel comp1,
redeclare GreenModel comp2);
Equivalent to
model C
RedModel comp1(p1=5);
GreenModel comp2;
GreenModel comp3;
connect(…);
end C;
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 33
replaceable PI controller; PI
n=100 wl
Motor motor;
Gearbox gearbox(n=100);
Shaft Jl(J=10);
Tachometer wl;
equation
connect(controller.out, motor.inp);
connect(motor.flange , gearbox.a);
connect(gearbox.b , Jl.a);
connect(Jl.b , wl.a);
connect(wl.w , controller.inp);
end MotorDrive;
• Redeclare model
• replace the model of many components
model C
replaceable model ColouredClass = GreenClass;
ColouredClass comp1(p1=5);
replaceable YellowClass comp2;
ColouredClass comp3;
connect(…);
end C;
class C2 =
C(redeclare model ColouredClass = BlueClass);
Equivalent to
model C
BlueClass comp1(p1=5);
YellowClass comp2;
BlueClass comp3;
connect(…);
end C;
Replaceable packages
Example: medium model
types
e.g. medium specific definitions:
type Temperature = SI.Temperature(min=293)
model Pipe
replaceable package Medium = PackageMedium extends PartialMedium;
Medium.BaseProperties medium; // basic medium model
...
equation
U = m*medium.u;
...
end Pipe;
... Constraining class:
end Components; All used packages for Medium,
Default medium must have the public components
package can be of PartialMedium. Within model
replaced Tank, only definitions from
PartialMedium can be used!!!
A selection box contains all loaded
classes that extend from PartialMedium
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 37
double click
model ThreeTanks
replaceable package Medium = Media.Water.SimpleLiquidWater
extends PartialMedium
annotation (choicesAllMatching=true);
Components.Tank tank1(
redeclare package Medium = Medium, ...);
...
end ThreeTanks; Modification
Advantage:
Several different components (packages, models, functions, ...) can access
the constant data records (by name).
Disadvantage:
It is not possible to modify the data records since they are declared as
constant.
Note:
It is not allowed to have parameter instances in packages.
model Robot1
Components.Motor motor1(data=MotorData.M101());
Components.Motor motor2(data=MotorData.M102(maxTorque=21));
...
end Robot1;
record constructor:
• A function that has the same name as the record
• All record variables are input arguments to this function
• The output argument is an instance of the record
• Allows to modify the original record data when using it
electrical line
R=R R=R R=R R=R
with losses p n
C=C
C=C
C=C
C1
C2
C3
ground
import Modelica.Electrical.Analog.Basic;
parameter Integer n=4;
Basic.Resistor Rvec[n] // 4 Resistances
equation
for i in 1:n-1 loop
connect(Rvec[i].n, Rvec[i+1].p); // connect resistors
end for;
C=C
C=C
C=C
end ULine
C1
C2
C3
ground
8. Global Variables
A
inner Real c;
B1
B2 outer Real c;
outer Real c;
world
y
Contains
definition of gravity field (inquired by all bodies via inner/outer) and
animation defaults (inquired by all models via inner/outer)
{
defaultComponentName = "world",
}
defaultAttributes = "inner",
missingInnerMessage = "
No 'world' component is defined. A default world
component with the default gravity field will be used
(g=9.81 in negative y-axis). If this is not desired,
drag MultiBody.World into the top level of your model.
");
...
end World;
When dragging model "World" into the diagram layer, a declaration is generated:
inner World world;
When the user does not drag the World object, an "inner" object is missing:
A default "inner World world" is automatically generated for the
simulation and the "missingInnerMessage" is printed as warning.
Advanced Modelica Tutorial, Modelica'2003, Nov. 3-4, 2003 54
9. Initialization
DAE (Differential Algebraic Equation system) State space form
derived from Modelica model
x& = f1 (x, t )
(1) 0 = f (x& , x, y , t ) (2)
y = f 2 (x, t )
x: variables appearing differentiated
y: algebraic variables
0 = x& (t0 )
have to be added.
Advantage:
In both cases the symbolic engine is used to determine a robust and efficient
solution of the algebraic equations with good diagnostics in problematic cases
(e.g., if structurally redundant initial conditions are given).
1. By additional attributes
start initial value of variable at t0 (default = 0)
fixed = true : v(start=v0, fixed=true) results in the additional
initial equation: "v = v0"
(default for parameter)
= false: "start" is a guess value that may be changed
during initialization
(= default for non-paramter variables)
initialize in steady-state
w = 0, der(w) = 0,
phi_start is used as guess value (for non-linear equation)
MultiBody.Joints.ActuatedRevolute rev(n={0,0,1},
initType = MultiBody.Types.Init.SteadyState)
joint
spring
spring(c(start=20, fixed=false)
c = 49.05
(plot window/Advanced/Time = 0)
Implementation:
Every joint, that has potential states, is implemented in the following way:
1. Define choices for initialization
Should be performed with enumerations (Modelica 2.0).
Emulate enumerations with a package of constants if not yet
supported by tool (e.g. not in Dymola):
package Init
constant Integer Free = 1;
constant Integer PositionVelocity = 2;
constant Integer SteadyState = 3;
constant Integer Position = 4;
constant Integer Velocity = 5;
constant Integer VelocityAcceleration = 6;
constant Integer PositionVelocityAcceleration = 7;
model Robot
annotation(version="1.0", uses(MultiBody(version="0.98")));
...
end Robot;
model B
annotation(uses(MultiBody(version="0.95 Beta 1")));
...
end B;
Examples
(for details, see Dymola\Documentation\Dymola5Manual.pdf, page 261):
convertClass("Modelica.Rotational1D",
"Modelica.Rotational")
convertModifiers("MultiBody.Joints.Cylindrical",
{"startValuesFixed=false"},
{"initType=if %startValuesFixed% then
MultiBody.Types.Init.PositionVelocity
else MultiBody.Types.Init.Free"});