Difference between revisions of "OpenModelica/C2/Control-flow-and-Event-handling/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
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 Windows, '''Mac OS X''' or '''FOSSEE on ARM'''.
+
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 should know the following:
+
|| To understand and practice this tutorial, you need to know the following:
  
 
* equation-based modeling of physical systems
 
* equation-based modeling of physical systems
Line 44: Line 44:
 
* class definition in Modelica
 
* class definition in Modelica
  
Pre-requisite tutorials are mentioned on our website.
+
Pre-requisite tutorials are mentioned on Spoken Tutorial website. Please go through them.
  
 
|-
 
|-
Line 50: Line 50:
  
 
'''Problem Statement'''
 
'''Problem Statement'''
||'''freeFall''' example was discussed in previous tutorials.  
+
||'''freeFall''' class was already discussed in previous tutorials.  
  
Now, let us add a constraint to '''freeFall''' class.  
+
Now, let us add a constraint to it.  
  
The ball is at rest until time '''t = 0.5''' seconds.  
+
Ball is at rest until '''t = 0.5''' seconds.  
  
Free fall under gravity starts at '''t = 0.5''' seconds
+
Free fall under gravity starts at time '''t = 0.5''' seconds
  
I have created a model '''‘timeEventExample’''' which simulates this condition.  
+
I have created a model named '''‘timeEventExample’''' which simulates this condition.  
  
 
|-
 
|-
 
||  
 
||  
||'''‘timeEventExample’''' file is provided in the Code Files link on spoken tutorial website.  
+
||'''‘timeEventExample’''' file is available in Code Files link on spoken tutorial website.  
  
Please download and save all the files provided in Code Files link.  
+
Please download and save all the files in Code Files link.  
  
 
For your convenience, '''‘freeFall’''' class is also made available.
 
For your convenience, '''‘freeFall’''' class is also made available.
 +
  
 
|-
 
|-
 
||  
 
||  
|| I have launched '''OMEdit''' on my system.  
+
|| Now let me go to OMEdit.
  
Let me switch to it.
+
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.
  
 
|-
 
|-
Line 83: Line 84:
  
 
|-
 
|-
|| Click on '''‘Open Model/Library File’''' in tool bar.
+
||  
|| To open '''‘timeEventExample’''', go to the toolbar.
+
||Note: Windows users may similarly search for OMEdit in start menu and open it.
  
Click on the tool named '''‘Open Model/Library File’'''.
+
|-
 +
|| 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’'''
 
|| Click on '''‘Open’'''
|| Locate '''‘timeEventExample’''' on your system and open it.
+
|| Locate '''‘timeEventExample’''' file in your system and click on Open.
 +
 
 +
|-
 +
||
 +
||You may alternatively use the file menu to open this file.
 +
 
 +
Note that timeEventExample had appeared in libraries browser.
 +
 
 +
|-
 +
||
 +
||Right click on this icon and select view class.
 +
 
 +
The model has now opened in Diagram 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 115:
  
 
'''model timeEventExample '''
 
'''model timeEventExample '''
|| Name of this model is '''‘timeEventExample’'''.
+
|| The Name of this model is '''‘timeEventExample’'''.
 
+
We discussed that '''‘class’''' and '''‘model’''' are synonymous.
+
  
 
|-
 
|-
 
|| “'''Time event demonstration”'''
 
|| “'''Time event demonstration”'''
|| Comments may be inserted on the first line of model as shown.
+
|| Comments such as this may be written on the first line of a model or class.
  
 
|-
 
|-
Line 110: Line 127:
  
 
'''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’''' denote the same quantities as in ‘freeFall’ class.
+
|| '''‘h’, ‘v’''' and '''‘g’''' represent the same quantities as in ‘freeFall’ class.
 
|-
 
|-
 
|| '''initial equation'''
 
|| '''initial equation'''
Line 122: Line 139:
 
|| '''v = 0;'''
 
|| '''v = 0;'''
 
|| Value of '''v''' at time '''t = 0''' is '''0'''.
 
|| Value of '''v''' at time '''t = 0''' is '''0'''.
 +
  
 
|-
 
|-
 
||  
 
||  
|| '''‘initial equation’''' section may contain differential equations as well.
+
|| Equation signifies the beginning of equation section of this model.
  
 
|-
 
|-
 
||  
 
||  
|| Now, we will see how to insert the constraint explained in slides.
+
|| Now, let us see how to insert the constraint explained in slides.
  
 
|-
 
|-
 
||  
 
||  
|| In the equation section, note that an '''‘if-else’''' block has been included.
+
|| Note that there is an '''‘if-else’''' block in equation section as shown.
  
We shall try to understand each statement of this '''‘if-else’''' block.  
+
Let us try to understand each statement of this '''‘if-else’''' block.  
  
 
|-
 
|-
 
|| '''if time >= 0.5 then'''
 
|| '''if time >= 0.5 then'''
|| This statement places a constraint on '''‘time’''' variable.
+
|| If statement places a constraint on '''‘time’''' variable.
  
 
'''‘time’''' is a built-in variable in Modelica.  
 
'''‘time’''' is a built-in variable in Modelica.  
Line 149: Line 167:
  
 
'''der(v) = g;'''
 
'''der(v) = g;'''
|| These equations are executed if time is greater than or equal to '''0.5''' seconds.
+
|| The ball experiences free fall after time '''0.5''' seconds.
 
+
The ball experiences free fall after '''0.5''' seconds.
+
  
Hence, these equations are the same as those in '''‘freeFall’''' class.
+
Hence, these two equations represent the same equations as those in '''‘freeFall’''' class.
  
 
|-
 
|-
 
|| '''else'''
 
|| '''else'''
|| '''‘else’''' block contains equations that are executed if time is less than '''0.5''' seconds.
+
|| '''‘else’''' branch contains equations which are executed when time is less than '''0.5''' seconds.
  
 
|-
 
|-
Line 163: Line 179:
  
 
'''der(v) = 0; '''
 
'''der(v) = 0; '''
|| The ball is at rest until '''0.5''' seconds. Hence, these two equations hold good in this case.  
+
|| The ball is at rest when time is less than '''0.5''' seconds. Hence, these two equations hold good in that case.  
  
 
|-
 
|-
Line 176: Line 192:
 
|| The model is now complete.
 
|| The model is now complete.
  
To simulate this model, click on simulate button in the toolbar
+
let us simulate it by clicking on simulate button.
  
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* Plotting perspective */
|| Close the pop-up window.
+
|| Plotting perspective has now opened.
  
Click on '''‘h’''' in the '''‘variables browser’'''.
+
Select '''‘h’''' in the '''‘variables browser’'''.
  
 
|-
 
|-
Line 188: Line 204:
 
|| This generates '''‘h’''' vs '''‘time’''' plot.  
 
|| This generates '''‘h’''' vs '''‘time’''' plot.  
  
The ball is at rest until '''0.5''' seconds. Hence, height remains the same till '''0.5''' seconds.
+
Since the ball is at rest until time t='''0.5''' seconds. Height remains the same.
 +
 
 +
|-
 +
||
 +
||To delete the result right click on timeEventExample in Variables browser and select Delete Result.
  
 
|-
 
|-
 
|| Click on '''Modeling perspective''' button
 
|| Click on '''Modeling perspective''' button
|| Let us switch to '''Modeling perspective'''.
+
|| To go back to '''Modeling perspective'''.
  
Click on '''Modeling perspective''' button at bottom right.
+
Click on '''Modeling''' button at bottom right.
  
 
|-
 
|-
Line 206: Line 226:
  
 
end if;
 
end if;
|| Now, let me delete '''‘der(v) = 0’''' equation present in the '''‘else’''' block.  
+
|| Now, let me delete the second equation in '''‘else’''' branch.
 +
 
 +
Click on Save button in the toolbar to save this model.
 +
 
 
|-
 
|-
 
|| Click on simulate button
 
|| Click on simulate button
|| Click on '''‘Simulate’''' button
+
|| Click on '''‘Simulate’'''
  
 
|-
 
|-
 
|| Hover the mouse over '''Messages browser''.
 
|| Hover the mouse over '''Messages browser''.
|| The simulation has failed. Go to '''Messages browser''' and scroll up.
+
|| The simulation has failed. Go to the '''Messages browser''' and scroll up to see the error message.
  
The error message says that each branch must have the same number of equations.  
+
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.
 
Therefore, the number of equations in '''‘if’''' and '''‘else’''' branches should each be equal to the number of variables.
Line 221: Line 244:
 
|-
 
|-
 
||  
 
||  
|| '''der(h) = v''' is a common equation to both '''‘if’''' and '''‘else’''' blocks.
+
||Let me insert the equation back in its place and click on save button.
 
+
Hence, we can reduce the '''‘if-else’''' block to a statement.
+
  
 
|-
 
|-
 
||  
 
||  
|| Let me delete the whole equation section starting from the ‘if time >= 0.5 then’ to ‘end if’.  
+
|| Note that '''der(h) = v''' is an equation which is commons to both '''‘if’''' and '''‘else’''' branches.  
  
 +
Hence, the '''‘if-else’''' block can be replaced by two statements.
 +
 +
 +
|-
 +
|| Let me show you how it is done.
 +
|| delete the '''‘if-else’''' block first.
 +
 +
The equation to be typed here have been provided in a file named '''‘if else-statement.txt’'''.
 +
 
|-
 
|-
 
||  
 
||  
 
|| Let us replace it with two statements.  
 
|| Let us replace it with two statements.  
  
These two statements are made available in a text file named '''‘if-else-statement.txt’'''.  
+
These file is also available in '''‘Code Files’''' link.
  
This file is provided in '''‘Code Files’''' link.
+
|-
 +
||
 +
|| Since it is a text file I have used gedit to open it.
  
 
|-
 
|-
 
|| /* gedit */
 
|| /* gedit */
|| I have opened this file in '''‘gedit’'''. Let me go to '''‘gedit’'''.
+
|| Let me go to '''‘gedit’'''.
  
Windows users may open it using '''‘Notepad’'''.
+
Windows users may open this file using '''‘Notepad’''' or any other text editor.
  
 
|-
 
|-
Line 247: Line 279:
  
 
'''der(v) = if time >= 0.5 then g else 0;'''
 
'''der(v) = if time >= 0.5 then g else 0;'''
|| Copy the two statements.
+
|| Copy the two equations.
  
 
|-
 
|-
 
|| /* timeEventExample */
 
|| /* timeEventExample */
|| Let me go back to '''OMEdit'''. Paste the two statements here.
+
|| Go back to '''OMEdit'''. Do a right click and select Paste
  
 
|-
 
|-
 
||  
 
||  
|| To save the model, click on '''‘Save’''' button.
+
|| 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 */
  
Click on '''‘h’''' in the '''‘variables browser’'''
+
Select '''‘h’''' in the '''‘variables browser’'''
 
|| The model has been simulated.  
 
|| The model has been simulated.  
  
Click on '''‘h’''' in the '''‘variables browser’'''.  
+
Select '''‘h’''' in the '''‘variables browser’''' once again.  
  
The plot of '''‘h’''' vs '''‘time’''' is the same as that in previous case.
+
Note that the '''‘h’''' vs '''‘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''' */
|| Let me go back 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 indicates that '''dv/dt = g''' if '''time >= 0.5''' otherwise '''dv/dt = 0'''.
+
|| This is an if-else statement which indicates that if '''time >= 0.5''' '''dv/dt = g''' otherwise '''dv/dt = 0'''.
  
 
|-
 
|-
 
|| Slide:
 
|| Slide:
  
CAUTION
+
|-
|| Note that the number of equations in '''‘if’''' branch = Number of equations in '''‘else’''' branch = Number of variables
+
|| Now let me go back to the slides.
 +
|| 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 Number of variables in the class or model.
  
 
|-
 
|-
Line 291: Line 328:
 
|| '''Event''' is an abrupt change in behavior of a system.
 
|| '''Event''' is an abrupt change in behavior of a system.
  
'''Events''' can be categorized as: time and state 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.
  
In '''‘timeEventExample’''', an event occurs at time t = 0.5 seconds
+
'''‘timeEventExample’''' handles a time event at t = 0.5 seconds
 +
 
 +
'''state event''' occurs when a system variable crosses certain value.
  
A '''state event''' occurs when a system variable crosses a certain value.
 
  
 
|-
 
|-
Line 303: Line 341:
  
 
Problem Statement
 
Problem Statement
|| Let us understand '''state event''' through an example.
+
||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.
  
Velocity of the ball reverts in direction.
+
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
 
Magnitude of velocity changes if the collision is inelastic
  
 
I have created a model named '''‘bouncingBall’''' to simulate this behavior.
 
I have created a model named '''‘bouncingBall’''' to simulate this behavior.
 +
 +
This file is available on Spoken Tutorial Website.
  
 
|-
 
|-
 
|| /* Switch to OMEdit */
 
|| /* Switch to OMEdit */
|| Let me go back to '''OMEdit'''.  
+
|| To view this model and simulate it, Let me go back to '''OMEdit'''.  
 +
 
  
'''‘bouncingBall’''' model is also available in '''‘Code Files’''' link  
 
  
 
|-
 
|-
 
|| Click on '''‘bouncingBall’''' tab at the top of modeling area
 
|| Click on '''‘bouncingBall’''' tab at the top of modeling area
|| Click on '''‘OpenModel/LIbrary File’''' tool. Locate '''‘bouncingBall’''' in your system and open it.
+
|| Click on '''‘OpenModel/LIbrary File’''' tool. Locate '''‘bouncingBall’''' file in your system and click on open.
  
 
|-
 
|-
 
|| Right-click on '''‘bouncingBall’'''
 
|| Right-click on '''‘bouncingBall’'''
|| Right-click on '''‘bouncingBall’''' icon and select '''‘View Class’'''.  
+
|| Right-click on '''‘bouncingBall’''' icon in libraries browser and select '''‘View Class’'''.  
  
If the model opens in '''‘Diagram View’''', click on '''‘Text View’''' button.
+
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 375:
  
 
'''parameter Real e = 0.8 "Coefficient of restitution";'''  
 
'''parameter Real e = 0.8 "Coefficient of restitution";'''  
|| '''‘e’''' is the coefficient of restitution for the collision of ball with the surface of Earth.
+
|| '''‘e’''' is the coefficient of restitution for inelastic collision of ball with Earth.
 +
 
 +
It is a parameter and its value remains constant at 0.8 through out the simulation run.
  
 
|-
 
|-
 
|| '''parameter Real radius = 0.001 "Radius of the ball in “m'";'''
 
|| '''parameter Real radius = 0.001 "Radius of the ball in “m'";'''
|| ‘radius’ represents the radius of ball in '''‘m’'''. It is equal to 0.001 meter
+
|| ‘radius’ represents the radius of ball in '''‘m’'''.
 
|-
 
|-
 
|| '''Real h(unit = “m”);'''
 
|| '''Real h(unit = “m”);'''
Line 344: Line 388:
  
 
'''parameter Real g(unit = “m/s2”) = -9.81;'''  
 
'''parameter Real g(unit = “m/s2”) = -9.81;'''  
|| '''‘h’, ‘v’''' and '''‘g’''' are the same as in ‘timeEventExample’
+
|| '''‘h’, ‘v’''' and '''‘g’''' represent the same quantities as in ‘timeEventExample’
  
 
|-
 
|-
Line 350: Line 394:
  
 
'''der(v) = g; '''
 
'''der(v) = g; '''
|| ‘These equations were discussed in '''‘freeFall’''' and '''‘timeEventExample’'''
+
|| ‘These two equations of motions have already been discussed in '''‘freeFall’''' class.
  
 
|-
 
|-
Line 360: Line 404:
 
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)); '''
Line 366: Line 411:
 
|-
 
|-
 
|| '''reinit(v, -e * pre(v)); '''
 
|| '''reinit(v, -e * pre(v)); '''
|| Here, it re-initializes velocity of ball with the negative product of ''''e'''' and velocity before touching ground.
+
|| Here, it re-initializes velocity of ball with the negative product of ''''e'''' and velocity of ball before touching the ground.
 
+
We will discuss this function in detail when we go back to slides.
+
  
 
|-
 
|-
Line 374: Line 417:
  
 
Click on '''‘Simulation Options’''' icon
 
Click on '''‘Simulation Options’''' icon
|| Let us simulate it.
+
|| To simulate this model
  
Click on '''‘Simulation Setup’''' button.  
+
Click on '''‘Simulation Setup’''' button in the toolbar.
  
Enter '''30''' in '''‘Stop Time’''' field.
+
Under General tab, change '''‘Stop Time’''' field to 30 units and
  
 
Click on '''‘Simulate’'''.
 
Click on '''‘Simulate’'''.
 +
 +
Close the pop up window.
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* Plotting perspective */
  
Click on '''‘h’''' in the '''‘variables browser’'''  
+
Select'''‘h’''' in '''‘variables browser’'''  
|| The simulation is now complete.
+
  
Click on '''‘h’''' in '''‘variables browser’'''.
+
|| Select'''‘h’''' in '''‘variables browser’'''  
  
'''‘h’''' runs into negative values which means that the simulation is faulty.
+
note that '''‘h’''' drops below zero which is unacceptable.
  
We noticed this problem with '''‘freeFall’''' class as well.
+
We have seen similar behavior while simulating '''‘freeFall’''' class.
  
 
|-
 
|-
 
||  
 
||  
|| The faulty plot of '''‘h’''' is due to numerical errors.
+
|| This faulty behavior is due to numerical errors.
 
+
Please refer to Zeno behavior of bouncing ball for a better understanding.
+
  
 
A discussion of numerical errors is beyond the scope of this tutorial.  
 
A discussion of numerical errors is beyond the scope of this tutorial.  
  
The faulty behavior is corrected in '''‘bouncingBallWithHysteresis’''' example.
+
Please refer to Zeno behavior of bouncing ball for a better understanding.
  
|-
+
Let me deselect h
|| Right-click on '''‘bouncingBall’''' in Variables Browser, Select '''‘Delete Result’'''
+
|| Right-click on '''‘bouncingBall’''' in ‘Variables Browser’ and select '''‘Delete Result’'''.
+
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides
+
|| Let us go back to the slides
  
 
|-
 
|-
Line 417: Line 457:
 
|| '''‘when’''' statement is used to signal an event.
 
|| '''‘when’''' statement is used to signal an event.
  
'''‘reinit(a,b)’''' assigns the value of '''‘b’''' to variable '''‘a’''' and resumes simulation
+
'''‘reinit(a,b)’''' assigns value of '''‘b’''' to variable '''‘a’''' and resumes simulation.
 +
 
 +
'''pre(a)''' returns the value of variable '''‘a’''' before event.
  
'''pre(a)''' returns the value of variable '''‘a’''' before an event.
+
For example, '''reinit(a, 10)''' assigns a value of '''10''' to '''‘a’''' when  event occurs.
  
For example, '''reinit(a, 10)''' assigns a value of '''10''' to '''‘a’''' whenever event occurs and resumes simulation
+
The faulty behavior of bouncingBall model is corrected in ''‘bouncingBallWithHysteresis’'''.
  
 
|-
 
|-
Line 427: Line 469:
  
 
Assignment
 
Assignment
|| As an assignment, simulate '''‘bouncingBallWithHysteresis’''' model and generate '''‘h’''' vs '''‘time’''' plot.  
+
||''‘bouncingBallWithHysteresis’''' model is available on spoken tutorial website.
  
This file is available on spoken tutorial website.
+
Simulate '''‘bouncingBallWithHysteresis’''' and generate '''‘h’''' vs '''‘time’''' plot.  
 +
 
 +
Notice the difference between ''‘bouncingBall’''' and ''‘bouncingBallWithHysteresis’'''
  
 
|-
 
|-
Line 439: Line 483:
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
|| Watch the video available at the link shown below.
+
|| 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'''
Line 459: Line 503:
  
 
Forum
 
Forum
|| If you have questions in this tutorial, please visit the webpage mentioned.
+
|| If you have questions related to this spoken tutorial, please visit the following website.
  
 
|-
 
|-
Line 465: Line 509:
  
 
Textbook Companion Project
 
Textbook Companion Project
|| We coordinate coding of solved examples from popular books.
+
|| Under Textbook Companion Project we coordinate coding of solved examples of popular books.
  
We give honorarium to contributors. Please visit our website.
+
We give honorarium and certificates to those who do this.
 +
 
 +
For more details please visit the following website.
  
 
|-
 
|-
Line 473: Line 519:
  
 
Lab Migration Project
 
Lab Migration Project
|| We help migrate labs from commercial simulators to OpenModelica.  
+
|| Under Lab Migration Project we help migrate commercial simulator labs to OpenModelica.
 +
 
 +
For more information please visit the website.
  
 
|-
 
|-
Line 485: Line 533:
  
 
Thanks
 
Thanks
|| We thank '''OpenModelica development''' team for their support.
+
|| We thank the development team of '''OpenModelica''' for their support.
  
 
|-
 
|-
 
||  
 
||  
|| Thank you for joining me in this spoken tutorial. Goodbye
+
|| Thank you for joining me in this tutorial. Goodbye.
 
|-
 
|-
 
|}
 
|}

Revision as of 12:11, 1 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:

How to use if-else statements

How to handle time and state events

How to use when statement.

Slide:

System Requirements

To record this tutorial, I am using

OpenModelica 1.9.2 and

Ubuntu Operating system version 14.04

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:
  • equation-based modeling of physical systems
  • branching in any programming language
  • class definition in Modelica

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.

Ball is at rest until t = 0.5 seconds.

Free fall under gravity starts at time t = 0.5 seconds

I have created a model named ‘timeEventExample’ which simulates this condition.

‘timeEventExample’ file is available in Code Files link on spoken tutorial website.

Please download and save all the files in Code Files link.

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 To open OMEdit on Ubuntu Operating System, click on Dash Home icon at the top left.
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.
You may alternatively use the file menu to open this file.

Note that timeEventExample had appeared in libraries browser.

Right click on this icon and select view class.

The model has now opened in Diagram view.

Click on third button which is the Text View.

We have already discussed that model and class are used synonymously in Modelica.

/* 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 30 m
v = 0; Value of v at time t = 0 is 0.


Equation signifies the beginning of equation section of this model.
Now, let us see how to insert the constraint explained in slides.
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 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.

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.
der(v) = 0; Time derivative of velocity is equal to zero as there is no net acceleration.
end if; ‘end if’ signifies the end of ‘if-else’ block
Click on simulate button The model is now complete.

let us simulate it by clicking on simulate button.

/* Plotting perspective */ Plotting perspective has now opened.

Select ‘h’ in the ‘variables browser’.

This generates ‘h’ vs ‘time’ plot.

Since the ball is at rest until time t=0.5 seconds. Height remains the same.

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 button Click on ‘Simulate’
Hover the mouse over 'Messages browser. The simulation has failed. 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.

Let me insert the equation back in its place and click on save button.
Note that der(h) = v is an equation which is commons to both ‘if’ and ‘else’ branches.

Hence, the ‘if-else’ block can be replaced by two statements.


Let me show you how it is done. delete the ‘if-else’ block first.

The equation to be typed here have been provided in a file named ‘if else-statement.txt’.

Let us replace it with two statements.

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 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’

The model has been simulated.

Select ‘h’ in the ‘variables browser’ once again.

Note that the ‘h’ vs ‘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.
Slide:
Now let me go back to the slides. 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 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 crosses 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.

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. 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 the coefficient of restitution for inelastic collision of ball with Earth.

It is a parameter and its value remains constant at 0.8 through out 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

Click on ‘Simulation Setup’ button in the toolbar.

Under General tab, change ‘Stop Time’ field to 30 units and

Click on ‘Simulate’.

Close the pop up window.

/* Plotting perspective */

Select‘h’ in ‘variables browser’

Select‘h’ in ‘variables browser’

note that ‘h’ drops below zero which is unacceptable.

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.

Please refer to Zeno behavior of bouncing ball for a better understanding.

Let me deselect h

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.

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:

Assignment

‘bouncingBallWithHysteresis’' model is available on spoken tutorial website.
Simulate ‘bouncingBallWithHysteresis’ and generate ‘h’ vs ‘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 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.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey