Modelica:将基于记录接口的记录传递给函数

问题描述 投票:0回答:1

我正在尝试创建一个以记录 (

CrossSection
) 作为输入的函数。然而,传递的记录是从记录接口派生的。它可以是不同可用的
CrossSections
之一。定义如下:

package CrossSections  
  partial record CrossSection
    constant String name="Generic";
    parameter SI.Area area "cross sectional area";
  end CrossSection;

  record Circle
    extends CrossSection(name="Circle",    
      final area = (d/2)^2*Modelica.Constants.pi);
    parameter Modelica.Units.SI.Diameter d "Diameter of the cross section";  
  end Circle;
  
  record Rectangle
    parameter SI.Length a,b;
    extends CrossSection(name="Rectangle",
      final area = a*b);
  end Rectangle;
end CrossSections

现在我的函数看起来像这样。根据传递的

CrossSection
形状,应执行不同的计算。

function foo
  input CrossSections.CrossSection cs;
  
  output Real o1;
  algorithm
    if Modelica.Utilities.Strings.isEqual("Circle", cs.name) then
      o1 := cs.area*3;
    elseif Modelica.Utilities.Strings.isEqual("Rectangle", cs.name) then
      o1 := cs.area*2;
    else
      o1 := cs.area*5;
    end if;
    return;
end foo;

但是,在这种情况下我收到错误:

Component 'cs' has partial type 'CrossSection'

此外,将输入更改为

replaceable input...
似乎也不起作用。

如何根据其接口传递记录?

physics modeling modelica openmodelica
1个回答
0
投票

您可以创建一个本地函数并使用您要使用的记录重新声明输入:

  model Example
    Real r;
  protected 
    function local_foo = foo(redeclare input CrossSections.Circle cs);
  equation 
    r = local_foo(Circle(d=1));
  end Example;
© www.soinside.com 2019 - 2024. All rights reserved.