问题
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;
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.
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;
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
Is it possible to do express this abstract switch in Modelica in some way?
回答1:
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);
回答2:
I guess it should work with something like:
model GenericSwitch
parameter Integer N(min=1);
replaceable model MyCtlr = Ctrl constrainedby Ctrl;
MyCtlr c[N](each u = u);
input Real u;
input Integer sel(min=1, max=N);
output Real y;
equation
y = c[sel].y;
end GenericSwitch;
model PSwitch = GenericSwitch(redeclare model MyCtrl = P);
model PISwitch = GenericSwitch(redeclare model MyCtrl = PI);
来源:https://stackoverflow.com/questions/29715165/abstract-switch-in-modelica