替换 Modelica 中已弃用的函数基数 (c)

Replacement of deprecated function cardinality(c) in Modelica

documentation 中指出,cardinality() 函数已弃用,不应再使用。但是,它仍然在 ThermoSysPro 等库中使用。

例如

if (cardinality(C) == 0) then
 some code
end if;

其中 CFluidInletFluidOutlet

谁能举个简单的例子说明如何替换它?

我所知道的唯一可以在这方面使用的是 connectorSizing 注释。它在 MLS 第 18.7 章中有描述。

它在 Modelica 标准库中多次使用,例如在 Modelica.Blocks.Math.MinMax 中通过参数 nu。使用时,该工具会根据连接数自动设置修饰符nu

  parameter Integer nu(min=0) = 0 "Number of input connections"
    annotation (Dialog(connectorSizing=true));
  Modelica.Blocks.Interfaces.RealVectorInput u[nu];

在下面的示例中,nu=2 是在图形层中创建连接时由 Dymola 自动生成的。我删除了图形注释,使代码更具可读性。

model ExCS
  Modelica.Blocks.Math.MinMax minMax(nu=2);
  Modelica.Blocks.Sources.Sine sine(freqHz=6.28);
  Modelica.Blocks.Sources.Constant const(k=0.5);

equation 
  connect(sine.y, minMax.u[1]);
  connect(const.y, minMax.u[2]);
end ExCS;

通常的解决方案是使连接器有条件,如果启用,您需要连接它。

对于物理连接器,您可以在以下位置查看热端口和支持的处理方式: Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort Modelica.Mechanics.Rotational.Interfaces.PartialElementaryOneFlangeAndSupport2

对于控制信号,您可以看到如何处理 p_inh_inModelica.Fluid.Sources.Boundary_pT Modelica.Fluid.Sources.Boundary_ph

然而,ThermoSysPro 的连接器不属于这些类别,理想情况下也应该清理。

cardinality()运算符在Modelica.Fluid.Sources.BaseClasses.PartialSource中使用,在其他流体库(IBSPAAixLibBuildingsBuildingSystemsIDEAS),格式为

  // Only one connection allowed to a port to avoid unwanted ideal mixing
  for i in 1:nPorts loop
    assert(cardinality(ports[i]) <= 1,"
      each ports[i] of boundary shall at most be connected to one component.
      If two or more connections are present, ideal mixing takes
      place with these connections, which is usually not the intention
      of the modeller. Increase nPorts to add an additional port.
     ");
   end for;

我偶尔会收到来自用户的模型,这些模型最终以某种方式与 ports[i] 建立了多个连接。这是如何发生的并不清楚,但我发现使用 cardinality() 有助于捕捉这种情况,否则可能会导致用户无意在流体端口中混合并且难以检测。