Skip to content

Commit a29517c

Browse files
committed
init
1 parent a45a1bc commit a29517c

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/reaction.jl

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

346+
"""
347+
get_variables(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 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)])
364+
end
365+
if has_noise_scaling(rx)
366+
sym_vars = unique!([sym_vars; get_variables(get_noise_scaling(rx))])
367+
end
368+
return sym_vars
369+
end
346370

347371
### Dependency-related Functions ###
348372

test/reactionsystem_core/reaction.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
# Fetch packages.
44
using Catalyst, Test
55

6+
# Sets the default `t` to use.
7+
t = default_t()
8+
9+
### Test Basic Accessors ###
10+
11+
# Tests the `get_variables` function.
12+
let
13+
# Declare symbolic variables.
14+
@parameters k1 k2 n1 n2 η1 η2
15+
@species X(t) Y(t) Z(t)
16+
@variables A(t)
17+
18+
# Create `Reaction`s.
19+
rx1 = Reaction(k1, [], [X])
20+
rx2 = Reaction(k1 + k2, [X], [Y], [1], [n1]; metadata = [:noise_scaling => η1])
21+
rx3 = Reaction(k1 + k2 + A, [X], [X, Y, Z], [1], [n1 + n2, 2, 1])
22+
rx4 = Reaction(X + t, [], [Y]; metadata = [:noise_scaling => η1 + η2])
23+
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])
29+
end
30+
631
### Tests Metadata ###
732

833
# Tests creation.

0 commit comments

Comments
 (0)