|
| 1 | +# Some algorithms don't support creating a cache and doing `solve!`, this unfortunately |
| 2 | +# makes it difficult to write generic code that supports caching. For the algorithms that |
| 3 | +# don't have a `__init` function defined, we create a "Fake Cache", which just calls |
| 4 | +# `__solve` from `solve!` |
| 5 | +@concrete mutable struct NonlinearSolveNoInitCache{iip, timeit} <: |
| 6 | + AbstractNonlinearSolveCache{iip, timeit} |
| 7 | + prob |
| 8 | + alg |
| 9 | + args |
| 10 | + kwargs::Any |
| 11 | +end |
| 12 | + |
| 13 | +function SciMLBase.reinit!( |
| 14 | + cache::NonlinearSolveNoInitCache, u0 = cache.prob.u0; p = cache.prob.p, kwargs...) |
| 15 | + prob = remake(cache.prob; u0, p) |
| 16 | + cache.prob = prob |
| 17 | + cache.kwargs = merge(cache.kwargs, kwargs) |
| 18 | + return cache |
| 19 | +end |
| 20 | + |
| 21 | +function Base.show(io::IO, cache::NonlinearSolveNoInitCache) |
| 22 | + print(io, "NonlinearSolveNoInitCache(alg = $(cache.alg))") |
| 23 | +end |
| 24 | + |
| 25 | +function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip}, |
| 26 | + alg::Union{AbstractNonlinearSolveAlgorithm, |
| 27 | + SimpleNonlinearSolve.AbstractSimpleNonlinearSolveAlgorithm}, |
| 28 | + args...; |
| 29 | + maxtime = nothing, |
| 30 | + kwargs...) where {uType, iip} |
| 31 | + return NonlinearSolveNoInitCache{iip, maxtime !== nothing}( |
| 32 | + prob, alg, args, merge((; maxtime), kwargs)) |
| 33 | +end |
| 34 | + |
| 35 | +function SciMLBase.solve!(cache::NonlinearSolveNoInitCache) |
| 36 | + return solve(cache.prob, cache.alg, cache.args...; cache.kwargs...) |
| 37 | +end |
0 commit comments