Nothing Special   »   [go: up one dir, main page]

Fazal - CFD - Assignment - Example 7.2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Department of Chemical Engineering.

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

Fig.1: Example 7.2 Data.

Fig.2: Solution provided for comparison..


2
Gauss Seidel and TDMA (Line by Line).

MATLAB CODE FOR GAUSS SEIDEL SCHEME:


%Example 7.2 (2-D Steady State Diffusion) Gauss-Seidel.
clear all;
clc;
%Data:
L=0.4;
W=0.3;
dz=0.01;
k=1000;
TN=100;
qW=500*1000;
qS=0; qE=0;

%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);

%Coefficients (East, West, North, South) and source terms):


aE=k*Ae/dx;
aW=k*Aw/dx;
aN=k*An/dy;
aS=k*As/dy;

suW=qW*Aw;
suN=TN*k*An/(dy/2);
sp=-k*An/(dy/2);

%For interior points (Nodes 6 and 7) No source:

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

%For node 1 (q(W) source from west and inulated south):

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;

%For node 5 (south insulated):

A(5,5)=aE+aW+aN;
A(5,9)=-aE;
A(5,1)=-aW;
A(5,6)=-aN;

%For node 8 (T(N) source from north):

A(8,8)=aE+aW+aS-sp;
A(8,12)=-aE;
A(8,4)=-aW;
A(8,7)=-aS;
B(8)=suN;

%For node 9 (east and south insulated):

A(9,9)=aW+aN;
A(9,5)=-aW;
A(9,10)=-aN;

%For node 10 and 11 (east insulated):

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

%For node 12 (T(N) source from north and east insulated):

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:

Fig.3: Gauss-Seidel solution (Convergence in 140 iterations).


5
Gauss Seidel and TDMA (Line by Line).

MATLAB CODE FOR TDMA (LINE BY LINE) SCHEME:


%Example 7.2 (2-D Steady State Diffusion) TDMA Line-by-Line.
clear all;
clc;
%Data:
L=0.4;
W=0.3;
dz=0.01;
k=1000;
TN=100;
qW=500*1000;
qS=0; qE=0;

%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);

%Coefficients (East, West, North, South) and source terms):


aE=k*Ae/dx;
aW=k*Aw/dx;
aN=k*An/dy;
aS=k*As/dy;

suW=qW*Aw;
suN=TN*k*An/(dy/2);
sp=-k*An/(dy/2);

%For interior points (Nodes 6 and 7) No source:

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

%For node 1 (q(W) source from west and inulated south):

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;

%For node 5 (south insulated):

A(5,5)=aE+aW+aN;
A(5,9)=-aE;
A(5,1)=-aW;
A(5,6)=-aN;

%For node 8 (T(N) source from north):

A(8,8)=aE+aW+aS-sp;
A(8,12)=-aE;
A(8,4)=-aW;
A(8,7)=-aS;
B(8)=suN;

%For node 9 (east and south insulated):

A(9,9)=aW+aN;
A(9,5)=-aW;
A(9,10)=-aN;

%For node 10 and 11 (east insulated):

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

%For node 12 (T(N) source from north and east insulated):

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);

%TDMA Line by line:

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 point 1 only:


C(1)=B(1)+aE*T(5)
Aj(1) = alpha(1)/D(1);
C_prime0=1; Aj0=1; %Any arbitrary values as these. do not exist
C_prime(1) = (C(1)+beta(1)*C_prime0)/(D(i)-beta(i)*Aj0) ;

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 line 2 (Nodes 5,6,7,8) East temperature guess, west temperature


%calculated (1,2,3,4):

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

fprintf('Solution converged in %d iterations by TDMA 2D method',k);

T_order= flipud(T)

X=(dx/2):dx:W;
Y=(dy/2):dy:L;

surf(X,Y,T_order);

Results:

Fig.4: TDMA-2D Solution (Convergence in 45 iterations).

You might also like