|
| 1 | +module NonlinearSolveNLSolversExt |
| 2 | + |
| 3 | +using ADTypes, FastClosures, NonlinearSolve, NLSolvers, SciMLBase, LinearAlgebra |
| 4 | +using FiniteDiff, ForwardDiff |
| 5 | + |
| 6 | +function SciMLBase.__solve(prob::NonlinearProblem, alg::NLSolversJL, args...; |
| 7 | + abstol = nothing, reltol = nothing, maxiters = 1000, alias_u0::Bool = false, |
| 8 | + termination_condition = nothing, kwargs...) |
| 9 | + NonlinearSolve.__test_termination_condition(termination_condition, :NLSolversJL) |
| 10 | + |
| 11 | + abstol = NonlinearSolve.DEFAULT_TOLERANCE(abstol, eltype(prob.u0)) |
| 12 | + reltol = NonlinearSolve.DEFAULT_TOLERANCE(reltol, eltype(prob.u0)) |
| 13 | + |
| 14 | + options = NEqOptions(; maxiter = maxiters, f_abstol = abstol, f_reltol = reltol) |
| 15 | + |
| 16 | + if prob.u0 isa Number |
| 17 | + f_scalar = @closure x -> prob.f(x, prob.p) |
| 18 | + |
| 19 | + if alg.autodiff === nothing |
| 20 | + if ForwardDiff.can_dual(typeof(prob.u0)) |
| 21 | + autodiff_concrete = :forwarddiff |
| 22 | + else |
| 23 | + autodiff_concrete = :finitediff |
| 24 | + end |
| 25 | + else |
| 26 | + if alg.autodiff isa AutoForwardDiff || alg.autodiff isa AutoPolyesterForwardDiff |
| 27 | + autodiff_concrete = :forwarddiff |
| 28 | + elseif alg.autodiff isa AutoFiniteDiff |
| 29 | + autodiff_concrete = :finitediff |
| 30 | + else |
| 31 | + error("Only ForwardDiff or FiniteDiff autodiff is supported.") |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + if autodiff_concrete === :forwarddiff |
| 36 | + fj_scalar = @closure (Jx, x) -> begin |
| 37 | + T = typeof(NonlinearSolve.NonlinearSolveTag()) |
| 38 | + x_dual = ForwardDiff.Dual{T}(x, one(x)) |
| 39 | + y = prob.f(x_dual, prob.p) |
| 40 | + return ForwardDiff.value(y), ForwardDiff.extract_derivative(T, y) |
| 41 | + end |
| 42 | + else |
| 43 | + fj_scalar = @closure (Jx, x) -> begin |
| 44 | + _f = Base.Fix2(prob.f, prob.p) |
| 45 | + return _f(x), FiniteDiff.finite_difference_derivative(_f, x) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + prob_obj = NLSolvers.ScalarObjective(; f = f_scalar, fg = fj_scalar) |
| 50 | + prob_nlsolver = NEqProblem(prob_obj; inplace = false) |
| 51 | + res = NLSolvers.solve(prob_nlsolver, prob.u0, alg.method, options) |
| 52 | + |
| 53 | + retcode = ifelse(norm(res.info.best_residual, Inf) ≤ abstol, ReturnCode.Success, |
| 54 | + ReturnCode.MaxIters) |
| 55 | + stats = SciMLBase.NLStats(-1, -1, -1, -1, res.info.iter) |
| 56 | + |
| 57 | + return SciMLBase.build_solution(prob, alg, res.info.solution, |
| 58 | + res.info.best_residual; retcode, original = res, stats) |
| 59 | + end |
| 60 | + |
| 61 | + f!, u0, resid = NonlinearSolve.__construct_extension_f(prob; alias_u0) |
| 62 | + |
| 63 | + jac! = NonlinearSolve.__construct_extension_jac(prob, alg, u0, resid; alg.autodiff) |
| 64 | + |
| 65 | + FJ_vector! = @closure (Fx, Jx, x) -> begin |
| 66 | + f!(Fx, x) |
| 67 | + jac!(Jx, x) |
| 68 | + return Fx, Jx |
| 69 | + end |
| 70 | + |
| 71 | + prob_obj = NLSolvers.VectorObjective(; F = f!, FJ = FJ_vector!) |
| 72 | + prob_nlsolver = NEqProblem(prob_obj) |
| 73 | + |
| 74 | + res = NLSolvers.solve(prob_nlsolver, u0, alg.method, options) |
| 75 | + |
| 76 | + retcode = ifelse(norm(res.info.best_residual, Inf) ≤ abstol, ReturnCode.Success, |
| 77 | + ReturnCode.MaxIters) |
| 78 | + stats = SciMLBase.NLStats(-1, -1, -1, -1, res.info.iter) |
| 79 | + |
| 80 | + return SciMLBase.build_solution(prob, alg, res.info.solution, |
| 81 | + res.info.best_residual; retcode, original = res, stats) |
| 82 | +end |
| 83 | + |
| 84 | +end |
0 commit comments