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

[bug] condition of an if-statement in an algorithm

[bug] condition of an if-statement in an algorithm

I just reported this bug for JModelica here in the PyFMI GitHub repository, but then I realized that OpenModelica also has then the same issue. Consider this simple code:

Code:


//test_1.mo
model test_1
  //parameters
  Real firstVar;
  Real secondVar;
  Real thirdVar;

algorithm
  firstVar := 1.0 * sin(time);
  secondVar := abs(firstVar);
  if (firstVar < secondVar) then
    thirdVar := 1;
  else
    thirdVar := -1;
  end if;
 
  annotation(
    experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-08, Interval = 0.02),
    __OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));
end test_1;

that results in:

https://i.imgur.com/EfGLhXw.png

click on the image to see the complete picture



which doesn't make sense. The "thirdVar" should return to -1 right after firstVar drops below secondVar.

Edited by: foadsf - Oct-04-19 12:02:24

Re: [bug] condition of an if-statement in an algorithm

I tried the if-statement in the equations section, the same problem:


Code:


model test_1
  //parameters
  Real firstVar;
  Real secondVar;
  Real thirdVar;

algorithm
  firstVar := 1.0 * sin(time);
  secondVar := abs(firstVar);
 
equation
 
  if (firstVar < secondVar) then
    thirdVar = 1;
  else
    thirdVar = -1;
  end if;
 
  annotation(
    experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-08, Interval = 0.02),
    __OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));
end test_1;

Re: [bug] condition of an if-statement in an algorithm

As I reported on this Tweet, Martin Sjölund replied to my question the Discord group, saying that the noEvent should be used for the conditions of the if-statement. I tested it for both OpenModelica and JModelica both working fine now.

Code:


model test_1
  //parameters
  Real firstVar;
  Real secondVar;
  Real thirdVar;

algorithm
  firstVar := 1.0 * sin(time);
  secondVar := abs(firstVar);
  if noEvent(firstVar < secondVar) then
    thirdVar := 1;
  else
    thirdVar := -1;
  end if;
 
  annotation(
    experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-08, Interval = 0.02),
    __OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "euler"));
end test_1;

but anyways it seems like bad behavior to me and I would consider changing this if I were the developer of these projects.  At least show the user some warning.



P.S. I kind of knew from past that adding noEvent is a good idea but I was kinda misguided by this answer suggesting that removing noEvent is OK.

Edited by: foadsf - Oct-04-19 14:56:47
There are 0 guests and 0 other users also viewing this topic
You are here: