You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/model_creation/programmatic_CRN_construction.md
+126-3Lines changed: 126 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -66,6 +66,9 @@ Here we have described the basics of symbolic variable declarations. A more thro
66
66
!!! note
67
67
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.
68
68
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
+
69
72
## [Creation of `Reaction`s](@id programmatic_CRN_construction_reactions)
70
73
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:
71
74
```@example programmatic_2
@@ -81,6 +84,9 @@ Here, `Reaction` takes three arguments:
81
84
82
85
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).
83
86
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
+
84
90
## [Creation of `ReactionSystem`s](@id programmatic_CRN_construction_reactionsystems)
85
91
Finally, we can use our `Reaction` vector as input to the Catalyst's `ReactionSystem` constructor. In addition to these, we need two additional arguments:
86
92
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,
98
104
```@example programmatic_2
99
105
getname(two_state_model)
100
106
```
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).
101
108
102
109
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.
## [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:
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.
136
191
137
192
## [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.
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).
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:
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:
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
+
138
263
139
264
## [Additional options for declaration of `Reaction`s](@id programmatic_CRN_construction_reactions_options)
140
265
@@ -143,8 +268,6 @@ Previously, we have described how to [simulate Catalyst models declared via the
143
268
## [Additional options for programmatic model creation](@id programmatic_CRN_construction_additional_options)
144
269
145
270
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
148
271
149
272
While the DSL provides a simple interface for creating `ReactionSystem`s, it can
150
273
often be convenient to build or augment a [`ReactionSystem`](@ref)
0 commit comments