Я разработал модель настраиваемой переменной, что означает, что переменная может быть определена параметром, указана через реальный ввод или оставлена неопределенной, чтобы ее можно было решить.
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, ConfigurableTempera, ConfigurableMassFlowRate, ..., содержащая единицы во входных параметрах и результатах моделирования без необходимости копировать и вставлять код модели. для каждого типа переменной и вручную назначить типы переменных в каждом классе.
Любая помощь приветствуется.
Спасибо, Джастин