@@ -288,14 +288,30 @@ end
288288# different. NonlinearSolve is more for robust / cached solvers while SimpleNonlinearSolve
289289# is meant for low overhead solvers, users can opt into the other termination modes but the
290290# 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)))
293294end
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 )
295304 T = promote_type (eltype (du), eltype (u))
296305 abstol = __get_tolerance (u, abstol, T)
297306 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 ))
299315 return DiffEqBase. get_abstol (tc_cache), DiffEqBase. get_reltol (tc_cache), tc_cache
300316end
301317
@@ -305,45 +321,25 @@ function check_termination(tc_cache, fx, x, xo, prob, alg)
305321end
306322function check_termination (tc_cache, fx, x, xo, prob, alg,
307323 :: AbstractNonlinearTerminationMode )
308- if Bool ( tc_cache (fx, x, xo))
324+ tc_cache (fx, x, xo) &&
309325 return build_solution (prob, alg, x, fx; retcode = ReturnCode. Success)
310- end
311326 return nothing
312327end
313328function check_termination (tc_cache, fx, x, xo, prob, alg,
314329 :: 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)
327332 return nothing
328333end
329334function check_termination (tc_cache, fx, x, xo, prob, alg,
330335 :: 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)
341337 if isinplace (prob)
342338 prob. f (fx, x, prob. p)
343339 else
344340 fx = prob. f (x, prob. p)
345341 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 )
347343 end
348344 return nothing
349345end
0 commit comments