/
linearreaction_test.jl
321 lines (272 loc) · 10.6 KB
/
linearreaction_test.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# calculates the mean from N stochastic A->B reactions at different rates
# this really tests different ways of constructing the jump problems
using DiffEqBase, DiffEqJump, Statistics
using Test
# using BenchmarkTools
# dobenchmark = true
doprint = false
dotest = true
Nrxs = 16
Nsims = 8000
tf = .1
baserate = .1
A0 = 100
exactmean = (t,ratevec) -> A0 * exp(-sum(ratevec) * t)
SSAalgs = [Direct(),RSSA()] #[Direct(),RSSA()]#, DirectFW(), FRM(), FRMFW()]
spec_to_dep_jumps = [collect(1:Nrxs),[]]
jump_to_dep_specs = [[1,2] for i=1:Nrxs]
namedpars = (vartojumps_map=spec_to_dep_jumps, jumptovars_map=jump_to_dep_specs)
rates = ones(Float64, Nrxs) * baserate;
cumsum!(rates, rates)
exactmeanval = exactmean(tf, rates)
function runSSAs(jump_prob)
Asamp = zeros(Int,Nsims)
for i in 1:Nsims
sol = solve(jump_prob, SSAStepper())
Asamp[i] = sol[1,end]
end
mean(Asamp)
end
# uses constant jumps as a tuple within a JumpSet
function A_to_B_tuple(N, method)
# jump reactions
jumpvec = []
for i in 1:N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumpvec, ConstantRateJump(ratefunc, affect!))
end
# convert jumpvec to tuple to send to JumpProblem...
jumps = ((jump for jump in jumpvec)...,)
jset = JumpSet((), jumps, nothing, nothing)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
# uses constant jumps as a vector within a JumpSet
function A_to_B_vec(N, method)
# jump reactions
jumps = Vector{ConstantRateJump}()
for i in 1:N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumps, ConstantRateJump(ratefunc, affect!))
end
# convert jumpvec to tuple to send to JumpProblem...
jset = JumpSet((), jumps, nothing, nothing)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
# uses a single mass action jump to represent all reactions
function A_to_B_ma(N, method)
reactstoch = Vector{Vector{Pair{Int,Int}}}();
netstoch = Vector{Vector{Pair{Int,Int}}}();
for i = 1:N
push!(reactstoch,[1 => 1])
push!(netstoch,[1 => -1, 2=>1])
end
majumps = MassActionJump(rates, reactstoch, netstoch)
jset = JumpSet((), (), nothing, majumps)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
# uses one mass action jump to represent half the reactions and a vector
# of constant jumps for the other half. Stores them in a JumpSet
function A_to_B_hybrid(N, method)
# half reactions are treated as mass action and half as constant jumps
switchidx = (N//2).num
# mass action reactions
reactstoch = Vector{Vector{Pair{Int,Int}}}();
netstoch = Vector{Vector{Pair{Int,Int}}}();
for i in 1:switchidx
push!(reactstoch,[1 => 1])
push!(netstoch,[1 => -1, 2=>1])
end
# jump reactions
jumps = Vector{ConstantRateJump}()
for i in (switchidx+1):N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumps, ConstantRateJump(ratefunc, affect!))
end
majumps = MassActionJump(rates[1:switchidx] , reactstoch, netstoch)
jset = JumpSet((), jumps, nothing, majumps)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
# uses a mass action jump to represent half the reactions and a vector
# of constant jumps for the other half. Passes them to JumpProblem as a splatted tuple
function A_to_B_hybrid_nojset(N, method)
# half reactions are treated as mass action and half as constant jumps
switchidx = (N//2).num
# mass action reactions
reactstoch = Vector{Vector{Pair{Int,Int}}}();
netstoch = Vector{Vector{Pair{Int,Int}}}();
for i in 1:switchidx
push!(reactstoch,[1 => 1])
push!(netstoch,[1 => -1, 2=>1])
end
# jump reactions
jumpvec = Vector{ConstantRateJump}()
for i in (switchidx+1):N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumpvec, ConstantRateJump(ratefunc, affect!))
end
constjumps = (jump for jump in jumpvec)
majumps = MassActionJump(rates[1:switchidx] , reactstoch, netstoch)
jumps = (constjumps...,majumps)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jumps...; save_positions=(false,false), namedpars...)
jump_prob
end
# uses a vector of mass action jumps of vectors to represent half the reactions and a vector
# of constant jumps for the other half. Passes them to JumpProblem as a JumpSet
function A_to_B_hybrid_vecs(N, method)
# half reactions are treated as mass action and half as constant jumps
switchidx = (N//2).num
# mass action reactions
majumps = Vector{MassActionJump}()
for i in 1:switchidx
push!(majumps, MassActionJump([rates[i]], [[1 => 1]], [[1 => -1, 2=>1]] ))
end
# jump reactions
jumpvec = Vector{ConstantRateJump}()
for i in (switchidx+1):N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumpvec, ConstantRateJump(ratefunc, affect!))
end
jset = JumpSet((), jumpvec, nothing, majumps)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
# uses a vector of scalar mass action jumps to represent half the reactions and a vector
# of constant jumps for the other half. Passes them to JumpProblem as a JumpSet
function A_to_B_hybrid_vecs_scalars(N, method)
# half reactions are treated as mass action and half as constant jumps
switchidx = (N//2).num
# mass action reactions
majumps = Vector{MassActionJump}()
for i in 1:switchidx
push!(majumps, MassActionJump(rates[i], [1 => 1], [1 => -1, 2=>1] ))
end
# jump reactions
jumpvec = Vector{ConstantRateJump}()
for i in (switchidx+1):N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumpvec, ConstantRateJump(ratefunc, affect!))
end
jset = JumpSet((), jumpvec, nothing, majumps)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
# uses a vector of scalar mass action jumps to represent half the reactions and a vector
# of constant jumps for the other half. Passes them to JumpProblem as a single splatted tuple.
function A_to_B_hybrid_tups_scalars(N, method)
# half reactions are treated as mass action and half as constant jumps
switchidx = (N//2).num
# mass action reactions
majumpsv = Vector{MassActionJump}()
for i in 1:switchidx
push!(majumpsv, MassActionJump(rates[i], [1 => 1], [1 => -1, 2=>1] ))
end
# jump reactions
jumpvec = Vector{ConstantRateJump}()
for i in (switchidx+1):N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumpvec, ConstantRateJump(ratefunc, affect!))
end
jumps = ((maj for maj in majumpsv)..., (jump for jump in jumpvec)...)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jumps...; save_positions=(false,false), namedpars...)
jump_prob
end
# uses a mass action jump to represent half the reactions and a tuple
# of constant jumps for the other half. Passes them to JumpProblem as a JumpSet.
function A_to_B_hybrid_tups(N, method)
# half reactions are treated as mass action and half as constant jumps
switchidx = (N//2).num
# mass action reactions
majumps = Vector{MassActionJump}()
for i in 1:switchidx
push!(majumps, MassActionJump([rates[i]], [[1 => 1]], [[1 => -1, 2=>1]] ))
end
# jump reactions
jumpvec = Vector{ConstantRateJump}()
for i in (switchidx+1):N
ratefunc = (u,p,t) -> rates[i] * u[1]
affect! = function (integrator)
integrator.u[1] -= 1
integrator.u[2] += 1
end
push!(jumpvec, ConstantRateJump(ratefunc, affect!))
end
jumps = ((jump for jump in jumpvec)...,)
jset = JumpSet((), jumps, nothing, majumps)
prob = DiscreteProblem([A0,0], (0.0,tf))
jump_prob = JumpProblem(prob, method, jset; save_positions=(false,false), namedpars...)
jump_prob
end
jump_prob_gens = [A_to_B_tuple, A_to_B_vec, A_to_B_ma, A_to_B_hybrid, A_to_B_hybrid_nojset,
A_to_B_hybrid_vecs, A_to_B_hybrid_vecs_scalars, A_to_B_hybrid_tups,A_to_B_hybrid_tups_scalars]
#jump_prob_gens = [A_to_B_tuple, A_to_B_ma, A_to_B_hybrid, A_to_B_hybrid_vecs, A_to_B_hybrid_vecs_scalars,A_to_B_hybrid_tups_scalars]
for method in SSAalgs
for jump_prob_gen in jump_prob_gens
jump_prob = jump_prob_gen(Nrxs, method)
meanval = runSSAs(jump_prob)
if doprint
println("Method: ", method, ", Jump input types: ", jump_prob_gen,
", sample mean = ", meanval, ", actual mean = ", exactmeanval)
end
@test abs(meanval - exactmeanval) < 1.
# if dobenchmark
# @btime (runSSAs($jump_prob);)
# end
end
end
# for dependency graph methods just test with mass action jumps
SSAalgs = [NRM(), SortingDirect()]
jump_prob_gens = [A_to_B_ma]
for method in SSAalgs
for jump_prob_gen in jump_prob_gens
jump_prob = jump_prob_gen(Nrxs, method)
meanval = runSSAs(jump_prob)
if doprint
println("Method: ", method, ", Jump input types: ", jump_prob_gen,
", sample mean = ", meanval, ", actual mean = ", exactmeanval)
end
@test abs(meanval - exactmeanval) < 1.
# if dobenchmark
# @btime (runSSAs($jump_prob);)
# end
end
end