Abstract switch in Modelica

I would like to motivate the question I asked earlier about the Modelica partial model array . Consider the following model for switching between two 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 can break when not selected for some time due to a mismatch x

. This can be eliminated by resetting x

when the PI controller is selected. However, this is not the point here.

I want to reverse this switch in two ways. First, to switch between a parametric number of controllers. Second, for abstract controllers using partial models. Let be Ctrl

a partial controller model.

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;

      

An abstract version of the switch should be something like this:

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. First, it is unclear how this model can be created, for example. with one controller P

and one PI

. Secondly, I get a warning that surprises me, namely: The following input is missing an anchor equation:c[1].u

Is there any way to express this abstract switch in Modelica?

+3


source to share


2 answers


This does not work with an array of models as you cannot bind it to other models via modification. You need to list all controllers you have in GenericSwitch. You can automatically generate GenericSwitch and Switch model 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);

      

+3


source


I think 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);

      

+1


source







All Articles