|
| 1 | +module SimpleNonlinearSolveTaylorDiffExt |
| 2 | +using SimpleNonlinearSolve: SimpleNonlinearSolve, SimpleHouseholder, Utils |
| 3 | +using NonlinearSolveBase: NonlinearSolveBase, ImmutableNonlinearProblem, |
| 4 | + AbstractNonlinearSolveAlgorithm |
| 5 | +using MaybeInplace: @bb |
| 6 | +using FastClosures: @closure |
| 7 | +import SciMLBase |
| 8 | +import TaylorDiff |
| 9 | + |
| 10 | +SimpleNonlinearSolve.is_extension_loaded(::Val{:TaylorDiff}) = true |
| 11 | + |
| 12 | +const NLBUtils = NonlinearSolveBase.Utils |
| 13 | + |
| 14 | +@inline function __get_higher_order_derivatives( |
| 15 | + ::SimpleHouseholder{N}, prob, x, fx) where {N} |
| 16 | + vN = Val(N) |
| 17 | + l = map(one, x) |
| 18 | + t = TaylorDiff.make_seed(x, l, vN) |
| 19 | + |
| 20 | + if SciMLBase.isinplace(prob) |
| 21 | + bundle = similar(fx, TaylorDiff.TaylorScalar{eltype(fx), N}) |
| 22 | + prob.f(bundle, t, prob.p) |
| 23 | + map!(TaylorDiff.value, fx, bundle) |
| 24 | + else |
| 25 | + bundle = prob.f(t, prob.p) |
| 26 | + fx = map(TaylorDiff.value, bundle) |
| 27 | + end |
| 28 | + invbundle = inv.(bundle) |
| 29 | + num = N == 1 ? map(TaylorDiff.value, invbundle) : |
| 30 | + TaylorDiff.extract_derivative(invbundle, Val(N - 1)) |
| 31 | + den = TaylorDiff.extract_derivative(invbundle, vN) |
| 32 | + return num, den, fx |
| 33 | +end |
| 34 | + |
| 35 | +function SciMLBase.__solve(prob::ImmutableNonlinearProblem, alg::SimpleHouseholder{N}, |
| 36 | + args...; abstol = nothing, reltol = nothing, maxiters = 1000, |
| 37 | + termination_condition = nothing, alias_u0 = false, kwargs...) where {N} |
| 38 | + length(prob.u0) == 1 || |
| 39 | + throw(ArgumentError("SimpleHouseholder only supports scalar problems")) |
| 40 | + x = NLBUtils.maybe_unaliased(prob.u0, alias_u0) |
| 41 | + fx = NLBUtils.evaluate_f(prob, x) |
| 42 | + |
| 43 | + iszero(fx) && |
| 44 | + return SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.Success) |
| 45 | + |
| 46 | + abstol, reltol, tc_cache = NonlinearSolveBase.init_termination_cache( |
| 47 | + prob, abstol, reltol, fx, x, termination_condition, Val(:simple)) |
| 48 | + |
| 49 | + @bb xo = similar(x) |
| 50 | + |
| 51 | + for i in 1:maxiters |
| 52 | + @bb copyto!(xo, x) |
| 53 | + num, den, fx = __get_higher_order_derivatives(alg, prob, x, fx) |
| 54 | + @bb x .+= N .* num ./ den |
| 55 | + solved, retcode, fx_sol, x_sol = Utils.check_termination(tc_cache, fx, x, xo, prob) |
| 56 | + solved && return SciMLBase.build_solution(prob, alg, x_sol, fx_sol; retcode) |
| 57 | + end |
| 58 | + |
| 59 | + return SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.MaxIters) |
| 60 | +end |
| 61 | + |
| 62 | +function SimpleNonlinearSolve.evaluate_hvvp_internal(hvvp, prob::ImmutableNonlinearProblem, u, a) |
| 63 | + if SciMLBase.isinplace(prob) |
| 64 | + binary_f = @closure (y, x) -> prob.f(y, x, prob.p) |
| 65 | + TaylorDiff.derivative!(hvvp, binary_f, cache.fu, u, a, Val(2)) |
| 66 | + else |
| 67 | + unary_f = Base.Fix2(prob.f, prob.p) |
| 68 | + hvvp = TaylorDiff.derivative(unary_f, u, a, Val(2)) |
| 69 | + end |
| 70 | + hvvp |
| 71 | +end |
| 72 | + |
| 73 | +end |
0 commit comments