Skip to content

Commit

Permalink
fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
oscardssmith committed Mar 13, 2023
1 parent 920cf16 commit b2ed52b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/src/solvers/NonlinearSystemSolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down Expand Up @@ -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.
Expand Down
43 changes: 26 additions & 17 deletions src/raphson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit b2ed52b

Please sign in to comment.