Difference between revisions of "OpenModelica/C2/Control-flow-and-Event-handling/English"
Nancyvarkey (Talk | contribs) |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 14: | Line 14: | ||
|| In this tutorial, we are going to learn: | || In this tutorial, we are going to learn: | ||
− | How to use '''if-else''' | + | * How to use '''if-else''' statement |
− | How to handle '''time''' and '''state''' events | + | * How to handle '''time''' and '''state''' events |
− | How to use '''when''' statement. | + | * How to use '''when''' statement. |
|- | |- | ||
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 | + | *'''Ubuntu Operating''' system version 14.04. |
− | But, this process is identical in | + | But, this process is identical in '''Windows, Mac OS X''' or '''FOSSEE OS''' on '''ARM'''. |
|- | |- | ||
Line 36: | Line 36: | ||
'''Pre-requisites''' | '''Pre-requisites''' | ||
− | || To practice this tutorial, you | + | || To understand and practice this tutorial, you need to know the following: |
− | * equation-based modeling of physical systems | + | * equation-based '''modeling''' of physical systems |
* branching in any programming language | * branching in any programming language | ||
− | * class definition in Modelica | + | * '''class''' definition in '''Modelica''' |
− | Pre-requisite tutorials are mentioned on | + | Pre-requisite tutorials are mentioned on '''Spoken Tutorial''' website. Please go through them. |
|- | |- | ||
Line 50: | Line 50: | ||
'''Problem Statement''' | '''Problem Statement''' | ||
− | ||'''freeFall''' | + | ||'''freeFall''' class was already discussed in previous tutorials. |
− | Now, let us add a | + | Now, let us add a '''constraint''' to it. |
− | |||
− | + | Ball is at rest until '''t = 0.5''' seconds. | |
− | I have created a model ''' | + | Free fall under '''gravity''' starts at time '''t = 0.5''' seconds. |
+ | |||
+ | |||
+ | I have created a '''model''' named '''timeEventExample''' which simulates this condition. | ||
|- | |- | ||
− | || | + | ||Point to '''timeEventExample''' file. |
− | ||''' | + | ||'''timeEventExample''' file is available in '''Code Files''' link on '''spoken tutorial''' website. |
− | Please download and save all the files | + | Please download and save all the files in '''Code Files''' link. |
− | For your convenience, ''' | + | |
+ | For your convenience, '''freeFall class''' is also made available. | ||
|- | |- | ||
|| | || | ||
− | || | + | || Now let me go to '''OMEdit'''. |
− | + | I have already launched it on my system. | |
|- | |- | ||
− | || Click on Dash Home | + | || Click on '''Dash Home''' |
− | || To open '''OMEdit''' on Ubuntu Operating System, click on '''Dash Home''' icon. | + | || To open '''OMEdit''' on '''Ubuntu Operating System''', click on '''Dash Home''' icon at the top left. |
+ | |||
+ | |- | ||
+ | || Type '''OMEdit''' in the Search bar, click on '''OMEdit''' icon. | ||
+ | || Type '''OMEdit''' in the '''Search bar''' and click on '''OMEdit''' icon. Let me go back. | ||
|- | |- | ||
|| | || | ||
− | || | + | ||Note: '''Windows''' users may similarly search for '''OMEdit''' in '''Start''' menu and open it. |
|- | |- | ||
− | || Click on ''' | + | || Click on '''Open Model/Library File''' in tool bar. |
− | || To open ''' | + | || To open '''timeEventExample''' file, click on the tool named '''Open Model/Library File'''. |
− | Click on | + | |- |
+ | || Click on '''Open'''. | ||
+ | || Locate '''timeEventExample''' file in your system and click on '''Open'''. | ||
|- | |- | ||
− | || | + | || Point to '''File''' menu |
− | || | + | ||You may alternatively use the '''File''' menu to open this file. |
+ | |||
+ | Note that '''timeEventExample''' has appeared in '''Libraries browser'''. | ||
+ | |||
+ | |- | ||
+ | ||Right-click >> select '''View class''' | ||
+ | ||Right-click on this icon and select '''View class'''. | ||
+ | |||
+ | |||
+ | The model has now opened in '''Diagram view'''. | ||
+ | |||
+ | |- | ||
+ | ||Click on '''Text View''' | ||
+ | ||Click on third button which is the '''Text View'''. | ||
+ | |||
+ | |||
+ | We have already discussed that '''model''' and '''class''' are used synonymously in '''Modelica'''. | ||
|- | |- | ||
Line 96: | Line 121: | ||
'''model timeEventExample ''' | '''model timeEventExample ''' | ||
− | || | + | || The name of this '''model''' is '''timeEventExample'''. |
− | + | ||
− | + | ||
|- | |- | ||
− | || | + | || '''Time event demonstration''' |
− | || Comments may be | + | || '''Comments''' such as this, may be written on the first line of a '''model''' or '''class'''. |
|- | |- | ||
− | || '''Real h(unit = “m”) | + | || '''Real h(unit = “m”), "Height of ball";''' |
'''Real v(unit = “m/s”) “Velocity of ball”;''' | '''Real v(unit = “m/s”) “Velocity of ball”;''' | ||
'''parameter Real g(unit = “m/s2”) = -9.81 “Acceleration due to gravity”;''' | '''parameter Real g(unit = “m/s2”) = -9.81 “Acceleration due to gravity”;''' | ||
− | || ''' | + | || '''h, v''' and '''g''' represent the same quantities as in '''freeFall class'''. |
|- | |- | ||
|| '''initial equation''' | || '''initial equation''' | ||
− | || ''' | + | || '''initial equation''' section contains initial conditions. |
|- | |- | ||
|| '''h = 30;''' | || '''h = 30;''' | ||
− | || Value of '''h''' at time '''t = 0''' is ''' | + | || Value of '''h''' at time '''t = 0''' is '''30m'''. |
|- | |- | ||
Line 124: | Line 147: | ||
|- | |- | ||
− | || | + | || Highlight '''Equation''' |
− | || ''' | + | || '''Equation''' signifies the beginning of equation section of this '''model'''. |
|- | |- | ||
|| | || | ||
− | || Now, | + | || Now, let us see how to insert the '''constraint''' explained in slides. |
|- | |- | ||
− | || | + | || Highlight '''if-else block''' |
− | || | + | || Note that there is an '''if-else block''' in '''equation''' section as shown. |
− | + | ||
+ | Let us try to understand each statement of this '''if-else block'''. | ||
|- | |- | ||
|| '''if time >= 0.5 then''' | || '''if time >= 0.5 then''' | ||
− | || | + | || '''if statement''' places a '''constraint''' on '''time''' variable. |
− | ''' | + | *'''time''' is a built-in variable in '''Modelica'''. |
− | It need not be explicitly declared in a class or model. | + | *It need not be explicitly declared in a '''class''' or '''model'''. |
|- | |- | ||
Line 149: | Line 173: | ||
'''der(v) = g;''' | '''der(v) = g;''' | ||
− | || | + | || The ball experiences free fall after time '''0.5''' seconds. |
− | + | ||
− | The ball experiences free fall after '''0.5''' seconds. | + | |
− | Hence, these equations | + | Hence, these two equations represent the same equations as those in '''freeFall class'''. |
|- | |- | ||
|| '''else''' | || '''else''' | ||
− | || ''' | + | || '''else branch''' contains '''equations''' which are executed when time is less than '''0.5''' seconds. |
|- | |- | ||
|| '''der(h) = v;''' | || '''der(h) = v;''' | ||
− | '''der(v) = 0; ''' | + | '''der(v) = 0;''' |
− | || The ball is at rest | + | || The ball is at rest when time is less than '''0.5''' seconds. |
+ | |||
+ | Hence, these two '''equations''' hold good in that case. | ||
− | |||
− | |||
− | |||
|- | |- | ||
|| '''end if;''' | || '''end if;''' | ||
− | || ''' | + | || '''end if''' signifies the end of '''if-else block'''. |
|- | |- | ||
|| Click on '''simulate''' button | || Click on '''simulate''' button | ||
− | || The model is now complete. | + | || The '''model''' is now complete. |
− | + | ||
+ | Let us simulate it by clicking on '''Simulate''' button. | ||
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | || | + | || '''Plotting perspective''' has now opened. |
− | + | Select '''h''' in the '''Variables browser'''. | |
|- | |- | ||
|| | || | ||
− | || This generates ''' | + | || This generates '''h''' versus '''time''' plot. |
− | + | Since the ball is at rest until time '''t=0.5''' seconds, height remains the same. | |
|- | |- | ||
− | || | + | || Right-click on '''timeEventExample''' >> select '''Delete Result''' |
− | || | + | ||To delete the result, right click on '''timeEventExample''' in Variables browser and select '''Delete Result'''. |
− | Click on '''Modeling perspective''' button at bottom right. | + | |- |
+ | || Click on '''Modeling perspective''' button. | ||
+ | || To go back to '''Modeling perspective''', click on '''Modeling''' button at bottom right. | ||
|- | |- | ||
Line 206: | Line 230: | ||
end if; | end if; | ||
− | || Now, let me delete ''' | + | || Now, let me delete the second '''equation''' in '''else branch'''. |
+ | |||
+ | Click on '''Save''' button in the toolbar, to save this '''model'''. | ||
+ | |||
|- | |- | ||
− | || Click on | + | || Click on '''Simulate'''. |
− | || Click on ''' | + | || Click on '''Simulate'''. |
|- | |- | ||
− | || | + | || |
− | || The simulation has failed | + | || The simulation has failed. |
− | + | |- | |
+ | ||Hover the mouse over '''Messages browser''. | ||
+ | ||Go to the '''Messages browser''' and scroll up to see the error message. | ||
− | Therefore, the number of equations in ''' | + | The error message says that the number of '''equations''' in each '''branch''' should be the same. |
+ | |||
+ | Therefore, the number of '''equations''' in '''if''' and '''else branches''' should each be equal to the number of variables. | ||
|- | |- | ||
− | || | + | || Put '''equation''' back and click on '''Save''' |
− | + | ||Let me insert the '''equation''' back in its place and click on '''Save''' button. | |
− | + | |- | |
+ | || Highlight '''equation''' | ||
+ | || Note that '''der(h) = v''' is an '''equation''' which is common to both '''if''' and '''else branches'''. | ||
+ | |||
+ | Hence, the '''if-else block''' can be replaced by two '''statements'''. | ||
+ | |||
+ | |- | ||
+ | ||Delete the '''if-else block''' | ||
+ | || Let me show you how it is done. | ||
+ | |||
+ | Delete the '''if-else block''' first. | ||
+ | |||
+ | |- | ||
+ | || | ||
+ | ||The equations to be typed here have been provided in a file named '''if else-statement.txt'''. | ||
+ | |||
|- | |- | ||
|| | || | ||
− | || | + | || These file is also available in '''Code Files''' link. |
|- | |- | ||
|| | || | ||
− | || | + | || Since it is a text file, I have used '''gedit''' to open it. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|| /* gedit */ | || /* gedit */ | ||
− | || | + | || Let me go to '''gedit'''. |
− | Windows users may open | + | '''Windows''' users may open this file using '''Notepad''' or any other text editor. |
|- | |- | ||
Line 247: | Line 289: | ||
'''der(v) = if time >= 0.5 then g else 0;''' | '''der(v) = if time >= 0.5 then g else 0;''' | ||
− | || Copy the two | + | || Copy the two equations. |
|- | |- | ||
|| /* timeEventExample */ | || /* timeEventExample */ | ||
− | || | + | || Go back to '''OMEdit'''. |
+ | |||
+ | Do a right-click and select '''Paste''' | ||
|- | |- | ||
− | || | + | || Click on '''Save'''. |
− | || | + | || Click on '''Save''' button in the toolbar once again. |
|- | |- | ||
|| Click on '''simulate''' button | || Click on '''simulate''' button | ||
|| Click on '''Simulate'''. | || Click on '''Simulate'''. | ||
+ | |||
+ | Close the pop up window. | ||
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | + | Select '''h''' in the '''Variables browser''' | |
− | + | ||
− | + | ||Select '''h''' in the '''Variables browser''' once again. | |
− | + | Note that the '''h''' versus '''time''' plot is the same as in previous case which indicates that both models are the same. | |
+ | |||
+ | |- | ||
+ | || | ||
+ | ||To delete the result, right-click on '''timeEventExample''' and select '''Delete Result'''. | ||
|- | |- | ||
|| /* Switch to '''Modeling perspective''' */ | || /* Switch to '''Modeling perspective''' */ | ||
− | || | + | || Click on '''Modeling''' button at bottom right. |
|- | |- | ||
|| der(v) = if time >= 0.5 then g else 0; | || der(v) = if time >= 0.5 then g else 0; | ||
− | || This if-else statement | + | || This is an '''if-else statement''' which indicates that if '''time >= 0.5''' '''dv/dt = g''' otherwise '''dv/dt = 0'''. |
|- | |- | ||
− | || Slide: | + | ||Switch to slides. |
+ | || Now let me go back to the slides. | ||
+ | |- | ||
+ | ||Slide: | ||
− | + | Caution | |
− | || | + | ||Please note that the number of '''equations''' in '''if branch''' should be equal to the number of equations in '''else branch'''. |
+ | |||
+ | And both of them should be equal to the number of variables in the '''class''' or '''model'''. | ||
|- | |- | ||
Line 291: | Line 345: | ||
|| '''Event''' is an abrupt change in behavior of a system. | || '''Event''' is an abrupt change in behavior of a system. | ||
− | '''Events''' | + | '''Events''' may be categorized into time events and state events. |
− | In a '''time event''', the time at which event occurs is known. | + | In a '''time event''', the time at which event occurs is precisely known. |
− | + | '''timeEventExample''' handles a time event at '''t = 0.5''' seconds | |
− | + | '''state event''' occurs when a system variable cross a certain value. | |
|- | |- | ||
Line 303: | Line 357: | ||
Problem Statement | Problem Statement | ||
− | || Let us understand | + | ||Let us try to understand state events using an example. |
− | A ball under free fall faces a state event on touching the ground. | + | A ball under free fall, faces a '''state event''' on touching the ground. |
− | + | The following behavior is observed when it hits the ground. | |
− | + | *Velocity of ball reverts in direction. | |
− | + | *Magnitude of velocity changes if the collision is inelastic. | |
|- | |- | ||
− | || | + | || |
− | || | + | ||I have created a '''model''' named '''bouncingBall''' to simulate this behavior. |
− | + | This file is available on '''Spoken Tutorial''' Website. | |
|- | |- | ||
− | || | + | || /* Switch to OMEdit */ |
− | || | + | || To view this '''model''' and simulate it, let me go back to '''OMEdit'''. |
|- | |- | ||
− | || | + | || Click on '''bouncingBall''' tab at the top of modeling area |
− | || | + | || Click on '''OpenModel/LIbrary File''' tool. |
− | If the model | + | Locate '''bouncingBall''' file in your system and click on '''Open'''. |
+ | |||
+ | |- | ||
+ | || Right-click on '''bouncingBall''' | ||
+ | || Right-click on '''bouncingBall''' icon in '''Libraries browser''' and select '''View Class'''. | ||
+ | |||
+ | If the '''model''' does not open in '''Text View,''' go to the top of '''modeling area''' and click on '''Text View''' button. | ||
|- | |- | ||
Line 333: | Line 393: | ||
'''parameter Real e = 0.8 "Coefficient of restitution";''' | '''parameter Real e = 0.8 "Coefficient of restitution";''' | ||
− | || ''' | + | || '''e''' is coefficient of restitution for inelastic collision of ball with Earth. |
+ | |||
+ | It is a parameter and its value remains constant at 0.8 throughout the simulation run. | ||
|- | |- | ||
− | || '''parameter Real radius = 0.001 "Radius of the ball in | + | || '''parameter Real radius = 0.001 "Radius of the ball in "m";''' |
− | || | + | || '''radius''' represents the radius of ball in '''m'''. |
|- | |- | ||
|| '''Real h(unit = “m”);''' | || '''Real h(unit = “m”);''' | ||
Line 344: | Line 406: | ||
'''parameter Real g(unit = “m/s2”) = -9.81;''' | '''parameter Real g(unit = “m/s2”) = -9.81;''' | ||
− | || ''' | + | || '''h, v''' and '''g''' represent the same quantities as in '''timeEventExample'''. |
|- | |- | ||
Line 350: | Line 412: | ||
'''der(v) = g; ''' | '''der(v) = g; ''' | ||
− | || | + | || These two equations of motions have already been discussed in '''freeFall class'''. |
|- | |- | ||
|| '''when h <= radius then''' | || '''when h <= radius then''' | ||
− | || ''' | + | || '''when statement''' is used to signal an event. |
It is used to specify the action to be taken when an event occurs. | It is used to specify the action to be taken when an event occurs. | ||
− | h <= radius holds true when the ball is in contact with ground. | + | '''h <= radius''' holds '''true''' when the ball is in contact with ground. |
+ | This '''statement''' is executed whenever the event occurs. | ||
|- | |- | ||
− | || '''reinit(v, -e * pre(v)); ''' | + | || '''reinit(v, -e * pre(v));''' |
− | || ''' | + | || '''reinit()''' function is used to re-initialize a variable. |
|- | |- | ||
− | || '''reinit(v, -e * pre(v)); ''' | + | || '''reinit(v, -e * pre(v));''' |
− | || Here, it re-initializes velocity of ball with the negative product of | + | || Here, it re-initializes velocity of ball with the negative product of '''e''' and velocity of ball before touching the ground. |
− | + | ||
− | + | ||
|- | |- | ||
|| /* Simulation Options */ | || /* Simulation Options */ | ||
− | Click on ''' | + | Click on '''Simulation Options''' icon |
− | || | + | || To simulate this model |
− | Click on ''' | + | * Click on '''Simulation Setup''' button in the toolbar. |
− | + | *Under '''General''' tab, change '''Stop Time''' field to 30 units and | |
− | Click on ''' | + | *Click on '''Simulate'''. |
+ | |||
+ | *Close the pop-up window. | ||
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | + | Select '''h''' in '''Variables browser''' | |
− | + | ||
− | + | || Select '''h''' in '''Variables browser'''. | |
− | ''' | + | Note that '''h''' drops below zero, which is unacceptable. |
− | We | + | |
+ | We have seen similar behavior while simulating '''freeFall class'''. | ||
|- | |- | ||
|| | || | ||
− | || | + | || This faulty behavior is due to numerical errors. |
− | + | ||
− | + | ||
A discussion of numerical errors is beyond the scope of this tutorial. | A discussion of numerical errors is beyond the scope of this tutorial. | ||
− | + | Please refer to '''Zeno''' behavior of bouncing ball for a better understanding. | |
|- | |- | ||
− | || | + | ||De-select '''h''' |
− | || | + | ||Let me de-select '''h'''. |
|- | |- | ||
− | || | + | || Switch back to slides. |
− | || Let | + | || Let us go back to the slides. |
|- | |- | ||
|| Slide: | || Slide: | ||
− | Event-related operators | + | '''Event-related operators''' |
− | || ''' | + | || '''when statement''' is used to signal an event. |
+ | |||
+ | '''reinit(a,b)''' assigns value of '''b''' to variable '''a''' and resumes simulation. | ||
+ | |||
+ | '''pre(a)''' returns the value of variable '''a''' before event. | ||
+ | |||
− | ''' | + | For example, '''reinit(a, 10)''' assigns a value of '''10''' to '''a''' when event occurs. |
− | |||
− | + | The faulty behavior of '''bouncingBall''' model is corrected in '''bouncingBallWithHysteresis'''. | |
|- | |- | ||
|| Slide: | || Slide: | ||
− | + | '''Assignment''' | |
− | + | ||
− | + | ||'''bouncingBallWithHysteresis''' model is available on '''spoken tutorial''' website. | |
+ | |||
+ | Simulate '''bouncingBallWithHysteresis''' and generate '''h''' versus '''time''' plot. | ||
+ | |||
+ | Notice the difference between '''bouncingBall''' and '''bouncingBallWithHysteresis''' | ||
|- | |- | ||
Line 438: | Line 506: | ||
|| Slide: | || Slide: | ||
− | 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 |
|- | |- | ||
|| Slide: | || Slide: | ||
− | Spoken Tutorial Workshops | + | '''Spoken Tutorial''' Workshops |
|| We conduct workshops using spoken tutorials | || We conduct workshops using spoken tutorials | ||
Line 459: | Line 528: | ||
Forum | Forum | ||
− | || If you have questions | + | || If you have any questions related to this spoken tutorial, please visit the following website. |
|- | |- | ||
|| Slide: | || Slide: | ||
− | Textbook Companion Project | + | '''Textbook Companion Project''' |
− | || | + | || Under '''Textbook Companion Project''' we coordinate coding of solved examples of popular books. |
− | We give honorarium to | + | We give honorarium and certificates to those who do this. |
+ | |||
+ | For more details please visit the following website. | ||
|- | |- | ||
|| Slide: | || Slide: | ||
− | Lab Migration Project | + | '''Lab Migration Project''' |
− | || | + | || Under '''Lab Migration Project''' we help migrate commercial simulator labs to OpenModelica. |
+ | |||
+ | For more information please visit the website. | ||
|- | |- | ||
|| Slide: | || Slide: | ||
− | 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 485: | Line 558: | ||
Thanks | Thanks | ||
− | || We thank '''OpenModelica | + | || We thank the development team of '''OpenModelica''' for their support. |
|- | |- | ||
|| | || | ||
− | || Thank you for joining me in this | + | || Thank you for joining me in this tutorial. Goodbye. |
|- | |- | ||
|} | |} |
Latest revision as of 05:37, 2 March 2016
Visual Cue | Narration |
Slide Number 1
Title Slide |
Welcome to the spoken tutorial on Control flow and Event handling. |
Slide:
Learning Objectives |
In this tutorial, we are going to learn:
|
Slide:
System Requirements |
To record this tutorial, I am using
But, this process is identical in Windows, Mac OS X or FOSSEE OS on ARM. |
Slide:
Pre-requisites |
To understand and practice this tutorial, you need to know the following:
Pre-requisite tutorials are mentioned on Spoken Tutorial website. Please go through them. |
Slide:
Problem Statement |
freeFall class was already discussed in previous tutorials.
Now, let us add a constraint to it.
Free fall under gravity starts at time t = 0.5 seconds.
|
Point to timeEventExample file. | timeEventExample file is available in Code Files link on spoken tutorial website.
Please download and save all the files in Code Files link.
|
Now let me go to OMEdit.
I have already launched it on my system. | |
Click on Dash Home | To open OMEdit on Ubuntu Operating System, click on Dash Home icon at the top left. |
Type OMEdit in the Search bar, click on OMEdit icon. | Type OMEdit in the Search bar and click on OMEdit icon. Let me go back. |
Note: Windows users may similarly search for OMEdit in Start menu and open it. | |
Click on Open Model/Library File in tool bar. | To open timeEventExample file, click on the tool named Open Model/Library File. |
Click on Open. | Locate timeEventExample file in your system and click on Open. |
Point to File menu | You may alternatively use the File menu to open this file.
Note that timeEventExample has appeared in Libraries browser. |
Right-click >> select View class | Right-click on this icon and select View class.
|
Click on Text View | Click on third button which is the Text View.
|
/* timeEventExample */
model timeEventExample |
The name of this model is timeEventExample. |
Time event demonstration | Comments such as this, may be written on the first line of a model or class. |
Real h(unit = “m”), "Height of ball";
Real v(unit = “m/s”) “Velocity of ball”; parameter Real g(unit = “m/s2”) = -9.81 “Acceleration due to gravity”; |
h, v and g represent the same quantities as in freeFall class. |
initial equation | initial equation section contains initial conditions. |
h = 30; | Value of h at time t = 0 is 30m. |
v = 0; | Value of v at time t = 0 is 0. |
Highlight Equation | Equation signifies the beginning of equation section of this model. |
Now, let us see how to insert the constraint explained in slides. | |
Highlight if-else block | Note that there is an if-else block in equation section as shown.
|
if time >= 0.5 then | if statement places a constraint on time variable.
|
der(h) = v;
der(v) = g; |
The ball experiences free fall after time 0.5 seconds.
Hence, these two equations represent the same equations as those in freeFall class. |
else | else branch contains equations which are executed when time is less than 0.5 seconds. |
der(h) = v;
der(v) = 0; |
The ball is at rest when time is less than 0.5 seconds.
Hence, these two equations hold good in that case. |
end if; | end if signifies the end of if-else block. |
Click on simulate button | The model is now complete.
|
/* Plotting perspective */ | Plotting perspective has now opened.
Select h in the Variables browser. |
This generates h versus time plot.
Since the ball is at rest until time t=0.5 seconds, height remains the same. | |
Right-click on timeEventExample >> select Delete Result | To delete the result, right click on timeEventExample in Variables browser and select Delete Result. |
Click on Modeling perspective button. | To go back to Modeling perspective, click on Modeling button at bottom right. |
/* timeEventExample */
else der(h) = v; Delete // der(v) = 0; end if; |
Now, let me delete the second equation in else branch.
Click on Save button in the toolbar, to save this model. |
Click on Simulate. | Click on Simulate. |
The simulation has failed. | |
Hover the mouse over 'Messages browser. | Go to the Messages browser and scroll up to see the error message.
The error message says that the number of equations in each branch should be the same. Therefore, the number of equations in if and else branches should each be equal to the number of variables. |
Put equation back and click on Save | Let me insert the equation back in its place and click on Save button. |
Highlight equation | Note that der(h) = v is an equation which is common to both if and else branches.
|
Delete the if-else block | Let me show you how it is done.
Delete the if-else block first. |
The equations to be typed here have been provided in a file named if else-statement.txt. | |
These file is also available in Code Files link. | |
Since it is a text file, I have used gedit to open it. | |
/* gedit */ | Let me go to gedit.
Windows users may open this file using Notepad or any other text editor. |
der(h) = v;
der(v) = if time >= 0.5 then g else 0; |
Copy the two equations. |
/* timeEventExample */ | Go back to OMEdit.
Do a right-click and select Paste |
Click on Save. | Click on Save button in the toolbar once again. |
Click on simulate button | Click on Simulate.
Close the pop up window. |
/* Plotting perspective */
Select h in the Variables browser |
Select h in the Variables browser once again.
Note that the h versus time plot is the same as in previous case which indicates that both models are the same. |
To delete the result, right-click on timeEventExample and select Delete Result. | |
/* Switch to Modeling perspective */ | Click on Modeling button at bottom right. |
der(v) = if time >= 0.5 then g else 0; | This is an if-else statement which indicates that if time >= 0.5 dv/dt = g otherwise dv/dt = 0. |
Switch to slides. | Now let me go back to the slides. |
Slide:
Caution |
Please note that the number of equations in if branch should be equal to the number of equations in else branch.
And both of them should be equal to the number of variables in the class or model. |
Slide:
Event |
Event is an abrupt change in behavior of a system.
Events may be categorized into time events and state events. In a time event, the time at which event occurs is precisely known. timeEventExample handles a time event at t = 0.5 seconds state event occurs when a system variable cross a certain value. |
Slide:
Problem Statement |
Let us try to understand state events using an example.
A ball under free fall, faces a state event on touching the ground. The following behavior is observed when it hits the ground.
|
I have created a model named bouncingBall to simulate this behavior.
This file is available on Spoken Tutorial Website. | |
/* Switch to OMEdit */ | To view this model and simulate it, let me go back to OMEdit. |
Click on bouncingBall tab at the top of modeling area | Click on OpenModel/LIbrary File tool.
Locate bouncingBall file in your system and click on Open. |
Right-click on bouncingBall | Right-click on bouncingBall icon in Libraries browser and select View Class.
If the model does not open in Text View, go to the top of modeling area and click on Text View button. |
/* bouncingBall */
parameter Real e = 0.8 "Coefficient of restitution"; |
e is coefficient of restitution for inelastic collision of ball with Earth.
It is a parameter and its value remains constant at 0.8 throughout the simulation run. |
parameter Real radius = 0.001 "Radius of the ball in "m"; | radius represents the radius of ball in m. |
Real h(unit = “m”);
Real v(unit = “m”); parameter Real g(unit = “m/s2”) = -9.81; |
h, v and g represent the same quantities as in timeEventExample. |
der(h) = v;
der(v) = g; |
These two equations of motions have already been discussed in freeFall class. |
when h <= radius then | when statement is used to signal an event.
It is used to specify the action to be taken when an event occurs. h <= radius holds true when the ball is in contact with ground. This statement is executed whenever the event occurs. |
reinit(v, -e * pre(v)); | reinit() function is used to re-initialize a variable. |
reinit(v, -e * pre(v)); | Here, it re-initializes velocity of ball with the negative product of e and velocity of ball before touching the ground. |
/* Simulation Options */
Click on Simulation Options icon |
To simulate this model
|
/* Plotting perspective */
Select h in Variables browser |
Select h in Variables browser.
Note that h drops below zero, which is unacceptable.
|
This faulty behavior is due to numerical errors.
A discussion of numerical errors is beyond the scope of this tutorial. Please refer to Zeno behavior of bouncing ball for a better understanding. | |
De-select h | Let me de-select h. |
Switch back to slides. | Let us go back to the slides. |
Slide:
Event-related operators |
when statement is used to signal an event.
reinit(a,b) assigns value of b to variable a and resumes simulation. pre(a) returns the value of variable a before event.
|
Slide:
Assignment |
bouncingBallWithHysteresis model is available on spoken tutorial website.
Simulate bouncingBallWithHysteresis and generate h versus time plot. Notice the difference between bouncingBall and bouncingBallWithHysteresis |
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 conduct workshops using spoken tutorials
Give certificates Please contact us. |
Slide:
Forum |
If you have any questions related to this spoken tutorial, please visit the following website. |
Slide:
Textbook Companion Project |
Under Textbook Companion Project we coordinate coding of solved examples of popular books.
We give honorarium and certificates to those who do this. For more details please visit the following website. |
Slide:
Lab Migration Project |
Under Lab Migration Project we help migrate commercial simulator labs to OpenModelica.
For more information please visit the website. |
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 for joining me in this tutorial. Goodbye. |