SlideShare a Scribd company logo
1 of 48
Download to read offline
1
OPEN MODELICA
INTRODUCTION & COMMANDS
Arun Umrao
www.sdmsacademy.in
DRAFT COPY - GPL LICENSING
2
www.sdmsacademy.in
Contents
1 Modelica Commands 5
1.1 Modelica Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.2 Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Escape Characters . . . . . . . . . . . . . . . . . . . . . . 8
Real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.3 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.4 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.1.5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.1.6 Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Declaration Equations . . . . . . . . . . . . . . . . . . . . 11
Modifier Equations . . . . . . . . . . . . . . . . . . . . . . 11
If-Else Equation . . . . . . . . . . . . . . . . . . . . . . . 11
For Equation . . . . . . . . . . . . . . . . . . . . . . . . . 12
Connect Equation . . . . . . . . . . . . . . . . . . . . . . 13
When Equation . . . . . . . . . . . . . . . . . . . . . . . . 13
1.1.7 Extends . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.1.8 Specialized Class . . . . . . . . . . . . . . . . . . . . . . . 14
1.1.9 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.2.1 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.2.2 Function Return Value . . . . . . . . . . . . . . . . . . . . 17
1.2.3 Protected Variable . . . . . . . . . . . . . . . . . . . . . . 17
1.2.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.2.5 Partial Function . . . . . . . . . . . . . . . . . . . . . . . 18
1.2.6 External Language . . . . . . . . . . . . . . . . . . . . . . 18
1.2.7 No Events . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.2.8 Smooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.2.9 Reinitiate . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.3 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.3.1 Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.3.2 Previous Sample Value . . . . . . . . . . . . . . . . . . . . 21
1.3.3 Change Event . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.3.4 Edge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.3.5 Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.4.1 Break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3
www.sdmsacademy.in
1.4.2 Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.4.3 For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.4.4 If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.4.5 In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.4.6 Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.4.7 Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.4.8 Terminate . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.4.9 Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.4.10 When . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.4.11 While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.5 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.5.1 Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.5.2 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . 27
Element-wise Operation . . . . . . . . . . . . . . . . . . . 28
Relational Operators . . . . . . . . . . . . . . . . . . . . . 29
1.5.3 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . 29
Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Minimum . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Products . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Summation . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Remainder . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Ceiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.5.4 Algebraic . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Absolute . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Sign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Square Root . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.5.5 Trigonometric . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.5.6 Special Functions . . . . . . . . . . . . . . . . . . . . . . . 37
Derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.6 Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.6.1 Connector . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Flow Connectors . . . . . . . . . . . . . . . . . . . . . . . 38
Stream Connectors . . . . . . . . . . . . . . . . . . . . . . 38
1.6.2 Connect . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.6.3 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.6.4 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.6.5 N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.6.6 P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4 Modelica Commands
www.sdmsacademy.in
1.6.7 Expandable Connectors . . . . . . . . . . . . . . . . . . . 40
1.7 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
1.7.1 Range Vector . . . . . . . . . . . . . . . . . . . . . . . . . 41
1.7.2 Size Function . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.7.3 Array Concatenate . . . . . . . . . . . . . . . . . . . . . . 42
1.7.4 Array Index . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.7.5 Array Mathematics . . . . . . . . . . . . . . . . . . . . . . 43
1.7.6 Array Constructor . . . . . . . . . . . . . . . . . . . . . . 45
1.8 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.8.1 Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . 46
1.8.2 Diagonal Matrix . . . . . . . . . . . . . . . . . . . . . . . 46
1.8.3 Transpose of Matrix . . . . . . . . . . . . . . . . . . . . . 47
1.8.4 Cross Products . . . . . . . . . . . . . . . . . . . . . . . . 47
1.9 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.9.1 Annotation & Units . . . . . . . . . . . . . . . . . . . . . 47
1.9.2 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.1. MODELICA CLASS 5
www.sdmsacademy.in
1Modelica Commands
The examples given below are written and compiled in two ways. Sim-
ple mathematical functions and examples are evaluated in OpenModelica with
OMC interface. Classes and models are simulated in ‘generic Modelica block’
of xcos in Scilab. As the core language of Modelica is C, hence it is presumed
that the reader’s have deep knowledge of properties and usability of C. The
depth of reading is for use of Modelica language in xcos, MapleSim simulators
only. For in-depth reading, use the Modelica language specifications document.
OMNotebook can also be used to compile and run the modelica codes. In one
execution cell, write the code and compile them. Call the model, function or
class from other execution cell. A new ModelicaInput/Command input cell can
also be created by the short-cut Alt+Enter.
1.1 Modelica Class
A class in Modelica is its fundamental structure. Classes provide the structure
for objects, also known as instances. Classes can contain equations, functions,
parameters etc that is used for computation in Modelica. The basic data types
used in Modelica are Real, Integer, String, Boolean and enumeration types. The
members of a class in Modelica have two levels, i.e. public or protected. Public
functions, members can be access from anywhere in the program while protected
functions, members can only be accessed within the class. Only public elements
of a class can be accessed via dot notation. Public elements are defined within
public heading while protected elements are defined within protected heading.
An element can not be declared twice or more. Modelica programs are built
from classes, also called models. From a class definition, it is possible to create
any number of objects that are known as instances of that class. A Modelica
class has syntax
✞
1 class <class name >
<variables /parameters >
3 equation
<equations >
5 end <class name >;
✌
✆
The instances of a modelica class is created as in C++ class. For above syntaxed
class, its instances is created like
✞
1 <class name > <instance name >;
✌
✆
A working example, explaining the equation ẋ = −kx is given below:
✞
1 class ALAW
6 Modelica Commands
3
www.sdmsacademy.in
Real x(start = 1);
parameter Real k = 1;
5 equation
der(x) = -k*x;
7 end ALAW ;
✌
✆
Here variable ‘x’ is real type and has a start value 1. A class said to be a partial
class if it can be called from other classes. For example, in electrical science,
there are hundreds of elements which have one input and one output. hence a
sub class can be declared for one input pin and one output pin.
✞
partial class TwoPins "Superclass of elements with two
electrical pins "
2 Pin p, n;
Voltage v;
4 Current i;
equation
6 v = p.v - n.v;
p.i + n.i = 0;
8 i = p.i;
end TwoPins ;
✌
✆
This partial class creates two pins ‘p’ and ‘n’ with voltage ‘v’ and current
‘i’. There are two values voltage and current computed by equations given
inequation block. Now, we have the partial class TwoPins which can be uses to
create a Resistor, by using relation V = i ∗ R. It describes the specific physical
characteristics of the relation between voltage and current for resistors.
✞
1 class Resistor "Ideal linear electrical Resistor "
// Access to partial class
3 extends TwoPins;
// Get relation between v & i
5 parameter Real R(unit = "Ohm") " Resistance ";
equation
7 R*i = v;
end Resistor ;
✌
✆
To simulate this class, first copy these codes in any one execution cell of OM-
Notebook and compile it. Use simulate() command to simulate the model in
second execution cell of OMNotebook as shown below:
✞
simulate ( Resistor ); // Argument is class name or model name
✌
✆
Get the output value by calling val() function for unknown variables as shown
below:
✞
1 val(v); // for solution value of potential
✌
✆
1.1. MODELICA CLASS 7
www.sdmsacademy.in
1.1.1 Identifiers
Identifiers are sequences of letters, digits, and other characters such as under-
score, which are used for naming various items in the language. Keywords
reserved for exclusive used of Modelica grammar can not be used as identifiers.
The first letter of an identifier must be a letter or underscore , followed by
any number of letters, digits, or underscores. Identifiers in Modelica are case
sensitive, i.e. ‘x’ and ‘X’ are two different identifiers.
1.1.2 Variable
A variable declared with prefix constant never changes. It is substituted by its
value wherever it occurs. A Real, Boolean, String and enumeration are other
type of variables. A real type variable is declared as
✞
1 class <class name >
Real x(start = 1) "Horizontal value";
3 Real y(start = 1) "Vertical Value";
equation
5 <equations >
end <class name >;
✌
✆
In modelica, variable can be a derived type. Derived type variable is that one
which is derived from the base varible.
✞
type Temp = Real (unit ="K"); // Temp variable
2 type AbsTemp = Temp (min =0); // Must be positive
✌
✆
Constant
Value of a constant type variable is remain unchanged during the execution of
class. It is declared as shown in the following syntax.
✞
class <class name >
2 constant PI 3.14 "PI value";
equation
4 <equations >
end <class name >;
✌
✆
String
String is a group or array of characters of variable size. A string type variable
is declared as:
8 Modelica Commands
5
www.sdmsacademy.in
✞
1 class <class name >
String Label "PI value";
3 equation
<equations >
end <class name >;
✌
✆
Escape Characters
String literals appear between double quotes as in “between”. Any character
in the Modelica language character set apart from double quote and backslash,
including new-line, can be directly included in a string without using an escape
code. Certain characters in string literals can be represented using escape codes,
i.e., the character is preceded by a backslash within the string. Those characters
are:
’ Single quote
" Double quote
? Question-mark
 Backslash itself
a Alert
b Backspace
f Form feed
n New-line
r Return
t Horizontal tab
v Vertical tab
Single quote and symbol of question mark may also appear without backslash
in string constants used in Modelica.
Real
A variable of type Real are obtained by solving the equations containing the
variable of type Real. A real type variable can store real values. There should
be same numbers of equations as there are unknown variables in the equation.
✞
1 model ADD
// First variable x
3 Real x;
// Second variable y
5 Real y;
equation
7 /* Required two equations to get *
*the values of variables x & y */
9 x+y=4;
1.1. MODELICA CLASS 9
www.sdmsacademy.in
x-y=1;
11 end ADD;
✌
✆
To simulate this model, first copy these codes in any one execution cell of OM-
Notebook and compile it. Use simulate() command to simulate the model in
second execution cell of OMNotebook as shown below:
✞
1 simulate (ADD);
✌
✆
Get the output value by calling val() function for unknown variables as shown
below:
✞
1 val(x); // for solution value of x
val(y); // for solution value of y
✌
✆
The output from above model is
✞
2.5
1.5
✌
✆
Integer
A variable of type Integer can store only integer values. If a real values is tried
to store in integer type variable, then its fraction part is truncated. It is declared
as
✞
model <Model Name >
2 parameter Integer y;
equation
4 ......
end <Model Name >;
✌
✆
1.1.3 Assignment
In Modelica, we can assign a default value 1.0 (say) to a variable ‘x’ (say) by
using ‘:=’ operator as shown below:
✞
1 class <class name >
input Real x := 1.0 // Default value 1.0 to x
3 equation
<equations >
5 end <class name >;
✌
✆
10 Modelica Commands
www.sdmsacademy.in
1.1.4 Parameters
The keyword parameter specifies to a variable as constant during its simulation
run. Its value can be initialized before a simulation run, or between simulation
runs. It makes variable of a special kind.
✞
1 class <class name >
Real x(start = 1) "Horizontal value";
3 Real y(start = 1) "Vertical Value";
parameter Real lambda = 0.3 "Slope";
5 equation
<equations >
7 end <class name >;
✌
✆
1.1.5 Comments
In Modelica, comments can be added inside the block of class or model by
starting ‘//’ symbol for a single line. For multiline comments, comments started
from ‘/*’ symbol and ended with next ‘*/’ symbol. Multiline nested comments
are not supported by Modelica.
✞
1 class <class name >
/* Here two varialbes are declared . They *
3 *are real type . One parameter variable *
*of real type is also declared . */
5 Real x(start = 1) "Horizontal value"; // Start with 1
Real y(start = 1) "Vertical Value"; // Start with 1
7 parameter Real lambda = 0.3 "Slope";
equation
9 <equations >
end <class name >;
✌
✆
There is another kind of documentation comment that is part of the Modelica
and therefore not ignored by the Modelica translator. Such comments may
occur at the ends of declarations, equations, or statements or at the beginning
of class definitions. For example:
✞
model MyModel
2 Real p "Conditional variable ";
Real y "Output Variable ";
4 algorithm
y := 0;
6 p:=1;
if(p<1) then
8 y := y + 0;
else
10 y := y + 10;
1.1. MODELICA CLASS 11
12
www.sdmsacademy.in
end if;
end MyModel;
✌
✆
1.1.6 Equations
Modelica is an equation-based language. Equations are more flexible and more
powerful than assignment statements. For example, product of resistance and
current is voltage.
Declaration Equations
Declaration equations are usually given as part of declarations of constants or
parameters. Such equations are actually required for constants, but are optional
for parameters. An equation always holds, which means that the mass never
changes value during simulation. It is also possible to specify a declaration
equation for a normal state variable, but this will make the variable behave as
a kind of constant.
✞
class DecEq
2 constant Integer one = 1; // Declaration equation
parameter Real mass = 22.5; // -"-
4 Real speed = 72.4; /* Declaration equation
for*
* a normal state variable
*/
6 end DecEq;
✌
✆
Modifier Equations
Modifier equations occur in a variable declaration when there is a need to modify
the default value of an attribute of the variable. A modifier equation replaces
the declaration equation for an attribute of the specific type. A common usage
is modifier equations for the start attribute of variables, the equations also occur
in type definitions.
✞
class ModEq
2 Real speed(start = 72.4); // Modifier equation
end ModEq;
✌
✆
If-Else Equation
An if -else equation can be declared in the equation block. This types of equa-
tions are used for conditionally execution of the equation. If if condition is
true then equation in if block is executed otherwise equation in else block is
12 Modelica Commands
www.sdmsacademy.in
executed. Each if condition shall be closed with end if. The syntax of this
function is
✞
1 class <class >
....
3 equation
if <condition > then
5 <sub equation >
elseif <condition > then
7 <sub equation >
else
9 <sub equation >
end if;
11 end <class >;
✌
✆
Example is given below:
✞
1 class IfEquation
parameter Real u = 4;
3 parameter Real a = 10;
parameter Real b = 2;
5 Real y;
equation
7 if u > a then
y = a;
9 elseif u < b then
y = b;
11 else
y = u;
13 end if;
end IfEquation ;
✌
✆
Classes are simulated similar to the simulation of modelica models.
For Equation
A for equation is a repetitive equation structures. Its syntax is as follows:
✞
for <indices > loop
2 <for equation >
end for
✌
✆
Remember that each opened for equation must be closed with end keyword as
shown in above syntax. An example for for equation is given below:
✞
1 constant Integer j=4;
Real x[j];
3 equation
for j in 1:j loop
1.1. MODELICA CLASS 13
www.sdmsacademy.in
5 x[j]=j;
end for;
✌
✆
Connect Equation
Connections between objects are introduced by connect equations in the equa-
tion part of a class. A connect equation has the following syntax:
✞
connect(<ref a>, <ref b>);
✌
✆
See the example below:
✞
1 class myClass
Component com[n];
3 equation
for i in 1:n-1 loop
5 connect (com[i].o,com[i+1]. i);
end for;
7 end myClass;
✌
✆
When Equation
when is used to compare two operands. If the condition is met true, codes inside
the when equation are executed otherwise they are skipped at condition of false.
Its syntax is used as
✞
1 when <condition > then
<codes >
3 end when
✌
✆
See the example, below:
✞
1 model BouncingBall
constant Real g = 9.81;
3 parameter Real c = 0.9;
Real height(start = 1,fixed=true );
5 Real velocity (start = 10, fixed=true );
equation
7 der(height) = velocity ;
der(velocity ) = -g;
9 when height <= 0 then // when condition
reinit(velocity , -c*velocity );
11 end when ;
end BouncingBall ;
✌
✆
Each when equation must be closed by using keyword end. In when equation,
the statement is always in the form of
14 Modelica Commands
www.sdmsacademy.in
✞
<variable > = <expression >
✌
✆
1.1.7 Extends
extends keyword extends the scope of current class to the other function. By
this way, code becomes harder to read since the formal parameter list is not
available in the functions. The partial base function is
✞
1 partial function pFunction
input Real x;// Input arg
3 output Real y;// Output arg
end pFunction ;
✌
✆
This partial base function is accessed by function ‘mFunc’ using extends keyword
as shown below:
✞
function mFunc
2 extends pFunction ;
algorithm
4 y := sin(x)/cos(x);
end mFunc;
✌
✆
extends keyword is used to access or inherit the restricted class.
1.1.8 Specialized Class
In Modelica, there are few specialized classes. For example, record, type, model,
block, package, function, connector are specialized classes and they have special
uses.
record This class as well as its components are allowed to be used as public
only. The elements of a record may not have prefixes input, output, inner, outer,
stream, or flow.
type It is predefined types, enumerations, array of type, or classes extending
from type.
model It is identical to class.
block It is same as model. It has restriction that each connector component
of the block must have input and/or output as prefixes for all connector variables.
connector Connectors are used to connect two or more Modelica blocks. It
and its components are allow to use in public mode.
package It contains declarations of classes and constants only.
1.2. FUNCTIONS 15
www.sdmsacademy.in
operator record It is similar to record. In operator record, operator over-
loading is possible.
operator It is similar to package, but it have only declarations of functions.
operator function It is short name of an operator with exactly one function.
It has same restriction as the function has.
1.1.9 Inheritance
Inheritance is a property that describes a relationship between two (or more)
types or classes or objects. It is possible to derive one class from another class
or even several classes. A base class is a class that is created with the intention
of deriving other classes from it. A child class is a class that is derived from
another class, i.e. from parent class.
✞
1 record Constants // Record data is base class
parameter Real a = 0.2;
3 parameter Real b = 0.6;
Real c;
5 end Constants ;
class myClass // child class
7 extends Constants ; // Access inheritance
equation
9 a + b + c = 1;
end myClass;
✌
✆
In above example, we declare a restrictive type class of ‘Constants’ with three
parameters. A child class ‘myClass’ is defined with inherited property of the
‘Constants’ class. To access the inheritance, extends keyword is used. In inher-
itance, the parameters with same name with or without same values can not be
re-declared.
1.2 Functions
Function is a group of coding scripts. It takes inputs and gives outputs. The
internal process of a function are performed by these coding scripts. The internal
process of a function is never checked by the compiler. Compiler only checks
whether rules of programming of the language are followed or not. This is why,
function behaves like a black box.
1.2.1 Function
In Modelica, there are several pre-declared mathematical functions like abs,
sqrt, mod, etc. Some other functions are available under Modelica libraries. For
example, sin, cos, exp, etc. are available in the math library Modelica.Math.
16 Modelica Commands
www.sdmsacademy.in
The arithmetic operators +, -, *, / can be regarded as functions that are used
through a convenient operator syntax. In Modelica, a function is declared as
✞
function MyFunc
2 input Real a[:]; // Array with variable size
input Real x := 1.0; // Default value 1.0 for x
4 output Real y;
protected // protect local variables
6 Real p;
algorithm
8 y := 0;
p := 1;
10 for i in 1: size (a,1) loop
y := y + a[i]*p;
12 p := p*x;
end for;
14 end MyFunc;
✌
✆
The above function can be called as
✞
z = MyFunc ({1, 2, 3, 4}, 21);
✌
✆
or as
✞
1 z = MyFunc(a={1, 2, 3, 4}, x=21) ;
✌
✆
Here output ‘y’ is assigned to variable ‘z’. A function may be several outputs
depends on how many output variables are declared in the function by using
output keyword.
✞
1 function MyFunc
input Real a;
3 input Real b;
output Real s;
5 output Real d;
output Real m;
7 algorithm
s := a+b; // sum output
9 d := a/b; // division output
m := a*b; // product output
11 end MyFunc;
✌
✆
Calling the function like
✞
1 MyFunc (2.0, 1.0)
✌
✆
Output is given as
✞
(3.0, 2.0, 2.0)
✌
✆
1.2. FUNCTIONS 17
www.sdmsacademy.in
We can assign the return values to other variables. To assign return values
to local variables, function is called as
✞
1 (i, j, k)=MyFunc (2.0, 1.0)
✌
✆
This function call will assign i = 3.0, j = 2.0 and k = 2.0 respectively. A
function can also be called from inside the class. The example is given below
✞
1 class MyFuncCall
Real s,d,p;
3 equation
(s,d,p) = MyFunc (2.0, 1.0) ;
5 end MyFuncCall ;
✌
✆
We can simulate the class or model like
✞
1 simulate ( MyFuncCall , stopTime =10 )
✌
✆
Result is stored in the variables ‘s’, ‘d’ and ‘p’. These results can be plotted by
using function plot().
1.2.2 Function Return Value
The return statement terminates the current function call. It can only be used
inside an algorithm section of a function.
✞
1 function MyFunc
.....
3 algorithm
if <case > then
5 return;
end if;
7 .....
end MyFunc;
✌
✆
1.2.3 Protected Variable
Inside functions or classes, some variables are declared for internal calculations.
These variables are instances of the function and may be accessed from the out-
side of the function. To protect their scope and accessibility, protected keyword
is used.
✞
function MyFunc
2 <inputs >/< outputs >
protected // protect local variables
4 Real p;
algorithm
18 Modelica Commands
www.sdmsacademy.in
6 <algorithmic statements >
end MyFunc;
✌
✆
1.2.4 Algorithms
Algorithmic statements in Modelica occur only within algorithm sections, start-
ing with the keyword algorithm. Algorithm sections may also be called algo-
rithm equations, since an algorithm section can be viewed as a group of equa-
tions involving one or more variables, and can appear among equation sections.
Algorithm sections are terminated by the appearance of one of the keywords
equation, public, protected, algorithm or end.
✞
1 .....
algorithm
3 y := 0;
p := 1;
5 for i in 1: size (a,1) loop
y := y + a[i]*p;
7 p := p*x;
end for;
9 .....
✌
✆
Inside the algorithm section, equations are solved by using statements. While
in equations the values assigned to the variables are solved mathematically.
1.2.5 Partial Function
A partial function returns a partially evaluated function that is also a function,
with the remaining not bound formal parameters still present in the same order
as in the original function declaration. A partial function is specified by the
function keyword followed by a function call.
✞
1 partial function pFunction
input Real x;
3 output Real y;
end pFunction ;
✌
✆
In a function, input argument is intialized by input keyword while ouput argu-
ment is initialized by output keyword.
1.2.6 External Language
In Modelica, the default language used for computation is C. We can use other
languages like Fortran by using keyword external. To use built in function in
Modelica, external is assigned “builtin” value. The builtin function or native
function of Modelica are accessed by using dot (.) as shown below:
1.2. FUNCTIONS 19
2
www.sdmsacademy.in
✞
package Modelica
package Math
4 function sin
input Real x;
6 output Real y;
external "builtin ";
8 end sin;
end Math ;
10 end Modelica ;
model UserModel
12 output p= Modelica .Math .sin (2);
end UserModel ;
✌
✆
Other values for external keyword are ”C” and “FORTRAN 77”.
1.2.7 No Events
We know that the square root of a negative number is an imaginary output. This
is why, the expressions containing square root function should not be evaluated
when an argument is negative. To avoid such problems, noEvent() function is
used. The syntax of this function is
✞
noEvent(if x<0 then 0 else sqrt (x)*x));
✌
✆
It prevents the zero crossing of the function.
1.2.8 Smooth
Another way to prevent zero crossing is smooth() function. This function is
used as
✞
1 smooth(0, if t<0 then 0 else t);
✌
✆
However, smooth does not guarantee that no events will be generated, and thus
it can be necessary to use noEvent inside smooth.
1.2.9 Reinitiate
The reinit keyword is used inside the body of a when-equation to re-initiate the
values of a variable. It has the following syntax:
✞
1 reinit(<variable >, <expression >);
✌
✆
reinit reinitialize the value of variable ‘variable’ to ‘expression’ at an instant of
event. Assume a variable ‘x’ whose value is decreases by one from 10 to continue.
20 Modelica Commands
www.sdmsacademy.in
To prevent its zero crossing, reinit function is used to reset the variable value to
10. reinit function may be expressed in form piecewise function as given below:
f =

x = 10 if x ≤ 0
x otherwise
In Above piecewise equation, whenever ‘x’ becomes zero or negative, it is reset
to 10. The assigned value to ‘f’ is 10 on reset and instantaneous value to variable
‘x’ otherwise. Following is bouncing ball model that uses reinit to change the
velocity direction of ball when it hits the ground.
✞
1 model BouncingBall The bouncing ball model
constant Real g = 9.81;
3 parameter Real c = 0.9; // Elasticity constant of ball
Real height(start = 1,fixed=true );
5 Real velocity (start = 10, fixed=true );
equation
7 der(height) = velocity ;
der(velocity ) = -g;
9 algorithm
when height = 0 then
11 reinit(velocity , -c*velocity );
end when ;
13 end BouncingBall ;
✌
✆
1.3 Events
An event is simply something that happens. It is true in the mathematical
sense. Modelica provides a number of built-in functions and operators related
to events and time. One function that can be used for generating repeated
events is sample.
1.3.1 Samples
The function sample() returns true and triggers events at instances of time. Its
syntax is
✞
1 sample(initial time (t_0), time delay (d) )
✌
✆
It creates events at time t0, t0 + d, t0 + 2d, . . .. See the example below:
✞
1 model mySample
parameter Real sample_interval = 0.05;
3 Real x(start=5);
Real y;
5 equation
der(x) = -x;
1.3. EVENTS 21
9
www.sdmsacademy.in
7 when sample (0, sample_interval ) then
y = x;
end when ;
11 end mySample ;
✌
✆
To simulate this model, use following sequence of commands in OMNotebook
execution cells.
✞
simulate (mySample )
2 plot (y)
✌
✆
1.3.2 Previous Sample Value
At an event, pre() gives the previous value of variable passed as its argument,
immediately before the event, except for event iteration of multiple events at
the same point in time when the value is from the previous iteration. It can not
be used within function. Its syntax is
✞
pre(variable );
✌
✆
1.3.3 Change Event
change() returns true when a value get changed at any instant of time. It is
used like
✞
1 change(value );
✌
✆
1.3.4 Edge
Similarly, edge() returns true when argument switches from false to true at any
instant of time. It is used like
✞
1 edge (value );
✌
✆
1.3.5 Delay
Function delay() is used to create a time delay for a variable. Its syntax is
✞
1 delay(variable , time delay );
✌
✆
22 Modelica Commands
www.sdmsacademy.in
1.4 Statements
A statement is any declarative sentence which is either true or false or have a
definite result.
1.4.1 Break
break keyword is similar to the keyword terminate. It is always used in side a
loop. It terminates the current loop and exit the computation out of the loop.
✞
1 model ADD
Real y;
3 Real p;
algorithm
5 y := 0;
p := 1;
7 for p in 1:10 loop
if p5 then
9 break;
end if
11 y := y + p;
end for;
13 end ADD;
✌
✆
1.4.2 Else
else is a part of if -else. First if checks the conditions and if condition is true
then expressions inside the if keyword is executed otherwise expressions inside
else is executed. Its syntax is
✞
1 if condition  then
expression 
3 elseif condition  then
expression 
5 else
expression 
7 end if;
✌
✆
Begining of if keyword should be ended with the end if keyword. See example
below:
✞
1 model MyModel
Real p;
3 Real y;
algorithm
5 y := 0;
1.4. STATEMENTS 23
www.sdmsacademy.in
p:=1;
7 if(p1) then
9 y := y + 0;
else
11 y := y + 10;
end if;
13 end MyModel;
✌
✆
1.4.3 For
This is a conditional function. Used to create loop with few conditions. It is
used inside the algorithm section of the Modelica model.
✞
model ADD
2 Real y;
Real p;
4 algorithm
y := 0;
6 p := 1;
for p in 1:10 loop
8 y := y + p;
end for;
10 end ADD;
✌
✆
Each for should be closed with end keyword as shown in above example.
1.4.4 If
if -else is group of keyword for conditional execution of expression either inside
if or inside else. First if checks the conditions and if condition is true then
expressions inside the if keyword is executed otherwise expressions inside else
is executed. Its syntax is
✞
if condition  then
2 expression 
elseif condition  then
4 expression 
else
6 expression 
end if;
✌
✆
Any block opened with if keyword should be closed with the end if keyword.
✞
1 model MyModel
Real p;
3 Real y;
algorithm
24 Modelica Commands
7
www.sdmsacademy.in
5 y := 0;
p:=1;
if(p0) then
9 y := y + 0;
else
11 y := y + 10;
end if;
13 end MyModel ;
✌
✆
1.4.5 In
in is used to create a set of data. For example, by using ‘:’ as shown below
✞
in 1:10 loop // 1, 2, ..., 9, 10
✌
✆
a set or an array is created having values from 1 to 10 (equal to or less than).
The difference between two consecutive values is 1. If in is modified like
✞
1 in 1:2:10 loop // 1, 3, 5, 7, 9
✌
✆
then, set or array is created have elements from 1 to 10 (equal to or less than)
having common difference of 2.
1.4.6 Loop
for, while functions are used as iterative functions. loop is post-fixed with
these function to perform iterations. The expression of a for-equation or while-
equations is a vector expression. It is evaluated once for each for-equation
or while-equations, and are evaluated in the scope immediately enclosing the
for-equation or while-equation. The expression of a for-equation shall be a
parameter expression. The loop-variable ( IDENT ) is in scope inside the loop-
construct and shall not be assigned to. The loop-variable has the same type as
the type of the elements of the vector expression.
✞
1 for i in 1:10 loop //i has 1, 2, 3, ..., 10
for r in 1.0:1.5:5.5 loop //r has 1.0, 2.5, 4.0, 5.5
3 for i in {1,3,6,7} loop //i has 1, 3, 6, 7
✌
✆
See the example below:
✞
1 model ADD
Real y;
3 Real p;
algorithm
5 y := 0;
p := 1;
7 for p in 1:10 loop
1.4. STATEMENTS 25
www.sdmsacademy.in
y := y + p;
9 end for;
11 end ADD;
✌
✆
1.4.7 Stream
A variable is declared stream type in a connector that describe a bi-directional
flow of matter. A connector is said to be stream type connector if it has atleast
one stream connector. A connector is declared as stream type by using this
keyword as prefix.
✞
stream hflow;
✌
✆
The stream properties for a variable can be inquired by using the built-in oper-
ator ‘inStream()’. The value of the stream variable corresponding to the actual
flow direction can be inquired through the built-in operator ‘actualStream()’.
1.4.8 Terminate
terminate() statement is used to terminate the analysis which was carried out.
It is used inside the conditional statement. See example below:
✞
1 model MyBlock
Real x(start =0);
3 Real y(start =1);
equation
5 x+y=5;
x-y=6;
7 algorithm
when y0 then
9 terminate (Analysis ended..);
end when ;
11 end MyBlock;
✌
✆
1.4.9 Then
then keyword is used with conditional keywords like if, when, for and while etc.
If condition is true then expressions in first block are executed otherwise the
expression is either skipped or expressions in next block are executed.
✞
1 // Check the condition for when
when x  2 then
3 /*if condition is true then execute*
*the expressions of this block. */
5 y2 = sin(x);
end when ;
✌
✆
26 Modelica Commands
www.sdmsacademy.in
Each when should be closed by end when keyword as shown above.
1.4.10 When
when-then is a conditional statement. when keyword is used to check the con-
dition and if condition is satisfied then the expression is executed otherwise the
expression is skipped.
✞
// Check the condition for when
2 when x  2 then
/*if condition is true then execute *
4 *the expressions of this block. */
y2 = sin(x);
6 end when ;
✌
✆
Each when should be closed by end when keyword as shown above.
1.4.11 While
while keyword is used to compare between two operands and perform a loop
until the conditions are met true. Each while is followed by do keyword as
shown below:
✞
while coditions  do
2 codes 
end while
✌
✆
Each while must be terminated by end keyword. See the example below:
✞
1 function myFunc
Real y = 1;
3 input Real a;
output Real s;
5 algorithm
while ya loop
7 s := a+y;
y := y+1;
9 end while;
end myFunc;
✌
✆
On calling the function like
✞
myFunc (15)
✌
✆
we get output
✞
29.0
✌
✆
1.5. MATHEMATICS 27
www.sdmsacademy.in
1.5 Mathematics
1.5.1 Expression
Expression is a mathematical relation have one or more mathematical operators.
For example, f(x) = x2
−2x+3 is a mathematical expression having three terms.
1.5.2 Operators
Operators are those symbols which are used to performed operations between
two operands. For example, ‘+’ symbol is used for addition between two
operands. Operators also perform comparison, binary manipulation too.
Arithmetic Operators
Arithmetic operators performs simple addition, subtraction, division, multipli-
cation and exponents etc. A list of arithmetic operators is shown in the following
table.
Operator Description
ˆ Exponent operator, raised the power ‘p’
over the base ‘b’.
/ Division operator. It divides the left
value with right value. Returns the
quotient. If, both left and right values
are integer then decimal part of the re-
sult is truncated.
∗ Multiplication operator. It multiply the
left value with right value. Returns
product of two numbers.
+ Addition operator. It adds the right
value with left value.
− Subtraction operator. It subtracts the
right value from left value.
Table 1.1: Arithmetic operators.
The examples of each arithmetic operator is given below:
✞
1  2^3
✌
✆
28 Modelica Commands
www.sdmsacademy.in
✞
8.0
✌
✆
✞
1  2/3
✌
✆
✞
0.666666666
✌
✆
✞
1  2*3
✌
✆
✞
6
✌
✆
✞
1  2+3
✌
✆
✞
5
✌
✆
✞
1  2-3
✌
✆
✞
-1
✌
✆
Element-wise Operation
When either of the two operands of an arithmetic operators are scalar of vector
array then, arithmetic operators are performed element-wise. For example,
A = [1, 2, 4]; B = [5, 6, 3]
then A+ B or A− B shall be performed elementwise, i.e. first element of vector
A shall be summed or subtracted by first element of the vector B. So,
A + B = [6, 8, 7]; A − B = [−4, −4, 1]
See the following example:
✞
1 model Elem
Real A[3] = {1, 2, 4};
3 Real B[3] = {5, 6, 3};
Real Sum [3];
5 Real Dif [3];
equation
7 Sum = A + B; // Result: {6, 8, 7}
Dif = A - B; // Result: {-4, -4, 1}
9 end Elem ;
✌
✆
1.5. MATHEMATICS 29
www.sdmsacademy.in
Simulate and call each elements of ‘Sum’ and ‘Dif’ vectors as shown below:
✞
1 simulate ( Elem );
val(Sum [1]) ;
✌
✆
Output is
✞
6
✌
✆
Relational Operators
The relation and assignment operators are shown in the table given below.
These operators are only defined for scalar operands of simple types. The result
is Boolean and is true or false if the relation is fulfilled or not, respectively. For
operands of type Boolean, falsetrue.
Operator Description
 Less than
 Greater than
≤ Less than or equal to
≥ Greater than or equal to
== Equals
 Inequality
1.5.3 Arithmetic Functions
We can write own model for arithmetic functions. Each model takes inputs and
returns result as outputs. There are several ready-made arithmetic function in
Modelica. We can rewrite them as per our requirement too. Few models of
arithmetic operators are explained in following sections.
Addition
Addition of two integers in Modelica is takes place by arithmetic symbol ‘+’.
Here a Modelica model ADD is created with one equation which adds two
parametric integers and result is stored in variable sum. The simulation model
for ADD block is given below:
✞
1 model ADD
parameter Integer x = 1;
3 parameter Integer y = 2;
Real sum;
30 Modelica Commands
7
www.sdmsacademy.in
5 equation
sum=x+y;
end ADD;
✌
✆
An addition function for evaluation of sum of two input numbers is given below:
✞
1 function MySum
input Real a;
3 input Real b;
output Real s;
5 algorithm
s := a+b; // Addition output
7 end MySum;
✌
✆
On calling above function as
✞
1 MySum(2,3)
✌
✆
The output is
✞
5
✌
✆
Minimum
The function min() is used to return the minimum value of the supplied ele-
ments. It is used like
✞
1 min(A) // Returns the smallest element of array A.
min(x,y) // Returns the smallest element between x and y.
✌
✆
The argument of min function may be an array or only two number arguments.
It returns the minimum value among all the elements or between two arguments.
If number arguments are more than two, then Modelica returns “wrong number
of elements” error. A function for getting minimum value of two input numbers
is given below:
✞
function MyMin
2 input Real a;
input Real b;
4 output Real s;
algorithm
6 s := min(a,b); // Minimum output
end MyMin;
✌
✆
On calling above function as
✞
1 MyMin(2,3)
✌
✆
1.5. MATHEMATICS 31
www.sdmsacademy.in
The output is
✞
2
✌
✆
As simulation model block, the min() function may be used as
✞
1 model MINIMUM
Real x;
3 Real y;
Real m;
5 equation
m=min(x,y);
7 end MINIMUM;
✌
✆
Maximum
The function max() is used to return the maximum value of the supplied ele-
ments. It is used like
✞
1 max(A) // Returns the greatest element of array A.
max(x,y) // Returns the greatest element between x and y.
✌
✆
The argument of max function may be an array or only two number arguments.
It returns the maximum value among all the elements or between two arguments.
If number arguments are more than two, then Modelica returns “wrong number
of elements” error.
✞
 max(2, -1)
✌
✆
✞
2
✌
✆
As simulation block, the max() function may be used as
✞
1 model MAXIMUM
Real x;
3 Real y;
Real m;
5 equation
m=max(x,y);
7 end MAXIMUM;
✌
✆
Products
The function product() is used to return the product value of the supplied ele-
ments. It is used like
32 Modelica Commands
www.sdmsacademy.in
✞
1 product(A) // Returns the products of all elements of array
A.
product(x, y) // Returns the product of elements x and y.
✌
✆
An product function for evaluation of product of two input numbers is given
below:
✞
function MyProd
2 input Real a;
input Real b;
4 output Real s;
algorithm
6 s := a*b; // Product output
end MyProd;
✌
✆
On calling above function as
✞
1 MyProd (2,3)
✌
✆
The output is
✞
6.0
✌
✆
Subtraction
‘–’ symbol is used to subtract the real numbers or integers. See example below
in which MySub function is written.
✞
1 function MySub
input Real a;
3 input Real b;
output Real s;
5 algorithm
s := a-b; // subtraction output
7 end MySub;
✌
✆
On calling above function as
✞
1 MySub(2,3)
✌
✆
The output is
✞
-1
✌
✆
A simulation model for subtraction block is given below:
1.5. MATHEMATICS 33
5
www.sdmsacademy.in
✞
1 model SUB
parameter Integer x = 1;
3 parameter Integer y = 2;
Real sum;
equation
7 sum=x-y;
end SUB;
✌
✆
Summation
The function sum() is used to return the sum value of the supplied elements. It
is used like
✞
1 sum(A) // Returns the sum of elements of array A.
sum(x,y) // Returns the sum of elements x and y.
✌
✆
See the example below, in which array’s elements are summed together and
result is returned.
✞
function MySum
2 input Real m[7] = {17.1 , 62.7, 7.5, 37.0, 25, 4.121 ,
12.333336};
output Real s;
4 algorithm
s := sum(m); // summation of array
6 end MySum;
✌
✆
On calling MySum as
✞
MySum()
✌
✆
The output result is
✞
165.754336
✌
✆
Division
div() function returns the division of two numbers. This function is used as
✞
1 div(n, d)
✌
✆
Here, ‘n’ is numerator and ‘d’ is denominator. Denominator can not be equals
to zero.
34 Modelica Commands
www.sdmsacademy.in
Modulo
mod() function returns the modulo of a number. If argument of mod() function
is negative number then it converted it into positive numbers. It does nothing
with positive numbers.
✞
1 mod(a real number )
✌
✆
Remainder
rem() returns the remainder value of dividend when it is divided by a divisor.
For example, when 5 is divided by 2, (5/2), it gives a remainder of 1. Its syntax
is
✞
1 rem(n, d)
✌
✆
Ceiling
ceil() is rounded up the given argument to next higher integer value. For ex-
ample, 1.25 is rounded up to 2. Similarly, −1.25 is rounded upto −1.
✞
1  ceil ( -1.25)
✌
✆
✞
-1.0
✌
✆
✞
1  ceil (1.25)
✌
✆
✞
2.0
✌
✆
This function is also called greatest integer function. For argument x, it can be
represented also as [x].
Floor
floor() is rounded down the given argument to next lower integer value. For
example, 1.25 is rounded down to 1. Similarly, −1.25 is rounded upto −2.
✞
1  floor( -1.25)
✌
✆
✞
-2.0
✌
✆
1.5. MATHEMATICS 35
www.sdmsacademy.in
✞
1  floor (1.25)
✌
✆
✞
1.0
✌
✆
Integer
integer() returns the floor of the given argument and truncates its fraction part.
Only whole number (i.e. integer) is returned by this function. For example, 1.25
is rounded down to 1. Similarly, −1.25 is rounded upto −2.
✞
1  floor( -1.25)
✌
✆
✞
-2
✌
✆
✞
1  floor (1.25)
✌
✆
✞
1
✌
✆
1.5.4 Algebraic
Absolute
abs() converts negative number as its input argument into positive number as
its output and do nothing with positive inputs. Piece-wise relation equivalent
to this function is given below:
abs =

−x if x  0
x otherwise
✞
1  abs ( -1.25)
✌
✆
✞
1.25
✌
✆
✞
1  abs (1.25)
✌
✆
✞
1.25
✌
✆
36 Modelica Commands
www.sdmsacademy.in
Sign
sign() returns the sign of an input argument. If the argument is a positive
number then it returns ‘1’ and if the argument is a negative number then it
returns ‘-1’. Piece-wise relation equivalent to this function is given below:
sign =

−1 if x  0
1 otherwise
✞
1  sign ( -1.25)
✌
✆
✞
-1
✌
✆
✞
1  sign (1.25)
✌
✆
✞
1
✌
✆
Square Root
sqrt() returns the square root of the supplied argument. The argument should
be only positive. Negative argument returns error. Assume that if x is a positive
number then its square root is given by
y =
√
x
✞
1  sqrt ( -1.25)
✌
✆
✞
Error: Negative value as argument to sqrt .
✌
✆
✞
1  sqrt (1.25)
✌
✆
✞
1.118033988749895
✌
✆
1.5.5 Trigonometric
Modelica has inbuilt trigonometric functions like, sin(), cos(), tan(), asin(),
acos(), atan(), atan2(), sinh(), cosh() and tanh(). These functions has usual
meaning. Similarly, there are few inbuilt logarithmic functions, such as exp(),
log() and log10().
1.6. CONNECTIONS 37
www.sdmsacademy.in
1.5.6 Special Functions
Special functions are readymade functions which deals with advanced mathe-
matics, like derivation, integrals, and advanced functions, like Bessel, Laplace
function etc.
Derivative
A first order derivative equation is given by
dx
dt
= −kv
It can also be written as
ẋ = −kv
It means the derivative of distance of an object from origin at a time t is the
velocity of the object. Here, k is a constant parameter. In Modelica, it is written
as
✞
1 der(x) = -kv
✌
✆
der() is special built-in derivative operator equivalent to derivative. Mathe-
matically, der(x) is equivalent to ẋ. To solve this equation, we need the same
numbers of equations equal to the numbers of unknown variables in the equa-
tion.
1.6 Connections
Connectors are used to make connection between two or more variables. Con-
nectors are defined outside the class and are used in equation part of the class. In
Modelica, there are three types of connectors, “Potential”, “flow” and “stream”.
1.6.1 Connector
A “Potential” type connector represents to the some kind of energy level. A
“Potential” type connectors are shown in the example given below:
✞
1 connector mPin
Real v;
3 flow Real i;
end mPin ;
5 class myPin
// input variables
7 mPin p;
// output variables
9 mPin n;
38 Modelica Commands
www.sdmsacademy.in
Real v;
11 equation
13 p.i=0;
n.i=0;
15 v=p.v-n.v;
end myPin;
✌
✆
Flow Connectors
The keyword flow declares a connector having net flow zero through itself. For
example, flow of current through a node. The value of a flow variable is positive
when the flow is into the component and negative when the flow is out from the
component.
Stream Connectors
If at least one variable in a connector is stream type, the connector is called
“stream connector” and the corresponding variable is called “stream variable”.
To declared a connector as stream connector, stream is prefixed to a variable
and it can only be used in a connector declaration. For every outside connector,
one equation is generated for every variable with the stream prefix. For inside
connectors, variables with the stream prefix do not lead to connection equations.
Connection equations with stream variables are generated in a model when using
the inStream() operator or the actualStream() operator.
1.6.2 Connect
Connections between objects are introduced by connect equations in the equa-
tion part of a class. A connect equation has the following syntax:
✞
1 connect(ref a, ref b);
✌
✆
✞
1 package test
model mymodel
3 Modelica .Electrical .Analog.Basic.Resistor r1;
Modelica .Electrical .Analog.Basic.Capacitor c1;
5 equation
connect (r1.p, c1.n);
7 end mymodel ;
end test ;
✌
✆
1.6.3 Input
input keyword is used the set the input variable in partial function or class.
Variables declared as input assigned the values received from argument. The
1.6. CONNECTIONS 39
www.sdmsacademy.in
result is stored in the variable declared as output. The values from arguments
are assigned first come first serve basis. Values are assigned to the input variable
from top to bottom.
✞
function MyFunc
2 input Real a; //a=2
input Real b; //b=1
4 output Real s;
output Real d;
6 output Real m;
algorithm
8 s := a+b; // sum output
d := a/b; // division output
10 m := a*b; // product output
end MyFunc;
✌
✆
Calling the function like
✞
1 MyFunc (2.0, 1.0)
✌
✆
Output is given as
✞
(3.0 ,2.0 ,2.0)
✌
✆
Keeping the variable ‘b’ top of the variable ‘a’:
✞
1 function MyFunc
input Real b; //b=2
3 input Real a; //a=1
output Real s;
5 output Real d;
output Real m;
7 algorithm
s := a+b; // sum output
9 d := a/b; // division output
m := a*b; // product output
11 end MyFunc;
✌
✆
Calling the function like
✞
1 MyFunc (2.0, 1.0)
✌
✆
Output is given as
✞
(3.0 ,0.5 ,2.0)
✌
✆
40 Modelica Commands
www.sdmsacademy.in
1.6.4 Output
It defines the output variables where results are stored. A output variable is
declared as
✞
1 output Real a;
output Integer b;
✌
✆
1.6.5 N
In electric module of Modelica, negative pin is denominated by ‘n’. It is declared
as
✞
Pin n;
✌
✆
1.6.6 P
In electric module of Modelica, positive pin is denominated by ‘p’. It is declared
as
✞
1 Pin p;
✌
✆
1.6.7 Expandable Connectors
If the expandable qualifier is present on a connector definition, all instances
of that connector are referred to as expandable connectors. Instances of con-
nectors that do not possess this qualifier will be referred to as non-expandable
connectors. Properties of the expandable connectors are
1. The expandable connectors can only be connected to other expandable
connectors.
2. If a variable appears as an input in one expandable connector, it should
appear as a non-input in at least one other expandable connector instance
in the same augmentation set.
3. An augmentation set is defined as the set of connected expandable connec-
tor instances that through the elaboration will have matching variables.
4. All components in an expandable connector are seen as connector instances
even if they are not declared as such.
5. An expandable connector may not contain a component declared with the
prefix flow, but may contain non-expandable connector components with
flow components
1.7. ARRAYS 41
www.sdmsacademy.in
6. The expandable connector instance is automatically augmented with a
new component having the used name and corresponding type.
7. If the undeclared component is sub-scripted, an array variable is created,
and a connection to the specific array element is performed.
8. If the variable on the other side of the connect-equation is input or output
the new component will be either input or output
1.7 Arrays
In Modelica, array types are
Type Description
Array Type A = elem A Scalar
Array Type A[n] = elem A 1D, n-elements Vector
Array Type A[E] = elem A 1D, enumerated index vector
Array Type A[n, m] = elem A n × m Matrix
Array Type A[n1, . . ., nk] = elem; k dimensional (k ≥ 0) array.
In Modelica, an array is created as a record containing several vectors and
matrices. We can choose what we required. A demo record data is given below.
✞
1 record Matrix
Real m1 [7] = {17.1 , 62.7, 7.5, 37.0, 25, 4.121 ,
12.333336};
3 Integer m2 [4, 2] = {{44, 5}, {2, 800}, {7, 23}, {2032 ,
77}};
String[1, 5] m3 = {{Magnus, circle, boy,
Marina, Miranda}};
5 String m4 [:];
Boolean m5 [:, 2];
7 Boolean m6 [0];
end Matrix;
✌
✆
1.7.1 Range Vector
A range vector is a vector containing a range of numeric values increasing or
decreasing by a fixed increment or decrements value for each additional element.
A range vector is created by using symbol ‘:’. There are two ways of using symbol
‘:’. Exampl for first way is given below:
42 Modelica Commands
www.sdmsacademy.in
✞
class RangeVectors
2 Integer v1 [6] = 3 : 8; //{3, 4, 5, 6, 7, 8}
4 Integer v2 [3]= -20 : -18; //{-20, -19, -18}
Real v3 [3] = 11.3 : 13.3;// {11.3 , 12.3, 13.3}
6 Integer v4 [0]; // Empty range vector
end RangeVectors ;
✌
✆
The default magnitude of common difference in the above method is ‘1’. For
different common difference, range vector is used as
✞
class RangeVectors
2 Integer v1 [6] = 3 :2: 8; //{3, 5, 7}
Integer v2 [3]= -20 :2: -18; //{-20, -18}
4 Real v3 [3] = 11.3 :1.5: 13.3;// {11.3 , 12.8}
end RangeVectors ;
✌
✆
1.7.2 Size Function
size() is used to get a vector of length of an array containing the dimension sizes
of array. It is used like
✞
1 size (array )
✌
✆
To get the specific element of the array, we use an index reference like
✞
1 size (array , index )
✌
✆
ndims() is used to get the number of dimensions of array.
1.7.3 Array Concatenate
General array concatenation can be done through the array concatenation op-
erator cat() that concatenates the arrays. The syntax of this function is
✞
1 cat(k, A, B, C, ...)
✌
✆
Here, k is the dimension of array concatenated. Concatenating along the first
dimension (1) is to concatenate rows and the second dimension (2) is to con-
catenate columns. See example below:
✞
1 class Concat
// Row concatenation k=1
3 Real [5] c1 = cat(1, {1, 2}, {10, 12, 13}) ;
// Result: {1, 2, 10, 12, 13}
5
// Column concatenation k=2
1.7. ARRAYS 43
www.sdmsacademy.in
7 Real [2, 3] c2 = cat(2, {{1, 2}, {3, 4}}, {{10} , {11}});
// Result: {{1, 2, 10}, {3, 4, 11}}
9 end Concat
✌
✆
A matrix can be constructed from scalar expressions by separating rows by
semicolon and columns by comma. The previous example of concatenation
along the second dimension can be expressed like this:
✞
1 class Concat2
Real [2, 3] c3 = cat(2, [1, 2; 3, 4], [10; 11]) ;
3 // Result: {{1, 2, 10}, {3, 4, 11}}
end Concat2;
✌
✆
1.7.4 Array Index
The Array Indexing operator is used to access array elements for retrieval of their
values or for updating these values. When at least one of the index expressions
in an array indexing operation is a vector expression, a slice or subsection of
the array is accessed rather than a single element.
✞
class ArrayIndex
2 Real [2, 2] A = {{2, 3}, {4, 5}}; // Definition of array
A
Real A_Retrieval = A[2, 2]; // Get array element
value 5
4 Real B[2, 2];
Real c;
6 algorithm
B := fill (1,2,2) ; // B will have the values {{1, 1},
{1, 1}}
8 B[2, 1] := 8; // Assignment to the array element
B[2, 1]
c := A[1, 1];
10 end ArrayIndex ;
✌
✆
1.7.5 Array Mathematics
Arithmetic operators on arrays and certain combinations of arrays and scalars
are available through the standard arithmetic operators, addition (+), subtrac-
tion (-), division (/), multiplication as well as through the exponentiation (ˆ)
operator. For addition and subtraction, the size of vectors should be same.
✞
class AddSub
2 Real Add1 = {1, 2, 3} + 1; // Not allowed!
Real Add2 = {1, 2, 3} + {1, 2}; // Not allowed ,
different sizes!
44 Modelica Commands
6
www.sdmsacademy.in
4 Real Add3 [2, 2] = {{1, 1}, {2, 2}} + {{1, 2}, {3, 4}};
// Result: {{2, 3}, {5,
6}}
Real Sub1 [3] = {1, 2, 3} - {1, 2, 0};// Result: {0, 0,
3}
8 end AddSub;
✌
✆
In case of product, scalar and vector products are allowed. Scalar and vector
products are performed element-wise. In matrix product, product is allowed
if number of rows of first matrix is equal to the number of columns of second
matrix.
✞
1 class Multiplication
Real m1 [3] = {1, 2, 3} * 2;
3 // Elementwise multiplication : {2, 4, 6};
5 Real m2 [3] = 3 * {1, 2, 3};
// Elementwise multiplication : {3, 6, 9};
7
Real m3 = {1, 2, 3} * {1, 2, 2};
9 // Scalar product: 11;
11 Real m4 [2] = {{1, 2}, {3, 4}} * {1, 2};
// Matrix product : {5, 11};
13 end Multiplication ;
✌
✆
In division, vector or matrix to scalar division is allowed but scalar to vector or
matrix division is not allowed.
✞
1 class Div
Real [3] Div1 , Div2 , Div3 ;
3 equation
Div1 = {2, 4, 6} / 2; // Result: {1, 2, 3}
5 Div2 = 6 / {1, 2, 3}; // Not allowed!
Div3 = {1, 2, 3} / {1, 2, 2}; // Not allowed!
7 end Div;
✌
✆
Array exponentiation is the same as repeated matrix multiplication and is there-
fore only declared when the first operand is a square matrix and the second
operand is a non-negative integer.
✞
1 class Exponent
Real m[2, 2];
3 equation
m = {{1, 3}, {4, 5}} ^ 2;
5 // Result is {{13, 18}, {24, 37}}.
end Exponent ;
✌
✆
1.8. MATRIX 45
www.sdmsacademy.in
1.7.6 Array Constructor
An array constructor function constructs and returns an array computed from
its arguments. Most of the constructor functions in the table below construct
an array by filling in values according to a certain pattern.
✞
class ConstructFunc
2 Real z[2,3] = zeros(2, 3);
// Constructs the matrix {{0,0,0}, {0,0,0}}
4 Real o[3] = ones (3);
// Constructs the vector {1, 1, 1}
6 Real f[2,2] = fill (2.1 ,2 ,2);
// Constructs the matrix {{2.1 , 2.1}, {2.1, 2.1}}
8 Boolean check[3, 4] = fill (true , 3, 4);
// Fills a Boolean matrix
10 Real id [3,3] = identity (3);
// Creates the matrix {{1,0,0}, {0,1,0}, {0, 0, 1}}
12 Real di [3,3] = diagonal ({1, 2, 3});
// Creates the matrix {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}
14 Real ls [5] = linspace (0.0, 8.0, 5);
// Computes the vector {0.0, 2.0, 4.0, 6.0, 8.0}
16 end ConstructFunc ;
✌
✆
The following set of functions are rather common in matrix and vector algebra,
but are not associated with any Modelica operator symbol.
✞
class AlgebraFunc
2 Real transp1 [2,2] = transpose ([1, 2; 3, 4]);
// Gives [1, 2; 3, 4] of type Integer [2, 2]
4 Real transp2 [2,2,1] = transpose ({{{1} ,{2}} ,{{3} ,{4}}});
// Gives {{{1} ,{2}} ,{{3} ,{4}}} of type Integer [2, 2, 1]
6 Real out[2, 2] = outerProduct ({2,1}, {3 ,2});
// Gives {{6, 4}, {3, 2}}
8 Real symm [2, 2] = symmetric ({{1, 2}, {3, 1}}) ;
// Gives {{1, 2}, {2, 1}}
10 Real c[3] = cross({1, 0, 0}, {0, 1, 0});
// Gives {0, 0, 1}
12 Real s[3, 3] = skew ({1, 2, 3});
// Gives {{0, -3, 2}, {3, 0, -1}, {-2, 1, 0}};
14 end AlgebraFunc ;
✌
✆
1.8 Matrix
Matrix in Modelic is created as
✞
class MyMatrix
46 Modelica Commands
www.sdmsacademy.in
2 Real m[rows , cols ] = [..; ..];
end MyMatrix ;
✌
✆
In Modelica matrix, elements are separated by comma (,) while rows are sepa-
rated by semicolon (;). The elements are placed inside the square brackets [..;..].
A two by two square matrix is given below.
✞
1 class MyMatrix
Real m[2, 2] = [1, 2; 3, 4];
3 end MyMatrix ;
✌
✆
Mostly all the mathematical operations on matrices are similar to the mathe-
matical operations on arrays.
1.8.1 Identity Matrix
identity() returns the n × n Integer identity matrix, with ones on the diagonal
and zeros at the other places. An identity matrix of order 3 × 3 is shown below:
I =

More Related Content

What's hot

Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...ssuserd6b1fd
 
Java Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun UmraoJava Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun Umraossuserd6b1fd
 
Advanced cardiovascular exercites
Advanced cardiovascular exercitesAdvanced cardiovascular exercites
Advanced cardiovascular exercitesMaria Lopez
 
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
Notes of 8085 micro processor Programming  for BCA, MCA, MSC (CS), MSC (IT) &...Notes of 8085 micro processor Programming  for BCA, MCA, MSC (CS), MSC (IT) &...
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...ssuserd6b1fd
 
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...ssuserd6b1fd
 
Principles of Linear Motion of Objects - Physics - Explained by Arun Umrao
Principles of Linear Motion of Objects - Physics - Explained by Arun UmraoPrinciples of Linear Motion of Objects - Physics - Explained by Arun Umrao
Principles of Linear Motion of Objects - Physics - Explained by Arun Umraossuserd6b1fd
 
Tinyos programming
Tinyos programmingTinyos programming
Tinyos programmingssuserf04f61
 
Reading Materials for Operational Research
Reading Materials for Operational Research Reading Materials for Operational Research
Reading Materials for Operational Research Derbew Tesfa
 
Differential Equations for Engineers
Differential Equations for EngineersDifferential Equations for Engineers
Differential Equations for Engineersamelslideshare
 
Math for programmers
Math for programmersMath for programmers
Math for programmersmustafa sarac
 

What's hot (13)

Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
 
Java Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun UmraoJava Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun Umrao
 
Advanced cardiovascular exercites
Advanced cardiovascular exercitesAdvanced cardiovascular exercites
Advanced cardiovascular exercites
 
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
Notes of 8085 micro processor Programming  for BCA, MCA, MSC (CS), MSC (IT) &...Notes of 8085 micro processor Programming  for BCA, MCA, MSC (CS), MSC (IT) &...
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
 
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
 
Advanced macro guide
Advanced macro guideAdvanced macro guide
Advanced macro guide
 
Hats guia de_macro
Hats guia de_macroHats guia de_macro
Hats guia de_macro
 
Electrónica digital: Logicsim
Electrónica digital: LogicsimElectrónica digital: Logicsim
Electrónica digital: Logicsim
 
Principles of Linear Motion of Objects - Physics - Explained by Arun Umrao
Principles of Linear Motion of Objects - Physics - Explained by Arun UmraoPrinciples of Linear Motion of Objects - Physics - Explained by Arun Umrao
Principles of Linear Motion of Objects - Physics - Explained by Arun Umrao
 
Tinyos programming
Tinyos programmingTinyos programming
Tinyos programming
 
Reading Materials for Operational Research
Reading Materials for Operational Research Reading Materials for Operational Research
Reading Materials for Operational Research
 
Differential Equations for Engineers
Differential Equations for EngineersDifferential Equations for Engineers
Differential Equations for Engineers
 
Math for programmers
Math for programmersMath for programmers
Math for programmers
 

Similar to Modlica an introduction by Arun Umrao

Advanced-java.pptx
Advanced-java.pptxAdvanced-java.pptx
Advanced-java.pptxMiltonMolla1
 
advanced java.pdf
advanced java.pdfadvanced java.pdf
advanced java.pdfAli Bozkurt
 
W java81
W java81W java81
W java81rasikow
 
Scilab help book 1 of 2
Scilab help book 1 of 2Scilab help book 1 of 2
Scilab help book 1 of 2Arun Umrao
 
Fortran programming help book
Fortran programming help bookFortran programming help book
Fortran programming help bookArun Umrao
 
A_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdfA_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdfssuser7fcce2
 
A practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinoldA practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinoldthe_matrix
 
A Practical Introduction To Python Programming
A Practical Introduction To Python ProgrammingA Practical Introduction To Python Programming
A Practical Introduction To Python ProgrammingNat Rice
 
A practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinoldA practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinoldFaruqolayinkaSalako
 
A_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdfA_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdfTariqSaeed80
 
Learn python the right way
Learn python the right wayLearn python the right way
Learn python the right wayDianaLaCruz2
 
C sharp programming
C sharp programmingC sharp programming
C sharp programmingsinghadarsh
 
C++ For Quantitative Finance
C++ For Quantitative FinanceC++ For Quantitative Finance
C++ For Quantitative FinanceASAD ALI
 
The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessoriuui
 
Introduction to methods of applied mathematics or Advanced Mathematical Metho...
Introduction to methods of applied mathematics or Advanced Mathematical Metho...Introduction to methods of applied mathematics or Advanced Mathematical Metho...
Introduction to methods of applied mathematics or Advanced Mathematical Metho...Hamed Oloyede
 

Similar to Modlica an introduction by Arun Umrao (20)

Advanced-java.pptx
Advanced-java.pptxAdvanced-java.pptx
Advanced-java.pptx
 
advanced java.pdf
advanced java.pdfadvanced java.pdf
advanced java.pdf
 
W java81
W java81W java81
W java81
 
Thats How We C
Thats How We CThats How We C
Thats How We C
 
Scilab help book 1 of 2
Scilab help book 1 of 2Scilab help book 1 of 2
Scilab help book 1 of 2
 
Notes of Java
Notes of JavaNotes of Java
Notes of Java
 
Fortran programming help book
Fortran programming help bookFortran programming help book
Fortran programming help book
 
A_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdfA_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdf
 
A practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinoldA practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinold
 
A Practical Introduction To Python Programming
A Practical Introduction To Python ProgrammingA Practical Introduction To Python Programming
A Practical Introduction To Python Programming
 
A practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinoldA practical introduction_to_python_programming_heinold
A practical introduction_to_python_programming_heinold
 
A_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdfA_Practical_Introduction_to_Python_Programming_Heinold.pdf
A_Practical_Introduction_to_Python_Programming_Heinold.pdf
 
Learn python the right way
Learn python the right wayLearn python the right way
Learn python the right way
 
C sharp programming
C sharp programmingC sharp programming
C sharp programming
 
C sharp programming[1]
C sharp programming[1]C sharp programming[1]
C sharp programming[1]
 
The maxima book
The maxima bookThe maxima book
The maxima book
 
C++ For Quantitative Finance
C++ For Quantitative FinanceC++ For Quantitative Finance
C++ For Quantitative Finance
 
thinkcspy3.pdf
thinkcspy3.pdfthinkcspy3.pdf
thinkcspy3.pdf
 
The C Preprocessor
The C PreprocessorThe C Preprocessor
The C Preprocessor
 
Introduction to methods of applied mathematics or Advanced Mathematical Metho...
Introduction to methods of applied mathematics or Advanced Mathematical Metho...Introduction to methods of applied mathematics or Advanced Mathematical Metho...
Introduction to methods of applied mathematics or Advanced Mathematical Metho...
 

More from ssuserd6b1fd

Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...
Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...
Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...ssuserd6b1fd
 
Decreasing increasing functions by arun umrao
Decreasing increasing functions by arun umraoDecreasing increasing functions by arun umrao
Decreasing increasing functions by arun umraossuserd6b1fd
 
Distribution of normal data understanding it numerical way by arun umrao
Distribution of normal data   understanding it numerical way by arun umraoDistribution of normal data   understanding it numerical way by arun umrao
Distribution of normal data understanding it numerical way by arun umraossuserd6b1fd
 
Decreasing and increasing functions by arun umrao
Decreasing and increasing functions by arun umraoDecreasing and increasing functions by arun umrao
Decreasing and increasing functions by arun umraossuserd6b1fd
 
What is meaning of epsilon and delta in limits of a function by Arun Umrao
What is meaning of epsilon and delta in limits of a function by Arun UmraoWhat is meaning of epsilon and delta in limits of a function by Arun Umrao
What is meaning of epsilon and delta in limits of a function by Arun Umraossuserd6b1fd
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...ssuserd6b1fd
 
Work and Energy Notes by Arun Umrao
Work and Energy Notes by Arun UmraoWork and Energy Notes by Arun Umrao
Work and Energy Notes by Arun Umraossuserd6b1fd
 
Notes of Units, Dimensions & Errors for IIT JEE by Arun Umrao
Notes of Units, Dimensions & Errors for IIT JEE by Arun UmraoNotes of Units, Dimensions & Errors for IIT JEE by Arun Umrao
Notes of Units, Dimensions & Errors for IIT JEE by Arun Umraossuserd6b1fd
 
Physics dictionary for CBSE, ISCE, Class X Students by Arun Umrao
Physics dictionary for CBSE, ISCE, Class X Students by Arun UmraoPhysics dictionary for CBSE, ISCE, Class X Students by Arun Umrao
Physics dictionary for CBSE, ISCE, Class X Students by Arun Umraossuserd6b1fd
 
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
Maxima & Minima of Functions - Differential Calculus by Arun UmraoMaxima & Minima of Functions - Differential Calculus by Arun Umrao
Maxima & Minima of Functions - Differential Calculus by Arun Umraossuserd6b1fd
 
Limit & Continuity of Functions - Differential Calculus by Arun Umrao
Limit & Continuity of Functions - Differential Calculus by Arun UmraoLimit & Continuity of Functions - Differential Calculus by Arun Umrao
Limit & Continuity of Functions - Differential Calculus by Arun Umraossuserd6b1fd
 
Principle of Integration - Basic Introduction - by Arun Umrao
Principle of Integration - Basic Introduction - by Arun UmraoPrinciple of Integration - Basic Introduction - by Arun Umrao
Principle of Integration - Basic Introduction - by Arun Umraossuserd6b1fd
 
Principle of Integral Applications - Integral Calculus - by Arun Umrao
Principle of Integral Applications - Integral Calculus - by Arun UmraoPrinciple of Integral Applications - Integral Calculus - by Arun Umrao
Principle of Integral Applications - Integral Calculus - by Arun Umraossuserd6b1fd
 
Principle of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun UmraoPrinciple of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun Umraossuserd6b1fd
 

More from ssuserd6b1fd (20)

Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...
Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...
Notes for c programming for mca, bca, b. tech cse, ece and msc (cs) 1 of 5 by...
 
Decreasing increasing functions by arun umrao
Decreasing increasing functions by arun umraoDecreasing increasing functions by arun umrao
Decreasing increasing functions by arun umrao
 
Distribution of normal data understanding it numerical way by arun umrao
Distribution of normal data   understanding it numerical way by arun umraoDistribution of normal data   understanding it numerical way by arun umrao
Distribution of normal data understanding it numerical way by arun umrao
 
Decreasing and increasing functions by arun umrao
Decreasing and increasing functions by arun umraoDecreasing and increasing functions by arun umrao
Decreasing and increasing functions by arun umrao
 
What is meaning of epsilon and delta in limits of a function by Arun Umrao
What is meaning of epsilon and delta in limits of a function by Arun UmraoWhat is meaning of epsilon and delta in limits of a function by Arun Umrao
What is meaning of epsilon and delta in limits of a function by Arun Umrao
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
Work and Energy Notes by Arun Umrao
Work and Energy Notes by Arun UmraoWork and Energy Notes by Arun Umrao
Work and Energy Notes by Arun Umrao
 
Notes of Units, Dimensions & Errors for IIT JEE by Arun Umrao
Notes of Units, Dimensions & Errors for IIT JEE by Arun UmraoNotes of Units, Dimensions & Errors for IIT JEE by Arun Umrao
Notes of Units, Dimensions & Errors for IIT JEE by Arun Umrao
 
Physics dictionary for CBSE, ISCE, Class X Students by Arun Umrao
Physics dictionary for CBSE, ISCE, Class X Students by Arun UmraoPhysics dictionary for CBSE, ISCE, Class X Students by Arun Umrao
Physics dictionary for CBSE, ISCE, Class X Students by Arun Umrao
 
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
Maxima & Minima of Functions - Differential Calculus by Arun UmraoMaxima & Minima of Functions - Differential Calculus by Arun Umrao
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
 
Limit & Continuity of Functions - Differential Calculus by Arun Umrao
Limit & Continuity of Functions - Differential Calculus by Arun UmraoLimit & Continuity of Functions - Differential Calculus by Arun Umrao
Limit & Continuity of Functions - Differential Calculus by Arun Umrao
 
Principle of Integration - Basic Introduction - by Arun Umrao
Principle of Integration - Basic Introduction - by Arun UmraoPrinciple of Integration - Basic Introduction - by Arun Umrao
Principle of Integration - Basic Introduction - by Arun Umrao
 
Principle of Integral Applications - Integral Calculus - by Arun Umrao
Principle of Integral Applications - Integral Calculus - by Arun UmraoPrinciple of Integral Applications - Integral Calculus - by Arun Umrao
Principle of Integral Applications - Integral Calculus - by Arun Umrao
 
Principle of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun UmraoPrinciple of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun Umrao
 

Recently uploaded

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Recently uploaded (20)

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

Modlica an introduction by Arun Umrao

  • 1. 1 OPEN MODELICA INTRODUCTION & COMMANDS Arun Umrao www.sdmsacademy.in DRAFT COPY - GPL LICENSING
  • 2. 2 www.sdmsacademy.in Contents 1 Modelica Commands 5 1.1 Modelica Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.1.2 Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Escape Characters . . . . . . . . . . . . . . . . . . . . . . 8 Real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.3 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.4 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.1.5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.1.6 Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Declaration Equations . . . . . . . . . . . . . . . . . . . . 11 Modifier Equations . . . . . . . . . . . . . . . . . . . . . . 11 If-Else Equation . . . . . . . . . . . . . . . . . . . . . . . 11 For Equation . . . . . . . . . . . . . . . . . . . . . . . . . 12 Connect Equation . . . . . . . . . . . . . . . . . . . . . . 13 When Equation . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1.7 Extends . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.1.8 Specialized Class . . . . . . . . . . . . . . . . . . . . . . . 14 1.1.9 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.2.1 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.2.2 Function Return Value . . . . . . . . . . . . . . . . . . . . 17 1.2.3 Protected Variable . . . . . . . . . . . . . . . . . . . . . . 17 1.2.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.5 Partial Function . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.6 External Language . . . . . . . . . . . . . . . . . . . . . . 18 1.2.7 No Events . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.2.8 Smooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.2.9 Reinitiate . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.3 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.3.1 Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.3.2 Previous Sample Value . . . . . . . . . . . . . . . . . . . . 21 1.3.3 Change Event . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.3.4 Edge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.3.5 Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.4.1 Break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
  • 3. 3 www.sdmsacademy.in 1.4.2 Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.4.3 For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.4.4 If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.4.5 In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.4.6 Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.4.7 Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.4.8 Terminate . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.4.9 Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.4.10 When . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.4.11 While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.5 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.5.1 Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.5.2 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . 27 Element-wise Operation . . . . . . . . . . . . . . . . . . . 28 Relational Operators . . . . . . . . . . . . . . . . . . . . . 29 1.5.3 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . 29 Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Minimum . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Products . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Summation . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Remainder . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Ceiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 1.5.4 Algebraic . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Absolute . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Sign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Square Root . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.5.5 Trigonometric . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.5.6 Special Functions . . . . . . . . . . . . . . . . . . . . . . . 37 Derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 1.6 Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 1.6.1 Connector . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Flow Connectors . . . . . . . . . . . . . . . . . . . . . . . 38 Stream Connectors . . . . . . . . . . . . . . . . . . . . . . 38 1.6.2 Connect . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.6.3 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.6.4 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 1.6.5 N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 1.6.6 P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
  • 4. 4 Modelica Commands www.sdmsacademy.in 1.6.7 Expandable Connectors . . . . . . . . . . . . . . . . . . . 40 1.7 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 1.7.1 Range Vector . . . . . . . . . . . . . . . . . . . . . . . . . 41 1.7.2 Size Function . . . . . . . . . . . . . . . . . . . . . . . . . 42 1.7.3 Array Concatenate . . . . . . . . . . . . . . . . . . . . . . 42 1.7.4 Array Index . . . . . . . . . . . . . . . . . . . . . . . . . . 43 1.7.5 Array Mathematics . . . . . . . . . . . . . . . . . . . . . . 43 1.7.6 Array Constructor . . . . . . . . . . . . . . . . . . . . . . 45 1.8 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 1.8.1 Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . 46 1.8.2 Diagonal Matrix . . . . . . . . . . . . . . . . . . . . . . . 46 1.8.3 Transpose of Matrix . . . . . . . . . . . . . . . . . . . . . 47 1.8.4 Cross Products . . . . . . . . . . . . . . . . . . . . . . . . 47 1.9 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 1.9.1 Annotation & Units . . . . . . . . . . . . . . . . . . . . . 47 1.9.2 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
  • 5. 1.1. MODELICA CLASS 5 www.sdmsacademy.in 1Modelica Commands The examples given below are written and compiled in two ways. Sim- ple mathematical functions and examples are evaluated in OpenModelica with OMC interface. Classes and models are simulated in ‘generic Modelica block’ of xcos in Scilab. As the core language of Modelica is C, hence it is presumed that the reader’s have deep knowledge of properties and usability of C. The depth of reading is for use of Modelica language in xcos, MapleSim simulators only. For in-depth reading, use the Modelica language specifications document. OMNotebook can also be used to compile and run the modelica codes. In one execution cell, write the code and compile them. Call the model, function or class from other execution cell. A new ModelicaInput/Command input cell can also be created by the short-cut Alt+Enter. 1.1 Modelica Class A class in Modelica is its fundamental structure. Classes provide the structure for objects, also known as instances. Classes can contain equations, functions, parameters etc that is used for computation in Modelica. The basic data types used in Modelica are Real, Integer, String, Boolean and enumeration types. The members of a class in Modelica have two levels, i.e. public or protected. Public functions, members can be access from anywhere in the program while protected functions, members can only be accessed within the class. Only public elements of a class can be accessed via dot notation. Public elements are defined within public heading while protected elements are defined within protected heading. An element can not be declared twice or more. Modelica programs are built from classes, also called models. From a class definition, it is possible to create any number of objects that are known as instances of that class. A Modelica class has syntax ✞ 1 class <class name > <variables /parameters > 3 equation <equations > 5 end <class name >; ✌ ✆ The instances of a modelica class is created as in C++ class. For above syntaxed class, its instances is created like ✞ 1 <class name > <instance name >; ✌ ✆ A working example, explaining the equation ẋ = −kx is given below: ✞ 1 class ALAW
  • 6. 6 Modelica Commands 3 www.sdmsacademy.in Real x(start = 1); parameter Real k = 1; 5 equation der(x) = -k*x; 7 end ALAW ; ✌ ✆ Here variable ‘x’ is real type and has a start value 1. A class said to be a partial class if it can be called from other classes. For example, in electrical science, there are hundreds of elements which have one input and one output. hence a sub class can be declared for one input pin and one output pin. ✞ partial class TwoPins "Superclass of elements with two electrical pins " 2 Pin p, n; Voltage v; 4 Current i; equation 6 v = p.v - n.v; p.i + n.i = 0; 8 i = p.i; end TwoPins ; ✌ ✆ This partial class creates two pins ‘p’ and ‘n’ with voltage ‘v’ and current ‘i’. There are two values voltage and current computed by equations given inequation block. Now, we have the partial class TwoPins which can be uses to create a Resistor, by using relation V = i ∗ R. It describes the specific physical characteristics of the relation between voltage and current for resistors. ✞ 1 class Resistor "Ideal linear electrical Resistor " // Access to partial class 3 extends TwoPins; // Get relation between v & i 5 parameter Real R(unit = "Ohm") " Resistance "; equation 7 R*i = v; end Resistor ; ✌ ✆ To simulate this class, first copy these codes in any one execution cell of OM- Notebook and compile it. Use simulate() command to simulate the model in second execution cell of OMNotebook as shown below: ✞ simulate ( Resistor ); // Argument is class name or model name ✌ ✆ Get the output value by calling val() function for unknown variables as shown below: ✞ 1 val(v); // for solution value of potential ✌ ✆
  • 7. 1.1. MODELICA CLASS 7 www.sdmsacademy.in 1.1.1 Identifiers Identifiers are sequences of letters, digits, and other characters such as under- score, which are used for naming various items in the language. Keywords reserved for exclusive used of Modelica grammar can not be used as identifiers. The first letter of an identifier must be a letter or underscore , followed by any number of letters, digits, or underscores. Identifiers in Modelica are case sensitive, i.e. ‘x’ and ‘X’ are two different identifiers. 1.1.2 Variable A variable declared with prefix constant never changes. It is substituted by its value wherever it occurs. A Real, Boolean, String and enumeration are other type of variables. A real type variable is declared as ✞ 1 class <class name > Real x(start = 1) "Horizontal value"; 3 Real y(start = 1) "Vertical Value"; equation 5 <equations > end <class name >; ✌ ✆ In modelica, variable can be a derived type. Derived type variable is that one which is derived from the base varible. ✞ type Temp = Real (unit ="K"); // Temp variable 2 type AbsTemp = Temp (min =0); // Must be positive ✌ ✆ Constant Value of a constant type variable is remain unchanged during the execution of class. It is declared as shown in the following syntax. ✞ class <class name > 2 constant PI 3.14 "PI value"; equation 4 <equations > end <class name >; ✌ ✆ String String is a group or array of characters of variable size. A string type variable is declared as:
  • 8. 8 Modelica Commands 5 www.sdmsacademy.in ✞ 1 class <class name > String Label "PI value"; 3 equation <equations > end <class name >; ✌ ✆ Escape Characters String literals appear between double quotes as in “between”. Any character in the Modelica language character set apart from double quote and backslash, including new-line, can be directly included in a string without using an escape code. Certain characters in string literals can be represented using escape codes, i.e., the character is preceded by a backslash within the string. Those characters are: ’ Single quote " Double quote ? Question-mark Backslash itself a Alert b Backspace f Form feed n New-line r Return t Horizontal tab v Vertical tab Single quote and symbol of question mark may also appear without backslash in string constants used in Modelica. Real A variable of type Real are obtained by solving the equations containing the variable of type Real. A real type variable can store real values. There should be same numbers of equations as there are unknown variables in the equation. ✞ 1 model ADD // First variable x 3 Real x; // Second variable y 5 Real y; equation 7 /* Required two equations to get * *the values of variables x & y */ 9 x+y=4;
  • 9. 1.1. MODELICA CLASS 9 www.sdmsacademy.in x-y=1; 11 end ADD; ✌ ✆ To simulate this model, first copy these codes in any one execution cell of OM- Notebook and compile it. Use simulate() command to simulate the model in second execution cell of OMNotebook as shown below: ✞ 1 simulate (ADD); ✌ ✆ Get the output value by calling val() function for unknown variables as shown below: ✞ 1 val(x); // for solution value of x val(y); // for solution value of y ✌ ✆ The output from above model is ✞ 2.5 1.5 ✌ ✆ Integer A variable of type Integer can store only integer values. If a real values is tried to store in integer type variable, then its fraction part is truncated. It is declared as ✞ model <Model Name > 2 parameter Integer y; equation 4 ...... end <Model Name >; ✌ ✆ 1.1.3 Assignment In Modelica, we can assign a default value 1.0 (say) to a variable ‘x’ (say) by using ‘:=’ operator as shown below: ✞ 1 class <class name > input Real x := 1.0 // Default value 1.0 to x 3 equation <equations > 5 end <class name >; ✌ ✆
  • 10. 10 Modelica Commands www.sdmsacademy.in 1.1.4 Parameters The keyword parameter specifies to a variable as constant during its simulation run. Its value can be initialized before a simulation run, or between simulation runs. It makes variable of a special kind. ✞ 1 class <class name > Real x(start = 1) "Horizontal value"; 3 Real y(start = 1) "Vertical Value"; parameter Real lambda = 0.3 "Slope"; 5 equation <equations > 7 end <class name >; ✌ ✆ 1.1.5 Comments In Modelica, comments can be added inside the block of class or model by starting ‘//’ symbol for a single line. For multiline comments, comments started from ‘/*’ symbol and ended with next ‘*/’ symbol. Multiline nested comments are not supported by Modelica. ✞ 1 class <class name > /* Here two varialbes are declared . They * 3 *are real type . One parameter variable * *of real type is also declared . */ 5 Real x(start = 1) "Horizontal value"; // Start with 1 Real y(start = 1) "Vertical Value"; // Start with 1 7 parameter Real lambda = 0.3 "Slope"; equation 9 <equations > end <class name >; ✌ ✆ There is another kind of documentation comment that is part of the Modelica and therefore not ignored by the Modelica translator. Such comments may occur at the ends of declarations, equations, or statements or at the beginning of class definitions. For example: ✞ model MyModel 2 Real p "Conditional variable "; Real y "Output Variable "; 4 algorithm y := 0; 6 p:=1; if(p<1) then 8 y := y + 0; else 10 y := y + 10;
  • 11. 1.1. MODELICA CLASS 11 12 www.sdmsacademy.in end if; end MyModel; ✌ ✆ 1.1.6 Equations Modelica is an equation-based language. Equations are more flexible and more powerful than assignment statements. For example, product of resistance and current is voltage. Declaration Equations Declaration equations are usually given as part of declarations of constants or parameters. Such equations are actually required for constants, but are optional for parameters. An equation always holds, which means that the mass never changes value during simulation. It is also possible to specify a declaration equation for a normal state variable, but this will make the variable behave as a kind of constant. ✞ class DecEq 2 constant Integer one = 1; // Declaration equation parameter Real mass = 22.5; // -"- 4 Real speed = 72.4; /* Declaration equation for* * a normal state variable */ 6 end DecEq; ✌ ✆ Modifier Equations Modifier equations occur in a variable declaration when there is a need to modify the default value of an attribute of the variable. A modifier equation replaces the declaration equation for an attribute of the specific type. A common usage is modifier equations for the start attribute of variables, the equations also occur in type definitions. ✞ class ModEq 2 Real speed(start = 72.4); // Modifier equation end ModEq; ✌ ✆ If-Else Equation An if -else equation can be declared in the equation block. This types of equa- tions are used for conditionally execution of the equation. If if condition is true then equation in if block is executed otherwise equation in else block is
  • 12. 12 Modelica Commands www.sdmsacademy.in executed. Each if condition shall be closed with end if. The syntax of this function is ✞ 1 class <class > .... 3 equation if <condition > then 5 <sub equation > elseif <condition > then 7 <sub equation > else 9 <sub equation > end if; 11 end <class >; ✌ ✆ Example is given below: ✞ 1 class IfEquation parameter Real u = 4; 3 parameter Real a = 10; parameter Real b = 2; 5 Real y; equation 7 if u > a then y = a; 9 elseif u < b then y = b; 11 else y = u; 13 end if; end IfEquation ; ✌ ✆ Classes are simulated similar to the simulation of modelica models. For Equation A for equation is a repetitive equation structures. Its syntax is as follows: ✞ for <indices > loop 2 <for equation > end for ✌ ✆ Remember that each opened for equation must be closed with end keyword as shown in above syntax. An example for for equation is given below: ✞ 1 constant Integer j=4; Real x[j]; 3 equation for j in 1:j loop
  • 13. 1.1. MODELICA CLASS 13 www.sdmsacademy.in 5 x[j]=j; end for; ✌ ✆ Connect Equation Connections between objects are introduced by connect equations in the equa- tion part of a class. A connect equation has the following syntax: ✞ connect(<ref a>, <ref b>); ✌ ✆ See the example below: ✞ 1 class myClass Component com[n]; 3 equation for i in 1:n-1 loop 5 connect (com[i].o,com[i+1]. i); end for; 7 end myClass; ✌ ✆ When Equation when is used to compare two operands. If the condition is met true, codes inside the when equation are executed otherwise they are skipped at condition of false. Its syntax is used as ✞ 1 when <condition > then <codes > 3 end when ✌ ✆ See the example, below: ✞ 1 model BouncingBall constant Real g = 9.81; 3 parameter Real c = 0.9; Real height(start = 1,fixed=true ); 5 Real velocity (start = 10, fixed=true ); equation 7 der(height) = velocity ; der(velocity ) = -g; 9 when height <= 0 then // when condition reinit(velocity , -c*velocity ); 11 end when ; end BouncingBall ; ✌ ✆ Each when equation must be closed by using keyword end. In when equation, the statement is always in the form of
  • 14. 14 Modelica Commands www.sdmsacademy.in ✞ <variable > = <expression > ✌ ✆ 1.1.7 Extends extends keyword extends the scope of current class to the other function. By this way, code becomes harder to read since the formal parameter list is not available in the functions. The partial base function is ✞ 1 partial function pFunction input Real x;// Input arg 3 output Real y;// Output arg end pFunction ; ✌ ✆ This partial base function is accessed by function ‘mFunc’ using extends keyword as shown below: ✞ function mFunc 2 extends pFunction ; algorithm 4 y := sin(x)/cos(x); end mFunc; ✌ ✆ extends keyword is used to access or inherit the restricted class. 1.1.8 Specialized Class In Modelica, there are few specialized classes. For example, record, type, model, block, package, function, connector are specialized classes and they have special uses. record This class as well as its components are allowed to be used as public only. The elements of a record may not have prefixes input, output, inner, outer, stream, or flow. type It is predefined types, enumerations, array of type, or classes extending from type. model It is identical to class. block It is same as model. It has restriction that each connector component of the block must have input and/or output as prefixes for all connector variables. connector Connectors are used to connect two or more Modelica blocks. It and its components are allow to use in public mode. package It contains declarations of classes and constants only.
  • 15. 1.2. FUNCTIONS 15 www.sdmsacademy.in operator record It is similar to record. In operator record, operator over- loading is possible. operator It is similar to package, but it have only declarations of functions. operator function It is short name of an operator with exactly one function. It has same restriction as the function has. 1.1.9 Inheritance Inheritance is a property that describes a relationship between two (or more) types or classes or objects. It is possible to derive one class from another class or even several classes. A base class is a class that is created with the intention of deriving other classes from it. A child class is a class that is derived from another class, i.e. from parent class. ✞ 1 record Constants // Record data is base class parameter Real a = 0.2; 3 parameter Real b = 0.6; Real c; 5 end Constants ; class myClass // child class 7 extends Constants ; // Access inheritance equation 9 a + b + c = 1; end myClass; ✌ ✆ In above example, we declare a restrictive type class of ‘Constants’ with three parameters. A child class ‘myClass’ is defined with inherited property of the ‘Constants’ class. To access the inheritance, extends keyword is used. In inher- itance, the parameters with same name with or without same values can not be re-declared. 1.2 Functions Function is a group of coding scripts. It takes inputs and gives outputs. The internal process of a function are performed by these coding scripts. The internal process of a function is never checked by the compiler. Compiler only checks whether rules of programming of the language are followed or not. This is why, function behaves like a black box. 1.2.1 Function In Modelica, there are several pre-declared mathematical functions like abs, sqrt, mod, etc. Some other functions are available under Modelica libraries. For example, sin, cos, exp, etc. are available in the math library Modelica.Math.
  • 16. 16 Modelica Commands www.sdmsacademy.in The arithmetic operators +, -, *, / can be regarded as functions that are used through a convenient operator syntax. In Modelica, a function is declared as ✞ function MyFunc 2 input Real a[:]; // Array with variable size input Real x := 1.0; // Default value 1.0 for x 4 output Real y; protected // protect local variables 6 Real p; algorithm 8 y := 0; p := 1; 10 for i in 1: size (a,1) loop y := y + a[i]*p; 12 p := p*x; end for; 14 end MyFunc; ✌ ✆ The above function can be called as ✞ z = MyFunc ({1, 2, 3, 4}, 21); ✌ ✆ or as ✞ 1 z = MyFunc(a={1, 2, 3, 4}, x=21) ; ✌ ✆ Here output ‘y’ is assigned to variable ‘z’. A function may be several outputs depends on how many output variables are declared in the function by using output keyword. ✞ 1 function MyFunc input Real a; 3 input Real b; output Real s; 5 output Real d; output Real m; 7 algorithm s := a+b; // sum output 9 d := a/b; // division output m := a*b; // product output 11 end MyFunc; ✌ ✆ Calling the function like ✞ 1 MyFunc (2.0, 1.0) ✌ ✆ Output is given as ✞ (3.0, 2.0, 2.0) ✌ ✆
  • 17. 1.2. FUNCTIONS 17 www.sdmsacademy.in We can assign the return values to other variables. To assign return values to local variables, function is called as ✞ 1 (i, j, k)=MyFunc (2.0, 1.0) ✌ ✆ This function call will assign i = 3.0, j = 2.0 and k = 2.0 respectively. A function can also be called from inside the class. The example is given below ✞ 1 class MyFuncCall Real s,d,p; 3 equation (s,d,p) = MyFunc (2.0, 1.0) ; 5 end MyFuncCall ; ✌ ✆ We can simulate the class or model like ✞ 1 simulate ( MyFuncCall , stopTime =10 ) ✌ ✆ Result is stored in the variables ‘s’, ‘d’ and ‘p’. These results can be plotted by using function plot(). 1.2.2 Function Return Value The return statement terminates the current function call. It can only be used inside an algorithm section of a function. ✞ 1 function MyFunc ..... 3 algorithm if <case > then 5 return; end if; 7 ..... end MyFunc; ✌ ✆ 1.2.3 Protected Variable Inside functions or classes, some variables are declared for internal calculations. These variables are instances of the function and may be accessed from the out- side of the function. To protect their scope and accessibility, protected keyword is used. ✞ function MyFunc 2 <inputs >/< outputs > protected // protect local variables 4 Real p; algorithm
  • 18. 18 Modelica Commands www.sdmsacademy.in 6 <algorithmic statements > end MyFunc; ✌ ✆ 1.2.4 Algorithms Algorithmic statements in Modelica occur only within algorithm sections, start- ing with the keyword algorithm. Algorithm sections may also be called algo- rithm equations, since an algorithm section can be viewed as a group of equa- tions involving one or more variables, and can appear among equation sections. Algorithm sections are terminated by the appearance of one of the keywords equation, public, protected, algorithm or end. ✞ 1 ..... algorithm 3 y := 0; p := 1; 5 for i in 1: size (a,1) loop y := y + a[i]*p; 7 p := p*x; end for; 9 ..... ✌ ✆ Inside the algorithm section, equations are solved by using statements. While in equations the values assigned to the variables are solved mathematically. 1.2.5 Partial Function A partial function returns a partially evaluated function that is also a function, with the remaining not bound formal parameters still present in the same order as in the original function declaration. A partial function is specified by the function keyword followed by a function call. ✞ 1 partial function pFunction input Real x; 3 output Real y; end pFunction ; ✌ ✆ In a function, input argument is intialized by input keyword while ouput argu- ment is initialized by output keyword. 1.2.6 External Language In Modelica, the default language used for computation is C. We can use other languages like Fortran by using keyword external. To use built in function in Modelica, external is assigned “builtin” value. The builtin function or native function of Modelica are accessed by using dot (.) as shown below:
  • 19. 1.2. FUNCTIONS 19 2 www.sdmsacademy.in ✞ package Modelica package Math 4 function sin input Real x; 6 output Real y; external "builtin "; 8 end sin; end Math ; 10 end Modelica ; model UserModel 12 output p= Modelica .Math .sin (2); end UserModel ; ✌ ✆ Other values for external keyword are ”C” and “FORTRAN 77”. 1.2.7 No Events We know that the square root of a negative number is an imaginary output. This is why, the expressions containing square root function should not be evaluated when an argument is negative. To avoid such problems, noEvent() function is used. The syntax of this function is ✞ noEvent(if x<0 then 0 else sqrt (x)*x)); ✌ ✆ It prevents the zero crossing of the function. 1.2.8 Smooth Another way to prevent zero crossing is smooth() function. This function is used as ✞ 1 smooth(0, if t<0 then 0 else t); ✌ ✆ However, smooth does not guarantee that no events will be generated, and thus it can be necessary to use noEvent inside smooth. 1.2.9 Reinitiate The reinit keyword is used inside the body of a when-equation to re-initiate the values of a variable. It has the following syntax: ✞ 1 reinit(<variable >, <expression >); ✌ ✆ reinit reinitialize the value of variable ‘variable’ to ‘expression’ at an instant of event. Assume a variable ‘x’ whose value is decreases by one from 10 to continue.
  • 20. 20 Modelica Commands www.sdmsacademy.in To prevent its zero crossing, reinit function is used to reset the variable value to 10. reinit function may be expressed in form piecewise function as given below: f = x = 10 if x ≤ 0 x otherwise In Above piecewise equation, whenever ‘x’ becomes zero or negative, it is reset to 10. The assigned value to ‘f’ is 10 on reset and instantaneous value to variable ‘x’ otherwise. Following is bouncing ball model that uses reinit to change the velocity direction of ball when it hits the ground. ✞ 1 model BouncingBall The bouncing ball model constant Real g = 9.81; 3 parameter Real c = 0.9; // Elasticity constant of ball Real height(start = 1,fixed=true ); 5 Real velocity (start = 10, fixed=true ); equation 7 der(height) = velocity ; der(velocity ) = -g; 9 algorithm when height = 0 then 11 reinit(velocity , -c*velocity ); end when ; 13 end BouncingBall ; ✌ ✆ 1.3 Events An event is simply something that happens. It is true in the mathematical sense. Modelica provides a number of built-in functions and operators related to events and time. One function that can be used for generating repeated events is sample. 1.3.1 Samples The function sample() returns true and triggers events at instances of time. Its syntax is ✞ 1 sample(initial time (t_0), time delay (d) ) ✌ ✆ It creates events at time t0, t0 + d, t0 + 2d, . . .. See the example below: ✞ 1 model mySample parameter Real sample_interval = 0.05; 3 Real x(start=5); Real y; 5 equation der(x) = -x;
  • 21. 1.3. EVENTS 21 9 www.sdmsacademy.in 7 when sample (0, sample_interval ) then y = x; end when ; 11 end mySample ; ✌ ✆ To simulate this model, use following sequence of commands in OMNotebook execution cells. ✞ simulate (mySample ) 2 plot (y) ✌ ✆ 1.3.2 Previous Sample Value At an event, pre() gives the previous value of variable passed as its argument, immediately before the event, except for event iteration of multiple events at the same point in time when the value is from the previous iteration. It can not be used within function. Its syntax is ✞ pre(variable ); ✌ ✆ 1.3.3 Change Event change() returns true when a value get changed at any instant of time. It is used like ✞ 1 change(value ); ✌ ✆ 1.3.4 Edge Similarly, edge() returns true when argument switches from false to true at any instant of time. It is used like ✞ 1 edge (value ); ✌ ✆ 1.3.5 Delay Function delay() is used to create a time delay for a variable. Its syntax is ✞ 1 delay(variable , time delay ); ✌ ✆
  • 22. 22 Modelica Commands www.sdmsacademy.in 1.4 Statements A statement is any declarative sentence which is either true or false or have a definite result. 1.4.1 Break break keyword is similar to the keyword terminate. It is always used in side a loop. It terminates the current loop and exit the computation out of the loop. ✞ 1 model ADD Real y; 3 Real p; algorithm 5 y := 0; p := 1; 7 for p in 1:10 loop if p5 then 9 break; end if 11 y := y + p; end for; 13 end ADD; ✌ ✆ 1.4.2 Else else is a part of if -else. First if checks the conditions and if condition is true then expressions inside the if keyword is executed otherwise expressions inside else is executed. Its syntax is ✞ 1 if condition then expression 3 elseif condition then expression 5 else expression 7 end if; ✌ ✆ Begining of if keyword should be ended with the end if keyword. See example below: ✞ 1 model MyModel Real p; 3 Real y; algorithm 5 y := 0;
  • 23. 1.4. STATEMENTS 23 www.sdmsacademy.in p:=1; 7 if(p1) then 9 y := y + 0; else 11 y := y + 10; end if; 13 end MyModel; ✌ ✆ 1.4.3 For This is a conditional function. Used to create loop with few conditions. It is used inside the algorithm section of the Modelica model. ✞ model ADD 2 Real y; Real p; 4 algorithm y := 0; 6 p := 1; for p in 1:10 loop 8 y := y + p; end for; 10 end ADD; ✌ ✆ Each for should be closed with end keyword as shown in above example. 1.4.4 If if -else is group of keyword for conditional execution of expression either inside if or inside else. First if checks the conditions and if condition is true then expressions inside the if keyword is executed otherwise expressions inside else is executed. Its syntax is ✞ if condition then 2 expression elseif condition then 4 expression else 6 expression end if; ✌ ✆ Any block opened with if keyword should be closed with the end if keyword. ✞ 1 model MyModel Real p; 3 Real y; algorithm
  • 24. 24 Modelica Commands 7 www.sdmsacademy.in 5 y := 0; p:=1; if(p0) then 9 y := y + 0; else 11 y := y + 10; end if; 13 end MyModel ; ✌ ✆ 1.4.5 In in is used to create a set of data. For example, by using ‘:’ as shown below ✞ in 1:10 loop // 1, 2, ..., 9, 10 ✌ ✆ a set or an array is created having values from 1 to 10 (equal to or less than). The difference between two consecutive values is 1. If in is modified like ✞ 1 in 1:2:10 loop // 1, 3, 5, 7, 9 ✌ ✆ then, set or array is created have elements from 1 to 10 (equal to or less than) having common difference of 2. 1.4.6 Loop for, while functions are used as iterative functions. loop is post-fixed with these function to perform iterations. The expression of a for-equation or while- equations is a vector expression. It is evaluated once for each for-equation or while-equations, and are evaluated in the scope immediately enclosing the for-equation or while-equation. The expression of a for-equation shall be a parameter expression. The loop-variable ( IDENT ) is in scope inside the loop- construct and shall not be assigned to. The loop-variable has the same type as the type of the elements of the vector expression. ✞ 1 for i in 1:10 loop //i has 1, 2, 3, ..., 10 for r in 1.0:1.5:5.5 loop //r has 1.0, 2.5, 4.0, 5.5 3 for i in {1,3,6,7} loop //i has 1, 3, 6, 7 ✌ ✆ See the example below: ✞ 1 model ADD Real y; 3 Real p; algorithm 5 y := 0; p := 1; 7 for p in 1:10 loop
  • 25. 1.4. STATEMENTS 25 www.sdmsacademy.in y := y + p; 9 end for; 11 end ADD; ✌ ✆ 1.4.7 Stream A variable is declared stream type in a connector that describe a bi-directional flow of matter. A connector is said to be stream type connector if it has atleast one stream connector. A connector is declared as stream type by using this keyword as prefix. ✞ stream hflow; ✌ ✆ The stream properties for a variable can be inquired by using the built-in oper- ator ‘inStream()’. The value of the stream variable corresponding to the actual flow direction can be inquired through the built-in operator ‘actualStream()’. 1.4.8 Terminate terminate() statement is used to terminate the analysis which was carried out. It is used inside the conditional statement. See example below: ✞ 1 model MyBlock Real x(start =0); 3 Real y(start =1); equation 5 x+y=5; x-y=6; 7 algorithm when y0 then 9 terminate (Analysis ended..); end when ; 11 end MyBlock; ✌ ✆ 1.4.9 Then then keyword is used with conditional keywords like if, when, for and while etc. If condition is true then expressions in first block are executed otherwise the expression is either skipped or expressions in next block are executed. ✞ 1 // Check the condition for when when x 2 then 3 /*if condition is true then execute* *the expressions of this block. */ 5 y2 = sin(x); end when ; ✌ ✆
  • 26. 26 Modelica Commands www.sdmsacademy.in Each when should be closed by end when keyword as shown above. 1.4.10 When when-then is a conditional statement. when keyword is used to check the con- dition and if condition is satisfied then the expression is executed otherwise the expression is skipped. ✞ // Check the condition for when 2 when x 2 then /*if condition is true then execute * 4 *the expressions of this block. */ y2 = sin(x); 6 end when ; ✌ ✆ Each when should be closed by end when keyword as shown above. 1.4.11 While while keyword is used to compare between two operands and perform a loop until the conditions are met true. Each while is followed by do keyword as shown below: ✞ while coditions do 2 codes end while ✌ ✆ Each while must be terminated by end keyword. See the example below: ✞ 1 function myFunc Real y = 1; 3 input Real a; output Real s; 5 algorithm while ya loop 7 s := a+y; y := y+1; 9 end while; end myFunc; ✌ ✆ On calling the function like ✞ myFunc (15) ✌ ✆ we get output ✞ 29.0 ✌ ✆
  • 27. 1.5. MATHEMATICS 27 www.sdmsacademy.in 1.5 Mathematics 1.5.1 Expression Expression is a mathematical relation have one or more mathematical operators. For example, f(x) = x2 −2x+3 is a mathematical expression having three terms. 1.5.2 Operators Operators are those symbols which are used to performed operations between two operands. For example, ‘+’ symbol is used for addition between two operands. Operators also perform comparison, binary manipulation too. Arithmetic Operators Arithmetic operators performs simple addition, subtraction, division, multipli- cation and exponents etc. A list of arithmetic operators is shown in the following table. Operator Description ˆ Exponent operator, raised the power ‘p’ over the base ‘b’. / Division operator. It divides the left value with right value. Returns the quotient. If, both left and right values are integer then decimal part of the re- sult is truncated. ∗ Multiplication operator. It multiply the left value with right value. Returns product of two numbers. + Addition operator. It adds the right value with left value. − Subtraction operator. It subtracts the right value from left value. Table 1.1: Arithmetic operators. The examples of each arithmetic operator is given below: ✞ 1 2^3 ✌ ✆
  • 28. 28 Modelica Commands www.sdmsacademy.in ✞ 8.0 ✌ ✆ ✞ 1 2/3 ✌ ✆ ✞ 0.666666666 ✌ ✆ ✞ 1 2*3 ✌ ✆ ✞ 6 ✌ ✆ ✞ 1 2+3 ✌ ✆ ✞ 5 ✌ ✆ ✞ 1 2-3 ✌ ✆ ✞ -1 ✌ ✆ Element-wise Operation When either of the two operands of an arithmetic operators are scalar of vector array then, arithmetic operators are performed element-wise. For example, A = [1, 2, 4]; B = [5, 6, 3] then A+ B or A− B shall be performed elementwise, i.e. first element of vector A shall be summed or subtracted by first element of the vector B. So, A + B = [6, 8, 7]; A − B = [−4, −4, 1] See the following example: ✞ 1 model Elem Real A[3] = {1, 2, 4}; 3 Real B[3] = {5, 6, 3}; Real Sum [3]; 5 Real Dif [3]; equation 7 Sum = A + B; // Result: {6, 8, 7} Dif = A - B; // Result: {-4, -4, 1} 9 end Elem ; ✌ ✆
  • 29. 1.5. MATHEMATICS 29 www.sdmsacademy.in Simulate and call each elements of ‘Sum’ and ‘Dif’ vectors as shown below: ✞ 1 simulate ( Elem ); val(Sum [1]) ; ✌ ✆ Output is ✞ 6 ✌ ✆ Relational Operators The relation and assignment operators are shown in the table given below. These operators are only defined for scalar operands of simple types. The result is Boolean and is true or false if the relation is fulfilled or not, respectively. For operands of type Boolean, falsetrue. Operator Description Less than Greater than ≤ Less than or equal to ≥ Greater than or equal to == Equals Inequality 1.5.3 Arithmetic Functions We can write own model for arithmetic functions. Each model takes inputs and returns result as outputs. There are several ready-made arithmetic function in Modelica. We can rewrite them as per our requirement too. Few models of arithmetic operators are explained in following sections. Addition Addition of two integers in Modelica is takes place by arithmetic symbol ‘+’. Here a Modelica model ADD is created with one equation which adds two parametric integers and result is stored in variable sum. The simulation model for ADD block is given below: ✞ 1 model ADD parameter Integer x = 1; 3 parameter Integer y = 2; Real sum;
  • 30. 30 Modelica Commands 7 www.sdmsacademy.in 5 equation sum=x+y; end ADD; ✌ ✆ An addition function for evaluation of sum of two input numbers is given below: ✞ 1 function MySum input Real a; 3 input Real b; output Real s; 5 algorithm s := a+b; // Addition output 7 end MySum; ✌ ✆ On calling above function as ✞ 1 MySum(2,3) ✌ ✆ The output is ✞ 5 ✌ ✆ Minimum The function min() is used to return the minimum value of the supplied ele- ments. It is used like ✞ 1 min(A) // Returns the smallest element of array A. min(x,y) // Returns the smallest element between x and y. ✌ ✆ The argument of min function may be an array or only two number arguments. It returns the minimum value among all the elements or between two arguments. If number arguments are more than two, then Modelica returns “wrong number of elements” error. A function for getting minimum value of two input numbers is given below: ✞ function MyMin 2 input Real a; input Real b; 4 output Real s; algorithm 6 s := min(a,b); // Minimum output end MyMin; ✌ ✆ On calling above function as ✞ 1 MyMin(2,3) ✌ ✆
  • 31. 1.5. MATHEMATICS 31 www.sdmsacademy.in The output is ✞ 2 ✌ ✆ As simulation model block, the min() function may be used as ✞ 1 model MINIMUM Real x; 3 Real y; Real m; 5 equation m=min(x,y); 7 end MINIMUM; ✌ ✆ Maximum The function max() is used to return the maximum value of the supplied ele- ments. It is used like ✞ 1 max(A) // Returns the greatest element of array A. max(x,y) // Returns the greatest element between x and y. ✌ ✆ The argument of max function may be an array or only two number arguments. It returns the maximum value among all the elements or between two arguments. If number arguments are more than two, then Modelica returns “wrong number of elements” error. ✞ max(2, -1) ✌ ✆ ✞ 2 ✌ ✆ As simulation block, the max() function may be used as ✞ 1 model MAXIMUM Real x; 3 Real y; Real m; 5 equation m=max(x,y); 7 end MAXIMUM; ✌ ✆ Products The function product() is used to return the product value of the supplied ele- ments. It is used like
  • 32. 32 Modelica Commands www.sdmsacademy.in ✞ 1 product(A) // Returns the products of all elements of array A. product(x, y) // Returns the product of elements x and y. ✌ ✆ An product function for evaluation of product of two input numbers is given below: ✞ function MyProd 2 input Real a; input Real b; 4 output Real s; algorithm 6 s := a*b; // Product output end MyProd; ✌ ✆ On calling above function as ✞ 1 MyProd (2,3) ✌ ✆ The output is ✞ 6.0 ✌ ✆ Subtraction ‘–’ symbol is used to subtract the real numbers or integers. See example below in which MySub function is written. ✞ 1 function MySub input Real a; 3 input Real b; output Real s; 5 algorithm s := a-b; // subtraction output 7 end MySub; ✌ ✆ On calling above function as ✞ 1 MySub(2,3) ✌ ✆ The output is ✞ -1 ✌ ✆ A simulation model for subtraction block is given below:
  • 33. 1.5. MATHEMATICS 33 5 www.sdmsacademy.in ✞ 1 model SUB parameter Integer x = 1; 3 parameter Integer y = 2; Real sum; equation 7 sum=x-y; end SUB; ✌ ✆ Summation The function sum() is used to return the sum value of the supplied elements. It is used like ✞ 1 sum(A) // Returns the sum of elements of array A. sum(x,y) // Returns the sum of elements x and y. ✌ ✆ See the example below, in which array’s elements are summed together and result is returned. ✞ function MySum 2 input Real m[7] = {17.1 , 62.7, 7.5, 37.0, 25, 4.121 , 12.333336}; output Real s; 4 algorithm s := sum(m); // summation of array 6 end MySum; ✌ ✆ On calling MySum as ✞ MySum() ✌ ✆ The output result is ✞ 165.754336 ✌ ✆ Division div() function returns the division of two numbers. This function is used as ✞ 1 div(n, d) ✌ ✆ Here, ‘n’ is numerator and ‘d’ is denominator. Denominator can not be equals to zero.
  • 34. 34 Modelica Commands www.sdmsacademy.in Modulo mod() function returns the modulo of a number. If argument of mod() function is negative number then it converted it into positive numbers. It does nothing with positive numbers. ✞ 1 mod(a real number ) ✌ ✆ Remainder rem() returns the remainder value of dividend when it is divided by a divisor. For example, when 5 is divided by 2, (5/2), it gives a remainder of 1. Its syntax is ✞ 1 rem(n, d) ✌ ✆ Ceiling ceil() is rounded up the given argument to next higher integer value. For ex- ample, 1.25 is rounded up to 2. Similarly, −1.25 is rounded upto −1. ✞ 1 ceil ( -1.25) ✌ ✆ ✞ -1.0 ✌ ✆ ✞ 1 ceil (1.25) ✌ ✆ ✞ 2.0 ✌ ✆ This function is also called greatest integer function. For argument x, it can be represented also as [x]. Floor floor() is rounded down the given argument to next lower integer value. For example, 1.25 is rounded down to 1. Similarly, −1.25 is rounded upto −2. ✞ 1 floor( -1.25) ✌ ✆ ✞ -2.0 ✌ ✆
  • 35. 1.5. MATHEMATICS 35 www.sdmsacademy.in ✞ 1 floor (1.25) ✌ ✆ ✞ 1.0 ✌ ✆ Integer integer() returns the floor of the given argument and truncates its fraction part. Only whole number (i.e. integer) is returned by this function. For example, 1.25 is rounded down to 1. Similarly, −1.25 is rounded upto −2. ✞ 1 floor( -1.25) ✌ ✆ ✞ -2 ✌ ✆ ✞ 1 floor (1.25) ✌ ✆ ✞ 1 ✌ ✆ 1.5.4 Algebraic Absolute abs() converts negative number as its input argument into positive number as its output and do nothing with positive inputs. Piece-wise relation equivalent to this function is given below: abs = −x if x 0 x otherwise ✞ 1 abs ( -1.25) ✌ ✆ ✞ 1.25 ✌ ✆ ✞ 1 abs (1.25) ✌ ✆ ✞ 1.25 ✌ ✆
  • 36. 36 Modelica Commands www.sdmsacademy.in Sign sign() returns the sign of an input argument. If the argument is a positive number then it returns ‘1’ and if the argument is a negative number then it returns ‘-1’. Piece-wise relation equivalent to this function is given below: sign = −1 if x 0 1 otherwise ✞ 1 sign ( -1.25) ✌ ✆ ✞ -1 ✌ ✆ ✞ 1 sign (1.25) ✌ ✆ ✞ 1 ✌ ✆ Square Root sqrt() returns the square root of the supplied argument. The argument should be only positive. Negative argument returns error. Assume that if x is a positive number then its square root is given by y = √ x ✞ 1 sqrt ( -1.25) ✌ ✆ ✞ Error: Negative value as argument to sqrt . ✌ ✆ ✞ 1 sqrt (1.25) ✌ ✆ ✞ 1.118033988749895 ✌ ✆ 1.5.5 Trigonometric Modelica has inbuilt trigonometric functions like, sin(), cos(), tan(), asin(), acos(), atan(), atan2(), sinh(), cosh() and tanh(). These functions has usual meaning. Similarly, there are few inbuilt logarithmic functions, such as exp(), log() and log10().
  • 37. 1.6. CONNECTIONS 37 www.sdmsacademy.in 1.5.6 Special Functions Special functions are readymade functions which deals with advanced mathe- matics, like derivation, integrals, and advanced functions, like Bessel, Laplace function etc. Derivative A first order derivative equation is given by dx dt = −kv It can also be written as ẋ = −kv It means the derivative of distance of an object from origin at a time t is the velocity of the object. Here, k is a constant parameter. In Modelica, it is written as ✞ 1 der(x) = -kv ✌ ✆ der() is special built-in derivative operator equivalent to derivative. Mathe- matically, der(x) is equivalent to ẋ. To solve this equation, we need the same numbers of equations equal to the numbers of unknown variables in the equa- tion. 1.6 Connections Connectors are used to make connection between two or more variables. Con- nectors are defined outside the class and are used in equation part of the class. In Modelica, there are three types of connectors, “Potential”, “flow” and “stream”. 1.6.1 Connector A “Potential” type connector represents to the some kind of energy level. A “Potential” type connectors are shown in the example given below: ✞ 1 connector mPin Real v; 3 flow Real i; end mPin ; 5 class myPin // input variables 7 mPin p; // output variables 9 mPin n;
  • 38. 38 Modelica Commands www.sdmsacademy.in Real v; 11 equation 13 p.i=0; n.i=0; 15 v=p.v-n.v; end myPin; ✌ ✆ Flow Connectors The keyword flow declares a connector having net flow zero through itself. For example, flow of current through a node. The value of a flow variable is positive when the flow is into the component and negative when the flow is out from the component. Stream Connectors If at least one variable in a connector is stream type, the connector is called “stream connector” and the corresponding variable is called “stream variable”. To declared a connector as stream connector, stream is prefixed to a variable and it can only be used in a connector declaration. For every outside connector, one equation is generated for every variable with the stream prefix. For inside connectors, variables with the stream prefix do not lead to connection equations. Connection equations with stream variables are generated in a model when using the inStream() operator or the actualStream() operator. 1.6.2 Connect Connections between objects are introduced by connect equations in the equa- tion part of a class. A connect equation has the following syntax: ✞ 1 connect(ref a, ref b); ✌ ✆ ✞ 1 package test model mymodel 3 Modelica .Electrical .Analog.Basic.Resistor r1; Modelica .Electrical .Analog.Basic.Capacitor c1; 5 equation connect (r1.p, c1.n); 7 end mymodel ; end test ; ✌ ✆ 1.6.3 Input input keyword is used the set the input variable in partial function or class. Variables declared as input assigned the values received from argument. The
  • 39. 1.6. CONNECTIONS 39 www.sdmsacademy.in result is stored in the variable declared as output. The values from arguments are assigned first come first serve basis. Values are assigned to the input variable from top to bottom. ✞ function MyFunc 2 input Real a; //a=2 input Real b; //b=1 4 output Real s; output Real d; 6 output Real m; algorithm 8 s := a+b; // sum output d := a/b; // division output 10 m := a*b; // product output end MyFunc; ✌ ✆ Calling the function like ✞ 1 MyFunc (2.0, 1.0) ✌ ✆ Output is given as ✞ (3.0 ,2.0 ,2.0) ✌ ✆ Keeping the variable ‘b’ top of the variable ‘a’: ✞ 1 function MyFunc input Real b; //b=2 3 input Real a; //a=1 output Real s; 5 output Real d; output Real m; 7 algorithm s := a+b; // sum output 9 d := a/b; // division output m := a*b; // product output 11 end MyFunc; ✌ ✆ Calling the function like ✞ 1 MyFunc (2.0, 1.0) ✌ ✆ Output is given as ✞ (3.0 ,0.5 ,2.0) ✌ ✆
  • 40. 40 Modelica Commands www.sdmsacademy.in 1.6.4 Output It defines the output variables where results are stored. A output variable is declared as ✞ 1 output Real a; output Integer b; ✌ ✆ 1.6.5 N In electric module of Modelica, negative pin is denominated by ‘n’. It is declared as ✞ Pin n; ✌ ✆ 1.6.6 P In electric module of Modelica, positive pin is denominated by ‘p’. It is declared as ✞ 1 Pin p; ✌ ✆ 1.6.7 Expandable Connectors If the expandable qualifier is present on a connector definition, all instances of that connector are referred to as expandable connectors. Instances of con- nectors that do not possess this qualifier will be referred to as non-expandable connectors. Properties of the expandable connectors are 1. The expandable connectors can only be connected to other expandable connectors. 2. If a variable appears as an input in one expandable connector, it should appear as a non-input in at least one other expandable connector instance in the same augmentation set. 3. An augmentation set is defined as the set of connected expandable connec- tor instances that through the elaboration will have matching variables. 4. All components in an expandable connector are seen as connector instances even if they are not declared as such. 5. An expandable connector may not contain a component declared with the prefix flow, but may contain non-expandable connector components with flow components
  • 41. 1.7. ARRAYS 41 www.sdmsacademy.in 6. The expandable connector instance is automatically augmented with a new component having the used name and corresponding type. 7. If the undeclared component is sub-scripted, an array variable is created, and a connection to the specific array element is performed. 8. If the variable on the other side of the connect-equation is input or output the new component will be either input or output 1.7 Arrays In Modelica, array types are Type Description Array Type A = elem A Scalar Array Type A[n] = elem A 1D, n-elements Vector Array Type A[E] = elem A 1D, enumerated index vector Array Type A[n, m] = elem A n × m Matrix Array Type A[n1, . . ., nk] = elem; k dimensional (k ≥ 0) array. In Modelica, an array is created as a record containing several vectors and matrices. We can choose what we required. A demo record data is given below. ✞ 1 record Matrix Real m1 [7] = {17.1 , 62.7, 7.5, 37.0, 25, 4.121 , 12.333336}; 3 Integer m2 [4, 2] = {{44, 5}, {2, 800}, {7, 23}, {2032 , 77}}; String[1, 5] m3 = {{Magnus, circle, boy, Marina, Miranda}}; 5 String m4 [:]; Boolean m5 [:, 2]; 7 Boolean m6 [0]; end Matrix; ✌ ✆ 1.7.1 Range Vector A range vector is a vector containing a range of numeric values increasing or decreasing by a fixed increment or decrements value for each additional element. A range vector is created by using symbol ‘:’. There are two ways of using symbol ‘:’. Exampl for first way is given below:
  • 42. 42 Modelica Commands www.sdmsacademy.in ✞ class RangeVectors 2 Integer v1 [6] = 3 : 8; //{3, 4, 5, 6, 7, 8} 4 Integer v2 [3]= -20 : -18; //{-20, -19, -18} Real v3 [3] = 11.3 : 13.3;// {11.3 , 12.3, 13.3} 6 Integer v4 [0]; // Empty range vector end RangeVectors ; ✌ ✆ The default magnitude of common difference in the above method is ‘1’. For different common difference, range vector is used as ✞ class RangeVectors 2 Integer v1 [6] = 3 :2: 8; //{3, 5, 7} Integer v2 [3]= -20 :2: -18; //{-20, -18} 4 Real v3 [3] = 11.3 :1.5: 13.3;// {11.3 , 12.8} end RangeVectors ; ✌ ✆ 1.7.2 Size Function size() is used to get a vector of length of an array containing the dimension sizes of array. It is used like ✞ 1 size (array ) ✌ ✆ To get the specific element of the array, we use an index reference like ✞ 1 size (array , index ) ✌ ✆ ndims() is used to get the number of dimensions of array. 1.7.3 Array Concatenate General array concatenation can be done through the array concatenation op- erator cat() that concatenates the arrays. The syntax of this function is ✞ 1 cat(k, A, B, C, ...) ✌ ✆ Here, k is the dimension of array concatenated. Concatenating along the first dimension (1) is to concatenate rows and the second dimension (2) is to con- catenate columns. See example below: ✞ 1 class Concat // Row concatenation k=1 3 Real [5] c1 = cat(1, {1, 2}, {10, 12, 13}) ; // Result: {1, 2, 10, 12, 13} 5 // Column concatenation k=2
  • 43. 1.7. ARRAYS 43 www.sdmsacademy.in 7 Real [2, 3] c2 = cat(2, {{1, 2}, {3, 4}}, {{10} , {11}}); // Result: {{1, 2, 10}, {3, 4, 11}} 9 end Concat ✌ ✆ A matrix can be constructed from scalar expressions by separating rows by semicolon and columns by comma. The previous example of concatenation along the second dimension can be expressed like this: ✞ 1 class Concat2 Real [2, 3] c3 = cat(2, [1, 2; 3, 4], [10; 11]) ; 3 // Result: {{1, 2, 10}, {3, 4, 11}} end Concat2; ✌ ✆ 1.7.4 Array Index The Array Indexing operator is used to access array elements for retrieval of their values or for updating these values. When at least one of the index expressions in an array indexing operation is a vector expression, a slice or subsection of the array is accessed rather than a single element. ✞ class ArrayIndex 2 Real [2, 2] A = {{2, 3}, {4, 5}}; // Definition of array A Real A_Retrieval = A[2, 2]; // Get array element value 5 4 Real B[2, 2]; Real c; 6 algorithm B := fill (1,2,2) ; // B will have the values {{1, 1}, {1, 1}} 8 B[2, 1] := 8; // Assignment to the array element B[2, 1] c := A[1, 1]; 10 end ArrayIndex ; ✌ ✆ 1.7.5 Array Mathematics Arithmetic operators on arrays and certain combinations of arrays and scalars are available through the standard arithmetic operators, addition (+), subtrac- tion (-), division (/), multiplication as well as through the exponentiation (ˆ) operator. For addition and subtraction, the size of vectors should be same. ✞ class AddSub 2 Real Add1 = {1, 2, 3} + 1; // Not allowed! Real Add2 = {1, 2, 3} + {1, 2}; // Not allowed , different sizes!
  • 44. 44 Modelica Commands 6 www.sdmsacademy.in 4 Real Add3 [2, 2] = {{1, 1}, {2, 2}} + {{1, 2}, {3, 4}}; // Result: {{2, 3}, {5, 6}} Real Sub1 [3] = {1, 2, 3} - {1, 2, 0};// Result: {0, 0, 3} 8 end AddSub; ✌ ✆ In case of product, scalar and vector products are allowed. Scalar and vector products are performed element-wise. In matrix product, product is allowed if number of rows of first matrix is equal to the number of columns of second matrix. ✞ 1 class Multiplication Real m1 [3] = {1, 2, 3} * 2; 3 // Elementwise multiplication : {2, 4, 6}; 5 Real m2 [3] = 3 * {1, 2, 3}; // Elementwise multiplication : {3, 6, 9}; 7 Real m3 = {1, 2, 3} * {1, 2, 2}; 9 // Scalar product: 11; 11 Real m4 [2] = {{1, 2}, {3, 4}} * {1, 2}; // Matrix product : {5, 11}; 13 end Multiplication ; ✌ ✆ In division, vector or matrix to scalar division is allowed but scalar to vector or matrix division is not allowed. ✞ 1 class Div Real [3] Div1 , Div2 , Div3 ; 3 equation Div1 = {2, 4, 6} / 2; // Result: {1, 2, 3} 5 Div2 = 6 / {1, 2, 3}; // Not allowed! Div3 = {1, 2, 3} / {1, 2, 2}; // Not allowed! 7 end Div; ✌ ✆ Array exponentiation is the same as repeated matrix multiplication and is there- fore only declared when the first operand is a square matrix and the second operand is a non-negative integer. ✞ 1 class Exponent Real m[2, 2]; 3 equation m = {{1, 3}, {4, 5}} ^ 2; 5 // Result is {{13, 18}, {24, 37}}. end Exponent ; ✌ ✆
  • 45. 1.8. MATRIX 45 www.sdmsacademy.in 1.7.6 Array Constructor An array constructor function constructs and returns an array computed from its arguments. Most of the constructor functions in the table below construct an array by filling in values according to a certain pattern. ✞ class ConstructFunc 2 Real z[2,3] = zeros(2, 3); // Constructs the matrix {{0,0,0}, {0,0,0}} 4 Real o[3] = ones (3); // Constructs the vector {1, 1, 1} 6 Real f[2,2] = fill (2.1 ,2 ,2); // Constructs the matrix {{2.1 , 2.1}, {2.1, 2.1}} 8 Boolean check[3, 4] = fill (true , 3, 4); // Fills a Boolean matrix 10 Real id [3,3] = identity (3); // Creates the matrix {{1,0,0}, {0,1,0}, {0, 0, 1}} 12 Real di [3,3] = diagonal ({1, 2, 3}); // Creates the matrix {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}} 14 Real ls [5] = linspace (0.0, 8.0, 5); // Computes the vector {0.0, 2.0, 4.0, 6.0, 8.0} 16 end ConstructFunc ; ✌ ✆ The following set of functions are rather common in matrix and vector algebra, but are not associated with any Modelica operator symbol. ✞ class AlgebraFunc 2 Real transp1 [2,2] = transpose ([1, 2; 3, 4]); // Gives [1, 2; 3, 4] of type Integer [2, 2] 4 Real transp2 [2,2,1] = transpose ({{{1} ,{2}} ,{{3} ,{4}}}); // Gives {{{1} ,{2}} ,{{3} ,{4}}} of type Integer [2, 2, 1] 6 Real out[2, 2] = outerProduct ({2,1}, {3 ,2}); // Gives {{6, 4}, {3, 2}} 8 Real symm [2, 2] = symmetric ({{1, 2}, {3, 1}}) ; // Gives {{1, 2}, {2, 1}} 10 Real c[3] = cross({1, 0, 0}, {0, 1, 0}); // Gives {0, 0, 1} 12 Real s[3, 3] = skew ({1, 2, 3}); // Gives {{0, -3, 2}, {3, 0, -1}, {-2, 1, 0}}; 14 end AlgebraFunc ; ✌ ✆ 1.8 Matrix Matrix in Modelic is created as ✞ class MyMatrix
  • 46. 46 Modelica Commands www.sdmsacademy.in 2 Real m[rows , cols ] = [..; ..]; end MyMatrix ; ✌ ✆ In Modelica matrix, elements are separated by comma (,) while rows are sepa- rated by semicolon (;). The elements are placed inside the square brackets [..;..]. A two by two square matrix is given below. ✞ 1 class MyMatrix Real m[2, 2] = [1, 2; 3, 4]; 3 end MyMatrix ; ✌ ✆ Mostly all the mathematical operations on matrices are similar to the mathe- matical operations on arrays. 1.8.1 Identity Matrix identity() returns the n × n Integer identity matrix, with ones on the diagonal and zeros at the other places. An identity matrix of order 3 × 3 is shown below: I =
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52. 1. 0. 0. 0. 1. 0. 0. 0. 1.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58. 1.8.2 Diagonal Matrix diagonal() returns a square matrix with the elements of vector v on the diagonal and all other elements zero. Its syntax is ✞ 1 diagonal (matrix name ) ✌ ✆ Take a matrix A =
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64. 1. 2. 3. 4. 1. 5. 8. 8. 9.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70. The diagonals of the matrix ‘a’ is D =
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82. If matrix name supplied to this function is of M × 1 size then it convert it into the matrix of M × M size with all the input elements at the diagonal of the output matrix. Elements other than diagonal are sets to zero. Take input matrix of type L =
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95. 1.9. MISCELLANEOUS 47 www.sdmsacademy.in Then diagonal matrix shall be D =
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101. 1. 0. 0. 0. 1. 0. 0. 0. 9.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107. 1.8.3 Transpose of Matrix transpose() returns the transpose of a given matrix. Its syntax is ✞ 1 transpose ( matrix name ) ✌ ✆ Assume a complex matrix of order 2 × 2 is A = 3 + i 2 1 2 + i Then transpose of the matrix will be A = 3 + i 1 2 2 + i 1.8.4 Cross Products cross() returns the cross or vector products between two matrices. For cross project, rows of one matrix should be equal to the column of other matrix. Cm×n = Am×n × Bm×n In vector product may be Am×n × Bm×n = Bm×n × Am×n or not. 1.9 Miscellaneous 1.9.1 Annotation Units Annotations are attributes that are not really part of the Modelica core language but still belongs to Modelica in a broad sense, and therefore are part of the Modelica language definition. Since annotations need to have a certain Syntax and Placement in classes. Graphical Annotations is the most common form of annotation in typical Modelica models. 1.9.2 Keywords In Modelica, there are following reserved keywords for exclusive use of Modelica. These keywords can not re-declared or redefined by the user itself.
  • 108. 48 Modelica Commands www.sdmsacademy.in algorithm discrete false loop pure and each final model record annotation else flow not redeclare assert elseif for operator replaceable block elsewhen function or return break encapsulated if outer stream class end import output then connect enumeration impure package true connector equation in parameter type constant expandable initial partial when constrainedby extends inner protected while der external input public within