Skip to content

Commit

Permalink
implement get_tmp_cache (#113)
Browse files Browse the repository at this point in the history
* implement et_tmp_cache

* format

* fix typo

Co-authored-by: Joshua Lampert <[email protected]>

* other plotting command for sum

---------

Co-authored-by: Joshua Lampert <[email protected]>
  • Loading branch information
ranocha and JoshuaLampert authored Jul 28, 2024
1 parent 6845b50 commit 38d69d8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PositiveIntegrators"
uuid = "d1b20bf0-b083-4985-a874-dc5121669aa5"
authors = ["Stefan Kopecz, Hendrik Ranocha, and contributors"]
version = "0.2.0"
version = "0.2.1"

[deps]
FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Finally, we can use [Plots.jl](https://docs.juliaplots.org/stable/) to visualize
using Plots
plot(sol, label = ["S" "I" "R"], legend=:right)
plot!(sol, idxs = ((t, S, I, R) -> (t, S + I + R), 0, 1, 2, 3), label = "S+I+R") #Plot S+I+R over time.
plot!(sol.t, sum.(sol.u), label = "S+I+R") # Plot S+I+R over time.
```
We see that there is always a nonnegative number of people in each compartment, while the population ``S+I+R`` remains constant over time.

Expand Down
3 changes: 2 additions & 1 deletion src/PositiveIntegrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ using OrdinaryDiffEq: @cache,
recursivefill!, _vec, wrapprecs, dolinsolve
import OrdinaryDiffEq: alg_order, isfsal,
calculate_residuals, calculate_residuals!,
alg_cache, initialize!, perform_step!,
alg_cache, get_tmp_cache,
initialize!, perform_step!,
_ode_interpolant, _ode_interpolant!

# 2. Export functionality defining the public API
Expand Down
9 changes: 9 additions & 0 deletions src/mprk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ struct MPEConservativeCache{PType, uType, tabType, F} <: OrdinaryDiffEqMutableCa
linsolve::F
end

get_tmp_cache(integrator, ::MPE, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)

# In-place
function alg_cache(alg::MPE, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
Expand Down Expand Up @@ -638,6 +640,8 @@ struct MPRK22ConservativeCache{uType, PType, tabType, F} <:
linsolve::F
end

get_tmp_cache(integrator, ::MPRK22, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)

# In-place
function alg_cache(alg::MPRK22, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
Expand Down Expand Up @@ -1218,6 +1222,11 @@ struct MPRK43ConservativeCache{uType, PType, tabType, F} <: OrdinaryDiffEqMutabl
linsolve::F
end

function get_tmp_cache(integrator, ::Union{MPRK43I, MPRK43II},
cache::OrdinaryDiffEqMutableCache)
(cache.σ,)
end

# In-place
function alg_cache(alg::Union{MPRK43I, MPRK43II}, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
Expand Down
4 changes: 4 additions & 0 deletions src/sspmprk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ struct SSPMPRK22ConservativeCache{uType, PType, tabType, F} <:
linsolve::F
end

get_tmp_cache(integrator, ::SSPMPRK22, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)

# In-place
function alg_cache(alg::SSPMPRK22, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
Expand Down Expand Up @@ -734,6 +736,8 @@ struct SSPMPRK43ConservativeCache{uType, PType, tabType, F} <: OrdinaryDiffEqMut
linsolve::F
end

get_tmp_cache(integrator, ::SSPMPRK43, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)

# In-place
function alg_cache(alg::SSPMPRK43, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
Expand Down
18 changes: 15 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ end
end
end

# Here we check that the type of p_prototype actually
# Here we check that the type of p_prototype actually
# defines the types of the Ps inside the algorithm caches.
# We test sparse, tridiagonal, and dense matrices.
@testset "Prototype type check" begin
Expand Down Expand Up @@ -1246,7 +1246,7 @@ end
p_prototype = P_dense)
prob_sparse = ConservativePDSProblem(prod_sparse!, u0, tspan;
p_prototype = P_sparse)
## nonconservative PDS
## nonconservative PDS
prob_default2 = PDSProblem(prod_dense!, dest!, u0, tspan)
prob_tridiagonal2 = PDSProblem(prod_tridiagonal!, dest!, u0, tspan;
p_prototype = P_tridiagonal)
Expand All @@ -1262,7 +1262,19 @@ end
for prob in (prob_default, prob_tridiagonal, prob_dense, prob_sparse,
prob_default2,
prob_tridiagonal2, prob_dense2, prob_sparse2)
solve(prob, alg; dt, adaptive = false)
sol1 = solve(prob, alg; dt, adaptive = false)

# test get_tmp_cache and integrator interface - modifying
# values from the cache should not change the final results
integrator = init(prob, alg; dt, adaptive = false)
step!(integrator)
cache = @inferred get_tmp_cache(integrator)
@test !isempty(cache)
tmp = first(cache)
fill!(tmp, NaN)
sol2 = solve!(integrator)
@test sol1.t sol2.t
@test sol1.u sol2.u
end
end
end
Expand Down

2 comments on commit 38d69d8

@ranocha
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111961

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 38d69d869752b999ad10fae11852053f942519d5
git push origin v0.2.1

Please sign in to comment.