尝试在 Dymola 中编译零大小的记录变量时出错

Error when trying to compile a zero-sized record variable in Dymola

我正在根据组件中建立的端口连接数创建一组记录。实际组件在连接数为 1 或更多时工作正常,但在没有建立连接时不起作用。寻找有关如何解决或解决方法的意见。

这是错误:

下面是一个 MWE:

model RecordIssue
  record Record
    Real a=1;
  end Record;

  parameter Integer nPorts=0 annotation (Dialog(connectorSizing=true));

  Modelica.Fluid.Interfaces.FluidPorts_a ports_a[nPorts];

  // declare record array instance for each port connection
  parameter Record[nPorts] recordInstance={Record() for i in 1:nPorts};

  // this next line is an issue only when nPorts = 0 or no connections made
  Real test[:]=recordInstance[:].a;
end RecordIssue;

expected/desired 的行为是变量 testnPorts=0 时不存在,这是 Real test[:] = {1.0 for i in 1:nPorts};

之类的正常行为

解决方法是确保记录变量永远不会为空并避免在 nPorts 为零时访问。

model RecordIssue
  record Record
    Real a=1;
  end Record;

  record Dummy
    extends Record(a=Modelica.Constants.inf);
  end Dummy;

  parameter Integer nPorts = 0 annotation (Dialog(connectorSizing=true));

  Modelica.Fluid.Interfaces.FluidPorts_a ports_a[nPorts];

  // declare record array and ensure that at least one exists
  parameter Record[max(nPorts, 1)] recordInstance = if nPorts > 0 then {Record() for i in 1:nPorts} else {Dummy()};
  
  // decide here if recordInstance shall be used
  Real test[nPorts] = if nPorts > 0 then recordInstance[:].a else fill(0, 0);
end RecordIssue;