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 2 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
36 changes: 36 additions & 0 deletions src/reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,42 @@ end
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)
get_variables!(set, rx.rate)
append!(set, rx.substrates)
append!(set, rx.products)
TorkelE marked this conversation as resolved.
Show resolved Hide resolved
for stoichs in (rx.substoich, rx.prodstoich), stoich in stoichs
get_variables!(set, stoich)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this check if stoich is a Symbolic? If not there is no reason to call get_variables!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is definitely an improvement.

end
if has_noise_scaling(rx)
get_variables!(set, get_noise_scaling(rx))
end
return unique!(set)
TorkelE marked this conversation as resolved.
Show resolved Hide resolved
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