Opened 4 years ago

Closed 4 years ago

#4816 closed defect (fixed)

Incorrect handling of else when and reinit

Reported by: Christian Andersson Owned by: Jonathan Kämpe
Priority: major Milestone: 2.0.x
Component: CodeGen/C Version: trunk
Keywords: Cc:


When equations with an "elsewhen" together with multiple reinits results in an incorrect simulation.
Consider the test model:

model TestElseWhen
    Real x(start = 1);
    der(x) = -x;
  when x < 0.9 then
    reinit(x, 0.8);
  elsewhen x < 0.7 then
    reinit(x, 0.4);
  end when;
end TestElseWhen;

Here there should be two jumps, one when x == 0.9 and one when x==0.7. Simulating the above however results in only one jump. This is due to the code generation of the reinit statements (in model_ode_derivatives_base(..)):

    tmp_1 = _x_0;
    tmp_2 = _x_0;
    if (tmp_1 != _x_0) {
        _x_0 = tmp_1;
        jmi->reinit_triggered = 1;
    if (tmp_2 != _x_0) {
        _x_0 = tmp_2;
        jmi->reinit_triggered = 1;

Here we see that if tmp_1 has been changed (due to a reinit) the first if statement triggers and x is changed. However when testing the second if statement, this will now also trigger due to that x is no longer equal to tmp_2 (due to that x was just changed) and x will be changed back to its original value.

Change History (2)

comment:1 Changed 4 years ago by Christian Andersson


Added a check for reinit so that states are actually changed. If not, an error is triggered. Related to ticket:4816

comment:2 Changed 4 years ago by Jesper Mattsson

Resolution: fixed
Status: newclosed


Fixed C code generation of several reinit of same variable in different elsewhen branches.

Note: See TracTickets for help on using tickets.