- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Problems with the number of iterations
Problems with the number of iterations
Problems with the number of iterations
Hi Guys,
I am trying to run Exercise 3 in Chapter 9 of Dr.Modelica, Algorithms. The algorithm is meant to find the sum of 1*(1*1)-1/(2*2)+1/(3*3)+1/(4*4)-1/(5*5)+1/(6*6).....
The code is implemented as below:
class SumCalculation
constant Real e = 0.000001;
Real sum(start = 0);
Real next(start = 1.0);
Integer k(start = 1);
Integer posneg(start = 1);
algorithm
while noEvent(abs(next)) >= e loop
sum := sum + next;
k := k + 1;
posneg := - posneg;
next := posneg/(k*k);
end while;
end SumCalculation;
When I try to simulate the above I get:
record SimulationResult
resultFile = "",
messages = "Simulation execution failed for model: SumCalculation
assert | debug | ERROR: Too many event iterations. System is inconsistent. Simulation terminate.
"
end SimulationResult;
I do not know why this code is giving me this error. I tried to decrease the number of iterations but I have the same problem. Could you please advice me where the problem might be?
Thank in advance
Re: Problems with the number of iterations
Hi,
as far as I see the solution in DrModelica there is wrong, since
the variables k is discrete and next is continuous. That means
they are treated different when the algorithm is initialized
(see Modelica Specification: 11.1.2).
So these both variables should treated the same.
I suggest to change all variables to the same variability:
Code:
class SumCalculation
constant Real e = 0.000001;
Real sum(start = 0);
Real next(start = 1.0);
Real k(start = 1);
Real posneg(start = 1);
algorithm
while noEvent(abs(next) >= e) loop
sum := sum + next;
k := k + 1;
posneg := - posneg;
next := posneg/(k*k);
end while;
end SumCalculation;
Also the noEvent() operator should applied
to the whole relation, since abs(v) is expanded to
noEvent(if v >= 0 then v else –v).
But actually the noEvent is not needed there at all, but
that a bug(see ticket #2618) in OpenModelica.
- wbraun
- 75 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Problems with the number of iterations