首页 文章

Modelica中的抽象开关

提问于
浏览
3

我想激发一个我之前问过的关于Modelica array of partial model的问题 . 考虑以下两个控制器之间的切换模型 .

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;

让我们忽略这样一个事实,即由于 x 的分歧,PI控制器在一段时间内未被选中时可能会中断 . 选择PI控制器时,可以通过重置 x 来解决此问题 . 但是,这不是重点 .

我想以两种方式抽象这个开关 . 首先,在参数数量的控制器之间切换 . 其次,使用部分模型抽象控制器 . 设 Ctrl 是控制器的部分模型 .

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 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

是否有可能以某种方式在Modelica中表达这个抽象开关?

2 回答

  • 3

    这不适用于模型数组,因为您无法通过修改将其绑定到不同的模型 . 您需要指定GenericSwitch中的所有控制器 . 如果需要,您可以自动生成GenericSwitch和Switch模型 .

    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);
    
  • 1

    我想它应该适用于:

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

相关问题