OpenModelica/C2/Control-flow-and-Event-handling/English

From Script | Spoken-Tutorial
Revision as of 11:15, 18 February 2016 by Kaushik Datta (Talk | contribs)

Jump to: navigation, search
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

Ubuntu Operating system version 14.04

But, this process is identical in Windows, Mac OS X or FOSSEE on ARM.

Slide:

Pre-requisites

To practice this tutorial, you should know the following:
  • equation-based modeling of physical systems
  • branching in any programming language
  • class definition in Modelica

Pre-requisite tutorials are mentioned on our website.

Slide:

Problem Statement

freeFall example was discussed in previous tutorials.

Now, let us add a constraint to freeFall class.

The ball is at rest until time t = 0.5 seconds.

Free fall under gravity starts at t = 0.5 seconds

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

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

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

For your convenience, ‘freeFall’ class is also made available.

I have launched OMEdit on my system.

Let me switch to it.

Click on Dash Home To open OMEdit on Ubuntu Operating System, click on Dash Home icon.
Type OMEdit in the Search bar and click on ‘OMEdit’ icon. Let me go back.
Click on ‘Open Model/Library File’ in tool bar. To open ‘timeEventExample’, go to the toolbar.

Click on the tool named ‘Open Model/Library File’.

Click on ‘Open’ Locate ‘timeEventExample’ on your system and open it.
/* timeEventExample */

model timeEventExample

Name of this model is ‘timeEventExample’.

We discussed that ‘class’ and ‘model’ are synonymous.

Time event demonstration” Comments may be inserted on the first line of model as shown.
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’ denote 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.
‘initial equation’ section may contain differential equations as well.
Now, we will see how to insert the constraint explained in slides.
In the equation section, note that an ‘if-else’ block has been included.

We shall try to understand each statement of this ‘if-else’ block.

if time >= 0.5 then This 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;

These equations are executed if time is greater than or equal to 0.5 seconds.

The ball experiences free fall after 0.5 seconds.

Hence, these equations are the same as those in ‘freeFall’ class.

else ‘else’ block contains equations that are executed if time is less than 0.5 seconds.
der(h) = v;

der(v) = 0;

The ball is at rest until 0.5 seconds. Hence, these two equations hold good in this 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.

To simulate this model, click on simulate button in the toolbar

/* Plotting perspective */ Close the pop-up window.

Click on ‘h’ in the ‘variables browser’.

This generates ‘h’ vs ‘time’ plot.

The ball is at rest until 0.5 seconds. Hence, height remains the same till 0.5 seconds.

Click on Modeling perspective button Let us switch to Modeling perspective.

Click on Modeling perspective button at bottom right.

/* timeEventExample */

else

 der(h) = v;

 Delete // der(v) = 0;

end if;

Now, let me delete ‘der(v) = 0’ equation present in the ‘else’ block.
Click on simulate button Click on ‘Simulate’ button
Hover the mouse over 'Messages browser. The simulation has failed. Go to Messages browser and scroll up.

The error message says that each branch must have the same number of equations.

Therefore, the number of equations in ‘if’ and ‘else’ branches should each be equal to the number of variables.

der(h) = v is a common equation to both ‘if’ and ‘else’ blocks.

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’.
Let us replace it with two statements.

These two statements are made available in a text file named ‘if-else-statement.txt’.

This file is provided in ‘Code Files’ link.

/* gedit */ I have opened this file in ‘gedit’. Let me go to ‘gedit’.

Windows users may open it using ‘Notepad’.

der(h) = v;

der(v) = if time >= 0.5 then g else 0;

Copy the two statements.
/* timeEventExample */ Let me go back to OMEdit. Paste the two statements here.
To save the model, click on ‘Save’ button.
Click on simulate button Click on Simulate.
/* Plotting perspective */

Click on ‘h’ in the ‘variables browser’

The model has been simulated.

Click on ‘h’ in the ‘variables browser’.

The plot of ‘h’ vs ‘time’ is the same as that in previous case.

/* Switch to Modeling perspective */ Let me go back to Modeling perspective.
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.
Slide:

CAUTION

Note that the number of equations in ‘if’ branch = Number of equations in ‘else’ branch = Number of variables
Slide:

Event

Event is an abrupt change in behavior of a system.

Events can be categorized as: time and state events.

In a time event, the time at which event occurs is known.

In ‘timeEventExample’, an event occurs at time t = 0.5 seconds

A state event occurs when a system variable crosses a certain value.

Slide:

Problem Statement

Let us understand state event through an example.

A ball under free fall faces a state event on touching the ground.

Velocity of the ball reverts in direction.

Magnitude of velocity changes if the collision is inelastic

I have created a model named ‘bouncingBall’ to simulate this behavior.

/* Switch to OMEdit */ 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 ‘OpenModel/LIbrary File’ tool. Locate ‘bouncingBall’ in your system and open it.
Right-click on ‘bouncingBall’ Right-click on ‘bouncingBall’ icon and select ‘View Class’.

If the model opens in ‘Diagram View’, click on ‘Text View’ button.

/* bouncingBall */

parameter Real e = 0.8 "Coefficient of restitution";

‘e’ is the coefficient of restitution for the collision of ball with the surface of Earth.
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
Real h(unit = “m”);

Real v(unit = “m”);

parameter Real g(unit = “m/s2”) = -9.81;

‘h’, ‘v’ and ‘g’ are the same as in ‘timeEventExample’
der(h) = v;

der(v) = g;

‘These equations were discussed in ‘freeFall’ and ‘timeEventExample’
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.

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 before touching ground.

We will discuss this function in detail when we go back to slides.

/* Simulation Options */

Click on ‘Simulation Options’ icon

Let us simulate it.

Click on ‘Simulation Setup’ button.

Enter 30 in ‘Stop Time’ field.

Click on ‘Simulate’.

/* Plotting perspective */

Click on ‘h’ in the ‘variables browser’

The simulation is now complete.

Click on ‘h’ in ‘variables browser’.

‘h’ runs into negative values which means that the simulation is faulty.

We noticed this problem with ‘freeFall’ class as well.

The faulty plot of ‘h’ 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.

The faulty behavior is corrected in ‘bouncingBallWithHysteresis’ example.

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
Slide:

Event-related operators

‘when’ statement is used to signal an event.

‘reinit(a,b)’ assigns the value of ‘b’ to variable ‘a’ and resumes simulation

pre(a) returns the value of variable ‘a’ before an event.

For example, reinit(a, 10) assigns a value of 10 to ‘a’ whenever event occurs and resumes simulation

Slide:

Assignment

As an assignment, simulate ‘bouncingBallWithHysteresis’ model and generate ‘h’ vs ‘time’ plot.

This file is available on spoken tutorial website.

This brings us to the end of this tutorial.
Slide:

About the Spoken Tutorial Project

Watch the video available at the link shown below.

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 in this tutorial, please visit the webpage mentioned.
Slide:

Textbook Companion Project

We coordinate coding of solved examples from popular books.

We give honorarium to contributors. Please visit our website.

Slide:

Lab Migration Project

We help migrate labs from commercial simulators to OpenModelica.
Slide:

Acknowledgements

Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India.
Slide:

Thanks

We thank OpenModelica development team for their support.
Thank you for joining me in this spoken tutorial. Goodbye

Contributors and Content Editors

Kaushik Datta, Nancyvarkey