-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from SciML/ap/nlsolvers
Wrapper for NLSolvers.jl
- Loading branch information
Showing
8 changed files
with
170 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# NLSolvers.jl | ||
|
||
This is a extension for importing solvers from NLSolvers.jl into the SciML interface. Note | ||
that these solvers do not come by default, and thus one needs to install the package before | ||
using these solvers: | ||
|
||
```julia | ||
using Pkg | ||
Pkg.add("NLSolvers") | ||
using NLSolvers, NonlinearSolve | ||
``` | ||
|
||
## Solver API | ||
|
||
```@docs | ||
NLSolversJL | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module NonlinearSolveNLSolversExt | ||
|
||
using ADTypes, FastClosures, NonlinearSolve, NLSolvers, SciMLBase, LinearAlgebra | ||
using FiniteDiff, ForwardDiff | ||
|
||
function SciMLBase.__solve(prob::NonlinearProblem, alg::NLSolversJL, args...; | ||
abstol = nothing, reltol = nothing, maxiters = 1000, alias_u0::Bool = false, | ||
termination_condition = nothing, kwargs...) | ||
NonlinearSolve.__test_termination_condition(termination_condition, :NLSolversJL) | ||
|
||
abstol = NonlinearSolve.DEFAULT_TOLERANCE(abstol, eltype(prob.u0)) | ||
reltol = NonlinearSolve.DEFAULT_TOLERANCE(reltol, eltype(prob.u0)) | ||
|
||
options = NEqOptions(; maxiter = maxiters, f_abstol = abstol, f_reltol = reltol) | ||
|
||
if prob.u0 isa Number | ||
f_scalar = @closure x -> prob.f(x, prob.p) | ||
|
||
if alg.autodiff === nothing | ||
if ForwardDiff.can_dual(typeof(prob.u0)) | ||
autodiff_concrete = :forwarddiff | ||
else | ||
autodiff_concrete = :finitediff | ||
end | ||
else | ||
if alg.autodiff isa AutoForwardDiff || alg.autodiff isa AutoPolyesterForwardDiff | ||
autodiff_concrete = :forwarddiff | ||
elseif alg.autodiff isa AutoFiniteDiff | ||
autodiff_concrete = :finitediff | ||
else | ||
error("Only ForwardDiff or FiniteDiff autodiff is supported.") | ||
end | ||
end | ||
|
||
if autodiff_concrete === :forwarddiff | ||
fj_scalar = @closure (Jx, x) -> begin | ||
T = typeof(NonlinearSolve.NonlinearSolveTag()) | ||
x_dual = ForwardDiff.Dual{T}(x, one(x)) | ||
y = prob.f(x_dual, prob.p) | ||
return ForwardDiff.value(y), ForwardDiff.extract_derivative(T, y) | ||
end | ||
else | ||
fj_scalar = @closure (Jx, x) -> begin | ||
_f = Base.Fix2(prob.f, prob.p) | ||
return _f(x), FiniteDiff.finite_difference_derivative(_f, x) | ||
end | ||
end | ||
|
||
prob_obj = NLSolvers.ScalarObjective(; f = f_scalar, fg = fj_scalar) | ||
prob_nlsolver = NEqProblem(prob_obj; inplace = false) | ||
res = NLSolvers.solve(prob_nlsolver, prob.u0, alg.method, options) | ||
|
||
retcode = ifelse(norm(res.info.best_residual, Inf) ≤ abstol, ReturnCode.Success, | ||
ReturnCode.MaxIters) | ||
stats = SciMLBase.NLStats(-1, -1, -1, -1, res.info.iter) | ||
|
||
return SciMLBase.build_solution(prob, alg, res.info.solution, | ||
res.info.best_residual; retcode, original = res, stats) | ||
end | ||
|
||
f!, u0, resid = NonlinearSolve.__construct_extension_f(prob; alias_u0) | ||
|
||
jac! = NonlinearSolve.__construct_extension_jac(prob, alg, u0, resid; alg.autodiff) | ||
|
||
FJ_vector! = @closure (Fx, Jx, x) -> begin | ||
f!(Fx, x) | ||
jac!(Jx, x) | ||
return Fx, Jx | ||
end | ||
|
||
prob_obj = NLSolvers.VectorObjective(; F = f!, FJ = FJ_vector!) | ||
prob_nlsolver = NEqProblem(prob_obj) | ||
|
||
res = NLSolvers.solve(prob_nlsolver, u0, alg.method, options) | ||
|
||
retcode = ifelse(norm(res.info.best_residual, Inf) ≤ abstol, ReturnCode.Success, | ||
ReturnCode.MaxIters) | ||
stats = SciMLBase.NLStats(-1, -1, -1, -1, res.info.iter) | ||
|
||
return SciMLBase.build_solution(prob, alg, res.info.solution, | ||
res.info.best_residual; retcode, original = res, stats) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters