Skip to content

Commit 617d89f

Browse files
committed
Error for species in TR rate added and tested
1 parent fc47179 commit 617d89f

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/spatial_reaction_systems/spatial_reactions.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ struct TransportReaction <: AbstractSpatialReaction
2929

3030
# Creates a diffusion reaction.
3131
function TransportReaction(rate, species)
32+
if any(!ModelingToolkit.isparameter(var) for var in ModelingToolkit.get_variables(rate))
33+
error("TransportReaction rate contain variables: $(filter(var -> !ModelingToolkit.isparameter(var), ModelingToolkit.get_variables(rate))). The rate must consist of parameters only.")
34+
end
3235
new(rate, species.val)
3336
end
3437
end
@@ -76,15 +79,29 @@ spatial_species(tr::TransportReaction) = [tr.species]
7679

7780
# Checks that a transport reaction is valid for a given reaction system.
7881
function check_spatial_reaction_validity(rs::ReactionSystem, tr::TransportReaction; edge_parameters=[])
79-
# Checks that the species exist in the reaction system (ODE simulation code becomes difficult if this is not required, as non-spatial jacobian and f function generated from rs is of wrong size).
80-
any(isequal(tr.species), species(rs)) || error("Currently, species used in TransportReactions must have previously been declared within the non-spatial ReactionSystem. This is not the case for $(tr.species).")
82+
# Checks that the species exist in the reaction system.
83+
# (ODE simulation code becomes difficult if this is not required, as non-spatial jacobian and f function generated from rs is of wrong size).
84+
if !any(isequal(tr.species), species(rs))
85+
error("Currently, species used in TransportReactions must have previously been declared within the non-spatial ReactionSystem. This is not the case for $(tr.species).")
86+
end
87+
8188
# Checks that the rate does not depend on species.
82-
isempty(intersect(ModelingToolkit.getname.(species(rs)), ModelingToolkit.getname.(Symbolics.get_variables(tr.rate)))) || error("The following species were used in rates of a transport reactions: $(setdiff(ModelingToolkit.getname.(species(rs)), ModelingToolkit.getname.(Symbolics.get_variables(tr.rate)))).")
89+
if !isempty(intersect(ModelingToolkit.getname.(species(rs)), ModelingToolkit.getname.(Symbolics.get_variables(tr.rate))))
90+
error("The following species were used in rates of a transport reactions: $(setdiff(ModelingToolkit.getname.(species(rs)), ModelingToolkit.getname.(Symbolics.get_variables(tr.rate)))).")
91+
end
92+
8393
# Checks that the species does not exist in the system with different metadata.
84-
any([isequal(tr.species, s) && !isequivalent(tr.species, s) for s in species(rs)]) && error("A transport reaction used a species, $(tr.species), with metadata not matching its lattice reaction system. Please fetch this species from the reaction system and used in transport reaction creation.")
85-
any([isequal(rs_p, tr_p) && !equivalent_metadata(rs_p, tr_p) for rs_p in parameters(rs), tr_p in Symbolics.get_variables(tr.rate)]) && error("A transport reaction used a parameter with metadata not matching its lattice reaction system. Please fetch this parameter from the reaction system and used in transport reaction creation.")
94+
if any([isequal(tr.species, s) && !isequivalent(tr.species, s) for s in species(rs)])
95+
error("A transport reaction used a species, $(tr.species), with metadata not matching its lattice reaction system. Please fetch this species from the reaction system and used in transport reaction creation.")
96+
end
97+
if any([isequal(rs_p, tr_p) && !equivalent_metadata(rs_p, tr_p) for rs_p in parameters(rs), tr_p in Symbolics.get_variables(tr.rate)])
98+
error("A transport reaction used a parameter with metadata not matching its lattice reaction system. Please fetch this parameter from the reaction system and used in transport reaction creation.")
99+
end
100+
86101
# Checks that no edge parameter occur among rates of non-spatial reactions.
87-
any([!isempty(intersect(Symbolics.get_variables(r.rate), edge_parameters)) for r in reactions(rs)]) && error("Edge paramter(s) were found as a rate of a non-spatial reaction.")
102+
if any([!isempty(intersect(Symbolics.get_variables(r.rate), edge_parameters)) for r in reactions(rs)])
103+
error("Edge paramter(s) were found as a rate of a non-spatial reaction.")
104+
end
88105
end
89106
equivalent_metadata(p1, p2) = isempty(setdiff(p1.metadata, p2.metadata, [Catalyst.EdgeParameter => true]))
90107

test/spatial_reaction_systems/lattice_reaction_systems.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ end
242242

243243
### Tests Error generation ###
244244

245+
# Test creation of TransportReaction with non-parameters in rate.
246+
# Tests that it works even when rate is highly nested.
247+
let
248+
@variables t
249+
@species X(t) Y(t)
250+
@parameters D1 D2 D3
251+
@test_throws ErrorException TransportReaction(D1 + D2*(D3 + Y), X)
252+
@test_throws ErrorException TransportReaction(Y, X)
253+
end
254+
245255
# Network where diffusion species is not declared in non-spatial network.
246256
let
247257
rs = @reaction_network begin

0 commit comments

Comments
 (0)