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.
我需要一些帮助来初始化openmodelica中的数组。我创建了一个modelica类,它应该生成一个可变大小的数组。大小将设置为一个参数,类型为integer。下面是我想做的一个例子。我一直在接收错误消息,并愿意接受任何提示!谢谢。
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;
2 个解决方案
#1
4
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.
在Modelica中不能使用在运行时具有可变大小的数组。在编译时需要知道所有的数组大小,所以大小需要是参数或常量。
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;
#2
0
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.
当原始数组的大小未知时,可以使用size命令,但是变量需要该信息才能实例化。如下所示。
function test
input Real[:] x1;
input Real[size(x1,1)] x2;
output Real[size(x1,1)] y;
algorithm
y = x1.*x2;
end test;
#1
4
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.
在Modelica中不能使用在运行时具有可变大小的数组。在编译时需要知道所有的数组大小,所以大小需要是参数或常量。
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;
#2
0
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.
当原始数组的大小未知时,可以使用size命令,但是变量需要该信息才能实例化。如下所示。
function test
input Real[:] x1;
input Real[size(x1,1)] x2;
output Real[size(x1,1)] y;
algorithm
y = x1.*x2;
end test;