Fazal - CFD - Assignment - Example 7.2
Fazal - CFD - Assignment - Example 7.2
Fazal - CFD - Assignment - Example 7.2
Assignment:
“MATLAB Code for Example 7.2:
2-D Steady State Diffusion using Gauss
Seidel and TDMA (Line by Line).
Subject: Computational Fluid Dynamics.
Submitted by:
Fazal e Rabbi (MS- Process Engineering).
10-24-2022
1
Gauss Seidel and TDMA (Line by Line).
INTRODUCTION:
The Gauss-Seidel method and TDMA-2D method was applied to solve the Two-Dimensional
Heat Diffusion equation with sources and insulation. Firstly, the differential equation was
discretized using Finite Volume Method and algebraic equations for 12 nodes were obtained,
according to the method for 2-D diffusion discussed in class by using (East-West, North-South
scheme). In order to solve the following matrix equation:
𝐴12𝑥12 𝑋12𝑥1 = 𝐵12𝑋1
%Discretization:
Nx=3;
Ny=4;
dx=W/Nx;
dy=L/Ny;
Ae=dy*dz; Aw=Ae;
An=dx*dz; As=An;
Nt=Nx*Ny;
A=zeros(Nt,Nt);
B=zeros(Nt,1);
suW=qW*Aw;
suN=TN*k*An/(dy/2);
sp=-k*An/(dy/2);
for i=6:1:7;
A(i,i)=aE+aW+aN+aS;
A(i,i+4)=-aE;
A(i,i-4)=-aW;
A(i,i+1)=-aN;
A(i,i-1)=-aS;
end
A(1,1)=aE+aN;
A(1,5)=-aE;
A(1,2)=-aN;
B(1)=suW;
3
Gauss Seidel and TDMA (Line by Line).
%For nodes 2 and 3 (q(W) source from west):
for i=2:1:3
A(i,i)=aE+aN+aS;
A(i,i+4)=-aE;
A(i,i+1)=-aN;
A(i,i-1)=-aS;
B(i)=suW;
end
%For node 4 (q(W) source from west and T(N) from north):
A(4,4)=aE+aS-sp;
A(4,8)=-aE;
A(4,3)=-aS;
B(4)=suW+suN;
A(5,5)=aE+aW+aN;
A(5,9)=-aE;
A(5,1)=-aW;
A(5,6)=-aN;
A(8,8)=aE+aW+aS-sp;
A(8,12)=-aE;
A(8,4)=-aW;
A(8,7)=-aS;
B(8)=suN;
A(9,9)=aW+aN;
A(9,5)=-aW;
A(9,10)=-aN;
for i=10:1:11
A(i,i)=aW+aN+aS;
A(i,i-4)=-aW;
A(i,i+1)=-aN;
A(i,i-1)=-aS;
end
A(12,12)=aW+aS-sp;
A(12,8)=-aW;
A(12,11)=-aS;
B(12)=suN;
4
Gauss Seidel and TDMA (Line by Line).
A
B
U=Gauss_Seidel(A,B);
T=zeros(Ny,Nx);
T(1,1)=U(4);
T(1,2)=U(8);
T(1,3)=U(12);
T(2,1)=U(3);
T(2,2)=U(7);
T(2,3)=U(11);
T(3,1)=U(2);
T(3,2)=U(6);
T(3,3)=U(10);
T(4,1)=U(1);
T(4,2)=U(5);
T(4,3)=U(9);
X=(dx/2):dx:W;
Y=(dy/2):dy:L;
surf(X,Y,T);
Results:
%Discretization:
Nx=3;
Ny=4;
dx=W/Nx;
dy=L/Ny;
Ae=dy*dz; Aw=Ae;
An=dx*dz; As=An;
Nt=Nx*Ny;
A=zeros(Nt,Nt);
B=zeros(Nt,1);
suW=qW*Aw;
suN=TN*k*An/(dy/2);
sp=-k*An/(dy/2);
for i=6:1:7;
A(i,i)=aE+aW+aN+aS;
A(i,i+4)=-aE;
A(i,i-4)=-aW;
A(i,i+1)=-aN;
A(i,i-1)=-aS;
end
A(1,1)=aE+aN;
A(1,5)=-aE;
A(1,2)=-aN;
B(1)=suW;
6
Gauss Seidel and TDMA (Line by Line).
%For nodes 2 and 3 (q(W) source from west):
for i=2:1:3
A(i,i)=aE+aN+aS;
A(i,i+4)=-aE;
A(i,i+1)=-aN;
A(i,i-1)=-aS;
B(i)=suW;
end
%For node 4 (q(W) source from west and T(N) from north):
A(4,4)=aE+aS-sp;
A(4,8)=-aE;
A(4,3)=-aS;
B(4)=suW+suN;
A(5,5)=aE+aW+aN;
A(5,9)=-aE;
A(5,1)=-aW;
A(5,6)=-aN;
A(8,8)=aE+aW+aS-sp;
A(8,12)=-aE;
A(8,4)=-aW;
A(8,7)=-aS;
B(8)=suN;
A(9,9)=aW+aN;
A(9,5)=-aW;
A(9,10)=-aN;
for i=10:1:11
A(i,i)=aW+aN+aS;
A(i,i-4)=-aW;
A(i,i+1)=-aN;
A(i,i-1)=-aS;
end
A(12,12)=aW+aS-sp;
A(12,8)=-aW;
A(12,11)=-aS;
B(12)=suN;
7
Gauss Seidel and TDMA (Line by Line).
A
B
%U=Gauss_Seidel(A,B);
alpha = zeros(Nt,1);
beta = zeros(Nt,1);
Aj = zeros(Nt, 1) ;
C=zeros(Nt,1);
C_prime = zeros(Nt, 1) ;
% getting the elements
% d - diagonal elements $ b - below diagonal $ a - above diagonal
D = diag(A);
beta(2:Nt) = -diag(A,-1);
alpha(1:Nt-1) = -diag(A,+1);
T=zeros(Ny,Nx);
k=0;
tol=0.01;
eval=ones(Ny,Nx);
while eval>tol
T_old=T;
%For line 1 (Nodes 1,2,3,4) aw=0, and East temperatures initial guess.
for i=2:1:4
C(i)=B(i)+aE*T(i+4);
Aj(i)=alpha(i)/(D(i)-beta(i)*Aj(i-1));
C_prime(i) = (C(i)+beta(i)*C_prime(i-1))/(D(i)-beta(i)*Aj(i-1)) ;
end
for i=4:-1:1
T(i,1)=Aj(i)*T(i+1)+C_prime(i);
end
for i=5:1:8
C(i)=B(i)+aW*T(i-4)+aE*T(i+4);
Aj(i)=alpha(i)/(D(i)-beta(i)*Aj(i-1));
C_prime(i) = (C(i)+beta(i)*C_prime(i-1))/(D(i)-beta(i)*Aj(i-1)) ;
end
for i=8:-1:5
T(i-4,2)=Aj(i)*T(i+1)+C_prime(i);
end
8
Gauss Seidel and TDMA (Line by Line).
%For line 3 (Nodes 9,10,11,12)aE=0, West temperatures calculated (5,6,7,8)
for i=9:1:12
C(i)=B(i)+aW*T(i-4);
Aj(i)=alpha(i)/(D(i)-beta(i)*Aj(i-1));
C_prime(i) = (C(i)+beta(i)*C_prime(i-1))/(D(i)-beta(i)*Aj(i-1)) ;
end
T13=1; %Does not exist, any arbitrary value.
T(4,3)=Aj(12)*T13+C_prime(12);
for i=11:-1:9
T(i-8,3)=Aj(i)*T(i+1)+C_prime(i);
end
eval=norm(T-T_old);
k=k+1;
end
T_order= flipud(T)
X=(dx/2):dx:W;
Y=(dy/2):dy:L;
surf(X,Y,T_order);
Results: