-
-
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.
Add caching for solvers without init
- Loading branch information
Showing
9 changed files
with
93 additions
and
20 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
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,37 @@ | ||
# Some algorithms don't support creating a cache and doing `solve!`, this unfortunately | ||
# makes it difficult to write generic code that supports caching. For the algorithms that | ||
# don't have a `__init` function defined, we create a "Fake Cache", which just calls | ||
# `__solve` from `solve!` | ||
@concrete mutable struct NonlinearSolveNoInitCache{iip, timeit} <: | ||
AbstractNonlinearSolveCache{iip, timeit} | ||
prob | ||
alg | ||
args | ||
kwargs::Any | ||
end | ||
|
||
function SciMLBase.reinit!( | ||
cache::NonlinearSolveNoInitCache, u0 = cache.prob.u0; p = cache.prob.p, kwargs...) | ||
prob = remake(cache.prob; u0, p) | ||
cache.prob = prob | ||
cache.kwargs = merge(cache.kwargs, kwargs) | ||
return cache | ||
end | ||
|
||
function Base.show(io::IO, cache::NonlinearSolveNoInitCache) | ||
print(io, "NonlinearSolveNoInitCache(alg = $(cache.alg))") | ||
end | ||
|
||
function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip}, | ||
alg::Union{AbstractNonlinearSolveAlgorithm, | ||
SimpleNonlinearSolve.AbstractSimpleNonlinearSolveAlgorithm}, | ||
args...; | ||
maxtime = nothing, | ||
kwargs...) where {uType, iip} | ||
return NonlinearSolveNoInitCache{iip, maxtime !== nothing}( | ||
prob, alg, args, merge((; maxtime), kwargs)) | ||
end | ||
|
||
function SciMLBase.solve!(cache::NonlinearSolveNoInitCache) | ||
return solve(cache.prob, cache.alg, cache.args...; cache.kwargs...) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@testitem "NoInit Caching" begin | ||
using LinearAlgebra | ||
import NLsolve, NLSolvers | ||
|
||
solvers = [SimpleNewtonRaphson(), SimpleTrustRegion(), SimpleDFSane(), NLsolveJL(), | ||
NLSolversJL(NLSolvers.LineSearch(NLSolvers.Newton(), NLSolvers.Backtracking()))] | ||
|
||
prob = NonlinearProblem((u, p) -> u .^ 2 .- p, [0.1, 0.3], 2.0) | ||
|
||
for alg in solvers | ||
cache = init(prob, alg) | ||
sol = solve!(cache) | ||
@test SciMLBase.successful_retcode(sol) | ||
@test norm(sol.resid, Inf) ≤ 1e-6 | ||
|
||
reinit!(cache; p = 5.0) | ||
@test cache.prob.p == 5.0 | ||
sol = solve!(cache) | ||
@test SciMLBase.successful_retcode(sol) | ||
@test norm(sol.resid, Inf) ≤ 1e-6 | ||
@test norm(sol.u .^ 2 .- 5.0, Inf) ≤ 1e-6 | ||
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