Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix performance of stoichiometry conversion in flux_balance_constraints #27

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/frontend/balance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ function flux_balance_constraints(
bal = A.balance(model)
obj = A.objective(model)

# The iteration through stoichiometry would be better done with eachrow(),
# unfortunately it seems to be enormously inefficient on column-major
# matrix formats, and findnz() on eachrow&eachcol output isn't very
# backwards compatible (at least not to 1.6). We thus enforce having a
# SparseMatrixCSC here and walk it manually.
stoiT = SparseArrays.SparseMatrixCSC(stoi')

constraints = C.ConstraintTree(
:fluxes^C.variables(keys = rxns, bounds = zip(lbs, ubs)) *
:flux_stoichiometry^C.ConstraintTree(
met => C.Constraint(
value = C.LinearValue(SparseArrays.sparse(row)),
value = let i = stoiT.colptr[row_idx], e = stoiT.colptr[row_idx+1] - 1
C.LinearValue(idxs = stoiT.rowval[i:e], weights = stoiT.nzval[i:e])
end,
bound = C.EqualTo(b),
) for (met, row, b) in zip(mets, eachrow(stoi), bal)
) for (met, row_idx, b) in zip(mets, 1:stoiT.n, bal)
) *
:objective^C.Constraint(C.LinearValue(SparseArrays.sparse(obj))),
)
Expand Down
Loading