Skip to content

Commit 3968efd

Browse files
committed
add test for generated jump system
1 parent 1d71d7a commit 3968efd

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

test/jumpsystem.jl

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,42 +57,50 @@ j₃ = ConstantRateJump(rate₃,affect₃)
5757
js2 = JumpSystem([j₁,j₃], t, [S,I,R], [β,γ])
5858
u₀ = [999,1,0]; p = (0.1/1000,0.01); tspan = (0.,250.)
5959
dprob = DiscreteProblem(u₀,tspan,p)
60-
jprob = JumpProblem(js2, dprob, Direct())
61-
sol = solve(jprob, SSAStepper())
62-
plot(sol)
60+
jprob = JumpProblem(js2, dprob, Direct(), save_positions=(false,false))
61+
Nsims = 10000
62+
function getmean(jprob,Nsims)
63+
m = 0.0
64+
for i = 1:Nsims
65+
sol = solve(jprob, SSAStepper())
66+
m += sol[end,end]
67+
end
68+
m/Nsims
69+
end
70+
m = getmean(jprob,Nsims)
6371

64-
# test the MT JumpProblem rates/affects are correct
65-
# rate2(u,p,t) = 0.01u[2]
66-
# jump2 = ConstantRateJump(rate2,affect2!)
67-
# mtjumps = jprob.discrete_jump_aggregation
68-
# @test abs(mtjumps.rates[1](u,p,tf) - jump1.rate(u,p,tf)) < 10*eps()
69-
# @test abs(mtjumps.rates[2](u,p,tf) - jump2.rate(u,p,tf)) < 10*eps()
70-
# mtjumps.affects![1](mtintegrator)
71-
# jump1.affect!(integrator)
72-
# @test all(integrator.u .== mtintegrator.u)
73-
# mtintegrator.u .= u; integrator.u .= u
74-
# mtjumps.affects![2](mtintegrator)
75-
# jump2.affect!(integrator)
76-
# @test all(integrator.u .== mtintegrator.u)
72+
#test the MT JumpProblem rates/affects are correct
73+
rate2(u,p,t) = 0.01u[2]
74+
jump2 = ConstantRateJump(rate2,affect2!)
75+
mtjumps = jprob.discrete_jump_aggregation
76+
@test abs(mtjumps.rates[1](u,p,tf) - jump1.rate(u,p,tf)) < 10*eps()
77+
@test abs(mtjumps.rates[2](u,p,tf) - jump2.rate(u,p,tf)) < 10*eps()
78+
mtjumps.affects![1](mtintegrator)
79+
jump1.affect!(integrator)
80+
@test all(integrator.u .== mtintegrator.u)
81+
mtintegrator.u .= u; integrator.u .= u
82+
mtjumps.affects![2](mtintegrator)
83+
jump2.affect!(integrator)
84+
@test all(integrator.u .== mtintegrator.u)
7785

78-
# # # direct vers
79-
# p = (0.1/1000,0.01)
80-
# prob = DiscreteProblem([999,1,0],(0.0,250.0),p)
81-
# r1(u,p,t) = (0.1/1000.0)*u[1]*u[2]
82-
# function a1!(integrator)
83-
# integrator.u[1] -= 1
84-
# integrator.u[2] += 1
85-
# end
86-
# j1 = ConstantRateJump(r1,a1!)
87-
# r2(u,p,t) = 0.01u[2]
88-
# function a2!(integrator)
89-
# integrator.u[2] -= 1
90-
# integrator.u[3] += 1
91-
# end
92-
# j2 = ConstantRateJump(r2,a2!)
93-
# jset = JumpSet((),(j1,j2),nothing,nothing)
94-
# jprob = JumpProblem(prob,Direct(),jset)
95-
# sol2 = solve(jprob, SSAStepper())
86+
# direct vers
87+
p = (0.1/1000,0.01)
88+
prob = DiscreteProblem([999,1,0],(0.0,250.0),p)
89+
r1(u,p,t) = (0.1/1000.0)*u[1]*u[2]
90+
function a1!(integrator)
91+
integrator.u[1] -= 1
92+
integrator.u[2] += 1
93+
end
94+
j1 = ConstantRateJump(r1,a1!)
95+
r2(u,p,t) = 0.01u[2]
96+
function a2!(integrator)
97+
integrator.u[2] -= 1
98+
integrator.u[3] += 1
99+
end
100+
j2 = ConstantRateJump(r2,a2!)
101+
jset = JumpSet((),(j1,j2),nothing,nothing)
102+
jprob = JumpProblem(prob,Direct(),jset, save_positions=(false,false))
103+
m2 = getmean(jprob,Nsims)
96104

97-
# using Plots
98-
# plot(sol)
105+
# test JumpSystem solution agrees with direct version
106+
@test abs(m-m2) ./ m < .01

0 commit comments

Comments
 (0)