Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Gillespie Algorithm

Random.TaskLocalRNG()

Do-it-yourself

Stochastic chemical reaction: Gillespie Algorithm (direct and first reaction method) Adapted from: Chemical and Biomedical Engineering Calculations Using Python Ch.4-3

ssa_alg (generic function with 1 method)

Propensity model for this example reaction. Reaction of A <-> B with rate constants k1 & k2

model (generic function with 1 method)
(k1 = 1.0, k2 = 0.5)

Stoichiometry for each reaction

2-element Vector{Vector{Int64}}: [-1, 1] [1, -1]

Initial conditions (Usually discrete values)

2-element Vector{Int64}: 200 0

Simulation time

10.0

Solve the problem using both direct and first reaction method

  0.436968 seconds (681.02 k allocations: 51.816 MiB, 98.73% compilation time)
  0.006292 seconds (16.34 k allocations: 14.937 MiB)
(t = [0.0, 0.006874672879606227, 0.0070050915014185, 0.01329115182676454, 0.015335530116774018, 0.01942319769041779, 0.021346508038833215, 0.02345030020299408, 0.025497753122539372, 0.02734483697039726 … 9.937773197389458, 9.938360255993572, 9.964450740930666, 9.96564917153764, 9.975440668199402, 9.978594329657025, 9.982318425245289, 9.987213207313044, 9.995833233887383, 10.024199143896238], u = [200 0; 199 1; … ; 75 125; 74 126])

Plot the solution from the direct method

Plot{Plots.GRBackend() n=2}

Plot the solution by the first reaction method

Plot{Plots.GRBackend() n=2}

Running 50 simulations serially TODO: parallelize this loop using Threads.@threads or Distributed for better performance

  0.302136 seconds (744.82 k allocations: 752.091 MiB, 31.13% gc time, 22.88% compilation time)

Average values and interpolation

b_avg (generic function with 1 method)

Plot the solution

Plot{Plots.GRBackend() n=102}

This notebook was generated using Literate.jl.