在Modelica中少于或不等于或不等于或相等?

发布于 2025-02-10 04:38:40 字数 581 浏览 2 评论 0原文

我尝试了一个使用OpenModelica v1.20.0的简单示例,但是我发现结果令人困惑。

代码如下:

model test
  Boolean state1;
  Boolean state2;
  Real f;
equation
  f = if time<1 then 0.5 else if time<3 then 0.4 else if time<5 then 0.3 else if time<7 then 0.4 else 0.5;
  state1 = f<=0.4;
  state2 = f<0.4 or f==0.4;
end test;

相应的结果如下:

显然,state1(&lt; =)的结果不是等于状态2(&lt; or ==),而状态1不是所需的结果。

为什么?

I tried a simple example with OpenModelica v1.20.0, but I find a confusing results.

The code is as follows:

model test
  Boolean state1;
  Boolean state2;
  Real f;
equation
  f = if time<1 then 0.5 else if time<3 then 0.4 else if time<5 then 0.3 else if time<7 then 0.4 else 0.5;
  state1 = f<=0.4;
  state2 = f<0.4 or f==0.4;
end test;

And the corresponding result is as follows:
simulation result

Obviously, the result of state1(<=) is not equal state2(< or ==), and state1 is not a desired result.

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉酒的小男人 2025-02-17 04:38:40

在Modelica中有一些需要考虑的事情:

我写的原因理想是是它是它并非总是有可能确保在事件中从字面上处理关系。该版本在Modelica中指定为3.2版;但是在实践中没有工作,因此后来被删除。

There are some things to consider in Modelica:

The reason I write ideally is that it is not always possible to guarantee that relations are handled literally at events. That was specified in Modelica up to version 3.2; but did not work in all cases in practice and has thus later been removed.

毅然前行 2025-02-17 04:38:40

我怀疑浮点问题,尽管行为仍然很奇怪。

我修复了有关,使用<代码> == 在函数外部的reals (您也应该看到),但行为与图中的行为相同。
我还试图确保所有比较都使用相同的参数0.4,以避免在不同的0.4文字之间浮动点不准确,并使用标称属性缩放f

即使使用&lt; = state1(在“等式”部分)和state2(由函数设置)di dim not 为state1state2提供相同的图。

model test
Boolean state1;
Boolean state2;
parameter Real threshold = 0.4;
Real f(nominal=0.5);

  function compare
  input Real a;
  input Real b;
  output Boolean res;
  algorithm
  res := a < b or a == b;
  //res := a <= b; // Even this does not produce the same result as state1
  end compare;
equation
  f = if time<1 then 0.5 else if time<3 then threshold else if time<5 then 0.3 else if time<7 then threshold else 0.5;
  state1 = f<=threshold;
  state2 = compare(f, threshold);

annotation(
    experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.02));
end test;

编辑:这似乎连接到事件解决方案,我打开了有关&lt; =操作员的行为的错误报告,请 https://github.com/openmodelica/openmodelica/openmodelica/issues/9140

现在,您可以使用state1 = noevent(f&lt; = threshold)来解决它;,而是。这似乎也可以在OM中正常工作。

编辑/分辨率:该问题已用您可能不应该这样写Modelica。一个不太合成的示例可能有助于解决您遇到的基本建模问题。

I suspect floating-point issues, although behaviour remains weird.

I fixed a translation warning about using == with Reals outside a function (you should see that too), but behaviour is the same as in your plot.
I also tried to make sure all comparisons use the same parameter 0.4, to avoid floating-point inaccuracies between the different 0.4 literals, and scaled f with a nominal attribute.

Even using <= for both state1 (in the equation section) and state2 (set by a function) does not produce the same plot for both state1 and state2.

model test
Boolean state1;
Boolean state2;
parameter Real threshold = 0.4;
Real f(nominal=0.5);

  function compare
  input Real a;
  input Real b;
  output Boolean res;
  algorithm
  res := a < b or a == b;
  //res := a <= b; // Even this does not produce the same result as state1
  end compare;
equation
  f = if time<1 then 0.5 else if time<3 then threshold else if time<5 then 0.3 else if time<7 then threshold else 0.5;
  state1 = f<=threshold;
  state2 = compare(f, threshold);

annotation(
    experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.02));
end test;

Edit: this seems connected to event resolution, I opened a bug report about the behaviour of the <= operator at https://github.com/OpenModelica/OpenModelica/issues/9140

For now, you can work around it by using state1 = noEvent(f<=threshold);, instead. This seems to work correctly in OM, too.

Edit/Resolution: The issue has been clarified with a comment -- you should probably not write Modelica like that. A less synthetic example might help solve the underlying modelling issue you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文