-
Notifications
You must be signed in to change notification settings - Fork 98
/
NLSolvers.jl
177 lines (148 loc) · 3.97 KB
/
NLSolvers.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
# Mock
"""
struct NewtonRaphsonSolver <:NonlinearSolver
# Private fields
end
Vanilla Newton-Raphson method
"""
struct NewtonRaphsonSolver <:NonlinearSolver
ls::LinearSolver
tol::Float64
max_nliters::Int
end
struct NewtonRaphsonCache <: GridapType
A::AbstractMatrix
b::AbstractVector
dx::AbstractVector
ns::NumericalSetup
end
function solve!(x::AbstractVector,nls::NewtonRaphsonSolver,op::NonlinearOperator,cache::Nothing)
b = residual(op, x)
A = jacobian(op, x)
dx = similar(b)
ss = symbolic_setup(nls.ls, A)
ns = numerical_setup(ss,A)
_solve_nr!(x,A,b,dx,ns,nls,op)
NewtonRaphsonCache(A,b,dx,ns)
end
function solve!(
x::AbstractVector,nls::NewtonRaphsonSolver,op::NonlinearOperator,cache::NewtonRaphsonCache)
b = cache.b
A = cache.A
dx = cache.dx
ns = cache.ns
residual!(b, op, x)
jacobian!(A, op, x)
numerical_setup!(ns,A)
_solve_nr!(x,A,b,dx,ns,nls,op)
cache
end
function _solve_nr!(x,A,b,dx,ns,nls,op)
# Check for convergence on the initial residual
isconv, conv0 = _check_convergence(nls,b)
if isconv; return; end
# Newton-like iterations
for nliter in 1:nls.max_nliters
# Solve linearized problem
rmul!(b,-1)
solve!(dx,ns,b)
x .+= dx
# Check convergence for the current residual
residual!(b, op, x)
isconv = _check_convergence(nls, b, conv0)
if isconv; return; end
if nliter == nls.max_nliters
@unreachable
end
# Assemble jacobian (fast in-place version)
# and prepare solver
jacobian!(A, op, x)
numerical_setup!(ns,A)
end
end
function _check_convergence(nls,b)
m0 = maximum(abs,b)
return (false, m0)
end
function _check_convergence(nls,b,m0)
m = maximum(abs,b)
return m < nls.tol * m0
end
# Default concrete implementation
"""
struct NLSolver <: NonlinearSolver
# private fields
end
The cache generated when using this solver has a field `result` that hosts the result
object generated by the underlying `nlsolve` function. It corresponds to the most latest solve.
"""
struct NLSolver <: NonlinearSolver
ls::LinearSolver
kwargs::Dict
end
"""
NLSolver(ls::LinearSolver;kwargs...)
NLSolver(;kwargs...)
Same kwargs as in `nlsolve`.
If `ls` is provided, it is not possible to use the `linsolve` kw-argument.
"""
function NLSolver(;kwargs...)
ls = BackslashSolver()
NLSolver(ls;kwargs...)
end
function NLSolver(ls::LinearSolver;kwargs...)
@assert ! haskey(kwargs,:linsolve) "linsolve cannot be used here. It is managed internally"
NLSolver(ls,kwargs)
end
mutable struct NLSolversCache <: GridapType
f0::AbstractVector
j0::AbstractMatrix
df::OnceDifferentiable
ns::NumericalSetup
result
end
function solve!(x::AbstractVector,nls::NLSolver,op::NonlinearOperator,cache::Nothing)
cache = _new_nlsolve_cache(x,nls,op)
_nlsolve_with_updated_cache!(x,nls,op,cache)
cache
end
function solve!(
x::AbstractVector,nls::NLSolver,op::NonlinearOperator,cache::NLSolversCache)
cache = _update_nlsolve_cache!(cache,x,op)
_nlsolve_with_updated_cache!(x,nls,op,cache)
cache
end
function _nlsolve_with_updated_cache!(x,nls,op,cache)
df = cache.df
ns = cache.ns
kwargs = nls.kwargs
function linsolve!(x,A,b)
numerical_setup!(ns,A)
solve!(x,ns,b)
end
r = nlsolve(df,x;linsolve=linsolve!,kwargs...)
cache.result = r
copy_entries!(x,r.zero)
end
function _new_nlsolve_cache(x0,nls,op)
f!(r,x) = residual!(r,op,x)
j!(j,x) = jacobian!(j,op,x)
fj!(r,j,x) = residual_and_jacobian!(r,j,op,x)
f0, j0 = residual_and_jacobian(op,x0)
df = OnceDifferentiable(f!,j!,fj!,x0,f0,j0)
ss = symbolic_setup(nls.ls,j0)
ns = numerical_setup(ss,j0)
NLSolversCache(f0,j0,df,ns,nothing)
end
function _update_nlsolve_cache!(cache,x0,op)
f!(r,x) = residual!(r,op,x)
j!(j,x) = jacobian!(j,op,x)
fj!(r,j,x) = residual_and_jacobian!(r,j,op,x)
f0 = cache.f0
j0 = cache.j0
ns = cache.ns
residual_and_jacobian!(f0,j0,op,x0)
df = OnceDifferentiable(f!,j!,fj!,x0,f0,j0)
numerical_setup!(ns,j0)
NLSolversCache(f0,j0,df,ns,nothing)
end