如何在 modelica 中为 when 语句生成复杂事件?

How can I generate complex events in modelica for when statement?

我尝试使用此代码:

Real x,y;
Boolean trigger(start = true) 
when x < y and trigger then
   trigger = false;
end when;

我只想为 "when" 生成一次事件。但是我的代码不起作用。 如何在 modelica 中为 when 语句生成复杂事件?

您遇到的问题似乎是第一条错误消息 Internal error BackendDAETransform.analyseStrongComponentBlock failed (Sorry - Support for Discrete Equation Systems is not yet implemented)。这似乎是 https://trac.openmodelica.org/OpenModelica/ticket/1232,我认为这是由于在 when 语句中重新定义了部分条件变量造成的。

您可以使用 reinit 解决此问题。另见 Bouncing ball example and the reference。它需要作用于状态变量,这就是为什么我把 der(trigger) 放在那里。

model test_when
  Real trigger(start = 1.0, fixed = true);
equation
  der(trigger) = 0;
  when trigger > 0.5 and time > 5 then
    reinit(trigger, 0);
  end when;
  annotation(
    experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.02));
end test_when;

可能有更好的方法来实现这一点。还有其他人对此有意见吗?

您可以检查编译日志(统计信息 - 事件)以确认仅触发了一个事件。

在 Dymola 中您收到以下错误消息:

The computational causality analysis requires the variables trigger to be solved from the equation: when x < y and trigger then trigger = false; end when; however, the when condition also depends on the unknowns.

You may be able to cut the loop by putting 'pre' around these references in the when condition.

因此解决方案是:

Real x,y;
Boolean trigger(start = true) ;
equation
when x < y and pre(trigger) then
   trigger = false;
end when;

如您所见,这非常简单(并在 Dymola 中模拟),但我还没有在 OpenModelica 中检查过。