给出一个实数数组(例如myArray
)和一个布尔数组(例如myMask
),我想拥有:
myMask[i] == true
,然后myArray[i] = myValueTrue
myMask[i] == false
,然后myArray[i] = myValueFalse
此功能有效
model BooleanIndexing
parameter Boolean myMask[3] = {false, true, true};
parameter Boolean myMask_negated[3] = {true, false, false};
Real myArray[3];
parameter Real myValueTrue = 3.0;
parameter Real myValueFalse = 5.0;
equation
myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] = fill(myValueFalse, Modelica.Math.BooleanVectors.countTrue(myMask_negated));
end BooleanIndexing;
但这不是
model BooleanIndexing
parameter Boolean myMask[3] = {false, true, true};
parameter Boolean myMask_negated[3] = not myMask;
Real myArray[3];
parameter Real myValueTrue = 3.0;
parameter Real myValueFalse = 5.0;
equation
myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] = fill(myValueFalse, Modelica.Math.BooleanVectors.countTrue(myMask_negated));
end BooleanIndexing;
唯一的区别是我初始化myMask_negated
的方式。
错误在OpenModelica中:
[BooleanIndexing: 9:3-9:139]: Illegal subscript Modelica.Math.BooleanVectors.index({myMask_negated[1], myMask_negated[2], myMask_negated[3]}) for dimensions 3 in component myArray[Modelica.Math.BooleanVectors.index(myMask_negated)].
[BooleanIndexing: 9:3-9:139]: Variable myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] not found in scope BooleanIndexing.
Error occurred while flattening model BooleanIndexing
在Dymola2018中
Translation of BooleanIndexing:
Failed to expand myArray[Modelica.Math.BooleanVectors.index(myMask)].
Errors or failure to expand the equation:
myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
Found in class BooleanIndexing, C:/workspace/modelica_vehicle/modelica_test/BooleanIndexing.mo at line 8.
Errors or failure to expand vector or matrix expressions.
Translation aborted.
直接布尔索引myArray[myMask]
在这里似乎不是解决方案。
我看不出它们为什么会失败,还有其他更优雅的解决方案。
答案 0 :(得分:2)
您的两个版本均不能保证生成正确数量的方程式。
myValueTrue
和myValueTrue_negated
都是参数,因此用户可以将向量的值更改为非互补的值。
因此,我建议设置
final parameter Boolean myMask_negated[3] = not myMask;
但这在Open Modelica和Dymola中均不起作用。
因此,我建议删除myMask_negated
并改为使用for循环。
可以使用两个单独的符号:
model BooleanIndexing
parameter Boolean myMask[3] = {false, true, true};
Real myArray[3];
parameter Real myValueTrue = 3.0;
parameter Real myValueFalse = 5.0;
equation
for i in Modelica.Math.BooleanVectors.index(myMask) loop
myArray[i] = myValueTrue;
end for;
for i in Modelica.Math.BooleanVectors.index(not myMask) loop
myArray[i] = myValueFalse;
end for;
end BooleanIndexing;
或使用数组构造函数的单个for循环,如下所示:
model BooleanIndexing2
parameter Boolean myMask[3] = {false, true, true};
Real myArray[3];
parameter Real myValueTrue = 3.0;
parameter Real myValueFalse = 5.0;
equation
myArray = {if value then myValueTrue else myValueFalse for value in myMask};
end BooleanIndexing2;