Skip to content

Commit 792a84d

Browse files
committed
still bug hunting
1 parent a2b2070 commit 792a84d

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

test/jumpsystem.jl

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ModelingToolkit, DiffEqBase, DiffEqJump, Test, LinearAlgebra
22
MT = ModelingToolkit
33

4-
# basic SIR model with tweaks
4+
# basic MT SIR model with tweaks
55
@parameters β γ t
66
@variables S I R
77
rate₁ = β*S*I
@@ -32,25 +32,24 @@ jump2 = VariableRateJump(rate2,affect2!)
3232
u = [100, 9, 5]
3333
p = (0.1/1000,0.01)
3434
tf = 1.0
35-
mutable struct TestInt
36-
u
37-
p
38-
t
35+
mutable struct TestInt{U,V,T}
36+
u::U
37+
p::V
38+
t::T
3939
end
4040
mtintegrator = TestInt(u,p,tf)
4141
integrator = TestInt(u,p,tf)
4242
@test abs(mtjump1.rate(u,p,tf) - jump1.rate(u,p,tf)) < 10*eps()
4343
@test abs(mtjump2.rate(u,p,tf) - jump2.rate(u,p,tf)) < 10*eps()
4444
mtjump1.affect!(mtintegrator)
4545
jump1.affect!(integrator)
46-
@test norm(integrator.u - mtintegrator.u) < 10*eps()
46+
@test all(integrator.u .== mtintegrator.u)
4747
mtintegrator.u .= u; integrator.u .= u
4848
mtjump2.affect!(mtintegrator)
4949
jump2.affect!(integrator)
50-
@test norm(integrator.u - mtintegrator.u) < 10*eps()
50+
@test all(integrator.u .== mtintegrator.u)
5151

52-
53-
# test can make and solve a jump problem
52+
# test MT can make and solve a jump problem
5453
rate₃ = γ*I
5554
affect₃ = [I ~ I - 1, R ~ R + 1]
5655
j₃ = ConstantRateJump(rate₃,affect₃)
@@ -60,5 +59,38 @@ dprob = DiscreteProblem(u₀,tspan,p)
6059
jprob = JumpProblem(js2, dprob, Direct())
6160
sol = solve(jprob, SSAStepper())
6261

63-
using Plots
64-
plot(sol)
62+
# test the MT JumpProblem rates/affects are correct
63+
rate2(u,p,t) = 0.01u[2]
64+
jump2 = ConstantRateJump(rate2,affect2!)
65+
mtjumps = jprob.discrete_jump_aggregation
66+
@test abs(mtjumps.rates[1](u,p,tf) - jump1.rate(u,p,tf)) < 10*eps()
67+
@test abs(mtjumps.rates[2](u,p,tf) - jump2.rate(u,p,tf)) < 10*eps()
68+
mtjumps.affects![1](mtintegrator)
69+
jump1.affect!(integrator)
70+
@test all(integrator.u .== mtintegrator.u)
71+
mtintegrator.u .= u; integrator.u .= u
72+
mtjumps.affects![2](mtintegrator)
73+
jump2.affect!(integrator)
74+
@test all(integrator.u .== mtintegrator.u)
75+
76+
# # direct vers
77+
# p = (0.1/1000,0.01)
78+
# prob = DiscreteProblem([999,1,0],(0.0,250.0),p)
79+
# r1(u,p,t) = (0.1/1000.0)*u[1]*u[2]
80+
# function a1!(integrator)
81+
# integrator.u[1] -= 1
82+
# integrator.u[2] += 1
83+
# end
84+
# j1 = ConstantRateJump(r1,a1!)
85+
# r2(u,p,t) = 0.01u[2]
86+
# function a2!(integrator)
87+
# integrator.u[2] -= 1
88+
# integrator.u[3] += 1
89+
# end
90+
# j2 = ConstantRateJump(r2,a2!)
91+
# jset = JumpSet((),(j1,j2),nothing,nothing)
92+
# jprob = JumpProblem(prob,Direct(),jset)
93+
# sol = solve(jprob, SSAStepper())
94+
95+
# using Plots
96+
# plot(sol)

0 commit comments

Comments
 (0)