1

Hi I have some puzzles about event and when in Modelica. Below is my code:

model test
  Integer bar(start=5, fixed=true);
equation 
  when (time < 2) then
    bar = 1;
  end when;
  annotation(experiment(StopTime=3));
end test;

My question is why I got 5 instead of 1 when time is less than 2? How can I understand the event(time < 2) in this case? What is the difference of when clause in Modelica and other programming language, like c.

Wei
  • 87
  • 1
  • 9

2 Answers2

2

The when equation is only active when the condition becomes true. In your case the condition time < 2 is true from the beginning and only becomes false.

The when-block can be intentionally translated to

b = time < 2;

if not(pre(b)) and b then
  bar = 1;
else
  bar = pre(bar);
end 

For further information you can consult the specification https://modelica.org/documents/ModelicaSpec33Revision1.pdf.

Tobias
  • 5,038
  • 1
  • 18
  • 39
  • your mean BECOME, which indicates the condition should change from FALSE to TRUE. Thanks. – Wei Oct 01 '14 at 19:43
  • @user3658215 Yes, the emphasis of 'become' is important. Thanks for the remark. I modified the answer in this regard. – Tobias Oct 02 '14 at 06:12
1

Tobias' answer is correct. But I think for beginners it might be a little daunting to invoke the pre construct or send them to the specification. So in addition to Tobias' answer, I would point the interested reader to this question as well as this chapter in my book. Of specific interest (I suspect) would be this subsection on when and how it is different from if.

Community
  • 1
  • 1
Michael Tiller
  • 9,291
  • 3
  • 26
  • 41