OpenModelica/C3/Modelica-Packages/English

From Script | Spoken-Tutorial
Revision as of 21:25, 19 March 2016 by Kaushik Datta (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Slide:

Title Slide

Welcome to the Spoken Tutorial on Packages as a part of the spoken tutorial series on OpenModelica.
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.
  • How to use Modelica Library.
Slide:

System Requirements

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

Windows users may use Notepad instead of gedit.

Slide:

Prerequisites

To practise 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

Package is a specialized class in Modelica.

It is a collection of classes.

Classes here may mean models, functions, types or any specialized classes.

package can be stored as a single file.

It can also be stored as a directory.

We will first learn about single file storage.

Slide:

Single file storage

All classes of a package may be stored in a single file with .mo extension.

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

/* OMEdit */

Click on Modeling button at bottom right

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,
  • bouncingBallWithImport.

Note that I have not selected the folder spokenTutorialExamples.

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

package spokenTutorialExamples

The first line defines the name of package.

Evidently, name of this package is spokenTutorialExamples.

Let me scroll down a little.

Highlight piecewise //

class freeFall

model bouncingBall

model bouncingBallWithUserTypes

//

Note that this package consists of
  • freeFall
  • bouncingBall
  • bouncingBallWithUserTypes models.

All three models were discussed in prerequisite tutorials.

Let me scroll down.

end spokenTutorialExamples; The end statement indicates where a 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 beside spokenTutorialExamples 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.

Click on simulate button.

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. Click on spokenTutorialExamples tab.
Hover over Simulate button in toolbar. Note that Simulate icon doesn’t appear in toolbar.

A package cannot be simulated.

Let us now unload spokenTutorialExamples from OMEdit.

Do a right-click on spokenTutorialExamples in Libraries Browser and select unload.

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

Let me go back to the slides.
Slide:

Directory storage

Packages may also be stored as directories as we’ve already discussed.

In this case, classes are stored in separate files.

Name of directory is same as that of the package.

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

Each file in the directory starts with a within statement.

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

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.

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 it has following files:

  • package.mo,
  • freeFall.mo,
  • bouncingBall.mo,
  • bouncingBallWithUserTypes.mo.
/* Directory */ package.mo indicates that this folder represents a 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.
/* OMEdit */

Ctrl+O

spokenTutorialExamples → package.mo

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

Press Ctrl + O. Navigate to the spokenTutorialExamples folder.

Select the package.mo file from this folder.

Double-click on spokenTutorialExamples in Libraries Browser. You can see spokenTutorialExamples package in Libraries Browser.

Double-click on spokenTutorialExamples icon.

/* spokenTutorialExamples */ Switch to Text View if it opens in Icon/Diagram View.
/* spokenTutorialExamples */

Scroll down

If you scroll down, you will notice that the package looks same as 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.
Right-click on spokenTutorialExamples and select unload. Unload spokenTutorialExamples.
/* Download location */

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.
within; within indicates that this file relates to a package.
package spokenTutorialExamples Name of this package is spokenTutorialExamples.
end spokenTutorialExamples; package.mo file can only contain the within statement and package declaration.
Click on freeFall tab. Click on freeFall tab.
/* freeFall */

within spokenTutorialExamples;

This indicates that this model 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 , bouncingBall follow a similar syntax.

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.

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

Let me go back to the slides.
Slide:

Modelica Library

Modelica Library is an open-source package.

OMEdit loads this library for every session.

It can be located in the Libraries Browser.

It has classes from mechanical, electrical and thermal domains.

Classes from this library can be referenced and used.

Let me switch to OMEdit to demonstrate Modelica Library.
Click on (plus) Modelica Expand Modelica in the Libraries Browser using the (plus) button.

This library has packages named Blocks , Complex Blocks etc.

Note that a package may further contain packages as well.

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

Click on (plus) button beside SIunits for expanding it.

Click on (minus) SIunits Note that there are type definitions available for quantities like Angle, Length etc.

We shall now understand how to use classes from SIunits package using bouncingBallWithImport model.

Compress SIunits.

Double-click on bouncingBallWithUserTypes and bouncingBallWithImport icons in Libraries Browser. Scroll down in Libraries Browser.

Double-click on bouncingBallWithUserTypes and bouncingBallWithImport.

Let me shift OMEdit window to the left for better visibility.
Let me discuss bouncingBallWithUserTypes first.

This model was discussed in prerequisite tutorials.

/* bouncingBallWithUserTypes */

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

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

This model has type definitions named Length and Velocity.

SIunits package from Modelica Library also has these type definitions.

It saves time to use these type definitions instead of explicitly defining them in every class.

/* bouncingBallWithImport */ Click on bouncingBallWithImport tab.
Modelica.SIunits.Length h; A class in a package is referenced using dot.

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

Modelica.SIunits.Length h; Variable h is declared to be of Length type defined in SIunits package.
Modelica.SIunits.Velocity v; Similarly, variable v is declared to be of Velocity type in SIunits package.
parameter radius and g are similarly defined.
Click on Simulate button.

Select h in Variables Browser.

Now, let me simulate this class.

Click on simulate button at the top.

You may observe that the output is the same as in the case of bouncingBallWithUserTypes.

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 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.

/* gedit */

Copy the statements:

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

Length h;

Velocity v;

Let me switch to gedit.

Click on import-statements.txt tab.

Copy the statements.

Paste //

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

Length h;

Velocity v;

//

Switch to OMEdit.

Paste the statements in bouncingBallWithImport model.

Delete // Modelica.SIunits.Length //

Modelica.SIunits.Velocity //

Delete the declaration statements for Length and Velocity.

Save the model by pressing Ctrl + S.

Click on Simulate button. Let me simulate this class.

Click on Simulate button.

Select h in Variables Browser and Click on Modeling button. Note that the output is same as in previous case.

Switch to Modeling perspective.

//

import Modelica.SIunits.Length;

import Modelica.SIunits.Velocity;

//

In this case, Modelica looks up for Length and Velocity type definitions in the path mentioned in import statements.

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;

//

Let me use one import statement instead of two.

Delete the two import statements.

Type //

import Modelica.SIunits.*;

Type import (space) Modelica (dot) SIunits (dot) asterisk.
This is known as wild-card import.

This way, any class from SIunits may be accessed.

Click on simulate. Save the model by pressing Ctrl + S and click on Simulate button.
Select h Select h in Variables Browser.

Note that the result is same as in previous case.

Click on Modeling. Delete the result and switch to Modeling perspective.
Let me go back to the slides.
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 do not have good bandwidth, you can download and watch it.

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.
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.
Slide:

Acknowledgements

Spoken Tutorial Project is supported by NMEICT, MHRD Government of India.
Slide:

Thanks

We thank OpenModelica development team for their support.
I would like to thank you for joining me in this tutorial.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey