Skip to content

Commit 251eea3

Browse files
committed
complete jsys
1 parent 15610c0 commit 251eea3

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

docs/src/assets/Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
55
Catalyst = "479239e8-5488-4da2-87a7-35f2df7eef83"
66
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
77
DiffEqParamEstim = "1130ab10-4a5a-5621-a13d-e4788d82bd4c"
8-
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
98
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
109
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1110
DynamicalSystems = "61744808-ddfa-5f27-97ff-6e42cc95d634"
@@ -38,7 +37,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3837

3938
[compat]
4039
BenchmarkTools = "1.5"
41-
BifurcationKit = "0.3"
40+
BifurcationKit = "0.3.4"
4241
CairoMakie = "0.12"
4342
Catalyst = "13"
4443
DataFrames = "1.6"

docs/src/model_creation/examples/smoluchowski_coagulation_equation.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ This tutorial shows how to programmatically construct a [`ReactionSystem`](@ref)
44
The Smoluchowski coagulation equation describes a system of reactions in which monomers may collide to form dimers, monomers and dimers may collide to form trimers, and so on. This models a variety of chemical/physical processes, including polymerization and flocculation.
55

66
We begin by importing some necessary packages.
7-
```julia
7+
```@example smcoag1
88
using ModelingToolkit, Catalyst, LinearAlgebra
99
using JumpProcesses
1010
using Plots, SpecialFunctions
1111
```
1212
Suppose the maximum cluster size is `N`. We assume an initial concentration of monomers, `Nₒ`, and let `uₒ` denote the initial number of monomers in the system. We have `nr` total reactions, and label by `V` the bulk volume of the system (which plays an important role in the calculation of rate laws since we have bimolecular reactions). Our basic parameters are then
13-
```julia
13+
```@example smcoag1
1414
# maximum cluster size
1515
N = 10
1616
@@ -29,12 +29,13 @@ n = floor(Int, N / 2)
2929
3030
# No. of forward reactions
3131
nr = ((N % 2) == 0) ? (n*(n + 1) - n) : (n*(n + 1))
32+
nothing #hide
3233
```
3334
The [Smoluchowski coagulation equation](https://en.wikipedia.org/wiki/Smoluchowski_coagulation_equation) Wikipedia page illustrates the set of possible reactions that can occur. We can easily enumerate the `pair`s of multimer reactants that can combine when allowing a maximal cluster size of `N` monomers. We initialize the volumes of the reactant multimers as `volᵢ` and `volⱼ`
3435

35-
```julia
36+
```@example smcoag1
3637
# possible pairs of reactant multimers
37-
pair = Int[]
38+
pair = []
3839
for i = 2:N
3940
halfi = floor(Int, i/2)
4041
push!(pair, [(1:halfi) (i .- (1:halfi))])
@@ -45,9 +46,10 @@ vⱼ = @view pair[:, 2] # Reactant 2 indices
4546
volᵢ = Vₒ * vᵢ # cm⁻³
4647
volⱼ = Vₒ * vⱼ # cm⁻³
4748
sum_vᵢvⱼ = @. vᵢ + vⱼ # Product index
49+
nothing #hide
4850
```
4951
We next specify the rates (i.e. kernel) at which reactants collide to form products. For simplicity, we allow a user-selected additive kernel or constant kernel. The constants(`B` and `C`) are adopted from Scott's paper [2](https://journals.ametsoc.org/view/journals/atsc/25/1/1520-0469_1968_025_0054_asocdc_2_0_co_2.xml)
50-
```julia
52+
```@example smcoag1
5153
# set i to 1 for additive kernel, 2 for constant
5254
i = 1
5355
if i == 1
@@ -61,7 +63,7 @@ elseif i==2
6163
end
6264
```
6365
We'll set the parameters and the initial condition that only monomers are present at ``t=0`` in `u₀map`.
64-
```julia
66+
```@example smcoag1
6567
# k is a vector of the parameters, with values given by the vector kv
6668
@parameters k[1:nr] = kv
6769
@@ -80,9 +82,10 @@ end
8082
u₀ = zeros(Int64, N)
8183
u₀[1] = uₒ
8284
u₀map = Pair.(collect(X), u₀) # map species to its initial value
85+
nothing #hide
8386
```
8487
Here we generate the reactions programmatically. We systematically create Catalyst `Reaction`s for each possible reaction shown in the figure on [Wikipedia](https://en.wikipedia.org/wiki/Smoluchowski_coagulation_equation). When `vᵢ[n] == vⱼ[n]`, we set the stoichiometric coefficient of the reactant multimer to two.
85-
```julia
88+
```@example smcoag1
8689
# vector to store the Reactions in
8790
rx = []
8891
for n = 1:nr
@@ -98,15 +101,15 @@ end
98101
rs = complete(rs)
99102
```
100103
We now convert the [`ReactionSystem`](@ref) into a `ModelingToolkit.JumpSystem`, and solve it using Gillespie's direct method. For details on other possible solvers (SSAs), see the [DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/types/jump_types/) documentation
101-
```julia
104+
```@example smcoag1
102105
# solving the system
103-
jumpsys = convert(JumpSystem, rs)
104-
dprob = DiscreteProblem(jumpsys, u₀map, tspan)
106+
jumpsys = complete(convert(JumpSystem, rs))
107+
dprob = DiscreteProblem(rs, u₀map, tspan)
105108
jprob = JumpProblem(jumpsys, dprob, Direct(), save_positions = (false, false))
106109
jsol = solve(jprob, SSAStepper(), saveat = tspan[2] / 30)
107110
```
108111
Lets check the results for the first three polymers/cluster sizes. We compare to the analytical solution for this system:
109-
```julia
112+
```@example smcoag1
110113
# Results for first three polymers...i.e. monomers, dimers and trimers
111114
v_res = [1; 2; 3]
112115

0 commit comments

Comments
 (0)