-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
SimpleNewtonRaphson(autodiff) | ||
SimpleNewtonRaphson(; autodiff = nothing) | ||
A low-overhead implementation of Newton-Raphson. This method is non-allocating on scalar | ||
and static array problems. | ||
!!! note | ||
As part of the decreased overhead, this method omits some of the higher level error | ||
catching of the other methods. Thus, to see better error messages, use one of the other | ||
methods like `NewtonRaphson`. | ||
### Keyword Arguments | ||
- `autodiff`: determines the backend used for the Jacobian. Defaults to `nothing` (i.e. | ||
automatic backend selection). Valid choices include jacobian backends from | ||
`DifferentiationInterface.jl`. | ||
""" | ||
@kwdef @concrete struct SimpleNewtonRaphson <: AbstractSimpleNonlinearSolveAlgorithm | ||
autodiff = nothing | ||
end | ||
|
||
const SimpleGaussNewton = SimpleNewtonRaphson | ||
|
||
function SciMLBase.__solve( | ||
prob::ImmutableNonlinearProblem, alg::SimpleNewtonRaphson, args...; | ||
abstol = nothing, reltol = nothing, maxiters = 1000, | ||
alias_u0 = false, termination_condition = nothing, kwargs...) | ||
x = Utils.maybe_unaliased(prob.u0, alias_u0) | ||
fx = Utils.get_fx(prob, x) | ||
fx = Utils.eval_f(prob, fx, x) | ||
|
||
iszero(fx) && | ||
return SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.Success) | ||
|
||
abstol, reltol, tc_cache = NonlinearSolveBase.init_termination_cache( | ||
prob, abstol, reltol, fx, x, termination_condition, Val(:simple)) | ||
|
||
autodiff = SciMLBase.has_jac(prob.f) ? alg.autodiff : | ||
NonlinearSolveBase.select_jacobian_autodiff(prob, alg.autodiff) | ||
|
||
@bb xo = similar(x) | ||
fx_cache = (SciMLBase.isinplace(prob) && !SciMLBase.has_jac(prob.f)) ? similar(fx) : | ||
nothing | ||
jac_cache = Utils.prepare_jacobian(prob, autodiff, fx_cache, x) | ||
J = Utils.compute_jacobian!!(nothing, prob, autodiff, fx_cache, x, jac_cache) | ||
|
||
for _ in 1:maxiters | ||
@bb copyto!(xo, x) | ||
δx = Utils.restructure(x, J \ Utils.safe_vec(fx)) | ||
@bb x .-= δx | ||
|
||
solved, retcode, fx_sol, x_sol = Utils.check_termination(tc_cache, fx, x, xo, prob) | ||
solved && return SciMLBase.build_solution(prob, alg, x_sol, fx_sol; retcode) | ||
|
||
fx = Utils.eval_f(prob, fx, x) | ||
J = Utils.compute_jacobian!!(J, prob, autodiff, fx_cache, x, jac_cache) | ||
end | ||
|
||
return SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.MaxIters) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters