OpenModelica/C2/Developing-an-equation-based-model/English

From Script | Spoken-Tutorial
Revision as of 15:31, 16 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 Developing an equation based model.
Slide:

Learning Objectives

In this tutorial, we will learn to:
  • Create a textual model in OMEdit and simulate it.
  • Declare variables and equations
  • Use Simulation Setup toolbox.
Slide:

System Requirements.

To record this tutorial, I am using
  • OpenModelica 1.9.2 and
  • Ubuntu Linux OS 14.04

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

Slide:

Pre-requisites

To understand this tutorial, you should need to know
  • equation-based modeling of physical systems.
Slide:

Problem Statement

Let us simulate the motion of a ball of mass `m', which is under free fall due to gravity.


The height of the ball from the surface of Earth is represented by variable ‘h’.


The velocity of the ball is represented by variable ‘v’.


‘Acceleration due to gravity’ is represented by ‘g’ and is assumed to be constant.


Variables directed away from the surface of Earth, are considered positive.

Slide:

Freefall equations

The equations of motion for a freely falling body, are as follows:


“dh/dt = v” “dv/dt = g”


The value of h at time t = 0 is 30 m and the value of v at time t = 0 is 0.

/* Switch to OMEdit */ Let me go to OMEdit. I have already launched it on my system.
To open OMEdit on Ubuntu Linux OS, click on Dash Home icon at the top left in the launcher.


Type ‘OMEdit’ in the search bar.


Now click on the ‘OMEdit’ icon.


Please go through pre-requisite tutorials for more information on Ubuntu Linux operating system.

Let me go back to OMEdit.
/* Switch to modeling perspective */


Click on the Modeling perspective button at the bottom right corner of OMEdit window.

‘OMEdit’ by default, opens in the ‘Welcome’ perspective.

At the bottom right-hand corner, you can locate buttons for ‘Welcome’, ‘Modeling’ and ‘Plotting’ perspectives.


Let me click on ‘Modeling perspective’.


‘Modeling perspective’ has opened

Move cursor over the area enclosed between Libraries Browser on the left.

Messages Browser appears at the bottom.

model file tabs on the top

I will be referring to the area between
  • Libraries Browser on the left,
  • Messages Browser at the bottom and
  • Toolbar on the top, as the modeling area.
Move cursor over the toolbar present above modeling area The toolbar has buttons related to file operations, graphical view and simulation.


We will learn about them as we go along.

Now, we shall use ‘freeFall’ class file provided in the Code Files link on spoken tutorial webpage.


Please download and save this file on your system.

/* Opening a new class file and switching views */

Go to File → Open Model/Library File

To open this class, go to the ‘File’ menu present in menu bar.


‘File’ menu has options for creating a new class, opening a model/library and saving a class.


Click on ‘Open Model/Library File

Locate ‘freeFall’ file that you downloaded and saved in your system and open it.
Hover over ‘Open model/library file’ You may also use the ‘Open Model/Library File’ button that my cursor points to, for opening a file.
Hover over ‘freeFall’ icon in ‘Libraries Browser Note that ‘freeFall’ icon has now appeared in ‘Libraries Browser’.


Libraries Browser’ shows all classes that are loaded in a session of OMEdit.

Right click on ‘freeFall’ icon and select ‘View Class’ Right click on ‘freeFall’ and select ‘View Class’.
Go to the top of the modeling area The class has now opened in Diagram view.

Go to the top of the modeling area.

Click on ‘Text View’ button at the top of modeling area. Click on the third button which is the ‘Text View’.

You may see buttons for ‘Icon View’ and ‘Diagram View’ as well.


We shall discuss more about icon and diagram views later on.

You may also create a new class named ‘freeFall’ and type the required information. Let me show you how it is done.
File → New Modelica Class. Go to File menu. Click on ‘New Modelica Class
Type // freeFall A dialog box opens as shown. Type ‘freeFall’ in the ‘Name’ field of dialog box.

I am using a different name since ‘freeFall’ class is already open in ‘OMEdit’.

Click on ‘Specialization’ drop-down menu. Choose ‘Model’ Click on ‘Specialization’ drop-down menu.

Choose ‘Class’.

We shall learn more about class specialization later on.

Click on ‘Ok’.

Delete // annotation New class has now opened.

Delete annotation section. We shall learn about annotation later on.

Type the required information and save the class.

File → Save To save the class, go to ‘File’ menu and click on ‘Save’.
Choose a location for your file and click on ‘Save’. The file is now saved.
At the top of modeling area, click on ‘freeFall’ tab.
Let us understand the syntax of Modelica using ‘freeFall’ example.


Programs in Modelica are arranged in the form of classes.

Highlight class freeFall


end freeFall;

The first line of a class defines its name. The name of this class is ‘freeFall’.
Highlight end freeFall; Every class must have an ‘end’ statement to indicate where the class ends.
This class has variable declarations and equations. Let us see how to declare variables.
Real h(start = 30, unit = “m”) Real’ signifies a data-type.
Highlight h (start = 30, unit = “m”) ‘h’ stands for the height of ball from surface of Earth.
Every data-type has certain attributes.


They are used to specify information related to variables of that type.

Real h (start = 30, unit = “m”); Initial conditions are required to simulate differential equations with respect to time.


‘start’ attribute is used to specify value of a variable at time t = 0. Here, it is 30 units.


The default value of ‘start’ attribute is zero for Real variables.

Real h ( start = 30, unit = “m”); ‘unit’ attribute specifies the unit of the variable. The unit of ‘h’ is ‘metre’.


‘unit’ attribute helps the user to keep track of units. Note that Modelica doesn’t have a system of units in place.

Real h(start = 30, unit = “m”) ; Every variable declaration must end with a semi-colon.
Real v (start = 0, unit = “m/s”); ‘v’ represents the velocity of the ball. It is a real variable.
Real v (start = 0, unit = “m/s”); The initial value of ‘v’ is zero. its unit is ‘meter per second’.
parameter Real g (unit = m/s2) = -9.81; ‘g’ signifies acceleration due to gravity. It is of real data-type.
parameter Real g (unit = m/s2) // = -9.81; The unit of ‘g’ is meter per second square.
Highlight parameter Real g(unit = m/s2) = -9.81; A ‘parameter’ remains constant throughout a simulation run.
parameter Real g(unit = m/s2) = -9.81; The value of ‘g’ is equal to 9.81. Negative sign is due to sign convention.
parameter Real g(unit = m/s2) = -9.81 “Acceleration due to gravity” The text in double quotes is a comment.


Comments provide information about the program. They are also useful for documentation.

Let me go back to the slides.
Slide:

Variable and Parameter declaration

‘parameter’ is a quantity that remains constant in a simulation run.


Modelica supports ‘Real, Integer, Boolean and String’ data-types. They are also known as built-in types.


‘start’ and ‘unit’ attributes have already been discussed.


‘min’ attribute is used to specify the minimum value.


‘max’ is used to specify the maximum value of a variable.


We shall discuss other attributes later on.

Let us go back to OMEdit.
/* freeFall */

equation

‘equation’ signifies the beginning of equation section of a class.
/* Equations of motion */ This is another way of inserting comments.
der(h) = v;

der(v) = g;

Note that the two equations of motion have been included here.
der(h) = v; ‘der()’ represents the time derivative of a variable.


Hence, der(h) represents dh/dt.

der(v) = g; der(v) represents dv/dt.
der(v) = g ; Every equation ends with a semi-colon.
Click on ‘simulate’ button Let us simulate this class.

So click on ‘simulate’ button in the toolbar.

Close the dialog box which pops up.
Plotting perspective This window is known as plotting perspective.

Plotting perspective opens on simulation of a class.


You may switch from ‘modeling’ to ‘plotting’ perspective using ‘plotting’ button at the bottom right.

Variables browser The ‘variables browser’ shows all variables and parameters of a class.


Let me scroll right.


There are columns named ‘Unit’ and ‘Description’.


Units of variables appear under ‘Unit’ column. They are specified using ‘unit’ attributes.


Description column displays comments written in double quotes.

Click on ‘h’ in the ‘variables browser’ Let me scroll left.


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


This generates ‘h’ vs ‘time’ plot.


By default, the simulation runs from 0 to 1 unit of time.


‘Time’ is a built-in variable in Modelica. It always appears in ‘variables browser’.


The unit of time depends on the system of equations used.

Let me deselect ‘h’.
Click on Modeling perspective button at bottom right. Let us go back to Modeling perspective.

Click on Modeling perspective button at bottom right.

class freeFall In Modelica, ‘class’ is used synonymously with ‘model’.


You may use ‘model’ instead of ‘class’.

Click on ‘Simulation Setup’ button in the toolbar Let us change the default simulation settings.


Click on ‘Simulation Setup’ button present in the toolbar.

‘Simulation Setup’ toolbox Under the General tab, locate ‘stop time’ field and change it to 5 units.


You may observe options for changing integration method etc.


Click on ‘Simulate’ button.

Plotting Perspective Close the dialog box. Click on ‘h’ in the ‘variables browser’.


The time interval has increased from 1 unit to 5 units.


But, the value of ‘h’ has gone below zero which is unacceptable.


We shall see in the later tutorials how to rectify this issue.

Click on ‘Modeling’ button Let me deselect ‘h’ and go back to Modeling perspective.


Click on ‘Modeling’ perspective button at bottom right.

Select


der(h) = v;


and delete it.

Ensure that the number of variables is equal to the number of equations.


In this case, there are two variables and two equations.


Let me delete the first equation: “der(h) = v” to see what happens.


Go to the ‘freeFall’ tab. Notice that a star appears beside its name. This indicates any unsaved changes. If it is not saved, the previous version of this class will be simulated.


To save this class, click on ‘Save’ button in the toolbar.


Notice that the star has now disappeared.


Click on ‘Simulate’.


An error message pops up in ‘Messages browser’. It says that there are too few equations. ‘Messages browser’ displays any error messages during simulation.

Let me insert the equation back in its place and click on Save.
Let me go back to the slides.
Slide:

Equations in Modelica

“der()” represents time derivative in Modelica


There is no data flow direction for equations.


For example, der(h) = v may be written as v = der(h).


Initial equations can be declared by ‘initial equation’ section.

We shall learn more about ‘initial equation’ section later on.

Slide:

Assignment

As an assignment, write a model to simulate the differential equation
  • dx/dt = -a*x
  • where a = 1,
  • x is a real variable
  • and the value of x at time t=0 is 5 units.
This brings us to the end of this tutorial.
Slide:

About the Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Slide:

Spoken Tutorial Workshops

We conduct workshops using spoken tutorials and give certificates to those who pass an online test.


Please contact us.

Slide:

Acknowledgements

Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
Slide:

Thanks

We thank OpenModelica team for their support.
Thank you! This is Bhargava signing off.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey