1D - Linear convection varying over number of grid points (dx)
Objective:
Numerical solution to the one-dimensional Linear Convection equation.
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
7. Set time step = 0.01
Expected output
1. For C = 1 and for time = 0.4 seconds, compare the original and final velocity profiles.
2. Make the comparison for n=20,40,80 and 160.
Formula of 1D linear convection:
`(delu)/(delt) = -C*(delu)/(delx)`
Firstly, i have created a function to access the formula of 1D linear convection
Matlab code with explaination:
function [x,u_old]=oned_linear_convection(L,n,n_start,n_end,Change_in_velocity,tm,c,dt,z)
x = linspace(0,L,n);
dx = x(2)-x(1); %grid spacing
n1 = ((n_start*L)/dx)+1 %starting node where the velocity is changed
n2 = ((n_end*L)/dx)+1 %ending node where the velocity is changed
u = ones(1,n); %assigining all the values of u to 1
u(n1:n2) = Change_in_velocity; %assigining the values to u where there is a change in velocity
u_old = u;
u_initial =u; %u_initial is just to show the initial profile
for j=1:tm %time loop
for i = 2:n %space loop
u(i) = u_old(i) - (c*(dt/dx)*(u_old(i) - u_old(i-1)));
end
u_old = u; %to update the velocity to u_old
figure(z)
plot (x,u_initial,'r','linewidth',2)
hold on
plot(x,u,'b')
axis([0 L+0.3 0 Change_in_velocity+0.3])
xlabel('Domain length')
ylabel('velocity')
end
end
now we need to enter the parameters and the function is called
close all
clear all
clc
%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
%1D linear convection = (du/dt) + C* (du/dx)
L = 1; %length of domain
c = 1; %velocity constant
t=0.4; %time
dt = 0.01; %time step
n = [20 40 80 160]; %number of nodes
n_start=0.1;
n_end=0.3;
tm=t/dt; %time marching steps
Change_in_velocity=2
for z=1:length(n)
[x1,u1]=oned_linear_convection(L,n(z),n_start,n_end,Change_in_velocity,tm,c,dt,z);
end
Output:
The above plots show the profile of velocity from intial position to final position along the domain length.
If we observe the the shape of the profile along the length of the domain, at initial it is of sqaure shape and at final it is curvy in nature i.e.. the profile is loosing its sqaure wave type shape along the length of the domain which is due to the error called artificial diffusion error
At n=20
At n=40
At n=80
At n=160
Results:
From the above plots we can observe that, the diffusion of velocity profile is decreasing as the number of nodes(grid points) is increasing i.e., the peak velocity value is increasing with increase in the number of nodes.But if we observe the plot at n=160,we can see the oscillations instead of a wave pattern. this is due to instability of solution.
Conclusion:
1. As the number of nodes are increased, the effect of diffusion will be reduced.
2. If we enter more number of nodes to get good results, the solution may blow up and results in unstability of solution.
3. In order to get better results, the number of grid points should be of optimum range i.e., neither high nor low.
Reasons behind such results:
For the simple problem of linear convection of a quantity u, the first order explicit upwind scheme becomes:
where a is the velocity magnitude, Δt is the timestep and Δx is the length between mesh elements.
By performing a Taylor series expansion on this scheme, this scheme introduces what is called a numerical diffusion, or numerical viscosity, equal to:
As expected from a diffusion coefficient, the numerical diffusion coefficient must be positive, otherwise, the solution will grow indefinitely with respect to time, making the numerical scheme unstable.
The right term inside the parenthesis of the above expression is commonly referred to as the Courant number, which is a dimensionless quantity. Therefore, the Courant number can be stated as follows:
It follows from the numerical diffusion coefficient discussion, that for any explicit simple linear convection problem, the Courant number must be equal or smaller than 1, otherwise, the numerical viscosity would be negative:
Therefore, the CFL condition expresses that the distance that any information travels during the timestep length within the mesh must be lower than the distance between mesh elements. In other words, information from a given cell or mesh element must propagate only to its immediate neighbours.
This discussion may only hold true for explicit schemes. Where as, implicit linear convection is unconditionally stable, and also lacks a CFL condition. This happens because implicit schemes use the entire domain to calculate each timestep. However, implicitly calculating (i.e., solving an implicit linear system) each timestep can be very costly. In addition, time accuracy decreases as the timestep increases. Therefore, it is logical is to use an explicit scheme with a lower timestep.
From the above case consider: - a=1; dt=1e-2; n=160 (dx = 1.587)
Courant number = 1.58 (i.e. greater than 1)
As the CFL number is greater than 1, the solution must go unstable and same can be seen from the above plot. The oscilation pattern seen the plot is due to unstablity.
For n=20,40,80 dt=0.056, 0.0256, 0.0127
CFL number is less than 1 and the solution is stable as shown in figure(1), figure(2) and figure(3)
Though the solution is stable, there is a possibility of existing of some errors.
If we observe the above plot, though the profile with n=20,40,80 are stable but they are not of same shape which is due to the artificial diffusion error.