arraysmodelicadymolaopenmodelicapre-allocation

Preallocation of array sizes in Modelica mos script


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


Solution

  • 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;