OpenModelica/C2/Functions-and-Types/English

From Script | Spoken-Tutorial
Revision as of 12:24, 15 March 2016 by Kaushik Datta (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
Slide Number 1

Title Slide

Welcome to the spoken tutorial on Functions and Types.
Slide:

Learning Objectives

In this tutorial, we are going to learn:
  • How to define a function.
  • How to use algorithm.
  • How to define a type.
Slide:

System Requirements

To record this tutorial, I am using
  • OpenModelica 1.9.2 and Ubuntu operating system version 14.04
  • But, this process is identical in Windows, Mac OS X or FOSSEE OS.
Slide:

Pre-requisites

To understand this tutorial You need to know how to define a class in Modelica.
  • You need knowledge of functions in any programming language.
  • Pre-requisite tutorials are mentioned on our website.
  • please go through them.
Slide:

function

Let us discuss a function now.

function is a specialized class that can take input and return output.

It contains algorithm section.

A function cannot contain equations and it cannot be simulated.

Slide:

Syntax of function

The syntax of a function is as shown.
Slide:

Problem Statement

Now let us write a function named polynomialEvaluator which
  • takes x as input and
  • returns f(x) = a x (squared) (plus) b x (plus) c where a=1, b=2 and c=1 as output.

/* OMEdit */ polynomialEvaluator file is available on our website.

Please download and save all the files available in Code Files link.

To demonstrate polynomialEvaluator function, let me go to OMEdit.

OMEdit is now open in Welcome perspective.

You are only seeing a part of the OMEdit window since I have zoomed in.

I shall show you the relevant portions by shifting the window whenever necessary.

‘Open Model/Library file’ tool To open the files that you have downloaded, click on Open Model/Library File tool.

I have saved all the files in a folder.

Let me select them together and click on open.

If we have saved these files in different folders, you may open each one of them individually.

Note that the following classes or functions are now open in OMEdit:
  • bouncingBallWithUserTypes
  • functionTester
  • multipleFunctionTester
  • multiplePolynomialEvaluator and
  • polynomialEvaluator.
Point to Text View icon. To open polynomialEvaluator function and view it, Right-click on the icon in Libraries Browser and select View Class.

If the function doesn’t open in Text View, open it in Text View.

/* polynomialEvaluator */

function polynomialEvaluator

Name of this function is polynomialEvaluator as we have already discussed.
input Real x; x is a real variable.
input Real x; input is a keyword which is used to specify input variables.
output Real fx; Similarly, output is a keyword which is used to specify output variables.

fx is a real variable which represents f(x).

protected Real a = 1; Any variable or parameter which is neither input nor output is specified using protected keyword.
protected parameter Real a = 1; a is a real parameter with a value of 1.
protected parameter Real b = 2;

protected parameter Real c = 1;

The values of a, b and c are as discussed in the slides already.
algorithm. Please note that a, b and c are protected parameters.

Algorithm represents the beginning of algorithm section of a function.

Algorithm section may contain assignment statements only.

fx := a*x^2 + b*x + c; This sign indicates assignment.
fx := a*x^2 + b*x + c; In an assignment statement the value of right hand side is assigned to the left hand side.
fx := a*x^2 + b*x + c; The left hand side is usually an unknown expression.

In this case fx is an unknown variable.

fx := a*x^2 + b*x + c; The value of right hand side expression here can be calculated if the value of x is known.


fx := a*x^2 + b*x + c; x is usually passed as an input argument to the function whenever it is called.
Now let us see how to call a function using functionTester class.

functionTester icon can already be seen in Libraries Browser since I have already opened it.

Right-click on functionTester and select View Class To open this class, double-click on its icon.

This is an alternative way of viewing a class.

you may also right click on its icon and select View Class.

Real z; z is a real variable.
z = polynomialEvaluator(10); polynomialEvaluator function is called with an input argument of 10 units and it is equated to z.
z = polynomialEvaluator(10); The input value (variable) of polynomialEvaluator that is x takes the value of 10 units.
Click on Simulate button. Now let us simulate this class.

To simulate this class you may right click on functionTester icon in Libraries Browser and select Simulate.

The class has now simulated.

You may also use the Simulate button in toolbar to simulate a class.

/* Plotting perspective */

Click on z in the variables browser

Now let me show you the Plotting perspective completely by shifting OMEdit window to the left.

Select z in the variables browser

Note that the value of z is equal to the value of f(x) at x = 10.

Now let me de-select z and delete this result.

Let me go back to Modeling perspective. Let me go back to Modeling perspective.
Point to polynomialEvaluator Click on polynomialEvaluator tab at the top.

Notice that polynomialEvaluator function has only one output variable.

Now let me show you how to output two or more variables using a function.

I have created a function named multiplePolynomialEvaluator which has two output variables.

Before viewing that function let me close the tabs of PolynomialEvaluator and FunctionTester.

Since the Libraries Browser is not visible, let me shift the window to the right.

Double-click multiplePolynomialEvaluator Double-click on multiplePolynomialEvaluator, multipleFunctionTester and bouncingBallWithUserTypes.

Shift the window back to its place.

Go to multiplePolynomialEvaluator tab.

This function is similar to polynomialEvaluator function except for an additional output variable.

output Real gx; An output variable name gx has been declared.
gx := a*x^2 - b*x + c; gx is assigned the value of a x (squared) (minus) b x (plus) c

The order in which output or input variables are declared is important.

We will understand more about this when we discuss multipleFunctionTester class.

Click on multipleFunctionTester tab Now let me switch to multipleFunctionTester tab.
/* multipleFunctionTester */

Real y;

Real z;

y and z are declared as real variables.
(y,z) = multiplePolynomialEvaluator(10); multiplePolynomialEvaluator function is called with an input argument of 10 units.

This means that the input variable of multiplePolynomialEvaluator takes a value of 10 units.

(y,z) = multiplePolynomialEvaluator(10); y and z take the values of f(x) and g(x) at x = 10 respectively.

y takes the value of output variable fx since the declaration of fx appears before the declaration of gx in the function.

Now let me simulate this class.

Click on Simulate button. Click on ‘Simulate’ button.

Close the pop up window.

/* Plotting perspective */

Select y and z in the variables browser

Select ‘y and z in the variables browser.


Note that the values of y and z are the same as f(x) and g(x) at x = 10 respectively.

Delete the result and go back to Modeling Perspective. Delete the result and go back to Modeling Perspective.
/* multipleFunctionTester */

(z,y) = multiplePolynomialEvaluator(10);

Now, let me interchange the order of y and z.


Delete (y,z) and type (z,y).


and save this class by pressing Ctrl+S.

Simulate this class once again.

Close the pop up window.

/* Plotting perspective */

Select y and z once again in the variables browser

Select y and z in the variables browser once again.


Note that the values of y and z have been interchanged as compared to the previous case.

Let me delete this result and go back to Modeling Perspective.

Let me go back to the slides.
Slide:

algorithm

algorithm is Modelica syntax element to enable procedural programming.

Only assignment statements are allowed in algorithm section.

Assignment statements use the following symbol.

Data flows in assignment statements from right to left.

Slide:

Restrictions on functions.

There are certain restrictions on functions defined in Modelica.

Use of der() in a function is invalid.

Use of time variable is not allowed.

Use of when statements is not allowed in a function.

A function may not have more than one algorithm section and

Models cannot be passed as arguments.

Slide:

type

type is a specialized class to define custom data-types in Modelica.


For example, physical quantities like velocity and current can be defined as data-types.

They can be used later to declare other variables.

Attributes of Modelica data-types like unit, and start can be changed accordingly.

For example, in the above case velocity is defined to be the same as real data type.

But its unit modified to m/s.

I have created a model named bouncingBallWithUserTypes to simulate type definitions.

Let me go back to OMEdit to demonstrate this model.
/* bouncingBallWithUserTypes */ Click on bouncingBallWithUserTypes tab.
This model is similar to the bouncingBall model which was discussed in previous tutorials.

Please go through to the per-requisite tutorials to understand bouncingBall model.

type Length = Real(unit="m"); Length is defined as Real datatype. Its unit is modified to m.
type Velocity = Real(unit="m/s"); Similarly Velocity is defined as Real.

Its unit is modified to m/s

Length h; h represents height of the ball from surface of earth.

It is defined as length datatype.

Velocity v; Similarly v represents velocity of ball.

It is declared to be of velocity datatype.

Remaining variable declarations and equations of this model are similar to bouncingBall model.

Now let me simulate this.

Close the pop up window.

/* Plotting perspective */ In the Variables Browser notice that h and v have their respective units associated with their data-types.

Let me select h in the variables browser.


The plot of h versus time is similar to that of bouncingBall model.

Let me de-select h.

Now let me go back to the slides.
Slide:

Assignment.

As an assignment breach the restrictions on functions and observe the errors produced.
This brings us to the end of this tutorial.
Slide:

About the Spoken Tutorial project

Watch the video available at following link.

http://spoken-tutorial.org/ http://spoken-tutorial.org] /What\_is\_a\_Spoken\_Tutorial

It summarises the Spoken Tutorial project

Slide:

Spoken Tutorial Workshops

We conducts workshops using spoken tutorials

Please contact us.

Slide:

Forum

If you have questions in this spoken tutorial, please visit the website.
Slide:

Textbook Companion Project

We coordinate coding of solved examples of popular books.

Please visit the websites for more information.

Slide:

Lab Migration Project

We help migrate commercial simulator labs to OpenModelica.
Slide:

Acknowledgements

Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India.
Slide:

Thanks

We thank the development team of OpenModelica for their support.
Thank you.

Contributors and Content Editors

Kaushik Datta