Skip to content

Commit 50a96d7

Browse files
committed
up
1 parent cb6c12d commit 50a96d7

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

docs/src/catalyst_functionality/chemistry_related_functionality.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ While Catalyst has primarily been designed around the modelling of biological sy
66

77
## Modelling with compound species
88

9-
#### Creating compound species programmatically
9+
### Creating compound species programmatically
1010
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:
1111
```@example chem1
1212
using Catalyst
@@ -17,7 +17,7 @@ Next, we create the `CO2` compound species:
1717
```@example chem1
1818
@compound CO2 ~ C + 2O
1919
```
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 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:
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:
2121
```@example chem1
2222
isspecies(CO2)
2323
```
@@ -53,7 +53,7 @@ When multiple compounds are created, they can be created simultaneously using th
5353
end
5454
```
5555

56-
#### Creating compound species within the DSL
56+
### Creating compound species within the DSL
5757
It is also possible to declare species as compound species within the `@reaction_network` DSL, using the `@compounds` options:
5858
```@example chem1
5959
rn = @reaction_network begin
@@ -63,12 +63,12 @@ rn = @reaction_network begin
6363
H2O ~ 2H + O
6464
H2CO3 ~ CO2 + H2O
6565
end
66-
(k1,k2), H2O+ CO2 <--> H2CO3
66+
(k1,k2), H2O + CO2 <--> H2CO3
6767
end
6868
```
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` option, or by appearing in a reaction*. E.g. the following is not valid
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
7070
```julia
71-
rn = @reaction_network begin
71+
rn = @reaction_network begin``
7272
@compounds begin
7373
C2O ~ C + 2O
7474
H2O ~ 2H + O
@@ -79,7 +79,7 @@ end
7979
```
8080
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`.
8181

82-
#### Designating metadata and default values for compounds
82+
### Designating metadata and default values for compounds
8383
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 `,`:
8484
```@example chem1
8585
@compound (CO2, [unit="mol"]) ~ C + 2O
@@ -94,6 +94,15 @@ If both default values and meta data are provided, the metadata is provided afte
9494
```
9595
In all of these cases, the side to the left of the `~` must be enclosed within `()`.
9696

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+
97106
## Balancing chemical reactions
98107
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:
99108
```@example chem1

src/chemistry_functionality.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -284,35 +284,35 @@ Example:
284284
```julia
285285
@variables t
286286
@species Si(t) Cl(t) H(t) O(t)
287-
@compound SiCl4(t) = Si + 4Cl
288-
@compound H2O(t) = 2H + O
289-
@compound H4SiO4(t) = 4H + Si + 4O
290-
@compound HCl(t) = H + Cl
291-
rx = Reaction(1.0,[SiCl4,H2O],[H4SiO4,HCl])
287+
@compound SiCl4 ~ Si + 4Cl
288+
@compound H2O ~ 2H + O
289+
@compound H4SiO4 ~ 4H + Si + 4O
290+
@compound HCl ~ H + Cl
291+
rx = @reaction 1.0, SiCl4 + H2O --> H4SiO4 HCl
292292
balance_reaction(rx) # Exactly one solution.
293293
```
294294
295295
```julia
296296
@variables t
297297
@species C(t) H(t) O(t)
298-
@compound CO(t) = C + O
299-
@compound CO2(t) = C + 2O
300-
@compound H2(t) = 2H
301-
@compound CH4(t) = C + 4H
302-
@compound H2O(t) = 2H + O
303-
rx = Reaction(1.0, [CO, CO2, H2], [CH4, H2O])
298+
@compound CO ~ C + O
299+
@compound CO2 ~ C + 2O
300+
@compound H2 ~ 2H
301+
@compound CH4 ~ C + 4H
302+
@compound H2O ~ 2H + O
303+
rx = @reaction 1.0, CO + CO2 + H2--> CH4 H2O
304304
balance_reaction(rx) # Multiple solutions.
305305
```
306306
307307
```julia
308308
@variables t
309309
@species Fe(t) S(t) O(t) H(t) N(t)
310-
@compound FeS2(t) = Fe + 2S
311-
@compound HNO3(t) = H + N + 3O
312-
@compound Fe2S3O12(t) = 2Fe + 3S + 12O
313-
@compound NO(t) = N + O
314-
@compound H2SO4(t) = 2H + S + 4O
315-
rx = Reaction(1.0, [FeS2, HNO3], [Fe2S3O12, NO, H2SO4])
310+
@compound FeS2 ~ Fe + 2S
311+
@compound HNO3 ~ H + N + 3O
312+
@compound Fe2S3O12 ~ 2Fe + 3S + 12O
313+
@compound NO ~ N + O
314+
@compound H2SO4 ~ 2H + S + 4O
315+
rx = @reaction 1.0, FeS2 + HNO3 --> Fe2S3O12 NO + H2SO4
316316
brxs = balance_reaction(rx) # No solution.
317317
```
318318

0 commit comments

Comments
 (0)