Skip to content

Commit 023f213

Browse files
committed
update
1 parent 18bc7ea commit 023f213

File tree

5 files changed

+79
-34
lines changed

5 files changed

+79
-34
lines changed

docs/src/api/catalyst_api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ accessor functions.
155155
```@docs
156156
species
157157
nonspecies
158+
noise_scaling_parameters
158159
reactionparams
159160
reactions
160161
numspecies

docs/src/catalyst_applications/advanced_simulations.md

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ tspan = (0.0, 10.0)
306306
p_1 = [:k1 => 1.0, :k2 => 1.0]
307307
308308
sprob_1 = SDEProblem(rn_1, u0, tspan, p_1)
309-
sol_1 = solve(sprob_1)
309+
sol_1 = solve(sprob_1, ImplicitEM())
310310
plot(sol_1; idxs = :X1, ylimit = (0.0, 20.0))
311311
```
312312
Here we can see that the `X` concentration fluctuates around a steady state of $X≈10.0$.
@@ -319,46 +319,76 @@ Setting $η<1.0$ will reduce noise and $η>1.0$ will increase noise. The syntax
319319
for setting a noise scaling parameter `η` is
320320
```@example ex3
321321
rn_2 = @reaction_network begin
322-
@parameters η
322+
@noise_scaling_parameters η
323323
(k1,k2), X1 <--> X2
324324
end
325+
```
326+
The `@noise_scaling_parameter` option is equivalent to the `@parameters` option, but also designates subsequent parameter as a *noise scaling parameter*. *η* becomes a parameter of the system, and we can now modualte its value to scale simualtion noise:
327+
```@example ex3
325328
u0 = [:X1 => 10.0, :X2 => 10.0]
326329
tspan = (0.0, 10.0)
327330
p_2 = [:k1 => 1.0, :k2 => 1.0, :η => 0.1]
328331
329-
sprob_2 = SDEProblem(rn_2, u0, tspan, p_2; noise_scaling = (@parameters η)[1])
330-
```
331-
Here, we first need to add `η` as a parameter to the system using the
332-
`@parameters η` option. Next, we pass the `noise_scaling = (@parameters η)[1]`
333-
argument to the `SDEProblem`. We can now simulate our system and confirm that
334-
noise is reduced:
335-
```@example ex3
336-
sol_2 = solve(sprob_2)
332+
sprob_2 = SDEProblem(rn_2, u0, tspan, p_2)
333+
sol_2 = solve(sprob_2, ImplicitEM())
337334
plot(sol_2; idxs = :X1, ylimit = (0.0, 20.0))
338335
```
339336

340-
Finally, it is possible to set individual noise scaling parameters for each
341-
reaction of the system. Our model has two reactions (`X1 --> X2` and `X2 -->
342-
X1`) so we will use two noise scaling parameters, `η1` and `η2`. We use the
343-
following syntax:
337+
It is worth noting that in the CLE, nosie is tied to *reactions* (and not species, which is a common missperception). If only a single noise scaling parameter is given, it will scale the noise for all reaction. However, it is also possible to set several nosie scaling parameters, with each scaling the noise of a single reaction. Our model has two reactions (`X1 --> X2` and `X2 --> X1`) so we will use two noise scaling parameters (`η1` and `η2`):
344338
```@example ex3
345339
rn_3 = @reaction_network begin
346-
@parameters η1 η2
340+
@noise_scaling_parameters η1 η2
347341
(k1,k2), X1 <--> X2
348342
end
349343
u0 = [:X1 => 10.0, :X2 => 10.0]
350344
tspan = (0.0, 10.0)
351345
p_3 = [:k1 => 1.0, :k2 => 1.0, :η1 => 0.1, :η2 => 1.0]
352346
353-
sprob_3 = SDEProblem(rn_3, u0, tspan, p_3; noise_scaling = @parameters η1 η2)
347+
sprob_3 = SDEProblem(rn_3, u0, tspan, p_3)
354348
```
355-
plotting the results, we see that we have less fluctuation than for the first
356-
simulation, but more as compared to the second one (which is as expected):
349+
Both the noise scaling parameters and the reaction are ordered (these orders can be seen by calling `reactions(rn_3)` and `noise_scaling_parameters(rn_3)`, respectively). The i'th noise scaling parameter scales the noise of the i'th reaction. Plotting the results, we see that we have less fluctuation than for the first simulation, but more as compared to the second one (which is as expected):
357350
```@example ex3
358-
sol_3 = solve(sprob_3)
351+
sol_3 = solve(sprob_3, ImplicitEM())
359352
plot(sol_3; idxs = :X1, ylimit = (0.0, 20.0))
360353
```
361354

355+
For systems with many reactions, the `η[1:n]` (where `n` is equal to the number of reactions) notation can be useful (this however, requires `@unpack`'ing the system's parameters):
356+
```@example ex3
357+
rn_4 = @reaction_network begin
358+
@noise_scaling_parameters η[1:6]
359+
(p, d), 0 <--> X
360+
(p, d), 0 <--> Y
361+
(p, d), 0 <--> Z
362+
end
363+
@unpack p, d, η = rn_4
364+
365+
u0_4 = [:X => 10.0, :Y => 10.0, :Z => 10.0]
366+
tspan = (0.0, 10.0)
367+
p_4 = [p => 10.0, d => 1., η[1]=>0.1, η[2]=>0.1, η[3]=>1., η[4]=>1., η[5]=>1., η[6]=>1.]
368+
369+
sprob_4 = SDEProblem(rn_4, u0_4, tspan, p_4)
370+
sol_4 = solve(sprob_4, ImplicitEM())
371+
plot(sol_4; ylimit = (0.0, 20.0))
372+
```
373+
Here, we have reduced the noise of the reactions involving `X` only.
374+
375+
Finally, it is possible to use the `@noise_scaling_parameter` option as a normal macro when creating reaction systems programmatically:
376+
```@example ex3
377+
@variables t
378+
@species X1(t) X2(t)
379+
@noise_scaling_parameters η
380+
@parameters k1 k2
381+
r1 = Reaction(k1,[X1],[X2],[1],[1])
382+
r2 = Reaction(k2,[X2],[X1],[1],[1])
383+
@named rn_5 = ReactionSystem([r1, r2], t, [X1, X2], [k1, k2, η])
384+
nothing # hide
385+
```
386+
which is equivalent to `rn_2`. In this example, calling `@noise_scaling_parameters η` is equivalent to calling `parameters η` with the `noise_scaling_parameter` metadata:
387+
```@example ex3
388+
@parameters η [noise_scaling_parameter=true]
389+
nothing # hide
390+
```
391+
362392
## Useful plotting options
363393
Catalyst, just like DifferentialEquations, uses the Plots package for all
364394
plotting. For a detailed description of differential equation plotting, see

src/Catalyst.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export mm, mmr, hill, hillr, hillar
8585

8686
# functions to query network properties
8787
include("networkapi.jl")
88-
export species, nonspecies, reactionparams, reactions, speciesmap, paramsmap
88+
export species, nonspecies, noise_scaling_parameters, reactionparams, reactions, speciesmap, paramsmap
8989
export numspecies, numreactions, numreactionparams, setdefaults!, symmap_to_varmap
9090
export make_empty_network, addspecies!, addparam!, addreaction!, reactionparamsmap
9191
export dependants, dependents, substoichmat, prodstoichmat, netstoichmat

src/networkapi.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ function nonspecies(network)
4545
unknowns(network)[(numspecies(network) + 1):end]
4646
end
4747

48+
"""
49+
noise_scaling_parameters(network)
50+
51+
Given a [`ReactionSystem`](@ref), return a vector of all noise scaling parameters defined in the system.
52+
"""
53+
function noise_scaling_parameters(network)
54+
filter(is_noise_scaling_parameter, parameters(network))
55+
end
56+
4857
"""
4958
reactionparams(network)
5059

test/model_simulation/simulate_SDEs.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ end
121121

122122
### Checks Simulations Don't Error ###
123123

124-
#Tries to create a large number of problem, ensuring there are no errors (cannot solve as solution likely to go into negatives).
124+
# Tries to create a large number of problem, ensuring there are no errors (cannot solve as solution likely to go into negatives).
125125
let
126126
for reaction_network in reaction_networks_all
127127
for factor in [1e-2, 1e-1, 1e0, 1e1]
@@ -160,21 +160,23 @@ end
160160
# Tests using default values for noise scaling.
161161
# Tests when reaction system is created programmatically.
162162
# Tests @noise_scaling_parameters macro.
163-
let
164-
@variables t
165-
@species X1(t) X2(t)
166-
@noise_scaling_parameters η=0.0
167-
@parameters k1 k2
168-
r1 = Reaction(k1,[X1],[X2],[1],[1])
169-
r2 = Reaction(k2,[X2],[X1],[1],[1])
170-
@named noise_scaling_network = ReactionSystem([r1, r2], t, [X1, X2], [k1, k2, η])
171-
172-
u0 = [:X1 => 1100.0, :X2 => 3900.0]
173-
p = [:k1 => 2.0, :k2 => 0.5, =>0.0]
174-
@test SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p)[] == 0.0
175-
end
163+
let
164+
η_stored =
165+
@variables t
166+
@species X1(t) X2(t)
167+
ηs = @noise_scaling_parameters $(η_stored)=0.0
168+
@parameters k1 k2
169+
r1 = Reaction(k1,[X1],[X2],[1],[1])
170+
r2 = Reaction(k2,[X2],[X1],[1],[1])
171+
@named noise_scaling_network = ReactionSystem([r1, r2], t, [X1, X2], [k1, k2, ηs[1]])
172+
173+
u0 = [:X1 => 1100.0, :X2 => 3900.0]
174+
p = [:k1 => 2.0, :k2 => 0.5, =>0.0]
175+
@test SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p)[] == 0.0
176+
end
176177

177178
# Complicated test with many combinations of options.
179+
# Tests the noise_scaling_parameters getter.
178180
let
179181
noise_scaling_network = @reaction_network begin
180182
@parameters k1 par1 [description="Parameter par1"] par2 η1 [noise_scaling_parameter=true]
@@ -190,6 +192,9 @@ let
190192

191193
sprob = SDEProblem(noise_scaling_network, u0, (0.0, 1000.0), p)
192194
@test sprob[:η1] == sprob[:η2] == sprob[:η3] == sprob[:η4] == 0.0
195+
196+
@unpack η1, η2, η3, η4 = noise_scaling_network
197+
isequal([η1, η2, η3, η4], noise_scaling_parameters(noise_scaling_network))
193198
end
194199

195200

0 commit comments

Comments
 (0)