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

Disallow repeated reactants and making hashing order independent #616

Merged
merged 6 commits into from
Feb 21, 2023
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
14 changes: 9 additions & 5 deletions src/networkapi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1302,11 +1302,15 @@ end

function hash(rx::Reaction, h::UInt)
h = Base.hash(rx.rate, h)
h = Base.hash(rx.substrates, h)
h = Base.hash(rx.products, h)
h = Base.hash(rx.prodstoich, h)
h = Base.hash(rx.substoich, h)
h = Base.hash(rx.netstoich, h)
for s in Iterators.flatten((rx.substrates, rx.products))
h ⊻= hash(s)
end
for s in Iterators.flatten((rx.substoich, rx.prodstoich))
h ⊻= hash(s)
end
for s in rx.netstoich
h ⊻= hash(s)
end
Base.hash(rx.only_use_rate, h)
end

Expand Down
4 changes: 4 additions & 0 deletions src/reactionsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ function Reaction(rate, subs, prods, substoich, prodstoich;
else
subs = value.(subs)
end
allunique(subs) ||
throw(ArgumentError("Substrates can not be repeated in the list provided to `Reaction`, please modify the stoichiometry for any repeated substrates instead."))
S = eltype(substoich)

if isnothing(prods)
Expand All @@ -152,6 +154,8 @@ function Reaction(rate, subs, prods, substoich, prodstoich;
else
prods = value.(prods)
end
allunique(prods) ||
throw(ArgumentError("Products can not be repeated in the list provided to `Reaction`, please modify the stoichiometry for any repeated products instead."))
T = eltype(prodstoich)

# try to get a common type for stoichiometry, using Any if have Syms
Expand Down
24 changes: 24 additions & 0 deletions test/reactionsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,27 @@ let
@test issetequal(parameters(osys), [k1, k2])
@test length(equations(osys)) == 3
end

# test errors for repeated substrates or products
let
@variables t
@species A(t) B(t)
@test_throws ArgumentError Reaction(1.0, [A, A, B], [B])
@test_throws ArgumentError Reaction(1.0, [B], [A, A])
@test_throws ArgumentError Reaction(1.0, [A, A], [B, B])
end

# test order of species and products doesn't matter for equality or hashing
let
@variables t
@species A(t) α(t)
rx = Reaction(1.0, [α, A], [α, A], [2, 3], [4, 5])
rx2 = Reaction(1.0, [A, α], [A, α], [3, 2], [5, 4])
@test rx == rx2
@test hash(rx) == hash(rx2)

rx = Reaction(1.0, [α, A], [α, A], [2, 3], [4, 5]; netstoich = [α => 2, A => 2])
rx2 = Reaction(1.0, [A, α], [A, α], [3, 2], [5, 4]; netstoich = [A => 2, α => 2])
@test rx == rx2
@test hash(rx) == hash(rx2)
end