【发布时间】: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、...,包含参数输入和模拟结果中的单位,而无需复制和粘贴每个变量类型的模型代码并手动分配每个类中的变量类型。
非常感谢任何帮助。
谢谢, 贾斯汀
【问题讨论】: