-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
3 changed files
with
169 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,161 @@ | ||
# Here we write out Newton Raphson and test integration with LineSearch.jl. Main tests are | ||
# over at NonlinearSolve.jl and SimpleNonlinearSolve.jl | ||
@testsetup module RootFinding | ||
|
||
using SciMLBase, DifferentiationInterface, ForwardDiff | ||
using SciMLBase: AbstractNonlinearProblem | ||
const DI = DifferentiationInterface | ||
|
||
function newton_raphson(prob::AbstractNonlinearProblem, ls) | ||
if SciMLBase.isinplace(prob) | ||
return newton_raphson_iip(prob, ls) | ||
else | ||
return newton_raphson_oop(prob, ls) | ||
end | ||
end | ||
|
||
function newton_raphson_oop(prob::AbstractNonlinearProblem, ls) | ||
u = copy(prob.u0) | ||
fu = prob.f(u, prob.p) | ||
|
||
ls_cache = init(prob, ls, fu, u) | ||
|
||
alphas = Float64[] | ||
iter = 0 | ||
for _ in 1:100 | ||
iter += 1 | ||
|
||
maximum(abs, fu) < 1e-8 && return true, fu, u, iter, alphas | ||
|
||
J = DI.jacobian(prob.f, AutoForwardDiff(), u, Constant(prob.p)) | ||
δu = -J \ fu | ||
|
||
ls_sol = solve!(ls_cache, u, δu) | ||
|
||
push!(alphas, ls_sol.step_size) | ||
@. u = u + ls_sol.step_size * δu | ||
|
||
fu = prob.f(u, prob.p) | ||
end | ||
|
||
return false, fu, u, iter, alphas | ||
end | ||
|
||
function newton_raphson_iip(prob::AbstractNonlinearProblem, ls) | ||
u = copy(prob.u0) | ||
fu = similar(u) | ||
fu2 = similar(u) | ||
prob.f(fu, u, prob.p) | ||
|
||
ls_cache = init(prob, ls, fu, u) | ||
|
||
alphas = Float64[] | ||
iter = 0 | ||
for _ in 1:100 | ||
iter += 1 | ||
|
||
maximum(abs, fu) < 1e-8 && return true, fu, u, iter, alphas | ||
|
||
J = DI.jacobian(prob.f, fu2, AutoForwardDiff(), u, Constant(prob.p)) | ||
δu = -J \ fu | ||
|
||
ls_sol = solve!(ls_cache, u, δu) | ||
|
||
push!(alphas, ls_sol.step_size) | ||
@. u = u + ls_sol.step_size * δu | ||
|
||
prob.f(fu, u, prob.p) | ||
end | ||
|
||
return false, fu, u, iter, alphas | ||
end | ||
|
||
export newton_raphson | ||
|
||
end | ||
|
||
@testitem "LineSearches.jl: Newton Raphson" setup=[RootFinding] begin | ||
using LineSearches, SciMLBase | ||
using ADTypes, Tracker, ForwardDiff, Zygote, Enzyme, ReverseDiff, FiniteDiff | ||
|
||
@testset "OOP Problem" begin | ||
nlf(x, p) = x .^ 2 .- p | ||
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0]) | ||
|
||
@testset for autodiff in ( | ||
AutoTracker(), AutoForwardDiff(), AutoZygote(), | ||
AutoEnzyme(), AutoReverseDiff(), AutoFiniteDiff() | ||
) | ||
@testset "method: $(nameof(typeof(method)))" for method in ( | ||
BackTracking(; order = 3), | ||
StrongWolfe(), | ||
HagerZhang(), | ||
MoreThuente(), | ||
Static() | ||
) | ||
linesearch = LineSearchesJL(; method, autodiff) | ||
converged, fu, u, iter, alphas = newton_raphson(nlp, linesearch) | ||
|
||
@test fu≈[0.0, 0.0] atol=1e-3 | ||
@test abs.(u)≈sqrt.([3.0, 3.0]) atol=1e-3 | ||
end | ||
end | ||
end | ||
|
||
@testset "In-Place Problem" begin | ||
nlf(dx, x, p) = (dx .= x .^ 2 .- p) | ||
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0]) | ||
|
||
@testset for autodiff in ( | ||
AutoForwardDiff(), AutoEnzyme(), AutoReverseDiff(), AutoFiniteDiff() | ||
) | ||
@testset "method: $(nameof(typeof(method)))" for method in ( | ||
BackTracking(; order = 3), | ||
StrongWolfe(), | ||
HagerZhang(), | ||
MoreThuente(), | ||
Static() | ||
) | ||
linesearch = LineSearchesJL(; method, autodiff) | ||
converged, fu, u, iter, alphas = newton_raphson(nlp, linesearch) | ||
|
||
@test fu≈[0.0, 0.0] atol=1e-3 | ||
@test abs.(u)≈sqrt.([3.0, 3.0]) atol=1e-3 | ||
end | ||
end | ||
end | ||
end | ||
|
||
@testitem "Native Line Search: Newton Raphson" setup=[RootFinding] begin | ||
using LineSearches, SciMLBase | ||
|
||
@testset "OOP Problem" begin | ||
nlf(x, p) = x .^ 2 .- p | ||
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0]) | ||
|
||
@testset "method: $(nameof(typeof(method)))" for method in ( | ||
LiFukushimaLineSearch(), | ||
NoLineSearch(0.5) | ||
) | ||
converged, fu, u, iter, alphas = newton_raphson(nlp, method) | ||
|
||
@test fu≈[0.0, 0.0] atol=1e-1 | ||
@test abs.(u)≈sqrt.([3.0, 3.0]) atol=1e-1 | ||
end | ||
end | ||
|
||
@testset "In-Place Problem" begin | ||
nlf(dx, x, p) = (dx .= x .^ 2 .- p) | ||
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0]) | ||
|
||
@testset "method: $(nameof(typeof(method)))" for method in ( | ||
LiFukushimaLineSearch(), | ||
NoLineSearch(0.5) | ||
) | ||
converged, fu, u, iter, alphas = newton_raphson(nlp, method) | ||
|
||
@test fu≈[0.0, 0.0] atol=1e-1 | ||
@test abs.(u)≈sqrt.([3.0, 3.0]) atol=1e-1 | ||
end | ||
end | ||
end |
b4b012c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
b4b012c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/116359
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: