Opened 13 years ago
Closed 11 years ago
#2104 closed defect (fixed)
Algebraic expressions in when conditions not parsed correctly
| Reported by: | 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 , 13 years ago
comment:2 by , 13 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 , 13 years ago
| Cc: | added | 
|---|---|
| Owner: | changed from to | 
| Status: | new → assigned | 
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 , 13 years ago
| Resolution: | → duplicate | 
|---|---|
| Status: | assigned → closed | 
We have to increase our warnings/errors a lot. 
I guess we can close this, since #2105 discusses the same.
comment:5 by , 13 years ago
| Resolution: | duplicate | 
|---|---|
| Status: | closed → reopened | 
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 , 13 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 , 13 years ago
| Status: | reopened → assigned | 
|---|
Your first model is correct, and as far as I see we have a bug in findZeroCrossings for algorithms.
I'll check it.
comment:9 by , 11 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | assigned → closed | 
It seems to be fixed in the meanwhile.


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