2

I am writing a mos script in Dymola in which I am dynamically computing array elements inside for loops. A lot of information gets printed on the command window. every time it prints

Redeclaring variable: Real traj_phie [34, 1002];
Redeclaring variable: Real traj_phie [35, 1002];

etc. I don't want to "redeclare" my array every time, I just want to "fill" it. Will the Preallocation of the array size solve this problem? If so, how can I preallocate array in mos script? I tried different ways like

Real[50,1002] traj_phie;

for which Dymola throws an error. So my questions are 1) Array preallocation inside mos script 2) Suppressing the command output. Can someone help me? Thank you

1 Answers1

3

There is an advanced flag, which allows you to suppress the command output:

Advanced.EchoScriptCommands = false

To initialize a vector, matrix or array with a certain size use the fill() function. There is nothing like NaN in Modelica, so you have to initialize with a certain value.

Then you can use slicing operations to assign just the line / row / elements of interest.

traj_phie = fill(0.0, 50, 10);

// Assign line by-line
for i in 1:50 loop
  traj_phie[i, :] = i*ones(10);
end for;
marco
  • 5,944
  • 10
  • 22
  • 1
    As a slight variation you can use fill(0.0, 50, 10) instead of fill(0, 50, 10) to ensure that traj_phie is seen as a real variable (although it shouldn't matter). – Hans Olsson Jan 28 '20 at 10:36