-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathreaction.py
193 lines (164 loc) · 7.42 KB
/
reaction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from typing import Optional, Union, List
from ufl import exp
from ufl.core.expr import Expr
from festim import k_B as _k_B
from festim.species import ImplicitSpecies as _ImplicitSpecies
from festim.species import Species as _Species
from festim.subdomain.volume_subdomain_1d import VolumeSubdomain1D as VS1D
class Reaction:
"""A reaction between two species, with a forward and backward rate.
Arguments:
reactant (Union[F.Species, F.ImplicitSpecies], List[Union[F.Species, F.ImplicitSpecies]]): The reactant.
product (Optional[Union[F.Species, List[F.Species]]]): The product.
k_0 (float): The forward rate constant pre-exponential factor.
E_k (float): The forward rate constant activation energy.
p_0 (float): The backward rate constant pre-exponential factor.
E_p (float): The backward rate constant activation energy.
volume (F.VolumeSubdomain1D): The volume subdomain where the reaction takes place.
Attributes:
reactant (Union[F.Species, F.ImplicitSpecies], List[Union[F.Species, F.ImplicitSpecies]]): The reactant.
product (Optional[Union[F.Species, List[F.Species]]]): The product.
k_0 (float): The forward rate constant pre-exponential factor.
E_k (float): The forward rate constant activation energy.
p_0 (float): The backward rate constant pre-exponential factor.
E_p (float): The backward rate constant activation energy.
volume (F.VolumeSubdomain1D): The volume subdomain where the reaction takes place.
Usage:
>>> # create two species
>>> reactant = [F.Species("A"), F.Species("B")]
>>> # create a product species
>>> product = F.Species("C")
>>> # create a reaction between the two species
>>> reaction = Reaction(reactant, product, k_0=1.0, E_k=0.2, p_0=0.1, E_p=0.3)
>>> print(reaction)
A + B <--> C
>>> # compute the reaction term at a given temperature
>>> temperature = 300.0
>>> reaction_term = reaction.reaction_term(temperature)
"""
def __init__(
self,
reactant: _Species | _ImplicitSpecies | list[_Species | _ImplicitSpecies],
k_0: float,
E_k: float,
volume: VS1D,
product: Optional[Union[_Species, list[_Species]]] = [],
p_0: float = None,
E_p: float = None,
) -> None:
self.k_0 = k_0
self.E_k = E_k
self.p_0 = p_0
self.E_p = E_p
self.reactant = reactant
self.product = product
self.volume = volume
@property
def reactant(self):
return self._reactant
@reactant.setter
def reactant(self, value):
if not isinstance(value, list):
value = [value]
if len(value) == 0:
raise ValueError(
"reactant must be an entry of one or more species objects, not an empty list."
)
for i in value:
if not isinstance(i, (_Species, _ImplicitSpecies)):
raise TypeError(
"reactant must be an F.Species or F.ImplicitSpecies, not "
+ f"{type(i)}"
)
self._reactant = value
def __repr__(self) -> str:
reactants = " + ".join([str(reactant) for reactant in self.reactant])
if isinstance(self.product, list):
products = " + ".join([str(product) for product in self.product])
else:
products = self.product
return f"Reaction({reactants} <--> {products}, {self.k_0}, {self.E_k}, {self.p_0}, {self.E_p})"
def __str__(self) -> str:
reactants = " + ".join([str(reactant) for reactant in self.reactant])
if isinstance(self.product, list):
products = " + ".join([str(product) for product in self.product])
else:
products = self.product
return f"{reactants} <--> {products}"
def reaction_term(
self,
temperature,
reactant_concentrations: List = None,
product_concentrations: List = None,
) -> Expr:
"""Compute the reaction term at a given temperature.
Arguments:
temperature: The temperature at which the reaction term is computed.
reactant_concentrations: The concentrations of the reactants. Must
be the same length as the reactants. If None, the ``concentration``
attribute of each reactant is used. If an element is None, the
``concentration`` attribute of the reactant is used.
product_concentrations: The concentrations of the products. Must
be the same length as the products. If None, the ``concentration``
attribute of each product is used. If an element is None, the
``concentration`` attribute of the product is used.
Returns:
The reaction term to be used in a formulation.
"""
if self.product == []:
if self.p_0 is not None:
raise ValueError(
f"p_0 must be None, not {self.p_0}"
+ " when no products are present."
)
if self.E_p is not None:
raise ValueError(
f"E_p must be None, not {self.E_p}"
+ " when no products are present."
)
else:
if self.p_0 == None:
raise ValueError(
"p_0 cannot be None when reaction products are present."
)
elif self.E_p == None:
raise ValueError(
"E_p cannot be None when reaction products are present."
)
products = self.product if isinstance(self.product, list) else [self.product]
# reaction rates
k = self.k_0 * exp(-self.E_k / (_k_B * temperature))
if self.p_0 and self.E_p:
p = self.p_0 * exp(-self.E_p / (_k_B * temperature))
elif self.p_0:
p = self.p_0
else:
p = 0
# if reactant_concentrations is provided, use these concentrations
reactants = self.reactant
if reactant_concentrations is not None:
assert len(reactant_concentrations) == len(reactants)
for i, reactant in enumerate(reactants):
if reactant_concentrations[i] is None:
reactant_concentrations[i] = reactant.concentration
else:
reactant_concentrations = [reactant.concentration for reactant in reactants]
# if product_concentrations is provided, use these concentrations
if product_concentrations is not None:
assert len(product_concentrations) == len(products)
for i, product in enumerate(products):
if product_concentrations[i] is None:
product_concentrations[i] = product.concentration
else:
product_concentrations = [product.concentration for product in products]
# multiply all concentrations to be used in the term
product_of_reactants = reactant_concentrations[0]
for reactant_conc in reactant_concentrations[1:]:
product_of_reactants *= reactant_conc
if products:
product_of_products = product_concentrations[0]
for product_conc in product_concentrations[1:]:
product_of_products *= product_conc
else:
product_of_products = 0
return k * product_of_reactants - (p * product_of_products)