Skip to content

Commit 4e69bd1

Browse files
committed
save progress
1 parent 5f59618 commit 4e69bd1

File tree

1 file changed

+123
-35
lines changed

1 file changed

+123
-35
lines changed

docs/src/model_creation/programmatic_CRN_construction.md

Lines changed: 123 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -149,46 +149,14 @@ oprob = ODEProblem(two_state_model, u0, tspan, ps)
149149
sol = solve(oprob)
150150
plot(sol)
151151
```
152-
While programmatically created models can also have their parameters and species designated using `Symbol`s, the reverse is not possible for DSL-created models. Here, the symbolic variables are never explicitly declared, and thus not available for designating their values. Symbolic designation can be enabled for DSL-created models [by using `@unpack`](@ref programmatic_CRN_construction_symbolic_representation_unpack).
152+
While programmatically created models can also have their parameters and species designated using `Symbol`s, the reverse is not possible for DSL-created models. Here, the symbolic variables are never explicitly declared, and thus not available for designating their values. Symbolic designation can be enabled for DSL-created models [by using `@unpack`](@ref programmatic_CRN_construction_symbolics_and_DSL_unpack).
153153

154154
Elsewhere, we also describe how e.g. `ODEProblem`s and [simulations solutions can be queried for the values of model quantities](@ref simulation_structure_interfacing). There, we use use `Symbol`s to represent model quantities, however, symbolic variables (when available) can again be used. E.g. we can use
155155
```@example programmatic_2
156156
sol[X1]
157157
```
158158
to retrieve $X$'s value across the simulation.
159159

160-
### [Using `@unpack` to extract symbolic variables from `ReactionSystem`s](@id programmatic_CRN_construction_symbolic_representation_unpack)
161-
Let us consider a simple birth-death model created using the DSL:
162-
```@example programmatic_3
163-
using Catalyst # hide
164-
bd_model = @reaction_network begin
165-
(p,d), 0 <--> X
166-
end
167-
nothing # hide
168-
```
169-
Since we have not explicitly declared `p`, `d`, and `X` using `@parameters` and `@species`, we cannot represent these symbolically (only using `Symbol`s). If we wish to do so, however, we can fetch these into our current scope using the `@unpack` macro:
170-
```@example programmatic_3
171-
@unpack p, d, X = bd_model
172-
nothing # hide
173-
```
174-
This lists first the quantities we wish to fetch (does not need to be the model's full set of parameters and species), then `=`, followed by the model name. `p`, `d` and `X` are now symbolic variables in the current scope, just as if they had been declared using `@parameters` or `@species`. We can confirm this:
175-
```@example programmatic_3
176-
X
177-
```
178-
Next, we can now use these to e.g. designate initial conditions and parameter values for model simulations:
179-
```@example programmatic_3
180-
using OrdinaryDiffEq, Plots # hide
181-
u0 = [X => 0.1]
182-
tspan = (0.0, 10.0)
183-
ps = [p => 1.0, d => 0.2]
184-
oprob = ODEProblem(bd_model, u0, tspan, ps)
185-
sol = solve(oprob)
186-
plot(sol)
187-
```
188-
189-
!!! warn
190-
Just like [when using `@parameters` and `@species`](@ref programmatic_CRN_construction_symbolic_variables), `@unpack` will overwrite any variables in the current scope which shares name with the imported quantities.
191-
192160
## [Additional options for declaration of symbolic variables](@id programmatic_CRN_construction_symbolic_variables_options)
193161
The declaration of symbolic variables for programmatic Catalyst modelling uses identical syntax as when [parameters/species are explicitly declared within the DSL](@ref ref), or as used within [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl). Here we will provide a brief summary iterating the same information.
194162

@@ -233,7 +201,7 @@ nothing # hide
233201

234202
If a parameter have a type, a metadata, and a default value, they are designated in the following order:
235203
```@example programmatic_4
236-
TODO: @parameters n(t)::Int64
204+
@parameters n(t)::Int64 = 2 ["Parameter n, which is an integer and defaults to the value 2."]
237205
nothing # hide
238206
```
239207

@@ -260,10 +228,130 @@ end
260228
nothing # hide
261229
```
262230

231+
### [Vector-valued symbolic variables](@id programmatic_CRN_construction_symbolic_variables_options_vectors)
232+
Sometimes, one wish to declare a large number of similar symbolic variables. E.g. if we have a system with ten species, each being produced at different rates, we could declare ten separate production parameters:
233+
```@example programmatic_5
234+
using Catalyst # hide
235+
@parameters p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
236+
```
237+
However, it is also possible to *declare a vector parameter* with ten different values:
238+
```@example programmatic_5
239+
@parameters p[1:10]
240+
```
241+
242+
We can use this to create our two-state model, but instead of declaring `X1`, `X2`, `k1`, and `k2` as separate entities, we create them as two length-two vectors:
243+
```@example programmatic_5
244+
t = default_t()
245+
@species X[1:2](t)
246+
@parameters k[1:2]
247+
nothing # hide
248+
```
249+
Next, we can designate the individual parameters using e.g. `X[1]`
250+
```@example programmatic_5
251+
rxs = [
252+
Reaction(k[1], [X[1]], [X[2]]),
253+
Reaction(k[2], [X[2]], [X[1]]),
254+
]
255+
@named two_state_model = ReactionSystem(rxs, t)
256+
```
257+
Now, while we still can provide individual values to `X[1]`, `X[2]`, `k[1]`, and `k[2]`, we can also declare their values directly as vectors:
258+
```@example programmatic_5
259+
using OrdinaryDiffEq, Plots # hide
260+
u0 = [X => [0.0, 2.0]]
261+
tspan = (0.0, 1.0)
262+
ps = [k => [1.0, 2.0]]
263+
oprob = ODEProblem(two_state_model, u0, tspan, ps)
264+
sol = solve(oprob)
265+
plot(sol)
266+
```
267+
268+
269+
## [Additional options for declaration of `Reaction`s](@id programmatic_CRN_construction_reactions_options)
270+
When describing the DSL, we also describe a range of [options for declaring various types of reactions](@ref ref). Each type of reaction that can be created using the DSL can also be created programmatically. Below, we briefly describe each case.
271+
272+
### [Reactions with ](@id programmatic_CRN_construction_reactions_options_)
273+
274+
### [](@id programmatic_CRN_construction_reactions_options_)
275+
276+
### [](@id programmatic_CRN_construction_reactions_options_)
277+
278+
### [](@id programmatic_CRN_construction_reactions_options_)
263279

264280
## [Additional options for declaration of `Reaction`s](@id programmatic_CRN_construction_reactions_options)
265281

266-
## [Creation of `Reaction`s using the`@reaction` ](@id programmatic_CRN_construction_reaction_macro)
282+
283+
## [Creation of `Reaction`s using the `@reaction`](@id programmatic_CRN_construction_reaction_macro)
284+
285+
## [Working with symbolic variables and the DSL](@id programmatic_CRN_construction_symbolics_and_DSL)
286+
The `@reaction` macro allowed us to use some of DSL-based modellings advantage (easy declaration of reactions) in programmatic modelling. Similarity, there are also ways to utilise concepts from programmatic modelling (like the declaration and use of symbolic variables) in DSL-based modelling. Below we briefly describe two of these.
287+
288+
### [Using `@unpack` to extract symbolic variables from `ReactionSystem`s](@id programmatic_CRN_construction_symbolics_and_DSL_unpack)
289+
Let us consider a simple birth-death model created using the DSL:
290+
```@example programmatic_3
291+
using Catalyst # hide
292+
bd_model = @reaction_network begin
293+
(p,d), 0 <--> X
294+
end
295+
nothing # hide
296+
```
297+
Since we have not explicitly declared `p`, `d`, and `X` using `@parameters` and `@species`, we cannot represent these symbolically (only using `Symbol`s). If we wish to do so, however, we can fetch these into our current scope using the `@unpack` macro:
298+
```@example programmatic_3
299+
@unpack p, d, X = bd_model
300+
nothing # hide
301+
```
302+
This lists first the quantities we wish to fetch (does not need to be the model's full set of parameters and species), then `=`, followed by the model name. `p`, `d` and `X` are now symbolic variables in the current scope, just as if they had been declared using `@parameters` or `@species`. We can confirm this:
303+
```@example programmatic_3
304+
X
305+
```
306+
Next, we can now use these to e.g. designate initial conditions and parameter values for model simulations:
307+
```@example programmatic_3
308+
using OrdinaryDiffEq, Plots # hide
309+
u0 = [X => 0.1]
310+
tspan = (0.0, 10.0)
311+
ps = [p => 1.0, d => 0.2]
312+
oprob = ODEProblem(bd_model, u0, tspan, ps)
313+
sol = solve(oprob)
314+
plot(sol)
315+
```
316+
317+
!!! warn
318+
Just like [when using `@parameters` and `@species`](@ref programmatic_CRN_construction_symbolic_variables), `@unpack` will overwrite any variables in the current scope which shares name with the imported quantities.
319+
320+
### [Interpolating variables into the DSL](@id advanced_intro_to_catalyst_interpolation)
321+
322+
The DSL allows Julia variables to be interpolated for the network name, within
323+
rate constant expressions, or for species/stoichiometry within reactions. Using
324+
the lower-level symbolic interface we can then define symbolic variables and
325+
parameters outside of the macro, which can then be used within expressions in
326+
the DSL (see the [Programmatic Construction of Symbolic Reaction Systems](@ref programmatic_CRN_construction)
327+
tutorial for details on the lower-level symbolic interface). For example,
328+
```@example advanced_intro_to_catalyst_interpolation
329+
using Catalyst
330+
@parameters k α
331+
@variables t
332+
@species A(t)
333+
spec = A
334+
par = α
335+
rate = k*A
336+
name = :network
337+
rn = @reaction_network $name begin
338+
$rate*B, 2*$spec + $par*B --> $spec + C
339+
end
340+
```
341+
As the parameters `k` and `α` were pre-defined and appeared via interpolation,
342+
we did not need to declare them within the `@reaction_network` macro,
343+
i.e. they are automatically detected as parameters:
344+
```@example advanced_intro_to_catalyst_interpolation
345+
parameters(rn)
346+
```
347+
as are the species coming from interpolated variables
348+
```@example advanced_intro_to_catalyst_interpolation
349+
species(rn)
350+
```
351+
352+
!!! note
353+
When using interpolation, expressions like `2$spec` won't work; the
354+
multiplication symbol must be explicitly included like `2*$spec`.
267355

268356
## [Additional options for programmatic model creation](@id programmatic_CRN_construction_additional_options)
269357

0 commit comments

Comments
 (0)