You are on page 1of 3

Capital University of Science and Technology

Department of Mechanical Engineering

ME4552: Computational Fluid Dynamics, Fall 2019


ASSIGNEMNT-4
Instructor: Dr. Muhammad Irfan Date: 03/12/19
Total Marks: 10 Due Date: 03/01/20

Instructions:

 Assignment submission should be on A4 size sheet.


 Your report should be concise and readable.
 Assignment should be submitted on or before the due date.
 Copied assignments will get zero credit.
 I strongly recommend that you use MATLAB, although other programming languages are also
allowed.

Q1: Consider the one-dimensional linear advection equation


u u
c  0,
t x
Where u is the function of x and t, and c is a constant. Assume a periodic boundary
condition, i.e., if there are m grid points on spatial axis, umn 1  u1n , umn 2  u2n , etc.
Solve this equation numerically using Lax-Friedrichs scheme
t n
u in 1  u in1  u in1   c u i 1  u in1   Ot , x 
1
2 2 x
for a square wave

0 if x  2 L 5

u x,0  1 if 2 L 5  x  3L 5
0 if x  3L 5

Where L is the length of the computational domain.

a) Is it possible to achieve numerical solution using this method?


b) Show how solution evolves with time.
c) Show the effects of different time steps t and comment on the results obtained.
d) Hand in listing of your program (m-files) and present your results in the form
of report.
Hint: You can start your solution using the following MATLAB script where Forward Time
Backward Space (FTBS) scheme is implemented.
clc;clear all;close all;
% lambda = 0.5; % lambda = c*dt/dx
c = 1.0;
iplot = 1;
myinit = 1; % Initial condition, 1 = step func, 2 = sine wave
my_scheme = 1;
nx = [100];
dt = 0.0015;

L = 1; % Length of domain
for kk = 1:length(nx)
dx(kk) = L/nx(kk); % Cell/grid size
lambda = (c*dt)/dx;
time = 1.2;
nt = time/dt;
nxp1 = nx(kk)+1; % Ghost cell
nxp2 = nx(kk)+2; % Ghost cell
a1 = (2*L/5)/dx(kk); % Intervals definition (Unit step function)
b1 = fix((L/3)/dx(kk)); % Intervals definition (Sine wave)
% ------------------------------------------------------------------------%
% Initial condition %
% ------------------------------------------------------------------------%
if myinit == 1
u_sol = [zeros(1,a1+1) ones(1,a1/2) zeros(1,a1)];
end
% ------------------------------------------------------------------------%
x = 0:dx(kk):L; % Grid points on x-axis
x_Ex = x;
u_Ex = u_sol; % Save the exact soltion
xmin = 0; % Define x & y - limits
xmax = L;
if myinit == 1
ymin = -0.5*max(u_sol);
elseif myinit == 2
ymin = -1.5*max(u_sol);
end
ymax = 1.5*max(u_sol);
% ------------------------------------------------------------------------%
% Start time loop and code the numerical discretizations %
% ------------------------------------------------------------------------%
for it = 1:nt
x_Ex = mod(x_Ex+lambda*dx(kk),L-dx(kk)); % Move x-axis for exact sol.

uim1 = u_sol(1);
uim2 = u_sol(2);
% ------------------------------------------------------------------------%
if my_scheme == 1
% Forward Time Backward Space (FTBS) -- Upwind.
% ------------------------------------------------------------------------%
for ix = 2:nx(kk)
tmp1 = u_sol(ix);
u_sol(ix) = u_sol(ix) - lambda*(u_sol(ix) - uim1);
uim1 = tmp1;
end
% ------------------------------------------------------------------------%
elseif my_scheme == 2
% Forward Time Central Space (FTCS)
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
elseif my_scheme == 3
% Lax-Friedrichs Scheme
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
elseif my_scheme == 4
% Lax-Wendroff Scheme
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
elseif my_scheme == 5
% Beam-Warming Scheme
% ------------------------------------------------------------------------%
%
%
% ------------------------------------------------------------------------%
%
%
end

% ------------------------------------------------------------------------%
% Plot results %
% ------------------------------------------------------------------------%
figure(kk);
if mod(it-1,iplot) == 0
plot(x(1:length(x)-1),u_sol(1:nx(kk)),'b-',...
x_Ex(1:length(x_Ex)-1),u_Ex(1:nx(kk)),'r-',...
'LineWidth',3,'MarkerSize',6)
if my_scheme == 1
legend('FTBS','Exact','location','northwest')
elseif my_scheme == 2
legend('FTCS','Exact','location','northwest')
elseif my_scheme == 3
legend('Lax-Friedrichs','Exact','location','northwest')
elseif my_scheme == 4
legend('Lax-Wendroff','Exact','location','northwest')
elseif my_scheme == 5
legend('Beam-Warming','Exact','location','northwest')
end
axis([xmin xmax ymin ymax]);
set(gca,'FontSize',24,'linewidth',2)
xlabel('x','FontSize',24)
ylabel('u','FontSize',24)
drawnow;
% pause(1);
% ginput(1);
end
end
i_tmp=round(x_Ex(1)*(nx(kk)/L));
% i_tmp=10;
u_tmp=u_Ex;
u_Ex(1:i_tmp)=u_tmp(nx(kk)-(i_tmp-1):nx(kk));
u_Ex(i_tmp+1:nx(kk))=u_tmp(1:(nx(kk)-i_tmp));
error(kk) = (L/nx(kk))*sqrt(sum((u_sol-u_Ex).^2));
end
x1=[.02 .2];
y1=[.02 .2].^2;

You might also like