|
1 | | -struct SimpleBatchedNewtonRaphson{AD, LS, TC <: NLSolveTerminationCondition} <: |
| 1 | +struct BatchedSimpleNewtonRaphson{CS, AD, FDT, TC <: NLSolveTerminationCondition} <: |
2 | 2 | AbstractBatchedNonlinearSolveAlgorithm |
3 | | - autodiff::AD |
4 | | - linsolve::LS |
5 | 3 | termination_condition::TC |
6 | 4 | end |
7 | 5 |
|
8 | | -# Implementation of solve using Package Extensions |
| 6 | +alg_autodiff(alg::BatchedSimpleNewtonRaphson{CS, AD, FDT}) where {CS, AD, FDT} = AD |
| 7 | +diff_type(alg::BatchedSimpleNewtonRaphson{CS, AD, FDT}) where {CS, AD, FDT} = FDT |
| 8 | + |
| 9 | +function BatchedSimpleNewtonRaphson(; chunk_size = Val{0}(), |
| 10 | + autodiff = Val{true}(), |
| 11 | + diff_type = Val{:forward}, |
| 12 | + termination_condition = NLSolveTerminationCondition(NLSolveTerminationMode.NLSolveDefault; |
| 13 | + abstol = nothing, |
| 14 | + reltol = nothing)) |
| 15 | + return BatchedSimpleNewtonRaphson{SciMLBase._unwrap_val(chunk_size), |
| 16 | + SciMLBase._unwrap_val(autodiff), |
| 17 | + SciMLBase._unwrap_val(diff_type), typeof(termination_condition)}(termination_condition) |
| 18 | +end |
| 19 | + |
| 20 | +function SciMLBase.__solve(prob::NonlinearProblem, alg::BatchedSimpleNewtonRaphson; |
| 21 | + abstol = nothing, reltol = nothing, maxiters = 1000, kwargs...) |
| 22 | + iip = SciMLBase.isinplace(prob) |
| 23 | + @assert !iip "BatchedSimpleNewtonRaphson currently only supports out-of-place nonlinear problems." |
| 24 | + u, f, reconstruct = _construct_batched_problem_structure(prob) |
| 25 | + |
| 26 | + tc = alg.termination_condition |
| 27 | + mode = DiffEqBase.get_termination_mode(tc) |
| 28 | + |
| 29 | + storage = _get_storage(mode, u) |
| 30 | + |
| 31 | + xₙ, xₙ₋₁ = copy(u), copy(u) |
| 32 | + T = eltype(u) |
| 33 | + |
| 34 | + atol = _get_tolerance(abstol, tc.abstol, T) |
| 35 | + rtol = _get_tolerance(reltol, tc.reltol, T) |
| 36 | + termination_condition = tc(storage) |
| 37 | + |
| 38 | + for i in 1:maxiters |
| 39 | + if alg_autodiff(alg) |
| 40 | + fₙ, 𝓙 = value_derivative(f, xₙ) |
| 41 | + else |
| 42 | + fₙ = f(xₙ) |
| 43 | + 𝓙 = FiniteDiff.finite_difference_jacobian(f, xₙ, diff_type(alg), eltype(xₙ), fₙ) |
| 44 | + end |
| 45 | + |
| 46 | + iszero(fₙ) && return DiffEqBase.build_solution(prob, |
| 47 | + alg, |
| 48 | + reconstruct(xₙ), |
| 49 | + reconstruct(fₙ); |
| 50 | + retcode = ReturnCode.Success) |
| 51 | + |
| 52 | + δx = reshape(𝓙 \ vec(fₙ), size(xₙ)) |
| 53 | + xₙ .-= δx |
| 54 | + |
| 55 | + if termination_condition(fₙ, xₙ, xₙ₋₁, atol, rtol) |
| 56 | + retcode, xₙ, fₙ = _result_from_storage(storage, xₙ, fₙ, f, mode, iip) |
| 57 | + return DiffEqBase.build_solution(prob, |
| 58 | + alg, |
| 59 | + reconstruct(xₙ), |
| 60 | + reconstruct(fₙ); |
| 61 | + retcode) |
| 62 | + end |
| 63 | + |
| 64 | + xₙ₋₁ .= xₙ |
| 65 | + end |
| 66 | + |
| 67 | + if mode ∈ DiffEqBase.SAFE_BEST_TERMINATION_MODES |
| 68 | + xₙ = storage.u |
| 69 | + fₙ = f(xₙ) |
| 70 | + end |
| 71 | + |
| 72 | + return DiffEqBase.build_solution(prob, |
| 73 | + alg, |
| 74 | + reconstruct(xₙ), |
| 75 | + reconstruct(fₙ); |
| 76 | + retcode = ReturnCode.MaxIters) |
| 77 | +end |
0 commit comments