Arrays in Modelica
Our first example demonstrates how to create and work with arrays in Modelica. Arrays are fundamental data structures that store collections of elements of the same type.
In this Modelica example:
We create an array
a
that will hold exactly 5 integers. By default, an array is initialized with zeros.We can set a value at an index using the
array[index] = value
syntax, and get a value witharray[index]
.The
size
function returns the length of an array along a specified dimension.We can declare and initialize an array in one line, as shown with arrays
b
andc
.Modelica supports multi-dimensional arrays. We demonstrate this with the
twoD
array.We use the
fill
function to initialize an array with a specific value.The
String
function is used to convert arrays to strings for printing.Modelica uses 1-based indexing, unlike many other programming languages which use 0-based indexing.
Note that when printed, arrays in Modelica appear in the form {v1, v2, v3, ...}
.
To run this model, you would typically use a Modelica simulation environment. The output would look similar to:
This example demonstrates the basics of working with arrays in Modelica, including initialization, accessing elements, and working with multi-dimensional arrays.