Modelica 中的抽象开关 [英] Abstract switch in Modelica
问题描述
我想提出一个我之前问过的关于 Modelica 部分模型数组的问题一>.考虑以下 2 个控制器之间的切换模型.
I would like to motivate a question I asked before about a Modelica array of partial model. Consider the following model of a switch between 2 controllers.
model Switch
input Real u;
input Integer sel;
output Real y;
protected
Real x;
equation
if sel == 1 then
y = 0.1 * (0 - u);
der(x) = 0;
else
y = 0.1 * (0 - u) + 0.2 * x;
der(x) = 0 - u;
end if;
end Switch;
我们忽略PI控制器由于x
的发散而在一段时间未被选中时可能会损坏的事实.这可以通过在选择 PI 控制器时重置 x
来解决.然而,这不是重点.
Let's ignore the fact that the PI controller may break when it is not selected for some time due to divergence of x
. This can be fixed by resetting x
when the PI controller is selected. However, this is not the point here.
我想以两种方式抽象这个开关.首先,在参数数量的控制器之间切换.其次,使用部分模型抽象控制器.让 Ctrl
成为控制器的部分模型.
I want to abstract this switch in 2 ways. Firstly, to switch among a parametric number of controllers. Secondly, to abstract controllers using partial models. Let Ctrl
be the partial model of a controller.
partial model Ctrl
input Real u;
output Real y;
end Ctrl;
我们可以如下实例化嵌入在交换机中的两个控制器.
We can instantiate the two controllers embedded in the switch as follows.
model P extends Ctrl;
equation
y = 0.1 * (0 - u);
end P;
model PI extends Ctrl;
protected
Real x;
equation
y = 0.1 * (0 - u) + 0.2 * x;
der(x) = 0 - u;
end PI;
开关的抽象版本应该是这样的:
The abstract version of the switch is supposed to be something like:
model Switch
parameter Integer N(min=1);
Ctrl c[N];
input Real u;
input Integer sel(min=1, max=N);
output Real y;
equation
for i in 1:N loop
c[i].u = u;
end for;
y = c[sel].y;
end Switch;
但是,这个模型有一些问题.首先,尚不清楚如何实例化此模型,例如带有一个 P
和一个 PI
控制器.其次,我收到一个令我惊讶的警告,即:以下输入缺少绑定方程:c[1].u
However, this model has some problems. Firstly, it is not clear how this model can be instantiated, e.g. with one P
and one PI
controller. Secondly, I get a warning which surprises me, namely: The following input lacks a binding equation: c[1].u
是否可以在 Modelica 中以某种方式表达这个抽象开关?
Is it possible to do express this abstract switch in Modelica in some way?
推荐答案
这不适用于模型数组,因为您无法通过修改将其绑定到不同的模型.您需要指定您在 GenericSwitch 中拥有的所有控制器.如果需要,您可以自动生成 GenericSwitch 和 Switch 模型.
This doesn't work with an array of models as you cannot bind it to different models via a modification. You need to specify all the controllers you have inside the GenericSwitch. You could generate the GenericSwitch and Switch model automatically if needed.
partial model Ctrl
input Real u;
output Real y;
end Ctrl;
model P
extends Ctrl;
equation
y = 0.1 * (0 - u);
end P;
model PI
extends Ctrl;
protected
Real x;
equation
y = 0.1 * (0 - u) + 0.2 * x;
der(x) = 0 - u;
end PI;
model GenericSwitch
replaceable model MyCtrl1 = Ctrl;
replaceable model MyCtrl2 = Ctrl;
MyCtrl1 c1(u = u);
MyCtrl2 c2(u = u);
input Real u;
input Integer sel;
output Real y;
equation
y = if sel == 1 then c1.y else c2.y;
end GenericSwitch;
model Switch = GenericSwitch(
redeclare model MyCtrl1 = P,
redeclare model MyCtrl2 = PI);
这篇关于Modelica 中的抽象开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!