Effect of Varying Time Steps
Aim: To study the effect of varying time steps on the solution of the 1D convection equation for a fixed number of grid points
`(delu)/(delt) +c(delu)/(delx)=0`
The above is the 1D linear convection equation. It is known as a one dimension because it has terms in one spacial axis and convection because of the similarity of the second term to the convective term of the Navier Strokes equation.
In the previous assignment the variation of the solution for the 1D convection equation was studied for varying number of grid points and was infered that the solution remains stable for a specific number of grid points and time step in that case it was N=80 at dt=1e-2 for time t=0.4sec.
In order to achieve a solution to this , we use the time marching method. In this method we discretize the time derivative by using the first order forward differencing and the space term by first order backward differencing.
`(delu)/(delt)` Time Derivative
`(delu)/(delx)` Space Derivative
MAIN FUNCTION
In order to find out the time elapsed and plot the same we use a inbuilt function in Octave known as tic toc.
% linear convection program
clear all
close all
clc
dt=[1e-1 1e-2 1e-3 1e-4]; % in sec
% using the tic toc function for simulation time
tic
[x1,uold1]=time_st(1e-1);
toc
t1=toc
tic
[x2,uold2]=time_st(1e-2);
toc
t2=toc
tic
[x3,uold3]=time_st(1e-3);
toc
t3=toc
tic
[x4,uold4]=time_st(1e-4);
toc
t4=toc
figure(1)
plot(x1,uold1,'k','linewidth',2)
hold on
plot(x2,uold2,'r','linewidth',2)
hold on
plot(x3,uold3,'m','linewidth',2)
hold on
plot(x4,uold4,'g','linewidth',2)
hold on
title('solution for varying timesteps for 80 grid points at time=0.4s','fontsize',20)
xlabel('length','fontsize',20)
ylabel('velocity','fontsize',20)
legend('1e-1','1e-2','1e-3','1e-4')
axis([0 1 0 2.5])
grid on
% sim time vs time steps
figure(2)
Sim_time=[t1 t2 t3 t4];
bar(Sim_time,0.5,'b')
set(gca,'xticklabel',{'1e-1','1e-2','1e-3','1e-4'},'fontsize',15);
grid on
xlabel("time steps",'fontsize',20)
ylabel("simulation time",'fontsize',20)
legend("TIME")
title(" Simulation time vs time steps",'fontsize',30);
SUB PROGRAM
This is the sub program to the above program. This program comprises of the main method used ie. the method to evaluate the solution for the 1D equation. Here I had learnt a new way to declare a function. This method is used when there is a requirement to return multiple values.
function [x,uold]=time_st(dt)
% inputs
L=1;
n=80;
c=1;
t=0.4;
%calculation
x=linspace(0,L,n);
%dx= x(2)-x(1);
dx=L/(n-1);
% defining start and stop location of the square wave
%x_start=0.1;
%x_stop=0.3;
%n_start=9;
%n_stop=25;
%initializing array and boundary conditions
u=ones(1,n);
u(1)=1;
%u(n_start:n_stop)=2;
for j=1:length(x)
if (x(j)>=0.1 && x(j)<=0.3)
u(j)=2;
else
u(j)=1;
endif
end
%u(n_start:n_stop)=2;
uold=u;
%time marching solution
nt=t/dt;
for z= 1:nt
for i=2:n
u(i)= uold(i)-c*(dt/dx)*(uold(i)-uold(i-1));
end
uold=u;
end
endfunction
PLOTS
Observation:
- From the above graph we can infer that the solution for the 1D convection equation for the time step 1e-1 is extremely unstable.
- The most stable solution for N=80 grid points was found out to be dt=1e-2. This is because the solution graph for this is quite similar to the original square wave.
- The solution for 1e-3 and 1e-4 are quite similar and have a higher rate of diffusion.
BAR GRAPH
Observation
- From this above bar plot we can infer that the time taken is directly proportional to the magnitude of the time step.
- We can observe that the time for 1e-1<1e-2<1e-3<1e-4.
- This is because of the number of timesteps in each case.
- Below is the number of steps in each case for T=0.4s.
- From the above snippet we can also infer that nt`alpha`dt`alpha`simulation time.
Elapsed time
CONCLUSION
From the above inferences, we come to a conclusion that higher the time step value(dt) the more unstable is the solution. The solution was found to be stable at dt=1e-2. Also the simulation time increases as dt decreases. But when the value of dt is decreased even further, the time taken will be longer also the rate of diffusion increases thus deviating from the desired solution.