1
1
using ModelingToolkit, DiffEqBase, DiffEqJump, Test, LinearAlgebra
2
2
MT = ModelingToolkit
3
3
4
- # basic SIR model with tweaks
4
+ # basic MT SIR model with tweaks
5
5
@parameters β γ t
6
6
@variables S I R
7
7
rate₁ = β* S* I
@@ -32,25 +32,24 @@ jump2 = VariableRateJump(rate2,affect2!)
32
32
u = [100 , 9 , 5 ]
33
33
p = (0.1 / 1000 ,0.01 )
34
34
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
39
39
end
40
40
mtintegrator = TestInt (u,p,tf)
41
41
integrator = TestInt (u,p,tf)
42
42
@test abs (mtjump1. rate (u,p,tf) - jump1. rate (u,p,tf)) < 10 * eps ()
43
43
@test abs (mtjump2. rate (u,p,tf) - jump2. rate (u,p,tf)) < 10 * eps ()
44
44
mtjump1. affect! (mtintegrator)
45
45
jump1. affect! (integrator)
46
- @test norm (integrator. u - mtintegrator. u) < 10 * eps ()
46
+ @test all (integrator. u .== mtintegrator. u)
47
47
mtintegrator. u .= u; integrator. u .= u
48
48
mtjump2. affect! (mtintegrator)
49
49
jump2. affect! (integrator)
50
- @test norm (integrator. u - mtintegrator. u) < 10 * eps ( )
50
+ @test all (integrator. u .== mtintegrator. u)
51
51
52
-
53
- # test can make and solve a jump problem
52
+ # test MT can make and solve a jump problem
54
53
rate₃ = γ* I
55
54
affect₃ = [I ~ I - 1 , R ~ R + 1 ]
56
55
j₃ = ConstantRateJump (rate₃,affect₃)
@@ -60,5 +59,38 @@ dprob = DiscreteProblem(u₀,tspan,p)
60
59
jprob = JumpProblem (js2, dprob, Direct ())
61
60
sol = solve (jprob, SSAStepper ())
62
61
63
- using Plots
64
- plot (sol)
62
+ # test the MT JumpProblem rates/affects are correct
63
+ rate2 (u,p,t) = 0.01 u[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
69
+ jump1. affect! (integrator)
70
+ @test all (integrator. u .== mtintegrator. u)
71
+ mtintegrator. u .= u; integrator. u .= u
72
+ mtjumps. affects
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