Skip to content

Commit 286db5f

Browse files
committed
update
1 parent 7174fa6 commit 286db5f

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

test/miscellaneous_tests/symbolic_stoichiometry.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ begin
5757
u0_1 = (A => 3.0, B => 2.0, C => 3.0, D => 5.0)
5858
ps_1 = (k => 5.0, α => 2)
5959
u0_2 = [Int64(u[2]) for u in u0_1]
60-
ps_2 = Tuple(p[2] for p in ps_1)
60+
ps_2 = [p[2] for p in ps_1]
6161
τ = 1.5
6262

6363
# Creates `ReactionSystem` model.
@@ -248,3 +248,46 @@ let
248248
jprob_int_ref = JumpProblem(rs_ref_int, dprob_int_ref, Direct(); rng)
249249
@test solve(jprob_int, SSAStepper(); seed) == solve(jprob_int_ref, SSAStepper(); seed)
250250
end
251+
252+
# Check that jump simulations (implemented with and without symbolic stoichiometries) yield simulations
253+
# with identical mean number of species at the end of the simulations.
254+
# Also checks that ODE simulations are identical.
255+
let
256+
# Creates the models.
257+
sir = @reaction_network begin
258+
@parameters n::Int64 k::Int64
259+
i, S + n*I --> k*I
260+
r, n*I --> n*R
261+
end
262+
sir_ref = @reaction_network begin
263+
i, S + I --> 2I
264+
r, I --> R
265+
end
266+
267+
ps = [:i => 1e-4, :r => 1e-2, :n => 1.0, :k => 2.0]
268+
ps_ref = [:i => 1e-4, :r => 1e-2]
269+
tspan = (0.0, 250.0) # tspan[2] is selected so that it is in the middle of the outbreak peak.
270+
u0 = [:S => 999.0, :I => 1.0, :R => 0.0]
271+
@test issetequal(unknowns(sir), unknowns(sir_ref))
272+
273+
# Checks that ODE simulations are identical.
274+
oprob = ODEProblem(sir, u0, tspan, ps)
275+
oprob_ref = ODEProblem(sir_ref, u0, tspan, ps_ref)
276+
@test solve(oprob, Tsit5()) solve(oprob_ref, Tsit5())
277+
278+
# Jumps. First ensemble problems for each systems is created.
279+
dprob = DiscreteProblem(sir, u0, tspan, ps)
280+
dprob_ref = DiscreteProblem(sir_ref, u0, tspan, ps_ref)
281+
jprob = JumpProblem(sir, dprob, Direct(), save_positions = (false, false))
282+
jprob_ref = JumpProblem(sir_ref, dprob_ref, Direct(), save_positions = (false, false))
283+
eprob = EnsembleProblem(jprob)
284+
eprob_ref = EnsembleProblem(jprob_ref)
285+
286+
# Simulates both ensemble problems. Checks that the distribution of values at the simulation ends is similar.
287+
sols = solve(eprob, SSAStepper(); trajectories = 100000)
288+
sols_ref = solve(eprob_ref, SSAStepper(); trajectories = 100000)
289+
end_vals = [[sol[s][end] for sol in sols.u] for s in species(sir)]
290+
end_vals_ref = [[sol[s][end] for sol in sols_ref.u] for s in species(sir_ref)]
291+
@test mean.(end_vals_ref) mean.(end_vals) atol=1e-2 rtol = 1e-2
292+
@test var.(end_vals_ref) var.(end_vals) atol=1e-2 rtol = 1e-2
293+
end

test/reactionsystem_structure/designating_parameter_types.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ let
6868
end
6969
end
7070

71+
# Test that the various structures stores the parameters using the correct type.
7172
# Test that the various structures stores the parameters using the correct type.
7273
let
7374
# Creates problems, integrators, and solutions.
@@ -87,8 +88,9 @@ let
8788
jsol = solve(jprob, SSAStepper(); seed)
8889
nsol = solve(nprob, NewtonRaphson())
8990

90-
# Checks the types of all stored parameter values.
91+
# Checks all stored parameters.
9192
for mtk_struct in [oprob, sprob, dprob, jprob, nprob, oinit, sinit, jinit, osol, ssol, jsol, nsol]
93+
# Checks that all parameters have the correct type.
9294
@test unwrap(mtk_struct.ps[p1]) isa Float64
9395
@test unwrap(mtk_struct.ps[d1]) isa Float64
9496
@test unwrap(mtk_struct.ps[p2]) isa Float64
@@ -99,6 +101,35 @@ let
99101
@test unwrap(mtk_struct.ps[d4]) isa Rational{Int64}
100102
@test unwrap(mtk_struct.ps[p5]) isa Rational{Int64}
101103
@test unwrap(mtk_struct.ps[d5]) isa Float32
104+
105+
# Checks that all parameters have the correct value.
106+
@test unwrap(mtk_struct.ps[p1]) == 1.0
107+
@test unwrap(mtk_struct.ps[d1]) == 1.0
108+
@test unwrap(mtk_struct.ps[p2]) == 1.2
109+
@test unwrap(mtk_struct.ps[d2]) == 1.2
110+
@test unwrap(mtk_struct.ps[p3]) == 2
111+
@test unwrap(mtk_struct.ps[d3]) == 2
112+
@test unwrap(mtk_struct.ps[p4]) == Float32(0.5)
113+
@test unwrap(mtk_struct.ps[d4]) == 1//2
114+
@test unwrap(mtk_struct.ps[p5]) == 3//2
115+
@test unwrap(mtk_struct.ps[d5]) == Float32(1.5)
116+
end
117+
118+
# Checks all stored variables
119+
for mtk_struct in [oprob, sprob, dprob, jprob, nprob, oinit, sinit, jinit]
120+
# Checks that all variables have the correct type.
121+
@test unwrap(mtk_struct[X1]) isa Float64
122+
@test unwrap(mtk_struct[X2]) isa Float64
123+
@test unwrap(mtk_struct[X3]) isa Float64
124+
@test unwrap(mtk_struct[X4]) isa Float64
125+
@test unwrap(mtk_struct[X5]) isa Float64
126+
127+
# Checks that all variables have the correct value.
128+
@test unwrap(mtk_struct[X1]) == 0.1
129+
@test unwrap(mtk_struct[X2]) == 0.2
130+
@test unwrap(mtk_struct[X3]) == 0.3
131+
@test unwrap(mtk_struct[X4]) == 0.4
132+
@test unwrap(mtk_struct[X5]) == 0.5
102133
end
103134

104135
# Indexing currently broken for NonlinearSystem integrators (MTK intend to support this though).

test/test_functions.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Various functions that are useful for running the tests, and used across several test sets.
22

33
# Fetches the (required) Random package.
4-
using Random
4+
using Random, Test
55

66
### Initial Condition/Parameter Generators ###
77

@@ -36,19 +36,31 @@ end
3636
### System function evaluation ###
3737

3838
# Evaluates the the drift function of the ODE corresponding to a reaction network.
39+
# Also checks that in place and out of place evaluations are identical.
3940
function f_eval(rs::ReactionSystem, u, p, t; combinatoric_ratelaws = true)
4041
prob = ODEProblem(rs, u, (0.0, 0.0), p; combinatoric_ratelaws)
41-
return prob.f(prob.u0, prob.p, t)
42+
du = zeros(length(u))
43+
prob.f(du, prob.u0, prob.p, t)
44+
@test du == prob.f(prob.u0, prob.p, t)
45+
return du
4246
end
4347

4448
# Evaluates the the Jacobian of the drift function of the ODE corresponding to a reaction network.
49+
# Also checks that in place and out of place evaluations are identical.
4550
function jac_eval(rs::ReactionSystem, u, p, t; combinatoric_ratelaws = true)
4651
prob = ODEProblem(rs, u, (0.0, 0.0), p; jac = true, combinatoric_ratelaws)
47-
return prob.f.jac(prob.u0, prob.p, t)
52+
J = zeros(length(u), length(u))
53+
prob.f.jac(J, prob.u0, prob.p, t)
54+
@test J == prob.f.jac(prob.u0, prob.p, t)
55+
return J
4856
end
4957

5058
# Evaluates the the diffusion function of the SDE corresponding to a reaction network.
59+
# Also checks that in place and out of place evaluations are identical.
5160
function g_eval(rs::ReactionSystem, u, p, t; combinatoric_ratelaws = true)
5261
prob = SDEProblem(rs, u, (0.0, 0.0), p; combinatoric_ratelaws)
53-
return prob.g(prob.u0, prob.p, t)
54-
end
62+
dW = zeros(length(u), numreactions(rs))
63+
prob.g(dW, prob.u0, prob.p, t)
64+
@test dW == prob.g(prob.u0, prob.p, t)
65+
return dW
66+
end

0 commit comments

Comments
 (0)