-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver_p3.m
312 lines (243 loc) · 8.2 KB
/
driver_p3.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
% driver for brusselator PDE test problem:
% u' = d1*u_xx + a - (b+1)*u + u^2*v,
% v' = d2*v_xx + b*u - u^2*v,
% with parameters a=0.6, b=2, d1=d2=0.25, over the spatial
% interval [0,1] and the time interval [0,80]. Initial
% conditions are u(x,0) = a + 0.1*sin(pi*x),
% v(x,0) = b/a + 0.1*sin(pi*x), and boundary conditions are
% homogeneous Dirichlet. We use a mesh with 100 spatial zones.
%
% Daniel R. Reynolds
% Department of Mathematics
% Southern Methodist University
% March 2017
% All Rights Reserved
clear
% set problem parameters
global a b d1 d2 m dx
a = 0.6;
b = 2;
d1 = 0.025;
d2 = 0.025;
m = 100;
dx = 1/(m-1);
fn = @f_p3;
fe = @fe_p3;
fi = @fi_p3;
Jn = @J_p3;
Ji = @Ji_p3;
Es = @EStab_p3;
Tf = 10;
tout = linspace(0,Tf,100);
hmin = 1e-6;
hmax = 1.0;
rtol = 1e-4;
atol = 1e-14*ones(2*m,1);
% initial conditions
xspan = linspace(0,1,m)';
u0 = a + 0.1*sin(pi*xspan);
v0 = b/a + 0.1*sin(pi*xspan);
Y0 = [u0; v0];
% plot "true" solution at end of run
opts = odeset('RelTol',1e-12, 'AbsTol',atol,'InitialStep',hmin/10, 'MaxStep',hmax);
[t,Ytrue] = ode15s(fn, tout, Y0, opts);
figure()
plot(xspan,Ytrue(end,1:m),xspan,Ytrue(end,m+1:2*m))
xlabel('t','FontSize',12), ylabel('y','FontSize',12)
title('1D Brusselator PDE test','FontSize',14)
set(gca,'FontSize',12)
print('-dpng','brusselator1D')
% run with an embedded diagonally-implicit RK method
mname = 'Kvaerno(7,4,5)-ESDIRK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with DIRK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,nl,~] = solve_DIRK(fn, Jn, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with a non-embedded diagonally-implicit RK method
mname = 'Cooper6-ESDIRK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with DIRK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,nl,~] = solve_DIRK(fn, Jn, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with a fully-implicit RK method
mname = 'RadauIIA-3-5-IRK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with IRK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,nl,~] = solve_IRK(fn, Jn, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with an embedded ARK method
mname1 = 'ARK5(4)8L[2]SA-ERK';
Be = butcher(mname1); s = numel(Be(1,:))-1;
mname2 = 'ARK5(4)8L[2]SA-ESDIRK';
Bi = butcher(mname2); s = numel(Bi(1,:))-1;
fprintf('\nRunning with ARK integrator: %s/%s (order = %i)\n',...
mname1,mname2,Be(s+1,1))
[t,Y,ns,nl] = solve_ARK(fe, fi, Ji, tout, Y0, Be, Bi, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% $$$ % run with an embedded explicit RK method
% $$$ mname = 'Merson-4-3-ERK';
% $$$ B = butcher(mname); s = numel(B(1,:))-1;
% $$$ fprintf('\nRunning with ERK integrator: %s (order = %i)\n',mname,B(s+1,1))
% $$$ [t,Y,ns,~] = solve_ERK(fn, Es, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
% $$$ err_max = max(max(abs(Y'-Ytrue)));
% $$$ err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
% $$$ fprintf('Accuracy/Work Results:\n')
% $$$ fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
% $$$ fprintf(' steps = %i (stages = %i)\n',ns,ns*s);
% $$$
% $$$
% $$$ % run with a non-embedded explicit RK method
% $$$ mname = 'ERK-4-4';
% $$$ B = butcher(mname); s = numel(B(1,:))-1;
% $$$ fprintf('\nRunning with ERK integrator: %s (order = %i)\n',mname,B(s+1,1))
% $$$ [t,Y,ns,~] = solve_ERK(fn, Es, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
% $$$ err_max = max(max(abs(Y'-Ytrue)));
% $$$ err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
% $$$ fprintf('Accuracy/Work Results:\n')
% $$$ fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
% $$$ fprintf(' steps = %i (stages = %i)\n',ns,ns*s);
% end of script
%------------------------- Utility routines -------------------------%
function dy = f_p3(t, y)
% extract solution components
global a b d1 d2 m dx
u = y(1:m);
v = y(m+1:2*m);
% initialize RHS terms
du = zeros(m,1);
dv = zeros(m,1);
% enforce stationary boundary conditions
du(1) = 0; du(m) = 0; dv(1) = 0; dv(m) = 0;
% diffusion components
du(2:m-1) = d1/dx/dx*(u(3:m)+u(1:m-2)-2*u(2:m-1));
dv(2:m-1) = d2/dx/dx*(v(3:m)+v(1:m-2)-2*v(2:m-1));
% reaction components
du(2:m-1) = du(2:m-1) + a - (b+1)*u(2:m-1) + u(2:m-1).*u(2:m-1).*v(2:m-1);
dv(2:m-1) = dv(2:m-1) + b*u(2:m-1) - u(2:m-1).*u(2:m-1).*v(2:m-1);
% combine together into output
dy = [du; dv];
% end function
end
function dy = fe_p3(t, y)
% extract solution components
global a b d1 d2 m dx
u = y(1:m);
v = y(m+1:2*m);
% initialize RHS terms
du = zeros(m,1);
dv = zeros(m,1);
% enforce stationary boundary conditions
du(1) = 0; du(m) = 0; dv(1) = 0; dv(m) = 0;
% reaction components
du(2:m-1) = a - (b+1)*u(2:m-1) + u(2:m-1).*u(2:m-1).*v(2:m-1);
dv(2:m-1) = b*u(2:m-1) - u(2:m-1).*u(2:m-1).*v(2:m-1);
% combine together into output
dy = [du; dv];
% end function
end
function dy = fi_p3(t, y)
% extract solution components
global a b d1 d2 m dx
u = y(1:m);
v = y(m+1:2*m);
% initialize RHS terms
du = zeros(m,1);
dv = zeros(m,1);
% enforce stationary boundary conditions
du(1) = 0; du(m) = 0; dv(1) = 0; dv(m) = 0;
% diffusion components
du(2:m-1) = d1/dx/dx*(u(3:m)+u(1:m-2)-2*u(2:m-1));
dv(2:m-1) = d2/dx/dx*(v(3:m)+v(1:m-2)-2*v(2:m-1));
% combine together into output
dy = [du; dv];
% end function
end
function J = J_p3(t, y)
% extract solution components
global a b d1 d2 m dx
u = y(1:m);
v = y(m+1:2*m);
% initialize Jacobian blocks terms
Juu = spalloc(m,m,3*m);
Juv = spalloc(m,m,m);
Jvu = spalloc(m,m,m);
Jvv = spalloc(m,m,3*m);
% diffusion components
for j=2:m-1
Juu(j,j-1) = Juu(j,j-1) + d1/dx/dx;
Juu(j,j) = Juu(j,j) - 2*d1/dx/dx;
Juu(j,j+1) = Juu(j,j+1) + d1/dx/dx;
end
for j=2:m-1
Jvv(j,j-1) = Jvv(j,j-1) + d2/dx/dx;
Jvv(j,j) = Jvv(j,j) - 2*d2/dx/dx;
Jvv(j,j+1) = Jvv(j,j+1) + d2/dx/dx;
end
% reaction components
for j=2:m-1
Juu(j,j) = Juu(j,j) - (b+1) + 2*u(j)*v(j);
end
for j=2:m-1
Juv(j,j) = Juv(j,j) + u(j)^2;
end
for j=2:m-1
Jvv(j,j) = Jvv(j,j) - u(j)^2;
end
for j=2:m-1
Jvu(j,j) = Jvu(j,j) + b - 2*u(j)*v(j);
end
% combine together into output
J = [Juu, Juv; Jvu, Jvv];
% end function
end
function J = Ji_p3(t, y)
% extract solution components
global a b d1 d2 m dx
u = y(1:m);
v = y(m+1:2*m);
% initialize Jacobian blocks terms
Juu = spalloc(m,m,3*m);
Juv = spalloc(m,m,m);
Jvu = spalloc(m,m,m);
Jvv = spalloc(m,m,3*m);
% diffusion components
for j=2:m-1
Juu(j,j-1) = Juu(j,j-1) + d1/dx/dx;
Juu(j,j) = Juu(j,j) - 2*d1/dx/dx;
Juu(j,j+1) = Juu(j,j+1) + d1/dx/dx;
end
for j=2:m-1
Jvv(j,j-1) = Jvv(j,j-1) + d2/dx/dx;
Jvv(j,j) = Jvv(j,j) - 2*d2/dx/dx;
Jvv(j,j+1) = Jvv(j,j+1) + d2/dx/dx;
end
% combine together into output
J = [Juu, Juv; Jvu, Jvv];
% end function
end
function dt = EStab_p3(t, y)
% compute the Jacobian ODE RHS
J = J_p3(t,y);
% determine the largest eigenvalue magnitude
lam = max(abs(eigs(J)));
% assume explicit stability region includes Euler stability region
% (this assumes that the eigenvalue is in fact negative).
dt = 1/lam;
% end function
end