Skip to content

Commit b5542f0

Browse files
committed
up
1 parent 3aebc3d commit b5542f0

File tree

2 files changed

+78
-76
lines changed

2 files changed

+78
-76
lines changed

src/reactionsystem_conversions.jl

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ function oderatelaw(rx; combinatoric_ratelaw = true)
4242
rl
4343
end
4444

45-
function assemble_drift(rs, ispcs; combinatoric_ratelaws = true, as_odes = true,
46-
include_zero_odes = true, remove_conserved = false)
47-
rhsvec = assemble_oderhs(rs, ispcs; combinatoric_ratelaws, remove_conserved)
48-
if as_odes
49-
D = Differential(get_iv(rs))
50-
eqs = [Equation(D(x), rhs)
51-
for (x, rhs) in zip(ispcs, rhsvec) if (include_zero_odes || (!_iszero(rhs)))]
52-
else
53-
eqs = [Equation(0, rhs) for rhs in rhsvec if (include_zero_odes || (!_iszero(rhs)))]
54-
end
55-
eqs
56-
end
57-
5845
function assemble_oderhs(rs, ispcs; combinatoric_ratelaws = true, remove_conserved = false)
5946
nps = get_networkproperties(rs)
6047
species_to_idx = Dict(x => i for (i, x) in enumerate(ispcs))
@@ -98,6 +85,19 @@ function assemble_oderhs(rs, ispcs; combinatoric_ratelaws = true, remove_conserv
9885
rhsvec
9986
end
10087

88+
function assemble_drift(rs, ispcs; combinatoric_ratelaws = true, as_odes = true,
89+
include_zero_odes = true, remove_conserved = false)
90+
rhsvec = assemble_oderhs(rs, ispcs; combinatoric_ratelaws, remove_conserved)
91+
if as_odes
92+
D = Differential(get_iv(rs))
93+
eqs = [Equation(D(x), rhs)
94+
for (x, rhs) in zip(ispcs, rhsvec) if (include_zero_odes || (!_iszero(rhs)))]
95+
else
96+
eqs = [Equation(0, rhs) for rhs in rhsvec if (include_zero_odes || (!_iszero(rhs)))]
97+
end
98+
eqs
99+
end
100+
101101
# this doesn't work with constraint equations currently
102102
function assemble_diffusion(rs, sts, ispcs; combinatoric_ratelaws = true,
103103
remove_conserved = false)
@@ -258,6 +258,57 @@ function ismassaction(rx, rs; rxvars = get_variables(rx.rate),
258258
return true
259259
end
260260

261+
@inline function makemajump(rx; combinatoric_ratelaw = true)
262+
@unpack rate, substrates, substoich, netstoich = rx
263+
zeroorder = (length(substoich) == 0)
264+
reactant_stoch = Vector{Pair{Any, eltype(substoich)}}()
265+
@inbounds for (i, spec) in enumerate(substrates)
266+
# move constant species into the rate
267+
if isconstant(spec)
268+
rate *= spec
269+
isone(substoich[i]) && continue
270+
for i in 1:(substoich[i] - 1)
271+
rate *= spec - i
272+
end
273+
else
274+
push!(reactant_stoch, substrates[i] => substoich[i])
275+
end
276+
end
277+
278+
if (!zeroorder) && combinatoric_ratelaw
279+
coef = prod(factorial, substoich)
280+
(!isone(coef)) && (rate /= coef)
281+
end
282+
283+
net_stoch = filter(p -> !drop_dynamics(p[1]), netstoich)
284+
isempty(net_stoch) &&
285+
error("$rx has no net stoichiometry change once accounting for constant and boundary condition species. This is not supported.")
286+
287+
MassActionJump(Num(rate), reactant_stoch, net_stoch, scale_rates = false,
288+
useiszero = false)
289+
end
290+
291+
# recursively visit each neighbor's rooted tree and mark everything in it as vrj
292+
function dfs_mark!(isvrjvec, visited, depgraph, i)
293+
visited[i] = true
294+
nhbrs = depgraph[i]
295+
for nhbr in nhbrs
296+
if !visited[nhbr]
297+
isvrjvec[nhbr] = true
298+
dfs_mark!(isvrjvec, visited, depgraph, nhbr)
299+
end
300+
end
301+
nothing
302+
end
303+
304+
# get_depgraph(rs)[i] is the list of reactions with rates depending on species changed by
305+
# i'th reaction.
306+
function get_depgraph(rs)
307+
jdeps = asgraph(rs)
308+
vdeps = variable_dependencies(rs)
309+
eqeq_dependencies(jdeps, vdeps).fadjlist
310+
end
311+
261312
function assemble_jumps(rs; combinatoric_ratelaws = true)
262313
meqs = MassActionJump[]
263314
ceqs = ConstantRateJump[]
@@ -323,57 +374,6 @@ function assemble_jumps(rs; combinatoric_ratelaws = true)
323374
vcat(meqs, ceqs, veqs)
324375
end
325376

326-
# get_depgraph(rs)[i] is the list of reactions with rates depending on species changed by
327-
# i'th reaction.
328-
function get_depgraph(rs)
329-
jdeps = asgraph(rs)
330-
vdeps = variable_dependencies(rs)
331-
eqeq_dependencies(jdeps, vdeps).fadjlist
332-
end
333-
334-
# recursively visit each neighbor's rooted tree and mark everything in it as vrj
335-
function dfs_mark!(isvrjvec, visited, depgraph, i)
336-
visited[i] = true
337-
nhbrs = depgraph[i]
338-
for nhbr in nhbrs
339-
if !visited[nhbr]
340-
isvrjvec[nhbr] = true
341-
dfs_mark!(isvrjvec, visited, depgraph, nhbr)
342-
end
343-
end
344-
nothing
345-
end
346-
347-
@inline function makemajump(rx; combinatoric_ratelaw = true)
348-
@unpack rate, substrates, substoich, netstoich = rx
349-
zeroorder = (length(substoich) == 0)
350-
reactant_stoch = Vector{Pair{Any, eltype(substoich)}}()
351-
@inbounds for (i, spec) in enumerate(substrates)
352-
# move constant species into the rate
353-
if isconstant(spec)
354-
rate *= spec
355-
isone(substoich[i]) && continue
356-
for i in 1:(substoich[i] - 1)
357-
rate *= spec - i
358-
end
359-
else
360-
push!(reactant_stoch, substrates[i] => substoich[i])
361-
end
362-
end
363-
364-
if (!zeroorder) && combinatoric_ratelaw
365-
coef = prod(factorial, substoich)
366-
(!isone(coef)) && (rate /= coef)
367-
end
368-
369-
net_stoch = filter(p -> !drop_dynamics(p[1]), netstoich)
370-
isempty(net_stoch) &&
371-
error("$rx has no net stoichiometry change once accounting for constant and boundary condition species. This is not supported.")
372-
373-
MassActionJump(Num(rate), reactant_stoch, net_stoch, scale_rates = false,
374-
useiszero = false)
375-
end
376-
377377

378378
### Equation Coupling ###
379379

src/reactionsystem_structure.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,13 @@ function reactionparamsmap(network)
794794
Dict(p => i for (i, p) in enumerate(reactionparams(network)))
795795
end
796796

797+
# used in the next function (`reactions(network)`).
798+
function namespace_reactions(network::ReactionSystem)
799+
rxs = reactions(network)
800+
isempty(rxs) && return Reaction[]
801+
map(rx -> namespace_equation(rx, network), rxs)
802+
end
803+
797804
"""
798805
reactions(network)
799806
@@ -809,11 +816,6 @@ function reactions(network)
809816
[rxs; reduce(vcat, namespace_reactions.(systems); init = Reaction[])]
810817
end
811818

812-
function namespace_reactions(network::ReactionSystem)
813-
rxs = reactions(network)
814-
isempty(rxs) && return Reaction[]
815-
map(rx -> namespace_equation(rx, network), rxs)
816-
end
817819

818820
"""
819821
numreactions(network)
@@ -1267,12 +1269,6 @@ function make_empty_network(; iv = DEFAULT_IV, name = gensym(:ReactionSystem))
12671269
end
12681270

12691271
# A helper function used in `flatten`.
1270-
function getsubsystypes(sys)
1271-
typeset = Set{Type}()
1272-
getsubsystypes!(typeset, sys)
1273-
typeset
1274-
end
1275-
12761272
function getsubsystypes!(typeset::Set{Type}, sys::T) where {T <: MT.AbstractSystem}
12771273
push!(typeset, T)
12781274
for subsys in get_systems(sys)
@@ -1281,6 +1277,12 @@ function getsubsystypes!(typeset::Set{Type}, sys::T) where {T <: MT.AbstractSyst
12811277
typeset
12821278
end
12831279

1280+
function getsubsystypes(sys)
1281+
typeset = Set{Type}()
1282+
getsubsystypes!(typeset, sys)
1283+
typeset
1284+
end
1285+
12841286
"""
12851287
Catalyst.flatten(rs::ReactionSystem)
12861288
@@ -1433,4 +1435,4 @@ function validate(rs::ReactionSystem, info::String = "")
14331435
end
14341436

14351437
# Checks if a unit consist of exponents with base 1 (and is this unitless).
1436-
unitless_exp(u) = istree(u) && (operation(u) == ^) && (arguments(u)[1] == 1)
1438+
(u) = istree(u) && (operation(u) == ^) && (arguments(u)[1] == 1)

0 commit comments

Comments
 (0)