【问题标题】:How to prevent unnecessary event detection in Modelica?如何防止 Modelica 中不必要的事件检测?
【发布时间】:2021-04-24 07:21:38
【问题描述】:

只有当 S2 为真时,我才能在 S1=... 语句中实现 y>1 的事件检测,否则会阻止它,因为它只会减慢一般情况下的模拟速度?

model zeroCrossingTest
// BOOLEAN VARIABLE DECLARATIONS 
Boolean S1(start=true, fixed=true);
Boolean S2(start=false, fixed=true);
Boolean S3(start=false, fixed=true);
Real y;
equation
der(y) = -y + 2;

// ================= 
// TRANSITIONS START 
S1 = if pre(S2) then (y>1) else 
        if pre(S1) then not ( (y>0.2)  )
 else false;

S2 = if pre(S1) then (y>0.2) else 
        if pre(S2) then not ( (y>0.5)  or  (y>1)  )
 else false;

S3 = if pre(S2) then (y>0.5) else 
        pre(S3);
end zeroCrossingTest; 

S1 的零交叉 = if pre(S2) then (y>1).. 仍然在 OpenModelica 中计算(时间状态事件 = 0.69),尽管 pre(S2) 为假

UPDATE1:不会产生不必要事件的替代实现.. 感谢@marco

model zeroCrossingTest_OriginalImplementation
  // BOOLEAN VARIABLE DECLARATIONS
  Boolean S1(start = true, fixed = true);
  Boolean S2(start = false, fixed = true);
  Boolean S3(start = false, fixed = true);
  Real y;
equation
  der(y) = -y + 2;
// =================
// TRANSITIONS START 
S1 = pre(S2) and y>1 or 
        pre(S1) and not y>0.2;
 
S2 = pre(S1) and y>0.2 or 
        pre(S2) and not ( y>0.5  or  y>1  );
 
S3 = pre(S2) and y>0.5 or 
        pre(S3);
end zeroCrossingTest_OriginalImplementation;

UPDATE2:一个更复杂的模型,它给出了布尔变量的相同行为,生成了不必要的状态事件(对于 y>0.7,y>0.8):

model zeroCrossingTest_OriginalImplementation
Boolean S1(start=true, fixed=true);
Boolean S2(start=false, fixed=true);
Boolean S3(start=false, fixed=true);
Boolean S4(start=false, fixed=true);
Boolean S5(start=false, fixed=true);
Boolean S6(start=false, fixed=true);
  Real y;
equation
  der(y) = -y + 2;
// ================= 
// TRANSITIONS START 
S1 = pre(S2) and (y>1) or 
        pre(S6) and (y>0.8) or 
        pre(S1) and not ( (y>0.2)  );
 
S2 = pre(S1) and (y>0.2) or 
        pre(S2) and not ( (y>0.5)  or  (y>1)  or  (y>0.6)  );
 
S3 = pre(S2) and (y>0.5) or 
        pre(S3);

S4 = pre(S2) and (y>0.6) or 
        pre(S4) and not ( (y>0.65)  );
 
S5 = pre(S4) and (y>0.65) or 
        pre(S5) and not ( (y>0.7)  );
 
S6 = pre(S5) and (y>0.7) or 
        pre(S6) and not ( (y>0.8)  );
end zeroCrossingTest_OriginalImplementation;

更新 3:此实现仅生成必要的事件(总共 2 个事件)。有什么理由反对这种方法?

model zeroCrossingTest_OriginalImplementation
Boolean S1(start=true, fixed=true);
Boolean S2(start=false, fixed=true);
Boolean S3(start=false, fixed=true);
Boolean S4(start=false, fixed=true);
Boolean S5(start=false, fixed=true);
Boolean S6(start=false, fixed=true);

Real inS1;
Real inS2;
Real inS3;
Real inS4;
Real inS5;
Real inS6;

  Real y;
equation
  der(y) = -y + 2;
  
// ===================== 
// inState Variables 
inS1= if pre(S1) then 1 else 0;
inS2= if pre(S2) then 1 else 0;
inS3= if pre(S3) then 1 else 0;
inS4= if pre(S4) then 1 else 0;
inS5= if pre(S5) then 1 else 0;
inS6= if pre(S6) then 1 else 0;
  
// ================= 
// TRANSITIONS START 
S1 = pre(S2) and (inS2*y>1) or 
        pre(S6) and (inS6*y>0.8) or 
        pre(S1) and not ( (inS1*y>0.2)  );
 
S2 = pre(S1) and (inS1*y>0.2) or 
        pre(S2) and not ( (inS2*y>0.5)  or  (inS2*y>1)  or  (inS2*y>0.6)  );
 
S3 = pre(S2) and (inS2*y>0.5) or 
        pre(S3);

S4 = pre(S2) and (inS2*y>0.6) or 
        pre(S4) and not ( (inS4*y>0.65)  );
 
S5 = pre(S4) and (inS4*y>0.65) or 
        pre(S5) and not ( (inS5*y>0.7)  );
 
S6 = pre(S5) and (inS5*y>0.7) or 
        pre(S6) and not ( (inS6*y>0.8)  );
end zeroCrossingTest_OriginalImplementation;

【问题讨论】:

  • 我知道,这不是一个真正的答案,但 Dymola 不会在 0.69 秒时计算事件。只是为了表明似乎可以避免计算......
  • 关于原始帖子和更新 1 和 2:正如 Hans Olsson 在他的评论中指出的那样,OpenModelica 的行为很奇怪,因为 if-then-else 方法应该有效。我建议在trac.openmodelica.org/OpenModelica 打开一个错误报告

标签: modelica openmodelica


【解决方案1】:

我不是 OpenModelica 专家,我不知道为什么 S1 在 OpenModelica 中会在 0.69 秒生成和事件(但在 Dymola 中不是,正如 Markus A. 已经指出的那样)。

但在这种情况下,可以通过将 y > 1 比较移到 if 条件中来防止不必要的事件:

  S1 = if pre(S2) and y > 1 then true 
       else if pre(S1) and not y > 0.2 then true 
       else false;

请注意,我通过考虑 precedence of operators 删除了大多数大括号。

你也可以删除 else if 分支:

  S1 = if pre(S2) and y > 1 or 
          pre(S1) and not y > 0.2 
       then true 
       else false;

由于比较已经返回 true 或 false,我们可以删除 if 并简单地编写:

  S1 = pre(S2) and y > 1 or 
       pre(S1) and not y > 0.2;

【讨论】:

  • 这正是我实现它的第一种方式,我非常有信心(尽管模型更复杂),它也会生成事件。但是现在当我用这个简单的模型重新检查时,你是对的,这不会在 0.69 生成事件。谢谢
  • 这很奇怪,因为 'and' 不能保证 Modelica 中的短路评估(工具仍然可以做到),而 if-then-else 应该保证不评估未选择的分支.
猜你喜欢
  • 2021-07-25
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
相关资源
最近更新 更多