Itâ€™s sometimes useful to create 1- 2- or higher-dimensional array/matrix variable for use in a model.  This can happen for instance when:

• Using TABLE statements in a model
• Modeling quantities which are fundamentally multi-dimensional (e.g., position or velocity in 3-D space)
• When building finite-difference models within Magnolia to approximate phenomena which requires solution of a PDE

The syntax for declaration and element retrieval of an array variable or constant is straightforward, but differs a little from legacy CSL implementations:

• Arrays are defined using the DIMENSION statement, but the shape of the array is denote using square brackets instead of parenthesis
• Arrays are 1-based (not zero-based as ini many populate languages)
• Lookup (indexing, retrieval) of an array element is also performed using the square bracket notation
• Index values for accessing an element in an array must be integer values, and thus need to be declared as such using a DIMENSION statement.
• Array variables/constants can be used by the INTEG operator directly.  Magnolia infers the shape of the argument to INTEG using the DIMENSION statements for the associated variables.


model ArraysExample

derivative

dimension x[2, 3], dx[2, 3], xic[2, 3], k[2, 3]
constant xic = 0.1, 0.2, 0.3, 0.4, 0.5, 0.6
constant k = 1, 2, 3, 4, 5, 6
dimension integer i, j

! Wrap FOR loops in procedural to avoid any potential sorting errors
procedural(dx = x)
for i = 1, 2
for j = 1, 3
dx[i, j] = -k[i, j]*x[i, j]
end
end
end

! The INTEG statement can be used on vector states, as long
! as both arguments are also vector values
x = integ(dx, xic)

constant tstop = 2.0
termt(t >= tstop, 'Stopped on time limit')

! Save array elements to scalars so they can be displayed in the interactive plot
x11 = x[1, 1]
x21 = x[2, 1]
x12 = x[1, 2]
x22 = x[2, 2]
x13 = x[1, 3]
x23 = x[2, 3]

end ! derivative

end ! program