由于算法部分中的延迟运算符而导致错误 [英] Error due to delay operator in algorithm section
问题描述
我已经在一个类的算法部分实现了一个延迟运算符,如下面的测试用例所示,但是在Open modelica中执行代码期间,我遇到了以下错误。我该如何解决该问题?
I have implemented a delay operator in algorithm section of a class as shown in the test case below, but during the execution of the codes in Open modelica, I face the below error. how can I fix the problem?
model test3
Real x=sin(377*time);
Real z;
parameter Real tau[:]={0.01,0.02};
equation
algorithm
for k in 1: 2 loop
z:=delay(x,tau[k]);
end for;
end test3;
推荐答案
像tbeu在回答中所说,这是一个问题OpenModelica。在Dymola中,您的示例将按预期进行模拟。因此,请在此处报告问题。
Like tbeu said in his answer, its an issue in OpenModelica. In Dymola your example simulates as expected. So please report the issue here.
研究一下,我意识到以下组合会阻止您的模型转换:
Investigating a bit I realized that the following combination prevents your model from translating:
- 延迟的使用
- 在算法部分
- 在for循环内
因此,您必须
如果您知道的大小,提前tau [:]
,使用分隔行来计算 z
而不是for循环:
If you know the size of tau[:]
in advance, use separate lines for the computation of z
instead of the for loop:
model sep_lines
Real x = sin(8 * time);
Real z[2];
parameter Real tau[2] = {0.02, 0.01};
equation
algorithm
z[1] := delay(x, tau[1]);
z[2] := delay(x, tau[2]);
end sep_lines;
也许您可以使用等式部分代替算法部分。然后,您根本不需要for循环,因为如果需要,函数调用(在本例中为 delay
)会自动矢量化。您的代码将减少为:
Maybe you can use an equation section instead of the algoirthm section. Then you don't need the for loop at all, since function calls (in this case the delay
) are vectorized automatically if needed. Your code will reduce to:
model eqs
Real x = sin(8 * time);
Real z[3];
parameter Real tau[3] = {0.03, 0.02, 0.01};
equation
z = delay(x, tau);
end eqs;
额外问题
如果for循环模型会用 while循环代替模型进行翻译和模拟。但是延迟信号 z [1]
不正确,并且误差随着 x $ c的频率而增加$ c>。在1 Hz之类的低频下,它几乎看不见,但是频率例如20 Hz幅度是错误的。因此,不要被这个解决方案所迷惑。
Extra issue
If the for loop is replaced with a while loop the model translates and simulates. But the delayed signal z[1]
is not correct and the error increases with the frequency of x
. At low frequencies like 1 Hz its barely visible, but with a frequency of e.g. 20 Hz the amplitude is considerably wrong. So don't get fooled by this solution.
model while_ "Compiles, but the amplitude of z[1] is wrong"
Real x = sin(2*Modelica.Constants.pi * 20 * time);
Real z[size(tau, 1)];
parameter Real tau[:] = {0.02, 0.01};
protected
Integer i;
algorithm
i := 0;
while i < size(tau, 1) loop
i := i + 1;
z[i] := delay(x, tau[i]);
end while;
end while_;
这篇关于由于算法部分中的延迟运算符而导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!