【问题标题】:Is it possible to set a variable type via a parameter in Modelica?是否可以通过 Modelica 中的参数设置变量类型?
【发布时间】:2016-10-25 06:03:37
【问题描述】:

我为可配置变量开发了一个模型,这意味着该变量可以由参数定义,通过实际输入指定,或者未定义以便可以求解。

model ConfigurableReal   

"Configurable variable that can be set by parameter, by real input, or left undetermined"

  parameter Boolean isSpecifiedByParameter = false 
    "true if value is specified by paramter"
    annotation(Dialog(group="Specify by Parameter", enable = not isSpecifiedByInput));

  parameter Boolean isSpecifiedByInput = false 
    "true if value is specified by real input"
    annotation(Dialog(group = "Specify by Real Input", enable=not isSpecifiedByParameter));

  parameter Real specifiedValue(unit=unitString) = 0 
    "specified value to use if isSpecifiedByParameter == true"
    annotation(Dialog(group="Specify by Parameter", enable = isSpecifiedByParameter));

  // Conditionally declare the realInput
  Modelica.Blocks.Interfaces.RealInput realInput if isSpecifiedByInput
    annotation (Placement(transformation(extent={{-140,-20},{-100,20}})));

  Real value "active value";

protected 
  Modelica.Blocks.Sources.Constant constantBlock(final k = specifiedValue) if isSpecifiedByParameter 
    "constant block to hold specified input";

  Modelica.Blocks.Interfaces.RealInput value_ "connected value";

equation 
  value = value_;

  // Connect the real input to the value if the real input exists
  // This only happens if isSpecifiedByInput==true, because the RealInput is
  // conditionally declared above
  connect(realInput, value_);

  // Connect the constant block to the value if the value is specified by parameter
  // This only happens if isSpecifiedByParameter == true, because the Constant is
  // conditionally declared above  
  connect(constantBlock.y, value_);

   annotation (Icon(coordinateSystem(preserveAspectRatio=false), graphics={
        Rectangle(
          extent={{-80,40},{80,-40}},
          lineColor={28,108,200},
          fillColor={255,255,255},
          fillPattern=FillPattern.Solid),
        Text(
          extent={{-70,30},{70,-32}},
          lineColor={28,108,200},
          textString="Config
Variable"),
        Text(
          extent={{-100,80},{100,50}},
          lineColor={28,108,200},
          fillColor={255,255,255},
          fillPattern=FillPattern.Solid,
          textString=DynamicSelect("", "value = " + String(value, significantDigits=4))),
        Text(
          extent={{-100,-50},{100,-80}},
          lineColor={28,108,200},
          fillColor={255,255,255},
          fillPattern=FillPattern.Solid,
          textString="%name")}),
        Diagram(
        coordinateSystem(preserveAspectRatio=false)));
end ConfigurableReal;

现在我想以利用数量/单位类型而不是仅利用 Reals 的方式扩展模型。是否可以通过参数分配变量类型?例如,是否可以如下声明一个变量?

parameter String variableType = "Pressure";
parameter typeOf(variableType) variableType;

如果是这样,我可以在 ConfigurableReal 模型中定义变量类型来引用 variableType 对象并通过以下方式编写可配置的压力模型:

model ConfigurablePressure extends ConfigurableReal(final variableType="Pressure");

如果没有,Modelica 中是否有类似 C# 类型参数的东西?

最后,我真的想找到一种方法来扩展我的 ConfigurableReal 模型,以便它可以是 ConfigurablePressure、ConfigurableTemperature、ConfigurableMassFlowRate、...,包含参数输入和模拟结果中的单位,而无需复制和粘贴每个变量类型的模型代码并手动分配每个类中的变量类型。

非常感谢任何帮助。

谢谢, 贾斯汀

【问题讨论】:

    标签: modelica dymola


    【解决方案1】:

    贾斯汀,你真正想要的是使用redeclare + replaceable。您提到了 C# 类型参数。 Modelica 确实有类似的东西,但它们看起来(在语法上)不太一样,而且由于 Modelica 的数学约束,它们还有一些额外的约束。

    如果你还没有读过我书中的the chapter on Architecture,那你就开始吧。

    为了完成对 C# 类型参数的类比,请考虑以下模型:

    model Circuit
      replaceable Resistor R constrainedby Element;
      ...
    end Circuit;
    

    这表示R 必须是Element 的子类型。默认情况下,它是Resistor,但您可以更改它。请注意ElementResistor 或两种类型。因此,您正在通过更改 Rtype 来更改它们。你用redeclare 改变了(见书)。

    在这种情况下,“类型参数”仅适用于一个变量R。但是您可以通过以下方式以更类似于 C# 的方式执行此操作:

    model Circuit
      replaceable model X = Resistor constrainedby Element;
      X R1;
      X R2;
    }
    

    在 C# 中,这类似于:

    class Circuit<X> where X : Element {
      X R1;
      X R2;
    }
    

    (据我所知,C# 没有默认设置)

    我必须跑步,但我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多