Skip to content

Commit 3edf040

Browse files
authored
Merge pull request #231 from isaacsas/disable-api-checks
disable checks for adding an exisiting variable
2 parents b481905 + f203e57 commit 3edf040

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/networkapi.jl

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,20 @@ function make_empty_network(; iv=Variable(:t))
169169
end
170170

171171
"""
172-
addspecies!(network::ReactionSystem, s::Variable)
172+
addspecies!(network::ReactionSystem, s::Variable; disablechecks=false)
173173
174-
Given a [`ReactionSystem`](@ref), add the species corresponding to the
175-
variable `s` to the network (if it is not already defined). Returns the
176-
integer id of the species within the system.
174+
Given a [`ReactionSystem`](@ref), add the species corresponding to the variable
175+
`s` to the network (if it is not already defined). Returns the integer id of the
176+
species within the system.
177+
178+
Notes:
179+
- `disablechecks` will disable checking for whether the passed in variable is
180+
already defined, which is useful when adding many new variables to the system.
181+
*Do not disable checks* unless you are sure the passed in variable is a new
182+
variable, as this will potentially leave the system in an undefined state.
177183
"""
178-
function addspecies!(network::ReactionSystem, s::Variable)
179-
curidx = findfirst(S -> isequal(S, s), species(network))
184+
function addspecies!(network::ReactionSystem, s::Variable; disablechecks=false)
185+
curidx = disablechecks ? nothing : findfirst(S -> isequal(S, s), species(network))
180186
if curidx === nothing
181187
push!(network.states, s)
182188
return length(species(network))
@@ -186,26 +192,36 @@ function addspecies!(network::ReactionSystem, s::Variable)
186192
end
187193

188194
"""
189-
addspecies!(network::ReactionSystem, s::Operation)
195+
addspecies!(network::ReactionSystem, s::Operation; disablechecks=false)
190196
191197
Given a [`ReactionSystem`](@ref), add the species corresponding to the
192198
variable `s` to the network (if it is not already defined). Returns the
193199
integer id of the species within the system.
200+
201+
- `disablechecks` will disable checking for whether the passed in variable is
202+
already defined, which is useful when adding many new variables to the system.
203+
*Do not disable checks* unless you are sure the passed in variable is a new
204+
variable, as this will potentially leave the system in an undefined state.
194205
"""
195-
function addspecies!(network::ReactionSystem, s::Operation)
206+
function addspecies!(network::ReactionSystem, s::Operation; disablechecks=false)
196207
!(s.op isa Variable) && error("If the passed in species is an Operation, it must correspond to an underlying Variable.")
197-
addspecies!(network, convert(Variable,s))
208+
addspecies!(network, convert(Variable,s); disablechecks=disablechecks)
198209
end
199210

200211
"""
201-
addparam!(network::ReactionSystem, p::Variable)
212+
addparam!(network::ReactionSystem, p::Variable; disablechecks=false)
202213
203214
Given a [`ReactionSystem`](@ref), add the parameter corresponding to the
204-
variable `p` to the network (if it is not already defined). Returns the
205-
integer id of the parameter within the system.
215+
variable `p` to the network (if it is not already defined). Returns the integer
216+
id of the parameter within the system.
217+
218+
- `disablechecks` will disable checking for whether the passed in variable is
219+
already defined, which is useful when adding many new variables to the system.
220+
*Do not disable checks* unless you are sure the passed in variable is a new
221+
variable, as this will potentially leave the system in an undefined state.
206222
"""
207-
function addparam!(network::ReactionSystem, p::Variable)
208-
curidx = findfirst(S -> isequal(S, p), params(network))
223+
function addparam!(network::ReactionSystem, p::Variable; disablechecks=false)
224+
curidx = disablechecks ? nothing : findfirst(S -> isequal(S, p), params(network))
209225
if curidx === nothing
210226
push!(network.ps, p)
211227
return length(params(network))
@@ -215,15 +231,20 @@ function addparam!(network::ReactionSystem, p::Variable)
215231
end
216232

217233
"""
218-
addparam!(network::ReactionSystem, p::Operation)
234+
addparam!(network::ReactionSystem, p::Operation; disablechecks=false)
219235
220236
Given a [`ReactionSystem`](@ref), add the parameter corresponding to the
221237
variable `p` to the network (if it is not already defined). Returns the
222238
integer id of the parameter within the system.
239+
240+
- `disablechecks` will disable checking for whether the passed in variable is
241+
already defined, which is useful when adding many new variables to the system.
242+
*Do not disable checks* unless you are sure the passed in variable is a new
243+
variable, as this will potentially leave the system in an undefined state.
223244
"""
224-
function addparam!(network::ReactionSystem, p::Operation)
245+
function addparam!(network::ReactionSystem, p::Operation; disablechecks=false)
225246
!(p.op isa Variable) && error("If the passed in parameter is an Operation, it must correspond to an underlying Variable.")
226-
addparam!(network, convert(Variable,p))
247+
addparam!(network, convert(Variable,p); disablechecks=disablechecks)
227248
end
228249

229250
"""

test/api.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ rxs = [Reaction(k1*S, [S,I], [I], [2,3], [2]),
4242
rs = ReactionSystem(rxs, t, [S,I,R], [k1,k2])
4343
deps = [s.op for s in dependents(rxs[2], rs)]
4444
@test isequal(deps, [R.op,I.op])
45+
addspecies!(rs, Variable(:S))
46+
@test length(species(rs)) == 3
47+
addspecies!(rs, Variable(:S), disablechecks=true)
48+
@test length(species(rs)) == 4
49+
addparam!(rs, Variable(:k1))
50+
@test length(params(rs)) == 2
51+
addparam!(rs, Variable(:k1), disablechecks=true)
52+
@test length(params(rs)) == 3

0 commit comments

Comments
 (0)