OpenModelica/C2/Arrays-in-Modelica/English

From Script | Spoken-Tutorial
Revision as of 12:48, 10 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 Arrays as a part of the series on OpenModelica
Slide:

Learning Objectives

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

System Requirements

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

But, this process is identical in Windows, Mac OS X or FOSSEE on ARM

Slide:

Prerequisites

To understand and practice this tutorial, you need
  • Knowledge of arrays in any programming language
  • Knowledge of class definition in Modelica
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 an array variable a whose size is 2.

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

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

Slide:

Vector Indexing

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

The syntax for accessing an element is as shown.

Vector indexing starts from 1.

Indices must be integers.

Problem Statement Let us develop a function named polynomialEvaluatorUsingVectors.

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

We shall replace a,b,c in polynomialEvaluator function with a vector

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

For your convenience, polynomialEvaluator function is also made available.

/* Switch to OMEdit */ Let me switch to OMEdit. It is now open in Welcome perspective.

I have opened the necessary files.

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

/* polynomialEvaluator */

protected parameter Real a = 1;

protected parameter Real b = 2;

protected parameter Real c = 1;

Now, let me view these classes. Double click on each icon.

Switch to polynomialEvaluator tab.

For more information on this function, watch the prerequisite tutorials.

Click on polynomialEvaluatorUsingVectors tab.
/* polynomialEvaluatorUsingVectors */

input Real x;

output Real fx;

Input and output variables are the same as polynomialEvaluator function.
protected parameter Real a[3] = {1,2,1}; Parameters a,b and c in polynomialEvaluator are replaced by a real vector parameter named a.
protected parameter Real a[3] = {1,2,1}; The size of this vector is 3.
protected parameter Real a[3] = {1,2,1}; Vector elements are specified in curly braces. They are separated by 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];

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

It is the coefficient of x^2 in the expression for fx.

fx := a[1]*x^2 + a[2]*x + a[3]; Similarly, second and third elements of vector a are accessed.
Rest of this function is same as polynomialEvaluator.
Now, let us take a look at functionTester class
Click on functionTester tab Click on functionTester tab to switch.
/* functionTester */

Real z;

z is a Real variable.
z = polynomialEvaluatorUsingVectors(10); polynomialEvaluatorUsingVectors function is called. The input argument is 10.
z = polynomialEvaluatorUsingVectors(10); z is equal to the output returned by function call.
Click on Simulate button Click on Simulate button in the toolbar.
/* Plotting perspective */

Click on z in variables browser

functionTester class is simulated.

Click on z in the variables browser.

z takes the value of f(x) at x = 10, which is 121

Click on Modeling button Click on Modeling button at bottom right.
Let me go back to the slides.
Slide:

for (Add another slide for syntax)

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

for loop can be used in both algorithm and equation sections

The syntax of for loop is as shown with an example.

Let me go back to OMEdit.
Click on polynomialEvaluatorUsingVectors tab.
/* polynomialEvaluatorUsingVectors */

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

To evaluate fx, we are accessing the elements of vector a.

This can also be done using a for loop. Now, let us include a for loop in the algorithm section.

Comment // fx := a[1]*x^2 + a[2]*x + a[3]; Comment the assignment statement for fx in the algorithm section by inserting double slash.
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 to open it.

Let me go to gedit.
Copy all the statements. Go back to OMEdit
Press Enter.

Paste the for loop using Ctrl + V. Press Ctrl + S to save the function.

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

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. Loop runs until i is 3.

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

// fx := fx + a[i]*x^(3-i); This statement iteratively adds terms of the polynomial f(x).
// end for; This statement indicates the end of for loop
Now, the function is complete. Let us use the class functionTester to test the function after modification.
Click on functionTester tab Click on functionTester tab. We have not made any changes to this class.
Click on Simulate button Click on Simulate button in the toolbar.
/* Plotting perspective */ The class is simulated.

Click on z in variables browser. Value of z remains the same after changes made to the function.

Switch to Modeling perspective Click on Modeling button at bottom right.
Let me go back to the slides.
Slide:

while

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

while loop cannot be used in equation section.

for is frequently used as compared to while.

Slide:

Arrays

Arrays may have more than one dimension.

They can constructed using vector notation.

2 dimensional arrays may be constructed using matrix notation as well.

Slide:

Syntax

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

Problem Statement

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.
This class performs matrix addition by accessing each element individually.
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}}; This represents array size. It has two dimensions. 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.

Size of the vector is equal to size of second dimension of the array. Hence, size of second dimension of myMatrix is 2.

Number of vectors is equal to the size of first dimension.

Higher dimensional arrays may be constructed similarly.

parameter Real[3, 2] adder = {{1,0},{0,1},{1,0}}; adder matrix is similarly constructed.
We need to access elements from two dimensions here.

A nested for loop is required.

for i in 1:3 loop This for loop runs through the first dimension of array.
for j in 1:2 loop This loop runs through the second dimension.
mySum[i, j] := myMatrix[i, j] + adder[i, j]; Corresponding elements of myMatrix and adder are added.

Elements of mySum array are produced from this addition.

end for;

end for;

They represent the end of each for loop.
Click on Simulate button Click on Simulate button in the toolbar.
/* Plotting perspective */ variables browser shows each individual element of the array.

Click on myMatrix[1,1], adder[1,1] and mySum[1,1] in variables browser.

Note that mySum[1,1] is the sum of myMatrix[1,1] and adder[1,1].

Hence, this result is correct. Let me delete the result.

Let us go back to the slides.
Slide:

Assignment

Try the following problems:

Write a function named vectorReversal that takes a vector as input and returns a vector with the order of elements reversed.

Write a function named matrixReversal that takes a matrix as input and returns a matrix with the order of elements in each row reversed.

Write a functionTester class to test these two functions

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/ 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.
Thank you for joining me in this tutorial. Goodbye!

Contributors and Content Editors

Kaushik Datta, Nancyvarkey