-
-
Notifications
You must be signed in to change notification settings - Fork 49
Towards a cleaner and more maintainable internals of NonlinearSolve.jl #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8869f80
Towards a cleaner and more maintainable internals of NonlinearSolve.jl
avik-pal 4fe75e8
Incorporate upstream changes in NonlinearSolve.jl
avik-pal e87a82d
Patch broken solvers + better testing
avik-pal 5963ec9
Finish TrustRegion tests
avik-pal eb3a6ff
Finalize tests
avik-pal 78beabe
Formatting
avik-pal dd66ce1
Make it a breaking change: autodiff args have different semantics
avik-pal 47135d4
Update docs compat
avik-pal 5e7bafc
Fix forward AD
avik-pal a9fc4b8
Non allocating for scalars
avik-pal 85f7449
Non allocating for static vectors
avik-pal f7e29aa
Ignore SVector for 1.6
avik-pal 6307028
Bump compat entries
avik-pal 5b46c2d
Fix jac prototype
avik-pal de8086c
Fix JacVec for not inplace problems
avik-pal 7e26d18
Add support for line search in Newton Raphson
avik-pal 83c0723
auto switch to finitediff for inplace problems
avik-pal 0b3eaa1
Drop 1.6 and require DiffEqBase 6.130
avik-pal 4cd2d97
Remove 1.6 downstream
avik-pal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 +1,3 @@ | ||
style = "sciml" | ||
format_markdown = true | ||
format_markdown = true | ||
annotate_untyped_fields_with_any = false |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -25,3 +25,4 @@ Manifest.toml | |
docs/src/assets/Project.toml | ||
|
||
.vscode | ||
wip |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,44 +1,63 @@ | ||
function scalar_nlsolve_ad(prob, alg, args...; kwargs...) | ||
f = prob.f | ||
p = value(prob.p) | ||
|
||
u0 = value(prob.u0) | ||
newprob = NonlinearProblem(f, u0, p; prob.kwargs...) | ||
|
||
sol = solve(newprob, alg, args...; kwargs...) | ||
|
||
uu = sol.u | ||
if p isa Number | ||
f_p = ForwardDiff.derivative(Base.Fix1(f, uu), p) | ||
else | ||
f_p = ForwardDiff.gradient(Base.Fix1(f, uu), p) | ||
end | ||
f_p = scalar_nlsolve_∂f_∂p(f, uu, p) | ||
f_x = scalar_nlsolve_∂f_∂u(f, uu, p) | ||
|
||
z_arr = -inv(f_x) * f_p | ||
|
||
f_x = ForwardDiff.derivative(Base.Fix2(f, p), uu) | ||
pp = prob.p | ||
sumfun = let f_x′ = -f_x | ||
((fp, p),) -> (fp / f_x′) * ForwardDiff.partials(p) | ||
sumfun = ((z, p),) -> map(zᵢ -> zᵢ * ForwardDiff.partials(p), z) | ||
if uu isa Number | ||
partials = sum(sumfun, zip(z_arr, pp)) | ||
elseif p isa Number | ||
partials = sumfun((z_arr, pp)) | ||
else | ||
partials = sum(sumfun, zip(eachcol(z_arr), pp)) | ||
end | ||
partials = sum(sumfun, zip(f_p, pp)) | ||
|
||
return sol, partials | ||
end | ||
|
||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, StaticArraysCore.SVector}, | ||
iip, | ||
<:Dual{T, V, P}}, | ||
alg::AbstractNewtonAlgorithm, | ||
args...; kwargs...) where {iip, T, V, P} | ||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, SVector, <:AbstractArray}, | ||
iip, <:Dual{T, V, P}}, alg::AbstractNewtonAlgorithm, args...; | ||
kwargs...) where {iip, T, V, P} | ||
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...) | ||
return SciMLBase.build_solution(prob, alg, Dual{T, V, P}(sol.u, partials), sol.resid; | ||
retcode = sol.retcode) | ||
dual_soln = scalar_nlsolve_dual_soln(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution(prob, alg, dual_soln, sol.resid; sol.retcode) | ||
end | ||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, StaticArraysCore.SVector}, | ||
iip, | ||
<:AbstractArray{<:Dual{T, V, P}}}, | ||
alg::AbstractNewtonAlgorithm, | ||
args...; | ||
|
||
function SciMLBase.solve(prob::NonlinearProblem{<:Union{Number, SVector, <:AbstractArray}, | ||
iip, <:AbstractArray{<:Dual{T, V, P}}}, alg::AbstractNewtonAlgorithm, args...; | ||
kwargs...) where {iip, T, V, P} | ||
sol, partials = scalar_nlsolve_ad(prob, alg, args...; kwargs...) | ||
return SciMLBase.build_solution(prob, alg, Dual{T, V, P}(sol.u, partials), sol.resid; | ||
retcode = sol.retcode) | ||
dual_soln = scalar_nlsolve_dual_soln(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution(prob, alg, dual_soln, sol.resid; sol.retcode) | ||
end | ||
|
||
function scalar_nlsolve_∂f_∂p(f, u, p) | ||
ff = p isa Number ? ForwardDiff.derivative : | ||
(u isa Number ? ForwardDiff.gradient : ForwardDiff.jacobian) | ||
return ff(Base.Fix1(f, u), p) | ||
end | ||
|
||
function scalar_nlsolve_∂f_∂u(f, u, p) | ||
ff = u isa Number ? ForwardDiff.derivative : ForwardDiff.jacobian | ||
return ff(Base.Fix2(f, p), u) | ||
end | ||
|
||
function scalar_nlsolve_dual_soln(u::Number, partials, | ||
::Union{<:AbstractArray{<:Dual{T, V, P}}, Dual{T, V, P}}) where {T, V, P} | ||
return Dual{T, V, P}(u, partials) | ||
end | ||
|
||
function scalar_nlsolve_dual_soln(u::AbstractArray, partials, | ||
::Union{<:AbstractArray{<:Dual{T, V, P}}, Dual{T, V, P}}) where {T, V, P} | ||
return map(((uᵢ, pᵢ),) -> Dual{T, V, P}(uᵢ, pᵢ), zip(u, partials)) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.