When and If in Modelica

扶醉桌前 提交于 2019-12-12 04:43:51

问题


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.


回答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.




回答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.



来源:https://stackoverflow.com/questions/26149006/when-and-if-in-modelica

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!