This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
SystemSolvers.jl
303 lines (244 loc) · 7.25 KB
/
SystemSolvers.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
module SystemSolvers
using ..MPIStateArrays
using ..MPIStateArrays: array_device, realview
using ..Mesh.Grids
import ..Mesh.Grids: polynomialorder, dimensionality
using ..Mesh.Topologies
using ..DGMethods
using ..DGMethods: DGModel
using ..BalanceLaws
using Adapt
using CUDA
using LinearAlgebra
using LazyArrays
using StaticArrays
using KernelAbstractions
const weighted_norm = false
# just for testing SystemSolvers
LinearAlgebra.norm(A::MVector, p::Real, weighted::Bool) = norm(A, p)
LinearAlgebra.norm(A::MVector, weighted::Bool) = norm(A, 2, weighted)
LinearAlgebra.dot(A::MVector, B::MVector, weighted) = dot(A, B)
LinearAlgebra.norm(A::AbstractVector, p::Real, weighted::Bool) = norm(A, p)
LinearAlgebra.norm(A::AbstractVector, weighted::Bool) = norm(A, 2, weighted)
LinearAlgebra.dot(A::AbstractVector, B::AbstractVector, weighted) = dot(A, B)
export linearsolve!,
settolerance!, prefactorize, construct_preconditioner, preconditioner_solve!
export AbstractSystemSolver,
AbstractIterativeSystemSolver, AbstractNonlinearSolver
export nonlinearsolve!
"""
AbstractSystemSolver
This is an abstract type representing a generic linear solver.
"""
abstract type AbstractSystemSolver end
"""
AbstractNonlinearSolver
This is an abstract type representing a generic nonlinear solver.
"""
abstract type AbstractNonlinearSolver <: AbstractSystemSolver end
"""
LSOnly
Only applies the linear solver (no Newton solver)
"""
struct LSOnly <: AbstractNonlinearSolver
linearsolver
end
function donewtoniteration!(
rhs!,
linearoperator!,
preconditioner,
Q,
Qrhs,
solver::LSOnly,
args...,
)
@info "donewtoniteration! linearsolve!", args...
linearsolve!(
linearoperator!,
preconditioner,
solver.linearsolver,
Q,
Qrhs,
args...;
max_iters = getmaxiterations(solver.linearsolver),
)
end
"""
Solving rhs!(Q) = Qrhs via Newton,
where `F = rhs!(Q) - Qrhs`
dF/dQ(Q^n) ΔQ ≈ jvp!(ΔQ; Q^n, F(Q^n))
preconditioner ≈ dF/dQ(Q)
"""
function nonlinearsolve!(
rhs!,
jvp!,
preconditioner,
solver::AbstractNonlinearSolver,
Q::AT,
Qrhs,
args...;
max_newton_iters = 10,
cvg = Ref{Bool}(),
) where {AT}
FT = eltype(Q)
tol = solver.tol
converged = false
iters = 0
if preconditioner === nothing
preconditioner = NoPreconditioner()
end
# Initialize NLSolver, compute initial residual
initial_residual_norm = initialize!(rhs!, Q, Qrhs, solver, args...)
if initial_residual_norm < tol
converged = true
end
converged && return iters
while !converged && iters < max_newton_iters
# dF/dQ(Q^n) ΔQ ≈ jvp!(ΔQ; Q^n, F(Q^n)), update Q^n in jvp!
update_Q!(jvp!, Q, args...)
# update preconditioner based on finite difference, with jvp!
preconditioner_update!(jvp!, rhs!.f!, preconditioner, nothing, FT(NaN))
# do newton iteration with Q^{n+1} = Q^{n} - dF/dQ(Q^n)⁻¹ (rhs!(Q) - Qrhs)
residual_norm, linear_iterations = donewtoniteration!(
rhs!,
jvp!,
preconditioner,
Q,
Qrhs,
solver,
args...,
)
@info "Linear solver converged in $linear_iterations iterations"
iters += 1
preconditioner_counter_update!(preconditioner)
if !isfinite(residual_norm)
error("norm of residual is not finite after $iters iterations of `donewtoniteration!`")
end
# Check residual_norm / norm(R0)
# Comment: Should we check "correction" magitude?
# ||Delta Q|| / ||Q|| ?
relresidual = residual_norm / initial_residual_norm
if relresidual < tol || residual_norm < tol
@info "Newton converged in $iters iterations!"
converged = true
end
end
converged || @warn "Nonlinear solver did not converge after $iters iterations"
cvg[] = converged
iters
end
"""
AbstractIterativeSystemSolver
This is an abstract type representing a generic iterative
linear solver.
The available concrete implementations are:
- [`GeneralizedConjugateResidual`](@ref)
- [`GeneralizedMinimalResidual`](@ref)
"""
abstract type AbstractIterativeSystemSolver <: AbstractSystemSolver end
"""
settolerance!(solver::AbstractIterativeSystemSolver, tolerance, relative)
Sets the relative or absolute tolerance of the iterative linear solver
`solver` to `tolerance`.
"""
settolerance!(
solver::AbstractIterativeSystemSolver,
tolerance,
relative = true,
) = (relative ? (solver.rtol = tolerance) : (solver.atol = tolerance))
doiteration!(
linearoperator!,
preconditioner,
Q,
Qrhs,
solver::AbstractIterativeSystemSolver,
threshold,
args...,
) = throw(MethodError(
doiteration!,
(linearoperator!, preconditioner, Q, Qrhs, solver, tolerance, args...),
))
initialize!(
linearoperator!,
Q,
Qrhs,
solver::AbstractIterativeSystemSolver,
args...,
) = throw(MethodError(initialize!, (linearoperator!, Q, Qrhs, solver, args...)))
"""
prefactorize(linop!, linearsolver, args...)
Prefactorize the in-place linear operator `linop!` for use with `linearsolver`.
"""
function prefactorize(
linop!,
linearsolver::AbstractIterativeSystemSolver,
args...,
)
return nothing
end
"""
linearsolve!(linearoperator!, solver::AbstractIterativeSystemSolver, Q, Qrhs, args...)
Solves a linear problem defined by the `linearoperator!` function and the state
`Qrhs`, i.e,
```math
L(Q) = Q_{rhs}
```
using the `solver` and the initial guess `Q`. After the call `Q` contains the
solution. The arguments `args` is passed to `linearoperator!` when it is
called.
"""
function linearsolve!(
linearoperator!,
preconditioner,
solver::AbstractIterativeSystemSolver,
Q,
Qrhs,
args...;
max_iters = length(Q),
cvg = Ref{Bool}(),
)
converged = false
iters = 0
if preconditioner === nothing
preconditioner = NoPreconditioner()
end
converged, threshold =
initialize!(linearoperator!, Q, Qrhs, solver, args...)
converged && return iters
while !converged && iters < max_iters
converged, inner_iters, residual_norm = doiteration!(
linearoperator!,
preconditioner,
Q,
Qrhs,
solver,
threshold,
args...,
)
iters += inner_iters
if !isfinite(residual_norm)
error("norm of residual is not finite after $iters iterations of `doiteration!`")
end
achieved_tolerance = residual_norm / threshold * solver.rtol
end
converged || @warn "Solver did not attain convergence after $iters iterations"
cvg[] = converged
iters
end
@kernel function linearcombination!(Q, cs, Xs, increment::Bool)
i = @index(Global, Linear)
if !increment
@inbounds Q[i] = -zero(eltype(Q))
end
@inbounds for j in 1:length(cs)
Q[i] += cs[j] * Xs[j][i]
end
end
include("generalized_minimal_residual_solver.jl")
include("generalized_conjugate_residual_solver.jl")
include("conjugate_gradient_solver.jl")
include("columnwise_lu_solver.jl")
include("preconditioners.jl")
include("batched_generalized_minimal_residual_solver.jl")
include("jacobian_free_newton_krylov_solver.jl")
end