Difference between revisions of "OpenModelica/C2/Functions-and-Types/English"
Line 6: | Line 6: | ||
'''Title Slide''' | '''Title Slide''' | ||
− | || Welcome to the spoken tutorial on '''Functions and Types''' | + | || Welcome to the spoken tutorial on '''Functions and Types'''. |
|- | |- | ||
Line 26: | Line 26: | ||
|| To record this tutorial, I am using | || To record this tutorial, I am using | ||
− | * '''OpenModelica 1.9.2 | + | * '''OpenModelica 1.9.2 and Ubuntu operating system version 14.04''' |
− | * But, this process is identical in Windows, '''Mac OS''' | + | * 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 | + | ||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 44: | ||
'''function''' | '''function''' | ||
− | || | + | || 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 | ||
− | + | It contains '''‘algorithm’''' section. | |
− | + | A '''function''' cannot contain equations and it cannot be simulated. | |
− | + | ||
− | A '''function''' cannot contain equations and it cannot be simulated | + | |
|- | |- | ||
Line 64: | Line 62: | ||
'''Problem Statement''' | '''Problem Statement''' | ||
− | || | + | || Now let us write a function named '''‘polynomialEvaluator’''' which |
− | * takes '''‘x’''' as input | + | * takes '''‘x’''' as input and |
− | * returns '''‘f(x) = a x (squared) (plus) b x (plus) c’''' as output | + | * returns '''‘f(x) = a x (squared) (plus) b x (plus) c’''' where a=1, b=2 and c=1 as output. |
|- | |- | ||
|| /* OMEdit */ | || /* OMEdit */ | ||
− | || | + | || '''polynomialEvaluator''' file is available on our website. |
− | Please download all files | + | 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'''. | ||
Line 80: | Line 78: | ||
'''OMEdit''' is now open in '''‘Welcome’''' perspective. | '''OMEdit''' is now open in '''‘Welcome’''' perspective. | ||
− | You are | + | You are only seeing a part of the '''OMEdit''' window since I have zoomed in. |
− | I shall | + | 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 | + | || To open the files that you have downloaded, click on '''Open Model/Library File''' tool. |
− | I have saved all | + | I have saved all the files in a folder. |
− | + | Let me select them together and click on '''open'''. | |
− | If | + | If we have saved these files in different folders, you may open each one of them individually. |
|- | |- | ||
|| | || | ||
− | || Note that the following classes | + | || Note that the following classes or functions are now open in '''OMEdit''': |
− | * ''' | + | * '''‘bouncingBallWithUserTypes’''' |
* '''‘functionTester’''' | * '''‘functionTester’''' | ||
− | |||
− | |||
* '''‘multipleFunctionTester’''' | * '''‘multipleFunctionTester’''' | ||
− | * ''' | + | * '''‘multiplePolynomialEvaluator’''' and |
+ | |||
+ | * '''‘polynomialEvaluator’''' | ||
|- | |- | ||
|| Point to '''Text View''' icon. | || 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'''. | If the function doesn’t open in '''Text View''', open it in '''Text View'''. | ||
Line 118: | Line 116: | ||
'''function polynomialEvaluator''' | '''function polynomialEvaluator''' | ||
− | || Name of this function is '''‘polynomialEvaluator’''' | + | || Name of this function is '''‘polynomialEvaluator’''' as we have already discussed. |
|- | |- | ||
Line 126: | Line 124: | ||
|- | |- | ||
|| '''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; | ||
− | || '''‘output’''' is a keyword used to specify output variables. | + | || 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'''; | || '''protected Real a = 1'''; | ||
− | || | + | || Any variable or parameter which is neither input nor output is specified using '''‘protected’''' keyword. |
− | + | ||
− | + | ||
|- | |- | ||
Line 150: | Line 144: | ||
'''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'''. | ||
− | || | + | || 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. | ||
Line 160: | Line 156: | ||
|- | |- | ||
||''' fx := a*x^2 + b*x + c'''; | ||''' fx := a*x^2 + b*x + c'''; | ||
− | || This sign indicates | + | || This sign indicates assignment. |
|- | |- | ||
|| '''fx := a*x^2 + b*x + c'''; | || '''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'''; | || '''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'''<nowiki>;</nowiki> | || '''fx := a*x^2 + b*x + c'''<nowiki>;</nowiki> | ||
− | || | + | || 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'''<nowiki>;</nowiki> | + | || '''fx := a*x^2 + b*x + c'''<nowiki>;</nowiki> |
− | || | + | || x is usually passed as an input argument to the function whenever it is called. |
|- | |- | ||
|| | || | ||
− | || Now | + | || 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. | |
− | + | ||
− | + | ||
|- | |- | ||
Line 192: | Line 185: | ||
|| 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 | + | 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 | + | || '''‘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 | + | || The input variable of polynomialEvaluator that is '''‘x’''' takes the value of 10 units. |
|- | |- | ||
|| 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. | ||
|- | |- | ||
Line 214: | Line 212: | ||
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. |
− | + | 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''' | || 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 := a*x^2 - b*x + c;''' | ||
− | || '''gx is assigned | + | || '''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 | || Click on '''‘multipleFunctionTester’''' tab | ||
− | || | + | || Now let me switch to '''‘multipleFunctionTester’''' tab. |
− | + | ||
− | + | ||
|- | |- | ||
Line 267: | Line 274: | ||
|- | |- | ||
|| '''(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. |
− | + | This means that the input variable of multiplePolynomialEvaluator takes a value of '''10''' units. | |
|- | |- | ||
|| ('''y,z''') = '''multiplePolynomialEvaluator(10)'''; | || ('''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 | |
− | + | Now let me simulate this class. | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|| 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 */ | ||
− | + | Select '''‘y’''' and '''‘z’''' in the '''‘variables browser’''' | |
− | || | + | || Select '''‘y’''' and '''‘z’''' in the '''‘variables browser’'''. |
− | Note that | + | 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. |
|- | |- | ||
Line 305: | Line 311: | ||
'''(z,y) = multiplePolynomialEvaluator(10)'''; | '''(z,y) = multiplePolynomialEvaluator(10)'''; | ||
− | || Now, let me change the order of '''‘y’''' and '''‘z’'''. | + | || Now, let me inter change the order of '''‘y’''' and '''‘z’'''. |
− | Delete '''‘(y,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 */ | || /* 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. | ||
|- | |- | ||
Line 337: | Line 345: | ||
'''algorithm''' | '''algorithm''' | ||
− | || '''algorithm''' is | + | || '''algorithm''' is '''Modelica syntax element''' to enable procedural programming. |
− | Only assignment statements are allowed | + | Only assignment statements are allowed in algorithm section. |
− | + | Assignment statements use the following symbol. | |
− | + | ||
− | Assignment statements use | + | |
Data flows in assignment statements from right to left. | Data flows in assignment statements from right to left. | ||
− | |||
− | |||
|- | |- | ||
Line 353: | Line 357: | ||
'''Restrictions on functions'''. | '''Restrictions on functions'''. | ||
− | || '''‘der()’''' | + | || There are certain restrictions on functions defined in Modelica. |
+ | |||
+ | Use of '''‘der()’''' in a function is invalid. | ||
− | '''‘time’''' variable | + | Use of ''''‘time’''' variable is not allowed. |
− | '''‘when’''' statements | + | Use of '''‘when’''' statements is not allowed in a function. |
− | A function | + | A function may not have more than one '''‘algorithm’''' section and |
− | Models cannot be passed as arguments | + | Models cannot be passed as arguments. |
|- | |- | ||
Line 367: | Line 373: | ||
'''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 | + | They can be used later to declare other variables. |
− | Attributes like ''' | + | 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''' | + | || Let me go back to '''OMEdit''' to demonstrate this model. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 391: | Line 397: | ||
|- | |- | ||
|| | || | ||
− | || This model is the | + | || 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’''' | + | || '''‘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’''' | + | || Similarly '''‘Velocity’''' is defined as Real. |
Its unit is modified to '''‘m/s’''' | Its unit is modified to '''‘m/s’''' | ||
Line 405: | Line 413: | ||
|- | |- | ||
|| '''Length h;''' | || '''Length h;''' | ||
− | || '''‘h’''' | + | || '''‘h’''' represents height of the ball from surface of earth. |
− | + | It is defined as length datatype. | |
|- | |- | ||
|| '''Velocity v;''' | || '''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 */ | || /* Plotting perspective */ | ||
+ | || Click on '''‘h’''' in '''‘variables browser’''' | ||
+ | || 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. |
|- | |- | ||
Line 447: | Line 452: | ||
'''Assignment'''. | '''Assignment'''. | ||
− | || | + | || 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. |
|- | |- | ||
Line 457: | Line 462: | ||
About the Spoken Tutorial project | About the Spoken Tutorial project | ||
− | || Watch the video available at | + | || 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 | ||
− | |||
− | |||
|- | |- | ||
Line 470: | Line 473: | ||
'''Spoken Tutorial''' Workshops | '''Spoken Tutorial''' Workshops | ||
|| We conducts workshops using spoken tutorials | || We conducts workshops using spoken tutorials | ||
− | |||
− | |||
Please contact us. | Please contact us. | ||
Line 479: | Line 480: | ||
'''Forum''' | '''Forum''' | ||
− | || If you have questions in this tutorial, please visit the | + | || If you have questions in this spoken tutorial, please visit the website. |
|- | |- | ||
Line 485: | Line 486: | ||
'''Textbook Companion Project''' | '''Textbook Companion Project''' | ||
− | || We coordinate coding of solved examples | + | || We coordinate coding of solved examples of popular books. |
− | + | Please visit the websites for more information. | |
|- | |- | ||
Line 493: | Line 494: | ||
'''Lab Migration Project''' | '''Lab Migration Project''' | ||
− | || We help migrate | + | || We help migrate commercial simulator labs to OpenModelica. |
|- | |- | ||
Line 505: | Line 506: | ||
'''Thanks''' | '''Thanks''' | ||
− | || We thank '''OpenModelica''' | + | || We thank the development team of '''OpenModelica''' for their support. |
|- | |- | ||
|| | || | ||
− | || Thank you | + | || Thank you. |
|- | |- | ||
|} | |} |
Revision as of 16:49, 3 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:
| |
Slide:
System Requirements |
To record this tutorial, I am using
| |
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
| |
/* 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:
| ||
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 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.
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 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’.
| |
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 inter change the order of ‘y’ and ‘z’.
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.
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.
They can be used later to declare other variables.
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 */ | Click on ‘h’ in ‘variables browser’ | In the ‘Variables Browser’ notice that ‘h’ and ‘v’ have their respective units associated with their data-types.
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. |