Skip to content

Commit 891472d

Browse files
Merge pull request #7 from weilandtd/patch-1
Higher order reactions
2 parents 9b71727 + 3e74ca3 commit 891472d

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

src/reactions.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@ ReactionSet(args...) = ReactionSet(tuple(args...))
5959
function build_jumps_from_reaction(r::Reaction;save_positions=(false,true))
6060
rate = function (t,u)
6161
val = r.rate_constant
62+
# for higher order interactions i.e. reactants [1,1] vaule = k*u[1]*(u[1]-1)
63+
# Since a single entitiy cannot interact with it self!
64+
k = 0.0
65+
prev_reactant = -1
6266
@fastmath @inbounds for i in eachindex(r.reactants)
63-
val *= u[r.reactants[i]]
67+
if prev_reactant == r.reactants[i]
68+
k += 1.0
69+
else
70+
k = 0.0
71+
end
72+
val *= (u[r.reactants[i]] - k)
73+
prev_reactant = r.reactants[i]
6474
end
6575
val
6676
end
@@ -75,8 +85,18 @@ end
7585
function build_jumps_from_reaction(r::VariableRateReaction;save_positions=(false,true))
7686
rate = function (t,u)
7787
val = r.rate_constant
88+
# for higher order interactions i.e. reactants [1,1] vaule = k*u[1]*(u[1]-1)
89+
# Since a single entitiy cannot interact with it self!
90+
k = 0.0
91+
prev_reactant = -1
7892
@fastmath @inbounds for i in eachindex(r.reactants)
79-
val *= u[r.reactants[i]]
93+
if prev_reactant == r.reactants[i]
94+
k += 1.0
95+
else
96+
k = 0.0
97+
end
98+
val *= (u[r.reactants[i]] - k)
99+
prev_reactant = r.reactants[i]
80100
end
81101
val
82102
end

test/higher_order_reactions.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using DiffEqBiological, DiffEqJump, DiffEqBase, OrdinaryDiffEq
2+
3+
k = rand()
4+
# First order
5+
r1 = Reaction(k,[1],((1,-1),(2,1)))
6+
jump = build_jumps_from_reaction(r1)
7+
@test jump.rate(0.0,[1,0]) == k
8+
9+
10+
# Second order
11+
r2 = Reaction(k,[1,1],((1,-2),(2,1)))
12+
jump2 = build_jumps_from_reaction(r2)
13+
@test jump2.rate(0.0,[1,0]) == 0
14+
@test jump2.rate(0.0,[2,0]) == k*2*1
15+
16+
# Thrid order
17+
r3 = Reaction(k,[1,1,1],((1,-3),(2,1)))
18+
jump3 = build_jumps_from_reaction(r3)
19+
@test jump3.rate(0.0,[1,0]) == 0
20+
@test jump3.rate(0.0,[2,0]) == 0
21+
@test jump3.rate(0.0,[3,0]) == k*3*2*1
22+
23+
# Mixed Third order
24+
r21 = Reaction(k ,[1,1,2],((1,-2),(2,-1)))
25+
jump21 = build_jumps_from_reaction(r21)
26+
@test jump21.rate(0.0,[1,1]) == 0
27+
@test jump21.rate(0.0,[2,1]) == k*2*1*1
28+
29+
# Mixed Third order different order
30+
r12 = Reaction(k ,[2,1,1],((1,-2),(2,-1)))
31+
jump12 = build_jumps_from_reaction(r12)
32+
jump12.rate(0.0,[1,1]) == 0
33+
@test jump12.rate(0.0,[2,1]) == k*2*1*1
34+
35+
36+
# Solve the dimerization problem
37+
prob = DiscreteProblem([1,0],(0.0,250.0))
38+
jump_prob = GillespieProblem(prob,Direct(),r2)
39+
sol = solve(jump_prob,Discrete())
40+
@test find( x-> x!=0 ,[u[2] for u in sol.u]) == []

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ tic()
55
@time @testset "Gillespie Tests" begin include("gillespie.jl") end
66
@time @testset "Variable Rate Reaction Tests" begin include("variable_rate_reactions.jl") end
77
@time @testset "@reaction_network Tests" begin include("reaction_network.jl") end
8+
@time @testset "Higher order reaction Tests" begin include("higher_order_reactions.jl") end
89
toc()

0 commit comments

Comments
 (0)