OpenModelica中"when"和"if"之间的区别? [英] difference between 'when' and 'if' in OpenModelica?

查看:329
本文介绍了OpenModelica中"when"和"if"之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenModelica的新手,并且对随软件一起分发的'BouncingBall.mo'代码有一些疑问.

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)何时" 如果" 之间有什么区别?

1) what's the difference between 'when' and 'if'?

2)代码中变量'foo'的目的是什么?

2)what's the purpose of variable 'foo' in the code?

3)在第(15)行中-当{h< = 0.0且v< = 0.0,impact}时" ,不应将'when'用作表达式足以作为"{h< = 0.0和v< = 0.0}" ,因为当发生影响时,它变为 TRUE 的目的是什么>影响(对我来说是多余的),影响之前的逗号()是什么意思?

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;

推荐答案

好的,这是很多问题.让我尝试回答他们:

OK, that's quite a few questions. Let me attempt to answer them:

  1. whenif有什么区别.

when子句中的问题仅在when子句中使用的条件表达式变为活动状态时才处于活动状态".相反,只要条件表达式保持为真,if语句内的方程式就为真.

The questions inside a when clause are only "active" at the instant that the conditional expressions used in the when clause becomes active. In contrast, equations inside an if statement are true as long as the conditional expression stays true.

foo的目的是什么?

可能是可视化的.它对我所看到的模型没有明显的影响.

Probably for visualization. It has no clear impact on the model that I can see.

为什么在when子句中列出impact.

您所谓的像这样的Zeno系统中的问题之一就是它将继续以越来越小的间隔无限期地反弹.我怀疑这里的impact标志是用来指示系统何时停止弹跳.通常通过检查以确保条件表达式h<=0.0在某些时候实际上变为 false 来完成.因为事件检测包括数值公差,所以在某些时候反弹的高度永远不会超出公差范围,因此您需要检测到这一点,否则球就不会再反弹并且只会继续下降. (如果不实际运行模拟并看到效果,很难解释.)

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 expression h<=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).

,when子句中的作用.

请考虑以下内容:when {a, b} then.问题是,如果要在ab变为true时触发when子句,您可能会认为您会将其写为when a or b then.但这是不正确的,因为只有在第一个变为真时才会触发.为了更好地了解这一点,请考虑以下代码:

Consider the following: when {a, b} then. The thing is, if you want to have a when clause trigger when either a or b become true, you might think you'll write it as when 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;

因此,这里的公式集1将执行两次,因为它将在a变为true时执行,然后在b变为true时再次执行.但是等式集2仅在a变为true时才会被执行.那是因为整个表达式a or b只能在一瞬间变成真.

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.

这些是关于when的常见混淆点.希望这些解释有所帮助.

These are common points of confusion about when. Hopefully these explanations help.

这篇关于OpenModelica中"when"和"if"之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆