initialising array with unknown size in modelica
Asked Answered
B

5

6

I need a little help with initialising arrays in openmodelica. I created a modelica class which should generate an array with variable size. The size is to be set as a parameter and is of type integer. Below is an example of what i want to do. I keep on receiving error messages and would gladly receive any hints! Thanks.

parameter Integer f_min;
parameter Integer f_max;
Integer Freq_steigerung;
Integer array_size;
Integer Freq[:];

equation
array_size = ceil((f_max-f_min)/Freq_steigerung);
Freq[array_size] = f_min: Freq_steigerung: f_max;
Bridgers answered 25/2, 2017 at 15:19 Comment(0)
I
8

You cannot have arrays with variable size at runtime in Modelica. All array sizes need to be known at compile time, so the sizes need to be parameters or constants.

You can have functions (or records) containing components with unknown array sizes but they need to be bound at call time (so still known during compilation).

Something like this will work:

model T
  parameter Integer f_min;
  parameter Integer f_max;
  parameter Integer Freq_steigerung;
  parameter Integer array_size = integer(ceil((f_max-f_min)/Freq_steigerung));
  Integer Freq[array_size];
equation
  Freq = f_min: Freq_steigerung: f_max;
end T;
Iridissa answered 25/2, 2017 at 15:39 Comment(1)
It works. Actually, initialising out of the equation also works.Bridgers
H
4

In many cases you can make it even simpler:

model T
  parameter Integer f_min;
  parameter Integer f_max;
  parameter Integer Freq_steigerung;
  Integer Freq[:]= f_min: Freq_steigerung: f_max;
end T;
Happen answered 12/11, 2020 at 14:38 Comment(0)
F
3

Below is a related answer regarding unknown array sizes that is applicable when using functions.

The size command can be employed when the size of the original array is unknown but variables require that information in order to be instantiated. This use is shown below.

function test
    input Real[:] x1;
    input Real[size(x1,1)] x2;

    output Real[size(x1,1)] y;
algorithm
    y = x1.*x2;
end test;
Ferrous answered 27/2, 2017 at 22:7 Comment(0)
I
1

It is also possible to have the size of an array be detemined only in the algorithm part of the function. Here you dont even have to explicitly give the size of the array.

See the use below.

function T
  input Integer f_min;
  input Integer f_max;
  input Integer Freq_steigerung;
  output Integer Freq[:];

algorithm 
  Freq := f_min:Freq_steigerung:f_max;
end T;
Inglenook answered 3/11, 2020 at 8:25 Comment(2)
There is an error in your code, Freq should be FrewPivoting
sry there was i typo... I fixed itInglenook
P
1

If this were being used in a model you would have to create a function to calculate the size of the output and have this as an input to your T function (or at least when using Dymola):

See:

package TestT
  function T
  input Integer f_min;
  input Integer f_max;
  input Integer Freq_steigerung;
  input Integer n;
  output Integer Frew[n];
  algorithm 
  Frew := f_min:Freq_steigerung:f_max;
  end T;

  function Tc
  input Integer f_min;
  input Integer f_max;
  input Integer Freq_steigerung;
  output Integer n;
  algorithm 
    n :=size(f_min:Freq_steigerung:f_max, 1);
  end Tc;

  model Test
    parameter Integer n = TestT.Tc(1,3,1);
    Real a[n] = TestT.T(1,3,1,n);
  end Test;
end TestT;
Pivoting answered 12/11, 2020 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.