Skip to content

Commit f712acf

Browse files
authored
Merge pull request #855 from SciML/get_variables_for_reactions
Add `get_variables` dispatch for `Reaction`
2 parents 080b89f + 8bca45b commit f712acf

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/reaction.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,45 @@ end
343343
MT.is_diff_equation(rx::Reaction) = false
344344
MT.is_alg_equation(rx::Reaction) = false
345345

346+
"""
347+
get_symbolics(set, rx::Reaction)
348+
349+
Returns all symbolic variables that are part of a reaction. This includes all variables
350+
encountered in:
351+
- Rates.
352+
- Among substrates and products.
353+
- Among stoichiometries.
354+
- Among potential noise scaling metadata.
355+
"""
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+
if isdefined(Main, :Infiltrator)
372+
Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__)
373+
end
374+
get_variables!(set, rx.rate)
375+
foreach(sub -> push!(set, sub), rx.substrates)
376+
foreach(prod -> push!(set, prod), rx.products)
377+
for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs
378+
(stoich isa BasicSymbolic) && get_variables!(set, stoich)
379+
end
380+
if has_noise_scaling(rx)
381+
get_variables!(set, get_noise_scaling(rx))
382+
end
383+
return (set isa AbstractVector) ? unique!(set) : set
384+
end
346385

347386
### Dependency-related Functions ###
348387

test/reactionsystem_core/reaction.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,39 @@
22

33
# Fetch packages.
44
using Catalyst, Test
5+
using Catalyst: get_symbolics
6+
using ModelingToolkit: value, get_variables!
7+
8+
# Sets the default `t` to use.
9+
t = default_t()
10+
11+
### Test Basic Accessors ###
12+
13+
# Tests the `get_variables` function.
14+
let
15+
# Declare symbolic variables.
16+
@parameters k1 k2 n1 n2 η1 η2 p
17+
@species X(t) Y(t) Z(t)
18+
@variables A(t)
19+
20+
# Create `Reaction`s.
21+
rx1 = Reaction(k1, [], [X])
22+
rx2 = Reaction(k1 + k2, [X], [Y], [1], [n1]; metadata = [:noise_scaling => η1])
23+
rx3 = Reaction(k1 + k2 + A, [X], [X, Y, Z], [1], [n1 + n2, 2, 1])
24+
rx4 = Reaction(X + t, [], [Y]; metadata = [:noise_scaling => η1 + η2])
25+
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])
37+
end
538

639
### Tests Metadata ###
740

0 commit comments

Comments
 (0)