OpenModelica/C2/Array-Functions-and-Operations/English-timed
From Script | Spoken-Tutorial
| Time | Narration |
| 00:01 | Welcome to the Spoken Tutorial on Array Functions and Operations. |
| 00:07 | In this tutorial, we are going to learn:
how to use OMShell , how to use array construction functions, |
| 00:17 | how to perform arithmetic operations on vectors and matrices, |
| 00:23 | how to use array conversion functions. |
| 00:27 | To record this tutorial, I am using OpenModelica 1.9.2, Ubuntu Operating System version 14.04 and gedit. |
| 00:40 | Windows users may use any text-editor like Notepad instead of gedit. |
| 00:47 | To understand and practice this tutorial, you need knowledge of function and array declaration in Modelica. |
| 00:56 | Prerequisite tutorials are mentioned on our website. Please go through them. |
| 01:02 | Let us learn more about OMShell now. |
| 01:06 | OMShell is an interactive command line tool. |
| 01:10 | It is a part of OpenModelica. |
| 01:13 | OpenModelica compiler can be invoked using commands typed in OMShell. |
| 01:20 | It can be used for loading classes and simulating them. |
| 01:25 | Functions can also be called in OMShell. |
| 01:29 | We shall now use classes named polynomialEvaluatorUsingVectors and functionTester to demonstrate OMShell. |
| 01:38 | These classes were discussed in previous tutorials. |
| 01:42 | For more information on these classes, please watch the prerequisite tutorials. |
| 01:48 | All the commands to be used in this tutorial are provided in a file named OMShell-commands.txt. |
| 01:57 | You may locate and download all the code files available on our website. |
| 02:03 | Please save all these code files in one directory for easy access. |
| 02:09 | Now let me launch OMShell. |
| 02:12 | To open OMShell on Ubuntu Operating System, click on Dash Home icon at top left in the launcher. |
| 02:21 | Type OMShell in the search bar. |
| 02:25 | Click on OMShell icon. |
| 02:28 | In Windows, you may find the icon in Start menu. |
| 02:33 | Now, let us learn a few useful commands. |
| 02:37 | Firstly, go to the location where you saved the text file named OMShell-commands.txt and open it. |
| 02:47 | Note that this file has all the commands to be used in this tutorial. |
| 02:52 | Hence, you may refer to this file whenever in doubt. |
| 02:57 | Now let me switch to OMShell. |
| 03:00 | Type: cd open and close parentheses. |
| 03:05 | Press Enter to display the result produced on execution of the command. |
| 03:11 | This prints the path to current directory. |
| 03:15 | Now, let us change current directory to the location where you have saved the code files. |
| 03:22 | Let me change directory on my system. |
| 03:25 | Type: cd open and close parentheses, within double quotes specify the path. Press Enter. |
| 03:38 | Note that a Windows path uses forward slash unlike the backward slash used in Ubuntu. |
| 03:46 | Windows users need to be cautious of this fact. |
| 03:51 | Now, let us load polynomialEvaluatorUsingVectors function. |
| 03:57 | Type loadFile within parentheses within double quotes polynomialEvaluatorUsingVectors.mo. |
| 04:11 | Note that F is upper-case in loadFile() command. |
| 04:16 | This command can be used to load class or model files with a file extension of .mo. |
| 04:25 | Now press Enter. |
| 04:28 | If the file is found, OMShell returns true. |
| 04:33 | Now, let us call this function interactively. |
| 04:37 | Type: polynomialEvaluatorUsingVectors with an argument of 10. Press Enter. |
| 04:47 | This command takes an input argument of 10 units and displays the result. |
| 04:55 | Let me now load functionTester class. |
| 04:59 | Type: loadFile open and close parentheses within double quotes functionTester.mo. Press Enter. |
| 05:12 | Now, let us simulate functionTester class. |
| 05:16 | Type simulate within parentheses functionTester comma startTime equals 0 stopTime equals 1. Press Enter. |
| 05:32 | The simulation is now complete. |
| 05:35 | Let us plot variable z from functionTester class. |
| 05:40 | Type plot within parentheses within curly braces z and press Enter. |
| 05:50 | This command generates a plot of variable z vs time. |
| 05:56 | Now let me go back to the slides. |
| 06:01 | Array construction functions are used to construct arrays of given size. |
| 06:06 | Now let us take a look at a few array construction functions. |
| 06:11 | We will also practice them using OMShell. |
| 06:15 | fill() is a function used to create an array with all the elements same. The syntax for fill is as shown. |
| 06:25 | First argument represents the number which fills the array. |
| 06:29 | Remaining arguments represent the size of each dimension. |
| 06:34 | zeros() is a function used to create an array filled with zeros. Syntax for zeros() function is as shown. |
| 06:44 | Arguments represent the size of each dimension of the array. |
| 06:50 | identity() function creates an identity matrix. It takes one argument that represents the size of both dimensions. |
| 07:02 | Now, let me demonstrate these functions using OMShell. |
| 07:06 | Let me go back to OMShell. |
| 07:09 | Type: fill within parentheses 5 comma 2 comma 2. |
| 07:16 | This command generates a two by two matrix with all its elements being 5. |
| 07:24 | The first argument represents the element to be filled within the array. |
| 07:30 | 2 represents the size of first dimension |
| 07:34 | and the third argument 2, represents the size of second dimension. |
| 07:40 | Now press Enter. |
| 07:43 | The result is as expected. |
| 07:46 | Elements with one set of curly braces represent a row. |
| 07:52 | Hence this matrix has two rows and two columns. |
| 07:57 | Now, let us use zeros() function to create a two by two matrix with all its elements zero. |
| 08:05 | Type: zeros within parentheses 2 comma 2 and press Enter. |
| 08:13 | The result is as expected. |
| 08:16 | Let us now try identity function. |
| 08:19 | Type: identity(3). |
| 08:23 | This creates an identity matrix which is 3 by 3 in its size. |
| 08:29 | We can also perform arithmetic operations and use assignment statements in OMShell. |
| 08:36 | Let us create two matrices and perform arithmetic operations on them. |
| 08:42 | Type: a colon equals within square brackets 1 comma 2 semicolon 3 comma 4. |
| 08:54 | Comma is used to separate elements in a row |
| 08:58 | whereas semi-colon is used to separate rows themselves. Now press Enter. |
| 09:07 | Type: b colon equals identity of 2. |
| 09:15 | This generates a 2 by 2 identity matrix. |
| 09:19 | Now, let us perform arithmetic operations on a and b. |
| 09:24 | Type: a plus b and press Enter. |
| 09:29 | This performs matrix addition. |
| 09:32 | Type a asterisk b. |
| 09:36 | This performs matrix multiplication. Press Enter. |
| 09:42 | Type: a dot asterisk b and press Enter. |
| 09:49 | This performs element-wise multiplication of the two matrices. |
| 09:55 | Note that it is not necessary to define data-types of variables used in OMShell. |
| 10:02 | Now let me switch back to the slides. |
| 10:06 | Reduction functions take array as input and return scalar as output. |
| 10:13 | min() is a function which returns the smallest value in an array. |
| 10:19 | 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. |
| 10:33 | Let me switch to OMShell to demonstrate these functions. |
| 10:38 | Let me create a new matrix. |
| 10:41 | x colon equals within square brackets 3 comma 4 semicolon 5 comma 6. |
| 10:52 | Type: min (x) to obtain the minimum value of x. |
| 11:00 | Type: max (x) to obtain the largest value in array x. |
| 11:08 | Similarly, type: sum (x) to obtain the sum of all elements |
| 11:15 | and product (x) to obtain the product of individual elements min array x. |
| 11:23 | Let me go back to the slides once again. |
| 11:27 | Let us now discuss various other functions that take an array as input. |
| 11:33 | abs() is a function which returns an array with the absolute values of all its elements. |
| 11:40 | size() returns a vector with the size of each dimension. |
| 11:45 | ndims() returns the number of dimensions in an array. |
| 11:51 | This brings us to the end of this tutorial. |
| 11:54 | In this tutorial, we used OMShell to interactively demonstrate array functions. |
| 12:01 | These functions are part of Modelica language specification. |
| 12:05 | Hence, they may be used while writing classes in OMEdit as well. |
| 12:11 | As an assignment, apply abs(), ndims() and size() functions to an array. |
| 12:19 | Secondly, we have used a two-dimensional array or matrix as an argument to most of the functions. |
| 12:28 | As an assignment, implement all these functions with three-dimensional arrays. |
| 12:35 | Watch the video available at the following link: |
| 12:39 | It summarizes the Spoken Tutorial project. |
| 12:42 | We conducts workshops using spoken tutorials, give certificates. Please contact us. |
| 12:48 | If you have questions in this spoken tutorial, please visit the webpage mentioned. |
| 12:54 | We coordinate coding of solved examples of popular books. Please contact us. |
| 13:00 | We help migrate commercial simulator labs to OpenModelica. |
| 13:06 | Spoken Tutorial Project is funded by NMEICT, MHRD Government of India. |
| 13:14 | We thank the development team of OpenModelica for their support. Thank you. |