Skip to content

Commit e3c929a

Browse files
committed
Safety checks for MINPACK and NLsolve
1 parent cbf0861 commit e3c929a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

docs/src/basics/NonlinearSolution.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ SciMLBase.NonlinearSolution
1414
`NonlinearSafeTerminationReturnCode.ProtectiveTermination` and is caused if the step-size
1515
of the solver was too large or the objective value became non-finite.
1616
- `ReturnCode.MaxIters` - The maximum number of iterations was reached.
17+
- `ReturnCode.Failure` - The nonlinear solve failed for some reason. This is used
18+
sparingly and mostly for wrapped solvers for which we don't have a better error code.

ext/NonlinearSolveMINPACKExt.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ using MINPACK
66
function SciMLBase.__solve(prob::Union{NonlinearProblem{uType, iip},
77
NonlinearLeastSquaresProblem{uType, iip}}, alg::CMINPACK, args...;
88
abstol = 1e-6, maxiters = 100000, alias_u0::Bool = false,
9-
kwargs...) where {uType, iip}
9+
termination_condition = nothing, kwargs...) where {uType, iip}
10+
@assert termination_condition===nothing "CMINPACK does not support termination conditions!"
11+
1012
if prob.u0 isa Number
1113
u0 = [prob.u0]
1214
else
@@ -64,7 +66,11 @@ function SciMLBase.__solve(prob::Union{NonlinearProblem{uType, iip},
6466

6567
u = reshape(original.x, size(u))
6668
resid = original.f
67-
retcode = original.converged ? ReturnCode.Success : ReturnCode.Failure
69+
# retcode = original.converged ? ReturnCode.Success : ReturnCode.Failure
70+
# MINPACK lies about convergence? or maybe uses some other criteria?
71+
# We just check for absolute tolerance on the residual
72+
objective = NonlinearSolve.DEFAULT_NORM(resid)
73+
retcode = ifelse(objective abstol, ReturnCode.Success, ReturnCode.Failure)
6874

6975
return SciMLBase.build_solution(prob, alg, u, resid; retcode, original)
7076
end

ext/NonlinearSolveNLsolveExt.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ using NonlinearSolve, NLsolve, DiffEqBase, SciMLBase
44
import UnPack: @unpack
55

66
function SciMLBase.__solve(prob::NonlinearProblem, alg::NLsolveJL, args...; abstol = 1e-6,
7-
maxiters = 1000, alias_u0::Bool = false, kwargs...)
7+
maxiters = 1000, alias_u0::Bool = false, termination_condition = nothing, kwargs...)
8+
@assert termination_condition===nothing "NLsolveJL does not support termination conditions!"
9+
810
if typeof(prob.u0) <: Number
911
u0 = [prob.u0]
1012
else

0 commit comments

Comments
 (0)