Modelica - 有异常的迭代器?
Modelica - iterator with exception?
稍微概括的例子:
如何在定义模型方程时创建带异常的 for 循环?
以下作品:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in {1,3,5,6,7,8,9} loop
q[i] = 0;
end for;
end Test;
但我更愿意这样写:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in 1:9 and not in {2,4} loop
q[i] = 0;
end for;
end Test;
这可能吗?
这应该是可能的,只要你确保每个未知数都有一个方程。
可能不是完美的解决方案,但实际上可读性很好:
model LoopException
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in 1:9 loop
if Modelica.Math.Vectors.find(i, {2, 4}) == 0 then
q[i] = 0;
end if;
end for;
end LoopException;
作为替代方案,您也可以尝试编写“带迭代器的数组构造函数”(Modelica Language Spec. 第 10.4.1 节),但这可能会有点混乱...
使用 2 个辅助函数,您可以轻松地模拟您想要的东西:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in allExcept(1:9, {2,4}) loop
q[i] = 0;
end for;
end Test;
以下是您需要的功能:
function contains
"Check if vector v contains any element with value e"
input Integer vec[:];
input Integer e;
output Boolean result;
algorithm
result := false;
for v in vec loop
if v == e then
result := true;
break;
end if;
end for;
end contains;
function allExcept
"Return all elements of vector v which are not part of vector ex"
input Integer all[:];
input Integer ex[:];
output Integer vals[size(all, 1) - size(ex, 1)];
protected
Integer i=1;
algorithm
for v in all loop
if not contains(ex, v) then
vals[i] := v;
i := i + 1;
end if;
end for;
end allExcept;
请注意,Modelica 工具在翻译过程中通常需要知道向量的大小,尤其是在生成方程式时。因此,需要以下行:
output Integer vals[size(all, 1) - size(ex, 1)];
当all
或ex
包含重复元素时,它将失败。
因此,如果您尝试
之类的操作,模型将不会翻译
for i in allExcept(1:3, {2, 2}) loop
稍微概括的例子:
如何在定义模型方程时创建带异常的 for 循环?
以下作品:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in {1,3,5,6,7,8,9} loop
q[i] = 0;
end for;
end Test;
但我更愿意这样写:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in 1:9 and not in {2,4} loop
q[i] = 0;
end for;
end Test;
这可能吗?
这应该是可能的,只要你确保每个未知数都有一个方程。
可能不是完美的解决方案,但实际上可读性很好:
model LoopException
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in 1:9 loop
if Modelica.Math.Vectors.find(i, {2, 4}) == 0 then
q[i] = 0;
end if;
end for;
end LoopException;
作为替代方案,您也可以尝试编写“带迭代器的数组构造函数”(Modelica Language Spec. 第 10.4.1 节),但这可能会有点混乱...
使用 2 个辅助函数,您可以轻松地模拟您想要的东西:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in allExcept(1:9, {2,4}) loop
q[i] = 0;
end for;
end Test;
以下是您需要的功能:
function contains
"Check if vector v contains any element with value e"
input Integer vec[:];
input Integer e;
output Boolean result;
algorithm
result := false;
for v in vec loop
if v == e then
result := true;
break;
end if;
end for;
end contains;
function allExcept
"Return all elements of vector v which are not part of vector ex"
input Integer all[:];
input Integer ex[:];
output Integer vals[size(all, 1) - size(ex, 1)];
protected
Integer i=1;
algorithm
for v in all loop
if not contains(ex, v) then
vals[i] := v;
i := i + 1;
end if;
end for;
end allExcept;
请注意,Modelica 工具在翻译过程中通常需要知道向量的大小,尤其是在生成方程式时。因此,需要以下行:
output Integer vals[size(all, 1) - size(ex, 1)];
当all
或ex
包含重复元素时,它将失败。
因此,如果您尝试
for i in allExcept(1:3, {2, 2}) loop