Skip to content

Commit 38d69d8

Browse files
implement get_tmp_cache (#113)
* implement et_tmp_cache * format * fix typo Co-authored-by: Joshua Lampert <51029046+JoshuaLampert@users.noreply.github.com> * other plotting command for sum --------- Co-authored-by: Joshua Lampert <51029046+JoshuaLampert@users.noreply.github.com>
1 parent 6845b50 commit 38d69d8

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PositiveIntegrators"
22
uuid = "d1b20bf0-b083-4985-a874-dc5121669aa5"
33
authors = ["Stefan Kopecz, Hendrik Ranocha, and contributors"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Finally, we can use [Plots.jl](https://docs.juliaplots.org/stable/) to visualize
174174
using Plots
175175
176176
plot(sol, label = ["S" "I" "R"], legend=:right)
177-
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.
177+
plot!(sol.t, sum.(sol.u), label = "S+I+R") # Plot S+I+R over time.
178178
```
179179
We see that there is always a nonnegative number of people in each compartment, while the population ``S+I+R`` remains constant over time.
180180

src/PositiveIntegrators.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ using OrdinaryDiffEq: @cache,
4040
recursivefill!, _vec, wrapprecs, dolinsolve
4141
import OrdinaryDiffEq: alg_order, isfsal,
4242
calculate_residuals, calculate_residuals!,
43-
alg_cache, initialize!, perform_step!,
43+
alg_cache, get_tmp_cache,
44+
initialize!, perform_step!,
4445
_ode_interpolant, _ode_interpolant!
4546

4647
# 2. Export functionality defining the public API

src/mprk.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ struct MPEConservativeCache{PType, uType, tabType, F} <: OrdinaryDiffEqMutableCa
337337
linsolve::F
338338
end
339339

340+
get_tmp_cache(integrator, ::MPE, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)
341+
340342
# In-place
341343
function alg_cache(alg::MPE, u, rate_prototype, ::Type{uEltypeNoUnits},
342344
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
@@ -638,6 +640,8 @@ struct MPRK22ConservativeCache{uType, PType, tabType, F} <:
638640
linsolve::F
639641
end
640642

643+
get_tmp_cache(integrator, ::MPRK22, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)
644+
641645
# In-place
642646
function alg_cache(alg::MPRK22, u, rate_prototype, ::Type{uEltypeNoUnits},
643647
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
@@ -1218,6 +1222,11 @@ struct MPRK43ConservativeCache{uType, PType, tabType, F} <: OrdinaryDiffEqMutabl
12181222
linsolve::F
12191223
end
12201224

1225+
function get_tmp_cache(integrator, ::Union{MPRK43I, MPRK43II},
1226+
cache::OrdinaryDiffEqMutableCache)
1227+
(cache.σ,)
1228+
end
1229+
12211230
# In-place
12221231
function alg_cache(alg::Union{MPRK43I, MPRK43II}, u, rate_prototype, ::Type{uEltypeNoUnits},
12231232
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},

src/sspmprk.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ struct SSPMPRK22ConservativeCache{uType, PType, tabType, F} <:
223223
linsolve::F
224224
end
225225

226+
get_tmp_cache(integrator, ::SSPMPRK22, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)
227+
226228
# In-place
227229
function alg_cache(alg::SSPMPRK22, u, rate_prototype, ::Type{uEltypeNoUnits},
228230
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
@@ -734,6 +736,8 @@ struct SSPMPRK43ConservativeCache{uType, PType, tabType, F} <: OrdinaryDiffEqMut
734736
linsolve::F
735737
end
736738

739+
get_tmp_cache(integrator, ::SSPMPRK43, cache::OrdinaryDiffEqMutableCache) = (cache.σ,)
740+
737741
# In-place
738742
function alg_cache(alg::SSPMPRK43, u, rate_prototype, ::Type{uEltypeNoUnits},
739743
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},

test/runtests.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ end
11981198
end
11991199
end
12001200

1201-
# Here we check that the type of p_prototype actually
1201+
# Here we check that the type of p_prototype actually
12021202
# defines the types of the Ps inside the algorithm caches.
12031203
# We test sparse, tridiagonal, and dense matrices.
12041204
@testset "Prototype type check" begin
@@ -1246,7 +1246,7 @@ end
12461246
p_prototype = P_dense)
12471247
prob_sparse = ConservativePDSProblem(prod_sparse!, u0, tspan;
12481248
p_prototype = P_sparse)
1249-
## nonconservative PDS
1249+
## nonconservative PDS
12501250
prob_default2 = PDSProblem(prod_dense!, dest!, u0, tspan)
12511251
prob_tridiagonal2 = PDSProblem(prod_tridiagonal!, dest!, u0, tspan;
12521252
p_prototype = P_tridiagonal)
@@ -1262,7 +1262,19 @@ end
12621262
for prob in (prob_default, prob_tridiagonal, prob_dense, prob_sparse,
12631263
prob_default2,
12641264
prob_tridiagonal2, prob_dense2, prob_sparse2)
1265-
solve(prob, alg; dt, adaptive = false)
1265+
sol1 = solve(prob, alg; dt, adaptive = false)
1266+
1267+
# test get_tmp_cache and integrator interface - modifying
1268+
# values from the cache should not change the final results
1269+
integrator = init(prob, alg; dt, adaptive = false)
1270+
step!(integrator)
1271+
cache = @inferred get_tmp_cache(integrator)
1272+
@test !isempty(cache)
1273+
tmp = first(cache)
1274+
fill!(tmp, NaN)
1275+
sol2 = solve!(integrator)
1276+
@test sol1.t sol2.t
1277+
@test sol1.u sol2.u
12661278
end
12671279
end
12681280
end

0 commit comments

Comments
 (0)