@@ -5,9 +5,12 @@ using ArrayInterface: ArrayInterface
5
5
using DifferentiationInterface: DifferentiationInterface
6
6
using FastClosures: @closure
7
7
using LinearAlgebra: LinearAlgebra, I, diagind
8
- using NonlinearSolveBase: NonlinearSolveBase, ImmutableNonlinearProblem
8
+ using NonlinearSolveBase: NonlinearSolveBase, ImmutableNonlinearProblem,
9
+ AbstractNonlinearTerminationMode,
10
+ AbstractSafeNonlinearTerminationMode,
11
+ AbstractSafeBestNonlinearTerminationMode
9
12
using SciMLBase: SciMLBase, AbstractNonlinearProblem, NonlinearLeastSquaresProblem,
10
- NonlinearProblem, NonlinearFunction
13
+ NonlinearProblem, NonlinearFunction, ReturnCode
11
14
using StaticArraysCore: StaticArray, SArray, SMatrix, SVector
12
15
13
16
const DI = DifferentiationInterface
@@ -60,7 +63,7 @@ function get_fx(prob::Union{ImmutableNonlinearProblem, NonlinearProblem}, x)
60
63
end
61
64
function get_fx (f:: NonlinearFunction , x, p)
62
65
if SciMLBase. isinplace (f)
63
- f. resid_prototype === nothing && return eltype (x).(f. resid_prototype)
66
+ f. resid_prototype === nothing || return eltype (x).(f. resid_prototype)
64
67
return safe_similar (x)
65
68
end
66
69
return f (x, p)
@@ -77,18 +80,18 @@ function fixed_parameter_function(prob::AbstractNonlinearProblem)
77
80
return Base. Fix2 (prob. f, prob. p)
78
81
end
79
82
80
- # __init_identity_jacobian (u::Number, fu, α = true) = oftype(u, α )
81
- # function __init_identity_jacobian(u, fu , α = true )
82
- # J = __similar(u, promote_type(eltype(u), eltype(fu)), length(fu), length(u))
83
- # fill!(J, zero(eltype(J)) )
84
- # J[diagind(J)] .= eltype(J)(α )
85
- # return J
86
- # end
87
- # function __init_identity_jacobian(u::StaticArray, fu, α = true)
88
- # S1, S2 = length(fu), length(u)
89
- # J = SMatrix{S1, S2, eltype(u)}(I * α )
90
- # return J
91
- # end
83
+ function identity_jacobian (u:: Number , fu:: Number , α = true )
84
+ return convert ( promote_type ( eltype (u), eltype (fu)) , α)
85
+ end
86
+ function identity_jacobian (u, fu, α = true )
87
+ J = safe_similar (u, promote_type ( eltype (u), eltype (fu)) )
88
+ fill! (J, zero ( eltype (J)))
89
+ J[ diagind (J)] . = eltype (J)(α)
90
+ return J
91
+ end
92
+ function identity_jacobian (u :: StaticArray , fu, α = true )
93
+ return SMatrix {length(fu), length(u), eltype(u)} (I * α)
94
+ end
92
95
93
96
identity_jacobian!! (J:: Number ) = one (J)
94
97
function identity_jacobian!! (J:: AbstractVector )
104
107
identity_jacobian!! (:: SMatrix{S1, S2, T} ) where {S1, S2, T} = SMatrix {S1, S2, T} (I)
105
108
identity_jacobian!! (:: SVector{S1, T} ) where {S1, T} = ones (SVector{S1, T})
106
109
110
+ # Termination Conditions
111
+ function check_termination (cache, fx, x, xo, prob)
112
+ return check_termination (cache, fx, x, xo, prob, cache. mode)
113
+ end
114
+
115
+ function check_termination (cache, fx, x, xo, _, :: AbstractNonlinearTerminationMode )
116
+ return cache (fx, x, xo), ReturnCode. Success, fx, x
117
+ end
118
+ function check_termination (cache, fx, x, xo, _, :: AbstractSafeNonlinearTerminationMode )
119
+ return cache (fx, x, xo), cache. retcode, fx, x
120
+ end
121
+ function check_termination (cache, fx, x, xo, prob, :: AbstractSafeBestNonlinearTerminationMode )
122
+ if cache (fx, x, xo)
123
+ x = cache. u
124
+ if SciMLBase. isinplace (prob)
125
+ prob. f (fx, x, prob. p)
126
+ else
127
+ fx = prob. f (x, prob. p)
128
+ end
129
+ return true , cache. retcode, fx, x
130
+ end
131
+ return false , ReturnCode. Default, fx, x
132
+ end
133
+
107
134
end
0 commit comments