-
-
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.
Put DualAbstractNonlinearProblem solving in subpackages
- Loading branch information
Showing
11 changed files
with
205 additions
and
24 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,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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
module NonlinearSolveQuasiNewtonForwardDiffExt | ||
|
||
using CommonSolve: CommonSolve, solve | ||
using ForwardDiff: ForwardDiff, Dual | ||
using SciMLBase: SciMLBase, AbstractNonlinearProblem, IntervalNonlinearProblem, | ||
NonlinearProblem, NonlinearLeastSquaresProblem, remake | ||
|
||
using NonlinearSolveBase: NonlinearSolveBase | ||
|
||
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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
module NonlinearSolveSpectralMethodsForwardDiffExt | ||
|
||
using CommonSolve: CommonSolve, solve | ||
using ForwardDiff: ForwardDiff, Dual | ||
using SciMLBase: SciMLBase, AbstractNonlinearProblem, IntervalNonlinearProblem, | ||
NonlinearProblem, NonlinearLeastSquaresProblem, remake | ||
|
||
using NonlinearSolveBase: NonlinearSolveBase | ||
|
||
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 |
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,44 @@ | ||
const EXTENSION_SOLVER_TYPES = [ | ||
LeastSquaresOptimJL, FastLevenbergMarquardtJL, NLsolveJL, NLSolversJL, | ||
SpeedMappingJL, FixedPointAccelerationJL, SIAMFANLEquationsJL, | ||
CMINPACK, PETScSNES | ||
] | ||
|
||
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 | ||
} | ||
|
||
for algType in EXTENSION_SOLVER_TYPES | ||
@eval function SciMLBase.__init( | ||
prob::DualAbstractNonlinearProblem, alg::$(algType), 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 | ||
end | ||
|
||
for algType in EXTENSION_SOLVER_TYPES | ||
@eval function SciMLBase.__solve( | ||
prob::DualAbstractNonlinearProblem, alg::$(algType), 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 | ||
end |