Skip to content

Commit c8ec7a0

Browse files
authored
Merge pull request #770 from SciML/chemistry_functionality_docs
Chemistry functionality docs
2 parents c560828 + 50a96d7 commit c8ec7a0

File tree

11 files changed

+829
-173
lines changed

11 files changed

+829
-173
lines changed

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ to assess (global) structural identifiability for all parameters and variables o
3939
```
4040
X's value will be `1.0` in the first vertex, but `0.0` in the remaining one (the system have 25 vertexes in total). SInce th parameters `p` and `d` are part of the non-spatial reaction network, their values are tied to vertexes. However, if the `D` parameter (which governs diffusion between vertexes) is given several values, these will instead correspond to the specific edges (and transportation along those edges.)
4141

42+
- Update how compounds are created. E.g. use
43+
```julia
44+
@variables t C(t) O(t)
45+
@compound CO2 ~ C + 2O
46+
```
47+
to create a compound species `CO2` that consists of `C` and 2 `O`.
48+
- Added documentation for chemistry related functionality (compound creation and reaction balancing).
4249
- Add a CatalystBifurcationKitExtension, permitting BifurcationKit's `BifurcationProblem`s to be created from Catalyst reaction networks. Example usage:
4350
```julia
4451
using Catalyst

docs/pages.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pages = Any["Home" => "index.md",
77
"catalyst_functionality/constraint_equations.md",
88
"catalyst_functionality/parametric_stoichiometry.md",
99
"catalyst_functionality/network_analysis.md",
10+
"catalyst_functionality/chemistry_related_functionality.md",
1011
"Model creation examples" => Any["catalyst_functionality/example_networks/basic_CRN_examples.md",
1112
"catalyst_functionality/example_networks/hodgkin_huxley_equation.md",
1213
"catalyst_functionality/example_networks/smoluchowski_coagulation_equation.md"]],

docs/src/api/catalyst_api.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ Base.convert
280280
ModelingToolkit.structural_simplify
281281
```
282282

283+
## Chemistry-related functionalities
284+
Various functionalities primarily relevant to modelling of chemical systems (but potentially also in biology).
285+
```@docs
286+
@compound
287+
@compounds
288+
iscompound
289+
components
290+
coefficients
291+
component_coefficients
292+
```
293+
283294
## Unit validation
284295
```@docs
285296
validate(rx::Reaction; info::String = "")
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# [Chemistry-related functionality](@id chemistry_functionality)
2+
3+
While Catalyst has primarily been designed around the modelling of biological systems, reaction network models are also common across chemistry. This section describes two types of functionality, that while of general interest, should be especially useful in the modelling of chemical systems.
4+
- The `@compound` option, which enables the user to designate that a specific species is composed of certain subspecies.
5+
- The `balance_reaction` function, which enables the user to balance a reaction so the same number of components occur on both sides.
6+
7+
## Modelling with compound species
8+
9+
### Creating compound species programmatically
10+
We will first show how to create compound species through [programmatic model construction](@ref programmatic_CRN_construction), and then demonstrate using the DSL. To create a compound species, use the `@compound` macro, first designating the compound, followed by its components (and their stoichiometries). In this example, we will create a CO₂ molecule, consisting of one C atom and two O atoms. First, we create species corresponding to the components:
11+
```@example chem1
12+
using Catalyst
13+
@variables t
14+
@species C(t) O(t)
15+
```
16+
Next, we create the `CO2` compound species:
17+
```@example chem1
18+
@compound CO2 ~ C + 2O
19+
```
20+
Here, the compound is the first argument to the macro, followed by its component (with the left-hand and right-hand sides separated by a `~` sign). While non-compound species (such as `C` and `O`) have their independent variable (in this case `t`) designated, independent variables are generally not designated for compounds (these are instead directly inferred from their components). Components with non-unit stoichiometries have this value written before the component (generally, the rules for designating the components of a compound are identical to those of designating the substrates or products of a reaction). The created compound, `CO2`, is also a species, and can be used wherever e.g. `C` can be used:
21+
```@example chem1
22+
isspecies(CO2)
23+
```
24+
In its metadata, however, is stored information of its components, which can be retrieved using the `components` (returning a vector of its component species) and `coefficients` (returning a vector with each component's stoichiometry) functions:
25+
```@example chem1
26+
components(CO2)
27+
```
28+
```@example chem1
29+
coefficients(CO2)
30+
```
31+
Alternatively, we can retrieve the components and their stoichiometric coefficients as a single vector using the `component_coefficients` function:
32+
```@example chem1
33+
component_coefficients(CO2)
34+
```
35+
Finally, it is possible to check whether a species is a compound or not using the `iscompound` function:
36+
```@example chem1
37+
iscompound(CO2)
38+
```
39+
40+
Compound components that are also compounds are allowed, e.g. we can create a carbonic acid compound (H₂CO₃) that consists of CO₂ and H₂O:
41+
```@example chem1
42+
@species H(t)
43+
@compound H2O ~ 2H + O
44+
@compound H2CO3 ~ CO2 + H2O
45+
```
46+
47+
When multiple compounds are created, they can be created simultaneously using the `@compounds` macro, e.g. the previous code-block can be re-written as:
48+
```@example chem1
49+
@species H(t)
50+
@compounds begin
51+
H2O ~ 2H + O
52+
H2CO3 ~ CO2 + H2O
53+
end
54+
```
55+
56+
### Creating compound species within the DSL
57+
It is also possible to declare species as compound species within the `@reaction_network` DSL, using the `@compounds` options:
58+
```@example chem1
59+
rn = @reaction_network begin
60+
@species C(t) H(t) O(t)
61+
@compounds begin
62+
C2O ~ C + 2O
63+
H2O ~ 2H + O
64+
H2CO3 ~ CO2 + H2O
65+
end
66+
(k1,k2), H2O + CO2 <--> H2CO3
67+
end
68+
```
69+
When creating compound species using the DSL, it is important to note that *every component must be known to the system as a species, either by being declared using the `@species` or `@compound` options, or by appearing in a reaction*. E.g. the following is not valid
70+
```julia
71+
rn = @reaction_network begin``
72+
@compounds begin
73+
C2O ~ C + 2O
74+
H2O ~ 2H + O
75+
H2CO3 ~ CO2 + H2O
76+
end
77+
(k1,k2), H2O+ CO2 <--> H2CO3
78+
end
79+
```
80+
as the components `C`, `H`, and `O` are not declared as a species anywhere. Please also note that only `@compounds` can be used as an option in the DSL, not `@compound`.
81+
82+
### Designating metadata and default values for compounds
83+
Just like for normal species, it is possible to designate metadata and default values for compounds. Metadata is provided after the compound name, but separated from it by a `,`:
84+
```@example chem1
85+
@compound (CO2, [unit="mol"]) ~ C + 2O
86+
```
87+
Default values are designated using `=`, and provided directly after the compound name.:
88+
```@example chem1
89+
@compound (CO2 = 2.0) ~ C + 2O
90+
```
91+
If both default values and meta data are provided, the metadata is provided after the default value:
92+
```@example chem1
93+
@compound (CO2 = 2.0, [unit="mol"]) ~ C + 2O
94+
```
95+
In all of these cases, the side to the left of the `~` must be enclosed within `()`.
96+
97+
### Compounds with multiple independent variables
98+
While we generally do not need to specify independent variables for compound, if the components (together) have more than one independent variable, this have to be done:
99+
```@example chem1
100+
@variables t s
101+
@species N(s) O(t)
102+
@compound NO2(t,s) ~ N + 2O
103+
```
104+
Here, `NO2` depend both on a spatial independent variable (`s`) and a time one (`t`). This is required since, while multiple independent variables can be inferred, their internal order cannot (and must hence be provided by the user).
105+
106+
## Balancing chemical reactions
107+
One use of defining a species as a compound is that they can be used to balance reactions so that the number of components are the same on both sides. Catalyst provides the `balance_reaction` function, which takes a reaction, and returns a balanced version. E.g. let us consider a reaction when carbon dioxide is formed from carbon and oxide `C + O --> CO2`. Here, `balance_reaction` enables us to find coefficients creating a balanced reaction (in this case, where the number of carbon and oxygen atoms are the same on both sides). To demonstrate, we first created the unbalanced reactions:
108+
```@example chem1
109+
rx = @reaction k, C + O --> $CO2
110+
```
111+
Here, the reaction rate (`k`) is not involved in the reaction balancing. We use interpolation for `CO2`, ensuring that the `CO2` used in the reaction is the same one we previously defined as a compound of `C` and `O`. Next, we call the `balance_reaction` function
112+
```@example chem1
113+
balance_reaction(rx)
114+
```
115+
which correctly finds the (rather trivial) solution `C + 2O --> CO2`. Here we note that `balance_reaction` actually returns a vector. The reason is that the reaction balancing problem may have several solutions. Typically, there is only a single solution (in which case this is the vector's only element). No, or an infinite number of, solutions is also possible depending on the given reaction.
116+
117+
Let us consider a more elaborate example, the reaction between ammonia (NH₃) and oxygen (O₂) to form nitrogen monoxide (NO) and water (H₂O). Let us first create the components and the unbalanced reaction:
118+
```@example chem2
119+
using Catalyst # hide
120+
@variables t
121+
@species N(t) H(t) O(t)
122+
@compounds begin
123+
NH3 ~ N + 3H
124+
O2 ~ 2O
125+
NO ~ N + O
126+
H2O ~ 2H + O
127+
end
128+
unbalanced_reaction = @reaction k, $NH3 + $O2 --> $NO + $H2O
129+
```
130+
We can now create a balanced version (where the amount of H, N, and O is the same on both sides):
131+
```@example chem2
132+
balanced_reaction = balance_reaction(unbalanced_reaction)[1]
133+
```
134+
135+
Reactions declared as a part of a `ReactionSystem` (e.g. using the DSL) can be retrieved for balancing using the [`reactions`](@ref) function. Please note that balancing these will not mutate the `ReactionSystem`, but a new reaction system will need to be created using the balanced reactions.
136+
137+
!!! note
138+
Reaction balancing is currently not supported for reactions involving compounds of compounds.

docs/src/catalyst_functionality/constraint_equations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $\lambda$. For now we'll assume the cell can grow indefinitely. We'll also keep
1717
track of one protein $P(t)$, which is produced at a rate proportional $V$ and
1818
can be degraded.
1919

20-
## Coupling ODE constraints via extending a system
20+
## [Coupling ODE constraints via extending a system](@id constraint_equations_coupling_constratins)
2121

2222
There are several ways we can create our Catalyst model with the two reactions
2323
and ODE for $V(t)$. One approach is to use compositional modeling, create

src/Catalyst.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export Graph, savegraph, complexgraph
102102

103103
# for creating compounds
104104
include("chemistry_functionality.jl")
105-
export @compound
106-
export components, iscompound, coefficients
105+
export @compound, @compounds
106+
export iscompound, components, coefficients, component_coefficients
107107
export balance_reaction
108108

109109
### Extensions ###

0 commit comments

Comments
 (0)