-
Notifications
You must be signed in to change notification settings - Fork 20
/
homework3_advdiff.m
149 lines (131 loc) · 6 KB
/
homework3_advdiff.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
% ----------------------------------------------------------------------- %
% __ __ __ _ __ __ %
% |\/| _ |_ | _ |_ |__| / |_ | \ _ (_ |__) |_ %
% | | (_| |_ | (_| |_) | \__ | |__/ (_) | | \ | %
% %
% ----------------------------------------------------------------------- %
% %
% Author: Alberto Cuoci <[email protected]> %
% CRECK Modeling Group <http://creckmodeling.chem.polimi.it> %
% Department of Chemistry, Materials and Chemical Engineering %
% Politecnico di Milano %
% P.zza Leonardo da Vinci 32, 20133 Milano %
% %
% ----------------------------------------------------------------------- %
% %
% This file is part of Matlab4CFDofRF framework. %
% %
% License %
% %
% Copyright(C) 2020 Alberto Cuoci %
% Matlab4CFDofRF is free software: you can redistribute it and/or %
% modify it under the terms of the GNU General Public License as %
% published by the Free Software Foundation, either version 3 of the %
% License, or (at your option) any later version. %
% %
% Matlab4CFDofRF is distributed in the hope that it will be useful, %
% but WITHOUT ANY WARRANTY; without even the implied warranty of %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %
% GNU General Public License for more details. %
% %
% You should have received a copy of the GNU General Public License %
% along with Matlab4CRE. If not, see <http://www.gnu.org/licenses/>. %
% %
%-------------------------------------------------------------------------%
% Exact solutions from K. Masatsuka, "I do like CFD, Vol. 1", 2018 %
% ----------------------------------------------------------------------- %
close all;
clear variables;
% User-defined data
%-------------------------------------------------------------------------%
nx=40; % number of grid points along x
ny=40; % number of grid points along y
nstep=150; % number of time steps
lengthx=1.0; % domain length along x [m]
lengthy=1.0; % domain length along y [m]
D=0.050; % diffusion coefficient [m2/s]
u=1; % velocity along x [m/s]
v=0.5; % velocity along y [m/s]
A = 1.25; % arbitrary constant
B = 0.75; % arbitrary constant
C = 1; % arbitrary constant
% Pre-processing of user-defined data
%-------------------------------------------------------------------------%
% Calculate grid steps
hx=lengthx/(nx-1); % grid step along x [m]
hy=lengthy/(ny-1); % grid step along y [m]
x=0:hx:lengthx; % x coordinates [m]
y=0:hy:lengthy; % y coordinates [m]
% Numerical setup: time step (stability conditions)
sigma = 0.75; % safety coefficient
dt_diff = 1/4*min(hx^2, hy^2)/D; % diffusion [s]
dt_conv = 4*D/(u^2+v^2); % convection [s]
dt = sigma*min(dt_diff, dt_conv); % time step [s]
fprintf('Co=%f Di=%f Pe=%f \n', ...
max(u,v)*dt/min(hx,hy), D*dt/min(hx^2,hy^2), ...
max(hx,hy)*max(u,v)/D);
% Memory allocation
f=zeros(nx,ny); % current numerical solution
f0=zeros(nx,ny); % initial numerical solution
fan=zeros(nx,ny); % analytical solution
% Initial condition
for i=1:nx
for j=1:ny
csi = u*x(i)+v*y(j);
eta = v*x(i)-u*y(j);
f0(i,j) = C*cos(B*pi*eta)*exp((1-sqrt(1+4*(A*pi*D)^2))/(2*D)*csi);
end
end
% Advancing in time
%-------------------------------------------------------------------------%
t = 0.;
f = f0;
for m=1:nstep
% Constant
gamma = D*pi*pi*(u^2+v^2)*(B^2-A^2);
% Analytical solution
for i=1:nx
for j=1:ny
fan(i,j) = f0(i,j) * exp(-gamma*t);
end
end
% Error (mean) between numerical and analytical solution
error = sum(sum(abs(f-fan)))/(nx*ny);
% Plot the current solution
plot(x,f(:,ny/2), x,fan(:,ny/2));
title('Solution along the horizontal axis at y=Ly/2');
legend('numerical', 'analytical');
xlabel('x-axis [m]'); ylabel('f value');
pause(0.0001)
% Boundary conditions (Dirichlet, constant in time)
f(:,1) = f0(:,1)*exp(-gamma*t);
f(:,ny) = f0(:,ny)*exp(-gamma*t);
f(1,:) = f0(1,:)*exp(-gamma*t);
f(nx,:) = f0(nx,:)*exp(-gamma*t);
% Forward Euler method
fo=f;
for i=2:nx-1
for j=2:ny-1
f(i,j) = fo(i,j)...
-(0.5*dt*u/hx)*(fo(i+1,j)-fo(i-1,j))...
-(0.5*dt*v/hy)*(fo(i,j+1)-fo(i,j-1))...
+(D*dt/hx^2)*(fo(i+1,j)-2*fo(i,j)+fo(i-1,j))...
+(D*dt/hy^2)*(fo(i,j+1)-2*fo(i,j)+fo(i,j-1));
end
end
% New time step
t=t+dt;
end
% Solution
figure;
pcolor(x, y, f');
colorbar; shading interp; colormap(jet);
title('numerical solution');
xlabel('x-axis [m]'); ylabel('y-axis [m]');
% Difference
figure;
abserr = abs(f-fan);
pcolor(x, y, abserr');
colorbar; shading interp; colormap(jet);
title('absolute error');
xlabel('x-axis [m]'); ylabel('y-axis [m]');