Gladson
Gladson

Reputation: 97

initialising array with unknown size in modelica

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;

Upvotes: 6

Views: 1488

Answers (5)

user2024223
user2024223

Reputation: 485

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;

Upvotes: 1

MMeissner
MMeissner

Reputation: 61

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;

Upvotes: 1

Hans Olsson
Hans Olsson

Reputation: 12507

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;

Upvotes: 4

Scott G
Scott G

Reputation: 2310

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;

Upvotes: 3

Adrian Pop
Adrian Pop

Reputation: 4231

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;

Upvotes: 8

Related Questions