Skip to content

Commit 556ca71

Browse files
committed
cycle wip
1 parent 0a938b7 commit 556ca71

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/network_analysis.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,43 @@ function conservationlaw_errorcheck(rs, pre_varmap)
718718
isempty(conservedequations(Catalyst.flatten(rs))) ||
719719
error("The system has conservation laws but initial conditions were not provided for some species.")
720720
end
721+
722+
"""
723+
cycles(rs::ReactionSystem)
724+
725+
Returns the matrix of cycles, or right eigenvectors of the stoichiometric matrix.
726+
"""
727+
728+
function cycles(rs::ReactionSystem)
729+
# nps = get_networkproperties(rs)
730+
nsm = netstoichmat(rs)
731+
cycles(nsm)
732+
end
733+
734+
function cycles(nsm::T; col_order = nothing) where {T <: AbstractMatrix}
735+
736+
# compute the left nullspace over the integers
737+
N = MT.nullspace(nsm; col_order)
738+
739+
# if all coefficients for a conservation law are negative, make positive
740+
for Nrow in eachcol(N)
741+
all(r -> r <= 0, Nrow) && (Nrow .*= -1)
742+
end
743+
744+
# check we haven't overflowed
745+
iszero(nsm * N) || error("Calculation of the cycle matrix was inaccurate, "
746+
* "likely due to numerical overflow. Please use a larger integer "
747+
* "type like Int128 or BigInt for the net stoichiometry matrix.")
748+
749+
T(N)
750+
end
751+
752+
"""
753+
fluxmodes(rs::ReactionSystem)
754+
755+
See documentation for [`cycles`](@ref).
756+
"""
757+
758+
function fluxmodes(rs::ReactionSystem)
759+
cycles(rs)
760+
end

src/reactionsystem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Base.@kwdef mutable struct NetworkProperties{I <: Integer, V <: BasicSymbolic{Re
8080
isempty::Bool = true
8181
netstoichmat::Union{Matrix{Int}, SparseMatrixCSC{Int, Int}} = Matrix{Int}(undef, 0, 0)
8282
conservationmat::Matrix{I} = Matrix{I}(undef, 0, 0)
83+
cyclemat::Matrix{I} = Matrix{I}(undef, 0, 0)
8384
col_order::Vector{Int} = Int[]
8485
rank::Int = 0
8586
nullity::Int = 0
@@ -1456,4 +1457,4 @@ function validate(rs::ReactionSystem, info::String = "")
14561457
end
14571458

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

0 commit comments

Comments
 (0)