Difference between revisions of "OpenModelica/C2/Functions-and-Types/English"
(4 intermediate revisions by the same user not shown) | |||
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 and Ubuntu operating system version 14.04''' | + | * '''OpenModelica 1.9.2''' and '''Ubuntu operating system version 14.04''' |
− | * But, this process is identical in Windows, '''Mac OS X''' or '''FOSSEE OS. | + | * But, this process is identical in '''Windows''', '''Mac OS X''' or '''FOSSEE OS'''. |
|- | |- | ||
Line 34: | Line 34: | ||
'''Pre-requisites''' | '''Pre-requisites''' | ||
− | ||To understand this tutorial You need to know how to define a class 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. please go through them. | + | * Pre-requisite tutorials are mentioned on our website. |
+ | |||
+ | * please go through them. | ||
|- | |- | ||
Line 44: | Line 46: | ||
'''function''' | '''function''' | ||
− | || Let us discuss a function now. | + | || Let us discuss a '''function''' now. |
− | ''' | + | '''function''' is a specialized class that can take input and return output. |
− | It contains ''' | + | 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 56: | 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 62: | Line 64: | ||
'''Problem Statement''' | '''Problem Statement''' | ||
− | || Now let us write a | + | || 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 */ | || /* OMEdit */ | ||
|| '''polynomialEvaluator''' file is available on our website. | || '''polynomialEvaluator''' file is available on our website. | ||
− | Please download and save all the files available in Code Files link. | + | Please download and save all the files available in '''Code Files''' link. |
− | To demonstrate ''' | + | To demonstrate '''polynomialEvaluator''' function, let me go to '''OMEdit'''. |
− | '''OMEdit''' is now open in ''' | + | '''OMEdit''' is now open in '''Welcome''' perspective. |
You are only seeing a part of the '''OMEdit''' window since I have zoomed in. | You are only seeing a part of the '''OMEdit''' window since I have zoomed in. | ||
Line 94: | Line 96: | ||
|- | |- | ||
|| | || | ||
− | || Note that the following classes or functions are now open in '''OMEdit''': | + | || Note that the following '''classes''' or '''functions''' are now open in '''OMEdit''': |
− | * ''' | + | * '''bouncingBallWithUserTypes''' |
− | * ''' | + | * '''functionTester''' |
− | * ''' | + | * '''multipleFunctionTester''' |
− | * ''' | + | * '''multiplePolynomialEvaluator''' and |
− | * ''' | + | * '''polynomialEvaluator'''. |
|- | |- | ||
|| Point to '''Text View''' icon. | || Point to '''Text View''' icon. | ||
− | || To open ''' | + | || 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 116: | Line 118: | ||
'''function polynomialEvaluator''' | '''function polynomialEvaluator''' | ||
− | || Name of this function is ''' | + | || Name of this '''function''' is '''polynomialEvaluator''' as we have already discussed. |
|- | |- | ||
|| input '''Real x;''' | || input '''Real x;''' | ||
− | || ''' | + | || '''x''' is a '''real''' variable. |
|- | |- | ||
|| '''input''' Real x; | || '''input''' Real x; | ||
− | || ''' | + | || '''input''' is a keyword which is used to specify '''input''' variables. |
|- | |- | ||
|| '''output''' Real fx; | || '''output''' Real fx; | ||
− | || Similarly, ''' | + | || Similarly, '''output''' is a keyword which is used to specify '''output''' variables. |
− | fx is a real variable which represents f(x). | + | '''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 ''' | + | || Any variable or parameter which is neither '''input''' nor '''output''' is specified using '''protected''' keyword. |
|- | |- | ||
|| '''protected parameter Real a = 1;''' | || '''protected parameter Real a = 1;''' | ||
− | || ''' | + | || '''a''' is a '''real''' parameter with a value of '''1'''. |
|- | |- | ||
Line 148: | Line 150: | ||
|- | |- | ||
|| '''algorithm'''. | || '''algorithm'''. | ||
− | || Please note that '''a''', '''b''' and '''c''' are protected parameters. | + | || Please note that '''a''', '''b''' and '''c''' are '''protected''' parameters. |
− | '''Algorithm''' represents the beginning of ''' | + | '''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 assignment. | + | || 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. | + | || 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. | + | || 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. | + | || 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. | + | || '''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. | + | || Now let us see how to call a '''function''' using '''functionTester''' class. |
− | '''functionTester''' icon can already be seen in ''' | + | '''functionTester''' icon can already be seen in '''Libraries Browser''' since I have already opened it. |
|- | |- | ||
− | || Right-click on ''' | + | || 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 a class. | + | This is an alternative way of viewing a '''class'''. |
you may also right click on its icon and select '''View Class'''. | you may also right click on its icon and select '''View Class'''. | ||
Line 191: | Line 195: | ||
|- | |- | ||
|| '''Real z;''' | || '''Real z;''' | ||
− | || ''' | + | || '''z''' is a '''real''' variable. |
|- | |- | ||
|| '''z = polynomialEvaluator(10);''' | || '''z = polynomialEvaluator(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 polynomialEvaluator that is ''' | + | || The '''input''' value (variable) of '''polynomialEvaluator''' that is '''x''' takes the value of '''10''' units. |
|- | |- | ||
− | || Click on ''' | + | || Click on '''Simulate''' button. |
− | || Now let us simulate this class. | + | || 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 ''' | + | Click on '''z''' in the '''variables browser''' |
− | || Now let me show you the Plotting perspective completely by shifting OMEdit window to the left. | + | || Now let me show you the '''Plotting perspective''' completely by shifting '''OMEdit''' window to the left. |
− | Select ''' | + | Select '''z''' in the '''variables browser''' |
− | Note that the value of ''' | + | 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. | + | Now let me de-select '''z''' and delete this result. |
|- | |- | ||
− | || Let me go back to Modeling perspective. | + | || Let me go back to '''Modeling perspective'''. |
− | || 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. | + | ||Click on '''polynomialEvaluator''' tab at the top. |
− | Notice that ''' | + | 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. | |
− | + | ||
− | Since the | + | |
|- | |- | ||
− | || Double-click ''' | + | || Double-click '''multiplePolynomialEvaluator''' |
− | || Double-click on ''' | + | || Double-click on '''multiplePolynomialEvaluator''', '''multipleFunctionTester''' and '''bouncingBallWithUserTypes'''. |
Shift the window back to its place. | Shift the window back to its place. | ||
− | Go to multiplePolynomialEvaluator tab. | + | Go to '''multiplePolynomialEvaluator''' tab. |
− | This function is similar to ''' | + | This '''function''' is similar to '''polynomialEvaluator''' function except for an additional '''output''' variable. |
|- | |- | ||
|| '''output Real gx;''' | || '''output Real gx;''' | ||
− | || An output variable name gx has been declared. | + | || 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 the 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. | + | The order in which '''output''' or '''input''' variables are declared is important. |
− | We will understand more about this when we discuss multipleFunctionTester class. | + | We will understand more about this when we discuss '''multipleFunctionTester''' class. |
|- | |- | ||
− | || Click on ''' | + | || Click on '''multipleFunctionTester''' tab |
− | || Now let me switch to ''' | + | || Now let me switch to '''multipleFunctionTester''' tab. |
|- | |- | ||
Line 270: | Line 276: | ||
'''Real z;''' | '''Real z;''' | ||
− | || ''' | + | || '''y''' and '''z''' are declared as '''real''' variables. |
|- | |- | ||
|| '''(y,z) = multiplePolynomialEvaluator(10)'''<nowiki>; </nowiki> | || '''(y,z) = multiplePolynomialEvaluator(10)'''<nowiki>; </nowiki> | ||
− | || ''' | + | || '''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. | + | 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''' 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 | + | '''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. | + | Now let me simulate this '''class'''. |
|- | |- | ||
− | || Click on ''' | + | || Click on '''Simulate''' button. |
|| Click on '''‘Simulate’''' button. | || Click on '''‘Simulate’''' button. | ||
Line 296: | Line 302: | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | Select ''' | + | Select '''y''' and '''z''' in the '''variables browser''' |
− | || Select ''' | + | || 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. | + | 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'''. |
− | || Delete the result and go back to Modeling Perspective. | + | || Delete the result and go back to '''Modeling Perspective'''. |
|- | |- | ||
Line 311: | Line 317: | ||
'''(z,y) = multiplePolynomialEvaluator(10)'''; | '''(z,y) = multiplePolynomialEvaluator(10)'''; | ||
− | || Now, let me | + | || Now, let me interchange the order of '''y''' and '''z'''. |
− | + | ||
− | + | ||
− | + | ||
− | type ''' | + | Delete '''(y,z)''' and type '''(z,y)'''. |
and save this class by pressing '''Ctrl+S'''. | and save this class by pressing '''Ctrl+S'''. | ||
− | Simulate this class once again. | + | Simulate this '''class''' once again. |
Close the pop up window. | Close the pop up window. | ||
Line 329: | Line 332: | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | Select ''' | + | Select '''y''' and '''z''' once again in the '''variables browser''' |
− | || Select ''' | + | || Select '''y''' and '''z''' in the '''variables browser''' once again. |
− | Note that the values of ''' | + | 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 delete this result and go back to '''Modeling Perspective'''. |
|- | |- | ||
Line 347: | Line 350: | ||
|| '''algorithm''' is '''Modelica syntax element''' to enable procedural programming. | || '''algorithm''' is '''Modelica syntax element''' to enable procedural programming. | ||
− | Only assignment statements are allowed in algorithm section. | + | Only '''assignment''' statements are allowed in '''algorithm''' section. |
− | Assignment statements use the following symbol. | + | '''Assignment''' statements use the following symbol. |
− | Data flows in assignment statements from right to left. | + | Data flows in '''assignment''' statements from right to left. |
|- | |- | ||
Line 357: | Line 360: | ||
'''Restrictions on functions'''. | '''Restrictions on functions'''. | ||
− | || There are certain restrictions on functions defined in Modelica. | + | || There are certain restrictions on functions defined in '''Modelica'''. |
− | Use of ''' | + | Use of '''der()''' in a function is invalid. |
− | Use of ''' | + | Use of '''time''' variable is not allowed. |
− | Use of ''' | + | Use of '''when''' statements is not allowed in a function. |
− | A function may not have more than one ''' | + | A '''function''' may not have more than one '''algorithm''' section and |
− | Models cannot be passed as arguments. | + | '''Models''' cannot be passed as '''arguments'''. |
|- | |- | ||
Line 373: | Line 376: | ||
'''type''' | '''type''' | ||
− | || ''' | + | || '''type''' is a specialized class to define custom '''data-types''' in '''Modelica'''. |
− | For example, physical quantities like ''' | + | For example, physical quantities like '''velocity''' and '''current''' can be defined as '''data-types'''. |
They can be used later to declare other variables. | 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. | + | I have created a '''model''' named '''bouncingBallWithUserTypes''' to simulate type definitions. |
|- | |- | ||
|| | || | ||
− | || Let me go back to '''OMEdit''' to demonstrate this model. | + | || Let me go back to '''OMEdit''' to demonstrate this '''model'''. |
|- | |- | ||
|| /* bouncingBallWithUserTypes */ | || /* bouncingBallWithUserTypes */ | ||
− | || Click on ''' | + | || Click on '''bouncingBallWithUserTypes''' tab. |
|- | |- | ||
|| | || | ||
− | || This model is similar to 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. | + | Please go through to the per-requisite tutorials to understand '''bouncingBall''' model. |
|- | |- | ||
|| '''type Length = Real(unit="m");''' | || '''type Length = Real(unit="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");''' | ||
− | || Similarly ''' | + | || Similarly '''Velocity''' is defined as '''Real'''. |
− | Its unit is modified to ''' | + | Its unit is modified to '''m/s''' |
|- | |- | ||
|| '''Length h;''' | || '''Length h;''' | ||
− | || ''' | + | || '''h''' represents height of the ball from surface of earth. |
− | It is defined as length datatype. | + | It is defined as '''length datatype'''. |
|- | |- | ||
|| '''Velocity v;''' | || '''Velocity v;''' | ||
− | || Similarly v represents velocity of ball. | + | || Similarly '''v''' represents velocity of ball. |
− | It is declared to be of velocity datatype. | + | It is declared to be of '''velocity datatype'''. |
|- | |- | ||
|| | || | ||
− | || Remaining variable declarations and equations of this model are similar to ''' | + | || Remaining variable declarations and equations of this '''model''' are similar to '''bouncingBall''' model. |
− | Now let me simulate this. | + | Now let me '''simulate''' this. |
Close the pop up window. | Close the pop up window. | ||
Line 433: | Line 437: | ||
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | + | || In the '''Variables Browser''' notice that '''h''' and '''v''' have their respective units associated with their data-types. | |
− | || In the ''' | + | |
+ | 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'''. | |
− | + | ||
− | + | ||
|- | |- | ||
Line 461: | Line 463: | ||
|| Slide: | || Slide: | ||
− | About the Spoken Tutorial project | + | About the '''Spoken Tutorial project''' |
|| Watch the video available at following link. | || 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 479: | Line 481: | ||
|| Slide: | || Slide: | ||
− | '''Forum''' | + | '''Forum to answer questions''' |
|| If you have questions in this spoken tutorial, please visit the website. | || If you have questions in this spoken tutorial, please visit the website. | ||
Line 494: | Line 496: | ||
'''Lab Migration Project''' | '''Lab Migration Project''' | ||
− | || We help migrate commercial simulator labs to OpenModelica. | + | || We help migrate commercial simulator labs to '''OpenModelica'''. |
|- | |- | ||
Line 500: | 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 508: | Line 510: | ||
|| We thank the development team of '''OpenModelica''' for their support. | || We thank the development team of '''OpenModelica''' for their support. | ||
− | + | Thank you. | |
− | + | ||
− | + | ||
− | + | ||
− | + |
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:
|
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.
|
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 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.
|
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.
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. 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.
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. |