问题
I'm new to OpenModelica and I've a few questions regarding the code of 'BouncingBall.mo' which is distributed with the software as example code.
1) what's the difference between 'when' and 'if'?
2)what's the purpose of variable 'foo' in the code?
3)in line(15) - "when {h <= 0.0 and v <= 0.0,impact}",, shouldn't the expression for 'when' be enough as "{h <= 0.0 and v <= 0.0}" because this becomes TRUE when impact occurs, what's the purpose of impact(to me its redundant here) and what does the comma(,) before impact means?
model BouncingBall
parameter Real e = 0.7 "coefficient of restitution";
parameter Real g = 9.81 "gravity acceleration";
Real h(start = 1) "height of ball";
Real v "velocity of ball";
Boolean flying(start = true) "true, if ball is flying";
Boolean impact;
Real v_new;
Integer foo;
equation
impact = h <= 0.0;
foo = if impact then 1 else 2;
der(v) = if flying then -g else 0;
der(h) = v;
when {h <= 0.0 and v <= 0.0,impact} then
v_new = if edge(impact) then -e * pre(v) else 0;
flying = v_new > 0;
reinit(v, v_new);
end when;
end BouncingBall;
回答1:
OK, that's quite a few questions. Let me attempt to answer them:
What is the difference between
when
andif
.The questions inside a
when
clause are only "active" at the instant that the conditional expressions used in thewhen
clause becomes active. In contrast, equations inside anif
statement are true as long as the conditional expression stays true.What's the purpose of
foo
?Probably for visualization. It has no clear impact on the model that I can see.
Why is
impact
listed in thewhen
clause.One of the problems you have so-called Zeno systems like this is that it will continue to bounce indefinitely with smaller and smaller intervals. I suspect the
impact
flag here is meant to indicate when the system has stopped bouncing. This is normally done by checking to make sure that the conditional expressionh<=0.0
actually becomes false at some point. Because event detection includes numerical tolerancing, at some point the height of the bounces never gets outside of the tolerance range and you need to detect this or the ball never bounces again and just continues to fall. (it's hard to explain without actually running the simulation and seeing the effect).What does the
,
do in thewhen
clause.Consider the following:
when {a, b} then
. The thing is, if you want to have awhen
clause trigger when eithera
orb
become true, you might think you'll write it aswhen a or b then
. But that's not correct because that will only trigger when the first one becomes true. To see this better, consider this code:
a = time>1.0; b = time>2.0; when {a, b} then // Equation set 1 end when; when a or b then // Equation set 2 end when;
So equation set 1 will get executed twice here because it will get executed when a
becomes true and then again when b
becomes true. But equation set 2 will only get executed once when a
becomes true. That's because the whole expression a or b
only becomes true at one instant.
These are common points of confusion about when
. Hopefully these explanations help.
来源:https://stackoverflow.com/questions/20016232/difference-between-when-and-if-in-openmodelica