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/catalyst_functionality/chemistry_related_functionality.md
+30-16Lines changed: 30 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,9 @@ using Catalyst
15
15
```
16
16
Next, we create the `CO2` compound species:
17
17
```@example chem1
18
-
@compound CO2(t) = C + 2O
18
+
@compound CO2 ~ C + 2O
19
19
```
20
-
Here, the compound is the first argument to the macro, followed by its component. The `(t)` indicates that `CO2` is a time dependant species. Components with non-unitary 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 a species in every sense, 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 as not designated for compounds (but instead directly inferred from their components). Components with non-unitary 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 a species in every sense, and can be used wherever e.g. `C` can be used:
21
21
```@example chem1
22
22
isspecies(CO2)
23
23
```
@@ -40,16 +40,16 @@ iscompound(CO2)
40
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
41
```@example chem1
42
42
@species H(t)
43
-
@compound H2O(t) = 2H + O
44
-
@compound H2CO3(t) = CO2 + H2O
43
+
@compound H2O ~ 2H + O
44
+
@compound H2CO3 ~ CO2 + H2O
45
45
```
46
46
47
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
48
```@example chem1
49
49
@species H(t)
50
50
@compounds begin
51
-
H2O(t) = 2H + O
52
-
H2CO3(t) = CO2 + H2O
51
+
H2O ~ 2H + O
52
+
H2CO3 ~ CO2 + H2O
53
53
end
54
54
```
55
55
@@ -59,9 +59,9 @@ It is also possible to declare species as compound species within the `@reaction
59
59
rn = @reaction_network begin
60
60
@species C(t) H(t) O(t)
61
61
@compounds begin
62
-
C2O(t) = C + 2O
63
-
H2O(t) = 2H + O
64
-
H2CO3(t) = CO2 + H2O
62
+
C2O ~ C + 2O
63
+
H2O ~ 2H + O
64
+
H2CO3 ~ CO2 + H2O
65
65
end
66
66
(k1,k2), H2O+ CO2 <--> H2CO3
67
67
end
@@ -70,15 +70,29 @@ When creating compound species using the DSL, it is important to note that *ever
70
70
```julia
71
71
rn =@reaction_networkbegin
72
72
@compoundsbegin
73
-
C2O(t) = C +2O
74
-
H2O(t) =2H + O
75
-
H2CO3(t) = CO2 + H2O
73
+
C2O~ C +2O
74
+
H2O~2H + O
75
+
H2CO3~ CO2 + H2O
76
76
end
77
77
(k1,k2), H2O+ CO2 <--> H2CO3
78
78
end
79
79
```
80
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
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. If default values are given, the left-hand side must be grouped using `()`:
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 teh default value:
92
+
```@example chem1
93
+
@compound (CO2 = 2.0, [unit="mol"]) ~ C + 2O
94
+
```
95
+
82
96
## Balancing chemical reactions
83
97
One use of defining a species as a compound is that they can be used to balance reactions to 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:
Copy file name to clipboardExpand all lines: src/chemistry_functionality.jl
+22-7Lines changed: 22 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -28,17 +28,28 @@ macro compound(expr)
28
28
make_compound(MacroTools.striplines(expr))
29
29
end
30
30
31
+
# Declares compound error messages:
32
+
const COMPOUND_CREATION_ERROR_BASE ="Malformed input to @compound. Should use form like e.g. \"@compound CO2 ~ C + 2O\"."
33
+
const COMPOUND_CREATION_ERROR_BAD_SEPARATOR ="Malformed input to @compound. Left-hand side (the compound) and the right-hand side (the components) should be separated by a \"~\" (e.g. \"@compound CO2 ~ C + 2O\"). If the left hand side contains metadata or default values, this should be enclosed by \"()\" (e.g. \"@compound (CO2 = 1.0, [output=true]) ~ C + 2O\")."
34
+
const COMPOUND_CREATION_ERROR_DEPENDENT_VAR_GIVEN ="Left hand side of @compound is malformed. Does the compound depend on a independent variable (e.g. \"CO2(t)\")? If so, that should not be the case, these are inferred from the compounds."
35
+
31
36
# Function managing the @compound macro.
32
37
functionmake_compound(expr)
33
38
# Error checks.
34
-
!(expr isa Expr) || (expr.head != :(=)) &&error("Malformed expression. Compounds should be declared using a \"=\".")
35
-
(length(expr.args) !=2) &&error("Malformed expression. Compounds should be consists of two expression, separated by a \"=\".")
36
-
((expr.args[1] isa Symbol) || (expr.args[1].head !=:call)) &&error("Malformed expression. There must be a single compound which depend on an independent variable, e.g. \"CO2(t)\".")
39
+
(expr isa Expr) ||error(COMPOUND_CREATION_ERROR_BASE)
# Loops through all components, add the component and the coefficients to the corresponding vectors (cannot extract directly using e.g. "getfield.(composition, :reactant)" because then we get something like :([:C, :O]), rather than :([C, O]))
44
55
components = :([]) # Becomes something like :([C, O]).
@@ -102,9 +113,13 @@ function make_compounds(expr)
102
113
# The output of the macros should be a vector with the compounds (same as for e.g. "@species begin ... end", also require for things to work in the DSL).
103
114
# Creates an output vector, and loops through all compounds, adding them to it.
Copy file name to clipboardExpand all lines: src/reaction_network.jl
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -471,7 +471,8 @@ function read_compound_options(opts)
471
471
# If the compound option is used retrive a list of compound species (need to be added to teh reaction system's species), and the option that creates them (used to declare them as compounds at the end).
472
472
ifhaskey(opts, :compounds)
473
473
compound_expr = opts[:compounds]
474
-
compound_species = [arg.args[1].args[1] for arg in compound_expr.args[3].args] # Loops through where in the "@compound begin ... end" the compound species names are.
474
+
# Find compound species names, and append the independent variable.
475
+
compound_species = [find_varname_in_declaration(arg.args[2]) for arg in compound_expr.args[3].args]
475
476
else# If option is not used, return empty vectors and expressions.
0 commit comments