# 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
if a > b then 5 else c + d  »
time is simulation time in model and block  »
Equations and Algorithms
Simple »
a + b = c;
if a > b then 5 else 3 = c;
(out1, out2) = fun(var1, par1);
for »
for i in 1 : 10 loop // i takes values 1, 2, …, 10
// 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;
when »
when a > 2 then
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;
terminate »
when b > 2 then
terminate("b reached target");
end when;
assert »
assert(a < 2, "a too large" );
assert(b < 2, "b too large" , AssertionLevel.warning);
Simple »
a := c + 2;
(out1, out2) := fun(var1, par1);
for »
for ... loop // see Equations ➔ for
// statements here
end for;
while »
while a > 2 loop
// statements here
end while;
break; out of while and for  »
if »
if a > 2 then
v1 := 2;
elseif a < 0 then // optional
v1 := 0;
else // optional
v1 := 3;
end if;
when »
when a > 2 then
y1 := sin(x);
y3 := 2 * x + y1 + y2;
elsewhen c > 3 then // optional
…
end when;
when {x > 2, sample(0,2)} then
…
end when;
terminate and assert see Equations  »
Classes
model
model DiffEq
Real x(start = 1);
equation
der(x) = -x;
end DiffEq;
block
input Real u;
output Real y;
equation
y = u + 1;
function »
function plusTwo
input Real u1;
output Real y1;
algorithm
y1 := u1 + 2;
end plusTwo;
» return; in a function algorithm terminates a function call
connector »
connector Pin
Real v;
flow Real i;
end Pin;
package
Contains other classes
record »
record 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
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;
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
ndims({{1, 2, 3}, {4, 5, 6}})2 »
size({{1, 2, 3}, {4, 5, 6}}, 1)2 »
size({{1, 2, 3}, {4, 5, 6}}){2, 3} »
Dimensionality Conversion »
scalar vector matrix({1, 2})
Constructors »
zeros(2, 3){{0, 0, 0}, {0, 0, 0}} »
fill(5, 2, 3){{5, 5, 5}, {5, 5, 5}} »
ones identity diagonal linspace  »
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})
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;
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;
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
Documentation Figure  »
Evaluate  »
Inline LateInline  »
experiment HideResult  »
Icon Diagram Text Line Polygon Rectangle  »
preferredView Dialog  »
version uses  »
derivative  »
inverse  »
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;
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;
Clock constructor: Clock(…)  »
previous(u)  »
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)
Utility Operators »
firstTick(u)
interval(u)