@@ -6,7 +6,65 @@ function reinit! end
6
6
7
7
end
8
8
9
- abstract type AbstractDescentDirection end
9
+ abstract type AbstractNonlinearSolveBaseAPI end # Mostly used for pretty-printing
10
+
11
+ function Base. show (io:: IO , :: MIME"text/plain" , alg:: AbstractNonlinearSolveBaseAPI )
12
+ main_name = nameof (typeof (alg))
13
+ modifiers = String[]
14
+ for field in fieldnames (typeof (alg))
15
+ val = getfield (alg, field)
16
+ Utils. is_default_value (val, field, getfield (alg, field)) && continue
17
+ push! (modifiers, " $(field) = $(val) " )
18
+ end
19
+ print (io, " $(main_name) ($(join (modifiers, " , " )) )" )
20
+ return
21
+ end
22
+
23
+ """
24
+ AbstractDescentDirection
25
+
26
+ Abstract Type for all Descent Directions used in NonlinearSolveBase. Given the Jacobian
27
+ `J` and the residual `fu`, these algorithms compute the descent direction `δu`.
28
+
29
+ For non-square Jacobian problems, if we need to solve a linear solve problem, we use a
30
+ least squares solver by default, unless the provided `linsolve` can't handle non-square
31
+ matrices, in which case we use the normal form equations ``JᵀJ δu = Jᵀ fu``. Note that
32
+ this factorization is often the faster choice, but it is not as numerically stable as
33
+ the least squares solver.
34
+
35
+ ### `InternalAPI.init` specification
36
+
37
+ ```julia
38
+ InternalAPI.init(
39
+ prob::AbstractNonlinearProblem, alg::AbstractDescentDirection, J, fu, u;
40
+ pre_inverted::Val = Val(false), linsolve_kwargs = (;),
41
+ abstol = nothing, reltol = nothing, alias_J::Bool = true,
42
+ shared::Val = Val(1), kwargs...
43
+ )::AbstractDescentCache
44
+ ```
45
+
46
+ - `pre_inverted`: whether or not the Jacobian has been pre_inverted.
47
+ - `linsolve_kwargs`: keyword arguments to pass to the linear solver.
48
+ - `abstol`: absolute tolerance for the linear solver.
49
+ - `reltol`: relative tolerance for the linear solver.
50
+ - `alias_J`: whether or not to alias the Jacobian.
51
+ - `shared`: Store multiple descent directions in the cache. Allows efficient and
52
+ correct reuse of factorizations if needed.
53
+
54
+ Some of the algorithms also allow additional keyword arguments. See the documentation for
55
+ the specific algorithm for more information.
56
+
57
+ ### Interface Functions
58
+
59
+ - `supports_trust_region(alg)`: whether or not the algorithm supports trust region
60
+ methods. Defaults to `false`.
61
+ - `supports_line_search(alg)`: whether or not the algorithm supports line search
62
+ methods. Defaults to `false`.
63
+
64
+ See also [`NewtonDescent`](@ref), [`Dogleg`](@ref), [`SteepestDescent`](@ref),
65
+ [`DampedNewtonDescent`](@ref).
66
+ """
67
+ abstract type AbstractDescentDirection <: AbstractNonlinearSolveBaseAPI end
10
68
11
69
supports_line_search (:: AbstractDescentDirection ) = false
12
70
supports_trust_region (:: AbstractDescentDirection ) = false
@@ -15,7 +73,46 @@ function get_linear_solver(alg::AbstractDescentDirection)
15
73
return Utils. safe_getproperty (alg, Val (:linsolve ))
16
74
end
17
75
18
- abstract type AbstractDescentCache end
76
+ """
77
+ AbstractDescentCache
78
+
79
+ Abstract Type for all Descent Caches.
80
+
81
+ ### `InternalAPI.solve!` specification
82
+
83
+ ```julia
84
+ InternalAPI.solve!(
85
+ cache::AbstractDescentCache, J, fu, u, idx::Val;
86
+ skip_solve::Bool = false, new_jacobian::Bool = true, kwargs...
87
+ )::DescentResult
88
+ ```
89
+
90
+ - `J`: Jacobian or Inverse Jacobian (if `pre_inverted = Val(true)`).
91
+ - `fu`: residual.
92
+ - `u`: current state.
93
+ - `idx`: index of the descent problem to solve and return. Defaults to `Val(1)`.
94
+ - `skip_solve`: Skip the direction computation and return the previous direction.
95
+ Defaults to `false`. This is useful for Trust Region Methods where the previous
96
+ direction was rejected and we want to try with a modified trust region.
97
+ - `new_jacobian`: Whether the Jacobian has been updated. Defaults to `true`.
98
+ - `kwargs`: keyword arguments to pass to the linear solver if there is one.
99
+
100
+ #### Returned values
101
+
102
+ - `descent_result`: Result in a [`DescentResult`](@ref).
103
+
104
+ ### Interface Functions
105
+
106
+ - `get_du(cache)`: get the descent direction.
107
+ - `get_du(cache, ::Val{N})`: get the `N`th descent direction.
108
+ - `set_du!(cache, δu)`: set the descent direction.
109
+ - `set_du!(cache, δu, ::Val{N})`: set the `N`th descent direction.
110
+ - `last_step_accepted(cache)`: whether or not the last step was accepted. Checks if the
111
+ cache has a `last_step_accepted` field and returns it if it does, else returns `true`.
112
+ - `preinverted_jacobian(cache)`: whether or not the Jacobian has been preinverted.
113
+ - `normal_form(cache)`: whether or not the linear solver uses normal form.
114
+ """
115
+ abstract type AbstractDescentCache <: AbstractNonlinearSolveBaseAPI end
19
116
20
117
SciMLBase. get_du (cache:: AbstractDescentCache ) = cache. δu
21
118
SciMLBase. get_du (cache:: AbstractDescentCache , :: Val{1} ) = SciMLBase. get_du (cache)
@@ -29,6 +126,79 @@ function last_step_accepted(cache::AbstractDescentCache)
29
126
return true
30
127
end
31
128
129
+ for fname in (:preinverted_jacobian , :normal_form )
130
+ @eval function $ (fname)(alg:: AbstractDescentCache )
131
+ res = Utils. unwrap_val (Utils. safe_getproperty (alg, Val ($ (QuoteNode (fname)))))
132
+ res === missing && return false
133
+ return res
134
+ end
135
+ end
136
+
137
+ """
138
+ AbstractDampingFunction
139
+
140
+ Abstract Type for Damping Functions in DampedNewton.
141
+
142
+ ### `InternalAPI.init` specification
143
+
144
+ ```julia
145
+ InternalAPI.init(
146
+ prob::AbstractNonlinearProblem, f::AbstractDampingFunction, initial_damping,
147
+ J, fu, u, args...;
148
+ internalnorm::F = L2_NORM, kwargs...
149
+ )::AbstractDampingFunctionCache
150
+ ```
151
+
152
+ Returns a [`NonlinearSolveBase.AbstractDampingFunctionCache`](@ref).
153
+ """
154
+ abstract type AbstractDampingFunction <: AbstractNonlinearAlgorithm end
155
+
156
+ """
157
+ AbstractDampingFunctionCache
158
+
159
+ Abstract Type for the Caches created by AbstractDampingFunctions
160
+
161
+ ### Interface Functions
162
+
163
+ - `requires_normal_form_jacobian(alg)`: whether or not the Jacobian is needed in normal
164
+ form. No default.
165
+ - `requires_normal_form_rhs(alg)`: whether or not the residual is needed in normal form.
166
+ No default.
167
+ - `returns_norm_form_damping(alg)`: whether or not the damping function returns the
168
+ damping factor in normal form. Defaults to
169
+ `requires_normal_form_jacobian(alg) || requires_normal_form_rhs(alg)`.
170
+ - `(cache::AbstractDampingFunctionCache)(::Nothing)`: returns the damping factor. The type
171
+ of the damping factor returned from `solve!` is guaranteed to be the same as this.
172
+
173
+ ### `InternalAPI.solve!` specification
174
+
175
+ ```julia
176
+ InternalAPI.solve!(
177
+ cache::AbstractDampingFunctionCache, J, fu, u, δu, descent_stats
178
+ )
179
+ ```
180
+
181
+ Returns the damping factor.
182
+ """
183
+ abstract type AbstractDampingFunctionCache <: AbstractNonlinearAlgorithm end
184
+
185
+ function requires_normal_form_jacobian end
186
+ function requires_normal_form_rhs end
187
+ function returns_norm_form_damping (f:: F ) where {F}
188
+ return requires_normal_form_jacobian (f) || requires_normal_form_rhs (f)
189
+ end
190
+
191
+ """
192
+ AbstractNonlinearSolveAlgorithm <: AbstractNonlinearAlgorithm
193
+
194
+ Abstract Type for all NonlinearSolveBase Algorithms.
195
+
196
+ ### Interface Functions
197
+
198
+ - `concrete_jac(alg)`: whether or not the algorithm uses a concrete Jacobian. Defaults
199
+ to `nothing`.
200
+ - `get_name(alg)`: get the name of the algorithm.
201
+ """
32
202
abstract type AbstractNonlinearSolveAlgorithm <: AbstractNonlinearAlgorithm end
33
203
34
204
get_name (alg:: AbstractNonlinearSolveAlgorithm ) = Utils. safe_getproperty (alg, Val (:name ))
@@ -47,20 +217,20 @@ concrete_jac(v::Bool) = v
47
217
concrete_jac (:: Val{false} ) = false
48
218
concrete_jac (:: Val{true} ) = true
49
219
50
- abstract type AbstractNonlinearSolveCache end
220
+ abstract type AbstractNonlinearSolveCache <: AbstractNonlinearSolveBaseAPI end
51
221
52
222
"""
53
223
AbstractLinearSolverCache
54
224
55
- Abstract Type for all Linear Solvers used in NonlinearSolve . Subtypes of these are
225
+ Abstract Type for all Linear Solvers used in NonlinearSolveBase . Subtypes of these are
56
226
meant to be constructured via [`construct_linear_solver`](@ref).
57
227
"""
58
- abstract type AbstractLinearSolverCache end
228
+ abstract type AbstractLinearSolverCache <: AbstractNonlinearSolveBaseAPI end
59
229
60
230
"""
61
231
AbstractJacobianCache
62
232
63
- Abstract Type for all Jacobian Caches used in NonlinearSolve . Subtypes of these are
233
+ Abstract Type for all Jacobian Caches used in NonlinearSolveBase . Subtypes of these are
64
234
meant to be constructured via [`construct_jacobian_cache`](@ref).
65
235
"""
66
- abstract type AbstractJacobianCache end
236
+ abstract type AbstractJacobianCache <: AbstractNonlinearSolveBaseAPI end
0 commit comments