Search code examples
modelica

Dealing with jumps/discontinuities in Modelica


I was wondering if anyone had any tips or tricks in terms of dealing with jumps/discontinuities in Modelica. I'm using OpenModelica, and a simplified example code for my problem is shown below.

model PowerGenerator
  Modelica.SIunits.Power P(start=0);
  output Modelica.SIunits.Energy E;
equation
if (5 < time) and (time < 15) then P = 3;
  else P = 0;
  end if;
  der(E) = P;
end PowerGenerator;

How can I make the jumps at 5 and 15 sec into continuous transitions, where the derivative of the slope is finite? I've tried the noEvent and smooth functions, but I haven't been able to make them do what I need.

Edit: The issue in my main model is that these events induce chattering, and so I also need it to work in real-time. Also in my full model, the events are state events, and so the time is not known.


Solution

  • The answer by Scott G is good, but it only works well if the change just depends on time. A more general solution is to use a low-pass filter:

    model PowerGenerator
      Modelica.SIunits.Power P(start=0);
      output Modelica.SIunits.Energy E;
      Modelica.Blocks.Continuous.LowpassButterworth lowpassButterworth(f=1)
        annotation (Placement(transformation(extent={{-40,20},{-20,40}})));
    equation 
      if (5 < time) and (time < 15) then 
         P = 3;
      else 
         P = 0;
      end if;
      lowpassButterworth.u=P;
      der(E) = lowpassButterworth.y;
    end PowerGenerator;
    

    (I would recommend using connect-statements instead - but the above should also be legal Modelica.)