Skip to content

Commit 90524ba

Browse files
committed
change to get_variables!
1 parent a29517c commit 90524ba

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/reaction.jl

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ MT.is_diff_equation(rx::Reaction) = false
344344
MT.is_alg_equation(rx::Reaction) = false
345345

346346
"""
347-
get_variables(rx::Reaction)
347+
get_symbolics(set, rx::Reaction)
348348
349349
Returns all symbolic variables that are part of a reaction. This includes all variables
350350
encountered in:
@@ -353,19 +353,31 @@ encountered in:
353353
- Among stoichiometries.
354354
- Among potential noise scaling metadata.
355355
"""
356-
function ModelingToolkit.get_variables(rx::Reaction)
357-
sym_vars = get_variables(rx.rate)
358-
sym_vars = unique!([sym_vars; rx.substrates; rx.products])
359-
for stoich in rx.substoich
360-
sym_vars = unique!([sym_vars; get_variables(stoich)])
361-
end
362-
for stoich in rx.prodstoich
363-
sym_vars = unique!([sym_vars; get_variables(stoich)])
356+
function get_symbolics(rx::Reaction)
357+
return ModelingToolkit.get_variables!([], rx)
358+
end
359+
360+
"""
361+
get_variables!(set, rx::Reaction)
362+
363+
Adds all symbolic variables that are part of a reaction to set. This includes all variables
364+
encountered in:
365+
- Rates.
366+
- Among substrates and products.
367+
- Among stoichiometries.
368+
- Among potential noise scaling metadata.
369+
"""
370+
function ModelingToolkit.get_variables!(set, rx::Reaction)
371+
get_variables!(set, rx.rate)
372+
append!(set, rx.substrates)
373+
append!(set, rx.products)
374+
for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs
375+
get_variables!(set, stoich)
364376
end
365377
if has_noise_scaling(rx)
366-
sym_vars = unique!([sym_vars; get_variables(get_noise_scaling(rx))])
378+
get_variables!(set, get_noise_scaling(rx))
367379
end
368-
return sym_vars
380+
return unique!(set)
369381
end
370382

371383
### Dependency-related Functions ###

test/reactionsystem_core/reaction.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Fetch packages.
44
using Catalyst, Test
5+
using Catalyst: get_symbolics
6+
using ModelingToolkit: value, get_variables!
57

68
# Sets the default `t` to use.
79
t = default_t()
@@ -11,7 +13,7 @@ t = default_t()
1113
# Tests the `get_variables` function.
1214
let
1315
# Declare symbolic variables.
14-
@parameters k1 k2 n1 n2 η1 η2
16+
@parameters k1 k2 n1 n2 η1 η2 p
1517
@species X(t) Y(t) Z(t)
1618
@variables A(t)
1719

@@ -21,11 +23,17 @@ let
2123
rx3 = Reaction(k1 + k2 + A, [X], [X, Y, Z], [1], [n1 + n2, 2, 1])
2224
rx4 = Reaction(X + t, [], [Y]; metadata = [:noise_scaling => η1 + η2])
2325

24-
# Test `get_variables`.
25-
@test issetequal(get_variables(rx1), [k1, X])
26-
@test issetequal(get_variables(rx2), [k1, k2, X, Y, n1, η1])
27-
@test issetequal(get_variables(rx3), [k1, k2, A, X, Y, Z, n1, n2])
28-
@test issetequal(get_variables(rx4), [X, t, Y, η1, η2])
26+
# Test `get_variables!`.
27+
@test issetequal(get_variables!([value(p)], rx1), [k1, X, p])
28+
@test issetequal(get_variables!([value(p)], rx2), [k1, k2, X, Y, n1, η1, p])
29+
@test issetequal(get_variables!([value(p)], rx3), [k1, k2, A, X, Y, Z, n1, n2, p])
30+
@test issetequal(get_variables!([value(p)], rx4), [X, t, Y, η1, η2, p])
31+
32+
# Test `get_symbolics`.
33+
@test issetequal(get_symbolics(rx1), [k1, X])
34+
@test issetequal(get_symbolics(rx2), [k1, k2, X, Y, n1, η1])
35+
@test issetequal(get_symbolics(rx3), [k1, k2, A, X, Y, Z, n1, n2])
36+
@test issetequal(get_symbolics(rx4), [X, t, Y, η1, η2])
2937
end
3038

3139
### Tests Metadata ###

0 commit comments

Comments
 (0)