-
Notifications
You must be signed in to change notification settings - Fork 3
/
advection_diffusion_2d.m
executable file
·127 lines (113 loc) · 5.99 KB
/
advection_diffusion_2d.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
% ----------------------------------------------------------------------- %
% __ __ _______ _ ____ _ _ _______ _____ %
% | \/ | /\|__ __| | /\ | _ \| || | /\|__ __| __ \ %
% | \ / | / \ | | | | / \ | |_) | || |_ / \ | | | |__) | %
% | |\/| | / /\ \ | | | | / /\ \ | _ <|__ _/ /\ \ | | | ___/ %
% | | | |/ ____ \| | | |____ / ____ \| |_) | | |/ ____ \| | | | %
% |_| |_/_/ \_|_| |______/_/ \_|____/ |_/_/ \_|_| |_| %
% %
% ----------------------------------------------------------------------- %
% %
% Author: Alberto Cuoci <alberto.cuoci@polimi.it> %
% 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 Matlab4ATP framework. %
% %
% License %
% %
% Copyright(C) 2022 Alberto Cuoci %
% Matlab4ATP 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 Matlab4ATP. If not, see <http://www.gnu.org/licenses/>. %
% %
%-------------------------------------------------------------------------%
% %
% Code: 2D advection-diffusion by the FTCS scheme %
% The code is adapted and extended from Tryggvason, Computational %
% Fluid Dynamics http://www.nd.edu/~gtryggva/CFD-Course/ %
% %
% ----------------------------------------------------------------------- %
close all;
clear variables;
% User-defined data
%-------------------------------------------------------------------------%
nx=33; % number of grid points along x
ny=33; % number of grid points along y
nstep=100; % number of time steps
lengthx=2.0; % domain length along x [m]
lengthy=2.0; % domain length along y [m]
D=0.025; % diffusion coefficient [m2/s]
u=-1; % velocity along x [m/s]
v= 0; % velocity along y [m/s]
fin=1; % inlet value of f
% 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]
% 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]
% Memory allocation
f=zeros(nx,ny); % current numerical solution
fo=zeros(nx,ny); % previous numerical solution
% Dirichlet boundary conditions along the east side
f(nx, ny*1/3:(ny*2/3+1)) = fin;
% Definition of rectangular mesh (graphical purposes only)
xaxis = 0:hx:lengthx;
yaxis = 0:hy:lengthy;
% Video setup
%-------------------------------------------------------------------------%
%video_name = 'advection_diffusion_2d.mp4';
%videompg4 = VideoWriter(video_name, 'MPEG-4');
%open(videompg4);
% Advancing in time
%-------------------------------------------------------------------------%
t = 0.;
for m=1:nstep
% Plot the current solution
hold off;
mesh(xaxis, yaxis, f');
axis([0 lengthx 0 lengthy 0 1.25]);
xlabel('x'); ylabel('y'); zlabel('f');
message = sprintf('time=%d\n', t);
time = annotation('textbox',[0.15 0.8 0.15 0.15],'String',message,'EdgeColor','none');
frame = getframe(gcf);
%writeVideo(videompg4,frame);
delete(time);
% 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
% Boundary conditions (Neumann's only)
f(1:nx,1)=fo(1:nx,2); % south
f(1:nx,ny)=fo(1:nx,ny-1); % north
f(1,1:ny)=fo(2,1:ny); % west
% New time step
t=t+dt;
end
% Closing the video stream
%close(videompg4);