Skip to content

Commit 4ec0010

Browse files
authored
Merge pull request #480 from isaacsas/neg_massaction_rate_fix
fix neg mass action rates with FP types
2 parents 4d52a24 + 5eb437b commit 4ec0010

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/massaction_rates.jl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# stochiometric coefficient.
44
###############################################################################
55

6-
@inline function evalrxrate(speciesvec::AbstractVector{T}, rxidx::S,
7-
majump::MassActionJump{U, V, W, X})::R where
8-
{T, S, R, U <: AbstractVector{R}, V, W, X}
6+
@inline function evalrxrate(speciesvec::AbstractVector{T}, rxidx,
7+
majump::MassActionJump{U})::R where {T <: Integer, R, U <: AbstractVector{R}}
98
val = one(T)
109
@inbounds for specstoch in majump.reactant_stoch[rxidx]
1110
specpop = speciesvec[specstoch[1]]
@@ -19,6 +18,25 @@
1918
@inbounds return val * majump.scaled_rates[rxidx]
2019
end
2120

21+
@inline function evalrxrate(speciesvec::AbstractVector{T}, rxidx,
22+
majump::MassActionJump{U})::R where {T <: Real, R, U <: AbstractVector{R}}
23+
val = one(T)
24+
@inbounds for specstoch in majump.reactant_stoch[rxidx]
25+
specpop = speciesvec[specstoch[1]]
26+
val *= specpop
27+
@inbounds for k in 2:specstoch[2]
28+
specpop -= one(specpop)
29+
val *= specpop
30+
end
31+
# we need to check the smallest rate law term is positive
32+
# i.e. for an order k reaction: x - k + 1 > 0
33+
(specpop <= 0) && return zero(R)
34+
end
35+
36+
@inbounds return val * majump.scaled_rates[rxidx]
37+
end
38+
39+
2240
@inline function executerx!(speciesvec::AbstractVector{T}, rxidx::S,
2341
majump::M) where {T, S, M <: AbstractMassActionJump}
2442
@inbounds net_stoch = majump.net_stoch[rxidx]

test/fp_unknowns.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Test, JumpProcesses, StableRNGs
2+
3+
rng = StableRNG(12345)
4+
5+
# test for https://github.com/SciML/JumpProcesses.jl/issues/479
6+
# A, ∅ --> X
7+
# 1, 2X + Y --> 3X
8+
# B, X --> Y
9+
# 1, X --> ∅
10+
function test(rng)
11+
# dep graphs
12+
dg = [[1, 2, 3, 4],
13+
[2, 3, 4],
14+
[2, 3, 4],
15+
[2, 3, 4]]
16+
vtoj = [[2, 3, 4],
17+
[2]]
18+
jtov = [[1],
19+
[1, 2],
20+
[1, 2],
21+
[1]]
22+
23+
# reaction as MassActionJump
24+
sr = [1.0, .5, 4.0, 1.0]
25+
rs = [Pair{Int,Int}[],[1 => 2, 2 => 1], [1 => 1], [1 => 1]]
26+
ns = [[1 => 1], [1 => 1, 2 => -1], [1 => -1, 2 => 1], [1 => -1]]
27+
maj = MassActionJump(sr, rs, ns; scale_rates = false)
28+
29+
Nsims = 10000
30+
u0 = [1.0, 10.0]
31+
tspan = (0.0, 50.0)
32+
dprob = DiscreteProblem(u0, tspan)
33+
SSAalgs = JumpProcesses.JUMP_AGGREGATORS
34+
Xmeans = zeros(length(SSAalgs))
35+
Ymeans = zeros(length(SSAalgs))
36+
for (j, agg) in enumerate(SSAalgs)
37+
jprob = JumpProblem(dprob, agg, maj; save_positions = (false, false), rng,
38+
vartojumps_map = vtoj, jumptovars_map = jtov, dep_graph = dg,
39+
scale_rates = false)
40+
for i in 1:Nsims
41+
sol = solve(jprob, SSAStepper())
42+
Xmeans[j] += sol[1,end]
43+
Ymeans[j] += sol[2,end]
44+
end
45+
end
46+
Xmeans ./= Nsims
47+
Ymeans ./= Nsims
48+
# for i in 2:length(SSAalgs)
49+
# @test abs(Xmeans[i] - Xmeans[1]) < (.1 * Xmeans[1])
50+
# @test abs(Ymeans[i] - Ymeans[1]) < (.1 * Ymeans[1])
51+
# end
52+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using JumpProcesses, DiffEqBase, SafeTestsets
1616
@time @safetestset "Mass Action Jump Tests; Gene Expr Model" begin include("geneexpr_test.jl") end
1717
@time @safetestset "Mass Action Jump Tests; Nonlinear Rx Model" begin include("bimolerx_test.jl") end
1818
@time @safetestset "Mass Action Jump Tests; Special Cases" begin include("degenerate_rx_cases.jl") end
19+
@time @safetestset "Mass Action Jump Tests; Floating Point Inputs" begin include("fp_unknowns.jl") end
1920
@time @safetestset "Direct allocations test" begin include("allocations.jl") end
2021
@time @safetestset "Bracketing Tests" begin include("bracketing.jl") end
2122
@time @safetestset "Composition-Rejection Table Tests" begin include("table_test.jl") end

0 commit comments

Comments
 (0)