OpenModelica/C2/Arrays-in-Modelica/English

From Script | Spoken-Tutorial
Revision as of 14:03, 16 March 2016 by Kaushik Datta (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
Slide:

Title Slide

Welcome to the spoken tutorial on Arrays.
Slide:

Learning Objectives

In this tutorial, we are going to learn:
  • How to declare array variables
  • How to construct arrays
  • How to use for and while loops and
  • How to use OMShell
Slide:

System Requirements

To record this tutorial, I am using:
  • OpenModelica 1.9.2

You may use any of the following operating systems to practice this tutorial.

Slide:

Prerequisites

To understand and practice this tutorial,
  • you need knowledge of arrays in any programming language
  • You need to know how to define a class in Modelica

Prerequisite tutorials are mentioned on our website.

Please go through them.

Slide:

Vector declaration (Add an example in the slide for declaration as well)

Vector is a one dimensional array

It has single index

Syntax for vector declaration is as shown.

The example shown declares a vector variable a whose size is 2.

A vector can be constructed by including the elements in curly braces.

This example defines a vector parameter a with 2 and 3 as its elements.

Slide:

Indexing vector elements

To access the elements of a vector, it is necessary to understand indexing.

Syntax for vector indexing is as shown.

Vector indexing starts from 1 and

Indices must be integers.

Problem Statement Let us develop a function named polynomialEvaluatorUsingVectors.

This function is an extension of polynomialEvaluator function which was discussed in previous tutorials.

We shall replace parameters a,b and c of polynomialEvaluator with a vector a.

Please download and save all the files available on our Code Files link.

For your convenience, polynomialEvaluator function is also made available.

/* Switch to OMEdit */ Now let me switch to OMEdit to demonstrate this function.

OMEdit is now open in Welcome perspective.

I have opened all the necessary files.

Note that the following classes or functions are now open in OMEdit: functionTester, matrixAdder, polynomialEvaluator and polynomialEvaluatorUsingVectors.

/* polynomialEvaluator */

protected parameter Real a = 1;

protected parameter Real b = 2;

protected parameter Real c = 1;

Now, to view them let me double click on each icon.

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

Go to polynomialEvaluator tab.

Open it in Text View.

For more information on this function, refer to the previous tutorials.

Let me go to polynomialEvaluatorUsingVectors.

Open it in Text View.

/* polynomialEvaluatorUsingVectors */

input Real x;

output Real fx;

Input and output variables are the same as in polynomialEvaluator function.
protected parameter Real a[3] = {1,2,1}; Parameters a,b and c of polynomialEvaluator are replaced with a vector a.
protected parameter Real a[3] = {1,2,1}; Size of this vector is 3.
protected parameter Real a[3] = {1,2,1}; The elements of this vectors are included in curly braces as shown.

The elements are separated by a comma.

algorithm

fx := a[1]*x^2 + a[2]*x + a[3];

This indicates the beginning of algorithm section.

The elements of vector a are accessed using their indices.

algorithm

fx := a[1]*x^2 + a[2]*x + a[3];

In the assignment statement the elements of vector a are accessed using their indices.

a[1] is the first element of vector a.

fx := a[1]*x^2 + a[2]*x + a[3]; Similarly, the second element and third element of vector a have been accessed as well.
Now, let me switch to functionTester tab.

Open it in Text View.

This class is similar to the functionTester class discussed in previous tutorial.

/* functionTester */

Real z;

z is a Real variable.
z = polynomialEvaluatorUsingVectors(10); polynomialEvaluatorUsingVectors function is called with an input argument of 10 units.
z = polynomialEvaluatorUsingVectors(10); The value returned by this function is equated to z.

Now let me Simulate this class.

Click on Simulate button Click on Simulate button.

Close the pop up window.

/* Plotting perspective */

Select z in the variables browser

functionTester class is simulated.

Select z in the variables browser.

Note that the value of z is equal to f(x) at x = 10.

This plot is the same as observed in case of polynomialEvaluator function.

Now let me de-select z and delete the result.

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

for loop (Add another slide for syntax)

for loop is used to iterate statements a given number of times

It can be used in algorithm and equation sections.

Syntax for for loop is as shown with an example.

To demonstrate how to use for loop let me go back to OMEdit.
Click on polynomialEvaluatorUsingVectors tab.
/* polynomialEvaluatorUsingVectors */

fx := a[1]*x^2 + a[2]*x + a[3];

In the assignment statement for fx, we are accessing the elements of vector a.

This can also be done using a for loop.

Now, let us see how to include a for loop in the algorithm section.

Comment // fx := a[1]*x^2 + a[2]*x + a[3]; Firstly, Comment the assignment statement for fx by inserting double slash at the beginning and end.

Save this function by pressing Ctrl+S.

The for loop to be inserted has been provided in a text file named for-loop.txt.

It is available on our website.

I have opened this file using gedit.

Windows users may use notepad or any other text editor to open it.

Let me go to gedit.
Copy all the statements by pressing Ctrl+C.

Go back to OMEdit

Press Enter.

Paste all the statements by pressing Ctrl + V.

Save this function by pressing Ctrl + S.

// fx := 0; Now, let me explain each statement of this loop.

This statement assigns fx an initial value of zero before the loop starts.

// for i in 1:3 loop Here, i serves as a loop counter.

The loop runs until value of i is 3.

It is not necessary to declare i before it is used.

Let me scroll down a bit.

// fx := fx + a[i]*x^(3-i); This statement iteratively adds terms of the polynomial f(x).

Polynomial f(x) has been discussed while discussing polynomialEvaluator function.

// end for; This statement indicates the end of for loop
Now, this function is complete.

To test this function let us use the class functionTester.

I have made no changes to this function to this class.

Click on Simulate button let me simulate this class by pressing Simulate button in the toolbar.
/* Plotting perspective */ Select z in variables browser.

Note that the value of z remains the same after changes are made to the function.

Let me de-select z and delete the result.

Go back to Modeling perspective Go back to Modeling perspective
Now let me go back to the slides once again.
Slide:

while loop

while loop is used to iterate statements until a given condition is satisfied.

while loop cannot be used in equation section.

for loop is more frequently used in Modelica as compared to while.

Slide:

Arrays

Let us discuss Arrays now.

Arrays are used to represent multi-dimensional data.

They can be constructed using vector notation.

Slide:

Syntax

Syntax for array declaration and indexing is as shown.
Slide:

Problem Statement

To understated more about array construction and indexing

Let us write a class named matrixAdder which

adds myMatrix and adder matrices to give mySum.

myMatrix and adder matrices are as shown.

Now, let me switch to OMEdit to demonstrate matrixAdder class.

It is already open in OMEdit.

/* Switch to OMEdit */ Click on matrixAdder tab.

Open it in Text view.

parameter Real[3, 2] myMatrix = {{1,2},{3,4},{5,6}}; myMatrix is a Real parameter array.
parameter Real[3, 2] myMatrix = {{1,2},{3,4},{5,6}}; Numbers in square bracket represent size of this array.

The size of first dimension is 3.

Similarly, the size of second dimension is 2

parameter Real[3, 2] myMatrix = // {{1,2},{3,4},{5,6}}; // Highlight piecewise myMatrix array is constructed using three vectors of two elements each.

{1,2} represents the first vector.

{3,4} is the second one and

{5,6}} represents the third vector.

Size of each of this vectors is equal to size of second dimension of this array.

Hence, the size of second dimension of myMatrix is 2.

The number of vectors is equal to the size of first dimension.

Hence, the size of first dimension is equal to 3.

parameter Real[3, 2] adder = {{1,0},{0,1},{1,0}}; adder matrix is constructed in a similar fashion.
To add this two arrays or matrices we need to access elements from two dimensions.

Hence a nested for loop is required.

for i in 1:3 loop This for loop runs through the first dimension.
for j in 1:2 loop Similarly this for loop runs through the second dimension.

Let me scroll down a bit.

mySum[i, j] := myMatrix[i, j] + adder[i, j]; Corresponding elements of myMatrix and adder matrices are added to yield mySum
end for;

end for;

This statements represent the end of each for loop.

The class is now complete.

Click on Simulate button Let me simulate it by clicking on Simulate button.

Close the pop up window if it appears.

/* Plotting perspective */ Let me expand variables column.

Select adder[1,1], myMatrix[1,1], and mySum[1,1].

Note that adder[1,1] plus myMatrix[1,1] gives mySum[1,1] which means that the result is accurate.

Let me de-select them and delete the result.

Let me go back to the slides.
Slide:

Assignment

As an assignment:

Write a function named vectorReversal to reverse the order of elements in a vector.

Similarly write a function matrixReversal to reverse the order of elements in each row of a matrix.

Write functionTester class to test these two functions.

This brings us to the end of this tutorial
Slide:

About the Spoken Tutorial project

Please watch the video available at following link:

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

It summarises the Spoken Tutorial project

Slide:

Spoken Tutorial Workshops

We conducts workshops using spoken tutorials

Give certificates.

Please contact us.

Slide:

Forum

If you have questions related to this spoken tutorial, please visit the following website.
Slide:

Textbook Companion Project

We coordinate coding of solved examples of popular books.

We give honorarium and certificates to those who do this.

Please visit the following 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 for joining me in this tutorial.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey