3

下面的 Modelica 模型通过了验证:

model TestController
  parameter Real off[4] = fill(20, 4) "Radiator turn off threshold";
  parameter Real on[4] = fill(19, 4) "Radiator turn on threshold";
  discrete Modelica.Blocks.Interfaces.RealInput x[4];
  output Modelica.Blocks.Interfaces.BooleanOutput h[4];
protected
  Boolean has_heater[4];
equation
  has_heater = {false, true, true, false};
algorithm
  for j in 1:4 loop
    h[j] := has_heater[j] and ((not h[j] and x[j] <= on[j]) or (h[j] and x[j] < off[j]));
  end for;
end TestController;

但是当我尝试模拟它时,编译代码中出现错误:

sme.12.0.0_1575385723_1403176131_main.cpp: In function ‘int function_zeroCrossing(f2c_integer*, double*, double*, f2c_integer*, double*, double*, f2c_integer*)’:
sme.12.0.0_1575385367_109695398_main.cpp:347:35: error: ‘$i1_j’ was not declared in this scope
  349 |   base_array_range_check1(&$tmp0, $i1_j - 1, "[:0:0-0:0]", "x[$i1_j]");
      |        

一定有一些基本的东西我不理解,为什么这个循环不模拟?如果我删除 h 值的第二个子句并使其简单地h[j] := has_heater[j]模拟工作。

4

2 回答 2

2

只是一个想法,作为答案发布,因为评论太长了:也许内联 for 循环工作得更好,而不是

  for j in 1:4 loop
    h[j] := has_heater[j] and ((not h[j] and x[j] <= on[j]) or (h[j] and x[j] < off[j]));
  end for;

试试看

  h := {has_heater[j] and ((not h[j] and x[j] <= on[j]) or (h[j] and x[j] < off[j])) for j in 1:4};

另见https://github.com/mtiller/ModelicaBook/issues/338

于 2019-12-03T21:18:29.743 回答
2

一般来说,避免使用 Modelica 中的算法,而是使用方程是一个好主意。我相信这很可能在 System Modeler 中工作(这和 Dymola 中的原始作品)。

model TestController
  parameter Real off[4] = fill(20, 4) "Radiator turn off threshold";
  parameter Real on[4] = fill(19, 4) "Radiator turn on threshold";
  discrete Modelica.Blocks.Interfaces.RealInput x[4];
  output Modelica.Blocks.Interfaces.BooleanOutput h[4];
protected
  Boolean has_heater[4];
equation
  has_heater = {false, true, true, false};
  for j in 1:4 loop
    h[j] := has_heater[j] and ((not pre(h[j]) and x[j] <= on[j]) or (pre(h[j]) and x[j] < off[j]));
  end for;
end TestController;

在这种情况下,它并不完全是微不足道的,因为您必须pre(h[j])在右侧使用,而对于算法来说,它是自动处理的。

于 2019-12-03T16:45:44.690 回答