1D LINEAR CONVECTION AND THE EFFECT OF TIME STEP SIZE ON SOLUTION
AIM:
To observe the effect of time step size on the solution of One Dimensional linear convection equation.
The governing equation:
The above equation is being discretized by using first order forward differencing approach for the time derivative and first order backward differencing approach for the space derivative.
where n is the current time step.
L=1 m (domain length)
n=80 (number of grid points)
C=1
There are four different time steps used to compare the effectives of the time step with the marching of the solution.
The time steps taken are:1e-4,1e-3,1e-2 and 1e-1.
u(t=0)=2 m/s for x=0.1 to 0.3
u(t=0)=1 m/s elsewhere
To observe different time steps of same solution and take into consideration the simulation time, a function has been created. tic and toc command is being used to start and stop the time stopwatch and the time taken to execute each function with respect to that time step has been recorded.
The function snippet:
%function for linear velocity
function u=linear_velocity(c,dt,dx,nt,n,u,uold)
%time loop
for k=1:nt
%space loop
for i=2:n
u(i)=uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
end
uold=u; %updating old velocities
end
end
This function solves the 1D linear equation in explicit form.
total time taken is 0.4 seconds.
Main progam:
clear all
close all
clc
%inputs for linear convection
L=1; %length of the domain
c=1;
n=81; %number of grid points
%calculations
x=linspace(0,L,n);
dx=x(2)-x(1); %width of space
%start and end point of the square wave
n_start=9;
n_end=25;
%Initialize and apply boundary conditions
u=ones(1,n);
u(n_start:n_end)=2;
%Boundary Conditions
u(1)=1;
uold=u;
u_initial=u;
dt=[1e-4 1e-3 1e-2 1e-1];
for j=1:4
nt=(0.4/dt(j)); %no of time steps
tic
unew=linear_velocity(c,dt(j),dx,nt,n,u,uold);
toc
T(j)=toc;
%plotting the velocities and comparing the effects of different time steps
plot(x,unew,'linewidth',2)
hold on
xlabel('domain length')
ylabel('velocity (u)')
legend('1e-4','1e-3','1e-2','1e-1')
axis([0 1 -2.5 2.5])
title({'1D Linear Convection at C=1 & n=80';'time =0.4 sec'})
end
%plotting the execution time as a function of time step
T_exe=[T(1) T(2) T(3) T(4)];
xaxis=categorical({'1e-4','1e-3','1e-2','1e-1'});
figure(2)
bar(xaxis,T_exe)
xlabel('time step')
ylabel('execution time')
title('comparison of execution time for different time step')
Plots:
comparison of different time steps:
Explanation of plots:
The solution for the time step 1e-1 blows up might be because it had a larger time step and hence gives an incorrect graph.
The time step 1e-1 might be greater than the time for the wave to travel to adjacent grid point and hence the solution possibly blew up.
For the time step 1e-2; the solution is better than all and for the time steps 1e-3 and 1e-4, they arenearly same with greater diffusion than 1e-2 time step.
Comparison of simulation time:
The time step 1e-4 being the lowest time step has got the highest execution time.
The 1e-2 and 1e-3 give better simulation time and result as per the CFL principle.
The blowing up of solution in the case of 1e-1 time step is justified by the CFL number.The stability of the solution can be analyzed by the on neumann stability analysis.