-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactdiff.m
More file actions
155 lines (111 loc) · 2.79 KB
/
reactdiff.m
File metadata and controls
155 lines (111 loc) · 2.79 KB
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
150
151
152
% REACTDIFF script for testing stencil-based CG for Diffusion/Reaction problems.
%
%
% Fabio VERBOSIO, Universita` della Svizzera italiana, November 2018
%
%% SET PARAMS
nx = 64;
ny = 64;
assert(nx == ny, 'Grid must be square. Set nx = ny.');
N = nx * ny;
x0 = 0;
xmax = 1;
y0 = 0;
ymax = 1;
tlim = 0.01;
tsteps = 10;
maxitCG = 100;
tolCG = 1e-4;
maxitNT = 20;
tolNT = 1e-8;
T = linspace(0, tlim, tsteps);
dt = tlim/tsteps;
bndN = 0.00;
bndS = 0.00;
bndW = 0.00;
bndE = 0.00;
bnd.bndN = bndN;
bnd.bndS = bndS;
bnd.bndE = bndE;
bnd.bndW = bndW;
dx = (xmax-x0)/(nx);
dy = (ymax-y0)/(ny);
%D = 1; % Diffusion constant
%R = 1;
F = 0.5; % Initial condition
alpha = dx^2/dt;
dxs = 100 / dx^2;
global A;
A = -gallery('poisson', nx);
%% SET INITIAL CONDITIONS
X = zeros(nx,ny);
% Center and radius of the circle
c = [xmax-1/4, y0+1/4];
r = min(c) / 2;
for j = 1:nx
x = x0 + j*dx;
for k = 1:ny
p = [x, y0+k*dy];
%disp(p);
if norm(c-p) <= r
X(j,k) = F;
end
end
end
%% SET BOUNDARY CONDITIONS
X(1,:) = bndN;
X(end,:) = bndS;
X(:,1) = bndW;
X(:,end) = bndE;
X = X(:);
%% RUN TIME-S
figure('Name',sprintf('%d x %d grid', nx, ny));
global f;
tTot = tic;
ts = 1;
for t = T
fprintf('T-step %4d/%4d:\n', ts, tsteps);
Xold = X;
imagesc(reshape(X, nx,nx)); colorbar; pause(0.5);
convNT = false;
for k = 1:maxitNT
%f = @(X) drstencil(X, Xold, nx, ny, alpha, dxs);
f = @(X) diffusereact(X, Xold, alpha, dxs);
X = f(Xold);
fB = norm(X);
fprintf('NWT: it=%3d, ||f(B)|| = %.4e | ', k, fB);
if (fB < tolNT)
fprintf('Newton method has converged!\n');
convNT = true;
break;
end
[dX, flg, relres, its] = stencil_pcg(f, X, tolCG, maxitCG);
fprintf('CG: ');
switch flg
case 1
fprintf('CG iterated %d times but did not converge.\n', maxitCG);
case 2
fprintf('CG preconditioner M was ill-conditioned.\n');
case 3
fprintf('CG stagnated.\n');
case 4
fprintf('CG: one of the scalar quantities is too small or too large.\n');
end
if flg > 0
break;
end
fprintf('residual %.4e after %3d iterations.\n', relres, its);
X = Xold - dX;
Xold = X;
%spy(reshape(dX, nx,nx))
end
imagesc(reshape(X, nx,nx)); colorbar;
if convNT
ts = ts+1;
continue;
else
error('Newton method FAILED to converge.');
end
end
fprintf('-----------------------------------\n');
fprintf('Simulation took %f seconds.\n', toc(tTot));