• Keine Ergebnisse gefunden

3   Basics

3.1   The Modelica Language

3.1.4   Specialized Classes

The key concept of the Modelica language is the previously introduced class. This general class concept founds the basis for specialized classes which have the same properties as a class, apart from restrictions, but also additional properties that make them usable under appropriate conditions. They offer a way to make the Modelica code easier to read and maintain. The specialized classes are:

record: a record is a specialized class to define data structures without behavior.

Equations are not allowed within a record definition.

type: a type is a specialized class to define aliases or extensions of predefined types (Real, Integer, Boolean, String, enumeration(…)), records, or arrays.

model: a model is a specialized class that is identical to the basic class concept with no restrictions or additional properties.

block: a block is specialized class with the same properties of a model except the restriction that every variable must be either input or output, i.e. all variables have to be declared with the prefixes input or output.

function: a function is a specialized class to implement mathematical functions. The inputs of a function have to be prefixed by input and the results by output.

connector: a connector is a specialized class to define the variables that are

interchanged within a connection between two components. No equations are allowed in a connector.

package: a package is a specialized class to organize and structure Modelica classes and specialized classes. It can contain the declarations of classes, specialized classes, and constants while parameters and variables cannot be declared in a package.

The specialized classes model, block, function, connector, and package are used in the Petri net library and are discussed in more detail hereafter.

MODEL

The specialized class model has the same properties as a class and is used for modeling purposes. Three different equation types can occur in a model: discrete, differential, and algebraic equations. Example 3.6 contains these equation types.

Example 3.6 model example

parameter Real a=0.8;

parameter Real b=1.78;

Real c(start=6.7);

Real d;

discrete Boolean e;

equation

der(c)=-a*c/(b+c); //Differential equation d=-3*c-7.2; //Algebraic equation when c<3.3 then //Discrete equation e=true;

end when;

end example;

The equation

der(c)=-a*c/(b+c);

is a differential equation and the corresponding variable c is a continuous-time variable. The operator der() accesses the derivative according to time. However, if an equation only involves algebraic formulas and no derivatives, it is an algebraic equation. In Example 3.6 the equation

d=-3*c-7.2;

is an algebraic equation while the equation when c<3.3 then

e=true;

end when;

is a discrete equation because the discrete-time variable e is only recalculated when the condition c<3.3 becomes true, i.e. only at event instants (see Section 3.1.6). Equation systems that contain algebraic as well as differential equations are called differential algebraic equation systems (DAEs) and if discrete equations also appear they are called hybrid differential algebraic equation systems (hybrid DAEs) (see Section 3.1.7).

BLOCK

A block has the same properties as a model with the restriction that all variables have to be either declared by the prefix input or output (see Example 3.7). If a block is used in another component, a binding equation has to be provided for each variable with the input -prefix to guarantee a balanced model (see Section 3.1.5). In Example 3.7 an instance of the block blockExample is used in the model modelExample. The variable s of the block has

the output-prefix and, hence, the equation is provided in the block. However, the variable u has the input-prefix and the equation has to be defined by a binding equation when the block is used. It is defined in the model by the equation u=time within brackets after the name of the block instance.

Example 3.7

block blockExample input Real u;

output Real s;

equation s=sin(u);

end blockExample;

model modelExample Real v;

blockExample exSin(u=time, s=v); //binding equation for u end modelExample;

FUNCTION

The specialized class function is used to implement mathematical algorithms with a sequence of statements. No equation sections are allowed in functions only algorithm sections. A typical structure of a function is outlined in Example 3.8.

Example 3.8

function functionName input typeI1 in1;

input typeI2 in2; ...

output typeO1 out1;

output typeO2 out2; ...

protected

<local variables>

...

algorithm ...

<statements>

...

end functionName;

The first part of a function declares inputs and outputs using the keywords input and output. After the keyword protected, local variables can be declared neither input nor output of the function. The algorithmic procedure starts with the keyword algorithm; thereby, all types of statements, mentioned in Section 3.1.3, can be used. The function of Example 3.9 calculates the sum of vector elements. The input is a vector of unknown size which is identified with the operator "[:]" and the output is the scalar y.

Example 3.9

function functionExample input Real x[:];

output Real y;

algorithm y:=0;

for i in 1:size(x,1) loop y:=y+x[i];

end for;

end functionExample;

A function can be called from within classes, models, blocks, or other functions by the prompt (out1, out2, ...) = functionName(inp1,inp2,...);

The function of Example 3.9 can be called by z = functionExample({1,2,3,8});

It is also possible to integrate functions written in C or FORTRAN 77 in the Modelica environment. This can be done with the aid of the key word external followed by the respective language within quotation marks. The function random of Example 3.10 generates a uniformly distributed random number by an external C-function. It has no inputs and returns the random number x. The Include annotation specifies the C-file that contains the respective implementation.

Example 3.10 Modelica function:

function random output Integer x;

external "C" annotation(Include = "#include <random.c>", __Dymola_pure=false);

end random;

C-function:

#include <stdlib.h>

#ifndef RANDOM_C

#define RANDOM_C int random() {

static int called=0;

int i;

if (!called) {

srand((unsigned) time(NULL));

called=1;

}

return rand();

}

#endif

Modelica functions are according to the specification (Modelica Association 2010) pure which means that

 Modelica functions are mathematical functions that always return the same result when they are called with the same input arguments.

 Modelica functions are side-effect free with respect to the internal Modelica state.

 Exception: An impure function is either an impure external function or a Modelica function which calls an impure external function. An impure function returns different values by calling with the same inputs. They can be called from within a when-equation or a when-statement. The function random of Example 3.10 is an example of an impure function which has no input arguments and always returns another value when it is

required. The Dymola tool identifies an impure function by the vendor-specific annotation __Dymola_pure=false.

CONNECTOR

The specialized class connector is used to implement connectors. Connectors declare variables that are interchanged between components. A possible structure of a connector is outlined in Example 3.11.

Example 3.11

connector connectorName input typeI1 in1;

input typeI2 in2;

...

output typeO1 out1;

output typeO2 out2;

...

end connectorName;

Two connectors can be connected by a connector equation (see Section 3.1.2), e.g.

connect(connector1, connector2);

The prefixes input and output define the location of the corresponding equation and guarantee in this manner balanced modeling (see Section 3.1.5). When a variable is provided with the input-prefix, the equation is part of the connected component while the equation of an output variable is located in the component where the connector is used.

PACKAGE

The specialized class package is used to structure and organize Modelica classes. The package of Example 3.12 comprises the examples of this and the previous sections.

Example 3.12

package packageExample

function random output Integer x;

external "C" annotation(Include = "#include <random.c>", __Dymola_pure=false);

end random;

block blockExample input Real u;

output Real s;

equation s=sin(u);

end blockExample;

model modelExample Real v;

example2_6a exSin(u=time, s=v);//binding equation for u end modelExample;

model example

parameter Real a=0.8;

parameter Real b=1.78;

Real c(start=6.7);

Real d;

discrete Boolean e;

equation

der(c)=-a*c/(b+c); //Differential equation d=-3*c-7.2; //Algebraic equation when c<3.3 then //Discrete equation e=true;

end when;

end example;

function functionExample input Real x[:];

output Real y;

algorithm y:=0;

for i in 1:size(x,1) loop y:=y+x[i];

end for;

end functionExample;

end packageExample;