Skip to content

Commit 62b8004

Browse files
committed
fix parametric stoich tutorial
1 parent 8c9b4be commit 62b8004

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

docs/src/model_creation/parametric_stoichiometry.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ revsys = @reaction_network revsys begin
1515
end
1616
reactions(revsys)
1717
```
18-
Note, as always the `@reaction_network` macro defaults to setting all symbols
18+
Notice, as described in the [Reaction rate laws used in simulations](@ref)
19+
section, the default rate laws involve factorials in the stoichiometric
20+
coefficients. For this reason we explicitly specify `m` and `n` as integers (as
21+
otherwise ModelingToolkit will implicitly assume they are floating point).
22+
23+
As always the `@reaction_network` macro defaults to setting all symbols
1924
neither used as a reaction substrate nor a product to be parameters. Hence, in
2025
this example we have two species (`A` and `B`) and four parameters (`k₊`, `k₋`,
2126
`m`, and `n`). In addition, the stoichiometry is applied to the rightmost symbol
@@ -37,21 +42,21 @@ We could have equivalently specified our systems directly via the Catalyst
3742
API. For example, for `revsys` we would could use
3843
```@example s1
3944
t = default_t()
40-
@parameters k₊ k₋ m::Int n
45+
@parameters k₊ k₋ m::Int n::Int
4146
@species A(t), B(t)
4247
rxs = [Reaction(k₊, [A], [B], [m], [m*n]),
4348
Reaction(k₋, [B], [A])]
4449
revsys2 = ReactionSystem(rxs,t; name=:revsys)
4550
revsys2 == revsys
4651
```
47-
which can be simplified using the `@reaction` macro to
52+
or
4853
```@example s1
49-
rxs2 = [(@reaction k₊, m*A --> (m*n)*B),
54+
rxs2 = [(@reaction k₊, $m*A --> ($m*$n)*B),
5055
(@reaction k₋, B --> A)]
5156
revsys3 = ReactionSystem(rxs2,t; name=:revsys)
5257
revsys3 == revsys
5358
```
54-
Note, the `@reaction` macro again assumes all symbols are parameters except the
59+
Here we interpolate in the pre-declared `m` and `n` symbolic variables using `$m` and `$n` to ensure the parameter is known to be integer-valued. The `@reaction` macro again assumes all symbols are parameters except the
5560
substrates or reactants (i.e. `A` and `B`). For example, in
5661
`@reaction k, F*A + 2(H*G+B) --> D`, the substrates are `(A,G,B)` with
5762
stoichiometries `(F,2*H,2)`.
@@ -63,10 +68,6 @@ osys = complete(osys)
6368
equations(osys)
6469
show(stdout, MIME"text/plain"(), equations(osys)) # hide
6570
```
66-
Notice, as described in the [Reaction rate laws used in simulations](@ref)
67-
section, the default rate laws involve factorials in the stoichiometric
68-
coefficients. For this reason we must specify `m` and `n` as integers, and hence
69-
*use a tuple for the parameter mapping*
7071
```@example s1
7172
p = (k₊ => 1.0, k₋ => 1.0, m => 2, n => 2)
7273
u₀ = [A => 1.0, B => 1.0]
@@ -78,8 +79,6 @@ We can now solve and plot the system
7879
sol = solve(oprob, Tsit5())
7980
plot(sol)
8081
```
81-
*If we had used a vector to store parameters, `m` and `n` would be converted to
82-
floating point giving an error when solving the system.* **Note, currently a [bug](https://github.com/SciML/ModelingToolkit.jl/issues/2296) in ModelingToolkit has broken this example by converting to floating point when using tuple parameters, see the alternative approach below for a workaround.**
8382

8483
An alternative approach to avoid the issues of using mixed floating point and
8584
integer variables is to disable the rescaling of rate laws as described in
@@ -89,6 +88,11 @@ directly building the problem from a `ReactionSystem` instead of first
8988
converting to an `ODESystem`). For the previous example this gives the following
9089
(different) system of ODEs
9190
```@example s1
91+
revsys = @reaction_network revsys begin
92+
@parameters m::Int64 n::Int64
93+
k₊, m*A --> (m*n)*B
94+
k₋, B --> A
95+
end
9296
osys = convert(ODESystem, revsys; combinatoric_ratelaws = false)
9397
osys = complete(osys)
9498
equations(osys)

0 commit comments

Comments
 (0)