0% found this document useful (0 votes)
64 views

Assign 6

This document contains code to solve two heat transfer problems using finite difference methods. For the first problem, the code sets up a 1D convection-diffusion equation and uses an explicit method (CDS or UDS) to solve it over multiple time steps. It plots the solution profile at the end. For the second problem, the code sets up a 2D convection-diffusion equation on a grid and uses the Gauss-Seidel method to iteratively solve it using CDS, UDS or a hybrid scheme. It plots the steady state contour of the solution.

Uploaded by

Suyog Jadhav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Assign 6

This document contains code to solve two heat transfer problems using finite difference methods. For the first problem, the code sets up a 1D convection-diffusion equation and uses an explicit method (CDS or UDS) to solve it over multiple time steps. It plots the solution profile at the end. For the second problem, the code sets up a 2D convection-diffusion equation on a grid and uses the Gauss-Seidel method to iteratively solve it using CDS, UDS or a hybrid scheme. It plots the steady state contour of the solution.

Uploaded by

Suyog Jadhav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

CFD ASSIGN 6

Question 1
L=1
rho = input('Density?');
Gamma = input('Diffusion coefficient?');
cvs = input('Number of control volumes?'); %30;
u = input('Velocity?');
opt = input('Enter 1 for CDS and 2 for UDS');
dx = L/cvs;
F = rho*u;
D = Gamma/dx;
n = cvs + 2;
M = zeros(n);
phi_old = 0.5;
dt = 0.1;
ap_old = rho*dt/dx;
%Node 2
switch opt
case 1,
aw_one = 2*D + F;
ae_one = D - F/2;
ap_one = aw_one + ae_one + ap_old;
case 2,
aw_one = 2*D + max([F,0]);
ae_one = D + max([-F,0]);
ap_one = aw_one + ae_one + ap_old;
end
%Intermediate nodes
switch opt
case 1,
aw = D + F/2;
ae = D - F/2;
ap = aw + ae + ap_old;
case 2,
aw = D + max([F,0]);
ae = D + max([-F,0]);
ap = aw + ae + ap_old;
end
%Penultimate node
switch opt
case 1,
aw_pen = D + F/2;
ae_pen = 2*D - F;
ap_pen = aw_pen + ae_pen + ap_old;
case 2,
aw_pen = D + max([F,0]);

081020119

ae_pen = 2*D + max([-F,0]);


ap_pen = aw_pen + ae_pen + ap_old;
end
%Node 1
M(1,1) = 1;
% Node 2
M(2,1) = -aw_one;
M(2,3) = -ae_one;
M(2,2) = ap_one;
% Intermediate nodes
for i= 3:n-2
M(i,i-1) = -aw;
M(i,i+1) = -ae;
M(i,i) = ap;
end
% Penultimate node
M(n-1,n-2) = -aw_pen;
M(n-1,n) = -ae_pen;
M(n-1,n-1) = ap_pen;
% Last node
M(n,n) = 1;
cnst = zeros(n,1);
phi_o = zeros(n,1);
phi_o(:) = phi_old;
res = 1;
t = 0;
while(res>0.0001)
cnst(1) = 0;
cnst(n) = 1;
for i = 2:n-1
cnst(i) = ap_old*phi_o(i);
end
% TDMA
new_M = zeros(n);
new_M(1,1) = M(1,1);
for i=2:n
new_M(i,i)=M(i,i)-(M(i-1,i)*M(i,i-1)/new_M(i-1,i-1));
end
for i=1:n-1
new_M(i,i+1)=M(i,i+1);
end

cnst_new=zeros(n,1);
cnst_new(1)=cnst(1);
for i=2:n
cnst_new(i)=cnst(i)-(cnst_new(i-1)*M(i,i-1)/new_M(i-1,i-1));
end
phi = zeros(n,1);
phi(n)=cnst_new(n)/new_M(n,n);
for i=n-1:-1:1
phi(i)=(cnst_new(i)-new_M(i,i+1)*phi(i+1))/new_M(i,i);
end
res = max(phi-phi_o);
phi_o = phi;
t = t+dt;
end
len = zeros(1,n);
len(2) = dx/2;
len(3) = 3*dx/2;
for i = 4:n-1
len(i) = dx+len(i-1);
end
len(n) = L;
phi_one = phi;
plot(len,phi,'o-',len,phi_one)
xlabel('Length')
ylabel('Phi')
legend('CDS',1)
grid on;

Question 2
rho = 1
Gamma = 1;
A = 10;
B = 2;
u = 1;
v = 4;
L = 20;
H = 20;
nv = 20;
mv = 20;
Tw = 100;
Te = 0;
Tb = 100;
Tt = 0;
opt = input('Enter 1 for CDS, 2 for UDS and 3 for Hybrid scheme:');
dx = L/nv;
dy = H/mv;
m = mv+2;
n = nv+2;

Dw = Gamma/dx*dy;
De = Gamma/dx*dy;
Dn = Gamma/dy*dx;
Ds = Gamma/dy*dx;
Fw = rho*u*dy;
Fe = rho*u*dy;
Fn = rho*v*dx;
Fs = rho*v*dx;
ap = zeros(m,n);
b = zeros(m,n);
b(:,:) = A*dx*dy;
aw = zeros(m,n);
ae = zeros(m,n);
an = zeros(m,n);
as = zeros(m,n);
ap(:,1) = 1;
ap(1,:) = 1;
ap(m,:) = 1;
ap(:,n) = 1;
b(1,:) = Tt;
b(:,1) = Tw;
b(:,n) = Te;
b(m,:) = Tb;
switch opt
case 1,
for i = 2:m-1
for j = 2:n-1
aw(i,j) = Dw + Fw/2;
ae(i,j) = De - Fe/2;
as(i,j) = Ds + Fs/2;
an(i,j) = Dn - Fn/2;
end
end
aw(2:m-1,2) = 2*Dw + Fw;
ae(2:m-1,n-1) = 2*De - Fe;
as(m-1,2:n-1) = 2*Ds + Fs;
an(2,2:n-1) = 2*Dn - Fn;
case 2 ,
for i = 2:m-1
for j = 2:n-1
aw(i,j) = Dw + max([Fw,0]);
ae(i,j) = De + max([-Fe,0]);
as(i,j) = Ds + max([Fs,0]);
an(i,j) = Dn + max([-Fn,0]);
end

end
aw(2:m-1,2) = 2*Dw + max([Fw,0]);
ae(2:m-1,n-1) = 2*De + max([-Fe,0]);
as(m-1,2:n-1) = 2*Ds + max([Fs,0]);
an(2,2:n-1) = 2*Dn + max([-Fn,0]);
case 3,
for i = 2:m-1
for j = 2:n-1
aw(i,j) = max([Fw,0,Dw+Fw/2]);
ae(i,j) = max([-Fe,0,De-Fe/2]);
as(i,j) = max([Fs,0,Ds+Fs/2]);
an(i,j) = max([-Fn,0,Dn-Fn/2]);
end
end
aw(2:m-1,2) = max([Fw,2*Dw+Fw,0]);
ae(2:m-1,n-1) = max([-Fe,2*De-Fe,0]);
as(m-1,2:n-1) = max([Fs,2*Ds+Fs,0]);
an(2,2:n-1) = max([-Fn,2*Dn-Fn,0]);
end
for i = 2:m-1
for j = 2:n-1
ap(i,j) = aw(i,j)+ae(i,j)+an(i,j)+as(i,j) + B*dx*dy ;
end
end
T = zeros(m+2,n+2);
T(m+1,2:n+1) = Tt;
T(2:m+1,2)= Tw;
T(2:m+1,n+1) = Te;
T(2,2:n+1) = Tb;
res = 1;
flag = 1;
Told = T;
while(res>0.0001)
sigma = 0;
Tnew = GS(ap,aw,ae,an,as,b,Told);
for i = 1:m
for j = 1:n
sigma = sigma + (Tnew(i,j) - Told(i,j))^2;
end
end
res(flag) = sqrt(sigma/(n*m));
flag =flag+1;
Told = Tnew;
end
Tfin = Tnew(2:m+1,2:n+1);
for i=1:m
Tfinal(i,:)=Tfin(size(Tfin,1)+1-i,:);
end

xvec = [0 0.5 1.5:19.5 20];


yvec = [0 0.5 1.5:19.5 20];
[X Y]=meshgrid(xvec,yvec);
contourf(X,Y,Tfinal)
xlabel('x')
ylabel('y')
title('Hybrid')
colorbar
print -djpeg 'Hybrid.jpg'

You might also like