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

From Script | Spoken-Tutorial
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 are going to learn:
  • How to create a textual model in OMEdit and simulate it.
  • How to declare variables and equations
  • How to use Simulation Setup toolbox.
Slide:

System Requirements.

To record this tutorial, I am using
  • OpenModelica 1.9.2 and
  • Ubuntu Linux Operating System 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 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.
Click on Dash Home To open OMEdit on Ubuntu Linux Operating System, click on Dash Home icon, which appears at the top left, in launcher.
Type OMEdit in the search bar >> click on OMEdit Type OMEdit in the search bar.


Click on the OMEdit icon.

On clicking OMEdit icon, you see a window such as this.


This window is known as "Welcome perspective".


OMEdit by default, opens in the "Welcome perspective"

/* Switch to modeling perspective */


Click on Modeling perspective button at the bottom right

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 now 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 more about these buttons, as we go along.

Now, we shall use ‘freeFall’ class file provided in the Code Files link on our Spoken Tutorial webpage.


Please download this file and save it 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 in Menu bar.


Click on Open Model/Library File

Locate the downloaded freeFall file Locate freeFall file that you downloaded and saved on your system and open it.
Hover over Open model/library file You may also use the tool named Open Model/Library File, that my cursor is pointing to, for opening a file.
Hover over freeFall icon in Libraries Browser Note that freeFall icon appears in the Libraries Browser.


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

Right click on freeFall icon >> select View Class Right click on freeFall icon and select View Class.
The class has now opened in Diagram view.


Do not worry if the class does not open in Diagram view.

I shall show you how to switch between different views.

Go to the top of Modeling area >> point to second button Go to the top of Modeling area.

Note that the second button stands for Diagram view.

Click on Text View button at the top of modeling area. Third button is Text View.

Click on it to switch to Text View.


The class has now opened in Text view.

Point to the first button Note that the first button stands for Icon View.


We will learn more about Icon view and Diagram view later on.

You may also create a new class named freeFall and type the required information.
Click on File >> New Modelica Class. To create a new class, go to File menu.


Select New Modelica Class.

A dialog box pops up, as shown.
Type freeFall1

In the Name field of this dialog box, type freeFall.


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

Note that two classes cannot have the same name.
Click on Specialization drop-down menu >> choose Class >> click on Ok Click on Specialization drop-down menu.

Select Class.


Click on Ok.

Point to the tool named New Modelica class A new class has been created.

You may also use the tool named New Modelica class to open a new class.

Delete annotation section Let me delete annotation section.
Click on File>> Save Now, you may type the required information here and save this class.


To save this class, go to File menu in Menu bar and click on Save.

Choose location Choose an appropriate location for this file and save it.
Now, let us understand the syntax of Modelica using freeFall class.

So switch to freeFall class.

Modelling area >> Click on freeFall tab. Go to top of Modelling area.

Click on freeFall tab.

Programs in Modelica are arranged in the form of classes.
Highlight class 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 me show you how to declare variables.

Real h(start = 30, unit = “m”) Real represents data-type.
Highlight h (start = 30, unit = “m”) h represents height of ball from surface of Earth.
Highlight start start is an attribute of Real variable.
Continue highlight Every data-type has certain attributes, which specify useful information related to variables.
Real h (start = 30, unit = “m”); start attribute specifies the initial value of a variable.


The initial value of h is 30 units.

Real h ( start = 30, unit = “m”); unit attribute specifies the unit of a variable.


The unit of h is metre.

Real h(start = 30, unit = “m”); Every variable declaration must end with a semi-colon.
Real v (start = 0, unit = “m/s”); v represents velocity of ball. It is of Real data-type.
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; grepresents acceleration due to gravity. It is of Real data-type.

And its unit is meter per second square.

Highlight parameter Real g(unit = m/s2) = -9.81; parameter is a quantity that remains constant in a simulation run.
parameter Real g(unit = m/s2) = -9.81; The value of g remains constant throughout the simulation run, with a value of 9.81.


The negative sign is due to sign convention used.

parameter Real g(unit = m/s2) = -9.81 “Acceleration due to gravity” The text in double quotes is a comment written with the declaration of g.


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

Now, let me go back to the slides.
Slide:

Variable and Parameter declaration

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


Real, Integer, Boolean and String data-types are supported in Modelica.

Highlight start and unit attributes have already been defined.


min attribute specifies the minimum value of a variable.


Similarly, max attribute specifies the maximum value of a variable.

Go back to OMEdit Let me go back to OMEdit.
/* freeFall */

equation

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

der(v) = g;

The two equations of motion for a freely falling body, as we have already discussed, have been included here.
der(h) = v; der() is Modelica function for time derivative.


Hence, der(h) represents dh/dt.

der(v) = g; And der(v) represents dv/dt.
der(v) = g; Every equation must end with a semi-colon.
Click on simulate button Let me show you how to simulate this class.

Click on simulate button in the toolbar.

Close the pop-up Close the pop-up window.
Plotting perspective This window is known as the Plotting perspective.


On successful simulation of a class, Plotting perspective opens automatically.

Variables browser Variables browser displays information related to variables and parameters of a class.
Point to Unit and Description Note that there are columns named Unit and Description.


Unit column specifies units of variables, as defined using Unit attribute.

Description column displays comments written in double quotes with variable declarations.
Click on h in the variables browser Let me show you how to generate a plot.


Select h.

This generates the plot of h with respect to time -
  • with h on y-axis
  • and time on the x-axis
By default, the simulation is run from 0 to 1 unit of time.


The unit of time depends on the system of units used for other variables.

De-select h Let me de-select h.
It is always a good practice to delete the results, once the necessary plots have been generated.
Right-click on freeFall >> select Delete result To delete this result, right-click on freeFall and select Delete result.


The result has now been deleted.

Click on Modeling perspective button at bottom right. Let me go back to Modeling perspective.


Click on Modeling button at bottom-right.

class freeFall class is used synonymously with model, in Modelica.


One may use model instead of class here, to produce the same effect.

Now, let me show you how to change the time interval for simulation.
Click on Simulation Setup button in the toolbar Click on Simulation Setup button present in the toolbar.
Locate Stop time field >> 5 Under General tab, locate Stop time field.

Change it to 5 units.

Click on Simulate >> close the pop-up Click on Simulate.

Close the pop-up window which appears.

Select h >> Plot window Let me select h once again, in the Variables browser.


This generates h v/s time plot.

Notice that the time interval has increased to 5 units.


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

We will learn how to rectify this issue in the later tutorials.
Right-click on freeFall >> select Delete result. Let me delete this result by right-clicking on freeFall and selecting Delete result.
Click on Modeling button Go back to Modeling perspective by clicking on Modeling perspective at bottom-right.
It is necessary to ensure that the number of equations is equal to the number of variables.


This class has two variables and two equations.

Select


der(h) = v;


and delete it.

Now, let me delete the first equation and simulate this class, to see what happens.
Point to star next to filename. I have deleted the first equation.


Notice that a star appears beside the name of the class in freeFall tab.

This indicates unsaved changes in the class.


Hence it is a good practice to save a class after a change has been made.

To save this class, go to the File menu and click on Save.


You may also use the Save button in toolbar, which my cursor points to, to save a file.

Click on Simulate. Now, let me simulate this class by clicking on Simulate button.
Error message pops up Note that an error message pops up in the Messages browser.


It says, there are too few equations and the model has 1 equation and 2 variables. Hence this cannot be simulated.

Let me insert the equation back in its place and click on Save button in the toolbar.
Click on Simulate button once again to simulate this class.

Note that the class simulates successfully, since the number of equations is equal to the number of variables.

Close the pop-up Close the pop-up window.
Let me go back to the slides.
Slide:

Equations in Modelica

“der()” is Modelica function for time derivative.


There is no data flow direction for equations.


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


Initial equations section is used to enter initial conditions.


We shall learn more about Initial equation later on.

Slide:

Assignment

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

About the Spoken Tutorial project

Watch the video at the following link. It summarizes the Spoken Tutorial project.
Slide:

Spoken Tutorial Workshops

We conduct workshops using spoken tutorials; give certificates.


Please contact us.

Slide:

Acknowledgements

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

Thanks

We thank the development team of OpenModelica for their support.
Thank you.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey