Skip to content

Commit 5f59618

Browse files
committed
save progress
1 parent 1ae6ac9 commit 5f59618

File tree

1 file changed

+126
-3
lines changed

1 file changed

+126
-3
lines changed

docs/src/model_creation/programmatic_CRN_construction.md

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Here we have described the basics of symbolic variable declarations. A more thro
6666
!!! note
6767
For users familiar with [ModelingToolkit](@ref ref), the `@species` macro have a very similar functionality to the `@variables` macro. However, `@species` appends additional metadata that the variable is a *species*, which is required for it to be used as a reaction reactant.
6868

69+
!!! note
70+
While symbolic variables are not explicitly created when models are created via the DSL, these are still declared and stored internally. When calling the [`parameters`](@ref) or [`species](@ref) functions on a DSL-created `ReactionSystem`, you will symbolic variables of the same form as we here have created programmatically.
71+
6972
## [Creation of `Reaction`s](@id programmatic_CRN_construction_reactions)
7073
In the next step, we can assembly our models reactions. In DSL-based modelling, these are listed within the `@reaction_network` macro. For programmatic modelling, these are instead declared using the `Reaction` structure's constructor:
7174
```@example programmatic_2
@@ -81,6 +84,9 @@ Here, `Reaction` takes three arguments:
8184

8285
Just like [when the DSL is used], more complicated reactions (featuring e.g. [](@ref ref), [](@ref ref), [](@ref ref), and [](@ref ref)) are possible. How to create such reactions is described [here](@ref ref).
8386

87+
!!! note
88+
While `Reaction`s are not explicitly created when models are created via the DSL, these are still declared and stored internally. When calling the [`reactions`](@ref) function on a DSL-created `REactionSystem`, you will receive `Reaction`s of the same form as we here have created programmatically.
89+
8490
## [Creation of `ReactionSystem`s](@id programmatic_CRN_construction_reactionsystems)
8591
Finally, we can use our `Reaction` vector as input to the Catalyst's `ReactionSystem` constructor. In addition to these, we need two additional arguments:
8692
1. The independent variable (typically time) must be explicitly provided.
@@ -98,6 +104,7 @@ This will automatically take name of the variable in which the model is stored,
98104
```@example programmatic_2
99105
getname(two_state_model)
100106
```
107+
There exists a few additional options that can be supplied to the `ReactionSystem` constructor. These are described in the `ReactionSystem`s API entry, which can be found [here](@ref ref).
101108

102109
While we now have programmatically created a `ReactionSystem`, there are two final points (described in the next two sections) we should consider before using it for e.g. simulations.
103110

@@ -132,9 +139,127 @@ two_state_model == two_state_model_dsl
132139
```
133140

134141
## [Symbolic designation of model quantities and simulation of programmatic models](@id programmatic_CRN_construction_symbolic_representation)
135-
Previously, we have described how to [simulate Catalyst models declared via the DSL](@ref ref). Here, we used `Symbol`s (e.g. `:k1` and `:X1`) to designate parameters and species' values. However, in programmatic model creation we have explicitly [declared symbolic variables](@ref programmatic_CRN_construction_symbolic_variables) corresponding to our parameters and species. Here, it is instead possible to use these to designate our simulation conditions. Below we utilise this to declare initial conditions and parameters, and then using this to
142+
Previously, we have described how to [simulate Catalyst models declared via the DSL](@ref ref). Here, we used `Symbol`s (e.g. `:k1` and `:X1`) to designate parameters and species' values. However, in programmatic model creation we have explicitly [declared symbolic variables](@ref programmatic_CRN_construction_symbolic_variables) corresponding to our parameters and species. Here, it is instead possible to use these to designate our simulation conditions. Below we utilise this to declare initial conditions and parameters, use these as input to a `ODEProblem`, and then simulate it and plot the result:
143+
```@example programmatic_2
144+
using OrdinaryDiffEq, Plots
145+
u0 = [X1 => 0.0, X2 => 2.0]
146+
tspan = (0.0, 1.0)
147+
ps = [k1 => 1.0, ks => 2.0]
148+
oprob = ODEProblem(two_state_model, u0, tspan, ps)
149+
sol = solve(oprob)
150+
plot(sol)
151+
```
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).
153+
154+
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
155+
```@example programmatic_2
156+
sol[X1]
157+
```
158+
to retrieve $X$'s value across the simulation.
159+
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.
136191

137192
## [Additional options for declaration of symbolic variables](@id programmatic_CRN_construction_symbolic_variables_options)
193+
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.
194+
195+
### [Designating default values](@id programmatic_CRN_construction_symbolic_variables_options_defaults)
196+
Species and parameters can be assigned default values. These permits the omission of their values from the initial condition and parameter values vector (in which case the default value is used instead). A default value is specified by simply a quantity's declaration by `=` followed by its default value. E.g. here we give the two-state model's species default initial condition values:
197+
```@example programmatic_4
198+
using Catalyst # hide
199+
@species X1(t)=0.0 X2(t)=2.0
200+
nothing # hide
201+
```
202+
A symbolic variables default value can be retrieved using the [`getdefault`](@ref ref) function:
203+
```@example programmatic_4
204+
getdefault(X1)
205+
```
206+
207+
A more throughout description on how to work with default values, including how to e.g. set parametric initial conditions, is provided [when defaults are introduced in the context of DSL-based modelling](@ref ref).
208+
209+
### [Designating metadata](@id programmatic_CRN_construction_symbolic_variables_options_metadata)
210+
Sometimes one might want to attach additional information to a symbolic variable. This is done through the addition of *metadata*. To attach metadata to a symbolic variable, simply follow its declaration by brackets (`[]`) enclosing a list of metadata and their values (separated by a `=`). E.g. here we attach [`description`](@ref ref) metadata to the two-state model's species:
211+
```@example programmatic_4
212+
@species X1(t) [description="Species X1"] X2(t) [description="Species X2"]
213+
nothing # hide
214+
```
215+
A symbolic variable can have several metadata, and different sets of metadata can be supplied to each declared symbolic variable:
216+
```@example programmatic_4
217+
@parameters k1 [description="Parameter X1"] k2 [description="Parameter X1", bounds=(1e-2, 1e2)]
218+
nothing # hide
219+
```
220+
221+
If a symbolic variable have both metadata and a default value, the default value is designated first, and the metadata second:
222+
```@example programmatic_4
223+
@species X1(t)=0.0 [description="Species X1"] X2(t)=2.0 [description="Species X2"]
224+
nothing # hide
225+
```
226+
227+
### [Designating parameter types](@id programmatic_CRN_construction_symbolic_variables_options_types)
228+
Sometimes it is desired to designate that a parameter should have a specific [type](@ref ref). When supplying this parameters value to e.g. an `ODEProblem`, that parameter will then be restricted to that specific type. Designating a type is done by appending the parameter with `::` followed by its type. E.g. to specify that a parameter `n` should be an `Int64` we do:
229+
```@example programmatic_4
230+
@parameters n(t)::Int64
231+
nothing # hide
232+
```
233+
234+
If a parameter have a type, a metadata, and a default value, they are designated in the following order:
235+
```@example programmatic_4
236+
TODO: @parameters n(t)::Int64
237+
nothing # hide
238+
```
239+
240+
### [Multi-line declarations](@id programmatic_CRN_construction_symbolic_variables_options_multiline)
241+
Sometimes, when declaring a large number of quantities (or providing extensive additional information), readability can be improved through a multi-line statement. Here, the `@parameters`/`@species` macros are followed by a `begin ... end` block. Each line within the block contains the declaration of a single parameter/species. E.g. the parameters and species of or two-state model could have been declared using
242+
```@example programmatic_4
243+
@parameters begin
244+
k1
245+
k2
246+
end
247+
@species begin
248+
X1(t)
249+
X2(t)
250+
end
251+
nothing # hide
252+
```
253+
254+
When using multi-line statements, default values, metadata, and types is appended *almost* identically as for the single-lines one. The one exception is that when metadata is added, this must be separated from the quantity by a `,`:
255+
```@example programmatic_4
256+
@parameters begin
257+
k1, [description = "Parameter k1"]
258+
k2, [description = "Parameter k2"]
259+
end
260+
nothing # hide
261+
```
262+
138263

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

@@ -143,8 +268,6 @@ Previously, we have described how to [simulate Catalyst models declared via the
143268
## [Additional options for programmatic model creation](@id programmatic_CRN_construction_additional_options)
144269

145270

146-
!!! note
147-
While `Reaction`s are not explicitly created when models are created via the DSL, these structures are still declared and stored internally. When calling the
148271

149272
While the DSL provides a simple interface for creating `ReactionSystem`s, it can
150273
often be convenient to build or augment a [`ReactionSystem`](@ref)

0 commit comments

Comments
 (0)