Difference between revisions of "OpenModelica/C2/Developing-an-equation-based-model/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 12: Line 12:
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
|| In this tutorial, we will learn to:
+
|| In this tutorial, we are going to learn:
  
* Create a textual model in '''OMEdit''' and simulate it.
+
* How to create a textual model in '''OMEdit''' and simulate it.
 +
* How to declare variables and equations
 +
* How to use '''Simulation Setup''' toolbox.
  
* Declare variables and equations
 
 
* Use '''Simulation Setup''' toolbox.
 
 
|-
 
|-
 
|| Slide:
 
|| Slide:
Line 24: Line 23:
 
'''System Requirements'''.
 
'''System Requirements'''.
 
|| To record this tutorial, I am using  
 
|| To record this tutorial, I am using  
 
 
* '''OpenModelica 1.9.2 ''' and
 
* '''OpenModelica 1.9.2 ''' and
 +
* '''Ubuntu Linux Operating System 14.04'''
  
* '''Ubuntu Linux OS 14.04'''
 
  
 
But, this process is identical in '''Windows, Mac OS X '''or''' FOSSEE OS on ARM'''.
 
But, this process is identical in '''Windows, Mac OS X '''or''' FOSSEE OS on ARM'''.
Line 35: Line 33:
  
 
Pre-requisites
 
Pre-requisites
|| To understand this tutorial, you should need to know
+
|| To understand this tutorial, you need to know
 
+
 
* equation-based modeling of physical systems.
 
* equation-based modeling of physical systems.
  
Line 43: Line 40:
  
 
'''Problem Statement'''
 
'''Problem Statement'''
|| Let us simulate the motion of a ball of mass '''`m'''', which is under free fall due to gravity.
+
|| 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 height of the ball from the surface of Earth is represented by variable '''h'''.
  
  
The velocity of the ball is represented by variable '''‘v’'''.
+
The velocity of the ball is represented by variable '''v'''.
  
  
‘Acceleration due to gravity’ is represented by '''‘g’''' and is assumed to be constant.
+
'''Acceleration due to gravity''' is represented by '''g''' and is assumed to be constant.
  
  
Line 61: Line 58:
  
 
'''Freefall equations'''
 
'''Freefall equations'''
|| The equations of motion for a freely falling body, are as follows:
+
|| The '''equations of motion''' for a freely falling body, are as follows:
  
  
'''“dh/dt = v”''' '''“dv/dt = g”'''
+
'''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'''.
+
 
 +
The value of '''h''' at time '''t = 0''' is '''30''' m and the value of '''v''' at time '''t = 0''' is '''0'''.
  
 
|-
 
|-
Line 74: Line 73:
  
 
|-
 
|-
||  
+
|| Click on '''Dash Home'''
|| To open '''OMEdit '''on '''Ubuntu Linux OS''', click on '''Dash Home '''icon at the top left in the launcher.  
+
|| 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'''.
  
Type ‘OMEdit’ in the search bar.
 
  
 +
Click on the '''OMEdit''' icon.
  
Now click on the '''‘OMEdit’''' icon.
+
|-
 +
||
 +
||On clicking '''OMEdit''' icon, you see a window such as this.
  
  
Please go through pre-requisite tutorials for more information on '''Ubuntu Linux '''operating system.
+
This window is known as '''"Welcome perspective"'''.
  
|-
+
 
||
+
'''OMEdit''' by default, opens in the '''"Welcome perspective"'''
|| Let me go back to OMEdit.
+
  
 
|-
 
|-
Line 94: Line 97:
  
  
Click on the Modeling perspective button at the bottom right corner of OMEdit window.
+
Click on '''Modeling perspective''' button at the bottom right
|| '''‘OMEdit’''' by default, opens in the '''‘Welcome’''' perspective.  
+
|| At the bottom right-hand corner, you can locate buttons for '''‘Welcome’, ‘Modeling’''' and '''‘Plotting’ perspectives'''.
  
At the bottom right-hand corner, you can locate buttons for '''‘Welcome’, ‘Modeling’''' and '''‘Plotting’''' perspectives.
 
  
 +
Let me click on''' ‘Modeling perspective’'''.
  
Let me click on''' ‘Modeling''' perspective’.
+
|-
 
+
||
 
+
||'''‘Modeling perspective’''' has now opened.
'''‘Modeling''' perspective’ has opened
+
  
 
|-
 
|-
|| Move cursor over the area enclosed between Libraries Browser on the left.
+
|| Move cursor over the area enclosed between '''Libraries Browser''' on the left >> '''Messages Browser''' appears at the bottom >> Model file tabs on the top
  
Messages Browser appears at the bottom.
 
  
model file tabs on the top
 
 
|| I will be referring to the area between  
 
|| I will be referring to the area between  
  
* Libraries Browser on the left,  
+
* '''Libraries Browser''' on the left,  
  
* Messages Browser at the bottom and
+
* '''Messages Browser''' at the bottom and
  
* Toolbar on the top, as the '''modeling area'''.
+
* '''Toolbar''' on the top, as the '''modeling area'''.
  
 
|-
 
|-
|| Move cursor over the toolbar present above modeling area
+
|| Move cursor over the '''toolbar''' present above modeling area
|| The toolbar has buttons related to '''file operations, graphical view''' and '''simulation'''.  
+
|| The '''toolbar''' has buttons related to '''file operations, graphical view''' and '''simulation'''.  
  
  
We will learn about them as we go along.
+
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 spoken tutorial webpage.  
+
|| Now, we shall use''' ‘freeFall’ class''' file provided in the '''Code Files''' link on our '''Spoken Tutorial webpage'''.  
  
  
Please download and save this file on your system.
+
Please download this file and save it on your system.
  
 
|-
 
|-
 
|| /* Opening a new class file and switching views */
 
|| /* Opening a new class file and switching views */
  
Go to File Open Model/Library File
+
Go to '''File''' >> '''Open Model/Library File'''
|| To open this '''class''', go to the '''‘File’''' menu present in menu bar.
+
|| To open this '''class''', go to the '''File''' menu 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 the downloaded '''freeFall''' file
 +
|| Locate '''freeFall''' file that you downloaded and saved on your system and open it.
  
Click on ‘'''Open Model/Library File'''
+
|-
 +
|| 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'''
|| Locate '''‘freeFall’''' file that you downloaded and saved in your system and open it.
+
|| Note that '''freeFall''' icon appears in the '''Libraries Browser'''.
 +
 
 +
 
 +
'''Libraries Browser''' shows all '''classes''' that are loaded in a session of '''OMEdit'''.
  
 
|-
 
|-
|| Hover over ‘Open model/library file’
+
|| Right click on '''freeFall''' icon >> select '''View Class'''
|| You may also use the ‘'''Open Model/Library File'''’ button that my cursor points to, for opening a file.
+
|| Right click on '''freeFall''' icon and select '''View Class'''.
 +
 
 
|-
 
|-
|| Hover over ‘freeFall’ icon in ‘'''Libraries Browser'''’
+
||  
|| Note that '''‘freeFall’''' icon has now appeared in '''Libraries Browser'''.  
+
|| The '''class''' has now opened in '''Diagram''' view.  
  
  
'''Libraries Browser'''’ shows all classes that are loaded in a session of '''OMEdit'''.
+
Do not worry if the '''class''' does not open in '''Diagram''' view.
|-
+
 
|| Right click on ‘freeFall’ icon and select ‘View Class’
+
I shall show you how to switch between different views.
|| Right click on '''‘freeFall’''' and select '''‘View Class’'''.
+
 
 
|-
 
|-
|| Go to the top of the modeling area
+
||Go to the top of ''' Modeling area''' >> point to second button
|| The class has now opened in '''Diagram''' view.  
+
||Go to the top of ''' Modeling area'''.
 +
 
 +
Note that the second button stands for '''Diagram''' view.
  
Go to the top of the''' modeling area'''.
 
 
|-
 
|-
|| Click on ‘Text View’ button at the top of modeling area.
+
|| Click on '''Text View''' button at the top of modeling area.
|| Click on the third button which is the ‘'''Text View'''.  
+
|| Third button is '''Text View'''.  
  
You may see buttons for ‘'''Icon View'''’ and ‘'''Diagram View'''’ as well.  
+
Click on it to switch to '''Text View'''.
  
  
We shall discuss more about '''icon '''and '''diagram views '''later on.
+
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. Let me show you how it is done.
+
|| You may also create a new '''class''' named '''freeFall''' and type the required information.  
  
 
|-
 
|-
|| File New Modelica Class.
+
|| Click on File >> New Modelica Class.
|| Go to '''File''' menu. Click on '''‘New Modelica Class'''
+
|| To create a new '''class''', go to '''File''' menu.  
 +
 
 +
 
 +
Select '''New Modelica Class'''.
  
 
|-
 
|-
|| Type // '''freeFall'''
+
||
|| A dialog box opens as shown. Type '''‘freeFall’''' in the ‘Name’ field of dialog box.
+
|| 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 since ‘freeFall’ class is already open in ‘OMEdit’.
+
I am using a different name '''freeFall1''' since '''freeFall class''' is already open in '''OMEdit'''.
  
 
|-
 
|-
|| Click on ‘Specialization’ drop-down menu. Choose ‘Model’
+
||
|| Click on '''‘Specialization’''' drop-down menu.  
+
||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'''.  
  
Choose '''‘Class’'''.
 
  
We shall learn more about class specialization later on.
+
Click on '''Ok'''.
  
Click on '''‘Ok’'''.
 
 
|-
 
|-
|| Delete // '''annotation'''
+
|| Point to the '''tool''' named '''New Modelica class'''
|| New class has now opened.  
+
|| A new '''class''' has been created.  
  
Delete annotation section. We shall learn about annotation later on.  
+
You may also use the '''tool''' named '''New Modelica class''' to open a new '''class'''.
  
Type the required information and save the class.
+
|-
 +
||Delete '''annotation''' section
 +
||Let me delete '''annotation section'''.  
  
 
|-
 
|-
|| File Save
+
|| Click on '''File'''>> '''Save'''
|| To save the class, go to '''‘File’''' menu and click on '''‘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 a location for your file and click on '''‘Save’'''. The file is now saved.  
+
|| Choose an appropriate location for this file and save it.
  
 
|-
 
|-
 
||  
 
||  
|| At the top of modeling area, click on ‘freeFall’ tab.
+
|| Now, let us understand the syntax of '''Modelica''' using '''freeFall class'''.
 +
 
 +
So switch to '''freeFall class'''.
  
 
|-
 
|-
||  
+
||Modelling area >> Click on '''freeFall''' tab.
|| Let us understand the syntax of '''Modelica''' using '''‘freeFall’''' example.  
+
||Go to top of '''Modelling area'''.
  
 +
Click on '''freeFall''' tab.
  
Programs in '''Modelica''' are arranged in the form of '''classes'''.  
+
|-
 +
||
 +
||Programs in '''Modelica''' are arranged in the form of '''classes'''.  
  
 
|-
 
|-
 
|| Highlight '''class freeFall'''
 
|| Highlight '''class freeFall'''
 +
 +
|| The first line of a '''class''' defines its name.
  
  
end freeFall;
+
The name of this '''class '''is '''freeFall'''.
|| The first line of a '''class''' defines its name. The name of this '''class '''is '''‘freeFall’'''.
+
  
 
|-
 
|-
 
|| Highlight '''end freeFall;'''
 
|| Highlight '''end freeFall;'''
|| Every '''class''' must have an '''‘end’ '''statement to indicate where the '''class '''ends.
+
|| 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'''.
+
|| This '''class '''has '''variable declarations '''and '''equations'''.  
 +
 
 +
 
 +
Let me show you how to declare '''variables'''.
  
 
|-
 
|-
 
|| '''Real''' h(start = 30, unit = “m”)  
 
|| '''Real''' h(start = 30, unit = “m”)  
|| '''Real'''’ signifies a '''data-type'''.  
+
|| '''Real''' represents '''data-type'''.  
  
 
|-
 
|-
 
|| Highlight '''h''' (start = 30, unit = “m”)  
 
|| Highlight '''h''' (start = 30, unit = “m”)  
|| '''‘h’''' stands for the height of ball from surface of Earth.
+
|| '''h''' represents height of ball from surface of Earth.
  
 
|-
 
|-
||  
+
||Highlight '''start'''
|| Every '''data-type''' has certain attributes.  
+
||'''start''' is an attribute of '''Real variable'''.
  
 
+
|-
They are used to specify information related to '''variables '''of that '''type'''.
+
|| Continue highlight
 +
|| Every '''data-type''' has certain attributes, which specify useful information related to '''variables'''.
  
 
|-
 
|-
 
|| Real h ('''start = 30''', unit = “m”);
 
|| Real h ('''start = 30''', unit = “m”);
|| Initial conditions are required to simulate differential equations with respect to time.  
+
|| '''start''' attribute specifies the initial value of a '''variable'''.  
  
  
'''‘start’ attribute '''is used to specify value of a variable at time t = 0. Here, it is 30 units.
+
The initial value of '''h''' is 30 units.  
 
+
 
+
The default value of '''‘start’ attribute '''is '''zero '''for '''Real variables.'''
+
  
 
|-
 
|-
 
|| Real h ( start = 30, '''unit = “m”''');
 
|| Real h ( start = 30, '''unit = “m”''');
|| '''‘unit’ attribute''' specifies the unit of the '''variable'''. The unit of''' ‘h’''' is '''‘metre’'''.  
+
|| '''unit attribute''' specifies the unit of a '''variable'''.  
  
  
'''‘unit’ attribute''' helps the user to keep track of units. Note that '''Modelica''' doesn’t have a system of units in place.
+
The unit of '''h''' is '''metre'''.  
  
 
|-
 
|-
|| Real h(start = 30, unit = “m”) '''<nowiki>;</nowiki>'''
+
|| Real h(start = 30, unit = “m”); '''
 
|| Every '''variable declaration''' must end with a '''semi-colon.'''
 
|| Every '''variable declaration''' must end with a '''semi-colon.'''
  
 
|-
 
|-
 
|| '''Real v''' (start = 0, unit = “m/s”);
 
|| '''Real v''' (start = 0, unit = “m/s”);
|| '''‘v’''' represents the '''velocity''' of the ball. It is a '''real variable.'''
+
|| '''v''' represents '''velocity''' of ball. It is of '''Real data-type.'''
  
 
|-
 
|-
 
|| Real v '''(start = 0, unit = “m/s”);'''
 
|| Real v '''(start = 0, unit = “m/s”);'''
|| The initial value of '''v’ '''is zero. its unit is '''‘meter per second’.'''
+
|| The initial value of '''v''' is zero.   Its unit is '''meter per second.'''
  
 
|-
 
|-
 
|| parameter '''Real g''' (unit = m/s2) = -9.81;  
 
|| parameter '''Real g''' (unit = m/s2) = -9.81;  
|| '''‘g’ '''signifies '''acceleration due to gravity'''. It is of '''real data-type.'''  
+
|| '''g'''represents '''acceleration due to gravity'''. It is of '''Real data-type.'''  
  
|-
+
And its unit is '''meter per second square.'''
|| 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;
 
|| Highlight '''parameter''' Real g(unit = m/s2) = -9.81;
|| A''' ‘parameter’ '''remains constant throughout a simulation run.
+
|| '''parameter''' is a quantity that remains constant in a simulation run.
  
 
|-
 
|-
 
|| parameter Real g(unit = m/s2) = '''-9.81;'''
 
|| parameter Real g(unit = m/s2) = '''-9.81;'''
|| The value of '''‘g’ '''is equal to 9.81. Negative sign is due to sign convention.  
+
|| 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”'''  
 
|| parameter Real g(unit = m/s2) = -9.81 '''“Acceleration due to gravity”'''  
|| The text in double quotes is a '''comment'''.  
+
|| The text in double quotes is a '''comment''' written with the declaration of '''g'''.  
  
  
'''Comments '''provide information about the program. They are also useful for documentation.  
+
'''Comments '''provide useful information about the program. They are also useful for documentation.  
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| Now, let me go back to the slides.
  
 
|-
 
|-
Line 315: Line 357:
  
 
Variable and Parameter declaration  
 
Variable and Parameter declaration  
|| '''‘parameter’''' is a quantity that remains constant in a simulation run.
+
|| '''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.'''
+
'''Real, Integer, Boolean''' and''' String data-types''' are supported in '''Modelica'''.
  
 +
|-
 +
||Highlight
 +
||'''start''' and '''unit attributes''' have already been defined.
  
'''‘start’''' and '''‘unit’ attributes''' have already been discussed.
 
  
 +
'''min attribute''' specifies the minimum value of a '''variable'''.
  
'''‘min’ attribute''' is used to specify the minimum value.
 
  
 
+
Similarly, '''max attribute''' specifies the maximum value of a '''variable'''.  
'''‘max’ '''is used to specify the maximum value of a variable.
+
 
+
 
+
We shall discuss other attributes later on.
+
  
 
|-
 
|-
||  
+
|| Go back to '''OMEdit'''
|| Let us go back to '''OMEdit.'''
+
|| Let me go back to '''OMEdit.'''
  
 
|-
 
|-
Line 340: Line 380:
  
 
'''equation'''  
 
'''equation'''  
|| '''‘equation’ '''signifies the beginning of equation section of a '''class'''.
+
|| '''‘equation’ '''signifies the beginning of '''equation section''' of a '''class'''.
  
 
|-
 
|-
|| /'''<nowiki>* Equations of motion */ </nowiki>'''
+
|| /* Equations of motion */
|| This is another way of inserting comments.  
+
|| This is an alternative way of inserting '''comments'''.  
  
 
|-
 
|-
Line 350: Line 390:
  
 
'''der(v) = g;'''
 
'''der(v) = g;'''
|| Note that the two equations of motion have been included here.
+
|| The two '''equations of motion''' for a freely falling body, as we have already discussed, have been included here.
  
 
|-
 
|-
 
|| '''der(h)''' = '''v''';
 
|| '''der(h)''' = '''v''';
|| '''‘der()''' represents the '''time derivative''' of a '''variable'''.
+
|| '''der()''' is '''Modelica function''' for '''time derivative'''.
  
  
Line 361: Line 401:
 
|-
 
|-
 
|| '''der(v)''' = '''g''';
 
|| '''der(v)''' = '''g''';
|| '''der(v)''' represents '''dv/dt.'''
+
|| And '''der(v)''' represents '''dv/dt.'''
  
 
|-
 
|-
|| '''der(v) = g''' '''<nowiki>;</nowiki>'''
+
|| '''der(v) = g''';
|| Every equation ends with a '''semi-colon'''.
+
|| Every '''equation''' must end with a '''semi-colon'''.
  
 
|-
 
|-
|| Click on ‘simulate’ button
+
|| Click on '''simulate''' button
|| Let us simulate this '''class'''.  
+
|| Let me show you how to simulate this '''class'''.  
  
So click on '''‘simulate’''' button in the toolbar.
+
Click on '''simulate''' button in the '''toolbar'''.
  
 
|-
 
|-
||  
+
|| Close the pop-up
|| Close the dialog box which pops up.
+
|| Close the pop-up window.
  
 
|-
 
|-
 
|| Plotting perspective
 
|| Plotting perspective
|| This window is known as plotting perspective.
+
|| This window is known as the '''Plotting perspective'''.
  
Plotting perspective opens on simulation of a '''class'''.
 
  
 
+
On successful simulation of a '''class, Plotting perspective''' opens automatically.
You may switch from '''‘modeling'''’ to '''‘plotting’''' '''perspective''' using '''‘plotting’''' button at the bottom right.
+
  
 
|-
 
|-
 
|| Variables browser
 
|| Variables browser
|| The ‘'''variables browser'''’ shows all '''variables''' and '''parameters''' of a '''class'''.
+
|| '''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'''.
  
Let me scroll right.
 
  
 +
'''Unit''' column specifies '''units''' of '''variables,''' as defined using '''Unit''' attribute.
  
There are columns named '''‘Unit’''' and '''‘Description’'''.
+
|-
 +
||
 +
||'''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'''.
  
Units of variables appear under ‘Unit’ column. They are specified using '''‘unit’''' attributes.
 
  
 +
Select '''h'''.
  
Description column displays comments written in double quotes.
+
|-
 +
||
 +
||This generates the plot of '''h with respect to time''' -
 +
* with '''h''' on '''y-axis'''
 +
* and '''time''' on the '''x-axis'''
  
 
|-
 
|-
|| Click on ‘h’ in the ‘variables browser’
+
||
|| Let me scroll left.
+
||By default, the simulation is run from 0 to 1 unit of time.  
  
  
Click on''' ‘h’ '''in the''' ‘variables browser’.'''
+
The unit of '''time''' depends on the system of '''units''' used for other variables.
  
 +
|-
 +
|| De-select '''h'''
 +
|| Let me de-select '''h.'''
  
This generates '''‘h’ vs ‘time’ plot.'''
+
|-
 +
||
 +
||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'''.
  
By default, the simulation runs from 0 to 1 unit of time.
 
  
 
+
The result has now been deleted.
'''‘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.
+
  
 
|-
 
|-
||  
+
|| Click on '''Modeling perspective''' button at bottom right.
|| Let me deselect''' ‘h’.'''
+
|| Let me go back to '''Modeling perspective'''.
  
|-
 
|| Click on Modeling perspective button at bottom right.
 
|| Let us go back to '''Modeling''' perspective.
 
  
Click on Modeling perspective button at bottom right.
+
Click on '''Modeling''' button at bottom-right.
  
 
|-
 
|-
 
|| '''class''' freeFall  
 
|| '''class''' freeFall  
|| In '''Modelica, ‘class’''' is used synonymously with '''‘model’'''.  
+
|| '''class''' is used synonymously with '''model,''' in '''Modelica'''.  
  
  
You may use ‘model’ instead of ‘class’.
+
One may use '''model''' instead of '''class''' here, to produce the same effect.
  
 
|-
 
|-
|| Click on ‘Simulation Setup’ button in the toolbar
+
||  
|| Let us change the default simulation settings.
+
|| 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.
  
Click on '''‘Simulation Setup’''' button present in the toolbar.
+
Change it to 5 units.  
  
 
|-
 
|-
|| ‘Simulation Setup’ toolbox
+
||Click on '''Simulate''' >> close the pop-up
|| Under the '''General''' tab, locate''' ‘stop time’''' field and change it to 5 units.  
+
||Click on '''Simulate'''.
  
 +
Close the pop-up window which appears.
  
You may observe options for changing integration method etc.
+
|-
 +
|| Select '''h''' >> Plot window
 +
|| Let me select '''h''' once again, in the '''Variables browser'''.
  
  
Click on '''‘Simulate’''' button.
+
This generates '''h''' v/s '''time''' plot.
  
 
|-
 
|-
|| Plotting Perspective
+
||
|| Close the dialog box. Click on''' ‘h’''' in the '''‘variables browser’.'''
+
||Notice that the time interval has increased to 5 units.
  
  
The time interval has increased from 1 unit 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.
  
But, the value of '''‘h’''' has gone below zero which is unacceptable.
+
|-
 +
||Right-click on '''freeFall''' >> select '''Delete result'''.
 +
||Let me delete this result by right-clicking on '''freeFall''' and selecting '''Delete result'''.
  
 
+
|-
We shall see in the later tutorials how to rectify this issue.
+
|| Click on '''Modeling''' button
 +
|| Go back to '''Modeling''' perspective by clicking on '''Modeling perspective''' at bottom-right.
  
 
|-
 
|-
|| Click on ‘Modeling’ button
+
||
|| Let me deselect''' ‘h’ '''and go back to '''Modeling''' perspective.
+
|| It is necessary to ensure that the number of equations is equal to the number of variables.  
  
  
Click on ‘Modeling’ perspective button at bottom right.
+
This '''class''' has two variables and two equations.  
  
 
|-
 
|-
Line 483: Line 549:
  
 
and delete it.
 
and delete it.
|| Ensure that the number of variables is equal to the number of equations.  
+
||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'''.
  
In this case, there are two variables and two equations.
 
  
 +
Notice that a star appears beside the name of the '''class''' in '''freeFall''' tab.
  
Let me delete the first equation: '''“der(h) = v”''' to see what happens.
+
This indicates unsaved changes in the '''class'''.  
  
  
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.
+
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'''.
  
To save this '''class, '''click on '''‘Save’''' button in the toolbar.
 
  
 +
You may also use the '''Save''' button in '''toolbar''', which my cursor points to, to save a file.
  
Notice that the star has now disappeared.
+
|-
 +
||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'''.
  
Click on '''‘Simulate’'''.
 
  
 
+
It says, there are too few equations and the model has 1 '''equation''' and 2 '''variables'''. Hence this cannot be simulated.
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 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.
 
|-
 
|-
 
||  
 
||  
Line 517: Line 602:
  
 
Equations in Modelica
 
Equations in Modelica
|| '''“der()”''' represents '''time derivative''' in '''Modelica'''
+
|| '''“der()”''' is '''Modelica''' function for  '''time derivative'''.
  
  
There is no data flow direction for equations.
+
There is no data flow direction for '''equations'''.
  
  
For example, '''der(h) = v''' may be written as '''v = der(h)'''.
+
For example, '''der(h) = v''' may also be written as '''v = der(h)'''.
  
  
Initial equations can be declared by '''‘initial equation’''' section.  
+
'''Initial equations section''' is used to enter initial conditions.
  
We shall learn more about''' ‘initial equation’''' section later on.
+
 
 +
We shall learn more about '''Initial equation''' later on.
  
 
|-
 
|-
Line 534: Line 620:
  
 
Assignment
 
Assignment
|| As an assignment, write a model to simulate the differential equation
+
|| As an assignment, write a '''model''' to simulate the '''differential equation'''
  
 
* '''dx/dt = -a*x '''
 
* '''dx/dt = -a*x '''
 
* where '''a = 1''',  
 
* where '''a = 1''',  
* '''x''' is a real variable
+
* '''x''' belongs to '''R'''
* and the value of x at time '''t=0''' is '''5''' units.
+
* and the value of x at time '''t=0''' is '''5'''
  
 
|-
 
|-
Line 549: Line 635:
  
 
About the Spoken Tutorial project
 
About the Spoken Tutorial project
|| The video at the following link summarises the Spoken Tutorial project.
+
|| Watch the video at the following link.  It summarizes the Spoken Tutorial project.
  
Please download and watch it.
 
 
|-
 
|-
 
|| Slide:
 
|| Slide:
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
|| We conduct workshops using spoken tutorials and give certificates to those who pass an online test.
+
|| We conduct workshops using spoken tutorials; give certificates.
  
  
Line 565: Line 650:
  
 
Acknowledgements
 
Acknowledgements
|| Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
+
|| '''Spoken Tutorial project''' is funded by NMEICT, MHRD, Government of India.
  
 
|-
 
|-
Line 571: Line 656:
  
 
Thanks
 
Thanks
|| We thank OpenModelica team for their support.
+
|| We thank the development team of '''OpenModelica''' for their support.
 +
 
 
|-
 
|-
 
||  
 
||  
|| Thank you! This is Bhargava signing off.
+
|| Thank you.
 
|-
 
|-
 
|}
 
|}

Latest revision as of 18:00, 24 February 2016

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