OpenModelica/C2/Array-Functions-and-Operations/English
Visual Cue | Narration |
Slide:
Title Slide |
Welcome to the Spoken Tutorial on Array Functions and Operations. |
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 any text editor like Notepad instead of gedit. |
Slide:
Prerequisites |
To understand and practice this tutorial, you need
|
Slide:
OMShell |
Let us learn more about OMShell now.
|
Narration | We shall now use classes named polynomialEvaluatorUsingVectors and functionTester to demonstrate OMShell.
These classes were discussed in previous tutorials. |
Narration | For more information on these classes, please watch the prerequisite tutorials. |
Narration | All the commands to be used in this tutorial are provided in a file named OMShell-commands.txt.
Please save all these code files in one directory for easy access. |
Click on Search icon in Icon Tray. | Now let me launch OMShell.
To open OMShell on Ubuntu Operating System, click on Dash Home icon at top left in the launcher. |
Type OMShell >> click on OMShell icon | Type OMShell in the search box.
|
In Windows, you may find the icon in Start menu. | |
/* OMShell */ | Now, let us learn a few useful commands.
Hence, you may refer to this file whenever in doubt. |
Switch to OMShell | Now let me switch to OMShell. |
Type // cd() // | Type cd open and close parentheses.
Press Enter to display the result produced on execution of the command. |
This prints the path to current directory. | |
Now let us change current directory to the location where you have saved the code files. | |
Type // cd(“path”) // | Let me change directory on my system.
Type cd(open and close parentheses) (within double quotes), specify the path. Press Enter. |
Note that a Windows path uses forward slash unlike the backward slash used in Ubuntu.
Windows users need to be cautious of this fact. | |
Type // loadFile(“polynomialEvaluatorUsingVectors.mo”) // | Now let us load polynomialEvaluatorUsingVectors function.
Type loadFile (within parentheses) (within double quotes) polynomialEvaluatorUsingVectors.mo. |
Highlight
Note that F is upper-case in loadFile() command. | |
Press Enter | This command can be used to load class or model files with a file extension of .mo.
Now press Enter. |
// true // | If the file is found, OMShell returns true. |
Type // polynomialEvaluatorUsingVectors(10) // | Now let us call this function interactively.
Type polynomialEvaluatorUsingVectors (with an argument of) 10. Press Enter. |
This command takes an input argument of 10 units and displays the result. | |
Type // loadFile(“functionTester.mo”) // >> Press Enter | Let me now load functionTester class.
Type loadFile (open and close parentheses) (within double quotes) functionTester.mo. Press Enter. |
Type // simulate(functionTester,startTime=0,stopTime=1) // >> Press Enter | Now let us simulate functionTester class.
Type simulate (within parentheses) functionTester (comma) startTime (equals) 0 stopTime (equals) 1. Press Enter. |
The simulation is now complete. | |
Let us plot variable z from functionTester class. | |
Type // plot({z}) // | Type plot (within parentheses) (within curly braces) z and press Enter.
This command generates a plot of variable z vs time. |
Now let me go back to the slides. | |
Slide:
Array Construction Functions
|
Array construction functions are used to construct arrays of given size.
We will also practice them using OMShell.
|
Back to OMShell | Now let me demonstrate these functions using OMShell.
|
/* OMShell */
Type // fill(5,2,2) // |
Type fill(within parentheses) 5 (comma) 2 (comma) 2.
This command generates a two by two matrix with all its elements being 5. The first arguments represents the element to be filled within the array. 2 represents the size of first dimension. and the third argument 2, represents the size of second dimension. |
Press Enter | Now press Enter. |
Result shown on screen | The result is as expected.
Elements with one set of curly braces represent a row. Hence this matrix has two rows and two columns. |
Type // zeros(2,2) // >> press Enter. | Now let us use zeros() function to create a (two by two) matrix with all its elements zero.
Type zeros (within parentheses) 2 (comma) 2 and press Enter. |
Result shown on screen | The result is as expected. |
Type // identity(3) // | Let us now try identity function.
Type identity(3). This creates an identity matrix which is 3 (by) 3 in its size. |
We can also perform arithmetic operations and use assignment statements in OMShell.
Let us create two matrices and perform arithmetic operations on them. | |
Type // a:=[1,2;3,4] // >> press Enter | Type a (colon) (equals) (within square brackets) 1 (comma) 2 (semicolon) 3 (comma) 4.
Now press Enter. |
Type // b:=identity(2) // | Type b (colon) (equals) identity (2).
This generates a 2 by 2 identity matrix. |
Type // a + b // >> press Enter | Now let us perform arithmetic operations on a and b.
Type a (plus) b and press Enter. This performs matrix addition. |
Type // a * b // >> press Enter | Type a (asterisk) b.
This performs matrix multiplication. Press Enter. |
Type // a .* b // >> press Enter | Type a (dot) (asterisk) b and press Enter.
This performs element-wise multiplication of the two matrices. |
Note that it is not necessary to define data-types of variables used in OMShell. | |
Now let me switch back to the slides. | |
Slide:
Reduction Functions |
Reduction functions take array as input and return scalar as output.
min() is a function which returns the smallest value in an array. Similarly, max() function returns the largest value in an array. sum() returns the sum of all elements and product() returns the product of all elements. |
Switch to OMShell | Let me switch to OMShell to demonstrate these functions. |
x = [3,4;5,6] | Let me create a new matrix.
x (colon)(equals) (within square brackets) 3 (comma) 4 (semicolon) 5 (comma) 6. |
Type // min(x) // >> press Enter | Type min (x) to obtain the minimum value of x. |
Type // max(x) // >> press Enter | Type max (x) to obtain the largest value in array x.. |
Type // sum(x) // >> press Enter | Similarly type sum (x) to obtain the sum of all elements. |
Type // product(x) // >> press Enter | and product (x) to obtain the product of individual elements min array x. |
Back to the slides | Let me go back to the slides once again. |
Slide:
Miscellaneous functions |
Let us now discuss various other functions that take an array as input.
|
This brings us to the end of this tutorial.
In this tutorial, we used OMShell to interactively demonstrate array functions. These functions are part of Modelica language specification. Hence, they may be used while writing classes in OMEdit as well. | |
Slide:
Assignment |
As an assignment, apply abs(), ndims() and size() functions to an array.
Secondly, we have used a two-dimensional array or matrix as an argument to most of the functions. As an assignment, implement all these functions with three-dimensional arrays. |
Slide:
About the Spoken Tutorial project |
Watch the video available at the following link:
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial Its summarises the Spoken Tutorial project. |
Slide:
Spoken Tutorial Workshops |
We conducts workshops using spoken tutorials.
Give certificates. Please contact us. |
Slide:
Forum to answer questions |
If you have questions in this spoken tutorial, please visit the webpage mentioned. |
Slide:
Textbook Companion Project |
We coordinate coding of solved examples of popular books.
Please contact us. |
Slide:
Lab Migration Project |
We help migrate commercial simulator labs to OpenModelica. |
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. |