我开发了一个可配置变量的模型,这意味着该变量可以由参数定义,通过实际输入指定,或者保留未定义以便可以求解。
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;
现在我想以利用数量/单位类型而不仅仅是实数的方式扩展模型。是否可以通过参数指定变量类型?例如,是否可以声明如下所示的变量?
parameter String variableType = "Pressure";
parameter typeOf(variableType) variableType;
如果是这样,我可以在 ConfigurableReal 模型中定义变量类型来引用variableType 对象并通过以下方式编写可配置的压力模型:
model ConfigurablePressure extends ConfigurableReal(final variableType="Pressure");
如果没有,Modelica 中是否有类似 C# 类型参数的东西?
最后,我真的想找到一种方法来扩展我的 ConfigurableReal 模型,以便它可以是 ConfigurablePressure、ConfigurableTemperature、ConfigurableMassFlowRate...,包含参数输入和模拟结果中的单位,而无需复制和粘贴每个变量类型的模型代码并在每个类中手动分配变量类型。
非常感谢任何帮助。
谢谢, 贾斯汀
最佳答案
Justin,你真正想要的是使用redeclare
+ replaceable
。您提到了 C# 类型参数。 Modelica 确实有类似的功能,但它们看起来(语法上)不太一样,而且由于 Modelica 的数学限制,它们还有一些额外的限制。
如果您还没有阅读 the chapter on Architecture在我的书中,这将为您提供一个开始。
为了完成与 C# 类型参数的类比,请考虑以下模型:
model Circuit
replaceable Resistor R constrainedby Element;
...
end Circuit;
这表明 R
必须是 Element
的子类型。默认情况下,它是一个电阻,但您可以更改它。请注意,Element
和 Resistor
或两种类型。因此,您可以通过更改它们来更改 R
的类型。您可以使用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# 没有默认值)
我必须运行,但我希望这会有所帮助。
关于modelica - 是否可以通过 Modelica 中的参数设置变量类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232470/