Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_variables dispatch for Reaction #855

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,45 @@
MT.is_diff_equation(rx::Reaction) = false
MT.is_alg_equation(rx::Reaction) = false

"""
get_symbolics(set, rx::Reaction)

Returns all symbolic variables that are part of a reaction. This includes all variables
encountered in:
- Rates.
- Among substrates and products.
- Among stoichiometries.
- Among potential noise scaling metadata.
"""
function get_symbolics(rx::Reaction)
return ModelingToolkit.get_variables!([], rx)
end

"""
get_variables!(set, rx::Reaction)

Adds all symbolic variables that are part of a reaction to set. This includes all variables
encountered in:
- Rates.
- Among substrates and products.
- Among stoichiometries.
- Among potential noise scaling metadata.
"""
function ModelingToolkit.get_variables!(set, rx::Reaction)
if isdefined(Main, :Infiltrator)
Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__)

Check warning on line 372 in src/reaction.jl

View check run for this annotation

Codecov / codecov/patch

src/reaction.jl#L372

Added line #L372 was not covered by tests
end
get_variables!(set, rx.rate)
foreach(sub -> push!(set, sub), rx.substrates)
foreach(prod -> push!(set, prod), rx.products)
for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs
(stoich isa BasicSymbolic) && get_variables!(set, stoich)
end
if has_noise_scaling(rx)
get_variables!(set, get_noise_scaling(rx))
end
return (set isa AbstractVector) ? unique!(set) : set
end

### Dependency-related Functions ###

Expand Down
33 changes: 33 additions & 0 deletions test/reactionsystem_core/reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@

# Fetch packages.
using Catalyst, Test
using Catalyst: get_symbolics
using ModelingToolkit: value, get_variables!

# Sets the default `t` to use.
t = default_t()

### Test Basic Accessors ###

# Tests the `get_variables` function.
let
# Declare symbolic variables.
@parameters k1 k2 n1 n2 η1 η2 p
@species X(t) Y(t) Z(t)
@variables A(t)

# Create `Reaction`s.
rx1 = Reaction(k1, [], [X])
rx2 = Reaction(k1 + k2, [X], [Y], [1], [n1]; metadata = [:noise_scaling => η1])
rx3 = Reaction(k1 + k2 + A, [X], [X, Y, Z], [1], [n1 + n2, 2, 1])
rx4 = Reaction(X + t, [], [Y]; metadata = [:noise_scaling => η1 + η2])

# Test `get_variables!`.
@test issetequal(get_variables!([value(p)], rx1), [k1, X, p])
@test issetequal(get_variables!([value(p)], rx2), [k1, k2, X, Y, n1, η1, p])
@test issetequal(get_variables!([value(p)], rx3), [k1, k2, A, X, Y, Z, n1, n2, p])
@test issetequal(get_variables!([value(p)], rx4), [X, t, Y, η1, η2, p])

# Test `get_symbolics`.
@test issetequal(get_symbolics(rx1), [k1, X])
@test issetequal(get_symbolics(rx2), [k1, k2, X, Y, n1, η1])
@test issetequal(get_symbolics(rx3), [k1, k2, A, X, Y, Z, n1, n2])
@test issetequal(get_symbolics(rx4), [X, t, Y, η1, η2])
end

### Tests Metadata ###

Expand Down
Loading