0

I am trying to write a While loop to track time. The code uses a default time step value. However, when the simulation time hits a desired time value, I want the code to recompute the time step to hit the desired value, but revert the time step to original value for subsequent calculations (unless it hits another desired value).

For example, I have the desired time values as [150, 300, 600, 1000, 3000, 6000, 10000]. Initial time is zero and the default time step (dt) is 10.5353.

The problem(s):

  1. I request someone to verify if my method is correct, and
  2. The code ends at tsim = 9992.8609, dt = 10.5353, and does not compute the end time (t = 10000).

Please help. Thanks in advance.

So far, I have written the following code:

rhom = 1300; % Density [kg/m3]
cm = 925; % Specific heat [J/kg/K]
km = 1.3; % Thermal conductivity [W/m/K]
N = 45; % Number of nodes []
L = 0.21; % Channel length [m]
dx = L/(N-1); % Incremental length [m]
i = 1; % While-loop counter []
j = 1; % Time to plot counter []

% Time calculation
dt(1) = a*rhom*cm*dx^2/2/km; % Time step [s]
tsim(1) = 0; % Set initial time to zero [s]

while tsim(i) <= tplot(end)
    if tplot(j) - tsim(i) < rhom*cm*dx^2/2/km && j < 7
        dt = tplot(j) - tsim(i);
        j = j + 1;
    else
        dt = rhom*cm*dx^2/2/km;
    end
    tsim(i+1) = tsim(i) + dt;
    disp(['tsim = ', num2str(tsim(i)), ', dt = ', num2str(dt)]);
    i = i + 1;
end

Result:

tsim = 0, dt = 10.5353 <---- Initial time is zero and the time step is 10.5353 (default)
tsim = 10.5353, dt = 10.5353
tsim = 21.0705, dt = 10.5353
tsim = 31.6058, dt = 10.5353
tsim = 42.141, dt = 10.5353
tsim = 52.6763, dt = 10.5353
tsim = 63.2115, dt = 10.5353
tsim = 73.7468, dt = 10.5353
tsim = 84.282, dt = 10.5353
tsim = 94.8173, dt = 10.5353
tsim = 105.3525, dt = 10.5353
tsim = 115.8878, dt = 10.5353
tsim = 126.423, dt = 10.5353
tsim = 136.9583, dt = 10.5353
tsim = 147.4935, dt = 2.5065 <---- The code recomputes the time step for next step
tsim = 150, dt = 10.5353 <---- The simulation time hits 150 s and the time step resets to default
tsim = 160.5353, dt = 10.5353
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120

0 Answers0