如何在Modelica中实现嵌套的if语句?

时间:2017-11-27 17:55:05

标签: modelica openmodelica jmodelica

我正在尝试实施HVAC系统的故障包容模型。故障在用户定义的时间开始,在本例中为faultTime = 1000。但是,if语句的第一部分根本没有实现。以下是与问题

相关的代码片段
fcuModel FCU;
Modelica.Blocks.Continuous.LimPID PI(k = 300, Ti = 1, yMax = 1, yMin = 1e-4);
parameter Real faultTime = 1000;
// fault modes: 0-normal, 1-fan failed, 2-valve stuck shut...
parameter Integer faultMode = 1;
equation
  connect(PI.u_m,FCU.Ts_zon); // connects zone temperature to PID measurement
  PI.u_s = 21; // set-point for zone temperature
  if time<faultTime then
    PI.y = FCU.val;
    PI.y = FCU.fs;
  else
    if faultMode == 0 then
      PI.y = FCU.val;
      PI.y = FCU.fs;
    elseif faultMode == 1 then
      PI.y = FCU.val;
      FCU.fs = 1e-4;
    end if;
  end if;  

当我模拟时,它运行没有错误,但它直接转到faultMode == 1下的等式,而没有模拟前1000秒的无故障状态。

1 个答案:

答案 0 :(得分:4)

我通过引入一些变量和更改一些参数来修改模型以使其直接工作。结果是:

model FCU

  Modelica.Blocks.Continuous.LimPID PI(k = 0.1, Ti = 1, yMax = 1, yMin = 1e-4);

  parameter Real faultTime = 1000;
  parameter Integer faultMode = 1;

  Real val;
  Real fs;

equation 
  PI.u_s = 21; // set-point for zone temperature
  PI.u_m = 20.9999; // no feedback as no system available  

  if time<faultTime then
    PI.y = val;
    PI.y = fs;
  else
    if faultMode == 0 then
      PI.y = val;
      PI.y = fs;
    elseif faultMode == 1 then
      PI.y = val;
      fs = 1e-4;
    else
     assert(false,"Unknown faultMode");
    end if;
  end if;
  annotation (experiment(StopTime=2000), uses(Modelica(version="3.2.2")));
end FCU;

下面的结果(在Dymola中模拟)似乎是我所期望的。 Result

希望这会有所帮助......