OpenModelica/C2/Functions-and-Types/English-timed
From Script | Spoken-Tutorial
| Time | Narration |
| 00:01 | Welcome to the spoken tutorial on Functions and Types. |
| 00:06 | In this tutorial, we are going to learn:
how to define a function, |
| 00:12 | how to use algorithm,
how to define a type. |
| 00:17 | To record this tutorial, I am using OpenModelica 1.9.2 and Ubuntu operating system version 14.04 |
| 00:27 | But this process is identical in Windows, Mac OS X or FOSSEE OS. |
| 00:35 | To understand this tutorial, you need to know how to define a class in Modelica. |
| 00:41 | You need knowledge of functions in any programming language. |
| 00:46 | Pre-requisite tutorials are mentioned on our website. please go through them. |
| 00:52 | Let us discuss a function now. |
| 00:55 | function is a specialized class that can take input and return output. |
| 01:01 | It contains algorithm section. |
| 01:04 | A function cannot contain equations and it cannot be simulated. |
| 01:10 | The syntax of a function is as shown. |
| 01:15 | 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. |
| 01:36 | polynomialEvaluator file is available on our website. |
| 01:40 | Please download and save all the files available in Code Files link. |
| 01:46 | To demonstrate polynomialEvaluator function, let me go to OMEdit. |
| 01:52 | OMEdit is now open in Welcome perspective. |
| 01:56 | You are only seeing a part of the OMEdit window since I have zoomed in. |
| 02:02 | I shall show you the relevant portions by shifting the window whenever necessary. |
| 02:09 | To open the files that you have downloaded, click on Open Model/Library File tool. |
| 02:16 | I have saved all the files in a folder. |
| 02:19 | Let me select them together and click on open. |
| 02:24 | If we have saved these files in different folders, you may open each one of them individually. |
| 02:31 | Note that the following classes or functions are now open in OMEdit: bouncingBallWithUserTypes , functionTester ,multipleFunctionTester, multiplePolynomialEvaluator and polynomialEvaluator. |
| 02:51 | To open polynomialEvaluator function and view it, Right-click on the icon in Libraries Browser and select View Class. |
| 03:02 | If the function doesn’t open in Text View, open it in Text View. |
| 03:08 | Name of this function is polynomialEvaluator as we have already discussed. |
| 03:14 | x is a real variable. |
| 03:17 | input is a keyword which is used to specify input variables. |
| 03:22 | Similarly, output is a keyword which is used to specify output variables. |
| 03:28 | fx is a real variable which represents f(x). |
| 03:33 | Any variable or parameter which is neither input nor output is specified using protected keyword. |
| 03:42 | a is a real parameter with a value of 1. |
| 03:47 | The values of a, b and c are as discussed in the slides already. |
| 03:53 | Please note that a, b and c are protected parameters. |
| 03:59 | Algorithm represents the beginning of algorithm section of a function. |
| 04:05 | Algorithm section may contain assignment statements only. |
| 04:10 | This sign indicates assignment. |
| 04:14 | In an assignment statement, the value of right hand side is assigned to the left hand side. |
| 04:20 | The left hand side is usually an unknown expression. |
| 04:25 | In this case, fx is an unknown variable. |
| 04:29 | The value of right hand side expression here can be calculated if the value of x is known. |
| 04:36 | x is usually passed as an input argument to the function whenever it is called. |
| 04:43 | Now let us see how to call a function using functionTester class. |
| 04:49 | functionTester icon can already be seen in Libraries Browser since I have already opened it. |
| 04:56 | To open this class, double-click on its icon. |
| 05:01 | This is an alternative way of viewing a class. |
| 05:05 | you may also right click on its icon and select View Class. |
| 05:11 | z is a real variable. |
| 05:14 | polynomialEvaluator function is called with an input argument of 10 units and it is equated to z. |
| 05:23 | The input value (variable) of polynomialEvaluator that is x takes the value of 10 units. |
| 05:31 | Now, let us simulate this class. |
| 05:34 | To simulate this class, you may right click on functionTester icon in Libraries Browser and select Simulate. |
| 05:45 | The class has now simulated. |
| 05:47 | You may also use the Simulate button in toolbar to simulate a class. |
| 05:53 | Now let me show you the Plotting perspective completely by shifting OMEdit window to the left. |
| 06:00 | Select z in the variables browser |
| 06:04 | Note that the value of z is equal to the value of f(x) at x = 10. |
| 06:12 | Now let me de-select z and delete this result. |
| 06:18 | Let me go back to Modeling perspective. |
| 06:21 | Click on polynomialEvaluator tab at the top. |
| 06:25 | Notice that polynomialEvaluator function has only one output variable. |
| 06:31 | Now let me show you how to output two or more variables using a function. |
| 06:38 | I have created a function named multiplePolynomialEvaluator which has two output variables. |
| 06:45 | Before viewing that function, let me close the tabs of PolynomialEvaluator and FunctionTester. |
| 06:54 | Since the Libraries Browser is not visible, let me shift the window to the right. |
| 07:01 | Double-click on multiplePolynomialEvaluator, multipleFunctionTester and bouncingBallWithUserTypes. |
| 07:11 | Shift the window back to its place. |
| 07:15 | Go to multiplePolynomialEvaluator tab. |
| 07:19 | This function is similar to polynomialEvaluator function except for an additional output variable. |
| 07:27 | An output variable name gx has been declared. |
| 07:32 | gx is assigned the value of a x (squared) (minus) b x (plus) c |
| 07:38 | The order in which output or input variables are declared is important. |
| 07:45 | We will understand more about this when we discuss multipleFunctionTester class. |
| 07:51 | Now, let me switch to multipleFunctionTester tab. |
| 07:56 | y and z are declared as real variables. |
| 08:01 | multiplePolynomialEvaluator function is called with an input argument of 10 units. |
| 08:08 | This means that the input variable of multiplePolynomialEvaluator takes a value of 10 units. |
| 08:17 | y and z take the values of f(x) and g(x) at x = 10 respectively. |
| 08:26 | y takes the value of output variable fx since the declaration of fx appears before the declaration of gx in the function. |
| 08:37 | Now let me simulate this class. |
| 08:40 | Click on ‘Simulate’ button. Close the pop up window. |
| 08:46 | Select y and z in the variables browser. |
| 08:51 | Note that the values of y and z are the same as f(x) and g(x) at x = 10 respectively. |
| 09:01 | Delete the result and go back to Modeling Perspective. |
| 09:06 | Now, let me interchange the order of y and z. |
| 09:11 | Delete (y,z) and type (z,y) |
| 09:17 | and save this class by pressing Ctrl+S. |
| 09:22 | Simulate this class once again. Close the pop-up window. |
| 09:28 | Select y and z in the variables browser once again. |
| 09:33 | Note that the values of y and z have been interchanged as compared to the previous case. |
| 09:41 | Let me delete this result and go back to Modeling Perspective. |
| 09:47 | Let me go back to the slides. |
| 09:50 | algorithm is Modelica syntax element to enable procedural programming. |
| 09:56 | Only assignment statements are allowed in algorithm section. |
| 10:01 | Assignment statements use the following symbol. |
| 10:06 | Data flows in assignment statements from right to left. |
| 10:10 | There are certain restrictions on functions defined in Modelica. |
| 10:16 | Use of der() in a function is invalid. Use of time variable is not allowed. |
| 10:23 | Use of when statements is not allowed in a function. |
| 10:28 | A function may not have more than one algorithm section and Models cannot be passed as arguments. |
| 10:36 | type is a specialized class to define custom data-types in Modelica. |
| 10:42 | For example, physical quantities like velocity and current can be defined as data-types. |
| 10:50 | They can be used later to declare other variables. |
| 10:54 | Attributes of Modelica data-types like unit and start can be changed accordingly. |
| 11:01 | For example, in the above case velocity is defined to be the same as real data type. |
| 11:08 | But its unit modified to meter per second (m/s). |
| 11:12 | I have created a model named bouncingBallWithUserTypes to simulate type definitions. |
| 11:19 | Let me go back to OMEdit to demonstrate this model. |
| 11:24 | Click on bouncingBallWithUserTypes tab. |
| 11:28 | This model is similar to the bouncingBall model which was discussed in previous tutorials. |
| 11:35 | Please go through the per-requisite tutorials to understand bouncingBall model. |
| 11:41 | Length is defined as Real datatype with its unit modified to meter (m) . |
| 11:47 | Similarly Velocity is defined as Real with its unit modified to meter per second (m/s) . |
| 11:54 | h represents height of the ball from surface of earth. |
| 11:58 | It is defined as length datatype. |
| 12:02 | Similarly, v represents velocity of ball. |
| 12:05 | It is declared to be of velocity datatype. |
| 12:09 | Remaining variable declarations and equations of this model are similar to bouncingBall model. |
| 12:18 | Now let me simulate this. Close the pop-up window. |
| 12:24 | In the Variables Browser, notice that h and v have their respective units associated with their data-types. |
| 12:34 | Let me select h in the variables browser. |
| 12:38 | The plot of h versus time is similar to that of bouncingBall model. |
| 12:43 | Let me de-select h. |
| 12:46 | Now, let me go back to the slides. |
| 12:49 | As an assignment, breach the restrictions on functions and observe the errors produced. |
| 12:56 | This brings us to the end of this tutorial. |
| 12:59 | Watch the video available at following link.
http://spoken-tutorial.org/ What_is_a_Spoken_Tutorial It summarizes the Spoken Tutorial project. |
| 13:05 | We conduct workshops using spoken tutorials. Please contact us. |
| 13:10 | If you have questions in this spoken tutorial, please visit the website. |
| 13:15 | We coordinate coding of solved examples of popular books. Please visit the websites for more information. |
| 13:23 | We help migrate commercial simulator labs to OpenModelica. |
| 13:29 | Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India. |
| 13:36 | We thank the development team of OpenModelica for their support.
Thank you. |