OpenModelica/C3/Component-oriented-modeling/English

From Script | Spoken-Tutorial
Revision as of 14:49, 29 March 2016 by Nancyvarkey (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
Slide:

Title Slide

Welcome to the spoken tutorial on Component oriented modeling.
Slide:

Learning Objectives

In this tutorial, we are going to learn:
  • How to instantiate a model.
  • How to define connector class and
  • How to develop model of a simple electric circuit using component models.
Slide:

System Requirements

To record this tutorial, I am using:
  • OpenModelica 1.9.2.
  • Ubuntu Operating System version 14.04.
  • But this process is identical to any of the following operating systems.
Slide:

Prerequisites

To understand and practice this tutorial, you need to know
  • how to define a class in Modelica.
  • you need to know how to define a package and Icon and Diagram Views.

Prerequisite tutorials are mentioned on our website.

Please go through them.

Slide:

Class Instantiation

Now let us learn more about Class Instantiation.

Modelica classes can be instantiated.

For example, an individual can be considered an instance of human being class.

Instance of a class has the same variables and equations as the class itself.

Slide:

Syntax

The syntax for Class Instantiation is as shown.
Now, let us understand this through an example.

Please download and save all the files available on our website.

Let me switch to OMEdit.

The following files are already open in OMEdit:

  • classInstantiationExample and
  • simpleCircuit.

Double-click on classInstantiationExample.

Let us discuss more about this class now.

/* classInstantiationExample */

bouncingBall Ball1(h(start = 40), h(start = 50));

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

Here, I have instantiated the bouncingBall class to create two objects named

  • object1 and
  • object2.


Note that each instance has different start values for the height variable h.


Please refer prerequisite tutorials for more information on the bouncingBall model.

Click on Simulate button. Let us simulate this class now.

Click on Simulate button in the toolbar.

The class did not simulate and it threw up an error.

This is because bouncingBall class is not open in OMEdit.

Load and Simulate Open bouncingBall class which you downloaded from the website.

Now, simulate this class once again.

Close the pop up window.

Click on the + button beside Ball1 in variables browser. Note that the class simulates successfully this time around.


This exercise demonstrates that a class should be open in OMEdit to be instantiated.

Expand object1 variables in the variables browser.

Note that the variables listed here are declared in bouncingBall class.

These variables are also a part of object1 and object2 since the adjust instances of the bouncingBall class.


Now delete the result and go back to the slides.

Slide:

Component orientation

Component orientation sets Modelica apart from other modeling and simulation softwares.

It is the single most important feature of Modelica.

component models represent single physical phenomenon.

They can be instantiated and connected to produce desired effect.

For example, an RLC circuit could be developed from resistor, inductor and capacitor models.

Slide:

Acausal connectors

Acausal connectors serve as the interface between component instances.

They are defined using connector class.

For example, pins can be used as connectors for electrical components.


We will learn more about this when we try to simulate the example for an electric circuit.


Connectors content across and flow variables and they cannot contain equations.

Slide:

Circuit Diagram

Now, let us simulate this Electric Circuit shown in the slide.

The voltage of battery is given by {Vo*Sin(2*pi*f*t)}, where Vo is 10 Volts, f is 1 Hz and the resistance is 5 ohm.

Slide:

Solution Methodology

Let us take a look at the Solution Methodology to model the electric circuit shown in previous slide:

Note that any Resistor and Voltage Source have two pins: Positive and Negative.

  • Hence, define a connector named pin.
  • Define a class named Ground with one instance of pin connector.
  • Define a class named Resistor.
  • The Resistor class should have two instances of the pin connector: Positive pin and the Negative pin.
  • Define a class named VoltageSource with two instances of pin connector as we have seen in the case of resistor class.
  • Define a class named simpleCircuit.
  • simpleCircuit should have instances of Resistor and VoltageSource.
  • Connect the respective pins of Resistor, ground and the VoltageSource.
The component models required have already been programmed.

Hence, I shall demonstrate only the last two steps of Solution Methodology.

Let me switch to OMEdit.

Go back to the Modeling perspective.

Click on the plus button beside simpleElectricCircuit in Libraries Browser. Let me shift OMEdit window to the right.


Expand simpleElectricCircuitpackage in the Libraries Browser.

Note that this package has five classes named:

  • pin,
  • Ground,
  • Resistor,
  • Voltage Source and
  • circuit.

Double-click on simpleElectricCircuit.

Close ClassinstantiationExample.


Let me shift OMEdit window to the left once again for better visibility.

//

import Modelica.SIunits.*

//

Modelica.SIunits package has been imported in the package simpleElectricCircuit.


Hence, type definitions in that package, can be used without reference to their full names.

Scroll down Let us try to understand pin connector.

Scroll down a little bit.

// connector pin // Pin is defined using connector class.
Voltage and current are the variables that a pin exchanges with its surroundings.
// Voltage v; // Potential at the pin is defined by v.


Voltage and Current are types defined in SIunits package of Modelica library.

// flow Current i; // Voltage across a component causes a current to flow through it.

Hence, current is a flow variable and is defined using flow keyword.

// annotation(....); // pin connector also has an Icon view specified by annotations as shown.
Scroll down to Resistor class Now let us discuss a little about Resistor class.

Scroll down a little more.

// pin p ann(...);

pin n ann(...); //

As discussed in the Solution Methodology, Resistor class has two instances of pin connector.
  • p stands for positive pin and
  • n stands for negative pin.
Now, let me show you how to instantiate a class using the drag and drop functionality of OMEdit.
Press Ctrl + N To demonstrate that, let me open a new class using Ctrl + N.
Type example in the name field. Name this class example1 and press Ok.

Shift OMEdit window to the right.

Go to Diagram View if the class opens in Text View.
Drag and drop pin icon in Libraries Browser. Now let me instantiate pin class.

Left-click on pin icon in Libraries Browser.

Hold and drag the icon to the diagram layer.


Drop it at any location on the canvas.


We have now created an instance of pin class.

You may also change the dimensions and location after dropping it, as shown.

Now let us see how this class is instantiated in Text View.

Go to Text View.

// simpleElectricCircuit // Notice the command for instantiation of class pin and an annotation based on its placement in Diagram View.

Hence, creating an instance of a class in Diagram View automatically reflects in the Text View.


Now let me close example1 tab.

Let us learn how to model electric circuit that we saw in the slides.

Double-click on Circuit icon which is also a part of simpleElectricCircuit package.

This class already has our circuit of interest assembled, as can be seen in its Diagram View.

It is ready to be simulated.

But let us build the same circuit in a new file.

We shall use drag and drop functionality that we have just learnt about.

Press Ctrl + N.

Name this file circuit(underscore)construction.


Press OK.

Click on Diagram View. Switch to Diagram View, if it opens in Text View.
Select, drag and drop Voltage Source Select, drag and drop the VoltageSource icon from Libraries Browser.


You may change its dimensions as you wish.

Select, drag and drop Resistor. Similarly, select, drag and drop the Resistor icon from Libraries Browser.
Select, drag and drop Ground. Do the same with Ground class.
Now, we need to connect the respective pins of each component.
Hover over the left pin of Voltage Source. Let us first connect the positive pin of Voltage Source to the positive pin of Resistor.

Hover over the left pin of Voltage Source.

The text that appears indicates that, this is the positive pin p.

Connect the positive pin of voltage source to positive pin of resistor. Left click on this pin and drag the cursor closer to the left pin of Resistor.

Leave the mouse when the appearance of cursor changes to cross from an arrow.

Connect the negative pin of voltage source to negative pin of resistor. Similarly, connect the negative pin of resistor to the negative pin of voltage source.
Connections to Ground We have not mentioned the connections with Ground in the circuit diagram.

But, we need to connect the negative pins of Resistor and Voltage Source to Ground individually.

This ensures a reference point for potential in the circuit.

Press Ctrl + S Now, this class is complete.

Save the class by pressing Ctrl + S.

Click on Simulate button. Click on Simulate button.


Close the pop up window.

The class has simulated successfully.


Let me shift OMEdit window to the left for better visibility of Variables browser.

Expand Resistor column in Variables Browser and select Ir. Expand Resistor column in the Variables Browser and select Ir.

Note that the profile is sinusoidal as expected.

Since the Voltage Source is an AC source instead of DC.

Hence, we have created a model from its component parts and simulated it.

We shall learn more about the Resistor and Voltage Source classes that we used here in the next tutorial.

Let me switch to the slides.

This brings us to the end of this tutorial.

Slide:

Assignment

As an assignment, construct an electric circuit with two resistors in series, with one Voltage Source.

Use the component models for Voltage source and Resistor provided in simple electric circuit package.

Slide:

About the Spoken Tutorial project

Watch the video available at the following link:

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

It summarises Spoken Tutorial project.

Slide:

Spoken Tutorial Workshops

We conduct workshops using spoken tutorials.

Please contact us.

Slide:

Forum to answer questions

If you have questions from this tutorial, please visit the website mentioned below.
Slide:

Textbook Companion Project

We coordinate coding of solved examples of popular books.

Please visit our website.

Slide:

Lab Migration Project

We help migrate commercial simulator labs to OpenModelica.

Please visit the following website for more information.

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