Difference between revisions of "OpenModelica/C3/Modelica-Packages/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by one other user not shown)
Line 7: Line 7:
  
 
'''Title Slide'''
 
'''Title Slide'''
|| Welcome to the Spoken Tutorial on '''Packages''' as a part of the spoken tutorial series on '''OpenModelica'''.
+
|| Welcome to the Spoken Tutorial on '''Packages'''.
  
 
|-
 
|-
Line 15: Line 15:
 
|| In this tutorial, we are going to learn:
 
|| In this tutorial, we are going to learn:
  
* How to create a package of classes in '''Modelica'''.
+
* How to create a '''package''' of '''classes''' in '''Modelica'''.
  
* How to reference classes in a '''package'''.
+
* How to reference '''classes''' in a '''package'''.
  
* How to import a '''package'''.
+
* How to import a '''package''' and
  
 
* How to use '''Modelica Library'''.
 
* How to use '''Modelica Library'''.
Line 29: Line 29:
 
|| 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''' and
 
* '''gedit'''.
 
* '''gedit'''.
  
'''Windows''' users may use '''Notepad''' instead of '''gedit'''.
+
'''Windows''' users may use '''Notepad''' or any other text editor instead of '''gedit'''.
  
 
|-
 
|-
Line 39: Line 39:
  
 
'''Prerequisites'''
 
'''Prerequisites'''
|| To practise this tutorial, you need
+
|| To understand and practice this tutorial, you need
  
 
* Knowledge of '''class''' and '''type definition''' in '''Modelica'''.
 
* Knowledge of '''class''' and '''type definition''' in '''Modelica'''.
Line 50: Line 50:
  
 
'''Package'''
 
'''Package'''
|| '''Package''' is a specialized class in '''Modelica'''.
+
|| A '''Package''' is a specialized '''class''' in '''Modelica'''.
  
 
It is a collection of '''classes'''.  
 
It is a collection of '''classes'''.  
  
'''Classes''' here may mean '''models, functions, types''' or any specialized '''classes'''.
+
It can be stored as a '''single file''' or a '''directory'''.
  
'''package''' can be stored as a single file.
 
  
It can also be stored as a directory.
+
We will first learn about '''single file storage'''.
 
+
We will first learn about single file storage.
+
  
 
|-
 
|-
Line 66: Line 63:
  
 
'''Single file storage'''
 
'''Single file storage'''
|| All classes of a package may be stored in a single file with '''.mo''' extension.
+
|| In '''single file storage''' all '''classes''' belonging to a '''package''' are written in a '''single file'''.
  
It is not recommended as the code may get lengthy in a few cases.
+
It is not recommended as it may get lengthy in a few cases.
  
 
|-
 
|-
Line 74: Line 71:
  
 
Click on '''Modeling''' button at bottom right
 
Click on '''Modeling''' button at bottom right
|| Let me switch to '''OMEdit''' to demonstrate single file storage for a package.
+
|| Now let me switch to '''OMEdit''' to demonstrate '''single file storage''' for a '''package'''.
  
 
|-
 
|-
Line 94: Line 91:
 
* '''spokenTutorialExamples.mo''',  
 
* '''spokenTutorialExamples.mo''',  
  
* '''bouncingBallWithUserTypes''',
+
* '''bouncingBallWithUserTypes.mo''' and
  
 
* '''bouncingBallWithImport'''.  
 
* '''bouncingBallWithImport'''.  
  
Note that I have not selected the folder '''spokenTutorialExamples'''.
+
 
 +
You may also open each of them individually.
 +
 
 +
 
 +
Note that I have not selected '''spokenTutorialExamples''' folder.
 +
 
 +
 
 +
We shall discuss more about it when we look at directory storage.
 +
 
 +
 
 +
|-
 +
||Click '''Open'''.
 +
||Click on '''Open'''.
 +
 
 +
You may see that '''spokenTutorialExamples''' package is now seen in '''Libraries Browser'''.
  
 
|-
 
|-
 
||  
 
||  
|| Double click on '''spokenTutorialExamples''' in '''Libraries Browser'''.
+
|| Double click on '''spokenTutorialExamples''' icon.
  
 
|-
 
|-
Line 112: Line 123:
  
 
'''package spokenTutorialExamples '''
 
'''package spokenTutorialExamples '''
|| The first line defines the name of package.
+
|| Now let me discuss '''spokenTutorialExamples'''
  
Evidently, name of this package is '''spokenTutorialExamples'''.
+
First line defines the name of a '''package'''.  
  
Let me scroll down a little.
+
Evidently, name of this package is '''spokenTutorialExamples'''.
  
 
|-
 
|-
Line 128: Line 139:
  
 
//
 
//
|| Note that this package consists of  
+
|| This package consists of
  
* '''freeFall'''
+
 
* '''bouncingBall'''
+
*'''freefall class'''
 +
* '''bouncingBall''' model and
 
* '''bouncingBallWithUserTypes''' models.
 
* '''bouncingBallWithUserTypes''' models.
 
All three models were discussed in prerequisite tutorials.
 
 
Let me scroll down.
 
  
 
|-
 
|-
 
|| '''end spokenTutorialExamples;'''
 
|| '''end spokenTutorialExamples;'''
|| The '''end''' statement indicates where a package ends.
+
|| '''end statement''' defines where the package ends.
  
 
|-
 
|-
Line 146: Line 154:
 
|| We have viewed all '''classes''' and '''models''' of this package at one place.  
 
|| We have viewed all '''classes''' and '''models''' of this package at one place.  
  
Now, let us learn how to view individual classes in a package.
+
Now, let us learn how to view individual ''classes'' in a package.
  
 
|-
 
|-
 
|| Click on the (+) button with '''spokenTutorialExamples''' in '''Libraries Browser'''.
 
|| Click on the (+) button with '''spokenTutorialExamples''' in '''Libraries Browser'''.
|| Click on the (+) button beside '''spokenTutorialExamples''' in '''Libraries Browser'''.  
+
|| Click on the (+) button besides '''spokenTutorialExamples''' icon in '''Libraries Browser'''.  
  
 
This displays the names of '''classes''' present in this package.
 
This displays the names of '''classes''' present in this package.
Line 158: Line 166:
 
|| Double-click on '''freeFall''' in '''Libraries Browser'''.  
 
|| Double-click on '''freeFall''' in '''Libraries Browser'''.  
  
'''freeFall''' class has now opened.  
+
'''freeFall class''' has now opened.  
  
Individual classes of a package may be simulated.  
+
Individual '''classes''' of a package may be simulated.  
  
Click on '''simulate''' button.
+
But the package itself can not be simulated.
 
+
Note that the class has simulated.
+
 
+
Go back to '''modeling view'''.
+
 
+
Similarly, other classes of the package may be viewed and simulated.
+
  
 
|-
 
|-
|| Click on '''spokenTutorialExamples''' tab.
+
|| Let me go back to '''spokenTutorialExamples''' tab.
|| Click on '''spokenTutorialExamples''' tab.
+
|| Let me go back to '''spokenTutorialExamples''' tab.
  
 
|-
 
|-
 
|| Hover over '''Simulate''' button in toolbar.
 
|| Hover over '''Simulate''' button in toolbar.
|| Note that '''Simulate''' icon doesn’t appear in toolbar.
+
|| Note that '''Simulate''' button doesn’t appear in the toolbar which indicates that this package cannot be simulated.
 
+
A package cannot be simulated.
+
  
 
|-
 
|-
 
||  
 
||  
|| Let us now unload '''spokenTutorialExamples''' from OMEdit.  
+
|| Now let me unload '''spokenTutorialExamples''' from OMEdit.  
  
Do a right-click on '''spokenTutorialExamples''' in '''Libraries Browser''' and select '''unload'''.
+
Do a right-click and select '''unload'''.
 +
 
 +
Select '''Yes'''.
  
 
This is done so as to avoid conflict when demonstrating directory storage.
 
This is done so as to avoid conflict when demonstrating directory storage.
Line 190: Line 192:
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| Now let me go back to the slides.
  
 
|-
 
|-
Line 196: Line 198:
  
 
'''Directory storage'''
 
'''Directory storage'''
|| '''Packages''' may also be stored as directories as we’ve already discussed.
+
|| In '''Directory storage classes''' are stored in separate files.
 
+
In this case, classes are stored in separate files.
+
  
 
Name of directory is same as that of the package.
 
Name of directory is same as that of the package.
  
A file named '''package.mo''' needs to be included in the directory.
+
A file named '''package.mo''' is to be included in the directory and
  
Each file in the directory starts with a '''within''' statement.  
+
Each '''class''' starts with a '''within statement'''.  
  
 
|-
 
|-
 
||  
 
||  
|| Now, let me demonstrate the example folder '''spokenTutorialExamples''' that you’ve downloaded.  
+
|| Now, let me demonstrate the folder '''spokenTutorialExamples''' that you’ve downloaded.  
  
 
Note that this folder represents the same package as that we have already discussed.  
 
Note that this folder represents the same package as that we have already discussed.  
 
We shall understand more about directory storage using both '''OMEdit''' and '''gedit'''.
 
  
 
|-
 
|-
Line 224: Line 222:
 
|| Double-click on '''spokenTutorialExamples''' folder.  
 
|| Double-click on '''spokenTutorialExamples''' folder.  
  
Notice that it has following files:  
+
Notice that the folder has following files:  
  
 
* '''package.mo''',  
 
* '''package.mo''',  
Line 230: Line 228:
 
* '''freeFall.mo''',  
 
* '''freeFall.mo''',  
  
* '''bouncingBall.mo''',
+
* '''bouncingBallWithUserTypes''' and
  
* '''bouncingBallWithUserTypes.mo'''.
+
* '''bouncingBall'''.
 +
 
 +
We shall understand more about directory storage using both '''OMEdit''' and '''gedit'''.
  
 
|-
 
|-
 
|| /* Directory */
 
|| /* Directory */
|| '''package.mo''' indicates that this folder represents a package.  
+
|| Note that '''package.mo''' indicates that this folder represents a package.  
  
Without this file, the folder doesn’t represent a Modelica package.
+
Without this file, the folder doesn’t represent a '''Modelica package'''.  
 
+
Apart from this file, this package has models named
+
 
+
* '''freeFall''',
+
 
+
* '''bouncingBall''' and
+
 
+
* '''bouncingBallWithUserTypes'''.
+
  
 
|-
 
|-
Line 256: Line 248:
 
|| Now, let me switch to '''OMEdit''' to demonstrate directory storage.
 
|| Now, let me switch to '''OMEdit''' to demonstrate directory storage.
  
Press '''Ctrl + O'''. Navigate to the '''spokenTutorialExamples''' folder.  
+
Press '''Ctrl + O'''.  
 +
 
 +
Navigate to the '''spokenTutorialExamples''' folder that you have downloaded.
  
Select the '''package.mo''' file from this folder.
+
Select '''package.mo''' from this folder and click on '''Open'''.
  
 
|-
 
|-
 
|| Double-click on '''spokenTutorialExamples''' in '''Libraries Browser'''.
 
|| Double-click on '''spokenTutorialExamples''' in '''Libraries Browser'''.
|| You can see '''spokenTutorialExamples''' package in '''Libraries Browser'''.  
+
|| '''spokenTutorialExamples''' package can now be seen in '''Libraries Browser'''.  
  
 
Double-click on '''spokenTutorialExamples''' icon.
 
Double-click on '''spokenTutorialExamples''' icon.
Line 268: Line 262:
 
|-
 
|-
 
|| /* spokenTutorialExamples */
 
|| /* spokenTutorialExamples */
|| Switch to '''Text View''' if it opens in '''Icon/Diagram View'''.
+
|| Open the package in '''Text View''' if it opens in either '''Icon/Diagram View'''.
  
 
|-
 
|-
Line 274: Line 268:
  
 
'''Scroll down'''
 
'''Scroll down'''
|| If you scroll down, you will notice that the package looks same as single-file storage.
+
|| If you scroll down, you may notice that this package is the same as that we have seen in '''single file storage'''.
  
 
|-
 
|-
 
||  
 
||  
 
|| The difference between single file and directory storage can only be understood when we open this '''package''' using a text editor like '''gedit'''.  
 
|| The difference between single file and directory storage can only be understood when we open this '''package''' using a text editor like '''gedit'''.  
 +
 +
You may use '''Notepad''' or any other text editor if you are using '''Windows'''.
  
 
|-
 
|-
 
|| Right-click on '''spokenTutorialExamples''' and select '''unload'''.
 
|| Right-click on '''spokenTutorialExamples''' and select '''unload'''.
|| Unload '''spokenTutorialExamples'''.
+
|| Now let me unload '''spokenTutorialExamples''' package from 'OMEdit'''.
  
 
|-
 
|-
Line 288: Line 284:
  
 
Double-click on '''bouncingBall.mo''', '''bouncingBallWithUserTypes ,freeFall, package.mo'''.
 
Double-click on '''bouncingBall.mo''', '''bouncingBallWithUserTypes ,freeFall, package.mo'''.
|| Let me go to '''spokenTutorialExamples''' folder that you’ve downloaded and open all the files using '''gedit'''.
+
|| Switch to '''spokenTutorialExamples''' folder and open all the files using '''gedit'''.
 +
 
 +
Now all the files are open in '''gedit'''.
 +
 
 +
Let us take a look at '''package.mo''' first.
  
 
|-
 
|-
 
|| '''within;'''  
 
|| '''within;'''  
|| '''within''' indicates that this file relates to a '''package'''.
+
|| '''within statement''' indicates that this file represents a '''package'''.
  
 
|-
 
|-
 
|| '''package spokenTutorialExamples'''
 
|| '''package spokenTutorialExamples'''
 
|| Name of this package is '''spokenTutorialExamples'''.  
 
|| Name of this package is '''spokenTutorialExamples'''.  
 +
 +
Note that the name of folder was '''spokenTutorialExamples'''  as well.
  
 
|-
 
|-
 
|| '''end spokenTutorialExamples;'''
 
|| '''end spokenTutorialExamples;'''
|| '''package.mo''' file can only contain the '''within''' statement and package declaration.
+
|| '''package.mo''' file can only contain the '''within statement''' and '''package''' declaration.
  
 
|-
 
|-
|| Click on '''freeFall''' tab.
+
|| Switch to '''freeFall''' tab.
|| Click on '''freeFall''' tab.
+
|| Let me switch to '''freeFall''' tab.
  
 
|-
 
|-
Line 310: Line 312:
  
 
'''within spokenTutorialExamples;'''
 
'''within spokenTutorialExamples;'''
|| This indicates that this model belongs to '''spokenTutorialExamples''' package.  
+
|| This '''within statement''' indicates that '''freefall class''' belongs to '''spokenTutorialExamples''' package.  
  
Rest of this file has information specific to '''freeFall''' class.
+
Rest of this file has information specific to '''freeFall class'''.
  
 
|-
 
|-
 
||  
 
||  
|| You may see that other models in this package namely '''bouncingBallWithUserTypes , bouncingBall''' follow a similar syntax.  
+
|| You may see that other models in this package namely '''bouncingBallWithUserTypes''' and '''bouncingBall''' follow a similar syntax as well.  
  
But, we didn’t observe '''within''' statement when we opened the package in '''OMEdit'''.  
+
But, we didn’t observe '''within statement''' when we opened the package in '''OMEdit'''.  
  
This indicates that '''within''' statement helps OMEdit recognise the file structure.  
+
This indicates that '''within statement''' helps '''OMEdit''' recognize the file structure.  
  
Hence, it is omitted by OMEdit while displaying the '''package'''.
+
Hence, it is omitted by '''OMEdit''' while displaying the '''package'''.
  
 
|-
 
|-
Line 332: Line 334:
  
 
'''Modelica Library'''
 
'''Modelica Library'''
|| '''Modelica Library''' is an open-source package.
+
|| We shall now understand more about '''Modelica Library'''.
 +
 
 +
*'''Modelica Library''' is an open-source package.
  
'''OMEdit''' loads this library for every session.  
+
*'''OMEdit''' automatically loads it for every session.  
  
It can be located in the '''Libraries Browser'''.
+
*It can be seen in '''Libraries Browser'''.
  
It has classes from mechanical, electrical and thermal domains.
+
*It has '''classes''' from '''mechanical, electrical''' and '''thermal''' domains.
  
'''Classes''' from this library can be referenced and used.
+
'''Classes''' of this library can be referenced and used.
  
 
|-
 
|-
 
||  
 
||  
|| Let me switch to '''OMEdit''' to demonstrate '''Modelica Library'''.
+
|| Now let me switch to '''OMEdit''' to demonstrate '''Modelica Library'''.
  
 
|-
 
|-
 
|| Click on '''(plus) Modelica '''
 
|| Click on '''(plus) Modelica '''
|| Expand '''Modelica''' in the '''Libraries Browser''' using the (plus) button.  
+
|| Locate '''Modelica''' icon in '''Libraries Browser''' and expand it.
  
This library has packages named '''Blocks , Complex Blocks''' etc.  
+
Note that '''Modelica Library''' has packages named '''Blocks, Complex Blocks''' etc.  
  
Note that a package may further contain packages as well.
+
You may note that a package may further contain packages as well as is evident here.
  
 
|-
 
|-
 
|| Click '''on (plus) SIunits'''
 
|| Click '''on (plus) SIunits'''
|| Of special mention is the '''SIunits''' package.  
+
|| Of special mention, is the '''SIunits''' package.  
  
Click on (plus) button beside '''SIunits''' for expanding it.  
+
Expand it.  
  
 
|-
 
|-
 
|| Click on '''(minus) SIunits'''
 
|| Click on '''(minus) SIunits'''
|| Note that there are type definitions available for quantities like Angle, Length etc.  
+
|| This package has type definitions for physical quantities like '''Angle, Length, Position''' etc.  
  
We shall now understand how to use classes from '''SIunits''' package using '''bouncingBallWithImport''' model.  
+
We shall see how to use these type definitions using '''bouncingBallWithImport class'''.  
  
 
Compress '''SIunits'''.
 
Compress '''SIunits'''.
Line 370: Line 374:
 
|-
 
|-
 
|| Double-click on '''bouncingBallWithUserTypes''' and '''bouncingBallWithImport''' icons in '''Libraries Browser'''.
 
|| Double-click on '''bouncingBallWithUserTypes''' and '''bouncingBallWithImport''' icons in '''Libraries Browser'''.
|| Scroll down in '''Libraries Browser'''.
+
|| And double-click on '''bouncingBallWithImport''' and '''bouncingBallWithUserTypes'''.
 
+
Double-click on '''bouncingBallWithUserTypes''' and '''bouncingBallWithImport'''.
+
  
 
|-
 
|-
Line 380: Line 382:
 
|-
 
|-
 
||  
 
||  
|| Let me discuss '''bouncingBallWithUserTypes''' first.  
+
|| We shall first look at '''bouncingBallWithUserTypes'''.  
  
This model was discussed in prerequisite tutorials.
+
We have learnt about this model in the prerequisite tutorials.
  
 
|-
 
|-
Line 392: Line 394:
 
|| This model has type definitions named '''Length''' and '''Velocity'''.
 
|| This model has type definitions named '''Length''' and '''Velocity'''.
  
'''SIunits''' package from '''Modelica Library''' also has these type definitions.
+
We can instead use the type definition in '''SIunits''' instead of defining them explicitly in every model.
  
It saves time to use these type definitions instead of explicitly defining them in every class.
+
We shall see how to do that using '''bouncingBallWithImport''' model.
  
 
|-
 
|-
 
|| /* bouncingBallWithImport */
 
|| /* bouncingBallWithImport */
|| Click on '''bouncingBallWithImport''' tab.
+
|| Let me switch to '''bouncingBallWithImport'''.
  
 
|-
 
|-
 
|| '''Modelica.SIunits.Length '''h;
 
|| '''Modelica.SIunits.Length '''h;
|| A '''class''' in a package is referenced using '''dot'''.  
+
|| A '''class''' in a package is referenced to using '''dot'''.  
  
'''Modelica.SIunits''' references '''SIunits''' package which belongs to '''Modelica''' library.  
+
'''Modelica.SIunits''' refers to '''SIunits''' package which belongs to '''Modelica''' library.  
  
 
|-
 
|-
 
|| Modelica.SIunits.Length''' h'''<nowiki>;</nowiki>
 
|| Modelica.SIunits.Length''' h'''<nowiki>;</nowiki>
|| Variable '''h''' is declared to be of '''Length''' type defined in '''SIunits''' package.
+
|| Variable '''h''' is declared to be of '''Length''' type which is defined in '''SIunits''' package.
  
 
|-
 
|-
 
|| '''Modelica.SIunits.Velocity v;'''
 
|| '''Modelica.SIunits.Velocity v;'''
|| Similarly, variable '''v''' is declared to be of '''Velocity''' type in '''SIunits''' package.
+
|| Similarly, variable '''v''' is declared to be of '''Velocity''' type which is defined in '''SIunits''' package.
  
 
|-
 
|-
 
||  
 
||  
|| parameter '''radius''' and '''g''' are similarly defined.
+
|| Note that parameters '''radius''' and '''g''' are declared in a similar fashion.
  
|-
+
Now let me '''Simulate''' this model.
|| Click on '''Simulate''' button.  
+
  
Select '''h''' in '''Variables Browser'''.
+
Click on '''Simulate''' button in the toolbar.
  
|| Now, let me simulate this class.  
+
Close the pop up window.
  
Click on '''simulate''' button at the top.  
+
Select '''h''' in the '''Variables Browser'''.
  
You may observe that the output is the same as in the case of '''bouncingBallWithUserTypes'''.
+
|-
 +
||
 +
|| Note that the plot obtained is similar to the one seen in the case of '''bouncingBallWithUserTypes'''.
 +
 
 +
let me de-select '''h'''.
 +
 
 +
Delete the result.
 +
 
 +
Switch to '''Modeling''' perspective.
  
 
|-
 
|-
Line 433: Line 442:
 
|| It is tedious to use the full name of a class every time.  
 
|| It is tedious to use the full name of a class every time.  
  
This can be simplified using '''import''' statement.  
+
This can be simplified using '''import statement'''.  
  
Now, we shall type a few statements that show the use of '''import'''.  
+
Now, we shall type a few '''statements''' that show the use of '''import'''.  
  
The statements to be typed are provided in text file named '''import-statements.txt'''.
 
  
Go to the location where you saved the files on your system and double-click on '''import-statements.txt'''.
+
The '''statements''' to be typed are provided in a text file named '''import-statements.txt'''.
 +
 
 +
Go to the location where you saved it on your system.
 +
 
 +
Double-click on '''import-statements.txt'''.
 +
 
 +
'''Windows''' users may use '''Notepad''' to open this file.
  
 
|-
 
|-
Line 453: Line 467:
  
 
'''Velocity v''';
 
'''Velocity v''';
|| Let me switch to '''gedit'''.  
+
|| The file is now open in '''gedit'''.
  
Click on '''import-statements.txt''' tab.
+
Copy all the statements using '''Ctrl+C''' or by doing right-click.
 
+
Copy the statements.
+
  
 
|-
 
|-
Line 473: Line 485:
 
|| Switch to '''OMEdit'''.  
 
|| Switch to '''OMEdit'''.  
  
Paste the statements in '''bouncingBallWithImport''' model.  
+
Paste all the '''statements''' at the beginning of the model.  
  
 
|-
 
|-
Line 479: Line 491:
  
 
'''Modelica.SIunits.Velocity''' //
 
'''Modelica.SIunits.Velocity''' //
|| Delete the declaration statements for '''Length''' and '''Velocity'''.  
+
|| Delete the '''declaration statements''' for '''Length''' and '''Velocity,''' which have been defined before.
  
Save the model by pressing '''Ctrl + S'''.
+
Delete the extra spaces.
 +
 
 +
 
 +
Save this model by pressing '''Ctrl + S'''.
 +
 
 +
Now the model is complete and ready for simulation.
  
 
|-
 
|-
 
|| Click on '''Simulate''' button.
 
|| Click on '''Simulate''' button.
|| Let me simulate this class.  
+
|| Click on '''Simulate''' button to simulate it.
  
Click on '''Simulate''' button.
+
Close the pop up window.
  
 
|-
 
|-
|| Select '''h''' in '''Variables Browser''' and Click on '''Modeling''' button.
+
|| Select '''h''' in the '''Variables Browser''' and Click on '''Modeling''' button.
|| Note that the output is same as in previous case.  
+
|| Select '''h''' in the '''Variables Browser'''.
 +
 
 +
Note that the plot is same as in previous case.
 +
 
 +
De-select '''h''' and delete the result.
  
 
Switch to '''Modeling''' perspective.
 
Switch to '''Modeling''' perspective.
Line 503: Line 524:
  
 
//
 
//
|| In this case, Modelica looks up for '''Length''' and '''Velocity''' type definitions in the path mentioned in '''import''' statements.  
+
|| Now let us now try to understand the '''import''' statements.
 +
 
 +
Using the '''import statements''', '''Modelica''' looks up for '''Length''' and '''Velocity''' type definitions using the path mentioned here.
  
 
This saves the trouble of referring to the package every time.
 
This saves the trouble of referring to the package every time.
Line 517: Line 540:
  
 
//
 
//
|| Let me use one '''import''' statement instead of two.
+
|| Now let us see how we can replace these two '''import statements''' by a single '''import statement'''.
  
Delete the two import statements.
+
Delete the two '''statements'''.
  
 
|-
 
|-
Line 525: Line 548:
  
 
'''import Modelica.SIunits.*;'''
 
'''import Modelica.SIunits.*;'''
|| Type '''import (space) Modelica (dot) SIunits (dot) asterisk'''.
+
|| And type '''import (space) Modelica (dot) SIunits (dot) asterisk (semicolon)'''
 +
 
 +
Save this model by pressing '''Ctrl + S'''  
  
 
|-
 
|-
 
||  
 
||  
|| This is known as wild-card import.  
+
|| This '''statement''' is known as a '''wild-card import'''.
  
This way, any class from '''SIunits''' may be accessed.
+
This way, any '''class''' from '''SIunits''' may be accessed without explicitly mentioning it.
  
 
|-
 
|-
|| Click on '''simulate'''.
+
|| Click on '''Simulate'''.
|| Save the model by pressing '''Ctrl + S''' and click on '''Simulate''' button.
+
|| Now let me '''Simulate''' this model.
 +
 
 +
Click on '''Simulate''' button.
 +
 
 +
Close the pop up window.
  
 
|-
 
|-
 
|| Select '''h'''
 
|| Select '''h'''
|| Select '''h''' in '''Variables Browser'''.  
+
|| Select '''h''' in the '''Variables Browser'''.  
  
Note that the result is same as in previous case.
+
You may notice the similarity of plot once again.
 +
 
 +
De-select '''h''' and delete the result.
  
 
|-
 
|-
 
|| Click on '''Modeling'''.
 
|| Click on '''Modeling'''.
|| Delete the result and switch to '''Modeling perspective'''.  
+
|| Go back to '''Modeling perspective'''.  
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| Now let me switch to the slides.
 
+
|-
+
||
+
|| This brings us to the end of this tutorial.
+
  
 
|-
 
|-
Line 560: Line 587:
 
||As an assignment,  
 
||As an assignment,  
  
Declare variable '''h''' and '''v''' of '''freeFall''' class as '''Length''' and '''Velocity''' types respectively.
+
Declare variable '''h''' and '''v''' of '''freeFall class''' as '''Length''' and '''Velocity''' types respectively.
  
These type definitions can be found in '''SIunits''' package of Modelica library.
+
These type definitions can be found in '''SIunits''' package of '''Modelica''' library.
 +
 
 +
|-
 +
||
 +
|| This brings us to the end of this tutorial.
  
 
|-
 
|-
Line 574: Line 605:
 
It summarises the '''Spoken Tutorial project'''.
 
It summarises the '''Spoken Tutorial project'''.
  
If you do not have good bandwidth, you can download and watch it.
+
If you have questions in this '''Spoken Tutorial''', please visit the website shown.
 
+
|-
+
|| Slide:
+
 
+
'''Spoken Tutorial Workshops'''
+
|| We conducts workshops using spoken tutorials.
+
 
+
Give certificates.
+
 
+
Please contact us.
+
 
+
|-
+
|| Slide:
+
 
+
'''Forum'''
+
|| If you have questions in this tutorial, please visit the webpage mentioned.
+
  
 
|-
 
|-
Line 607: Line 622:
 
'''Lab Migration Project'''
 
'''Lab Migration Project'''
 
|| We help migrate labs from commercial simulators to '''OpenModelica'''.
 
|| We help migrate labs from commercial simulators to '''OpenModelica'''.
 +
 +
Please visit the following website.
  
 
|-
 
|-
Line 618: Line 635:
  
 
'''Thanks'''
 
'''Thanks'''
|| We thank '''OpenModelica''' development team for their support.
+
|| We thank the development team of '''OpenModelica''' for their support.
  
 
|-
 
|-
 
||  
 
||  
|| I would like to thank you for joining me in this tutorial.
+
|| I would like to thank you for joining me in this tutorial.
 +
 
 +
Good bye.

Latest revision as of 14:11, 5 April 2016

Visual Cue Narration
Slide:

Title Slide

Welcome to the Spoken Tutorial on Packages.
Slide:

Learning Objectives

In this tutorial, we are going to learn:
  • How to create a package of classes in Modelica.
  • How to reference classes in a package.
  • How to import a package and
  • How to use Modelica Library.
Slide:

System Requirements

To record this tutorial, I am using
  • OpenModelica 1.9.2 and
  • Ubuntu Operating System version 14.04 and
  • gedit.

Windows users may use Notepad or any other text editor instead of gedit.

Slide:

Prerequisites

To understand and practice this tutorial, you need
  • Knowledge of class and type definition in Modelica.
  • Prerequisite tutorials are mentioned on our website.

Please go through them.

Slide:

Package

A Package is a specialized class in Modelica.

It is a collection of classes.

It can be stored as a single file or a directory.


We will first learn about single file storage.

Slide:

Single file storage

In single file storage all classes belonging to a package are written in a single file.

It is not recommended as it may get lengthy in a few cases.

/* OMEdit */

Click on Modeling button at bottom right

Now let me switch to OMEdit to demonstrate single file storage for a package.
Please download and save all the files available on our website.

You may see that there is a file and a folder bearing the name spokenTutorialExamples.

Please download both of them.

Now, let me open the necessary files in OMEdit.

Press Ctrl + O.

Go to the appropriate location on your system and select

  • spokenTutorialExamples.mo,
  • bouncingBallWithUserTypes.mo and
  • bouncingBallWithImport.


You may also open each of them individually.


Note that I have not selected spokenTutorialExamples folder.


We shall discuss more about it when we look at directory storage.


Click Open. Click on Open.

You may see that spokenTutorialExamples package is now seen in Libraries Browser.

Double click on spokenTutorialExamples icon.
Switch to Text View if the file opens in Icon/Diagram View.
/* spokenTutorialExamples */

package spokenTutorialExamples

Now let me discuss spokenTutorialExamples

First line defines the name of a package.

Evidently, name of this package is spokenTutorialExamples.

Highlight piecewise //

class freeFall

model bouncingBall

model bouncingBallWithUserTypes

//

This package consists of


  • freefall class
  • bouncingBall model and
  • bouncingBallWithUserTypes models.
end spokenTutorialExamples; end statement defines where the package ends.
We have viewed all classes and models of this package at one place.

Now, let us learn how to view individual classes in a package.

Click on the (+) button with spokenTutorialExamples in Libraries Browser. Click on the (+) button besides spokenTutorialExamples icon in Libraries Browser.

This displays the names of classes present in this package.

Double-click on freeFall. Double-click on freeFall in Libraries Browser.

freeFall class has now opened.

Individual classes of a package may be simulated.

But the package itself can not be simulated.

Let me go back to spokenTutorialExamples tab. Let me go back to spokenTutorialExamples tab.
Hover over Simulate button in toolbar. Note that Simulate button doesn’t appear in the toolbar which indicates that this package cannot be simulated.
Now let me unload spokenTutorialExamples from OMEdit.

Do a right-click and select unload.

Select Yes.

This is done so as to avoid conflict when demonstrating directory storage.

Now let me go back to the slides.
Slide:

Directory storage

In Directory storage classes are stored in separate files.

Name of directory is same as that of the package.

A file named package.mo is to be included in the directory and

Each class starts with a within statement.

Now, let me demonstrate the folder spokenTutorialExamples that you’ve downloaded.

Note that this folder represents the same package as that we have already discussed.

Let me demonstrate the file structure of this folder.

Go to the location where you saved the downloaded files on your system.

/* spokenTutorialExamples folder */ Double-click on spokenTutorialExamples folder.

Notice that the folder has following files:

  • package.mo,
  • freeFall.mo,
  • bouncingBallWithUserTypes and
  • bouncingBall.

We shall understand more about directory storage using both OMEdit and gedit.

/* Directory */ Note that package.mo indicates that this folder represents a package.

Without this file, the folder doesn’t represent a Modelica package.

/* OMEdit */

Ctrl+O

spokenTutorialExamples → package.mo

Now, let me switch to OMEdit to demonstrate directory storage.

Press Ctrl + O.

Navigate to the spokenTutorialExamples folder that you have downloaded.

Select package.mo from this folder and click on Open.

Double-click on spokenTutorialExamples in Libraries Browser. spokenTutorialExamples package can now be seen in Libraries Browser.

Double-click on spokenTutorialExamples icon.

/* spokenTutorialExamples */ Open the package in Text View if it opens in either Icon/Diagram View.
/* spokenTutorialExamples */

Scroll down

If you scroll down, you may notice that this package is the same as that we have seen in single file storage.
The difference between single file and directory storage can only be understood when we open this package using a text editor like gedit.

You may use Notepad or any other text editor if you are using Windows.

Right-click on spokenTutorialExamples and select unload. Now let me unload spokenTutorialExamples package from 'OMEdit.
/* Download location */

Double-click on bouncingBall.mo, bouncingBallWithUserTypes ,freeFall, package.mo.

Switch to spokenTutorialExamples folder and open all the files using gedit.

Now all the files are open in gedit.

Let us take a look at package.mo first.

within; within statement indicates that this file represents a package.
package spokenTutorialExamples Name of this package is spokenTutorialExamples.

Note that the name of folder was spokenTutorialExamples as well.

end spokenTutorialExamples; package.mo file can only contain the within statement and package declaration.
Switch to freeFall tab. Let me switch to freeFall tab.
/* freeFall */

within spokenTutorialExamples;

This within statement indicates that freefall class belongs to spokenTutorialExamples package.

Rest of this file has information specific to freeFall class.

You may see that other models in this package namely bouncingBallWithUserTypes and bouncingBall follow a similar syntax as well.

But, we didn’t observe within statement when we opened the package in OMEdit.

This indicates that within statement helps OMEdit recognize the file structure.

Hence, it is omitted by OMEdit while displaying the package.

Let me go back to the slides.
Slide:

Modelica Library

We shall now understand more about Modelica Library.
  • Modelica Library is an open-source package.
  • OMEdit automatically loads it for every session.
  • It can be seen in Libraries Browser.
  • It has classes from mechanical, electrical and thermal domains.

Classes of this library can be referenced and used.

Now let me switch to OMEdit to demonstrate Modelica Library.
Click on (plus) Modelica Locate Modelica icon in Libraries Browser and expand it.

Note that Modelica Library has packages named Blocks, Complex Blocks etc.

You may note that a package may further contain packages as well as is evident here.

Click on (plus) SIunits Of special mention, is the SIunits package.

Expand it.

Click on (minus) SIunits This package has type definitions for physical quantities like Angle, Length, Position etc.

We shall see how to use these type definitions using bouncingBallWithImport class.

Compress SIunits.

Double-click on bouncingBallWithUserTypes and bouncingBallWithImport icons in Libraries Browser. And double-click on bouncingBallWithImport and bouncingBallWithUserTypes.
Let me shift OMEdit window to the left for better visibility.
We shall first look at bouncingBallWithUserTypes.

We have learnt about this model in the prerequisite tutorials.

/* bouncingBallWithUserTypes */

type Length = Real(unit = “m”);

type Velocity = Real(unit = “m/s”);

This model has type definitions named Length and Velocity.

We can instead use the type definition in SIunits instead of defining them explicitly in every model.

We shall see how to do that using bouncingBallWithImport model.

/* bouncingBallWithImport */ Let me switch to bouncingBallWithImport.
Modelica.SIunits.Length h; A class in a package is referenced to using dot.

Modelica.SIunits refers to SIunits package which belongs to Modelica library.

Modelica.SIunits.Length h; Variable h is declared to be of Length type which is defined in SIunits package.
Modelica.SIunits.Velocity v; Similarly, variable v is declared to be of Velocity type which is defined in SIunits package.
Note that parameters radius and g are declared in a similar fashion.

Now let me Simulate this model.

Click on Simulate button in the toolbar.

Close the pop up window.

Select h in the Variables Browser.

Note that the plot obtained is similar to the one seen in the case of bouncingBallWithUserTypes.

let me de-select h.

Delete the result.

Switch to Modeling perspective.

It is tedious to use the full name of a class every time.

This can be simplified using import statement.

Now, we shall type a few statements that show the use of import.


The statements to be typed are provided in a text file named import-statements.txt.

Go to the location where you saved it on your system.

Double-click on import-statements.txt.

Windows users may use Notepad to open this file.

/* gedit */

Copy the statements:

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

Length h;

Velocity v;

The file is now open in gedit.

Copy all the statements using Ctrl+C or by doing right-click.

Paste //

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

Length h;

Velocity v;

//

Switch to OMEdit.

Paste all the statements at the beginning of the model.

Delete // Modelica.SIunits.Length //

Modelica.SIunits.Velocity //

Delete the declaration statements for Length and Velocity, which have been defined before.

Delete the extra spaces.


Save this model by pressing Ctrl + S.

Now the model is complete and ready for simulation.

Click on Simulate button. Click on Simulate button to simulate it.

Close the pop up window.

Select h in the Variables Browser and Click on Modeling button. Select h in the Variables Browser.

Note that the plot is same as in previous case.

De-select h and delete the result.

Switch to Modeling perspective.

//

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

//

Now let us now try to understand the import statements.

Using the import statements, Modelica looks up for Length and Velocity type definitions using the path mentioned here.

This saves the trouble of referring to the package every time.

A detailed discussion of lookup rules is beyond the scope of this tutorial.

Delete //

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

//

Now let us see how we can replace these two import statements by a single import statement.

Delete the two statements.

Type //

import Modelica.SIunits.*;

And type import (space) Modelica (dot) SIunits (dot) asterisk (semicolon)

Save this model by pressing Ctrl + S

This statement is known as a wild-card import.

This way, any class from SIunits may be accessed without explicitly mentioning it.

Click on Simulate. Now let me Simulate this model.

Click on Simulate button.

Close the pop up window.

Select h Select h in the Variables Browser.

You may notice the similarity of plot once again.

De-select h and delete the result.

Click on Modeling. Go back to Modeling perspective.
Now let me switch to the slides.
Slide:

Assignment

As an assignment,

Declare variable h and v of freeFall class as Length and Velocity types respectively.

These type definitions can be found in SIunits package of Modelica library.

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 /What\_is\_a\_Spoken\_Tutorial

It summarises the Spoken Tutorial project.

If you have questions in this Spoken Tutorial, please visit the website shown.

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.

Please visit the following 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.
I would like to thank you for joining me in this tutorial.

Good bye.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey