saad saleem
saad saleem

Reputation: 5

altering Newton`s cooling example in Dymola to show sinusoidal behavior

I am trying to alter the Newton cooling problem (link: https://mbe.modelica.university/behavior/equations/physical/#physical-types) so that :

1) T_inf is 300K for the first 5 seconds

2) At T=5, I switch it to sinusoidal wave with T_inf having an average value of 400 K, peak to peak amplitude of 50 K and period of 10 seconds

3) At T=85s, I want to change the period of the sine wave to 0.01 seconds, keeping everything else the same. Simulation has to end in 100s

I am successful in parts 1 and 2, but part 3 isn't running for me. My code is below.

model MAE5833_Saleem_NewtonCooling_HW2_default

  // Types
  type Temperature = Real (unit="K", min=0);
  type ConvectionCoefficient = Real (unit="W/(m2.K)", min=0);
  type Area = Real (unit="m2", min=0);
  type Mass = Real (unit="kg", min=0);
  type SpecificHeat = Real (unit="J/(K.kg)", min=0);

  // Parameters
  parameter Temperature T0=400 "Initial temperature";
  parameter ConvectionCoefficient h=0.7 "Convective cooling coefficient";
  parameter Area A=1.0 "Surface area";
  parameter Mass m=0.1 "Mass of thermal capacitance";
  parameter SpecificHeat c_p=1.2 "Specific heat";

  parameter Real freqHz=0.1 "Frequency of sine wave in from 5 to 85 seconds";
  parameter Real freq2=100 "Time period of 0.01s after 85 seconds";
  parameter Real amplitude=25 "Peak to peak of 50K";
  parameter Real starttime=5;
  parameter Real T_init=300;
  parameter Real T_new=400;

  Temperature T "Temperature";
  Temperature T_inf;

initial equation 
  T = T0 "Specify initial value for T";

equation 
  m*c_p*der(T) = h*A*(T_inf - T) "Newton's law of cooling";

algorithm 
  when {time > starttime,time < 85} then
    T_inf := (T_new - T_init) + amplitude*Modelica.Math.sin(2*3.14*freqHz*(time - starttime));
  elsewhen time > 85 then
    T_inf := (T_new - T_init) + amplitude*Modelica.Math.sin(2*3.14*freq2*(time - starttime));
  elsewhen time < starttime then
    T_inf := T_init;
  end when;

  annotation (experiment(
      StopTime=100,
      Interval=0.001,
      __Dymola_Algorithm="Rkfix2"));
end MAE5833_Saleem_NewtonCooling_HW2_default;

Upvotes: 0

Views: 85

Answers (1)

marco
marco

Reputation: 6655

You have to use an if statement in this case instead of when.

Here is the updated equation section, with some further suggestions below:

equation 
  m*c_p*der(T) = h*A*(T_inf - T) "Newton's law of cooling";

  if time >= starttime and time < 85 then
    T_inf = (T_new - T_init) + amplitude*sin(2*Modelica.Constants.pi*freqHz*(time - starttime));
  elseif time >= 85 then
    T_inf = (T_new - T_init) + amplitude*sin(2*Modelica.Constants.pi*freq2*(time - starttime));
  else
    T_inf = T_init;
  end if;
  • you can use sin instead of Modelica.Math.sin, as the function is built in
  • use Modelica.Constants.pi instead of defining pi yourself
  • I have merged your algorithm into the equation section. Don't use an algorithm section unless there is a very good reason to do so.

Upvotes: 3

Related Questions