%ftcs_block_cavity.m
%
%forward in time centered in space
%based on heat transfer energy balance at all nodes
%
%two dimensional finite difference solution to a rectangular block with
%a rectangular crack or cavity
clear
clc
%define material properties
k = 0.401; %conduction coefficient units = W/cm*K
Cp = 385; %specific heat units = J/kg*K
rho = 0.008933; %density units = kg/cm^3
%define geometry
Lx = 50; Ly = 20;
dx = 1; %differential x length of cells
Nx = Lx/dx; %number of x nodes
dy = 1; %differential y length of cells
Ny = Ly/dy; %Number of Y nodes
tf = 60; %units of seconds
dt = 1; %seconds
Box_x = 0; %define lower left corner of box
Box_y = 0;
%Initialize mesh
T(:,:,1)=25*ones(Nx,Ny);
T(1,:,1)=100;
time(1) = 0;
time(2) = dt;
t=1;
while time(t+1)<=tf %index time step increments
for i=1:Nx; %index in x direction
for j=1:Ny; %index in y direction
if i==1; %identify left boundary
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + 2000)+T(i,j,t);
elseif j>1&j<Ny;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + 2000 + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
elseif j==Ny;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + 2000 + dx*(T(i,j-1,t)-T(i,j,t))/dy)+T(i,j,t);
end
if j==1; %lower boundary
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx)+T(i,j,t);
elseif j==Ny; %upper boundary
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
else %all other vertical intermediate nodes on left boundary
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
end
T(i,1,t+1)=T(i,2,t+1);
if j==1; %lower boundary
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx)+T(i,j,t);
elseif j==Ny; %top boundary
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
end
if j==1; %bottom
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx)+T(i,j,t);
elseif j==Ny;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
end
if j==1;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx)+T(i,j,t);
elseif j==Ny;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
end
if j==1;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx)+T(i,j,t);
elseif j>1&j<Ny;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/(dx) + dx*(T(i,j+1,t)-T(i,j,t))/dy + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/dy)+T(i,j,t);
elseif j==Ny;
T(i,j,t+1) = (dt*k/(rho*Cp*dx*dy))*(dy*(T(i-1,j,t)-T(i,j,t))/dx + dy*(T(i+1,j,t)-T(i,j,t))/dx + dx*(T(i,j-1,t)-T(i,j,t))/(dy))+T(i,j,t);
end
if j==1;
elseif i==Nx;
T(i,j,t+1)=100;
end
end
end
end
t = t+1;
time(t+1) = time(t)+dt;
end
x(1) = Box_x;
y(1) = Box_y;
for i=2:Nx;
x(i) = x(i-1)+dx;
end
for j=2:Ny;
y(j) = y(j-1)+dy;
end
figure(1); clf;
Tfinal = T(:,:,end);
mesh(y,x,Tfinal)
title(strcat('Solution at time = ',num2str(time(end))));
xlabel('y')
ylabel('x')
true_time = time(1:end-1);
clear time;
time = true_time;
figure(2); clf;
center_y = round(Ly/2);
for e = 1:length(time)
T1(e) = T(2,center_y,e);
T4(e) = T(end,center_y,e);
end
plot(time,T1,'k',time,T4,'b')
title('Temp history of specific center line nodes')
xlabel('time')
ylabel('T ^oC')