-
-
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.
refactor: Move dual nonlinear solving to NonlinearSolveBase (#513)
- Loading branch information
Showing
15 changed files
with
294 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@concrete mutable struct NonlinearSolveForwardDiffCache <: AbstractNonlinearSolveCache | ||
cache | ||
prob | ||
alg | ||
p | ||
values_p | ||
partials_p | ||
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
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,34 @@ | ||
const DualNonlinearProblem = NonlinearProblem{ | ||
<:Union{Number, <:AbstractArray}, iip, | ||
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}} | ||
} where {iip, T, V, P} | ||
const DualNonlinearLeastSquaresProblem = NonlinearLeastSquaresProblem{ | ||
<:Union{Number, <:AbstractArray}, iip, | ||
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}} | ||
} where {iip, T, V, P} | ||
const DualAbstractNonlinearProblem = Union{ | ||
DualNonlinearProblem, DualNonlinearLeastSquaresProblem | ||
} | ||
|
||
function SciMLBase.__init( | ||
prob::DualAbstractNonlinearProblem, alg::GeneralizedFirstOrderAlgorithm, args...; kwargs... | ||
) | ||
p = NonlinearSolveBase.nodual_value(prob.p) | ||
newprob = SciMLBase.remake(prob; u0 = NonlinearSolveBase.nodual_value(prob.u0), p) | ||
cache = init(newprob, alg, args...; kwargs...) | ||
return NonlinearSolveForwardDiffCache( | ||
cache, newprob, alg, prob.p, p, ForwardDiff.partials(prob.p) | ||
) | ||
end | ||
|
||
function SciMLBase.__solve( | ||
prob::DualAbstractNonlinearProblem, alg::GeneralizedFirstOrderAlgorithm, args...; kwargs... | ||
) | ||
sol, partials = NonlinearSolveBase.nonlinearsolve_forwarddiff_solve( | ||
prob, alg, args...; kwargs... | ||
) | ||
dual_soln = NonlinearSolveBase.nonlinearsolve_dual_solution(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution( | ||
prob, alg, dual_soln, sol.resid; sol.retcode, sol.stats, sol.original | ||
) | ||
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
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
46 changes: 46 additions & 0 deletions
46
lib/NonlinearSolveQuasiNewton/ext/NonlinearSolveQuasiNewtonForwardDiffExt.jl
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,46 @@ | ||
module NonlinearSolveQuasiNewtonForwardDiffExt | ||
|
||
using CommonSolve: CommonSolve, init | ||
using ForwardDiff: ForwardDiff, Dual | ||
using SciMLBase: SciMLBase, NonlinearProblem, NonlinearLeastSquaresProblem | ||
|
||
using NonlinearSolveBase: NonlinearSolveBase, NonlinearSolveForwardDiffCache, nodual_value | ||
|
||
using NonlinearSolveQuasiNewton: QuasiNewtonAlgorithm | ||
|
||
const DualNonlinearProblem = NonlinearProblem{ | ||
<:Union{Number, <:AbstractArray}, iip, | ||
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}} | ||
} where {iip, T, V, P} | ||
const DualNonlinearLeastSquaresProblem = NonlinearLeastSquaresProblem{ | ||
<:Union{Number, <:AbstractArray}, iip, | ||
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}} | ||
} where {iip, T, V, P} | ||
const DualAbstractNonlinearProblem = Union{ | ||
DualNonlinearProblem, DualNonlinearLeastSquaresProblem | ||
} | ||
|
||
function SciMLBase.__solve( | ||
prob::DualAbstractNonlinearProblem, alg::QuasiNewtonAlgorithm, args...; kwargs... | ||
) | ||
sol, partials = NonlinearSolveBase.nonlinearsolve_forwarddiff_solve( | ||
prob, alg, args...; kwargs... | ||
) | ||
dual_soln = NonlinearSolveBase.nonlinearsolve_dual_solution(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution( | ||
prob, alg, dual_soln, sol.resid; sol.retcode, sol.stats, sol.original | ||
) | ||
end | ||
|
||
function SciMLBase.__init( | ||
prob::DualAbstractNonlinearProblem, alg::QuasiNewtonAlgorithm, args...; kwargs... | ||
) | ||
p = nodual_value(prob.p) | ||
newprob = SciMLBase.remake(prob; u0 = nodual_value(prob.u0), p) | ||
cache = init(newprob, alg, args...; kwargs...) | ||
return NonlinearSolveForwardDiffCache( | ||
cache, newprob, alg, prob.p, p, ForwardDiff.partials(prob.p) | ||
) | ||
end | ||
|
||
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
46 changes: 46 additions & 0 deletions
46
lib/NonlinearSolveSpectralMethods/ext/NonlinearSolveSpectralMethodsForwardDiffExt.jl
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,46 @@ | ||
module NonlinearSolveSpectralMethodsForwardDiffExt | ||
|
||
using CommonSolve: CommonSolve, init | ||
using ForwardDiff: ForwardDiff, Dual | ||
using SciMLBase: SciMLBase, NonlinearProblem, NonlinearLeastSquaresProblem | ||
|
||
using NonlinearSolveBase: NonlinearSolveBase, NonlinearSolveForwardDiffCache, nodual_value | ||
|
||
using NonlinearSolveSpectralMethods: GeneralizedDFSane | ||
|
||
const DualNonlinearProblem = NonlinearProblem{ | ||
<:Union{Number, <:AbstractArray}, iip, | ||
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}} | ||
} where {iip, T, V, P} | ||
const DualNonlinearLeastSquaresProblem = NonlinearLeastSquaresProblem{ | ||
<:Union{Number, <:AbstractArray}, iip, | ||
<:Union{<:Dual{T, V, P}, <:AbstractArray{<:Dual{T, V, P}}} | ||
} where {iip, T, V, P} | ||
const DualAbstractNonlinearProblem = Union{ | ||
DualNonlinearProblem, DualNonlinearLeastSquaresProblem | ||
} | ||
|
||
function SciMLBase.__solve( | ||
prob::DualAbstractNonlinearProblem, alg::GeneralizedDFSane, args...; kwargs... | ||
) | ||
sol, partials = NonlinearSolveBase.nonlinearsolve_forwarddiff_solve( | ||
prob, alg, args...; kwargs... | ||
) | ||
dual_soln = NonlinearSolveBase.nonlinearsolve_dual_solution(sol.u, partials, prob.p) | ||
return SciMLBase.build_solution( | ||
prob, alg, dual_soln, sol.resid; sol.retcode, sol.stats, sol.original | ||
) | ||
end | ||
|
||
function SciMLBase.__init( | ||
prob::DualAbstractNonlinearProblem, alg::GeneralizedDFSane, args...; kwargs... | ||
) | ||
p = nodual_value(prob.p) | ||
newprob = SciMLBase.remake(prob; u0 = nodual_value(prob.u0), p) | ||
cache = init(newprob, alg, args...; kwargs...) | ||
return NonlinearSolveForwardDiffCache( | ||
cache, newprob, alg, prob.p, p, ForwardDiff.partials(prob.p) | ||
) | ||
end | ||
|
||
end |
Oops, something went wrong.