Difference between revisions of "OpenModelica/C3/Modelica-Packages/English"
(Created page with "{| border=1 ||'''Visual Cue''' ||'''Narration''' |- || Slide: '''Title Slide''' || Welcome to the Spoken Tutorial on '''Packages''' as a part of the spoken tutorial series o...") |
Nancyvarkey (Talk | contribs) |
||
(5 intermediate revisions by one other user not shown) | |||
Line 7: | Line 7: | ||
'''Title Slide''' | '''Title Slide''' | ||
− | || Welcome to the Spoken Tutorial on '''Packages | + | || 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 | + | || 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'''. | ||
− | ''' | + | It can be stored as a '''single file''' or 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''' | ||
− | || | + | || In '''single file storage''' all '''classes''' belonging to a '''package''' are written in a '''single file'''. |
− | It is not recommended as | + | 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 | ||
− | || | + | || 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 | + | |
+ | 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''' | + | || Double click on '''spokenTutorialExamples''' icon. |
|- | |- | ||
Line 112: | Line 123: | ||
'''package spokenTutorialExamples ''' | '''package spokenTutorialExamples ''' | ||
− | || | + | || Now let me discuss '''spokenTutorialExamples''' |
− | + | First line defines the name of a '''package'''. | |
− | + | Evidently, name of this package is '''spokenTutorialExamples'''. | |
|- | |- | ||
Line 128: | Line 139: | ||
// | // | ||
− | || | + | || This package consists of |
− | * ''' | + | |
− | * '''bouncingBall''' | + | *'''freefall class''' |
+ | * '''bouncingBall''' model and | ||
* '''bouncingBallWithUserTypes''' models. | * '''bouncingBallWithUserTypes''' models. | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
|| '''end spokenTutorialExamples;''' | || '''end spokenTutorialExamples;''' | ||
− | || | + | || '''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 | + | || 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''' | + | '''freeFall class''' has now opened. |
− | Individual classes of a package may be simulated. | + | 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. | || Hover over '''Simulate''' button in toolbar. | ||
− | || Note that '''Simulate''' | + | || 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. | This is done so as to avoid conflict when demonstrating directory storage. | ||
Line 190: | Line 192: | ||
|- | |- | ||
|| | || | ||
− | || | + | || Now let me go back to the slides. |
|- | |- | ||
Line 196: | Line 198: | ||
'''Directory storage''' | '''Directory storage''' | ||
− | || ''' | + | || In '''Directory storage 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''' | + | A file named '''package.mo''' is to be included in the directory and |
− | Each | + | Each '''class''' starts with a '''within statement'''. |
|- | |- | ||
|| | || | ||
− | || Now, let me demonstrate the | + | || 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. | ||
− | |||
− | |||
|- | |- | ||
Line 224: | Line 222: | ||
|| Double-click on '''spokenTutorialExamples''' folder. | || Double-click on '''spokenTutorialExamples''' folder. | ||
− | Notice that | + | Notice that the folder has following files: |
* '''package.mo''', | * '''package.mo''', | ||
Line 230: | Line 228: | ||
* '''freeFall.mo''', | * '''freeFall.mo''', | ||
− | * ''' | + | * '''bouncingBallWithUserTypes''' and |
− | * ''' | + | * '''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 | + | Without this file, the folder doesn’t represent a '''Modelica package'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
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 | + | Select '''package.mo''' from this folder and click on '''Open'''. |
|- | |- | ||
|| Double-click on '''spokenTutorialExamples''' in '''Libraries Browser'''. | || Double-click on '''spokenTutorialExamples''' 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 */ | ||
− | || | + | || 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 | + | || 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'''. | ||
− | || | + | || 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'''. | ||
− | || | + | || 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 | + | || '''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''' | + | || '''package.mo''' file can only contain the '''within statement''' and '''package''' declaration. |
|- | |- | ||
− | || | + | || Switch to '''freeFall''' tab. |
− | || | + | || Let me switch to '''freeFall''' tab. |
|- | |- | ||
Line 310: | Line 312: | ||
'''within spokenTutorialExamples;''' | '''within spokenTutorialExamples;''' | ||
− | || This indicates that | + | || This '''within statement''' indicates that '''freefall class''' belongs to '''spokenTutorialExamples''' package. |
− | Rest of this file has information specific to '''freeFall''' | + | Rest of this file has information specific to '''freeFall class'''. |
|- | |- | ||
|| | || | ||
− | || You may see that other models in this package namely '''bouncingBallWithUserTypes | + | || 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''' | + | But, we didn’t observe '''within statement''' when we opened the package in '''OMEdit'''. |
− | This indicates that '''within''' | + | 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''' | + | || We shall now understand more about '''Modelica Library'''. |
− | ''' | + | *'''Modelica Library''' is an open-source package. |
− | + | *'''OMEdit''' automatically loads it for every session. | |
− | It | + | *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 ''' | || 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''' | || Click '''on (plus) SIunits''' | ||
− | || Of special mention is the '''SIunits''' package. | + | || Of special mention, is the '''SIunits''' package. |
− | + | Expand it. | |
|- | |- | ||
|| Click on '''(minus) SIunits''' | || Click on '''(minus) SIunits''' | ||
− | || | + | || This package has type definitions for physical quantities like '''Angle, Length, Position''' etc. |
− | We shall | + | 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'''. | ||
− | || | + | || And double-click on '''bouncingBallWithImport''' and '''bouncingBallWithUserTypes'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 380: | Line 382: | ||
|- | |- | ||
|| | || | ||
− | || | + | || We shall first look at '''bouncingBallWithUserTypes'''. |
− | + | 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''' | + | 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 */ | || /* bouncingBallWithImport */ | ||
− | || | + | || 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''' | + | '''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. |
|- | |- | ||
|| | || | ||
− | || | + | || 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. | ||
|- | |- | ||
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''' | + | 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'''. |
− | |||
− | Go to the location where you saved | + | 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'''; | ||
− | || | + | || The file is now open in '''gedit'''. |
− | + | Copy all the statements using '''Ctrl+C''' or by doing right-click. | |
− | + | ||
− | + | ||
|- | |- | ||
Line 473: | Line 485: | ||
|| Switch to '''OMEdit'''. | || Switch to '''OMEdit'''. | ||
− | Paste the | + | 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. |
+ | |||
+ | 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. | ||
− | || | + | || Click on '''Simulate''' button to simulate it. |
− | + | 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 | + | || 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: | ||
// | // | ||
− | || | + | || 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: | ||
// | // | ||
− | || | + | || Now let us see how we can replace these two '''import statements''' by a single '''import statement'''. |
− | Delete the two | + | Delete the two '''statements'''. |
|- | |- | ||
Line 525: | Line 548: | ||
'''import Modelica.SIunits.*;''' | '''import Modelica.SIunits.*;''' | ||
− | || | + | || 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 ''' | + | || Click on '''Simulate'''. |
− | || | + | || 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'''. |
− | + | You may notice the similarity of plot once again. | |
+ | |||
+ | De-select '''h''' and delete the result. | ||
|- | |- | ||
|| Click on '''Modeling'''. | || 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. | ||
|- | |- | ||
Line 565: | Line 605: | ||
It summarises the '''Spoken Tutorial project'''. | It summarises the '''Spoken Tutorial project'''. | ||
− | If you | + | If you have questions in this '''Spoken Tutorial''', please visit the website shown. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | '''Spoken Tutorial | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 598: | 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 609: | Line 635: | ||
'''Thanks''' | '''Thanks''' | ||
− | || We thank '''OpenModelica''' | + | || 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:
|
Slide:
System Requirements |
To record this tutorial, I am using
Windows users may use Notepad or any other text editor instead of gedit. |
Slide:
Prerequisites |
To understand and practice this tutorial, you need
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.
|
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
| |
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
|
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:
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.
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.
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.
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. |