在Modelica中少于或不等于或不等于或相等?
我尝试了一个使用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:
Obviously, the result of state1(<=) is not equal state2(< or ==), and state1 is not a desired result.
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Modelica中有一些需要考虑的事情:
state1
应该理想情况下,在1到7s中仍然是正确的,其中f&lt; = 0.4,否则为false; https://specification.modelica.modelica.orgg/maint/3.5/equication 。 “ alt =” dymola。我写的原因理想是是它是它并非总是有可能确保在事件中从字面上处理关系。该版本在Modelica中指定为3.2版;但是在实践中没有工作,因此后来被删除。
There are some things to consider in Modelica:
state1
should ideally still be true from 1 to 7s where f<=0.4, and false otherwise; https://specification.modelica.org/maint/3.5/equations.html#events-and-synchronization So simulating that in Dymola generatesstate2
cannot be defined like that https://specification.modelica.org/maint/3.5/operators-and-expressions.html#equality-relational-and-logical-operatorsThe 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.
我怀疑浮点问题,尽管行为仍然很奇怪。
我修复了有关,使用<代码> == 在函数外部的reals (您也应该看到),但行为与图中的行为相同。
我还试图确保所有比较都使用相同的参数0.4,以避免在不同的
0.4
文字之间浮动点不准确,并使用标称属性缩放f
。即使使用
&lt; =
state1
(在“等式”部分)和state2
(由函数设置)di dim not 为state1
和state2
提供相同的图。编辑:这似乎连接到事件解决方案,我打开了有关
&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 scaledf
with a nominal attribute.Even using
<=
for bothstate1
(in the equation section) andstate2
(set by a function) does not produce the same plot for bothstate1
andstate2
.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/9140For 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.