@@ -133,6 +133,35 @@ function gentypefun_exprs(name; esc_exprs=true, gen_inplace=true, gen_outofplace
133
133
exprs
134
134
end
135
135
136
+ # ####################### reaction network operators #######################
137
+
138
+ # note, this is allocating, and could potentially be made more efficient by iterating
139
+ # through reaction components directly...
140
+ """
141
+ ==(rn1::DiffEqBase.AbstractReactionNetwork, rn2::DiffEqBase.AbstractReactionNetwork)
142
+
143
+ Tests whether the underlying species symbols, parameter symbols and reactions
144
+ are the same in the two networks. Ignores order network components were defined,
145
+ so the integer id of any individual species/parameters/reactions may be
146
+ different between the two networks. *Does not* currently account for different
147
+ reaction definitions, so "k, X+Y --> Y + Z" will not be the same as "k, Y+X -->
148
+ Y + Z"
149
+ """
150
+ function (== )(rn1:: DiffEqBase.AbstractReactionNetwork , rn2:: DiffEqBase.AbstractReactionNetwork )
151
+ issetequal (species (rn1), species (rn2)) || return false
152
+ issetequal (params (rn1), params (rn2)) || return false
153
+ nr1 = numreactions (rn1)
154
+ nr2 = numreactions (rn2)
155
+ (nr1 == nr2) || return false
156
+ idx1 = 1 : nr1
157
+ idx2 = 1 : nr2
158
+ issetequal (substrates .(rn1, idx1), substrates .(rn2, idx2)) || return false
159
+ issetequal (products .(rn1, idx1), products .(rn2, idx2)) || return false
160
+ issetequal (dependants .(rn1, idx1), dependants .(rn2, idx2)) || return false
161
+ issetequal (rateexpr .(rn1, idx1), rateexpr .(rn2, idx2)) || return false
162
+ issetequal (ismassaction .(rn1, idx1), ismassaction .(rn2, idx2)) || return false
163
+ end
164
+
136
165
# ####################### functions to extend a network ####################
137
166
138
167
"""
@@ -209,88 +238,88 @@ the noise scaling coefficient.
209
238
add_scale_noise_param! (rn:: DiffEqBase.AbstractReactionNetwork , scale_noise_name:: String ) = add_scale_noise_param! (rn, Symbol (scale_noise_name))
210
239
211
240
"""
212
- addreaction!(network, rateexpr ::Union{Expr,Symbol,Int,Float64}, rxexpr::Expr)
241
+ addreaction!(network, rateex ::Union{Expr,Symbol,Int,Float64}, rxexpr::Expr)
213
242
214
243
Given an AbstractReaction network, add a reaction with the passed in rate and
215
244
reaction expressions. i.e. a reaction of the form
216
245
```julia
217
246
k*X, 2X + Y --> 2W
218
247
```
219
- would have `rateexpr =:(k*X)` and `rxexpr=:(2X + Y --> W)`,
248
+ would have `rateex =:(k*X)` and `rxexpr=:(2X + Y --> W)`,
220
249
```julia
221
250
10.5, 0 --> X
222
251
```
223
- would have `rateexpr =10.5` and `rxexpr=:(0 --> X)`, and
252
+ would have `rateex =10.5` and `rxexpr=:(0 --> X)`, and
224
253
```julia
225
254
k, X+X --> Z
226
255
```
227
- would have `rateexpr =:k` and `rxexpr=:(X+X --> Z)`.
256
+ would have `rateex =:k` and `rxexpr=:(X+X --> Z)`.
228
257
229
258
All normal DSL reaction definition notation should be supported.
230
259
"""
231
- function addreaction! (rn:: DiffEqBase.AbstractReactionNetwork , rateexpr :: ExprValues , rxexpr:: Expr )
232
- ex = Expr (:block , :(($ rateexpr , $ rxexpr)))
260
+ function addreaction! (rn:: DiffEqBase.AbstractReactionNetwork , rateex :: ExprValues , rxexpr:: Expr )
261
+ ex = Expr (:block , :(($ rateex , $ rxexpr)))
233
262
newrxs = get_reactions (ex)
234
263
foreach (rx -> push! (rn. reactions,ReactionStruct (rx, species (rn))), newrxs)
235
264
nothing
236
265
end
237
266
238
267
"""
239
- addreaction!(network, rateexpr ::Union{Expr,Symbol,Int,Float64}, substrates, products)
268
+ addreaction!(network, rateex ::Union{Expr,Symbol,Int,Float64}, substrates, products)
240
269
241
270
Given an AbstractReaction network, add a reaction with the passed in rate,
242
- `rateexpr `, substrate stoichiometry, and product stoichiometry. Stoichiometries
271
+ `rateex `, substrate stoichiometry, and product stoichiometry. Stoichiometries
243
272
are represented as tuples of `Pair{Symbol,Int}`. i.e. a reaction of the form
244
273
```julia
245
274
k*X, 2X + Y --> 2W
246
275
```
247
- would have `rateexpr =:(k*X)`, `substrates=(:X=>2, :Y=>2)`` and
276
+ would have `rateex =:(k*X)`, `substrates=(:X=>2, :Y=>2)`` and
248
277
`products=(W=>2,)`,
249
278
```julia
250
279
10.5, 0 --> X
251
280
```
252
- would have `rateexpr =10.5`, `substrates=()` and `products=(:X=>1,)`, and
281
+ would have `rateex =10.5`, `substrates=()` and `products=(:X=>1,)`, and
253
282
```julia
254
283
k, X+X --> Z
255
284
```
256
- would have `rateexpr =:k`, `substrates=(:X=>2,)` and `products=(:Z=>2,)`.
285
+ would have `rateex =:k`, `substrates=(:X=>2,)` and `products=(:Z=>2,)`.
257
286
258
287
All normal DSL reaction definition notation should be supported for the
259
- `rateexpr `.
288
+ `rateex `.
260
289
"""
261
- function addreaction! (rn:: DiffEqBase.AbstractReactionNetwork , rateexpr :: ExprValues ,
290
+ function addreaction! (rn:: DiffEqBase.AbstractReactionNetwork , rateex :: ExprValues ,
262
291
subs:: Tuple{Vararg{Pair{Symbol,Int}}} ,
263
292
prods:: Tuple{Vararg{Pair{Symbol,Int}}} ) where {T <: Number }
264
293
265
294
substrates = ReactantStruct[ReactantStruct (p[1 ],p[2 ]) for p in subs]
266
295
dependents = Symbol[p[1 ] for p in subs]
267
296
products = ReactantStruct[ReactantStruct (p[1 ],p[2 ]) for p in prods]
268
- rate_DE = mass_rate_DE (substrates, true , rateexpr )
269
- rate_SSA = mass_rate_SSA (substrates, true , rateexpr )
297
+ rate_DE = mass_rate_DE (substrates, true , rateex )
298
+ rate_SSA = mass_rate_SSA (substrates, true , rateex )
270
299
271
- # resolve dependents from rateexpr
272
- if rateexpr isa Number
300
+ # resolve dependents from rateex
301
+ if rateex isa Number
273
302
ismassaction = true
274
- elseif rateexpr isa Symbol
275
- if haskey (speciesmap (rn), rateexpr )
303
+ elseif rateex isa Symbol
304
+ if haskey (speciesmap (rn), rateex )
276
305
ismassaction = false
277
- if rateexpr ∉ dependents
278
- push! (dependents, rateexpr )
306
+ if rateex ∉ dependents
307
+ push! (dependents, rateex )
279
308
end
280
- elseif haskey (paramsmap (rn), rateexpr )
309
+ elseif haskey (paramsmap (rn), rateex )
281
310
ismassaction = true
282
311
else
283
- error (" rateexpr is a symbol that is neither a species or parameter." )
312
+ error (" rateex is a symbol that is neither a species or parameter." )
284
313
end
285
314
else # isa Expr
286
315
287
316
# mimicing ReactionStruct constructor for now, but this should be optimized...
288
317
newdeps = unique! (recursive_content (rate_DE, speciesmap (rn), Vector {Symbol} ()))
289
- ismassaction = length (newdeps) == length ( intersect! ( dependents, newdeps)) ? true : false
318
+ ismassaction = issetequal ( dependents,newdeps)
290
319
dependents = newdeps
291
320
end
292
321
293
- push! (rn. reactions, ReactionStruct (substrates, products, rateexpr , rate_DE, rate_SSA, dependents, ismassaction))
322
+ push! (rn. reactions, ReactionStruct (substrates, products, rateex , rate_DE, rate_SSA, dependents, ismassaction))
294
323
nothing
295
324
end
296
325
0 commit comments