|
| 1 | +""" |
| 2 | + SimpleBroyden(; linesearch = Val(false), alpha = nothing) |
1 | 3 |
|
| 4 | +A low-overhead implementation of Broyden. This method is non-allocating on scalar and static |
| 5 | +array problems. |
| 6 | +
|
| 7 | +### Keyword Arguments |
| 8 | +
|
| 9 | + - `linesearch`: If `linesearch` is `Val(true)`, then we use the `LiFukushimaLineSearch` |
| 10 | + line search else no line search is used. For advanced customization of the line search, |
| 11 | + use `Broyden` from `NonlinearSolve.jl`. |
| 12 | + - `alpha`: Scale the initial jacobian initialization with `alpha`. If it is `nothing`, we |
| 13 | + will compute the scaling using `2 * norm(fu) / max(norm(u), true)`. |
| 14 | +""" |
| 15 | +@concrete struct SimpleBroyden <: AbstractSimpleNonlinearSolveAlgorithm |
| 16 | + linesearch <: Union{Val{false}, Val{true}} |
| 17 | + alpha |
| 18 | +end |
| 19 | + |
| 20 | +function SimpleBroyden(; |
| 21 | + linesearch::Union{Bool, Val{true}, Val{false}} = Val(false), alpha = nothing) |
| 22 | + linesearch = linesearch isa Bool ? Val(linesearch) : linesearch |
| 23 | + return SimpleBroyden(linesearch, alpha) |
| 24 | +end |
| 25 | + |
| 26 | +function SciMLBase.__solve(prob::ImmutableNonlinearProblem, alg::SimpleBroyden, args...; |
| 27 | + abstol = nothing, reltol = nothing, maxiters = 1000, |
| 28 | + alias_u0 = false, termination_condition = nothing, kwargs...) |
| 29 | + x = Utils.maybe_unaliased(prob.u0, alias_u0) |
| 30 | + fx = Utils.get_fx(prob, x) |
| 31 | + fx = Utils.eval_f(prob, fx, x) |
| 32 | + T = promote_type(eltype(fx), eltype(x)) |
| 33 | + |
| 34 | + iszero(fx) && |
| 35 | + return SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.Success) |
| 36 | + |
| 37 | + @bb xo = copy(x) |
| 38 | + @bb δx = similar(x) |
| 39 | + @bb δf = copy(fx) |
| 40 | + @bb fprev = copy(fx) |
| 41 | + |
| 42 | + if alg.alpha === nothing |
| 43 | + fx_norm = L2_NORM(fx) |
| 44 | + x_norm = L2_NORM(x) |
| 45 | + init_α = ifelse(fx_norm ≥ 1e-5, max(x_norm, T(true)) / (2 * fx_norm), T(true)) |
| 46 | + else |
| 47 | + init_α = inv(alg.alpha) |
| 48 | + end |
| 49 | + |
| 50 | + J⁻¹ = Utils.identity_jacobian(fx, x, init_α) |
| 51 | + @bb J⁻¹δf = copy(x) |
| 52 | + @bb xᵀJ⁻¹ = copy(x) |
| 53 | + @bb δJ⁻¹n = copy(x) |
| 54 | + @bb δJ⁻¹ = copy(J⁻¹) |
| 55 | + |
| 56 | + abstol, reltol, tc_cache = NonlinearSolveBase.init_termination_cache( |
| 57 | + prob, abstol, reltol, fx, x, termination_condition, Val(:simple)) |
| 58 | + |
| 59 | + if alg.linesearch === Val(true) |
| 60 | + ls_alg = LiFukushimaLineSearch(; nan_maxiters = nothing) |
| 61 | + ls_cache = init(prob, ls_alg, fx, x) |
| 62 | + else |
| 63 | + ls_cache = nothing |
| 64 | + end |
| 65 | + |
| 66 | + for _ in 1:maxiters |
| 67 | + @bb δx = J⁻¹ × vec(fprev) |
| 68 | + @bb δx .*= -1 |
| 69 | + |
| 70 | + if ls_cache === nothing |
| 71 | + α = true |
| 72 | + else |
| 73 | + ls_sol = solve!(ls_cache, xo, δx) |
| 74 | + α = ls_sol.step_size # Ignores the return code for now |
| 75 | + end |
| 76 | + |
| 77 | + @bb @. x = xo + α * δx |
| 78 | + fx = Utils.eval_f(prob, fx, x) |
| 79 | + @bb @. δf = fx - fprev |
| 80 | + |
| 81 | + # Termination Checks |
| 82 | + solved, retcode, fx_sol, x_sol = Utils.check_termination(tc_cache, fx, x, xo, prob) |
| 83 | + solved && return SciMLBase.build_solution(prob, alg, x_sol, fx_sol; retcode) |
| 84 | + |
| 85 | + @bb J⁻¹δf = J⁻¹ × vec(δf) |
| 86 | + d = dot(δx, J⁻¹δf) |
| 87 | + @bb xᵀJ⁻¹ = transpose(J⁻¹) × vec(δx) |
| 88 | + |
| 89 | + @bb @. δJ⁻¹n = (δx - J⁻¹δf) / d |
| 90 | + |
| 91 | + δJ⁻¹n_ = Utils.safe_vec(δJ⁻¹n) |
| 92 | + xᵀJ⁻¹_ = Utils.safe_vec(xᵀJ⁻¹) |
| 93 | + @bb δJ⁻¹ = δJ⁻¹n_ × transpose(xᵀJ⁻¹_) |
| 94 | + @bb J⁻¹ .+= δJ⁻¹ |
| 95 | + |
| 96 | + @bb copyto!(xo, x) |
| 97 | + @bb copyto!(fprev, fx) |
| 98 | + end |
| 99 | + |
| 100 | + return SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.MaxIters) |
| 101 | +end |
0 commit comments