From b2ed52b8ccfdcdeedfebf16047c916613995ca37 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Mon, 13 Mar 2023 09:27:47 -0400 Subject: [PATCH] fix typo --- docs/src/solvers/NonlinearSystemSolvers.md | 4 +- src/raphson.jl | 43 +++++++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/docs/src/solvers/NonlinearSystemSolvers.md b/docs/src/solvers/NonlinearSystemSolvers.md index b4ef3a46d..b3f2f2716 100644 --- a/docs/src/solvers/NonlinearSystemSolvers.md +++ b/docs/src/solvers/NonlinearSystemSolvers.md @@ -23,7 +23,7 @@ then `NLSolveJL`'s `:anderson` can be a good choice. ## Full List of Methods !!! note - + For the full details on the capabilities and constructors of the different solvers, see the Detailed Solver APIs section! @@ -56,7 +56,7 @@ methods excel at small problems and problems defined with static arrays. large-scale nonlinear systems of equations. !!! note - + When used with certain types for the states `u` such as a `Number` or `StaticArray`, these solvers are very efficient and non-allocating. These implementations are thus well-suited for small systems of equations. diff --git a/src/raphson.jl b/src/raphson.jl index 52a6054b9..0f1205245 100644 --- a/src/raphson.jl +++ b/src/raphson.jl @@ -49,7 +49,7 @@ for large-scale and numerically-difficult nonlinear systems. Currently, the linear solver and chunk size choice only applies to in-place defined `NonlinearProblem`s. That is expected to change in the future. """ -struct NewtonRaphson{CS, AD, FDT, L, P, ST, CJ, TC<:NLSolveTerminationCondition} <: +struct NewtonRaphson{CS, AD, FDT, L, P, ST, CJ, TC <: NLSolveTerminationCondition} <: AbstractNewtonAlgorithm{CS, AD, FDT, ST, CJ} linsolve::L precs::P @@ -60,10 +60,12 @@ function NewtonRaphson(; chunk_size = Val{0}(), autodiff = Val{true}(), standardtag = Val{true}(), concrete_jac = nothing, diff_type = Val{:forward}, linsolve = nothing, precs = DEFAULT_PRECS, termination_condition = NLSolveTerminationCondition(NLSolveTerminationMode.NLSolveDefault; - abstol=nothing, reltol=nothing)) + abstol = nothing, + reltol = nothing)) NewtonRaphson{_unwrap_val(chunk_size), _unwrap_val(autodiff), diff_type, typeof(linsolve), typeof(precs), _unwrap_val(standardtag), - _unwrap_val(concrete_jac), typeof(termination_condition)}(linsolve, precs, termination_condition) + _unwrap_val(concrete_jac), typeof(termination_condition)}(linsolve, precs, + termination_condition) end mutable struct NewtonRaphsonCache{iip, fType, algType, uType, duType, resType, pType, @@ -89,22 +91,26 @@ mutable struct NewtonRaphsonCache{iip, fType, algType, uType, duType, resType, p termination_condition::TC prob::probType - function NewtonRaphsonCache{iip}(f::fType, alg::algType, u::uType, uprev::uType, fu::resType, + function NewtonRaphsonCache{iip}(f::fType, alg::algType, u::uType, uprev::uType, + fu::resType, p::pType, uf::ufType, linsolve::L, J::jType, du1::duType, jac_config::JC, iter::Int, maxiters::Int, internalnorm::INType, retcode::SciMLBase.ReturnCode.T, abstol::tolType, - reltol::tolType, termination_condition::TC, prob::probType) where { + reltol::tolType, termination_condition::TC, + prob::probType) where { iip, fType, algType, uType, duType, resType, pType, INType, tolType, - probType, ufType, L, jType, TC, JC} + probType, ufType, L, jType, TC, + JC} new{iip, fType, algType, uType, duType, resType, pType, INType, tolType, probType, ufType, L, jType, JC, TC}(f, alg, u, uprev, fu, p, - uf, linsolve, J, du1, jac_config, iter, - maxiters, internalnorm, - retcode, abstol, reltol, termination_condition, prob) + uf, linsolve, J, du1, jac_config, iter, + maxiters, internalnorm, + retcode, abstol, reltol, + termination_condition, prob) end end @@ -144,11 +150,11 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::NewtonRaphson tc = alg.termination_condition ueltype = eltype(uType) abstol = !isnothing(abstol) ? abstol : - (!isnothing(tc.abstol) ? tc.abstol : - real(oneunit(ueltype)) * (eps(real(one(ueltype))))^(4 // 5)) + (!isnothing(tc.abstol) ? tc.abstol : + real(oneunit(ueltype)) * (eps(real(one(ueltype))))^(4 // 5)) reltol = !isnothing(reltol) ? reltol : - (!isnothing(tc.reltol) ? tc.reltol : eps(real(one(ueltype)))^(4 // 5)) + (!isnothing(tc.reltol) ? tc.reltol : eps(real(one(ueltype)))^(4 // 5)) mode = DiffEqBase.get_termination_mode(tc) storage = mode ∈ DiffEqBase.SAFE_TERMINATION_MODES ? Dict() : nothing @@ -164,14 +170,15 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::NewtonRaphson if iip fu = zero(u) f(fu, u, p) - uprev = u + uprev = deepcopy(u) else fu = f(u, p) - uprev = deepcopy(u) + uprev = u end uf, linsolve, J, du1, jac_config = jacobian_caches(alg, f, u, p, Val(iip)) - return NewtonRaphsonCache{iip}(f, alg, u, uprev, fu, p, uf, linsolve, J, du1, jac_config, + return NewtonRaphsonCache{iip}(f, alg, u, uprev, fu, p, uf, linsolve, J, du1, + jac_config, 1, maxiters, internalnorm, ReturnCode.Default, abstol, reltol, termination_condition, prob) @@ -180,9 +187,10 @@ end function perform_step!(cache::NewtonRaphsonCache{true}) @unpack u, fu, f, p, alg = cache @unpack J, linsolve, du1 = cache - jacobian!(J, cache) cache.uprev .= u + jacobian!(J, cache) + # u = u - J \ fu linres = dolinsolve(alg.precs, linsolve, A = J, b = _vec(fu), linu = _vec(du1), p = p, reltol = cache.abstol) @@ -205,7 +213,8 @@ function SciMLBase.solve!(cache::NewtonRaphsonCache) while cache.iter < cache.maxiters perform_step!(cache) cache.iter += 1 - if cache.termination_condition(cache.fu, cache.u, cache.uprev, cache.abstol, cache.reltol) + if cache.termination_condition(cache.fu, cache.u, cache.uprev, cache.abstol, + cache.reltol) break end end