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
# [Coupling chemical reaction networks with differential or algebraic equations](@id coupled_equations)
2
-
Catalyst is primarily designed to model [chemical reaction networks (CRNs)](@ref ref), which are defined by a set of species and reaction events. These can then be used to generate e.g. ODEs which can be simulated. However, there are a large number phenomena, many relevant to the fields utilising CRNs, that cannot be described by this formalism. To model these, more general equations (either [differential](https://en.wikipedia.org/wiki/Differential_equation) or [algebraic](https://en.wikipedia.org/wiki/Algebraic_equation)) are needed. Here, Catalyst provides the functionality of coupling CRN models with algebraic or differential equations models. For creating pure non-CRN models, we recommend using [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl).
2
+
Catalyst is primarily designed to model [chemical reaction networks (CRNs)](@ref ref), which are defined by a set of *species* and a set of *reaction events*. Once defined, CRNs can be converted to e.g. ODEs or SDEs. However, many phenomenarelevant to fields utilising CRNs (e.g. systems biology) cannot be described using this formalism. To model these, more general equations (either [differential](https://en.wikipedia.org/wiki/Differential_equation) or [algebraic](https://en.wikipedia.org/wiki/Algebraic_equation)) are required. Here, Catalyst provides the functionality of coupling CRN models with algebraic or differential equations models. For creating pure non-CRN models, we recommend using [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl).
Example of phenomena which might be modelled with differential equations are:
5
+
- The volume of a cell, or its amount of available nutrients.
6
+
- The public's awayness of of a novel infectious disease.
7
+
- The current temperature in a chemical reactor.
8
+
9
+
Example of phenomena which might be modelled with algebraic equations are:
10
+
- The concentration of species which reactions are "fast enough" that they can be considered to be at steady state.
11
+
- The relations between pressure, volume, molecular numbers, and temperature given bye [*the ideal gas law*](https://en.wikipedia.org/wiki/Ideal_gas_law).
Let us consider a simple model of a growth factor ($G$), which controls a cell's volume ($V$). Here, we can model the production and degradation of the growth factor using a simple CRN in Catalyst:
6
15
```@example coupled_eqs_1
7
16
using Catalyst
8
17
crn = @reaction_network begin
9
18
(p,d), 0 <--> G
10
19
end
11
20
```
12
-
CRNs, however, model quantities that exists in (potentially very large) discrete quantities (like number of protein molecules) and change in discrete reaction events (like production and degradation events). This does not hold for a cell's volume. Here we will instead model the cell volume using the following differential equation:
21
+
CRNs, however, model quantities that exists in (potentially very large) discrete quantities (like number of protein molecules) and change in discrete reaction events (like production and degradation events). This does not hold for a cell's volume (which is a *continuous quantity*). Here we will instead model the cell volume using the following differential equation:
In this case, we can insert this differential equation directly into our model using the `@equations` option, and `D(V)`to describe the differential with respect to $V$:
17
-
```@examplecoupled_eqs_1
25
+
where the volume's growth increase with $G$ at saturates ones large enough. It is possible to insert this differential equation directly into our model using the DSL's `@equations` option (here, `D(V)`denotes the differential of $V$ with respect to time):
26
+
```@examplecoupled_eqs_basic_example
18
27
coupled_crn = @reaction_network begin
19
28
@equations begin
20
-
D(V) ~ G - V
29
+
D(V) ~ G - 1.0
21
30
end
22
31
(p,d), 0 <--> G
23
32
end
24
33
nothing # hide
25
34
```
26
35
We can check the ODE this model would generate [using Latexify](@ref ref):
27
-
```@examplecoupled_eqs_1
36
+
```@examplecoupled_eqs_basic_example
28
37
using Latexify
29
38
latexify(coupled_crn; form=:ode)
30
39
```
31
-
Finally, the model can be simualted using standard syntax (while providing initial conditions for $V$ as well as for $G$):
40
+
Finally, the model can be simulated using standard syntax (while providing initial conditions for $V$ as well as for $G$):
32
41
```@example coupled_eqs_1
33
42
using OrdinaryDiffEq, Plots
34
43
u0 = [:G => 0.1, :V => 1.0]
@@ -39,62 +48,136 @@ sol = solve(oprob)
39
48
plot(sol)
40
49
```
41
50
42
-
## Variables and species
43
-
The unknowns of CRN models are called *species*. These are unknown variables of a system which can partake in reaction events. Catalyst support another form of unknowns, *variables*. While variables (like species) may have values that change continuously throughout a simulation, they cannot be substrates or products of a reaction. They can, however, be the subject of differential equations. Species, *cannot* be part of differential equations.
51
+
## [Variables and species](@id coupled_equations_variables)
52
+
Catalyst's normal CRN models depend on two types of quantities, *parameters* (which values are explicitly provided at the beginning of simulations) *species* (which values are implicitly inferred from a CRN model by e.g. simulating it). The coupling of CRNs and equations require the introduction of a third quantity, *variables*. Variables, just like species, have values which are implicitly given my a model. For many purposes, species and variables can be treated identically, and a model's set of species and variables is called its *unknowns*.
53
+
54
+
Variables and species, however, are different in that:
55
+
- Only species are permissible reaction reactants.
56
+
- Only variables are permitted subjects of differentials in coupled CRN/differential equations.
57
+
58
+
Previously, we have described how model species can [explicitly be declared using the `@species` DSL option](@ref ref). Model variables can be declared in exactly the same manner, but using the `@variables` option. While quantities declared using `@variables` are considered variables (not species), its syntax (for e.g. designating [default values](@ref ref) or [metadata](@ref ref)) is otherwise identical to `@species`. Below we declare a simple cell model (with a growth factor species $G$ and a volume variable $V$):
59
+
```@example coupled_eqs_variable_intro
60
+
using Catalyst # hide
61
+
cell_model = @reaction_network begin
62
+
@variables V(t)
63
+
(p,d), 0 <--> G
64
+
end
65
+
```
66
+
While Catalyst, generally, can infer what quantities are species and parameters, this typically does not hold for variables. Catalyst only infers a quantity to be a variable if it is [the subject of a time differential](@ref coupled_equations_differential_equations). In all other situations, variables must explicitly be declared.
67
+
68
+
Note that variable (like species) are time-dependant, and (like species) must be declared as such (e.g. using `V(t)` rather than `V`) when the `@variables` option is used. We can now simulate our model (remembering to provide a initial conditions for $V$ as well as $G$):
69
+
```@example dsl_advanced_variables
70
+
using OrdinaryDiffEq, Plots
71
+
u0 = [:G => 0.1, :V => 1.0]
72
+
tspan = (0.0, 10.0)
73
+
p = [:p => 1.0, :d => 0.5]
74
+
oprob = ODEProblem(cell_model, u0, tspan, p)
75
+
sol = solve(oprob, Tsit5())
76
+
plot(sol)
77
+
```
78
+
Here, since we have not inserted $V$ into any equation describing how its value may change, it remains constant throughout the simulations. The following sections will describe how to declare such equation as part of Catalyst models.
79
+
80
+
Finally, we note that variables (like parameters and species) are valid components of the rate expressions. E.g. we can modify our cell module so that the reaction rates (inversely) depends on the volume variable:
81
+
```@example coupled_eqs_variable_intro
82
+
cell_model = @reaction_network begin
83
+
@variables V(t)
84
+
(p/V,d/V), 0 <--> G
85
+
end
86
+
```
87
+
88
+
It is possible to check a models species, variables, and unknowns through the `species`, `variables`, and `unknowns` functions:
89
+
```@example coupled_eqs_variable_intro
90
+
species(cell_model)
91
+
```
92
+
```@example coupled_eqs_variable_intro
93
+
variables(cell_model)
94
+
```
95
+
```@example coupled_eqs_variable_intro
96
+
unknowns(cell_model)
97
+
```
98
+
44
99
45
100
## [Coupling CRNs with differential equation](@id coupled_equations_differential_equations)
46
-
Like [demonstrated in the previous example](@ref coupled_equations_basic_example), differential equations can be added to CRN models through the `@equations` option (in the DSL, non-DSL approaches for coupled CRN/equations [are described later](@ref ref)). These are then saved as part of the generated `ReactionSystem` model, and added to the system of ODEs generated from the CRN. The `@equations` option is followed by a `begin ... end` block, where each line correspond to a single equation. Here we create a simple growth model which describes the growth of a cell's volume ($V$), which depends on a growth factor ($G$), and also depletes a nutrient supply ($N$):
47
-
```@example coupled_eqs_2
101
+
Differential equations can be added to CRN models through the DSL's `@equations` option ([programmatic](@ref ref) creation of couple CRN/equation model [is described later](@ref coupled_equations_programmatic)). These are then saved as part of the generated `ReactionSystem` model, and added to the system of ODEs generated from the CRN.
102
+
103
+
Let us consider a simple cell growth model consisting of a single growth factor ($G$) that is produced/degraded according to the following reactions:
Our model will also consider the variables $N$ (the amount of available nutrient) and $V$ (the cell's volume). These are described through the following differential equations:
The reactions can be modelled using Catalyst's normal reaction notation. The equations are added using the `@equations`, which is followed by a`begin ... end` block, where each line contain one equation. The time differential is written using `D(...)` and equality between the left and right-hand sides are denoted using `~` (*not*`=`!).
114
+
```@example coupled_eqs_diff_eq
48
115
growth_model = @reaction_network begin
49
116
@equations begin
50
-
D(V) ~ G - V
51
117
D(N) ~ -V
118
+
D(V) ~ G - V
52
119
end
53
120
(N*p,d), 0 <--> G
54
121
end
122
+
```
123
+
The equations left and right-hand sides are declared using the same rules as reaction rate (and can be any valid algebraic expression containing variables, species, parameters, numbers, and function calls). We can check the ODE our model generate [by using Latexify](@ref ref):
124
+
```@example coupled_eqs_diff_eq
55
125
latexify(growth_model; form=:ode)
56
126
```
57
-
Here, while $G$ is inferred to be a species, $V$ and $N$ are inferred to be variables. We can check this using the `species` and `variables` functions:
58
-
```@example coupled_eqs_2
127
+
128
+
Previously, we described how coupled CRN/equation models contain [both *species* and *variables*](@ref coupled_equations_variables), and how variables can be explicitly declared using the `@variables` option. Here, since both $N$ and $V$ are the subject of the default differential $D$, Catalyst automatically infers these to be variables. We which unknowns are species and variables through the corresponding functions:
129
+
```@example coupled_eqs_diff_eq
59
130
species(growth_model)
60
131
```
61
-
```@examplecoupled_eqs_2
132
+
```@examplecoupled_eqs_diff_eq
62
133
variables(growth_model)
63
134
```
64
-
Previously, we have described how Catalyst automatically infers what is a species and what is a parameter depending on where in the model it occurs. Catalyst can also infer variables, however, the only situation where this is done is *terms that occur as the subject of the default differential on the left-hand side of a differential equation* E.g. above both $D$ and $V$ are inferred to be variables.Catalyst does not infer any other components to be species or variables. This means that if we want to include parameters in our differential equations, these must explicitly be declared as such:
65
-
```@example coupled_eqs_2
135
+
136
+
Currently, our differential equations depend on the species and variables $G$, $N$, and $V$. They may also depend on parameters, however, before we introduce additional quantities to these, we must be careful to ensure that Catalyst can correctly infer their type. Catalyst uses the following rules for automatically inferring what to consider a parameter, a species, and a variable:
137
+
- Quantities explicitly declared using the `@parameters`, `@species`, and `@variables` options have the corresponding types.
138
+
- Undeclared quantities occurring as reaction substrates or products are inferred to be species.
139
+
- Undeclared quantities occurring as the single subject of the default differential (`D`) on the left-hand side of an equation is inferred to be a variable.
140
+
- Remaining undeclared quantities that occur in either reaction rates or stoichiometries are inferred to be parameters.
141
+
142
+
Catalyst cannot infer the type of quantities that do not fulfil any of these conditions. This includes parameters that occur only within declared equations. Below we introduce such parameters by also explicitly declaring them as such using the `@parameters` option, and then confirm that they have been added properly using the `parameters` function:
143
+
```@example coupled_eqs_diff_eq
66
144
growth_model = @reaction_network begin
67
-
@parameters g d
145
+
@parameters dN dV
68
146
@equations begin
69
-
D(V) ~ g*G - V
70
-
D(N) ~ -d*V
147
+
D(N) ~ - dN * V
148
+
D(V) ~ G - dV * V
71
149
end
72
150
(N*p,d), 0 <--> G
73
151
end
74
152
parameters(growth_model)
75
153
```
76
-
Here, with the following exceptions:
77
-
- Variables can neither be substrates or products of reactions.
78
-
- Species cannot be subject of differentials in differential equations.
79
-
- Neither species nor variables can occur as stoichiometric coefficients within reactions
80
154
81
-
species and variables may occur throughout the model. E.g. in our example above, the production rate of $G$ depends on $N$, while the growth grate of $V$ depends on $G$.
82
-
83
-
Generally, differential equations are formed similarly as in the [ModelingToolkit.jl package](https://github.com/SciML/ModelingToolkit.jl) (on which [Catalyst is built](@ref ref)). In practise, they follow exactly the same rules as when forming expressions for [rates in Catalyst](@ref ref). The only noteworthy exceptions are:
84
-
- The `~` (not `=`!) is used to separate the left-hand and right-hand sides of the equations.
85
-
- By default, `D(...)` is used to represent the differential operator with respect to time (or the [default independent variable](@ref ref)).
155
+
Once declared, we can simulate our model using standard syntax (providing initial conditions for all unknowns, whether species or variables):
Differential equations does not need to be written in a pure `D(...) ~ ...` form (with the differential isolated on the left-hand side). However, if this is not the case, `D` is not automatically inferred to be the differential operator. Here, a differential must be [manually declared](@ref ref). Higher-order differentials are also permitted, how to model these are described [here](@ref ref). In these cases, what is a variable is not inferred at all, and this must hence be declared explicitly.
166
+
Finally, a few additional notes regarding coupled CRN/differential equation models:
167
+
- The rules for declaring differential equations are identical to those used by the [ModelingToolkit.jl package](https://github.com/SciML/ModelingToolkit.jl) (on which [Catalyst is built](@ref ref)). Users familiar with this package can directly use this familiarity when declaring equations within Catalyst.
168
+
- While the default differential `D` is typically used, it is possible to [define custom differentials](@ref ref).
169
+
- Catalyst only infers a quantity `X` to be a variable for expressions on the form `D(X) ~ ...`, where the left-hand side contain no differentials. For equations with multiple, higher-order, or custom differentials, or where the left-hand side contain several terms, no variables are inferred (and these must be declared explicitly using `@variables`).
170
+
- Declared differential equations may contain higher-order differential equations, however, these have [some additional considerations](@ref ref).
171
+
- Generally `D` is inferred to be the differential with respect to the [system's independent variable](@ref ref) (typically time). However, this is only the case in the same situations when Catalyst infers variables (e.g. equations on the form `D(X) ~ ...`). For other situations, the differential must [explicitly be declared](@ref ref).
88
172
89
173
## [Coupling CRNs with algebraic equation](@id coupled_equations_algebraic_equations)
90
174
Catalyst also permits the coupling of CRN models with *algebraic equations* (equations not containing differentials). In practise, these are handled similarly to how differential equations are handled, but with the following differences:
91
-
- The equations does not contain any differentials.
92
-
- The `structural_simplify = true` option must be provided to any `ODEProblem` (or other problem) generated from a problem containing an algebraic equation.
93
-
- Catalyst will not infer what is a variable
175
+
- The `structural_simplify = true` option must be provided to any `ODEProblem` (or other problem) generated from a model containing algebraic equations.
176
+
- Catalyst cannot infer any variables, so these must explicitly be declared.
94
177
95
178
A special case of algebraic equations (where a new variable is trivially produced by an algebraic expression) are *observables*. These are described in a separate section [here](@ref ref).
96
179
97
-
## [Converting coupled crn/equations to non-ODE systems](@id coupled_equations_conversions)
180
+
## [Converting coupled CRN/equation models to non-ODE systems](@id coupled_equations_conversions)
98
181
As [previously described](@ref ref), Catalyst permits the conversion of `ReactionSystem`s to a range of model types. WIth the exception of `JumpProblem`s, all these can be generated from CRNs coupled to equation models. Below, we briefly describe each case.
99
182
100
183
### [Converting coupled models to SteadyState problems](@id coupled_equations_conversions_steady_state)
0 commit comments