Modelica Quick Reference
Basic
/*
Comments */
are ignored //
when building a model
[A-Za-z_][A-Za-z_0-9]*
Convention: ClassName variableName
pin_a functionName
Reals: 1.2345 15. 1e5 -1.5E5 -3e-7 .7 .75e+2
Integers: 1 100 2147483647
Strings: "a string"
Booleans: true
false
^ * / + -
String concatenation: "a" + "b"
> >=
< <= == <>
not and or
Equations and Algorithms
for »
for i in 1 : 10 loop // i
takes values 1, 2, …, 10
// equations here
end for;
// equations here
end for;
for r in 1 : 2 : 10 loop
for i in {1, 3, 6, 7} loop
connect »
connect(a.c,
b.c);
if »
if a > 2 then
v1 = 2;
elseif a < 0 then // optional
v1 + v2 = 0;
else // usually required for balanced models
v1 = 0;
end if;
v1 = 2;
elseif a < 0 then // optional
v1 + v2 = 0;
else // usually required for balanced models
v1 = 0;
end if;
when »
when a > 2 then
b = true;
elsewhen c > 3 then // optional
b = false;
end when;
b = true;
elsewhen c > 3 then // optional
b = false;
end when;
when {x > 2, sample(0,2)} then
reinit(v, -e * pre(v));
end when;
reinit(v, -e * pre(v));
end when;
terminate »
when b > 2 then
terminate("b reached target");
end when;
terminate("b reached target");
end when;
Classes
model
model DiffEq
Real x(start = 1);
equation
der(x) = -x;
end DiffEq;
Real x(start = 1);
equation
der(x) = -x;
end DiffEq;
block
block AddOne
input Real u;
output Real y;
equation
y = u + 1;
end AddOne;
input Real u;
output Real y;
equation
y = u + 1;
end AddOne;
function »
function plusTwo
input Real u1;
output Real y1;
algorithm
y1 := u1 + 2;
end plusTwo;
input Real u1;
output Real y1;
algorithm
y1 := u1 + 2;
end plusTwo;
connector »
connector Pin
Real v;
flow Real i;
end Pin;
Real v;
flow Real i;
end Pin;
package
Contains other classes
record »
record MyRecord
Real r;
Integer i;
end MyRecord;
Real r;
Integer i;
end MyRecord;
type
type Length = Real(unit = "m");
Operators »
operator record operator operator
function
constant Real c = 3;
does not change between simulations
parameter Real p;
does not change during simulations
Real v;
varies continuously during simulation
Integer i;
discrete Real
v;
changes at discrete times
Component c1 if condition1;
equation
connect(c1, …); // disappears if not condition1
equation
connect(c1, …); // disappears if not condition1
Real:
A Floating-Point Number »
Attributes:
quantity
unit
displayUnit
min
max
start
fixed
nominal
unbounded
stateSelect
Integer »
Attributes:
quantity
min
max
start
fixed
Boolean »
Attributes:
quantity
start
fixed
String »
Attributes:
quantity
start
fixed
Enumeration »
type TSize = enumeration(small, medium,
large);
TSize size = TSize.medium;
TSize size = TSize.medium;
initial equation
initial algorithm
initial()
start
modelica://MyClass/a/path/image.png
»
Built-in Functions
abs
sign
sqrt
do not generate events
max(1, 2) ⇒
2, min(1,
2) ⇒ 1
div
mod
rem
ceil
floor
integer
sin
cos
tan
asin
acos
atan
atan2
sinh
cosh
tanh
exp
log
log10
der(expr)
delay(expr, …)
cardinality(c)
homotopy(actual, simplified)
semiLinear(x, k+, k-)
inStream(v)
actualStream(v)
getInstanceName()
initial()
terminal()
noEvent(…)
smooth(p, expr)
sample(start, interval)
pre(y)
edge(b)
change(v)
reinit(x, expr)
Integer
EnumTypeName
String
Arrays
{…} is shorthand for array(…)
{i for i in 1 : 3} ⇒ {1, 2, 3}
{r for r in 1 : 2 : 10} ⇒ {1, 3, 5, 7,
9}
{i^2 for i in {1, 3, 7, 6}} ⇒ {1, 9, 49,
36}
[]
concatenates arrays
[1; 2] ⇒ {{1}, {2}},
[1, 2] ⇒ {{1, 2}}
[{{1, 2}}, {{3, 4}}] ⇒ {{1, 2, 3,
4}}
[{{1, 2}}; {{3, 4}}] ⇒ {{1, 2}, {3,
4}}
a[1, 2]
a[end - 1, end] ⇒ a[size(a, 1) - 1, size(a,
2)]
Full slice: a[:] ⇒ a[1:end]
jth column: a[:, j]
jth row: a[j] ⇔ a[j,
:]
a[j : k] ⇒ {a[j],
a[j+1], …, a[k]}
=
:=
+
-
.*
./
apply elementwise
2 .^ {3, 4} ⇒ {2 ^ 3,
2 ^ 4}
{2, 3} .^ 4 ⇒ {2 ^ 4,
3 ^ 4}
Matrix product or inner product: a * b
Size
Dimensionality Conversion »
scalar
vector
matrix({1, 2})
Reductions »
min(…)
max(…)
sum(…)
product(…)
Reduction expressions: sum(i for i in 1:3) ⇒
1 + 2 +
3
Array reductions: min({1, 2, 3, 4}) ⇒ 1
Two scalars: max(1, 2) ⇒ 2
Matrix and Vector Algebra »
transpose
outerProduct
symmetric
cross
skew
Real x[2](each start = 1.0); // == x[2](start = {1.0, 1.0})
Advanced
model M
public // default if not explicitly protected
// can be modified when M is used as a component:
parameter Real p = 2;
protected
// protected from modification or redeclaration:
parameter Real q = 2;
…
end M;
public // default if not explicitly protected
// can be modified when M is used as a component:
parameter Real p = 2;
protected
// protected from modification or redeclaration:
parameter Real q = 2;
…
end M;
final
prevents modification and redeclaration
»
import A.B;
import A.{B1, B2, …};
import A.*;
import MyB = A.B;
function myFun
input Real x;
output Real y;
external "C" y = myFun(x);
end myFun;
input Real x;
output Real y;
external "C" y = myFun(x);
end myFun;
Annotations »
Library
Include
IncludeDirectory
LibraryDirectory
SourceDirectory
Type Mapping »
Modelica | Input | Output | Return |
---|---|---|---|
Real | double | double * | double |
Integer | int | int * | int |
Boolean | int | int * | int |
String | const char * | const char ** | const char * |
Enumeration | int | int * | int |
model A
outer Real v;
/* code using "global" v */
end A;
model B
inner Real v = 2.5; // "global" value v
A a1; // a1.v == 2.5
A a2; // a2.v == 2.5
…
end B;
outer Real v;
/* code using "global" v */
end A;
model B
inner Real v = 2.5; // "global" value v
A a1; // a1.v == 2.5
A a2; // a2.v == 2.5
…
end B;
Components
replaceable MotorA motor;
extends Sys(redeclare MotorB motor(R = 100));
Sys mySys(redeclare MotorB motor(R =
100));
Classes
replaceable type MyType = Real;
extends Sys(redeclare
type MyType = Real(unit = "V"));
Sys mySys(redeclare type MyType = Real(unit = "V"));
constrainedby »
replaceable MotorA motor constrainedby BaseMotor;
Base-clock Conversion Operators »
sample(u, clock)
hold(u)
Sub-clock Conversion Operators »
subSample(u, factor)
superSample(u, factor)
shiftSample(u, shiftCounter,
resolution)
backSample(u, backCounter,
resolution)
noClock(u)