如果条件发生变化,Modelica Simulation 就会中断

eti*_*dad 1 modelica

我使用 if 在 modelica 上尝试了一个简单的代码:

model thermostat1
  parameter Real T0=10;
  Real T(start=T0);
equation 
  if T<73 then
    der(T)=-T+80;  
  else
     der(T)=-T+50;
  end if;
end thermostat1;
Run Code Online (Sandbox Code Playgroud)

模拟在 T 达到 73 的时刻停止。

为什么模拟不能继续使用新方程( der(T)=-T+50 )?

我该如何解决?

谢谢你。

Mar*_* A. 6

What happens in your model is called chattering. This means, that there are very frequent events happening.

In you case specifically this is caused by the condition T<73 combined with the definitions of the derivatives. Let me try to explain what happens with the model you have provided:

  1. The temperature rises until it reaches 73
  2. Then the temperature starts to fall because the condition in the if-statement turns fall
  3. This causes the if-statement to immediately change to true again, making the temperature rise
  4. This causes the if-statement to change to false again, making the temperature fall
  5. goto 3.

This causes the condition T<73 to change at a very high frequency, in turn creating many events, which have to be handled by the solver. This makes the progress in time very little (what you refer to as "the simulation stops").

There are multiple ways to solve this problem. One is to add a hysteresis to the model. I did that in code below. To describe the hysteresis part I used the code from Modelica.Blocks.Logical.Hysteresis. This will make the boolean variable upperLimit (which replaces your T<73) change only if temperature gets higher than T_highand lower than T_low. I chose this solution as it seems reasonable for a thermostat.

model thermostat1
  parameter Real T0=10;

  parameter Real T_High=74;
  parameter Real T_Low=72;

  Boolean upperLimit "Output of hysteresis block";
  Real T(start=T0);

equation 
  upperLimit =not pre(upperLimit) and T > T_High or pre(upperLimit) and T >= T_Low;

  if upperLimit then
    der(T)=-T+50;
  else
    der(T)=-T+80;
  end if;

end thermostat1;
Run Code Online (Sandbox Code Playgroud)

The result of the simulation then looks like: 上面代码的结果,由 Dymola 计算

More general information can be found e.g. at http://book.xogeny.com/behavior/discrete/decay/