Difference between revisions of "OpenModelica/C2/Control-flow-and-Event-handling/English-timed"
From Script | Spoken-Tutorial
Jyotisolanki (Talk | contribs) (Created page with "{| border=1 ||''' Time ''' ||'''Narration''' |- ||00:01 || Welcome to the spoken tutorial on '''Control flow and Event handling'''. |- || 00:07 || In this tutorial, we are g...") |
Sandhya.np14 (Talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
||00:01 | ||00:01 | ||
− | || Welcome to the spoken tutorial on '''Control flow and Event handling'''. | + | || Welcome to the '''spoken tutorial''' on '''Control flow and Event handling'''. |
|- | |- | ||
|| 00:07 | || 00:07 | ||
− | || In this tutorial, we are going to learn: | + | || In this tutorial, we are going to learn: |
+ | how to use '''if-else''' statement , | ||
+ | how to handle '''time''' and '''state''' events, | ||
+ | how to use '''when''' statement. | ||
|- | |- | ||
|| 00:19 | || 00:19 | ||
− | || To record this tutorial, I am using : '''OpenModelica 1.9.2''' and '''Ubuntu Operating''' | + | || To record this tutorial, I am using : '''OpenModelica 1.9.2''' and '''Ubuntu Operating system''' version '''14.04'''. |
|- | |- | ||
Line 25: | Line 28: | ||
|- | |- | ||
|| 00:47 | || 00:47 | ||
− | ||branching in any programming language , '''class''' definition in '''Modelica''' | + | ||branching in any programming language , '''class''' definition in '''Modelica'''. |
|- | |- | ||
Line 37: | Line 40: | ||
|- | |- | ||
|| 01:05 | || 01:05 | ||
− | ||Now, let us add a | + | ||Now, let us add a constraint to it. |
|- | |- | ||
|| 01:08 | || 01:08 | ||
− | ||Ball is at rest until '''t = 0.5''' seconds. Free fall under | + | ||Ball is at rest until '''t = 0.5''' seconds. Free fall under gravity starts at time '''t = 0.5''' seconds. |
|- | |- | ||
Line 53: | Line 56: | ||
|- | |- | ||
|| 01:32 | || 01:32 | ||
− | ||Please download and save all the files in '''Code Files''' link. | + | ||Please download and '''save''' all the files in '''Code Files''' link. |
|- | |- | ||
Line 73: | Line 76: | ||
|- | |- | ||
|| 01:58 | || 01:58 | ||
− | || Type '''OMEdit''' in the '''Search bar''' and click on '''OMEdit''' icon. Let me go back. | + | || Type: '''OMEdit''' in the '''Search bar''' and click on '''OMEdit''' icon. Let me go back. |
|- | |- | ||
Line 117: | Line 120: | ||
|- | |- | ||
|| 03:02 | || 03:02 | ||
− | || '''Comments''' such as this | + | || '''Comments''' such as this may be written on the first line of a '''model''' or '''class'''. |
|- | |- | ||
Line 141: | Line 144: | ||
|- | |- | ||
|| 03:36 | || 03:36 | ||
− | || Now, let us see how to insert the | + | || Now, let us see how to insert the constraint explained in slides. |
|- | |- | ||
|| 03:43 | || 03:43 | ||
− | || Note that there is an '''if-else block''' in '''equation''' section as shown. | + | || Note that there is an ''''if-else'block''' in '''equation''' section as shown. |
|- | |- | ||
|| 03:48 | || 03:48 | ||
− | ||Let us try to understand each statement of this '''if-else block'''. | + | ||Let us try to understand each statement of this ''''if-else' block'''. |
|- | |- | ||
|| 03:53 | || 03:53 | ||
− | || '''if statement''' places a | + | || ''''if' statement''' places a constraint on '''time''' variable. |
|- | |- | ||
Line 173: | Line 176: | ||
|- | |- | ||
|| 04:19 | || 04:19 | ||
− | || '''else branch''' contains | + | || ''''else' branch''' contains equations which are executed when time is less than '''0.5''' seconds. |
|- | |- | ||
Line 213: | Line 216: | ||
|- | |- | ||
|| 05:06 | || 05:06 | ||
− | ||To delete the result, right click on '''timeEventExample''' in Variables browser and select '''Delete Result'''. | + | ||To delete the result, right click on '''timeEventExample''' in '''Variables browser''' and select '''Delete Result'''. |
|- | |- | ||
Line 221: | Line 224: | ||
|- | |- | ||
|| 05:21 | || 05:21 | ||
− | || Now, let me delete the second '''equation''' in '''else | + | || Now, let me delete the second '''equation''' in '''else''' branch. |
|- | |- | ||
Line 237: | Line 240: | ||
|- | |- | ||
||05:37 | ||05:37 | ||
− | ||Go to the '''Messages browser''' and scroll up to see the error message. | + | ||Go to the '''Messages browser''' and scroll up to see the '''error message'''. |
|- | |- | ||
Line 245: | Line 248: | ||
|- | |- | ||
|| 05:49 | || 05:49 | ||
− | ||Therefore, the number of '''equations''' in '''if''' and '''else | + | ||Therefore, the number of '''equations''' in '''if''' and '''else''' branches should each be equal to the number of variables. |
|- | |- | ||
Line 253: | Line 256: | ||
|- | |- | ||
|| 06:05 | || 06:05 | ||
− | || Note that '''der(h) = v''' is an | + | || Note that '''der(h) = v''' is an equation which is common to both '''if''' and '''else branches'''. |
|- | |- | ||
|| 06:14 | || 06:14 | ||
− | ||Hence, the '''if-else | + | ||Hence, the '''if-else''' block can be replaced by two '''statements'''. |
|- | |- | ||
Line 265: | Line 268: | ||
|- | |- | ||
|| 06:23 | || 06:23 | ||
− | ||Delete the '''if-else | + | ||Delete the '''if-else''' block first. |
|- | |- | ||
Line 289: | Line 292: | ||
|- | |- | ||
|| 06:54 | || 06:54 | ||
− | || Copy the two equations. | + | || '''Copy''' the two equations. |
|- | |- | ||
Line 297: | Line 300: | ||
|- | |- | ||
|| 07:00 | || 07:00 | ||
− | ||Do a right-click and select '''Paste''' | + | ||Do a right-click and select '''Paste'''. |
|- | |- | ||
Line 329: | Line 332: | ||
|- | |- | ||
|| 07:38 | || 07:38 | ||
− | || This is an '''if-else | + | || This is an '''if-else''' statement which indicates that if '''time >= 0.5''' '''dv/dt = g''' otherwise '''dv/dt = 0'''. |
|- | |- | ||
||07:52 | ||07:52 | ||
− | || Now let me go back to the | + | || Now let me go back to the '''slide'''s. |
|- | |- | ||
||07:55 | ||07:55 | ||
− | ||Please note that the number of '''equations''' in '''if | + | ||Please note that the number of '''equations''' in '''if''' branch should be equal to the number of equations in '''else''' branch. |
|- | |- | ||
|| 08:03 | || 08:03 | ||
− | ||And both of them should be equal to the number of variables in the '''class''' or '''model'''. | + | ||And, both of them should be equal to the number of variables in the '''class''' or '''model'''. |
|- | |- | ||
Line 349: | Line 352: | ||
|- | |- | ||
|| 08:15 | || 08:15 | ||
− | ||'''Events''' may be categorized into time | + | ||'''Events''' may be categorized into '''time event'''s and '''state event'''s. |
|- | |- | ||
Line 361: | Line 364: | ||
|- | |- | ||
|| 08:32 | || 08:32 | ||
− | ||'''state event''' occurs when a system variable | + | ||'''state event''' occurs when a system variable crosses a certain value. |
|- | |- | ||
Line 369: | Line 372: | ||
|- | |- | ||
|| 08:43 | || 08:43 | ||
− | ||A ball under free fall | + | ||A ball under free fall faces a '''state event''' on touching the ground. |
|- | |- | ||
Line 413: | Line 416: | ||
|- | |- | ||
|| 09:41 | || 09:41 | ||
− | || '''e''' is coefficient of restitution for inelastic collision of ball with Earth. | + | || '''e''' is '''coefficient of restitution''' for inelastic collision of ball with Earth. |
|- | |- | ||
|| 09:48 | || 09:48 | ||
− | ||It is a parameter and its value remains constant at 0.8 throughout the simulation run. | + | ||It is a parameter and its value remains constant at 0.8 throughout the '''simulation run'''. |
|- | |- | ||
|| 09:56 | || 09:56 | ||
− | || '''radius''' represents the radius of ball in | + | || '''radius''' represents the radius of ball in meter. |
|- | |- | ||
Line 433: | Line 436: | ||
|- | |- | ||
|| 10:13 | || 10:13 | ||
− | || '''when | + | || '''when''' statement is used to signal an '''event'''. |
|- | |- | ||
Line 457: | Line 460: | ||
|- | |- | ||
|| 10:49 | || 10:49 | ||
− | || To simulate this model | + | || To simulate this model, click on '''Simulation Setup''' button in the toolbar. |
|- | |- | ||
|| 10:55 | || 10:55 | ||
− | ||Under '''General''' tab, change '''Stop Time''' field to 30 | + | ||Under '''General''' tab, change '''Stop Time''' field to '''30 unit'''s and click on '''Simulate'''. |
|- | |- | ||
Line 473: | Line 476: | ||
|- | |- | ||
|| 11:10 | || 11:10 | ||
− | ||Note that '''h''' drops below zero | + | ||Note that '''h''' drops below zero which is unacceptable. |
|- | |- | ||
Line 501: | Line 504: | ||
|- | |- | ||
|| 11:42 | || 11:42 | ||
− | || '''when | + | || '''when''' statement is used to signal an event. |
|- | |- | ||
Line 537: | Line 540: | ||
|- | |- | ||
|| 12:36 | || 12:36 | ||
− | || Watch the video available at following link.'''http://spoken-tutorial.org/ | + | || Watch the video available at following link. |
+ | '''http://spoken-tutorial.org/What_is_a_Spoken_Tutorial''' | ||
|- | |- | ||
|| 12:40 | || 12:40 | ||
− | ||It summarises the '''Spoken Tutorial''' project | + | ||It summarises the '''Spoken Tutorial''' project. |
|- | |- | ||
|| 12:42 | || 12:42 | ||
− | || We conduct workshops using spoken tutorials | + | || We conduct workshops using spoken tutorials, give certificates. Please contact us. |
|- | |- | ||
Line 557: | Line 561: | ||
|- | |- | ||
|| 13:03 | || 13:03 | ||
− | ||We give honorarium and certificates to those who do this. For more details please visit the following website. | + | ||We give honorarium and certificates to those who do this. For more details, please visit the following website. |
|- | |- | ||
|| 13:10 | || 13:10 | ||
− | || Under '''Lab Migration Project''' we help migrate commercial simulator labs to OpenModelica. | + | || Under '''Lab Migration Project''', we help migrate commercial simulator labs to OpenModelica. |
|- | |- |
Latest revision as of 16:03, 18 August 2018
Time | Narration |
00:01 | Welcome to the spoken tutorial on Control flow and Event handling. |
00:07 | In this tutorial, we are going to learn:
how to use if-else statement , how to handle time and state events, how to use when statement. |
00:19 | To record this tutorial, I am using : OpenModelica 1.9.2 and Ubuntu Operating system version 14.04. |
00:30 | But, this process is identical in Windows, Mac OS X or FOSSEE OS on ARM. |
00:38 | To understand and practice this tutorial, you need to know the following: equation-based modeling of physical systems , |
00:47 | branching in any programming language , class definition in Modelica. |
00:53 | Pre-requisite tutorials are mentioned on Spoken Tutorial website. Please go through them. |
01:00 | freeFall class was already discussed in previous tutorials. |
01:05 | Now, let us add a constraint to it. |
01:08 | Ball is at rest until t = 0.5 seconds. Free fall under gravity starts at time t = 0.5 seconds. |
01:19 | I have created a model named timeEventExample which simulates this condition. |
01:25 | timeEventExample file is available in Code Files link on spoken tutorial website. |
01:32 | Please download and save all the files in Code Files link. |
01:38 | For your convenience, freeFall class is also made available. |
01:43 | Now let me go to OMEdit. |
01:45 | I have already launched it on my system. |
01:49 | To open OMEdit on Ubuntu Operating System, click on Dash Home icon at the top left. |
01:58 | Type: OMEdit in the Search bar and click on OMEdit icon. Let me go back. |
02:06 | Note: Windows users may similarly search for OMEdit in Start menu and open it. |
02:13 | To open timeEventExample file, click on the tool named Open Model/Library File. |
02:21 | Locate timeEventExample file in your system and click on Open. |
02:28 | You may alternatively use the File menu to open this file. |
02:34 | Note that timeEventExample has appeared in Libraries browser. |
02:39 | Right-click on this icon and select View class. |
02:44 | The model has now opened in Diagram view. |
02:47 | Click on third button which is the Text View. |
02:51 | We have already discussed that model and class are used synonymously in Modelica. |
02:58 | The name of this model is timeEventExample. |
03:02 | Comments such as this may be written on the first line of a model or class. |
03:08 | h, v and g represent the same quantities as in freeFall class. |
03:16 | initial equation section contains initial conditions. |
03:21 | Value of h at time t = 0 is 30m. |
03:26 | Value of v at time t = 0 is 0. |
03:31 | Equation signifies the beginning of equation section of this model. |
03:36 | Now, let us see how to insert the constraint explained in slides. |
03:43 | Note that there is an 'if-else'block in equation section as shown. |
03:48 | Let us try to understand each statement of this 'if-else' block. |
03:53 | 'if' statement places a constraint on time variable. |
03:58 | time is a built-in variable in Modelica. |
04:01 | It need not be explicitly declared in a class or model. |
04:06 | The ball experiences free fall after time 0.5 seconds. |
04:12 | Hence, these two equations represent the same equations as those in freeFall class. |
04:19 | 'else' branch contains equations which are executed when time is less than 0.5 seconds. |
04:27 | The ball is at rest when time is less than 0.5 seconds. |
04:32 | Hence, these two equations hold good in that case. |
04:36 | end if signifies the end of if-else block. |
04:41 | The model is now complete. |
04:44 | Let us simulate it by clicking on Simulate button. |
04:48 | Plotting perspective has now opened. |
04:51 | Select h in the Variables browser. |
04:55 | This generates h versus time plot. |
04:58 | Since the ball is at rest until time t=0.5 seconds, height remains the same. |
05:06 | To delete the result, right click on timeEventExample in Variables browser and select Delete Result. |
05:15 | To go back to Modeling perspective, click on Modeling button at bottom right. |
05:21 | Now, let me delete the second equation in else branch. |
05:26 | Click on Save button in the toolbar, to save this model. |
05:31 | Click on Simulate. |
05:34 | The simulation has failed. |
05:37 | Go to the Messages browser and scroll up to see the error message. |
05:42 | The error message says that the number of equations in each branch should be the same. |
05:49 | Therefore, the number of equations in if and else branches should each be equal to the number of variables. |
05:58 | Let me insert the equation back in its place and click on Save button. |
06:05 | Note that der(h) = v is an equation which is common to both if and else branches. |
06:14 | Hence, the if-else block can be replaced by two statements. |
06:20 | Let me show you how it is done. |
06:23 | Delete the if-else block first. |
06:26 | The equations to be typed here have been provided in a file named if else-statement.txt. |
06:34 | These file is also available in Code Files link. |
06:39 | Since it is a text file, I have used gedit to open it. |
06:45 | Let me go to gedit. |
06:47 | Windows users may open this file using Notepad or any other text editor. |
06:54 | Copy the two equations. |
06:57 | Go back to OMEdit. |
07:00 | Do a right-click and select Paste. |
07:04 | Click on Save button in the toolbar once again. |
07:08 | Click on Simulate. |
07:11 | Close the pop up window. |
07:14 | Select h in the Variables browser once again. |
07:18 | Note that the h versus time plot is the same as in previous case which indicates that both models are the same. |
07:27 | To delete the result, right-click on timeEventExample and select Delete Result. |
07:34 | Click on Modeling button at bottom right. |
07:38 | This is an if-else statement which indicates that if time >= 0.5 dv/dt = g otherwise dv/dt = 0. |
07:52 | Now let me go back to the slides. |
07:55 | Please note that the number of equations in if branch should be equal to the number of equations in else branch. |
08:03 | And, both of them should be equal to the number of variables in the class or model. |
08:10 | Event is an abrupt change in behavior of a system. |
08:15 | Events may be categorized into time events and state events. |
08:20 | In a time event, the time at which event occurs is precisely known. |
08:25 | timeEventExample handles a time event at t = 0.5 seconds |
08:32 | state event occurs when a system variable crosses a certain value. |
08:38 | Let us try to understand state events using an example. |
08:43 | A ball under free fall faces a state event on touching the ground. |
08:48 | The following behavior is observed when it hits the ground. |
08:52 | Velocity of ball reverts in direction. |
08:55 | Magnitude of velocity changes if the collision is inelastic. |
09:00 | I have created a model named bouncingBall to simulate this behavior. |
09:06 | This file is available on Spoken Tutorial Website. |
09:10 | To view this model and simulate it, let me go back to OMEdit. |
09:15 | Click on OpenModel/LIbrary File tool. |
09:19 | Locate bouncingBall file in your system and click on Open. |
09:25 | Right-click on bouncingBall icon in Libraries browser and select View Class. |
09:33 | If the model does not open in Text View, go to the top of modeling area and click on Text View button. |
09:41 | e is coefficient of restitution for inelastic collision of ball with Earth. |
09:48 | It is a parameter and its value remains constant at 0.8 throughout the simulation run. |
09:56 | radius represents the radius of ball in meter. |
10:00 | h, v and g represent the same quantities as in timeEventExample. |
10:07 | These two equations of motions have already been discussed in freeFall class. |
10:13 | when statement is used to signal an event. |
10:17 | It is used to specify the action to be taken when an event occurs. |
10:22 | h <= radius holds true when the ball is in contact with ground. |
10:29 | This statement is executed whenever the event occurs. |
10:34 | reinit() function is used to re-initialize a variable. |
10:39 | Here, it re-initializes velocity of ball with the negative product of e and velocity of ball before touching the ground. |
10:49 | To simulate this model, click on Simulation Setup button in the toolbar. |
10:55 | Under General tab, change Stop Time field to 30 units and click on Simulate. |
11:04 | Close the pop-up window. |
11:06 | Select h in Variables browser. |
11:10 | Note that h drops below zero which is unacceptable. |
11:16 | We have seen similar behavior while simulating freeFall class. |
11:21 | This faulty behavior is due to numerical errors. |
11:25 | A discussion of numerical errors is beyond the scope of this tutorial. |
11:30 | Please refer to Zeno behavior of bouncing ball for a better understanding. |
11:36 | Let me de-select h. |
11:39 | Let us go back to the slides. |
11:42 | when statement is used to signal an event. |
11:45 | reinit(a,b) assigns value of b to variable a and resumes simulation. |
11:54 | pre(a) returns the value of variable a before event. |
11:59 | For example, reinit(a, 10) assigns a value of 10 to a when event occurs. |
12:08 | The faulty behavior of bouncingBall model is corrected in bouncingBallWithHysteresis. |
12:15 | bouncingBallWithHysteresis model is available on spoken tutorial website. |
12:21 | Simulate bouncingBallWithHysteresis and generate h versus time plot. |
12:27 | Notice the difference between bouncingBall and bouncingBallWithHysteresis |
12:33 | This brings us to the end of this tutorial. |
12:36 | Watch the video available at following link. |
12:40 | It summarises the Spoken Tutorial project. |
12:42 | We conduct workshops using spoken tutorials, give certificates. Please contact us. |
12:48 | If you have any questions related to this spoken tutorial, please visit the following website. |
12:55 | Under Textbook Companion Project we coordinate coding of solved examples of popular books. |
13:03 | We give honorarium and certificates to those who do this. For more details, please visit the following website. |
13:10 | Under Lab Migration Project, we help migrate commercial simulator labs to OpenModelica. |
13:17 | For more information please visit the website. |
13:21 | Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India. |
13:28 | We thank the development team of OpenModelica for their support. |
13:33 | Thank you for joining me in this tutorial. Goodbye. |