Effect of Time Step Size for the 1D Linear Convection Problem
INTRODUCTION:
A multiphysics problem exhibits certain characteristics that are helpful for simulations. The two dimensional Navier Stokes Momentum Equation is given by the formula:
.
The idea is for us to isolate the flow physics and implement numerical schemes in the flow problem.
MATHEMATICAL FORMULATION:
- The way we can accomplish this is by taking one term and see how it affects our results. We are limiting it to One Dimensional Convection Flow Problem which is given by the partial differential equations shown below:
u is the initial velocity which varies partially with time(t) as well as space(x). c is the constant which determines the convection rate
- For the partial differential equation to vary with time and space we make use of the numerical scheme and the initial velocity u is presented as,
where,
n represents the time step node
i represents the spatial nodes
- We are trying to develop a numerical scheme where we apply backward differencing for time derivative and forward differencing for space derivative.
This is the equation we implement for our 1D convection problem.
OBJECTIVE:
Write a function that accepts the time step as an argument and solves the same problem. You will compare the effect of the time step on the numerical solution.
1. Set n = 80
2. Time steps to use = 1e-4, 1e-3, 1e-2, and 1e-1
PROBLEM ASSUMPTIONS:
1. Assume that the domain length is L = 1m
2. The initial velocity profile is a step function. It is equal to 2m/s between x= 0.1 and 0.3 and 1m/s everywhere else
3. Use first-order forward differencing for the time derivative
4. Use first-order backward/rearward differencing for the space term.
5. Time step and number of grid points should be variables in your calculation
6. Constant Velocity, C = 1 m/s
7. The size of the time step varies as: [1e-4 1e-3 1e-2 1e-1]
MATLAB:-
FUNCTION PROGRAM:
function linear_convection_time_step(dt)
L=1; %Length of the domain
c=1; %Velocity of the domain
n=81; %Number of grid points
t=0.4; %Simulation time
nt=t/dt; %No of time steps
x=linspace(0,L,n); %Spacing the grid points
dx=L/(n-1); %Distance between two grid points
%Defining start and end positions of the square wave
n_start=find(x==0.1);
n_stop=find(x==0.3);
%Initialize arrays and apply BC's
u=ones(1,n);
u(n_start:n_stop)=2;
% Copying the values for reference
uold=u;
uinitial=u;
%Time Loop
for k=1:nt
%Space Loop
for i=2:n-1
u(i)=uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
end
%Update old velocities
uold=u;
end
%Plot for velocities with differenct time step size
figure(1)
subplot(2,1,1)
plot(x,u,'linewidth',3)
hold on
grid on
%plot(x,uinitial,'linewidth',3)
axis([0 1 0 3]);
pause(0.03);
%Calculating the value of CFL number to predict the stability
CFL=(c*dt/dx);
fprintf('The CFL number for %g grid points: %g. ',n,CFL)
if CFL<1
disp('The solution is Stable.')
elseif CFL==1
disp('The solution is Most Stable.')
else
disp('The solution is Unstable.')
end
MAIN PROGRAM:
clear all
close all
clc
n=81; %grid points
dt=[1e-4 1e-3 1e-2 1e-1]; %Time step size
%tic toc function to help us calculate the time taken to perform an
%operation
tic
linear_convection_time_step(dt(1))
t1=toc;
hold on
tic
linear_convection_time_step(dt(2))
t2=toc;
hold on
tic
linear_convection_time_step(dt(3))
t3=toc;
hold on
tic
linear_convection_time_step(dt(4))
t4=toc;
legend('dt = 1e-4','dt = 1e-3','dt = 1e-2','dt = 1e-1')
xlabel('Spatial Distance [x] (m)')
ylabel('Velocity [U] (m/s)')
title('Effect of time step size for the 1D Linear Convection Problem')
hold on
sim_time=[t1,t2,t3,t4];
time_step_size=categorical({'1e-4','1e-3','1e-2','1e-1'});
%Bar Plot for calculation the simulation time
subplot(2,1,2)
bar(time_step_size,sim_time,0.5)
xlabel('Time Step Size')
ylabel('Simulation Time (s)')
title('Time Taken for Simulation (dt)')
OUTPUT:
The CFL number for 81 grid points: 0.008. The solution is Stable.
The CFL number for 81 grid points: 0.08. The solution is Stable.
The CFL number for 81 grid points: 0.8. The solution is Stable.
The CFL number for 81 grid points: 8. The solution is Unstable.
RESULTS AND DISCUSSIONS:
- For a wide range of time step sizes, we can predict the stability of our solutions making use of CFL numbers. The most stable solution for our problem is having the value of CFL=0.8 at a time step size of 0.01. Here the value of CFL is 0.8<1. That is almost close to one and gives support for the most stable solution.
- For other time step sizes, artificial diffusion occurs due to which errors are introduced to the system similar to first-order terms. Terms with second-order errors and above are neglected for our problem.
- The gradients will die as the time step moves forward which causes the diffusion to happen in our final velocity profiles.
- For time step sizes 1e-4 and 1e-3, the final velocity profiles are similar but when the time step increases further to 0.1 we can see wiggles and oscillations causing unstable solutions. This happens because of the value of CFL>1.
- For the second bar graph, we can see that when the time step size is increased the computational time decreases. So more time is required for running the simulation and providing the results.
CONCLUSION:
Thus we understood the importance of stability in our results and how time step sizes affect the results in our simulations.