-
-
Couldn't load subscription status.
- Fork 235
Adaptive IDSolve #2881
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
base: master
Are you sure you want to change the base?
Adaptive IDSolve #2881
Changes from 2 commits
d84627c
f02492f
c96581c
e54472a
cd0fbf6
c25238c
e9a58fa
bdfad62
4b597fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,38 +4,38 @@ authors = ["vyudu <[email protected]>"] | |
| version = "1.2.0" | ||
|
|
||
| [deps] | ||
| SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7" | ||
| SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" | ||
| UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" | ||
| DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" | ||
| Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
| NonlinearSolveFirstOrder = "5959db7a-ea39-4486-b5fe-2dd0bf03d60d" | ||
| OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" | ||
| Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
| SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" | ||
| SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" | ||
| UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" | ||
|
|
||
| [extras] | ||
| JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" | ||
| OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" | ||
| Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
| Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" | ||
| AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a" | ||
| [sources] | ||
| OrdinaryDiffEqCore = {path = "../OrdinaryDiffEqCore"} | ||
|
|
||
| [compat] | ||
| Test = "1.10.0" | ||
| AllocCheck = "0.2" | ||
| Aqua = "0.8.11" | ||
| DiffEqBase = "6.176" | ||
| JET = "0.9.18, 0.10.4" | ||
| NonlinearSolveFirstOrder = "1.9.0" | ||
| OrdinaryDiffEqCore = "1.29.0" | ||
| OrdinaryDiffEqSDIRK = "1.6.0" | ||
| Reexport = "1.2" | ||
| SciMLBase = "2.99" | ||
| SimpleNonlinearSolve = "2.7" | ||
| OrdinaryDiffEqCore = "1.29.0" | ||
| Aqua = "0.8.11" | ||
| SymbolicIndexingInterface = "0.3.38" | ||
| julia = "1.10" | ||
| JET = "0.9.18, 0.10.4" | ||
| Test = "1.10.0" | ||
| UnPack = "1.0.2" | ||
| AllocCheck = "0.2" | ||
| DiffEqBase = "6.176" | ||
| Reexport = "1.2" | ||
| julia = "1.10" | ||
|
|
||
| [extras] | ||
| AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a" | ||
| Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" | ||
| JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" | ||
| OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" | ||
| Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
|
||
| [targets] | ||
| test = ["OrdinaryDiffEqSDIRK", "Test", "JET", "Aqua", "AllocCheck"] | ||
|
|
||
| [sources.OrdinaryDiffEqCore] | ||
| path = "../OrdinaryDiffEqCore" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """ | ||
| IDSolve() | ||
|
|
||
| First order solver for `ImplicitDiscreteSystems`. | ||
| """ | ||
| # struct IDSolve{CS, AD, NLS, FDT, ST, CJ} <: | ||
| struct IDSolve{NLS} <: | ||
| OrdinaryDiffEqAlgorithm | ||
| nlsolve::NLS | ||
| extrapolant::Symbol | ||
| controller::Symbol | ||
| end | ||
|
|
||
| function IDSolve(; | ||
| nlsolve = NewtonRaphson(), #NLNewton(), | ||
| extrapolant = :constant, | ||
| controller = :PI, | ||
| ) | ||
|
|
||
| IDSolve{typeof(nlsolve)}(nlsolve, extrapolant, controller) | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,65 @@ | ||
| mutable struct ImplicitDiscreteState{uType, pType, tType} | ||
| struct ImplicitDiscreteState{uType, pType, tType} | ||
| u::uType | ||
| p::pType | ||
| t_next::tType | ||
| t::tType | ||
| end | ||
|
|
||
| mutable struct IDSolveCache{uType} <: OrdinaryDiffEqMutableCache | ||
| mutable struct IDSolveCache{uType, cType} <: OrdinaryDiffEqMutableCache | ||
| u::uType | ||
| uprev::uType | ||
| state::ImplicitDiscreteState | ||
| prob::Union{Nothing, SciMLBase.AbstractNonlinearProblem} | ||
| z::uType | ||
| nlcache::cType | ||
| end | ||
|
|
||
| function alg_cache(alg::IDSolve, u, rate_prototype, ::Type{uEltypeNoUnits}, | ||
| ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, | ||
| dt, reltol, p, calck, | ||
| ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} | ||
| state = ImplicitDiscreteState(isnothing(u) ? nothing : zero(u), p, t) | ||
| IDSolveCache(u, uprev, state, nothing) | ||
| state = ImplicitDiscreteState(zero(u), p, t) | ||
| f_nl = (resid, u_next, p) -> f(resid, u_next, p.u, p.p, p.t) | ||
|
|
||
| u_len = length(u) | ||
| nlls = !isnothing(f.resid_prototype) && (length(f.resid_prototype) != u_len) | ||
| prob = if nlls | ||
| NonlinearLeastSquaresProblem{isinplace(f)}( | ||
| NonlinearFunction(f_nl; resid_prototype = f.resid_prototype), | ||
| u, state) | ||
| else | ||
| NonlinearProblem{isinplace(f)}(f_nl, u, state) | ||
| end | ||
|
|
||
| nlcache = init(prob, alg.nlsolve) | ||
|
|
||
| IDSolveCache(u, uprev, state.u, nlcache) | ||
| end | ||
|
|
||
| isdiscretecache(cache::IDSolveCache) = true | ||
|
|
||
| struct IDSolveConstantCache <: OrdinaryDiffEqConstantCache | ||
| prob::Union{Nothing, SciMLBase.AbstractNonlinearProblem} | ||
| end | ||
| # struct IDSolveConstantCache <: OrdinaryDiffEqConstantCache | ||
| # prob::Union{Nothing, SciMLBase.AbstractNonlinearProblem} | ||
| # end | ||
termi-official marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| function alg_cache(alg::IDSolve, u, rate_prototype, ::Type{uEltypeNoUnits}, | ||
| ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, | ||
| dt, reltol, p, calck, | ||
| ::Val{false}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} | ||
| state = ImplicitDiscreteState(isnothing(u) ? nothing : zero(u), p, t) | ||
| IDSolveCache(u, uprev, state, nothing) | ||
| state = ImplicitDiscreteState(zero(u), p, t) | ||
| f_nl = (u_next, p) -> f(u_next, p.u, p.p, p.t) | ||
|
|
||
| u_len = length(u) | ||
| nlls = !isnothing(f.resid_prototype) && (length(f.resid_prototype) != u_len) | ||
| prob = if nlls | ||
| NonlinearLeastSquaresProblem{isinplace(f)}( | ||
| NonlinearFunction(f_nl; resid_prototype = f.resid_prototype), | ||
| u, state) | ||
| else | ||
| NonlinearProblem{isinplace(f)}(f_nl, u, state) | ||
| end | ||
|
|
||
| nlcache = init(prob, alg.nlsolve) | ||
|
|
||
| # FIXME Use IDSolveConstantCache | ||
| IDSolveCache(u, uprev, state.u, nlcache) | ||
| end | ||
|
|
||
| get_fsalfirstlast(cache::IDSolveCache, rate_prototype) = (nothing, nothing) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,16 +71,16 @@ end | |
| end | ||
| end | ||
|
|
||
| @testset "Handle nothing in u0" begin | ||
| function empty(u_next, u, p, t) | ||
| nothing | ||
| end | ||
| # @testset "Handle nothing in u0" begin | ||
|
||
| # function empty(u_next, u, p, t) | ||
| # nothing | ||
| # end | ||
|
|
||
| tsteps = 5 | ||
| u0 = nothing | ||
| idprob = ImplicitDiscreteProblem(empty, u0, (0, tsteps), []) | ||
| @test_nowarn integ = init(idprob, IDSolve()) | ||
| end | ||
| # tsteps = 5 | ||
| # u0 = nothing | ||
| # idprob = ImplicitDiscreteProblem(empty, u0, (0, tsteps), []) | ||
| # @test_nowarn integ = init(idprob, IDSolve()) | ||
| # end | ||
|
|
||
| @testset "Create NonlinearLeastSquaresProblem" begin | ||
| function over(u_next, u, p, t) | ||
|
|
@@ -92,23 +92,23 @@ end | |
| idprob = ImplicitDiscreteProblem( | ||
| ImplicitDiscreteFunction(over, resid_prototype = zeros(3)), u0, (0, tsteps), []) | ||
| integ = init(idprob, IDSolve()) | ||
| @test integ.cache.prob isa NonlinearLeastSquaresProblem | ||
| @test integ.cache.nlcache.prob isa NonlinearLeastSquaresProblem | ||
|
|
||
| function under(u_next, u, p, t) | ||
| [u_next[1] - u_next[2] - 1] | ||
| end | ||
| idprob = ImplicitDiscreteProblem( | ||
| ImplicitDiscreteFunction(under; resid_prototype = zeros(1)), u0, (0, tsteps), []) | ||
| integ = init(idprob, IDSolve()) | ||
| @test integ.cache.prob isa NonlinearLeastSquaresProblem | ||
| @test integ.cache.nlcache.prob isa NonlinearLeastSquaresProblem | ||
|
|
||
| function full(u_next, u, p, t) | ||
| [u_next[1]^2 - 3, u_next[2] - u[1]] | ||
| end | ||
| idprob = ImplicitDiscreteProblem( | ||
| ImplicitDiscreteFunction(full; resid_prototype = zeros(2)), u0, (0, tsteps), []) | ||
| integ = init(idprob, IDSolve()) | ||
| @test integ.cache.prob isa NonlinearProblem | ||
| @test integ.cache.nlcache.prob isa NonlinearProblem | ||
| end | ||
|
|
||
| @testset "InitialFailure thrown" begin | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reasoning here to include the current
uin the signature of this function, but no information on dt?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dtis just a parameter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess my question is simply, why is$dt$ (or tprev) not a parameter, but $uprev$ is part of the function signature?