Skip to content

Commit 639ae0b

Browse files
authored
Merge pull request #90 from isaacsas/use-discretefunctions
add DiscreteProblem overload for reaction_networks
2 parents 2c207e9 + 4d26d62 commit 639ae0b

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
julia 1.0
22
DiffEqJump 5.6.0
3-
DiffEqBase 3.11.0
3+
DiffEqBase 5.1.0
44
Compat 0.17.0
55
DataStructures 0.8.1
66
Reexport 0.1.0

src/DiffEqBiological.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ include("problem.jl")
2020

2121
export @reaction_network, @reaction_func, @min_reaction_network
2222
export addodes!, addsdes!, addjumps!
23-
export ODEProblem, SDEProblem, JumpProblem, SteadyStateProblem
23+
export ODEProblem, SDEProblem, DiscreteProblem, JumpProblem, SteadyStateProblem
2424

2525
end # module

src/problem.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ function DiffEqBase.SDEProblem(rn::DiffEqBase.AbstractReactionNetwork, u0::Union
1010
SDEProblem(rn.sdefun, rn.g::Function, u0, args...;noise_rate_prototype=rn.p_matrix, kwargs...)
1111
end
1212

13+
### DiscreteProblem, passing through syms
14+
function DiffEqBase.DiscreteProblem(rn::DiffEqBase.AbstractReactionNetwork, u0, tspan::Tuple, p=nothing; kwargs...)
15+
f = DiffEqBase.DISCRETE_INPLACE_DEFAULT
16+
df = DiscreteFunction{true,typeof(f),Nothing,typeof(rn.syms)}(f,nothing,rn.syms)
17+
DiscreteProblem(df, u0, tspan, p; kwargs...)
18+
end
19+
1320
### JumpProblem ###
1421
function build_jump_problem(prob, aggregator, rn, jumps, kwargs...)
1522
if typeof(prob)<:DiscreteProblem && any(x->typeof(x) <: VariableRateJump, jumps)

test/discreteproblem_test.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using DiffEqBase, DiffEqBiological, DiffEqJump, Statistics
2+
3+
dotestmean = true
4+
doprintmeans = false
5+
reltol = .01 # required test accuracy
6+
algs = (Direct(),)
7+
8+
# run the given number of SSAs and return the mean
9+
function runSSAs(jump_prob, Nsims, idx)
10+
Psamp = zeros(Int, Nsims)
11+
for i in 1:Nsims
12+
sol = solve(jump_prob, SSAStepper())
13+
Psamp[i] = sol[idx,end]
14+
end
15+
mean(Psamp)
16+
end
17+
18+
function execute_test(prob, u0, tf, rates, rs, Nsims, expected_avg, idx, test_name)
19+
for method in algs
20+
jump_prob = JumpProblem(prob, method, rs, save_positions=(false,false))
21+
avg_val = runSSAs(jump_prob, Nsims, idx)
22+
23+
if dotestmean
24+
if doprintmeans
25+
println(test_name, ", method = ", typeof(method), ", mean = ", avg_val, ", act_mean = ", expected_avg)
26+
end
27+
@test abs(avg_val - expected_avg) < reltol * expected_avg
28+
end
29+
end
30+
31+
end
32+
33+
# full macro
34+
Nsims = 32000
35+
tf = .01
36+
u0 = [200, 100, 150]
37+
expected_avg = 84.876015624999994
38+
rs = @reaction_network dtype begin
39+
k1, 2A --> B
40+
k2, B --> 2A
41+
k3, A + B --> C
42+
k4, C --> A + B
43+
k5, 3C --> 3A
44+
end k1 k2 k3 k4 k5
45+
rates = [1., 2., .5, .75, .25]
46+
prob = DiscreteProblem(u0, (0.0, tf), rates)
47+
execute_test(prob, u0, tf, rates, rs, Nsims, expected_avg, 1, "Nonlinear rx test (no syms)")
48+
49+
prob = DiscreteProblem(rs, u0, (0.0, tf), rates)
50+
execute_test(prob, u0, tf, rates, rs, Nsims, expected_avg, 1, "Nonlinear rx test (syms)")
51+
52+
# min_network macro
53+
rs = @min_reaction_network dtype begin
54+
k1, 2A --> B
55+
k2, B --> 2A
56+
k3, A + B --> C
57+
k4, C --> A + B
58+
k5, 3C --> 3A
59+
end k1 k2 k3 k4 k5
60+
addjumps!(rs)
61+
prob = DiscreteProblem(u0, (0.0, tf), rates)
62+
execute_test(prob, u0, tf, rates, rs, Nsims, expected_avg, 1, "Nonlinear rx test, min network (no syms)")
63+
64+
prob = DiscreteProblem(rs, u0, (0.0, tf), rates)
65+
execute_test(prob, u0, tf, rates, rs, Nsims, expected_avg, 1, "Nonlinear rx test, min network (syms)")
66+

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DiffEqBiological
22
using Test
33

4+
# full macro tests
45
@time begin
56
@time @testset "Model Macro" begin include("make_model_test.jl") end
67
@time @testset "Gillespie Tests" begin include("gillespie.jl") end
@@ -11,6 +12,7 @@ using Test
1112
@time @testset "Mass Action Jumps" begin include("mass_act_jump_tests.jl") end
1213
end
1314

15+
# min macro tests
1416
@time begin
1517
@time @testset "Model Macro (Min)" begin include("make_model_test_min.jl") end
1618
@time @testset "Gillespie Tests (Min)" begin include("gillespie_min.jl") end
@@ -19,4 +21,9 @@ end
1921
@time @testset "Additional Functions (Min)" begin include("func_test_min.jl") end
2022
@time @testset "Steady state solver (Min)" begin include("steady_state_min.jl") end
2123
@time @testset "Mass Action Jumps (Min)" begin include("mass_act_jump_tests_min.jl") end
24+
end
25+
26+
# tests that handle both macros
27+
@time begin
28+
@time @testset "Discrete Problem" begin include("discreteproblem_test.jl") end
2229
end

0 commit comments

Comments
 (0)