问题
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;
回答1:
Looks like a tool issue. On the other hand, why would you calculate z for k=1 and never use it?
回答2:
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:
- usage of delay
- in an algorithm section
- inside a for loop
Hence, you have to get rid of either of those.
Workarounds
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;
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;
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_;
回答3:
This is a bug in OpenModelica. A ticket for it was created: https://trac.openmodelica.org/OpenModelica/ticket/5572
来源:https://stackoverflow.com/questions/56916012/error-due-to-delay-operator-in-algorithm-section