-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegraph_example.jl
132 lines (94 loc) · 3.83 KB
/
telegraph_example.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
# This script produces the data given in sub-section "3.2 The telegraph
# model" of the main text.
# SECTION 1: PREAMBLE
# 1a. Import the necessary packages
using ModelingToolkit
using Catalyst
using DifferentialEquations
using SparseArrays
using FiniteStateProjection
using FiniteStateProjection: singleindices, pairedindices, netstoichmat
# 1b. Import "mfpt_functions.jl" script as follows if they are saved in the
# same directory, otherwise amend the path accordingly
include("mfpt_functions.jl")
# SECTION 2: THE TELEGRAPH MODEL
# parameters & variables are defined in Equation 13 of the main text
@parameters λ, μ, K, δ
@variables t, G, M
model = @reaction_network begin
λ * (1 - G), 0 --> G
μ, G --> 0
K, G --> G + M
δ, M --> 0
end λ μ K δ
# assign parameter values to the model
# by changing the value of λ one can reproduce all three
# plots in Figure 3(c) e.g. λ = (10.0, 1.0, 0.1)
λ, μ, K, δ = 1.0, 20.0, 1000.0, 1.0
params = (λ, μ, K, δ)
# define what fraction of the model's steady-state mean you are
# interested in
frac_ss_mean = 0.8
# SECTION 3: COMPUTING THE DATA IN FIGURE 3
# compute the average time taken to reach some percentage (frac_ss_mean)
# of the steady-state mean in both the stochastic and deterministic regimes
# for increasing values of the parameter μ
μs = collect(0.0:0.1:100.0)
det_wts = []
sto_wts = []
# set the steady-state mean of the system
ss_mean = 100
for μ in μs
params = (λ, μ, K, δ)
# adjust K such that the steady-state mean stays constant for different values of μ
K = ss_mean * ((params[1] + params[2]) / params[1])
params = (λ, μ, K, δ)
# compute 80% of the steady-state mean
target = ss_mean * frac_ss_mean
# compute the deterministic FPT
firsttimeprob_det, firsttimecb_det = let
# Initial conditions
setdefaults!(model, [:G => 0, :M => 0, :λ => params[1], :μ => params[2], :K => params[3], :δ => params[4]])
# Convert to an ODESystem
odesys = convert(ODESystem, model)
# Find its integer index within the solution, u
Sidx = 2
# Condition function to stop the simulation
function MFPT_S_det(u, t, integrator, Sidx = Sidx)
u[Sidx] >= target
end
# An affect! to stop the simulation when the condition is true
function stop_sim(integrator)
savevalues!(integrator, true)
terminate!(integrator)
nothing
end
# Create the callback
cb_det = DiscreteCallback(MFPT_S_det, stop_sim)
# Define the ODEProblem
oprob = ODEProblem(model, [], (0.0, 500.0), params)
oprob, cb_det
end
# solve the ODEProblem and terminate when the callback is reached i.e.
# when 80% of the steady-state mean is achieved
sol = solve(firsttimeprob_det, Rosenbrock23(), adaptive=false, dt=0.001, save_end=false, callback=firsttimecb_det)
det_wt = sol.t[end]
# compute the (stochcastic) MFPT using the functions in "mfpt_functions.jl"
u0 = zeros(size(model.states)[1], Int((round(ss_mean * frac_ss_mean, digits=0))))
sto_wt = fsp_time(model, params, Int((round(ss_mean * frac_ss_mean, digits=0))), 0, u0)[1]
push!(det_wts, det_wt)
push!(sto_wts, sto_wt)
end
# SECTION 4: PLOTTING THE DATA
using LaTeXStrings
using Plots
using Plots.PlotMeasures
msw = 0
ms = 5
p_det_wts = plot(log10.(μs), det_wts; seriestype=:scatter, label="Deterministic",
xlabel=L"log(μ)", ylabel="Waiting time", color=:darkolivegreen,
markerstrokewidth=msw, markersize=ms)
# NOTE: depending on the value of λ = (0.1, 1.0, 10.0) you can change the argument
# "ylims" to best represent the differences in the data
p_stoc_wts = plot!(log10.(μs), sto_wts; seriestype=:scatter, label="Stochastic",
framestyle=:box, color=:coral1, markerstrokewidth=msw, markersize=ms, ylims=(1.5,3.0))