Skip to content

Commit

Permalink
Merge pull request #136 from SciML/ap/nlls_term
Browse files Browse the repository at this point in the history
Use the different norms for termination
  • Loading branch information
ChrisRackauckas committed Mar 31, 2024
2 parents 0f02306 + e37f938 commit a8fda19
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 41 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SimpleNonlinearSolve"
uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7"
authors = ["SciML"]
version = "1.6.0"
version = "1.7.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down Expand Up @@ -39,13 +39,13 @@ SimpleNonlinearSolveZygoteExt = "Zygote"
ADTypes = "0.2.6"
AllocCheck = "0.1.1"
Aqua = "0.8"
ArrayInterface = "7.7"
ArrayInterface = "7.8"
CUDA = "5.2"
ChainRulesCore = "1.22"
ConcreteStructs = "0.2.3"
DiffEqBase = "6.146"
DiffEqBase = "6.149"
DiffResults = "1.1"
FastClosures = "0.3"
FastClosures = "0.3.2"
FiniteDiff = "2.22"
ForwardDiff = "0.10.36"
LinearAlgebra = "1.10"
Expand All @@ -59,7 +59,7 @@ Random = "1.10"
ReTestItems = "1.23"
Reexport = "1.2"
ReverseDiff = "1.15"
SciMLBase = "2.26.3"
SciMLBase = "2.28.0"
SciMLSensitivity = "7.56"
StaticArrays = "1.9"
StaticArraysCore = "1.4.2"
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/broyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleBroyden, args...;
@bb δJ⁻¹n = copy(x)
@bb δJ⁻¹ = copy(J⁻¹)

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

ls_cache = __get_linesearch(alg) === Val(true) ?
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/dfsane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane{M}, args...
τ_min = T(alg.τ_min)
τ_max = T(alg.τ_max)

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

fx_norm = NONLINEARSOLVE_DEFAULT_NORM(fx)^nexp
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/halley.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleHalley, args...;
T = eltype(x)

autodiff = __get_concrete_autodiff(prob, alg.autodiff)
abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

@bb xo = copy(x)
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/klement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleKlement, args...;
T = eltype(x)
fx = _get_fx(prob, x)

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

@bb δx = copy(x)
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/lbroyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end

U, Vᵀ = __init_low_rank_jacobian(x, fx, x isa StaticArray ? threshold : Val(η))

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

@bb xo = copy(x)
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/raphson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function SciMLBase.__solve(prob::Union{NonlinearProblem, NonlinearLeastSquaresPr
@bb xo = copy(x)
J, jac_cache = jacobian_cache(autodiff, prob.f, fx, x, prob.p)

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

for i in 1:maxiters
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/trustRegion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleTrustRegion, args.
J, jac_cache = jacobian_cache(autodiff, prob.f, fx, x, prob.p)
fx, ∇f = value_and_jacobian(autodiff, prob.f, fx, x, prob.p, jac_cache; J)

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fx, x,
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)

# Set default trust region radius if not specified by user.
Expand Down
54 changes: 25 additions & 29 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,30 @@ end
# different. NonlinearSolve is more for robust / cached solvers while SimpleNonlinearSolve
# is meant for low overhead solvers, users can opt into the other termination modes but the
# default is to use the least overhead version.
function init_termination_cache(abstol, reltol, du, u, ::Nothing)
return init_termination_cache(abstol, reltol, du, u, AbsNormTerminationMode())
function init_termination_cache(prob::NonlinearProblem, abstol, reltol, du, u, ::Nothing)
return init_termination_cache(prob, abstol, reltol, du, u,
AbsNormTerminationMode(Base.Fix1(maximum, abs)))
end
function init_termination_cache(abstol, reltol, du, u, tc::AbstractNonlinearTerminationMode)
function init_termination_cache(
prob::NonlinearLeastSquaresProblem, abstol, reltol, du, u, ::Nothing)
return init_termination_cache(prob, abstol, reltol, du, u,
AbsNormTerminationMode(Base.Fix2(norm, 2)))
end

function init_termination_cache(
prob::Union{NonlinearProblem, NonlinearLeastSquaresProblem},
abstol, reltol, du, u, tc::AbstractNonlinearTerminationMode)
T = promote_type(eltype(du), eltype(u))
abstol = __get_tolerance(u, abstol, T)
reltol = __get_tolerance(u, reltol, T)
tc_cache = init(du, u, tc; abstol, reltol)
tc_ = if hasfield(typeof(tc), :internalnorm) && tc.internalnorm === nothing
internalnorm = ifelse(
prob isa NonlinearProblem, Base.Fix1(maximum, abs), Base.Fix2(norm, 2))
DiffEqBase.set_termination_mode_internalnorm(tc, internalnorm)
else
tc
end
tc_cache = init(du, u, tc_; abstol, reltol, use_deprecated_retcodes = Val(false))
return DiffEqBase.get_abstol(tc_cache), DiffEqBase.get_reltol(tc_cache), tc_cache
end

Expand All @@ -305,45 +321,25 @@ function check_termination(tc_cache, fx, x, xo, prob, alg)
end
function check_termination(tc_cache, fx, x, xo, prob, alg,
::AbstractNonlinearTerminationMode)
if Bool(tc_cache(fx, x, xo))
tc_cache(fx, x, xo) &&
return build_solution(prob, alg, x, fx; retcode = ReturnCode.Success)
end
return nothing
end
function check_termination(tc_cache, fx, x, xo, prob, alg,
::AbstractSafeNonlinearTerminationMode)
if Bool(tc_cache(fx, x, xo))
if tc_cache.retcode == NonlinearSafeTerminationReturnCode.Success
retcode = ReturnCode.Success
elseif tc_cache.retcode == NonlinearSafeTerminationReturnCode.PatienceTermination
retcode = ReturnCode.ConvergenceFailure
elseif tc_cache.retcode == NonlinearSafeTerminationReturnCode.ProtectiveTermination
retcode = ReturnCode.Unstable
else
error("Unknown termination code: $(tc_cache.retcode)")
end
return build_solution(prob, alg, x, fx; retcode)
end
tc_cache(fx, x, xo) &&
return build_solution(prob, alg, x, fx; retcode = tc_cache.retcode)
return nothing
end
function check_termination(tc_cache, fx, x, xo, prob, alg,
::AbstractSafeBestNonlinearTerminationMode)
if Bool(tc_cache(fx, x, xo))
if tc_cache.retcode == NonlinearSafeTerminationReturnCode.Success
retcode = ReturnCode.Success
elseif tc_cache.retcode == NonlinearSafeTerminationReturnCode.PatienceTermination
retcode = ReturnCode.ConvergenceFailure
elseif tc_cache.retcode == NonlinearSafeTerminationReturnCode.ProtectiveTermination
retcode = ReturnCode.Unstable
else
error("Unknown termination code: $(tc_cache.retcode)")
end
if tc_cache(fx, x, xo)
if isinplace(prob)
prob.f(fx, x, prob.p)
else
fx = prob.f(x, prob.p)
end
return build_solution(prob, alg, tc_cache.u, fx; retcode)
return build_solution(prob, alg, tc_cache.u, fx; retcode = tc_cache.retcode)
end
return nothing
end
Expand Down

2 comments on commit a8fda19

@avik-pal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104010

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.7.0 -m "<description of version>" a8fda1999e1f88867980e1288f5e905b86917ccf
git push origin v1.7.0

Please sign in to comment.