OpenModelica/C2/Functions-and-Types/English
Visual Cue | Narration |
Slide Number 1
Title Slide |
Welcome to the spoken tutorial on Functions and Types, as a part of the series on OpenModelica |
Slide:
Learning Objectives |
In this tutorial, we are going to learn:
|
Slide:
System Requirements |
To record this tutorial, I am using
|
Slide:
Pre-requisites |
You need to know how a class is defined in Modelica.
You need knowledge of functions in any programming language. Pre-requisite tutorials are mentioned on our website. |
Slide:
function |
Modelica has specialized classes for specific purposes.
‘function’ is a specialized class that can take input and return output Every ‘function’ contains an ‘algorithm’ section. We will understand more about ‘algorithm’ when we discuss an example. 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 |
Let us write a function named ‘polynomialEvaluator’ which
|
/* OMEdit */ | You may find ‘polynomialEvaluator’ file on our website.
Please download all files provided in Code Files link. To demonstrate ‘polynomialEvaluator’ function, let me go to OMEdit. OMEdit is now open in ‘Welcome’ perspective. You are seeing only a part of the window since I have zoomed in. I shall shift the window to show you the relevant portions when required. |
‘Open Model/Library file’ tool | To open these files, click on Open Model/Library File tool.
I have saved all these files in a folder. Select them together and click on open. If they are saved in different folders, please open each file individually. |
Note that the following classes and functions are now open in OMEdit:
| |
Point to Text View icon. | Right-click on ‘polynomialEvaluator’ 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’ |
input Real x; | ‘x’ is a real variable. |
input Real x; | ‘input’ is a keyword used to specify input variables. |
output Real fx; | ‘fx’ is a real variable signifying f(x) |
output Real fx; | ‘output’ is a keyword used to specify output variables. |
protected Real a = 1; | In a function, variables other than input and output are specified using ‘protected’ keyword.
‘protected’ variables can only be accessed from within the class. |
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 b and c are as discussed in slides. |
algorithm. | This indicates the beginning of ‘algorithm’ section of function.
Algorithm section may contain assignment statements only. |
fx := a*x^2 + b*x + c; | This sign indicates an assignment. |
fx := a*x^2 + b*x + c; | The value of right hand side expression is assigned to left hand side. |
fx := a*x^2 + b*x + c; | Left hand side of an assignment statement is usually unknown. fx is an unknown variable. |
fx := a*x^2 + b*x + c; | This expression can be evaluated from the value of ‘x’.
‘x’ is passed as an argument to the function. |
fx := a*x^2 + b*x + c; | Assignment statements end in a semi-colon. |
Now, we need a class to test this function.
I have created a class named ‘functionTester’. Since I have opened all code files, ‘functionTester’ icon can be seen in ‘Libraries Browser’ | |
Right-click on ‘functionTester’ and select ‘View Class’ | To open this class, double-click on its icon.
This is an alternative way of viewing it. |
Real z; | ‘z’ is defined as a real variable. |
z = polynomialEvaluator(10); | ‘polynomialEvaluator’ function is called with an argument of 10. |
z = polynomialEvaluator(10); | The input variable of function that is ‘x’ takes the value of 10 units. |
Click on ‘Simulate’ button. | Click on ‘Simulate’ button. |
/* Plotting perspective */
Click on ‘z’ in the ‘variables browser’ |
Click on ‘z’ in the ‘variables browser’
Note that ‘z’ is the value of function ‘polynomialEvaluator’ with x = 10. |
Click on ‘Modeling’ button at bottom right. | Click on ‘Modeling’ button at bottom right. |
Point to polynomialEvaluator | ‘polynomialEvaluator’ function has only one output.
|
Double-click ‘multiplePolynomialEvaluator’ | I have already opened this file. Double-click on ‘multiplePolynomialEvaluator’ icon. |
output Real gx; | This function returns an extra output gx.
Other variables remain the same as ‘polynomialEvaluator’ function. |
output Real fx;
output Real gx; |
The order in which output or input variables are defined is important.
It is the order in which function accepts/returns values. |
gx := a*x^2 - b*x + c; | gx is assigned a value of a x (squared) (minus) b x (plus) c |
Click on ‘multipleFunctionTester’ tab | We shall use ‘multipleFunctionTester’ class to test this function.
Double-click on ‘multipleFunctionTester’ in ‘Libraries Browser’ |
/* multipleFunctionTester */
Real y; Real z; |
‘y’ and ‘z’ are declared as real variables. |
(y,z) = multiplePolynomialEvaluator(10); | ‘multiplePolynomialEvaluator’ is called with an argument of 10 units.
Hence, the value of ‘x’ is 10. |
(y,z) = multiplePolynomialEvaluator(10); | Notice that ‘y’ appears before ‘z’.
‘y’ takes the value of f(x) at x = 10. This is because fx is declared before gx in the function.
|
Click on ‘Simulate’ button. | Click on ‘Simulate’ button. |
/* Plotting perspective */
Click on ‘y’ and ‘z’ in the ‘variables browser’ |
Click on ‘y’ and ‘z’ in the ‘variables browser’.
|
Click on ‘Modeling’ button. | Click on ‘Modeling’ button at bottom right. |
/* multipleFunctionTester */
(z,y) = multiplePolynomialEvaluator(10); |
Now, let me change the order of ‘y’ and ‘z’.
|
Click on ‘Simulate’ button | Click on ‘Simulate’. |
/* Plotting perspective */
Click on ‘y’ and ‘z’ in the ‘variables browser’ |
Click on ‘y’ and ‘z’ in the ‘variables browser’.
|
Let me go back to the slides. | |
Slide:
algorithm |
algorithm is a Modelica syntax element.
Only assignment statements are allowed inside algorithm section Declaring equations in algorithm sections is not allowed. Assignment statements use `:=' instead of `=' Data flows in assignment statements from right to left. The right hand side expression is assigned to left hand side expression. |
Slide:
Restrictions on functions. |
‘der()’ cannot be used inside a function
‘time’ variable cannot be used ‘when’ statements are not allowed A function cannot have more than one ‘algorithm’ section Models cannot be passed as arguments to a function |
Slide:
type |
‘type’ is a specialized class to define custom data-types
They can be used to declare variables later.
|
Let me go back to OMEdit | |
Point to ‘bouncingBallWithUserTypes’ model. | ‘bouncingBallWithUserTypes’ model will be used to demonstrate type definitions. |
/* bouncingBallWithUserTypes */ | Click on ‘bouncingBallWithUserTypes’ tab. |
This model is the same as ‘bouncingBall’ model except for user-defined types. | |
type Length = Real(unit="m"); | ‘Length’ type is defined as Real. Its unit is modified to ‘m’. |
type Velocity = Real(unit="m/s"); | ‘Velocity’ type is defined as Real.
Its unit is modified to ‘m/s’ |
Length h; | ‘h’ is defined to be of the type ‘Length’.
Therefore, the unit of ‘h’ is ‘m’. |
Velocity v; | Velocity of the ball is defined to be of type ‘Velocity’. |
Other variable declarations and equations remain the same as in ‘bouncingBall’ model. | |
Click on ‘Simulate’ button | Click on ‘Simulate’ button in the toolbar. |
/* Plotting perspective */
|
The model has been simulated.
Let me scroll right in ‘Variables Browser’.
|
Let me go back to the slides | |
Slide:
Assignment. |
Breach the restrictions on functions. Observe the errors produced as a result. |
This brings us to the end of this tutorial | |
Slide:
About the Spoken Tutorial project |
Watch the video available at the link shown below
http://spoken-tutorial.org/ http://spoken-tutorial.org] /What\_is\_a\_Spoken\_Tutorial It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it. |
Slide:
Spoken Tutorial Workshops |
We conducts workshops using spoken tutorials
Give certificates Please contact us. |
Slide:
Forum |
If you have questions in this tutorial, please visit the webpage mentioned. |
Slide:
Textbook Companion Project |
We coordinate coding of solved examples from popular books.
We give honorarium to contributors. Please visit our website. |
Slide:
Lab Migration Project |
We help migrate labs from commercial simulators to OpenModelica. |
Slide:
Acknowledgements |
Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India. |
Slide:
Thanks |
We thank OpenModelica development team for their support. |
Thank you for joining me in this tutorial. Goodbye! |