Skip to content

Commit f849d27

Browse files
Merge pull request #29 from palmtree2013/plan_to_add_coevolve
Support `DelayCoevolve`
2 parents ad1719b + 3320428 commit f849d27

34 files changed

+1318
-750
lines changed

Project.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DelaySSAToolkit"
22
uuid = "d39380a9-d0d7-4ae6-ae63-0e8d8b7efa9e"
33
authors = ["Xiaoming Fu"]
4-
version = "0.2.3"
4+
version = "0.2.4"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -18,7 +18,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1818
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
1919

2020
[compat]
21-
Catalyst = "12.0"
21+
Catalyst = "12.0"
2222
DataStructures = "0.17, 0.18"
2323
DiffEqBase = "6.45"
2424
DocStringExtensions = "0.8, 0.9"
@@ -34,6 +34,9 @@ julia = "1.6, 1.7, 1.8"
3434
[extras]
3535
Catalyst = "479239e8-5488-4da2-87a7-35f2df7eef83"
3636
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
37+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
38+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
39+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
3740

3841
[targets]
3942
test = ["Test"]

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
|:-----------------:|:----------------:|:----------------:|
55
| [![doc dev badge]][doc dev link] | [![ci badge]][ci link] [![cov badge]][cov link] | [![download badge]][download link]| -->
66

7-
| **Documentation** | **Build Status** |
8-
|:-----------------:|:----------------:|
9-
| [![doc dev badge]][doc dev link] | [![ci badge]][ci link] [![cov badge]][cov link] |
7+
| **Documentation** | **Build Status** | **Code Style** |
8+
|:-----------------:|:----------------:|:----------------:|
9+
| [![doc dev badge]][doc dev link] | [![ci badge]][ci link] [![cov badge]][cov link] | [![SciML Code Style](https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826)](https://github.com/SciML/SciMLStyle)
1010

1111
[doc dev badge]: https://img.shields.io/badge/docs-dev-blue.svg
1212
[doc dev link]: https://palmtree2013.github.io/DelaySSAToolkit.jl/dev/

docs/make.jl

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
using DelaySSAToolkit
22
using Documenter
33

4-
DocMeta.setdocmeta!(DelaySSAToolkit, :DocTestSetup, :(using DelaySSAToolkit); recursive=true)
4+
DocMeta.setdocmeta!(DelaySSAToolkit, :DocTestSetup, :(using DelaySSAToolkit);
5+
recursive = true)
56

67
makedocs(;
7-
modules=[DelaySSAToolkit],
8-
authors="Xiaoming Fu",
9-
repo="https://github.com/palmtree2013/DelaySSAToolkit.jl/blob/{commit}{path}#{line}",
10-
sitename="DelaySSAToolkit.jl",
11-
format=Documenter.HTML(;
12-
mathengine=Documenter.Writers.HTMLWriter.MathJax2(),
13-
prettyurls=get(ENV, "CI", "false") == "true",
14-
canonical="https://palmtree2013.github.io/DelaySSAToolkit.jl",
15-
assets=String[],
16-
),
17-
pages=[
18-
"Home" => "index.md",
19-
"Tutorials" => [
20-
"tutorials/tutorials.md",
21-
"tutorials/bursty.md",
22-
"tutorials/delay_degradation.md",
23-
"tutorials/heterogeneous_delay.md",
24-
"tutorials/delay_oscillator.md",
25-
"tutorials/stochastic_delay.md",
26-
],
27-
"Algorithm" => [
28-
"algorithms/notations.md",
29-
"algorithms/delayrejection.md",
30-
"algorithms/delaydirect.md",
31-
"algorithms/delaymnrm.md",
32-
],
33-
"Theory" => "theory.md",
34-
"API" => "api.md",
35-
],
36-
)
8+
modules = [DelaySSAToolkit],
9+
authors = "Xiaoming Fu",
10+
repo = "https://github.com/palmtree2013/DelaySSAToolkit.jl/blob/{commit}{path}#{line}",
11+
sitename = "DelaySSAToolkit.jl",
12+
format = Documenter.HTML(;
13+
mathengine = Documenter.Writers.HTMLWriter.MathJax2(),
14+
prettyurls = get(ENV, "CI", "false") == "true",
15+
canonical = "https://palmtree2013.github.io/DelaySSAToolkit.jl",
16+
assets = String[]),
17+
pages = [
18+
"Home" => "index.md",
19+
"Tutorials" => [
20+
"tutorials/tutorials.md",
21+
"tutorials/bursty.md",
22+
"tutorials/delay_degradation.md",
23+
"tutorials/heterogeneous_delay.md",
24+
"tutorials/delay_oscillator.md",
25+
"tutorials/stochastic_delay.md",
26+
],
27+
"Algorithm" => [
28+
"algorithms/notations.md",
29+
"algorithms/delayrejection.md",
30+
"algorithms/delaydirect.md",
31+
"algorithms/delaymnrm.md",
32+
],
33+
"Theory" => "theory.md",
34+
"API" => "api.md",
35+
])
3736

3837
deploydocs(;
39-
repo="github.com/palmtree2013/DelaySSAToolkit.jl",
40-
devbranch="main",
41-
)
38+
repo = "github.com/palmtree2013/DelaySSAToolkit.jl",
39+
devbranch = "main")

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ DelaySSAToolkit.jl is a tool developed on top of [JumpProcesses.jl](https://gith
2020

2121
DelaySSAToolkit can be installed through the Julia package manager:
2222
```julia
23-
]add DelaySSAToolkit
24-
using DelaySSAToolkit
23+
using Pkg
24+
Pkg.add("DelaySSAToolkit")
2525
```
2626
and you might need to run
2727
```julia

docs/src/tutorials/heterogeneous_delay.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ ensprob2 = EnsembleProblem(jprob, prob_func = prob_func)
7878
Note that a simulation of $10^4$ samples with very high production number (up to ~1000 for X and ~3000 for Y) only takes few minutes on a laptop:
7979
```julia-repl
8080
julia> @time ens2 = solve(ensprob2, SSAStepper(), EnsembleThreads(), trajectories = 10^4)
81-
82-
78.925908 seconds (249.65 M allocations: 28.632 GiB, 6.60% gc time)
83-
EnsembleSolution Solution of length 10000 with uType
8481
```
8582
Here we plot the histogram of the number of unfinished reactant $X$s in the delay channel.
8683

examples/bursty.jl

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,70 @@ begin # construct reaction network
99
@parameters a b t
1010
@variables X(t)
1111
burst_sup = 30
12-
rxs = [Reaction(a*b^i/(1+b)^(i+1),nothing,[X],nothing,[i]) for i in 1:burst_sup]
12+
rxs = [Reaction(a * b^i / (1 + b)^(i + 1), nothing, [X], nothing, [i])
13+
for i in 1:burst_sup]
1314
rxs = vcat(rxs)
14-
@named rs = ReactionSystem(rxs,t,[X],[a,b])
15+
@named rs = ReactionSystem(rxs, t, [X], [a, b])
1516
end
1617

1718
# convert the ReactionSystem to a JumpSystem
18-
jumpsys = convert(JumpSystem, rs, combinatoric_ratelaws=false)
19+
jumpsys = convert(JumpSystem, rs, combinatoric_ratelaws = false)
1920

2021
u0 = [0]
2122
de_chan0 = [[]]
22-
tf = 200.
23-
tspan = (0,tf)
23+
tf = 200.0
24+
tspan = (0, tf)
2425
ps = [0.0282, 3.46]
25-
dprob = DiscreteProblem(jumpsys,u0,tspan,ps)
26-
τ = 130.
26+
dprob = DiscreteProblem(jumpsys, u0, tspan, ps)
27+
τ = 130.0
2728

2829
delay_trigger_affect! = []
2930
for i in 1:burst_sup
3031
push!(delay_trigger_affect!, function (integrator, rng)
31-
append!(integrator.de_chan[1], fill(τ, i))
32-
end)
32+
append!(integrator.de_chan[1], fill(τ, i))
33+
end)
3334
end
3435
delay_trigger_affect!
3536
delay_trigger = Dict([Pair(i, delay_trigger_affect![i]) for i in 1:burst_sup])
36-
delay_complete = Dict(1=>[1=>-1])
37+
delay_complete = Dict(1 => [1 => -1])
3738
delay_interrupt = Dict()
38-
alg = DelayRejection()
39+
# alg = DelayRejection()
40+
alg = DelayCoevolve()
3941

4042
delayjumpset = DelayJumpSet(delay_trigger, delay_complete, delay_interrupt)
41-
jprob = DelayJumpProblem(jumpsys, dprob, alg, delayjumpset, de_chan0, save_positions=(true, true), save_delay_channel = false)
43+
jprob = DelayJumpProblem(jumpsys, dprob, alg, delayjumpset, de_chan0,
44+
save_positions = (true, true), save_delay_channel = false)
4245
# saveat = 0:1:tf
4346
seed = 4
4447
@time sol = solve(jprob, SSAStepper(), seed = seed)
4548

46-
4749
ensprob = EnsembleProblem(jprob)
48-
@time ens = solve(ensprob, SSAStepper(), EnsembleSerial(), trajectories=1e5)
50+
@time ens = solve(ensprob, SSAStepper(), EnsembleSerial(), trajectories = 1e5)
4951

5052
# Check with the exact probability distribution
5153
using TaylorSeries
52-
function taylor_coefficients(NT::Int,at_x,gen::Function)
54+
function taylor_coefficients(NT::Int, at_x, gen::Function)
5355
Q = zeros(NT)
54-
taylor_gen = taylor_expand(u->gen(u),at_x,order=NT)
56+
taylor_gen = taylor_expand(u -> gen(u), at_x, order = NT)
5557
for j in 1:NT
56-
Q[j] = taylor_gen[j-1]
58+
Q[j] = taylor_gen[j - 1]
5759
end
5860
return Q
5961
end
60-
function delay_bursty(params,NT::Int)
62+
function delay_bursty(params, NT::Int)
6163
a, b, τ, t = params
62-
gen1(u) = exp(a*b*min(τ,t)*u/(1-b*u))
63-
taylor_coefficients(NT,-1,gen1)
64+
gen1(u) = exp(a * b * min(τ, t) * u / (1 - b * u))
65+
taylor_coefficients(NT, -1, gen1)
6466
end
6567

66-
6768
using Catalyst.EnsembleAnalysis
68-
using Plots; theme(:vibrant)
69+
using Plots;
70+
theme(:vibrant);
6971
sol_end = componentwise_vectors_timepoint(ens, tf)
70-
histogram(sol_end,bins=0:1:60,normalize=:pdf, label = "Delay SSA",fillalpha = 0.6, linecolor = :orange)
72+
histogram(sol_end, bins = 0:1:60, normalize = :pdf, label = "Delay SSA", fillalpha = 0.6,
73+
linecolor = :orange)
7174

72-
sol_exact = delay_bursty([ps;130;200], 61)
73-
fig = plot!(0:60,sol_exact, linewidth = 3, label = "Exact solution", fmt=:svg, xlabel = "# of products", ylabel = "Probability")
75+
sol_exact = delay_bursty([ps; 130; 200], 61)
76+
fig = plot!(0:60, sol_exact, linewidth = 3, label = "Exact solution", fmt = :svg,
77+
xlabel = "# of products", ylabel = "Probability")
7478
# savefig(fig, "docs/src/assets/bursty.svg")

examples/delay_degradation.jl

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,75 @@
1010
# x_I(t)= 0<=t<=τ ? C*β/(a-γ)*((1-exp(-γ*t))/γ - (1-exp(-a*t))/a) : C*β/a*((1-exp(-γ*τ))/γ + exp(-a*t)*(1-exp(-(a-γ)τ))/(a-γ))
1111
# where a = β + γ
1212

13-
1413
using Catalyst
15-
using DelaySSAToolkit
14+
using DelaySSAToolkit
1615

1716
rn = @reaction_network begin
18-
C, 0 --> Xₐ
19-
γ, Xₐ --> 0
20-
β, Xₐ --> Xᵢ
21-
γ, Xᵢ --> 0
17+
C, 0 --> Xₐ
18+
γ, Xₐ --> 0
19+
β, Xₐ --> Xᵢ
20+
γ, Xᵢ --> 0
2221
end C γ β
2322

2423
jumpsys = convert(JumpSystem, rn, combinatoric_ratelaws = false)
2524

2625
u0 = [0, 0]
2726
tf = 30
28-
saveat = .1
27+
saveat = 0.1
2928
de_chan0 = [[]]
30-
C, γ, β = [2., 0.1, 0.5]
29+
C, γ, β = [2.0, 0.1, 0.5]
3130
p = [C, γ, β]
32-
tspan = (0.,tf)
31+
tspan = (0.0, tf)
3332
# aggregatoralgo = DelayRejection()
3433
# aggregatoralgo = DelayMNRM()
3534
aggregatoralgo = DelayDirect()
3635
# aggregatoralgo = DelayDirectCR()
3736
dprob = DiscreteProblem(u0, tspan, p)
3837

39-
τ = 15.
38+
τ = 15.0
4039
delay_trigger_affect! = function (integrator, rng)
41-
append!(integrator.de_chan[1], τ)
40+
append!(integrator.de_chan[1], τ)
4241
end
43-
delay_trigger = Dict(3=>delay_trigger_affect!)
44-
delay_complete = Dict(1=>[2=>-1])
42+
delay_trigger = Dict(3 => delay_trigger_affect!)
43+
delay_complete = Dict(1 => [2 => -1])
4544
delay_affect! = function (integrator, rng)
4645
i = rand(rng, 1:length(integrator.de_chan[1]))
47-
deleteat!(integrator.de_chan[1],i)
46+
deleteat!(integrator.de_chan[1], i)
4847
end
49-
delay_interrupt = Dict(4=>delay_affect!)
50-
delaysets = DelayJumpSet(delay_trigger,delay_complete,delay_interrupt)
51-
djprob = DelayJumpProblem(jumpsys, dprob, aggregatoralgo, delaysets, de_chan0, save_positions = (false, false), save_delay_channel =false)
52-
sol1 =@time solve(djprob, SSAStepper(), seed = 2, saveat =0.1)
53-
54-
48+
delay_interrupt = Dict(4 => delay_affect!)
49+
delaysets = DelayJumpSet(delay_trigger, delay_complete, delay_interrupt)
50+
djprob = DelayJumpProblem(jumpsys, dprob, aggregatoralgo, delaysets, de_chan0,
51+
save_positions = (false, false), save_delay_channel = false)
52+
sol1 = @time solve(djprob, SSAStepper(), seed = 2, saveat = 0.1)
5553

5654
ens_prob = EnsembleProblem(djprob)
5755
Sample_size = Int(10^4)
58-
@time ens = solve(ens_prob, SSAStepper(),EnsembleThreads(),trajectories = Sample_size, saveat = .1)
56+
@time ens = solve(ens_prob, SSAStepper(), EnsembleThreads(), trajectories = Sample_size,
57+
saveat = 0.1)
5958

60-
61-
using Plots, DiffEqBase; theme(:vibrant)
62-
plot(ens[1], label = ["X_A" "X_I"], fmt =:svg)
59+
using Plots, DiffEqBase;
60+
theme(:vibrant);
61+
plot(ens[1], label = ["X_A" "X_I"], fmt = :svg)
6362
# savefig("docs/src/assets/delay_degradation1.svg")
6463

65-
66-
a = β + γ
67-
x_A(t) = C/a*(1-exp(-a*t))
68-
x_I(t)= 0<=t<=τ ? C*β/(a-γ)*((1-exp(-γ*t))/γ - (1-exp(-a*t))/a) : C*β/a*((1-exp(-γ*τ))/γ + exp(-a*t)*(1-exp((a-γ)τ))/(a-γ))
69-
64+
a = β + γ
65+
x_A(t) = C / a * (1 - exp(-a * t))
66+
function x_I(t)
67+
0 <= t <= τ ? C * β / (a - γ) * ((1 - exp(-γ * t)) / γ - (1 - exp(-a * t)) / a) :
68+
C * β / a * ((1 - exp(-γ * τ)) / γ + exp(-a * t) * (1 - exp((a - γ)τ)) / (a - γ))
69+
end
7070

7171
using StatsBase
7272
using Catalyst.EnsembleAnalysis
73-
tsm(t) = Catalyst.EnsembleAnalysis.timepoint_mean(ens,t)
73+
tsm(t) = Catalyst.EnsembleAnalysis.timepoint_mean(ens, t)
7474

7575
mean_A(t) = tsm(t)[1]
7676
mean_I(t) = tsm(t)[2]
7777
timestamps = 0:0.1:tf
78-
plot(timestamps,x_A.(timestamps),linewidth=3, label = "X_A Exact", xlabel = "Time", ylabel = "# of X_A")
79-
plot!(timestamps,mean_A.(timestamps),linewidth=3,line=:dash, label = "X_A SSA")
80-
plot!(timestamps,x_I.(timestamps),linewidth=3, label = "X_I Exact")
81-
plot!(timestamps,mean_I.(timestamps),linewidth=3,line=:dash, legend = :topleft, label = "X_I SSA")
78+
plot(timestamps, x_A.(timestamps), linewidth = 3, label = "X_A Exact", xlabel = "Time",
79+
ylabel = "# of X_A")
80+
plot!(timestamps, mean_A.(timestamps), linewidth = 3, line = :dash, label = "X_A SSA")
81+
plot!(timestamps, x_I.(timestamps), linewidth = 3, label = "X_I Exact")
82+
plot!(timestamps, mean_I.(timestamps), linewidth = 3, line = :dash, legend = :topleft,
83+
label = "X_I SSA")
8284
# savefig("docs/src/assets/delay_degradation2.svg")
83-
84-

0 commit comments

Comments
 (0)