Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

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.

Re: Problems with the number of iterations

Hi,

Thanks for the reply.  It is quite helpful.  I wanted to ask about the noEvent() operator, what does it implies?

Thank you for the help

Re: Problems with the number of iterations

Okey ! Thanks !

There are 0 guests and 0 other users also viewing this topic
You are here: