Skip to content

Commit a9670c4

Browse files
committed
retooling input map
1 parent 67f1146 commit a9670c4

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

src/networkapi.jl

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,17 +1657,20 @@ end
16571657
unitless_exp(u) = istree(u) && (operation(u) == ^) && (arguments(u)[1] == 1)
16581658

16591659
"""
1660-
iscomplexbalanced(rs::ReactionSystem, rates::Vector)
1660+
iscomplexbalanced(rs::ReactionSystem, parametermap)
16611661
16621662
Constructively compute whether a network will have complex-balanced equilibrium
1663-
solutions, following the method in [this paper](https://link.springer.com/article/10.1007/s10910-015-0498-2#Sec3). Accepts a map of rates [k1 => 1.0, k2 => 2.0,...]k2
1663+
solutions, following the method in van der Schaft et al., [2015](https://link.springer.com/article/10.1007/s10910-015-0498-2#Sec3). Accepts a dictionary, vector, or tuple ofvariable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
16641664
"""
16651665

1666-
function iscomplexbalanced(rs::ReactionSystem, rates::Dict{Any, Float64})
1667-
if length(rates) != numparams(rs)
1668-
error("The number of reaction rates must be equal to the number of parameters")
1666+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Dict)
1667+
if length(parametermap) != numparams(rs)
1668+
error("Incorrect number of parameters specified.")
16691669
end
16701670

1671+
pmap = symmap_to_varmap(rs, parametermap)
1672+
pmap = Dict(ModelingToolkit.value(k) => v for (k,v) in pmap)
1673+
16711674
sm = speciesmap(rs)
16721675
cm = reactioncomplexmap(rs)
16731676
complexes, D = reactioncomplexes(rs)
@@ -1678,14 +1681,16 @@ function iscomplexbalanced(rs::ReactionSystem, rates::Dict{Any, Float64})
16781681
error("The supplied ReactionSystem has reactions that are not ismassaction. Testing for being complex balanced is currently only supported for pure mass action networks.")
16791682
end
16801683

1684+
rates = [substitute(rate, pmap) for rate in reactionrates(rs)]
1685+
16811686
# Construct kinetic matrix, K
16821687
K = zeros(nr, nc)
16831688
for c in 1:nc
16841689
complex = complexes[c]
16851690
for (r, dir) in cm[complex]
16861691
rxn = rxns[r]
16871692
if dir == -1
1688-
K[r, c] = rates[rxn.rate]
1693+
K[r, c] = rates[r]
16891694
end
16901695
end
16911696
end
@@ -1697,22 +1702,36 @@ function iscomplexbalanced(rs::ReactionSystem, rates::Dict{Any, Float64})
16971702
g = incidencematgraph(rs)
16981703
R = ratematrix(rs, rates)
16991704
ρ = matrixtree(g, R)
1700-
@assert isapprox(L*ρ, zeros(nc), atol=1e-12)
17011705

17021706
# Determine if 1) ρ is positive and 2) D^T Ln ρ lies in the image of S^T
17031707
if all(>(0), ρ)
17041708
img = D'*log.(ρ)
1705-
rank(S') == rank(hcat(S', img)) ? return true : return false
1709+
if rank(S') == rank(hcat(S', img)) return true else return false end
17061710
else
17071711
return false
17081712
end
17091713
end
17101714

1711-
function ratematrix(rs::ReactionSystem, rates::Dict{Any, Float64})
1712-
if length(rates) != numparams(rs)
1713-
error("The number of reaction rates must be equal to the number of parameters")
1714-
end
1715+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Vector{Pair{Symbol, Float64}})
1716+
pdict = Dict(parametermap)
1717+
iscomplexbalanced(rs, pdict)
1718+
end
1719+
1720+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Tuple{Pair{Symbol, Float64}})
1721+
pdict = Dict(parametermap)
1722+
iscomplexbalanced(rs, pdict)
1723+
end
17151724

1725+
iscomplexbalanced(rs::ReactionSystem, parametermap) = error("Parameter map must be a dictionary, tuple, or vector of symbol/value pairs.")
1726+
1727+
1728+
"""
1729+
ratematrix(rs::ReactionSystem, parametermap)
1730+
1731+
Given a reaction system with n complexes, outputs an n-by-n matrix where R_{ij} is the rate constant of the reaction between complex i and complex j.
1732+
"""
1733+
1734+
function ratematrix(rs::ReactionSystem, rates::Vector{Float64})
17161735
complexes, D = reactioncomplexes(rs)
17171736
n = length(complexes)
17181737
rxns = reactions(rs)
@@ -1722,13 +1741,34 @@ function ratematrix(rs::ReactionSystem, rates::Dict{Any, Float64})
17221741
rxn = rxns[r]
17231742
s = findfirst(==(-1), @view D[:,r])
17241743
p = findfirst(==(1), @view D[:,r])
1725-
ratematrix[s, p] = rates[rxn.rate]
1744+
ratematrix[s, p] = rates[r]
17261745
end
17271746
ratematrix
17281747
end
17291748

1730-
"""
1731-
"""
1749+
function ratematrix(rs::ReactionSystem, parametermap::Dict{Symbol, Float64})
1750+
if length(parametermap) != numparams(rs)
1751+
error("The number of reaction rates must be equal to the number of parameters")
1752+
end
1753+
pmap = symmap_to_varmap(rs, parametermap)
1754+
rates = [substitute(rate, pmap) for rate in reactionrates(rs)]
1755+
ratematrix(rs, rates)
1756+
end
1757+
1758+
function ratematrix(rs::ReactionSystem, parametermap::Vector{Pair{Symbol, Float64}})
1759+
pdict = Dict(parametermap)
1760+
ratematrix(rs, pdict)
1761+
end
1762+
1763+
function ratematrix(rs::ReactionSystem, parametermap::Tuple{Pair{Symbol, Float64}})
1764+
pdict = Dict(parametermap)
1765+
ratematrix(rs, pdict)
1766+
end
1767+
1768+
ratematrix(rs::ReactionSystem, parametermap) = error("Parameter map must be a dictionary, tuple, or vector of symbol/value pairs.")
1769+
1770+
### BELOW: Helper functions for iscomplexbalanced
1771+
17321772
function matrixtree(g::SimpleDiGraph, distmx::Matrix)
17331773
n = nv(g)
17341774
if size(distmx) != (n, n)
@@ -1753,7 +1793,6 @@ function matrixtree(g::SimpleDiGraph, distmx::Matrix)
17531793
trees = collect(Combinatorics.combinations(collect(edges(ug)), n-1))
17541794
trees = SimpleGraph.(trees)
17551795
trees = filter!(t->isempty(Graphs.cycle_basis(t)), trees)
1756-
# trees = spanningtrees(g)
17571796

17581797
# constructed rooted trees for every vertex, compute sum
17591798
for v in 1:n
@@ -1773,10 +1812,3 @@ function treeweight(t::SimpleDiGraph, g::SimpleDiGraph, distmx::Matrix)
17731812
end
17741813
prod
17751814
end
1776-
1777-
# TODO: implement Winter's algorithm for generating spanning trees
1778-
function spanningtrees(g::SimpleGraph)
1779-
1780-
end
1781-
1782-
sm = speciesmap(rs)

0 commit comments

Comments
 (0)