@@ -28,6 +28,22 @@ struct NLSolveSafeTerminationOptions{T1, T2, T3}
28
28
min_max_factor:: T3
29
29
end
30
30
31
+ TruncatedStacktraces. @truncate_stacktrace NLSolveSafeTerminationOptions
32
+
33
+ Base. @kwdef mutable struct NLSolveSafeTerminationResult{T}
34
+ best_objective_value:: T = Inf64
35
+ best_objective_value_iteration:: Int = 0
36
+ return_code:: NLSolveSafeTerminationReturnCode.T = NLSolveSafeTerminationReturnCode. Failure
37
+ end
38
+
39
+ # Remove once support for AbstractDict has been dropped
40
+ function __setproperty! (n:: NLSolveSafeTerminationResult , prop:: Symbol , value)
41
+ setproperty! (n, prop, value)
42
+ end
43
+ function __setproperty! (d:: AbstractDict , prop:: Symbol , value)
44
+ d[prop] = value
45
+ end
46
+
31
47
const BASIC_TERMINATION_MODES = (NLSolveTerminationMode. SteadyStateDefault,
32
48
NLSolveTerminationMode. NLSolveDefault,
33
49
NLSolveTerminationMode. Norm, NLSolveTerminationMode. Rel,
@@ -89,6 +105,8 @@ struct NLSolveTerminationCondition{mode, T,
89
105
safe_termination_options:: S
90
106
end
91
107
108
+ TruncatedStacktraces. @truncate_stacktrace NLSolveTerminationCondition 1
109
+
92
110
function Base. show (io:: IO , s:: NLSolveTerminationCondition{mode} ) where {mode}
93
111
print (io,
94
112
" NLSolveTerminationCondition(mode = $(mode) , abstol = $(s. abstol) , reltol = $(s. reltol) " )
@@ -116,8 +134,14 @@ function NLSolveTerminationCondition(mode; abstol::T = 1e-8, reltol::T = 1e-6,
116
134
return NLSolveTerminationCondition {mode, T, typeof(options)} (abstol, reltol, options)
117
135
end
118
136
119
- function (cond:: NLSolveTerminationCondition )(storage:: Union{<:AbstractDict, Nothing} )
137
+ function (cond:: NLSolveTerminationCondition )(storage:: Union {<: AbstractDict ,
138
+ NLSolveSafeTerminationResult,
139
+ Nothing})
120
140
mode = get_termination_mode (cond)
141
+ if storage isa AbstractDict
142
+ Base. depwarn (" `storage` of type ($(typeof (storage)) <: AbstractDict) has been deprecated. Pass in a `NLSolveSafeTerminationResult` instance instead" ,
143
+ :NLSolveTerminationCondition )
144
+ end
121
145
# We need both the dispatches to support solvers that don't use the integrator
122
146
# interface like SimpleNonlinearSolve
123
147
if mode in BASIC_TERMINATION_MODES
@@ -144,8 +168,8 @@ function (cond::NLSolveTerminationCondition)(storage::Union{<:AbstractDict, Noth
144
168
patience_objective_multiplier = cond. safe_termination_options. patience_objective_multiplier
145
169
146
170
if mode ∈ SAFE_BEST_TERMINATION_MODES
147
- storage[ :best_objective_value ] = aType (Inf )
148
- storage[ :best_objective_value_iteration ] = 0
171
+ __setproperty! ( storage, :best_objective_value , aType (Inf ) )
172
+ __setproperty! ( storage, :best_objective_value_iteration , 0 )
149
173
end
150
174
151
175
if mode ∈ SAFE_BEST_TERMINATION_MODES
@@ -158,14 +182,15 @@ function (cond::NLSolveTerminationCondition)(storage::Union{<:AbstractDict, Noth
158
182
159
183
if mode ∈ SAFE_BEST_TERMINATION_MODES
160
184
if objective < storage[:best_objective_value ]
161
- storage[ :best_objective_value ] = objective
162
- storage[ :best_objective_value_iteration ] = nstep + 1
185
+ __setproperty! ( storage, :best_objective_value , objective)
186
+ __setproperty! ( storage, :best_objective_value_iteration , nstep + 1 )
163
187
end
164
188
end
165
189
166
190
# Main Termination Criteria
167
191
if objective <= criteria
168
- storage[:return_code ] = NLSolveSafeTerminationReturnCode. Success
192
+ __setproperty! (storage, :return_code ,
193
+ NLSolveSafeTerminationReturnCode. Success)
169
194
return true
170
195
end
171
196
@@ -181,19 +206,21 @@ function (cond::NLSolveTerminationCondition)(storage::Union{<:AbstractDict, Noth
181
206
if maximum (last_k_values) <
182
207
typeof (criteria)(cond. safe_termination_options. min_max_factor) *
183
208
minimum (last_k_values)
184
- storage[:return_code ] = NLSolveSafeTerminationReturnCode. PatienceTermination
209
+ __setproperty! (storage, :return_code ,
210
+ NLSolveSafeTerminationReturnCode. PatienceTermination)
185
211
return true
186
212
end
187
213
end
188
214
end
189
215
190
216
# Protective break
191
217
if objective >= objective_values[1 ] * protective_threshold * length (du)
192
- storage[:return_code ] = NLSolveSafeTerminationReturnCode. ProtectiveTermination
218
+ __setproperty! (storage, :return_code ,
219
+ NLSolveSafeTerminationReturnCode. ProtectiveTermination)
193
220
return true
194
221
end
195
222
196
- storage[ :return_code ] = NLSolveSafeTerminationReturnCode. Failure
223
+ __setproperty! ( storage, :return_code , NLSolveSafeTerminationReturnCode. Failure)
197
224
return false
198
225
end
199
226
return _termination_condition_closure_safe
0 commit comments