-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdc_step.m
329 lines (245 loc) · 9.72 KB
/
sdc_step.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function sol = sdc_step(ode,ti,yi,opts,param)
% we'll just hardcode for explicit euler presently.
% get "unscaled" quadrature nodes
switch opts.grid
case 1
% uniform nodes
t = linspace(0,1,opts.nquad);
case 2
% Gaussian (Gauss Legendre) nodes
t = gaussnodes(0,1,opts.nquad);
case 3
% Chebychev
t = chebyshevnodes(0,1,opts.nquad);
case 4
% Gauss-Lobatto nodes
t = gauss_lobatto(0,1,opts.nquad);
otherwise
error('unknown option in opts.grid')
end % switch opts.grid
m = length(yi);
% bug in h?
%h = (t(2:end)-t(1:end-1))*param.dt;
h = (t - [0, t(1:end-1)])*param.dt;
if opts.pred ~= 1
error('hard coded for explicit euler');
end
if opts.corr ~= 1
error('hard coded for explicit euler');
end
if opts.grid ~= 2
error(['hard coded for nodes that do not include left ' ...
'endpoint']);
end
% predictor
level = 1;
y{level} = zeros(m,opts.nquad);
y{level}(:,1) = yi + h(1)*ode(ti,yi);
for n = 2:opts.nquad
y{level}(:,n) = y{level}(:,n-1) + h(n)*ode(ti+h(n),y{level}(:,n-1));
end
% explicit corrector
for level = 2:opts.levels
y{level} = zeros(m,opts.nquad);
for n = 1:opts.nquad
f{level-1}(:,n) = ode(ti+t(n)*param.dt,y{level-1}(:,n));
end
y{level}(:,1) = yi;
S = generate_integration_matrix(t,t(1));
for k = 1:opts.nquad
y{level}(:,1) = y{level}(:,1) + param.dt*S(1,k)*f{level-1}(:,k);
end
for n = 2:opts.nquad
S = generate_integration_matrix(t,t(n)) - generate_integration_matrix(t,t(n-1));
y{level}(:,n) = y{level}(:,n-1) +...
h(n)*ode(ti+t(n-1)*param.dt,y{level}(:,n-1)) - ...
h(n)*f{level-1}(:,n-1);
for k = 1:opts.nquad
y{level}(:,n) = y{level}(:,n) + param.dt*S(1,k)*f{level-1}(:,k);
end
end
end
% for n = 2:opts.nquad
% K = zeros(m,param.pred.s);
% for s = 1:param.pred.s
% temp = y{1}(:,n-1) + K(:,1:s-1)*param.pred.A(s,1:s-1)';
% K(:,s) = h(n-1)*ode(tk+param.pred.c(s)*h(n-1),temp);
% end
% y{1}(:,n) = y{1}(:,n-1) + K*param.pred.b';
% tk = tk + h(n-1);
% end
% case{1}
% % diagonally implicit
% % structure for values at quadrature nodes
% K = zeros(m,param.pred.s);
% tk = ti;
% switch opts.grid
% case {1,4}
% % uniform node/gauss lobattos (includes left endpoint)
% y{1}(:,1) = yi;
% otherwise
% h0 = param.dt*(t(1)-0);
% for s = 1:param.pred.s
% temp = yi + K(:,1:s-1)*param.pred.A(s,1:s-1)';
% func_hand = @(Ki) Ki - h0*ode(tk+param.pred.c(s)*h0,...
% temp+param.pred.A(s,s)*Ki);
% k_init = param.dt*ode(tk+param.pred.c(s)*h0,...
% yi+param.pred.c(s)*param.dt*ode(tk,yi));
% [K(:,s),~,flag] = ...
% fsolve(func_hand, k_init, optimset('Display','off',...
% 'TolFun',1e-9));
% if flag > 1
% error('did not converge to root')
% end
% end
% y{1}(:,1) = yi + K*param.pred.b';
% tk = tk+h0;
% end
% for n = 2:opts.nquad
% K = zeros(m,param.pred.s);
% for s = 1:param.pred.s
% temp = y{level}(:,n-1) + K(:,1:s-1)*param.pred.A(s,1:s-1)';
% func_hand = @(Ki) Ki - h(n-1)*ode(tk+param.pred.c(s)*h(n-1),...
% temp+param.pred.A(s,s)*Ki);
% k_init = param.dt*ode(tk+param.pred.c(s)*h(n-1),...
% y{level}(:,n-1)+...
% param.pred.c(s)*param.dt*ode(tk,y{level}(:,n-1)));
% [K(:,s),~,flag] = ...
% fsolve(func_hand, k_init, optimset('Display','off',...
% 'TolFun',1e-9));
% if flag > 1
% error('did not converge to root')
% end
% end
% y{1}(:,n) = y{1}(:,n-1) + K*param.pred.b';
% tk = tk + h(n-1);
% end
% otherwise
% error('fully implicit integrators not coded yet')
% end
% if opts.levels > 1
% % there are correction loops!
% % in practice, can compute S{} and L{} once and reuse for all
% % large intervals
% % note, we scale by dt later -- this is the key difference from
% % former code
% S = generate_integration_matrix(t,t);
% % n = 1
% Sstage{1} = generate_integration_matrix(t,param.corr.c*t(1));
% Lstage{1} = generate_interpolation_matrix(t,param.corr.c*t(1));
% for n = 2:opts.nquad
% Sstage{n} = generate_integration_matrix(t,t(n-1)+param.corr.c*(t(n)-t(n-1)));
% Lstage{n} = generate_interpolation_matrix(t,t(n-1)+param.corr.c*(t(n)-t(n-1)));
% end
% end
% for level = 2:opts.levels
% y{level} = zeros(m,opts.nquad);
% % correction loops -- can generalize to different tableaus later
% % compute f(t,y) at quadrature points
% f = zeros(m,opts.nquad);
% for n = 1:opts.nquad
% f(:,n) = ode(ti+param.dt*t(n),y{level-1}(:,n));
% end
% switch param.corr.type
% case{0}
% % explicit
% K = zeros(m,param.corr.s);
% tk = ti;
% switch opts.grid
% case {1,4}
% % uniform nodes/gauss lobatto (includes left endpoint)
% y{level}(:,1) = yi;
% otherwise
% % again, handle first time step differently
% for s = 1:param.corr.s
% temp = yi + K(:,1:s-1)*param.corr.A(s,1:s-1)' + ...
% param.dt * f * Sstage{1}(s,:)';
% K(:,s) = h0*ode(tk+param.corr.c(s)*h0,temp) - ...
% h0*f*Lstage{1}(s,:)';
% end
% y{level}(:,1) = yi + K*param.corr.b' + param.dt*f*S(1,:)';
% tk = tk+h0;
% end
% for n = 2:opts.nquad
% K = zeros(m,param.corr.s);
% for s = 1:param.corr.s
% temp = y{level}(:,n-1) + K(:,1:s-1)*param.corr.A(s,1:s-1)' + ...
% param.dt * f * (Sstage{n}(s,:)' - S(n-1,:)');
% K(:,s) = h(n-1)*ode(tk+param.corr.c(s)*h(n-1),temp) - ...
% h(n-1)*f*Lstage{n}(s,:)';
% end
% y{level}(:,n) = y{level}(:,n-1) + K*param.corr.b' + param.dt*f*(S(n,:)'-S(n-1,:)');
% tk = tk + h(n-1);
% end
% case{1}
% % diagonally implicit
% K = zeros(m,param.corr.s);
% tk = ti;
% switch opts.grid
% case {1,4}
% % uniform nodes/gauss lobatto (includes left endpoint)
% y{level}(:,1) = yi;
% otherwise
% % again, handle first time step differently
% for s = 1:param.corr.s
% temp = yi + K(:,1:s-1)*param.corr.A(s,1:s-1)' + ...
% param.dt * f * Sstage{1}(s,:)';
% func_hand = @(Ki) Ki - ...
% h0*ode(tk+param.corr.c(s)*h0,...
% temp+param.corr.A(s,s)*Ki) + ...
% h0*f*Lstage{1}(s,:)';
% k_init = h0*ode(tk+param.corr.c(s)*h0,...
% yi+param.corr.c(s)*param.dt*ode(tk,yi));
% [K(:,s),~,flag] = ...
% fsolve(func_hand, k_init, optimset('Display','off',...
% 'TolFun',1e-9));
% if flag > 1
% error('did not converge to root')
% end
% end
% y{level}(:,1) = yi + K*param.corr.b' + param.dt*f*S(1,:)';
% tk = tk+h0;
% end % switch
% for n = 2:opts.nquad
% K = zeros(m,param.corr.s);
% for s = 1:param.corr.s
% temp = y{level}(:,n-1) + K(:,1:s-1)*param.corr.A(s,1:s-1)' + ...
% param.dt * f * (Sstage{n}(s,:)' - S(n-1,:)');
% func_hand = @(Ki) Ki - ...
% h(n-1)*ode(tk+param.corr.c(s)*h(n-1),...
% temp+param.corr.A(s,s)*Ki) + ...
% h(n-1)*f*Lstage{n}(s,:)';
% k_init = h(n-1)*ode(tk+param.corr.c(s)*h(n-1),...
% y{level}(:,n-1)+param.corr.c(s)*param.dt*ode(tk,yi));
% [K(:,s),~,flag] = ...
% fsolve(func_hand, k_init, optimset('Display','off',...
% 'TolFun',1e-9));
% if flag > 1
% error('did not converge to root')
% end
% end
% y{level}(:,n) = y{level}(:,n-1) + K*param.corr.b' + param.dt*f*(S(n,:)'-S(n-1,:)');
% tk = tk + h(n-1);
% end
% otherwise
% % implicit
% error('fully implicit integrators not coded yet');
% end
% end
switch opts.grid
case{1,4} % uniform or gauss--lobatto
sol = y{opts.levels}(:,end);
otherwise % chebychev or gauss--legendre
if opts.interp == 1
% use interpolation!
Lfinal = generate_interpolation_matrix(t,1);
sol = y{opts.levels}*Lfinal';
else
% generate picard/collocation solution
S_final = generate_integration_matrix(t,1);
sol = yi;
for n = 1:opts.nquad
sol = sol + param.dt*ode(ti + t(n)*param.dt,y{opts.levels}(:,n))*S_final(n);
end
end
end