Opened 12 years ago

Closed 10 years ago

#2104 closed defect (fixed)

Algebraic expressions in when conditions not parsed correctly

Reported by: jwharington@… Owned by: Willi Braun
Priority: high Milestone: 1.9.1
Component: Backend Version: trunk
Keywords: Cc: Jens Frenkel, Willi Braun, Lennart Ochel

Description

Depending on the order of the conditional expression, when conditions don't function correctly in all cases.

Example code:

class bugtime
  discrete Real timeAtActivation;
  Boolean next;

  algorithm

    // this works
    when (time>0.1) then
      timeAtActivation := time;
      next := true;
    end when;

    // this works
    when next and (time>0.1+timeAtActivation) then
      print("time>0.1+timeAtActivation");
    end when;

    // this doesnt work
    when next and (time-timeAtActivation>0.2) then
      print("when time-timeAtActivation>0.2");
    end when;

    // this one does work
    if next and (time-timeAtActivation>0.3) then
      print("if time-timeAtActivation>0.3");
      next:= false;
    end if;

  annotation( experiment( StopTime=2, method='euler' ) );

end bugtime;

Change History (9)

comment:1 by Martin Sjölund, 12 years ago

Note that this model isn't valid (see #2105). Does it work correctly if you use elsewhen?

comment:2 by jwharington@…, 12 years ago

Using elsewhen as you recommend, as shown below, doesn't even compile.

Error: Internal error Inverse Algorithm needs to be solved for {next,$whenCondition3,$whenCondition1,$whenCondition2,timeAtActivation} }}}


{{{
class bugtime
  discrete Real timeAtActivation;
  Boolean next;
algorithm
  when time > 0.1 then
    timeAtActivation := time;
    next := true;
  elsewhen next and time > 0.1 + timeAtActivation then
    print("time>0.1+timeAtActivation");
  elsewhen next and time - timeAtActivation > 0.2 then
    print("when time-timeAtActivation>0.2");
  end when;
end bugtime;
}}}

comment:3 by Martin Sjölund, 12 years ago

Cc: Jens Frenkel Willi Braun Lennart Ochel added
Owner: changed from probably noone to Willi Braun
Status: newassigned

The following works fine, so I guess the backend developers forgot some cases... when-equation (which should be recommended over when-statements) didn't work regardless.

class bugtime
  discrete Real timeAtActivation;
  Boolean next;
algorithm
  when time > 0.1 then
    timeAtActivation := time;
    next := true;
  elsewhen next and time > 0.1 + timeAtActivation then
    timeAtActivation := pre(timeAtActivation);
    next := pre(next);
    print("time>0.1+timeAtActivation");
  elsewhen next and time - timeAtActivation > 0.2 then
    timeAtActivation := pre(timeAtActivation);
    next := pre(next);
    print("when time-timeAtActivation>0.2");
  end when;
end bugtime;

comment:4 by Lennart Ochel, 12 years ago

Resolution: duplicate
Status: assignedclosed

We have to increase our warnings/errors a lot.
I guess we can close this, since #2105 discusses the same.

comment:5 by jwharington@…, 12 years ago

Resolution: duplicate
Status: closedreopened

Sorry to re-open this, but the example sjoelund provides doesn't work correctly.

It does compile yes, but note that when it is run,
"when time-timeAtActivation>0.2" is not printed out. In other words, a simple rearranging of the time/timeAtActivation condition means the condition is or isn't caught.

Example

class bugtime
  discrete Real timeAtActivation;
  Boolean next;
algorithm
  when time > 0.1 then
    timeAtActivation := time;
    next := true;
  elsewhen next and time - timeAtActivation > 0.1 then
    timeAtActivation := pre(timeAtActivation);
    next := pre(next);
    print("this is never seen");
  end when;
end bugtime;

Can you see now?

  • "time-timeAtActivation > 0.1" fails to be triggered
  • "time > 0.1 + timeAtActivation" is correctly triggered

comment:6 by jwharington@…, 12 years ago

Further to my previous re-opening, I don't believe it's a duplicate of #2105. Even if I'm doing something wrong in this example code, the problem in this particular case is to do with parsing or evaluation of when conditions, which is a different kind of problem to #2105.

And also, thank you very much everyone that you responded to the bug report so quickly!

comment:7 by Willi Braun, 12 years ago

Status: reopenedassigned

Your first model is correct, and as far as I see we have a bug in findZeroCrossings for algorithms.
I'll check it.

comment:8 by Martin Sjölund, 11 years ago

Milestone: 1.9.01.9.1

Postponed until 1.9.1

comment:9 by Willi Braun, 10 years ago

Resolution: fixed
Status: assignedclosed

It seems to be fixed in the meanwhile.

Note: See TracTickets for help on using tickets.