Skip to content

Commit a5fb32a

Browse files
authored
Merge pull request #430 from isaacsas/fix_saveat_plotting
Fix saveat plotting
2 parents 7be7a72 + 9902ecc commit a5fb32a

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ PoissonRandom = "0.4"
3737
RandomNumbers = "1.5"
3838
RecursiveArrayTools = "3.12"
3939
Reexport = "1.0"
40-
SciMLBase = "2.30.1"
40+
SciMLBase = "2.46"
4141
StaticArrays = "1.9"
4242
SymbolicIndexingInterface = "0.3.13"
4343
UnPack = "1.0.2"

src/JumpProcesses.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using RandomNumbers, LinearAlgebra, Markdown, DocStringExtensions
77
using DataStructures, PoissonRandom, Random, ArrayInterface
88
using FunctionWrappers, UnPack
99
using Graphs
10-
using SciMLBase: SciMLBase
10+
using SciMLBase: SciMLBase, isdenseplot
1111
using Base.FastMath: add_fast
1212

1313
import DiffEqBase: DiscreteCallback, init, solve, solve!, plot_indices, initialize!

src/SSA_stepper.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ function DiffEqBase.__init(jump_prob::JumpProblem,
183183
u = typeof(prob.u0)[]
184184
end
185185

186-
sol = DiffEqBase.build_solution(prob, alg, t, u, dense = false,
186+
save_everystep = any(cb.save_positions)
187+
sol = DiffEqBase.build_solution(prob, alg, t, u, dense = save_everystep,
187188
calculate_error = false,
188189
stats = DiffEqBase.Stats(0),
189190
interp = DiffEqBase.ConstantInterpolation(t, u))
190-
save_everystep = any(cb.save_positions)
191191

192192
if saveat isa Number
193193
_saveat = prob.tspan[1]:saveat:prob.tspan[2]
@@ -331,3 +331,9 @@ function DiffEqBase.terminate!(integrator::SSAIntegrator, retcode = ReturnCode.T
331331
end
332332

333333
export SSAStepper
334+
335+
function SciMLBase.isdenseplot(sol::ODESolution{
336+
T, N, uType, uType2, DType, tType, rateType, discType, P,
337+
SSAStepper}) where {T, N, uType, uType2, DType, tType, rateType, discType, P}
338+
sol.dense
339+
end

test/save_positions.jl

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
1-
using JumpProcesses, OrdinaryDiffEq, Test
1+
using JumpProcesses, OrdinaryDiffEq, Test, SciMLBase
22
using StableRNGs
33
rng = StableRNG(12345)
44

55
# test that we only save when a jump occurs
6-
for alg in (Coevolve(),)
7-
u0 = [0]
8-
tspan = (0.0, 30.0)
6+
let
7+
for alg in (Coevolve(),)
8+
u0 = [0]
9+
tspan = (0.0, 30.0)
910

11+
dprob = DiscreteProblem(u0, tspan)
12+
# set the rate to 0, so that no jump ever occurs; but urate is positive so
13+
# Coevolve will consider many candidates before the end of the simmulation.
14+
# None of these points should be saved.
15+
jump = VariableRateJump((u, p, t) -> 0, (integrator) -> integrator.u[1] += 1;
16+
urate = (u, p, t) -> 1.0, rateinterval = (u, p, t) -> 5.0)
17+
jumpproblem = JumpProblem(dprob, alg, jump; dep_graph = [[1]],
18+
save_positions = (false, true), rng)
19+
sol = solve(jumpproblem, SSAStepper())
20+
@test sol.t == [0.0, 30.0]
21+
22+
oprob = ODEProblem((du, u, p, t) -> 0, u0, tspan)
23+
jump = VariableRateJump((u, p, t) -> 0, (integrator) -> integrator.u[1] += 1;
24+
urate = (u, p, t) -> 1.0, rateinterval = (u, p, t) -> 5.0)
25+
jumpproblem = JumpProblem(oprob, alg, jump; dep_graph = [[1]],
26+
save_positions = (false, true), rng)
27+
sol = solve(jumpproblem, Tsit5(); save_everystep = false)
28+
@test sol.t == [0.0, 30.0]
29+
end
30+
end
31+
32+
# test isdenseplot gives correct values for SSAStepper and non-SSAStepper models
33+
let
34+
rate(u, p, t) = max(u[1], 0.0)
35+
affect!(integ) = (integ.u[1] -= 1; nothing)
36+
crj = ConstantRateJump(rate, affect!)
37+
u0 = [10.0]
38+
tspan = (0.0, 10.0)
1039
dprob = DiscreteProblem(u0, tspan)
11-
# set the rate to 0, so that no jump ever occurs; but urate is positive so
12-
# Coevolve will consider many candidates before the end of the simmulation.
13-
# None of these points should be saved.
14-
jump = VariableRateJump((u, p, t) -> 0, (integrator) -> integrator.u[1] += 1;
15-
urate = (u, p, t) -> 1.0, rateinterval = (u, p, t) -> 5.0)
16-
jumpproblem = JumpProblem(dprob, alg, jump; dep_graph = [[1]],
17-
save_positions = (false, true))
18-
sol = solve(jumpproblem, SSAStepper())
19-
@test sol.t == [0.0, 30.0]
40+
sps = ((true, true), (true, false), (false, true), (false, false))
41+
42+
# for pure jump problems dense = save_everystep
43+
vals = (true, true, true, false)
44+
for (sp, val) in zip(sps, vals)
45+
jprob = JumpProblem(dprob, Direct(), crj; save_positions = sp, rng)
46+
sol = solve(jprob, SSAStepper())
47+
@test SciMLBase.isdenseplot(sol) == val
48+
end
49+
50+
# for mixed problems sol.dense currently ignores save_positions
51+
oprob = ODEProblem((du, u, p, t) -> du[1] = 0.1, u0, tspan)
52+
for sp in sps
53+
jprob = JumpProblem(oprob, Direct(), crj; save_positions = sp, rng)
54+
sol = solve(jprob, Tsit5())
55+
@test sol.dense == true
56+
@test SciMLBase.isdenseplot(sol) == true
2057

21-
oprob = ODEProblem((du, u, p, t) -> 0, u0, tspan)
22-
jump = VariableRateJump((u, p, t) -> 0, (integrator) -> integrator.u[1] += 1;
23-
urate = (u, p, t) -> 1.0, rateinterval = (u, p, t) -> 5.0)
24-
jumpproblem = JumpProblem(oprob, alg, jump; dep_graph = [[1]],
25-
save_positions = (false, true))
26-
sol = solve(jumpproblem, Tsit5(); save_everystep = false)
27-
@test sol.t == [0.0, 30.0]
58+
sol = solve(jprob, Tsit5(); dense = false)
59+
@test sol.dense == false
60+
@test SciMLBase.isdenseplot(sol) == false
61+
end
2862
end

0 commit comments

Comments
 (0)