Difference between revisions of "OpenModelica/C2/Functions-and-Types/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(5 intermediate revisions by the same user not shown)
Line 6: Line 6:
  
 
'''Title Slide'''
 
'''Title Slide'''
|| Welcome to the spoken tutorial on '''Functions and Types''', as a part of the series on '''OpenModelica'''
+
|| Welcome to the spoken tutorial on '''Functions and Types'''.
  
 
|-
 
|-
Line 14: Line 14:
 
|| In this tutorial, we are going to learn:
 
|| In this tutorial, we are going to learn:
  
* How to define a '''function'''
+
* How to define a '''function'''.
  
* How to use '''algorithm'''
+
* How to use '''algorithm'''.
  
* How to define a '''type'''
+
* How to define a '''type'''.
  
 
|-
 
|-
Line 26: Line 26:
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
  
* '''OpenModelica 1.9.2 Linux version'''  
+
* '''OpenModelica 1.9.2''' and '''Ubuntu operating system version 14.04'''  
  
* But, this process is identical in Windows, '''Mac OS''' and '''FOSSEE on ARM'''
+
* But, this process is identical in '''Windows''', '''Mac OS X''' or '''FOSSEE OS'''.
  
 
|-
 
|-
Line 34: Line 34:
  
 
'''Pre-requisites'''
 
'''Pre-requisites'''
||You need to know how a class is defined in '''Modelica'''.
+
||To understand this tutorial You need to know how to define a '''class''' in '''Modelica'''.
  
You need knowledge of functions in any programming language.
+
* You need knowledge of '''functions''' in any programming language.
  
Pre-requisite tutorials are mentioned on our website.
+
* Pre-requisite tutorials are mentioned on our website.
 +
 
 +
* please go through them.
  
 
|-
 
|-
Line 44: Line 46:
  
 
'''function'''
 
'''function'''
|| '''Modelica''' has specialized classes for specific purposes.
+
|| Let us discuss a '''function''' now.
  
'''‘function’''' is a specialized class that can take input and return output
+
'''function''' is a specialized class that can take input and return output.
  
Every '''‘function’''' contains an '''‘algorithm’''' section.  
+
It contains '''algorithm''' section.
  
We will understand more about '''‘algorithm’''' when we discuss an example.
+
A '''function''' cannot contain equations and it cannot be simulated.
 
+
A '''function''' cannot contain equations and it cannot be simulated
+
  
 
|-
 
|-
Line 58: Line 58:
  
 
'''Syntax of function'''
 
'''Syntax of function'''
|| The syntax of a function is as shown.
+
|| The syntax of a '''function''' is as shown.
  
 
|-
 
|-
Line 64: Line 64:
  
 
'''Problem Statement'''
 
'''Problem Statement'''
|| Let us write a function named '''‘polynomialEvaluator’''' which
+
|| Now let us write a '''function''' named '''polynomialEvaluator''' which
 
+
* takes '''‘x’''' as input
+
  
* returns '''‘f(x) = a x (squared) (plus) b x (plus) c’''' as output
+
* 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 */
 
|| /* OMEdit */
|| You may find '''‘polynomialEvaluator’''' file on our website.
+
|| '''polynomialEvaluator''' file is available on our website.
  
Please download all files provided in Code Files link.
+
Please download and save all the files available in '''Code Files''' link.
  
To demonstrate '''‘polynomialEvaluator’''' function, let me go to '''OMEdit'''.
+
To demonstrate '''polynomialEvaluator''' function, let me go to '''OMEdit'''.
  
'''OMEdit''' is now open in '''‘Welcome’''' perspective.
+
'''OMEdit''' is now open in '''Welcome''' perspective.
  
You are seeing only a part of the window since I have zoomed in.  
+
You are only seeing a part of the '''OMEdit''' window since I have zoomed in.  
  
I shall shift the window to show you the relevant portions when required.
+
I shall show you the relevant portions by shifting the window whenever necessary.
  
 
|-
 
|-
 
|| '''‘Open Model/Library file’''' tool
 
|| '''‘Open Model/Library file’''' tool
|| To open these files, click on '''Open Model/Library File''' tool.
+
|| To open the files that you have downloaded, click on '''Open Model/Library File''' tool.
  
I have saved all these files in a folder.  
+
I have saved all the files in a folder.  
  
Select them together and click on '''open'''.  
+
Let me select them together and click on '''open'''.  
  
If they are saved in different folders, please open each file individually.
+
If we have saved these files in different folders, you may open each one of them individually.
  
 
|-
 
|-
 
||  
 
||  
|| Note that the following classes and functions are now open in '''OMEdit''':  
+
|| Note that the following '''classes''' or '''functions''' are now open in '''OMEdit''':  
  
* '''‘polynomialEvaluator’'''
+
* '''bouncingBallWithUserTypes'''
  
* '''‘functionTester’'''
+
* '''functionTester'''
  
* '''‘multiplePolynomialEvaluator’'''
+
* '''multipleFunctionTester'''
  
* '''‘multipleFunctionTester’'''
+
* '''multiplePolynomialEvaluator''' and
  
* '''‘bouncingBallWithUserTypes’'''
+
* '''polynomialEvaluator'''.
  
 
|-
 
|-
 
|| Point to '''Text View''' icon.
 
|| Point to '''Text View''' icon.
|| Right-click on '''‘polynomialEvaluator’''' and select '''‘View Class’'''.  
+
|| 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'''.
 
If the function doesn’t open in '''Text View''', open it in '''Text View'''.
Line 118: Line 118:
  
 
'''function polynomialEvaluator'''
 
'''function polynomialEvaluator'''
|| Name of this function is '''‘polynomialEvaluator’'''
+
|| Name of this '''function''' is '''polynomialEvaluator''' as we have already discussed.
  
 
|-
 
|-
 
|| input '''Real x;'''
 
|| input '''Real x;'''
|| '''‘x’''' is a real variable.  
+
|| '''x''' is a '''real''' variable.  
  
 
|-
 
|-
 
|| '''input''' Real x;
 
|| '''input''' Real x;
|| '''‘input’''' is a keyword used to specify input variables.
+
|| '''input''' is a keyword which is used to specify '''input''' variables.
  
 
|-
 
|-
|| output '''Real fx;'''
+
|| '''output''' Real fx;
|| '''‘fx’''' is a real variable signifying '''f(x)'''
+
|| Similarly, '''output''' is a keyword which is used to specify '''output''' variables.
  
|-
+
'''fx''' is a '''real''' variable which represents '''f(x)'''.  
|| '''output''' Real fx;
+
|| '''‘output’''' is a keyword used to specify output variables.
+
  
 
|-
 
|-
 
|| '''protected Real a = 1''';
 
|| '''protected Real a = 1''';
|| In a function, variables other than input and output are specified using '''‘protected’''' keyword.
+
|| Any variable or parameter which is neither '''input''' nor '''output''' is specified using '''protected''' keyword.  
 
+
'''‘protected’''' variables can only be accessed from within the class.
+
  
 
|-
 
|-
 
|| '''protected parameter Real a = 1;'''
 
|| '''protected parameter Real a = 1;'''
|| '''‘a’''' is a real parameter with a value of 1.
+
|| '''a''' is a '''real''' parameter with a value of '''1'''.
  
 
|-
 
|-
Line 150: Line 146:
  
 
'''protected parameter Real c = 1;'''
 
'''protected parameter Real c = 1;'''
|| The values of '''b''' and '''c''' are as discussed in slides.
+
|| The values of '''a''', '''b''' and '''c''' are as discussed in the slides already.
  
 
|-
 
|-
 
|| '''algorithm'''.
 
|| '''algorithm'''.
|| This indicates the beginning of '''‘algorithm’''' section of function.
+
|| 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.
+
'''Algorithm''' section may contain '''assignment''' statements only.
  
 
|-
 
|-
 
||''' fx := a*x^2 + b*x + c''';
 
||''' fx := a*x^2 + b*x + c''';
|| This sign indicates an assignment.  
+
|| This sign indicates '''assignment'''.  
  
 
|-
 
|-
 
|| '''fx := a*x^2 + b*x + c''';
 
|| '''fx := a*x^2 + b*x + c''';
|| The value of right hand side expression is assigned to left hand side.
+
|| In an '''assignment''' statement the value of right hand side is assigned to the left hand side.
  
 
|-
 
|-
 
|| '''fx := a*x^2 + b*x + c''';
 
|| '''fx := a*x^2 + b*x + c''';
|| Left hand side of an assignment statement is usually unknown. fx is an unknown variable.  
+
|| The left hand side is usually an unknown expression.  
 +
 
 +
In this case '''fx''' is an unknown variable.  
  
 
|-
 
|-
 
|| '''fx := a*x^2 + b*x + c'''<nowiki>;</nowiki>
 
|| '''fx := a*x^2 + b*x + c'''<nowiki>;</nowiki>
|| This expression can be evaluated from the value of '''‘x’'''.
+
|| The value of right hand side expression here can be calculated if the value of '''x''' is known.
  
'''‘x’''' is passed as an argument to the function.
 
  
 
|-
 
|-
|| '''fx := a*x^2 + b*x + c'''<nowiki>;</nowiki>'''
+
|| '''fx := a*x^2 + b*x + c'''<nowiki>;</nowiki>
|| Assignment statements end in a semi-colon.
+
|| '''x''' is usually passed as an '''input''' argument to the '''function''' whenever it is called.
  
 
|-
 
|-
 
||  
 
||  
|| Now, we need a class to test this function.
+
|| Now let us see how to call a '''function''' using '''functionTester''' class.
  
I have created a class named '''‘functionTester’'''.
+
'''functionTester''' icon can already be seen in '''Libraries Browser''' since I have already opened it.
 
+
Since I have opened all code files, '''‘functionTester’''' icon can be seen in '''‘Libraries Browser’'''
+
  
 
|-
 
|-
|| Right-click on '''‘functionTester’''' and select '''‘View Class’'''
+
|| Right-click on '''functionTester''' and select '''View Class'''
|| To open this class, double-click on its icon.
+
|| To open this '''class''', double-click on its icon.
  
This is an alternative way of viewing it.  
+
This is an alternative way of viewing a '''class'''.
 +
 
 +
you may also right click on its icon and select '''View Class'''.
  
 
|-
 
|-
 
|| '''Real z;'''
 
|| '''Real z;'''
|| '''‘z’''' is defined as a real variable.
+
|| '''z''' is a '''real''' variable.
  
 
|-
 
|-
 
|| '''z = polynomialEvaluator(10);'''
 
|| '''z = polynomialEvaluator(10);'''
|| '''‘polynomialEvaluator’''' function is called with an argument of '''10'''.  
+
|| '''polynomialEvaluator''' function is called with an '''input''' argument of '''10''' units and it is equated to '''z'''.
  
 
|-
 
|-
 
|| '''z = polynomialEvaluator(10)'''<nowiki>;</nowiki>
 
|| '''z = polynomialEvaluator(10)'''<nowiki>;</nowiki>
|| The input variable of function that is '''‘x’''' takes the value of 10 units.
+
|| The '''input''' value (variable) of '''polynomialEvaluator''' that is '''x''' takes the value of '''10''' units.
  
 
|-
 
|-
|| Click on '''‘Simulate’''' button.
+
|| Click on '''Simulate''' button.
|| 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 */
 
|| /* Plotting perspective */
  
Click on '''‘z’''' in the '''‘variables browser’'''
+
Click on '''z''' in the '''variables browser'''
|| Click on '''‘z’''' in the '''‘variables browser’'''
+
|| Now let me show you the '''Plotting perspective''' completely by shifting '''OMEdit''' window to the left.
  
Note that '''‘z’''' is the value of function '''‘polynomialEvaluator’''' with '''x = 10'''.  
+
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.
  
 
|-
 
|-
|| Click on '''‘Modeling’''' button at bottom right.
+
|| Let me go back to '''Modeling perspective'''.
|| Click on '''‘Modeling’''' button at bottom right.
+
|| Let me go back to '''Modeling perspective'''.
  
 
|-
 
|-
 
|| Point to '''polynomialEvaluator'''
 
|| Point to '''polynomialEvaluator'''
||'''‘polynomialEvaluator’''' function has only one output.
+
||Click on '''polynomialEvaluator''' tab at the top.
  
 +
Notice that '''polynomialEvaluator''' function has only one '''output''' variable.
  
Let us demonstrate how to return multiple outputs using a function named '''‘multiplePolynomialEvaluator’'''.  
+
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.
|| Double-click '''‘multiplePolynomialEvaluator’'''
+
|| I have already opened this file. Double-click on '''‘multiplePolynomialEvaluator’''' icon.
+
  
 +
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.
 
|-
 
|-
|| '''output Real gx;'''
+
|| Double-click '''multiplePolynomialEvaluator'''
|| This function returns an extra output '''gx'''.
+
|| Double-click on '''multiplePolynomialEvaluator''', '''multipleFunctionTester''' and '''bouncingBallWithUserTypes'''.
  
Other variables remain the same as '''‘polynomialEvaluator’''' function.
+
Shift the window back to its place.
  
|-
+
Go to '''multiplePolynomialEvaluator''' tab.
|| '''output Real fx;'''
+
  
'''output Real gx;'''
+
This '''function''' is similar to '''polynomialEvaluator''' function except for an additional '''output''' variable.
|| The order in which output or input variables are defined is important.  
+
  
It is the order in which function accepts/returns values.
+
|-
 +
|| '''output Real gx;'''
 +
|| An '''output''' variable name '''gx''' has been declared.
  
 
|-
 
|-
 
|| '''gx := a*x^2 - b*x + c;'''  
 
|| '''gx := a*x^2 - b*x + c;'''  
|| '''gx is assigned a value of a x (squared) (minus) b x (plus) 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.
|| Click on '''‘multipleFunctionTester’''' tab
+
|| We shall use '''‘multipleFunctionTester’''' class to test this function.  
+
  
Double-click on '''‘multipleFunctionTester’''' in '''‘Libraries Browser’'''
+
We will understand more about this when we discuss '''multipleFunctionTester''' class.
 +
 
 +
|-
 +
|| Click on '''multipleFunctionTester''' tab
 +
|| Now let me switch to '''multipleFunctionTester''' tab.
  
 
|-
 
|-
Line 263: Line 276:
  
 
'''Real z;'''
 
'''Real z;'''
|| '''‘y’''' and '''‘z’''' are declared as real variables.
+
|| '''y''' and '''z''' are declared as '''real''' variables.
  
 
|-
 
|-
 
|| '''(y,z) = multiplePolynomialEvaluator(10)'''<nowiki>; </nowiki>
 
|| '''(y,z) = multiplePolynomialEvaluator(10)'''<nowiki>; </nowiki>
|| '''‘multiplePolynomialEvaluator’''' is called with an argument of '''10''' units.
+
|| '''multiplePolynomialEvaluator''' function is called with an '''input''' argument of '''10''' units.
  
Hence, the value of '''‘x’''' is '''10'''.
+
This means that the '''input''' variable of '''multiplePolynomialEvaluator''' takes a value of '''10''' units.
  
 
|-
 
|-
 
|| ('''y,z''') = '''multiplePolynomialEvaluator(10)''';  
 
|| ('''y,z''') = '''multiplePolynomialEvaluator(10)''';  
  
|| Notice that '''‘y’''' appears before '''‘z’'''.  
+
|| '''y''' and '''z''' take the values of '''f(x)''' and '''g(x)''' at '''x = 10''' respectively.
  
'''‘y’''' takes the value of '''f(x)''' at '''x = 10'''.  
+
'''y''' takes the value of '''output''' variable '''fx''' since the declaration of '''fx''' appears  before the declaration of '''gx''' in the '''function'''.
  
This is because fx is declared before gx in the function.
+
Now let me simulate this '''class'''.
 
+
 
+
Similarly, '''‘z’''' takes the value of '''g(x)''' at '''x = 10'''.
+
  
 
|-
 
|-
 +
|| Click on '''Simulate''' button.
 
|| Click on '''‘Simulate’''' button.
 
|| Click on '''‘Simulate’''' button.
|| Click on '''‘Simulate’''' button.
+
 
 +
Close the pop up window.
  
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* Plotting perspective */
  
Click on '''‘y’''' and '''‘z’''' in the '''‘variables browser’'''
+
Select '''y''' and '''z''' in the '''variables browser'''
|| Click on '''‘y’''' and '''‘z’''' in the '''‘variables browser’'''.
+
|| Select '''‘y''' and '''z''' in the '''variables browser'''.
  
  
Note that they take values of '''f(x)''' and '''g(x)''' respectively at '''x = 10'''.
+
Note that the values of '''y''' and '''z''' are the same as '''f(x)''' and '''g(x)''' at '''x = 10''' respectively.
  
 
|-
 
|-
|| Click on '''‘Modeling’''' button.
+
|| Delete the result and go back to '''Modeling Perspective'''.
|| Click on '''‘Modeling’''' button at bottom right.
+
|| Delete the result and go back to '''Modeling Perspective'''.
  
 
|-
 
|-
Line 305: Line 317:
 
'''(z,y) = multiplePolynomialEvaluator(10)''';
 
'''(z,y) = multiplePolynomialEvaluator(10)''';
  
|| Now, let me change the order of '''‘y’''' and '''‘z’'''.
+
|| Now, let me interchange the order of '''y''' and '''z'''.
  
  
Delete '''(y,z)'''.
+
Delete '''(y,z)''' and  type '''(z,y)'''.  
  
  
Type '''“(z,y)”''' instead.  
+
and save this class by pressing '''Ctrl+S'''.
  
 +
Simulate this '''class''' once again.
  
Save the model by clicking on '''‘Save’''' button.
+
Close the pop up window.
  
 
|-
 
|-
|| Click on '''‘Simulate’''' button
+
|| /* Plotting perspective */
|| Click on '''‘Simulate’'''.
+
  
|-
+
Select '''y''' and '''z''' once again in the '''variables browser'''
|| /* Plotting perspective */
+
|| Select '''y''' and  '''z''' in the '''variables browser''' once again.
  
Click on '''‘y’''' and '''‘z’''' in the '''‘variables browser’'''
 
|| Click on '''‘y’''' and  '''‘z’''' in the '''‘variables browser’'''.
 
  
 +
Note that the values of '''y''' and '''z''' have been interchanged as compared to the previous case.
  
The values of '''‘y’''' and '''‘z’''' have been interchanged.
+
Let me delete this result and go back to '''Modeling Perspective'''.
  
 
|-
 
|-
Line 337: Line 348:
  
 
'''algorithm'''
 
'''algorithm'''
|| '''algorithm''' is a '''Modelica syntax element'''.
+
|| '''algorithm''' is '''Modelica syntax element''' to enable procedural programming.
  
Only assignment statements are allowed inside algorithm section
+
Only '''assignment''' statements are allowed in '''algorithm''' section.
  
Declaring equations in algorithm sections is not allowed.
+
'''Assignment''' statements use the following symbol.
  
Assignment statements use '''`:='''' instead of '''`=''''
+
Data flows in '''assignment''' statements from right to left.
 
+
Data flows in assignment statements from right to left.
+
 
+
The right hand side expression is assigned to left hand side expression.
+
  
 
|-
 
|-
Line 353: Line 360:
  
 
'''Restrictions on functions'''.
 
'''Restrictions on functions'''.
|| '''‘der()’''' cannot be used inside a function
+
|| There are certain restrictions on functions defined in '''Modelica'''.
  
'''‘time’''' variable cannot be used
+
Use of '''der()''' in a function is invalid.
  
'''‘when’''' statements are not allowed  
+
Use of  '''time''' variable is not allowed.
  
A function cannot have more than one '''‘algorithm’''' section
+
Use of '''when''' statements is not allowed in a function.
  
Models cannot be passed as arguments to a function
+
A '''function''' may not have more than one '''algorithm''' section and
 +
 
 +
'''Models''' cannot be passed as '''arguments'''.
  
 
|-
 
|-
Line 367: Line 376:
  
 
'''type'''
 
'''type'''
|| '''‘type’''' is a specialized class to define custom data-types
+
|| '''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'''.
+
For example, physical quantities like '''velocity''' and '''current''' can be defined as '''data-types'''.
  
They can be used to declare variables later.
+
They can be used later to declare other variables.
  
 +
'''Attributes''' of Modelica data-types like '''unit''', and '''start''' can be changed accordingly.
  
Attributes like '''‘unit’, ‘start’''' etc can be changed to suit needs.
+
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'''
+
|| Let me go back to '''OMEdit''' to demonstrate this '''model'''.
 
+
|-
+
|| Point to '''‘bouncingBallWithUserTypes’''' model.
+
|| '''‘bouncingBallWithUserTypes’''' model will be used to demonstrate type definitions.  
+
  
 
|-
 
|-
 
|| /* bouncingBallWithUserTypes */
 
|| /* bouncingBallWithUserTypes */
|| Click on '''‘bouncingBallWithUserTypes’''' tab.
+
|| Click on '''bouncingBallWithUserTypes''' tab.
  
 
|-
 
|-
 
||  
 
||  
|| This model is the same as '''‘bouncingBall’''' model except for user-defined types.
+
|| 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");'''
 
|| '''type Length = Real(unit="m");'''
|| '''‘Length’''' type is defined as Real. Its unit is modified to '''‘m’'''.
+
|| '''Length''' is defined as '''Real''' datatype. Its unit is modified to '''m'''.
  
 
|-
 
|-
 
|| '''type Velocity = Real(unit="m/s");'''
 
|| '''type Velocity = Real(unit="m/s");'''
|| '''‘Velocity’''' type is defined as Real.  
+
|| Similarly '''Velocity''' is defined as '''Real'''.  
  
Its unit is modified to '''‘m/s’'''
+
Its unit is modified to '''m/s'''
  
 
|-
 
|-
 
|| '''Length h;'''
 
|| '''Length h;'''
|| '''‘h’''' is defined to be of the type '''‘Length’'''.  
+
|| '''h''' represents height of the ball from surface of earth.
  
Therefore, the unit of '''‘h’''' is '''‘m’'''.
+
It is defined as '''length datatype'''.
  
 
|-
 
|-
 
|| '''Velocity v;'''  
 
|| '''Velocity v;'''  
|| Velocity of the ball is defined to be of type '''‘Velocity’'''.
+
|| Similarly '''v''' represents velocity of ball.
 +
 
 +
It is declared to be of '''velocity datatype'''.
  
 
|-
 
|-
 
||  
 
||  
|| Other variable declarations and equations remain the same as in '''‘bouncingBall’''' model.
+
|| Remaining variable declarations and equations of this '''model''' are similar to '''bouncingBall''' model.
  
|-
+
Now let me '''simulate''' this.
|| Click on '''‘Simulate’''' button
+
 
|| Click on '''‘Simulate’''' button in the toolbar.
+
Close the pop up window.
  
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* 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'''.
  
Click on '''‘h’''' in '''‘variables browser’'''
 
|| The model has been simulated.
 
 
Let me scroll right in '''‘Variables Browser’'''.
 
 
 
Note that '''‘h’''' and '''‘v’''' have their respective units displayed in the '''‘variables browser’'''.
 
 
 
Click on '''‘h’'''.
 
  
 +
The plot of '''h''' versus '''time''' is similar to that of '''bouncingBall''' model.
  
The plot is as expected.
+
Let me de-select '''h'''.
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides
+
|| Now let me go back to the slides.
  
 
|-
 
|-
Line 447: Line 454:
  
 
'''Assignment'''.
 
'''Assignment'''.
|| Breach the restrictions on functions. Observe the errors produced as a result.
+
|| As an assignment breach the restrictions on functions and observe the errors produced.
  
 
|-
 
|-
 
||  
 
||  
|| This brings us to the end of this tutorial
+
|| This brings us to the end of this tutorial.
  
 
|-
 
|-
 
|| Slide:
 
|| Slide:
  
About the Spoken Tutorial project
+
About the '''Spoken Tutorial project'''
|| Watch the video available at the link shown below
+
|| Watch the video available at following link.
  
 
'''http://spoken-tutorial.org/ http://spoken-tutorial.org] /What\_is\_a\_Spoken\_Tutorial'''
 
'''http://spoken-tutorial.org/ http://spoken-tutorial.org] /What\_is\_a\_Spoken\_Tutorial'''
  
It summarises the Spoken Tutorial project
+
It summarises the '''Spoken Tutorial project'''
 
+
If you do not have good bandwidth, you can download and watch it.
+
  
 
|-
 
|-
Line 470: Line 475:
 
'''Spoken Tutorial''' Workshops
 
'''Spoken Tutorial''' Workshops
 
|| We conducts workshops using spoken tutorials
 
|| We conducts workshops using spoken tutorials
 
Give certificates
 
  
 
Please contact us.
 
Please contact us.
Line 478: Line 481:
 
|| Slide:
 
|| Slide:
  
'''Forum'''
+
'''Forum to answer questions'''
|| If you have questions in this tutorial, please visit the webpage mentioned.
+
|| If you have questions in this spoken tutorial, please visit the website.
  
 
|-
 
|-
Line 485: Line 488:
  
 
'''Textbook Companion Project'''
 
'''Textbook Companion Project'''
|| We coordinate coding of solved examples from popular books.
+
|| We coordinate coding of solved examples of popular books.
  
We give honorarium to contributors. Please visit our website.
+
Please visit the websites for more information.
  
 
|-
 
|-
Line 493: Line 496:
  
 
'''Lab Migration Project'''
 
'''Lab Migration Project'''
|| We help migrate labs from commercial simulators to OpenModelica.  
+
|| We help migrate commercial simulator labs to '''OpenModelica'''.  
  
 
|-
 
|-
Line 499: Line 502:
  
 
'''Acknowledgements'''
 
'''Acknowledgements'''
|| '''Spoken Tutorial Project''' is supported by NMEICT, MHRD, Government of India.
+
|| '''Spoken Tutorial Project''' is supported by '''NMEICT, MHRD''', Government of India.
  
 
|-
 
|-
Line 505: Line 508:
  
 
'''Thanks'''
 
'''Thanks'''
|| We thank '''OpenModelica''' development team for their support.
+
|| We thank the development team of '''OpenModelica''' for their support.
  
|-
+
Thank you.
||
+
|| Thank you for joining me in this tutorial. Goodbye!
+
|-
+
|}
+

Latest revision as of 12:35, 15 March 2016

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 to answer questions

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