Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DelayCoevolve #29

Merged
merged 11 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DelaySSAToolkit"
uuid = "d39380a9-d0d7-4ae6-ae63-0e8d8b7efa9e"
authors = ["Xiaoming Fu"]
version = "0.2.3"
version = "0.2.4"

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

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

[targets]
test = ["Test"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
|:-----------------:|:----------------:|:----------------:|
| [![doc dev badge]][doc dev link] | [![ci badge]][ci link] [![cov badge]][cov link] | [![download badge]][download link]| -->

| **Documentation** | **Build Status** |
|:-----------------:|:----------------:|
| [![doc dev badge]][doc dev link] | [![ci badge]][ci link] [![cov badge]][cov link] |
| **Documentation** | **Build Status** | **Code Style** |
|:-----------------:|:----------------:|:----------------:|
| [![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)

[doc dev badge]: https://img.shields.io/badge/docs-dev-blue.svg
[doc dev link]: https://palmtree2013.github.io/DelaySSAToolkit.jl/dev/
Expand Down
66 changes: 32 additions & 34 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
using DelaySSAToolkit
using Documenter

DocMeta.setdocmeta!(DelaySSAToolkit, :DocTestSetup, :(using DelaySSAToolkit); recursive=true)
DocMeta.setdocmeta!(DelaySSAToolkit, :DocTestSetup, :(using DelaySSAToolkit);
recursive = true)

makedocs(;
modules=[DelaySSAToolkit],
authors="Xiaoming Fu",
repo="https://github.com/palmtree2013/DelaySSAToolkit.jl/blob/{commit}{path}#{line}",
sitename="DelaySSAToolkit.jl",
format=Documenter.HTML(;
mathengine=Documenter.Writers.HTMLWriter.MathJax2(),
prettyurls=get(ENV, "CI", "false") == "true",
canonical="https://palmtree2013.github.io/DelaySSAToolkit.jl",
assets=String[],
),
pages=[
"Home" => "index.md",
"Tutorials" => [
"tutorials/tutorials.md",
"tutorials/bursty.md",
"tutorials/delay_degradation.md",
"tutorials/heterogeneous_delay.md",
"tutorials/delay_oscillator.md",
"tutorials/stochastic_delay.md",
],
"Algorithm" => [
"algorithms/notations.md",
"algorithms/delayrejection.md",
"algorithms/delaydirect.md",
"algorithms/delaymnrm.md",
],
"Theory" => "theory.md",
"API" => "api.md",
],
)
modules = [DelaySSAToolkit],
authors = "Xiaoming Fu",
repo = "https://github.com/palmtree2013/DelaySSAToolkit.jl/blob/{commit}{path}#{line}",
sitename = "DelaySSAToolkit.jl",
format = Documenter.HTML(;
mathengine = Documenter.Writers.HTMLWriter.MathJax2(),
prettyurls = get(ENV, "CI", "false") == "true",
canonical = "https://palmtree2013.github.io/DelaySSAToolkit.jl",
assets = String[]),
pages = [
"Home" => "index.md",
"Tutorials" => [
"tutorials/tutorials.md",
"tutorials/bursty.md",
"tutorials/delay_degradation.md",
"tutorials/heterogeneous_delay.md",
"tutorials/delay_oscillator.md",
"tutorials/stochastic_delay.md",
],
"Algorithm" => [
"algorithms/notations.md",
"algorithms/delayrejection.md",
"algorithms/delaydirect.md",
"algorithms/delaymnrm.md",
],
"Theory" => "theory.md",
"API" => "api.md",
])

deploydocs(;
repo="github.com/palmtree2013/DelaySSAToolkit.jl",
devbranch="main",
)
repo = "github.com/palmtree2013/DelaySSAToolkit.jl",
devbranch = "main")
4 changes: 2 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ DelaySSAToolkit.jl is a tool developed on top of [JumpProcesses.jl](https://gith

DelaySSAToolkit can be installed through the Julia package manager:
```julia
]add DelaySSAToolkit
using DelaySSAToolkit
using Pkg
Pkg.add("DelaySSAToolkit")
```
and you might need to run
```julia
Expand Down
3 changes: 0 additions & 3 deletions docs/src/tutorials/heterogeneous_delay.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ ensprob2 = EnsembleProblem(jprob, prob_func = prob_func)
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:
```julia-repl
julia> @time ens2 = solve(ensprob2, SSAStepper(), EnsembleThreads(), trajectories = 10^4)

78.925908 seconds (249.65 M allocations: 28.632 GiB, 6.60% gc time)
EnsembleSolution Solution of length 10000 with uType
```
Here we plot the histogram of the number of unfinished reactant $X$s in the delay channel.

Expand Down
54 changes: 29 additions & 25 deletions examples/bursty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,70 @@ begin # construct reaction network
@parameters a b t
@variables X(t)
burst_sup = 30
rxs = [Reaction(a*b^i/(1+b)^(i+1),nothing,[X],nothing,[i]) for i in 1:burst_sup]
rxs = [Reaction(a * b^i / (1 + b)^(i + 1), nothing, [X], nothing, [i])
for i in 1:burst_sup]
rxs = vcat(rxs)
@named rs = ReactionSystem(rxs,t,[X],[a,b])
@named rs = ReactionSystem(rxs, t, [X], [a, b])
end

# convert the ReactionSystem to a JumpSystem
jumpsys = convert(JumpSystem, rs, combinatoric_ratelaws=false)
jumpsys = convert(JumpSystem, rs, combinatoric_ratelaws = false)

u0 = [0]
de_chan0 = [[]]
tf = 200.
tspan = (0,tf)
tf = 200.0
tspan = (0, tf)
ps = [0.0282, 3.46]
dprob = DiscreteProblem(jumpsys,u0,tspan,ps)
τ = 130.
dprob = DiscreteProblem(jumpsys, u0, tspan, ps)
τ = 130.0

delay_trigger_affect! = []
for i in 1:burst_sup
push!(delay_trigger_affect!, function (integrator, rng)
append!(integrator.de_chan[1], fill(τ, i))
end)
append!(integrator.de_chan[1], fill(τ, i))
end)
end
delay_trigger_affect!
delay_trigger = Dict([Pair(i, delay_trigger_affect![i]) for i in 1:burst_sup])
delay_complete = Dict(1=>[1=>-1])
delay_complete = Dict(1 => [1 => -1])
delay_interrupt = Dict()
alg = DelayRejection()
# alg = DelayRejection()
alg = DelayCoevolve()

delayjumpset = DelayJumpSet(delay_trigger, delay_complete, delay_interrupt)
jprob = DelayJumpProblem(jumpsys, dprob, alg, delayjumpset, de_chan0, save_positions=(true, true), save_delay_channel = false)
jprob = DelayJumpProblem(jumpsys, dprob, alg, delayjumpset, de_chan0,
save_positions = (true, true), save_delay_channel = false)
# saveat = 0:1:tf
seed = 4
@time sol = solve(jprob, SSAStepper(), seed = seed)


ensprob = EnsembleProblem(jprob)
@time ens = solve(ensprob, SSAStepper(), EnsembleSerial(), trajectories=1e5)
@time ens = solve(ensprob, SSAStepper(), EnsembleSerial(), trajectories = 1e5)

# Check with the exact probability distribution
using TaylorSeries
function taylor_coefficients(NT::Int,at_x,gen::Function)
function taylor_coefficients(NT::Int, at_x, gen::Function)
Q = zeros(NT)
taylor_gen = taylor_expand(u->gen(u),at_x,order=NT)
taylor_gen = taylor_expand(u -> gen(u), at_x, order = NT)
for j in 1:NT
Q[j] = taylor_gen[j-1]
Q[j] = taylor_gen[j - 1]
end
return Q
end
function delay_bursty(params,NT::Int)
function delay_bursty(params, NT::Int)
a, b, τ, t = params
gen1(u) = exp(a*b*min(τ,t)*u/(1-b*u))
taylor_coefficients(NT,-1,gen1)
gen1(u) = exp(a * b * min(τ, t) * u / (1 - b * u))
taylor_coefficients(NT, -1, gen1)
end


using Catalyst.EnsembleAnalysis
using Plots; theme(:vibrant)
using Plots;
theme(:vibrant);
sol_end = componentwise_vectors_timepoint(ens, tf)
histogram(sol_end,bins=0:1:60,normalize=:pdf, label = "Delay SSA",fillalpha = 0.6, linecolor = :orange)
histogram(sol_end, bins = 0:1:60, normalize = :pdf, label = "Delay SSA", fillalpha = 0.6,
linecolor = :orange)

sol_exact = delay_bursty([ps;130;200], 61)
fig = plot!(0:60,sol_exact, linewidth = 3, label = "Exact solution", fmt=:svg, xlabel = "# of products", ylabel = "Probability")
sol_exact = delay_bursty([ps; 130; 200], 61)
fig = plot!(0:60, sol_exact, linewidth = 3, label = "Exact solution", fmt = :svg,
xlabel = "# of products", ylabel = "Probability")
# savefig(fig, "docs/src/assets/bursty.svg")
72 changes: 36 additions & 36 deletions examples/delay_degradation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,75 @@
# 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-γ))
# where a = β + γ


using Catalyst
using DelaySSAToolkit
using DelaySSAToolkit

rn = @reaction_network begin
C, 0 --> Xₐ
γ, Xₐ --> 0
β, Xₐ --> Xᵢ
γ, Xᵢ --> 0
C, 0 --> Xₐ
γ, Xₐ --> 0
β, Xₐ --> Xᵢ
γ, Xᵢ --> 0
end C γ β

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

u0 = [0, 0]
tf = 30
saveat = .1
saveat = 0.1
de_chan0 = [[]]
C, γ, β = [2., 0.1, 0.5]
C, γ, β = [2.0, 0.1, 0.5]
p = [C, γ, β]
tspan = (0.,tf)
tspan = (0.0, tf)
# aggregatoralgo = DelayRejection()
# aggregatoralgo = DelayMNRM()
aggregatoralgo = DelayDirect()
# aggregatoralgo = DelayDirectCR()
dprob = DiscreteProblem(u0, tspan, p)

τ = 15.
τ = 15.0
delay_trigger_affect! = function (integrator, rng)
append!(integrator.de_chan[1], τ)
append!(integrator.de_chan[1], τ)
end
delay_trigger = Dict(3=>delay_trigger_affect!)
delay_complete = Dict(1=>[2=>-1])
delay_trigger = Dict(3 => delay_trigger_affect!)
delay_complete = Dict(1 => [2 => -1])
delay_affect! = function (integrator, rng)
i = rand(rng, 1:length(integrator.de_chan[1]))
deleteat!(integrator.de_chan[1],i)
deleteat!(integrator.de_chan[1], i)
end
delay_interrupt = Dict(4=>delay_affect!)
delaysets = DelayJumpSet(delay_trigger,delay_complete,delay_interrupt)
djprob = DelayJumpProblem(jumpsys, dprob, aggregatoralgo, delaysets, de_chan0, save_positions = (false, false), save_delay_channel =false)
sol1 =@time solve(djprob, SSAStepper(), seed = 2, saveat =0.1)


delay_interrupt = Dict(4 => delay_affect!)
delaysets = DelayJumpSet(delay_trigger, delay_complete, delay_interrupt)
djprob = DelayJumpProblem(jumpsys, dprob, aggregatoralgo, delaysets, de_chan0,
save_positions = (false, false), save_delay_channel = false)
sol1 = @time solve(djprob, SSAStepper(), seed = 2, saveat = 0.1)

ens_prob = EnsembleProblem(djprob)
Sample_size = Int(10^4)
@time ens = solve(ens_prob, SSAStepper(),EnsembleThreads(),trajectories = Sample_size, saveat = .1)
@time ens = solve(ens_prob, SSAStepper(), EnsembleThreads(), trajectories = Sample_size,
saveat = 0.1)


using Plots, DiffEqBase; theme(:vibrant)
plot(ens[1], label = ["X_A" "X_I"], fmt =:svg)
using Plots, DiffEqBase;
theme(:vibrant);
plot(ens[1], label = ["X_A" "X_I"], fmt = :svg)
# savefig("docs/src/assets/delay_degradation1.svg")


a = β + γ
x_A(t) = C/a*(1-exp(-a*t))
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-γ))

a = β + γ
x_A(t) = C / a * (1 - exp(-a * t))
function 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 - γ))
end

using StatsBase
using Catalyst.EnsembleAnalysis
tsm(t) = Catalyst.EnsembleAnalysis.timepoint_mean(ens,t)
tsm(t) = Catalyst.EnsembleAnalysis.timepoint_mean(ens, t)

mean_A(t) = tsm(t)[1]
mean_I(t) = tsm(t)[2]
timestamps = 0:0.1:tf
plot(timestamps,x_A.(timestamps),linewidth=3, label = "X_A Exact", xlabel = "Time", ylabel = "# of X_A")
plot!(timestamps,mean_A.(timestamps),linewidth=3,line=:dash, label = "X_A SSA")
plot!(timestamps,x_I.(timestamps),linewidth=3, label = "X_I Exact")
plot!(timestamps,mean_I.(timestamps),linewidth=3,line=:dash, legend = :topleft, label = "X_I SSA")
plot(timestamps, x_A.(timestamps), linewidth = 3, label = "X_A Exact", xlabel = "Time",
ylabel = "# of X_A")
plot!(timestamps, mean_A.(timestamps), linewidth = 3, line = :dash, label = "X_A SSA")
plot!(timestamps, x_I.(timestamps), linewidth = 3, label = "X_I Exact")
plot!(timestamps, mean_I.(timestamps), linewidth = 3, line = :dash, legend = :topleft,
label = "X_I SSA")
# savefig("docs/src/assets/delay_degradation2.svg")


Loading