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
Returns `true` if the input is a compound species (else false).
16
+
"""
17
+
iscompound(s::Num) =iscompound(MT.value(s))
18
+
functioniscompound(s)
19
+
MT.getmetadata(s, CompoundSpecies, false)
20
+
end
21
+
22
+
"""
23
+
components(s)
24
+
25
+
Returns a vector with a list of all the components of a compound species (created using e.g. the @compound macro).
26
+
"""
27
+
components(s::Num) =components(MT.value(s))
28
+
functioncomponents(s)
29
+
MT.getmetadata(s, CompoundComponents)
30
+
end
31
+
32
+
"""
33
+
coefficients(s)
34
+
35
+
Returns a vector with a list of all the stoichiometric coefficients of the components of a compound species (created using e.g. the @compound macro).
36
+
"""
37
+
coefficients(s::Num) =coefficients(MT.value(s))
38
+
functioncoefficients(s)
39
+
MT.getmetadata(s, CompoundCoefficients)
40
+
end
41
+
42
+
"""
43
+
component_coefficients(s)
44
+
45
+
Returns a Vector{Pari{Symbol,Int64}}, listing a compounds species (created using e.g. the @compound macro) all the coefficients and their stoichiometric coefficients.
return [c => co for (c, co) inzip(components(s), coefficients(s))]
50
+
end
51
+
52
+
10
53
### Create @compound Macro(s) ###
11
54
12
55
"""
@@ -146,135 +189,9 @@ function make_compounds(expr)
146
189
return compound_declarations
147
190
end
148
191
149
-
## Compound Getters ###
150
-
151
-
"""
152
-
iscompound(s)
153
-
154
-
Returns `true` if the input is a compound species (else false).
155
-
"""
156
-
iscompound(s::Num) =iscompound(MT.value(s))
157
-
functioniscompound(s)
158
-
MT.getmetadata(s, CompoundSpecies, false)
159
-
end
160
-
161
-
"""
162
-
components(s)
163
-
164
-
Returns a vector with a list of all the components of a compound species (created using e.g. the @compound macro).
165
-
"""
166
-
components(s::Num) =components(MT.value(s))
167
-
functioncomponents(s)
168
-
MT.getmetadata(s, CompoundComponents)
169
-
end
170
-
171
-
"""
172
-
coefficients(s)
173
-
174
-
Returns a vector with a list of all the stoichiometric coefficients of the components of a compound species (created using e.g. the @compound macro).
175
-
"""
176
-
coefficients(s::Num) =coefficients(MT.value(s))
177
-
functioncoefficients(s)
178
-
MT.getmetadata(s, CompoundCoefficients)
179
-
end
180
-
181
-
"""
182
-
component_coefficients(s)
183
-
184
-
Returns a Vector{Pari{Symbol,Int64}}, listing a compounds species (created using e.g. the @compound macro) all the coefficients and their stoichiometric coefficients.
# Add the atom to the atoms array if it's not already present
228
-
push!(unique_atoms, atom)
229
-
n_atoms +=1
230
-
A = [A; zeros(Int, 1, ncompounds)]
231
-
i = n_atoms
232
-
end
233
-
234
-
# Adjust coefficient based on whether the compound is a product or substrate
235
-
coeff *= coeffsign
236
-
237
-
A[i, j] = coeff
238
-
end
239
-
end
240
-
241
-
# update for iterating through products
242
-
coeffsign =-1
243
-
jbase =length(substrates)
244
-
end
245
-
246
-
return A
247
-
end
248
-
249
-
# Internal function used by "balance_reaction".
250
-
functionget_balanced_stoich(reaction::Reaction)
251
-
# Create the reaction matrix A that is m atoms by n compounds
252
-
A =create_matrix(reaction)
253
-
254
-
# get an integer nullspace basis
255
-
X = ModelingToolkit.nullspace(A)
256
-
nullity =size(X, 2)
257
-
258
-
stoichvecs =Vector{Vector{Int64}}()
259
-
for (j, col) inenumerate(eachcol(X))
260
-
signs =unique(col)
261
-
262
-
# If there is only one basis vector and the signs are not all the same this means
263
-
# we have a solution that would require moving at least one substrate to be a
264
-
# product (or vice-versa). We therefore do not return anything in this case.
265
-
# If there are multiple basis vectors we don't currently determine if we can
266
-
# construct a linear combination giving a solution, so we just return them.
267
-
if (nullity >1) || (all(>=(0), signs) ||all(<=(0), signs))
268
-
coefs =abs.(col)
269
-
common_divisor =reduce(gcd, coefs)
270
-
coefs .=div.(coefs, common_divisor)
271
-
push!(stoichvecs, coefs)
272
-
end
273
-
end
274
-
275
-
return stoichvecs
276
-
end
277
-
278
195
"""
279
196
balance_reaction(reaction::Reaction)
280
197
@@ -345,3 +262,87 @@ function balance_reaction(reaction::Reaction)
345
262
(length(balancedrxs) >1) && (@warn"Infinite balanced reactions from ($reaction) are possible, returning a basis for them. Note that we do not check if they preserve the set of substrates and products from the original reaction.")
346
263
return balancedrxs
347
264
end
265
+
266
+
# Internal function used by "balance_reaction".
267
+
functionget_balanced_stoich(reaction::Reaction)
268
+
# Create the reaction matrix A that is m atoms by n compounds
269
+
A =create_matrix(reaction)
270
+
271
+
# get an integer nullspace basis
272
+
X = ModelingToolkit.nullspace(A)
273
+
nullity =size(X, 2)
274
+
275
+
stoichvecs =Vector{Vector{Int64}}()
276
+
for (j, col) inenumerate(eachcol(X))
277
+
signs =unique(col)
278
+
279
+
# If there is only one basis vector and the signs are not all the same this means
280
+
# we have a solution that would require moving at least one substrate to be a
281
+
# product (or vice-versa). We therefore do not return anything in this case.
282
+
# If there are multiple basis vectors we don't currently determine if we can
283
+
# construct a linear combination giving a solution, so we just return them.
284
+
if (nullity >1) || (all(>=(0), signs) ||all(<=(0), signs))
285
+
coefs =abs.(col)
286
+
common_divisor =reduce(gcd, coefs)
287
+
coefs .=div.(coefs, common_divisor)
288
+
push!(stoichvecs, coefs)
289
+
end
290
+
end
291
+
292
+
return stoichvecs
293
+
end
294
+
295
+
# Reaction balancing error.
296
+
const COMPOUND_OF_COMPOUND_ERROR =ErrorException("Reaction balancing does not currently work for reactions involving compounds of compounds.")
297
+
298
+
# Note this does not correctly handle compounds of compounds currently.
299
+
# Internal function used by "balance_reaction" (via "get_balanced_stoich").
0 commit comments