Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#5188 closed discussion (invalid)

When not triggering in initialization

Reported by: andreas.heuermann@… Owned by: Lennart Ochel
Priority: normal Milestone: Future
Component: Initialization Version: v1.13.0-dev-nightly
Keywords: when, if edge, initial equation Cc:

Description

Is the following behavior for when equations while initializing desired? When simulating the following model a stays 1, so the when equation is not evaluated.

model initial_when
  Real x(start=1), dx;
  discrete Real a(start=1);
  Boolean y(start=false);
  Boolean z(start=false);
  Boolean h1(start=false, fixed=true), h2;

equation
  der(x) = dx;
  dx = a*x;
  
  h1 = x>=0;
  h2 = dx>=2;
  
  when h1 then // equivalent to "if edge(h1) then" // equivalent to "if h1 and not pre(h1) then" 
    y = true;
  end when;
  
  when y then
    a = 2.0;
  end when;
  
  when h2 then
    z = true;
  end when;
  
end initial_when;

If

when h1 then
  y = true;
end when;

is replaced with

  if edge(h1) then
    y = true;
  else
    y = pre(y);
  end if;

y is set to true as expected. Of course a is still wrong.
Dymola is returning the same result as OpenModelica in both cases.

Change History (1)

comment:1 by Francesco Casella, 6 years ago

Resolution: invalid
Status: newclosed

From the Modelica Specification, Section 8.6

The equations of a when-clause are active during initialization, if and only if they are explicitly enabled with the initial()} operator; and only in one of the two forms when initial() then or when {...,initial(), ...} then.

So, as far as I understand, the behaviour of both tools is correct.

If you replace when with if, then it's a different story, as the above-stated rule no longer applies. Of course this only holds for y, the other when clauses remain inactive, so the outcome is correct again.

Last edited 6 years ago by Francesco Casella (previous) (diff)
Note: See TracTickets for help on using tickets.