-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
newton.jl
337 lines (285 loc) · 9.48 KB
/
newton.jl
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
329
330
331
332
333
334
335
336
337
## initialize!
@muladd function initialize!(nlsolver::NLSolver{<:NLNewton,false}, integrator)
@unpack dt = integrator
@unpack cache = nlsolver
cache.invγdt = inv(dt * nlsolver.γ)
cache.tstep = integrator.t + nlsolver.c * dt
nothing
end
@muladd function initialize!(nlsolver::NLSolver{<:NLNewton,true}, integrator)
@unpack u,uprev,t,dt,opts = integrator
@unpack cache = nlsolver
@unpack weight = cache
cache.invγdt = inv(dt * nlsolver.γ)
cache.tstep = integrator.t + nlsolver.c * dt
calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, u,
opts.abstol, opts.reltol, opts.internalnorm, t)
nothing
end
## compute_step!
"""
compute_step!(nlsolver::NLSolver{<:NLNewton}, integrator)
Compute next iterate of numerically stable modified Newton iteration
that is specialized for implicit methods.
Please check
https://github.com/SciML/DiffEqDevMaterials/blob/master/newton/output/main.pdf
for more details.
# References
M.E.Hoseaa and L.F.Shampine, "Analysis and implementation of TR-BDF2",
Applied Numerical Mathematics, Volume 20, Issues 1–2, February 1996, Pages
21-37.
[doi:10.1016/0168-9274(95)00115-8](https://doi.org/10.1016/0168-9274(95)00115-8).
Ernst Hairer and Gerhard Wanner, "Solving Ordinary Differential
Equations II, Springer Series in Computational Mathematics. ISBN
978-3-642-05221-7. Section IV.8.
[doi:10.1007/978-3-642-05221-7](https://doi.org/10.1007/978-3-642-05221-7).
"""
@muladd function compute_step!(nlsolver::NLSolver{<:NLNewton,false}, integrator)
@unpack uprev,t,p,dt,opts = integrator
@unpack z,tmp,γ,α,cache = nlsolver
@unpack tstep,W,invγdt = cache
f = nlsolve_f(integrator)
isdae = f isa DAEFunction
if isdae
# not all predictors are uprev, for other forms of predictors, defined in u₀
if isdefined(integrator.cache, :u₀)
ustep = @.. integrator.cache.u₀ + z
else
ustep = @.. uprev + z
end
dustep = @. (tmp + α * z) * invγdt
ztmp = f(dustep, ustep, p, t)
else
mass_matrix = integrator.f.mass_matrix
if nlsolver.method === COEFFICIENT_MULTISTEP
ustep = z
# tmp = outertmp ./ hγ
if mass_matrix === I
ztmp = tmp .+ f(z, p, tstep) .- (α * invγdt) .* z
else
update_coefficients!(mass_matrix, ustep, p, tstep)
ztmp = tmp .+ f(z, p, tstep) .- (mass_matrix * z) .* (α * invγdt)
end
else
ustep = @. tmp + γ * z
if mass_matrix === I
ztmp = (dt .* f(ustep, p, tstep) .- z) .* invγdt
else
update_coefficients!(mass_matrix, ustep, p, tstep)
ztmp = (dt .* f(ustep, p, tstep) .- mass_matrix * z) .* invγdt
end
end
end
if DiffEqBase.has_destats(integrator)
integrator.destats.nf += 1
end
# update W
if W isa DiffEqBase.AbstractDiffEqLinearOperator
W = update_coefficients!(W, ustep, p, tstep)
end
dz = _reshape(W \ _vec(ztmp), axes(ztmp))
if DiffEqBase.has_destats(integrator)
integrator.destats.nsolve += 1
end
atmp = calculate_residuals(dz, uprev, ustep, opts.abstol, opts.reltol, opts.internalnorm, t)
ndz = opts.internalnorm(atmp, t)
# NDF and BDF are special because the truncation error is directly
# propertional to the total displacement.
if integrator.alg isa QNDF
ndz *= error_constant(integrator, alg_order(integrator.alg))
end
# compute next iterate
nlsolver.ztmp = z .- dz
ndz
end
@muladd function compute_step!(nlsolver::NLSolver{<:NLNewton,true}, integrator)
@unpack uprev,t,p,dt,opts = integrator
@unpack z,tmp,ztmp,γ,α,iter,cache = nlsolver
@unpack W_γdt,ustep,tstep,k,atmp,dz,W,new_W,invγdt,linsolve,weight = cache
f = nlsolve_f(integrator)
isdae = f isa DAEFunction
if DiffEqBase.has_destats(integrator)
integrator.destats.nf += 1
end
if isdae
@.. ztmp = (tmp + α * z) * invγdt
# not all predictors are uprev, for other forms of predictors, defined in u₀
if isdefined(integrator.cache, :u₀)
@.. ustep = integrator.cache.u₀ + z
else
@.. ustep = uprev + z
end
f(k, ztmp, ustep, p, tstep)
b = _vec(k)
else
mass_matrix = integrator.f.mass_matrix
if nlsolver.method === COEFFICIENT_MULTISTEP
ustep = z
f(k, z, p, tstep)
if mass_matrix === I
@.. ztmp = tmp + k - (α * invγdt) * z
else
update_coefficients!(mass_matrix, ustep, p, tstep)
mul!(_vec(ztmp), mass_matrix, _vec(z))
@.. ztmp = tmp + k - (α * invγdt) * ztmp
end
else
@.. ustep = tmp + γ * z
f(k, ustep, p, tstep)
if mass_matrix === I
@.. ztmp = (dt * k - z) * invγdt
else
update_coefficients!(mass_matrix, ustep, p, tstep)
mul!(_vec(ztmp), mass_matrix, _vec(z))
@.. ztmp = (dt * k - ztmp) * invγdt
end
end
b = _vec(ztmp)
end
# update W
if W isa DiffEqBase.AbstractDiffEqLinearOperator
update_coefficients!(W, ustep, p, tstep)
end
if integrator.opts.adaptive
reltol = integrator.opts.reltol
else
reltol = eps(eltype(dz))
end
linres = dolinsolve(integrator, linsolve; A = iter == 1 && new_W ? W : nothing, b = _vec(b), linu = _vec(dz), reltol = reltol)
cache.linsolve = linres.cache
if DiffEqBase.has_destats(integrator)
integrator.destats.nsolve += 1
end
# relaxed Newton
# Diagonally Implicit Runge-Kutta Methods for Ordinary Differential
# Equations. A Review, by Christopher A. Kennedy and Mark H. Carpenter
# page 54.
if isdae
γdt = α * invγdt
else
γdt = γ * dt
end
!(W_γdt ≈ γdt) && (rmul!(dz, 2/(1 + γdt / W_γdt)))
calculate_residuals!(atmp, dz, uprev, ustep, opts.abstol, opts.reltol, opts.internalnorm, t)
ndz = opts.internalnorm(atmp, t)
# NDF and BDF are special because the truncation error is directly
# propertional to the total displacement.
if integrator.alg isa QNDF
ndz *= error_constant(integrator, alg_order(integrator.alg))
end
# compute next iterate
@.. ztmp = z - dz
ndz
end
@muladd function compute_step!(nlsolver::NLSolver{<:NLNewton,true,<:Array}, integrator)
@unpack uprev,t,p,dt,opts = integrator
@unpack z,tmp,ztmp,γ,α,iter,cache = nlsolver
@unpack W_γdt,ustep,tstep,k,atmp,dz,W,new_W,invγdt,linsolve,weight = cache
f = nlsolve_f(integrator)
isdae = f isa DAEFunction
if DiffEqBase.has_destats(integrator)
integrator.destats.nf += 1
end
if isdae
@inbounds @simd ivdep for i in eachindex(z)
ztmp[i] = (tmp[i] + α * z[i]) * invγdt
end
if isdefined(integrator.cache, :u₀)
@inbounds @simd ivdep for i in eachindex(z)
ustep[i] = integrator.cache.u₀[i] + z[i]
end
#@.. ustep = integrator.cache.u₀ + z
else
@inbounds @simd ivdep for i in eachindex(z)
ustep[i] = uprev[i] + z[i]
end
end
f(k, ztmp, ustep, p, tstep)
b = _vec(k)
else
mass_matrix = integrator.f.mass_matrix
if nlsolver.method === COEFFICIENT_MULTISTEP
ustep = z
f(k, z, p, tstep)
if mass_matrix === I
@inbounds @simd ivdep for i in eachindex(z)
ztmp[i] = tmp[i] + k[i] - (α * invγdt) * z[i]
end
else
update_coefficients!(mass_matrix, ustep, p, tstep)
mul!(_vec(ztmp), mass_matrix, _vec(z))
@inbounds @simd ivdep for i in eachindex(z)
ztmp[i] = tmp[i] + k[i] - (α * invγdt) * ztmp[i]
end
end
else
@inbounds @simd ivdep for i in eachindex(z)
ustep[i] = tmp[i] + γ * z[i]
end
f(k, ustep, p, tstep)
if mass_matrix === I
@inbounds @simd ivdep for i in eachindex(z)
ztmp[i] = (dt * k[i] - z[i]) * invγdt
end
else
update_coefficients!(mass_matrix, ustep, p, tstep)
mul!(_vec(ztmp), mass_matrix, _vec(z))
@inbounds @simd ivdep for i in eachindex(z)
ztmp[i] = (dt * k[i] - ztmp[i]) * invγdt
end
end
end
b = _vec(ztmp)
end
# update W
if W isa DiffEqBase.AbstractDiffEqLinearOperator
update_coefficients!(W, ustep, p, tstep)
end
if integrator.opts.adaptive
reltol = integrator.opts.reltol
else
reltol = eps(eltype(dz))
end
linres = dolinsolve(integrator, linsolve; A = iter == 1 && new_W ? W : nothing, b = _vec(b), linu = _vec(dz), reltol = reltol)
cache.linsolve = linres.cache
if DiffEqBase.has_destats(integrator)
integrator.destats.nsolve += 1
end
# relaxed Newton
# Diagonally Implicit Runge-Kutta Methods for Ordinary Differential
# Equations. A Review, by Christopher A. Kennedy and Mark H. Carpenter
# page 54.
if isdae
γdt = α * invγdt
else
γdt = γ * dt
end
!(W_γdt ≈ γdt) && (rmul!(dz, 2/(1 + γdt / W_γdt)))
calculate_residuals!(atmp, dz, uprev, ustep, opts.abstol, opts.reltol, opts.internalnorm, t)
ndz = opts.internalnorm(atmp, t)
# NDF and BDF are special because the truncation error is directly
# propertional to the total displacement.
if integrator.alg isa QNDF
ndz *= error_constant(integrator, alg_order(integrator.alg))
end
# compute next iterate
@inbounds @simd ivdep for i in eachindex(z)
ztmp[i] = z[i] - dz[i]
end
ndz
end
## resize!
function Base.resize!(nlcache::NLNewtonCache, ::AbstractNLSolver, integrator, i::Int)
resize!(nlcache.ustep, i)
resize!(nlcache.k, i)
resize!(nlcache.atmp, i)
resize!(nlcache.dz, i)
resize!(nlcache.du1, i)
if nlcache.jac_config !== nothing
resize_jac_config!(nlcache.jac_config, i)
end
resize!(nlcache.weight, i)
# resize J and W (or rather create new ones of appropriate size and type)
resize_J_W!(nlcache, integrator, i)
nothing
end