%This program complished the process of 2D Eulerian advection with method
%of allocating memory temporarily.
clear all;clf;
% Set parameters
Nt = 11;
output_interval = 1;
Lx = 1000000;
Ly = 1500000;
x_midd = Lx/2;
y_midd = Ly/2;
Nx = 51;
Ny = 31;
kw = 10;%W/(m*K)
denw = 3300;%Kg/m^3
Cpw = 1100;%J/(Kg*K)
ks = 3;%W/(m*K)
dens = 3200;%Kg/m^3
Cps = 1000;%J/(Kg*K)
bt = 1000;%K,background temperature
wt = 1300;%K,wave temperature
Nx1 = Nx+1;
Ny1 = Ny+1;
dx = Lx / (Nx-1);
dy = Ly / (Ny-1);
% Basic E-nodes.
x = 0:dx:Lx;
y = 0:dy:Ly;
[X,Y] = meshgrid(0:dx:Lx,0:dy:Ly);
% Initialize k,den*Cp and temperature matrix at the temperature points.
kht = zeros(Ny,Nx);
hct = zeros(Ny,Nx);
td = zeros(Ny,Nx);
T = zeros(Ny,Nx);
vx = zeros(Ny,Nx);
vy = zeros(Ny,Nx);
for j=1:1:Nx
for i=1:1:Ny
T(i,j) = bt;
kht(i,j) = ks;
hct(i,j) = dens*Cps;%unit:(Pa*s)
if ( x(j)<=x_midd+100000 && x(j)>=x_midd-100000 && y(i)<=y_midd+150000 && y(i)>=y_midd-150000)
kht(i,j) = kw;
hct(i,j) = denw*Cpw;%unit:(Pa*s)
T(i,j) = wt;
end
td(i,j) = kht(i,j)/hct(i,j);
vx(i,j) = 1e-9;%m/s
vy(i,j) = 1e-9;%m/s
end
end
aaa=hct;
% 2.7548e-06(suround),9.3750e-07(wave)
dtexp = min(dx,dy)^2/(3*td(2,2));
if(vx(1,1)~=0)
dtexp=min(dtexp,abs(dx/vx(1,1))); % Limitation for horizontal advection timestep
end
if(vy(1,1)~=0)
dtexp=min(dtexp,abs(dy/vy(1,1))); % Limitation for vertical advection timestep
end
dt = 0.7*dtexp;
%grids has N2 rows and N1 columns
[x,y] = meshgrid(0:Lx/(Nx-1):Lx,0:Ly/(Ny-1):Ly);
b = zeros(Nx*Ny, 1);
coe = zeros(Nx*Ny, Nx*Ny);
b1 = zeros(Nx*Ny, 1);
coe1 = zeros(Nx*Ny, Nx*Ny);
for t = 1:Nt
khtdt = kht;
hctdt = hct;
% Change k and den*Cp at internal nodes with upwind difference
for j = 2:Nx-1
for i = 2:Ny-1
khtdt(i,j) = dt*( -vx(i,j)*( kht(i,j)-kht(i,j-1) )/dx -vy(i,j)*( kht(i,j)-kht(i-1,j) )/dy ) +kht(i,j) ;
hctdt(i,j) = dt*( -vx(i,j)*( hct(i,j)-hct(i,j-1) )/dx -vy(i,j)*( hct(i,j)-hct(i-1,j) )/dy ) +hct(i,j) ;
end
end
kht = khtdt;
hct = hctdt;
% Constant temperature boundary condition.
% Tbc = constant = 1000
% up and down
for j = 1:Nx
i = 1;
k = (j-1)*Ny + i;
coe(k, k) = 1;
b(k, 1) = 1000;
i = Ny;
k = (j-1)*Ny + i;
coe(k, k) = 1;
b(k, 1) = 1000;
end
% left and right
for i = 1:Ny
j = 1;
k = (j-1)*Ny + i;
coe(k, k) = 1;
b(k, 1) = 1000;
j = Nx;
k = (j-1)*Ny + i;
coe(k, k) = 1;
b(k, 1) = 1000;
end
% coefficients of interval points
for j = 2:Nx-1
for i = 2:Ny-1
k = (j-1)*Ny + i; % k is the index
if vx(i,j)>0
dtdx = (T(i,j)-T(i,j-1))/dx;
elseif vx(i,j)<0
dtdx = (T(i,j+1)-T(i,j))/dx;
end
if vy(i,j)>0
dtdy = (T(i,j)-T(i-1,j))/dy;
elseif vy(i,j)<0
dtdy = (T(i+1,j)-T(i,j))/dy;
end
coe(k, k) = (khtdt(i,j)+khtdt(i,j+1))/(2.*dx.^2)+(khtdt(i,j-1)+khtdt(i,j))/(2.*dx.^2)...
+(khtdt(i,j)+khtdt(i+1,j))/(2.*dy.^2)+(khtdt(i-1,j)+khtdt(i,j))/(2.*dy.^2)...
+hctdt(i,j)/dt; % middle T3
coe(k, k-1) = -(khtdt(i-1,j)+khtdt(i,j))/(2.*dy.^2); % up T2
coe(k, k+1) = -(khtdt(i,j)+khtdt(i+1,j))/(2.*dy.^2); % down T4
coe(k, k-Ny) = -(khtdt(i,j-1)+khtdt(i,j))/(2.*dx.^2); % left T1
coe(k, k+Ny) = -(khtdt(i,j)+khtdt(i,j+1))/(2.*dx.^2); % right T5
b(k, 1) = T(i,j)*hctdt(i,j)/dt - hctdt(i,j)*(vx(i,j)*dtdx+vy(i,j)*dtdy);
end
end
%direct method:right martix is divided by coe martix on the left
u = coe \ b;
%transform vector to grids
%grids has N2 rows and N1 columns
U = reshape(u, Ny, Nx);
T = U;
figure(1);
pcolor(x,y,T);colorbar;
xlabel('Horizontal(Km)');
ylabel('Vertical(Km)');
zlabel('Gravitational potential');
title(['(CTbc) Inplicit form solution of T(advection) after',num2str(t-1),' deltat'])
set(gca,'xaxislocation','top');
set (gca,'YDir','reverse')
shading interp;
pause(1);
end