Skip to content

Commit 72dc98b

Browse files
committed
save progress
1 parent b53e83f commit 72dc98b

File tree

1 file changed

+119
-36
lines changed

1 file changed

+119
-36
lines changed

docs/src/catalyst_functionality/coupled_crn_equations.md

Lines changed: 119 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
# [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 phenomena relevant 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).
33

4-
## [Basic example](@id coupled_equations_basic_example)
4+
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).
12+
13+
### [Basic example](@id coupled_equations_basic_example)
514
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:
615
```@example coupled_eqs_1
716
using Catalyst
817
crn = @reaction_network begin
918
(p,d), 0 <--> G
1019
end
1120
```
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:
1322
```math
14-
\frac{d\mathbf{V}}{dt} = \mathbf{G(t)} - \mathbf{G(V)},
23+
\frac{d\mathbf{V}}{dt} = \mathbf{G(t)} - \mathbf{V(t)}
1524
```
16-
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-
```@example coupled_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+
```@example coupled_eqs_basic_example
1827
coupled_crn = @reaction_network begin
1928
@equations begin
20-
D(V) ~ G - V
29+
D(V) ~ G - 1.0
2130
end
2231
(p,d), 0 <--> G
2332
end
2433
nothing # hide
2534
```
2635
We can check the ODE this model would generate [using Latexify](@ref ref):
27-
```@example coupled_eqs_1
36+
```@example coupled_eqs_basic_example
2837
using Latexify
2938
latexify(coupled_crn; form=:ode)
3039
```
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$):
3241
```@example coupled_eqs_1
3342
using OrdinaryDiffEq, Plots
3443
u0 = [:G => 0.1, :V => 1.0]
@@ -39,62 +48,136 @@ sol = solve(oprob)
3948
plot(sol)
4049
```
4150

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+
4499

45100
## [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:
104+
```math
105+
\mathbf{N} \cdot \mathbf{p}, \mathbf{∅} \to \mathbf{G} \\
106+
\mathbf{d}, \mathbf{G} \to \mathbf{∅}
107+
```
108+
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:
109+
```math
110+
\frac{d\mathbf{N}}{dt} = - \mathbf{V(t)} \\
111+
\frac{d\mathbf{V}}{dt} = \mathbf{G(t)} - \mathbf{V(t)}
112+
```
113+
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
48115
growth_model = @reaction_network begin
49116
@equations begin
50-
D(V) ~ G - V
51117
D(N) ~ -V
118+
D(V) ~ G - V
52119
end
53120
(N*p,d), 0 <--> G
54121
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
55125
latexify(growth_model; form=:ode)
56126
```
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
59130
species(growth_model)
60131
```
61-
```@example coupled_eqs_2
132+
```@example coupled_eqs_diff_eq
62133
variables(growth_model)
63134
```
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
66144
growth_model = @reaction_network begin
67-
@parameters g d
145+
@parameters dN dV
68146
@equations begin
69-
D(V) ~ g*G - V
70-
D(N) ~ -d*V
147+
D(N) ~ - dN * V
148+
D(V) ~ G - dV * V
71149
end
72150
(N*p,d), 0 <--> G
73151
end
74152
parameters(growth_model)
75153
```
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
80154

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):
156+
```@example coupled_eqs_diff_eq
157+
using OrdinaryDiffEq, Plots # hide
158+
u0 = [:G => 0.1, :V => 1.0, :N => 1.0]
159+
tspan = (0.0, 10.0)
160+
p = [:p => 1.0, :d => 0.5, :dN => 0.2, :dV => 0.1]
161+
oprob = ODEProblem(growth_model, u0, tspan, p)
162+
sol = solve(oprob, Tsit5())
163+
plot(sol)
164+
```
86165

87-
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).
88172

89173
## [Coupling CRNs with algebraic equation](@id coupled_equations_algebraic_equations)
90174
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.
94177

95178
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).
96179

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)
98181
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.
99182

100183
### [Converting coupled models to SteadyState problems](@id coupled_equations_conversions_steady_state)

0 commit comments

Comments
 (0)