@@ -288,14 +288,30 @@ end
288
288
# different. NonlinearSolve is more for robust / cached solvers while SimpleNonlinearSolve
289
289
# is meant for low overhead solvers, users can opt into the other termination modes but the
290
290
# default is to use the least overhead version.
291
- function init_termination_cache (abstol, reltol, du, u, :: Nothing )
292
- return init_termination_cache (abstol, reltol, du, u, AbsNormTerminationMode ())
291
+ function init_termination_cache (prob:: NonlinearProblem , abstol, reltol, du, u, :: Nothing )
292
+ return init_termination_cache (prob, abstol, reltol, du, u,
293
+ AbsNormTerminationMode (Base. Fix1 (maximum, abs)))
293
294
end
294
- function init_termination_cache (abstol, reltol, du, u, tc:: AbstractNonlinearTerminationMode )
295
+ function init_termination_cache (
296
+ prob:: NonlinearLeastSquaresProblem , abstol, reltol, du, u, :: Nothing )
297
+ return init_termination_cache (prob, abstol, reltol, du, u,
298
+ AbsNormTerminationMode (Base. Fix2 (norm, 2 )))
299
+ end
300
+
301
+ function init_termination_cache (
302
+ prob:: Union{NonlinearProblem, NonlinearLeastSquaresProblem} ,
303
+ abstol, reltol, du, u, tc:: AbstractNonlinearTerminationMode )
295
304
T = promote_type (eltype (du), eltype (u))
296
305
abstol = __get_tolerance (u, abstol, T)
297
306
reltol = __get_tolerance (u, reltol, T)
298
- tc_cache = init (du, u, tc; abstol, reltol)
307
+ tc_ = if hasfield (typeof (tc), :internalnorm ) && tc. internalnorm === nothing
308
+ internalnorm = ifelse (
309
+ prob isa NonlinearProblem, Base. Fix1 (maximum, abs), Base. Fix2 (norm, 2 ))
310
+ DiffEqBase. set_termination_mode_internalnorm (tc, internalnorm)
311
+ else
312
+ tc
313
+ end
314
+ tc_cache = init (du, u, tc_; abstol, reltol, use_deprecated_retcodes = Val (false ))
299
315
return DiffEqBase. get_abstol (tc_cache), DiffEqBase. get_reltol (tc_cache), tc_cache
300
316
end
301
317
@@ -305,45 +321,25 @@ function check_termination(tc_cache, fx, x, xo, prob, alg)
305
321
end
306
322
function check_termination (tc_cache, fx, x, xo, prob, alg,
307
323
:: AbstractNonlinearTerminationMode )
308
- if Bool ( tc_cache (fx, x, xo))
324
+ tc_cache (fx, x, xo) &&
309
325
return build_solution (prob, alg, x, fx; retcode = ReturnCode. Success)
310
- end
311
326
return nothing
312
327
end
313
328
function check_termination (tc_cache, fx, x, xo, prob, alg,
314
329
:: AbstractSafeNonlinearTerminationMode )
315
- if Bool (tc_cache (fx, x, xo))
316
- if tc_cache. retcode == NonlinearSafeTerminationReturnCode. Success
317
- retcode = ReturnCode. Success
318
- elseif tc_cache. retcode == NonlinearSafeTerminationReturnCode. PatienceTermination
319
- retcode = ReturnCode. ConvergenceFailure
320
- elseif tc_cache. retcode == NonlinearSafeTerminationReturnCode. ProtectiveTermination
321
- retcode = ReturnCode. Unstable
322
- else
323
- error (" Unknown termination code: $(tc_cache. retcode) " )
324
- end
325
- return build_solution (prob, alg, x, fx; retcode)
326
- end
330
+ tc_cache (fx, x, xo) &&
331
+ return build_solution (prob, alg, x, fx; retcode = tc_cache. retcode)
327
332
return nothing
328
333
end
329
334
function check_termination (tc_cache, fx, x, xo, prob, alg,
330
335
:: AbstractSafeBestNonlinearTerminationMode )
331
- if Bool (tc_cache (fx, x, xo))
332
- if tc_cache. retcode == NonlinearSafeTerminationReturnCode. Success
333
- retcode = ReturnCode. Success
334
- elseif tc_cache. retcode == NonlinearSafeTerminationReturnCode. PatienceTermination
335
- retcode = ReturnCode. ConvergenceFailure
336
- elseif tc_cache. retcode == NonlinearSafeTerminationReturnCode. ProtectiveTermination
337
- retcode = ReturnCode. Unstable
338
- else
339
- error (" Unknown termination code: $(tc_cache. retcode) " )
340
- end
336
+ if tc_cache (fx, x, xo)
341
337
if isinplace (prob)
342
338
prob. f (fx, x, prob. p)
343
339
else
344
340
fx = prob. f (x, prob. p)
345
341
end
346
- return build_solution (prob, alg, tc_cache. u, fx; retcode)
342
+ return build_solution (prob, alg, tc_cache. u, fx; retcode = tc_cache . retcode )
347
343
end
348
344
return nothing
349
345
end
0 commit comments