Skip to content

Commit

Permalink
Revert "Stoichiometry matrix is now sparse. This should help memory p…
Browse files Browse the repository at this point in the history
…roblems."

This reverts commit 401aff2.

Building the LIL sparse matrix was very slow, so testing the dense one again.
Should be a temporary thing...

Conflicts: (resolved)

	source/rmg/model.py
  • Loading branch information
rwest committed Oct 28, 2009
1 parent d7418f1 commit e525b08
Showing 1 changed file with 3 additions and 34 deletions.
37 changes: 3 additions & 34 deletions source/rmg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import math
import numpy
import scipy.integrate
import scipy.sparse


import constants
Expand Down Expand Up @@ -236,28 +235,10 @@ def getStoichiometryMatrix(self):
columns represent the reactions in the core and edge in order.
"""
speciesList, reactionList = self.getLists()
#stoichiometry = numpy.zeros((len(speciesList), len(reactionList)), float)
stoichiometry = scipy.sparse.lil_matrix((len(speciesList), len(reactionList)), dtype=float)
# sparse matrix examples at http://www.scipy.org/SciPy_Tutorial
stoichiometry = numpy.zeros((len(speciesList), len(reactionList)), float)
for j, rxn in enumerate(reactionList):
for i, spec in enumerate(speciesList):
stoichiometry[i,j] = rxn.getStoichiometricCoefficient(spec)

# Advantages of the LIL format
# - supports flexible slicing
# - changes to the matrix sparsity structure are efficient
#
# Disadvantages of the LIL format
# - arithmetic operations LIL + LIL are slow (consider CSR or CSC)
# - slow column slicing (consider CSC)
# - slow matrix vector products (consider CSR or CSC)
#
# Intended Usage
# - LIL is a convenient format for constructing sparse matrices
# - once a matrix has been constructed, convert to CSR or
# CSC format for fast arithmetic and matrix vector operations
# - consider using the COO format when constructing large matrices
stoichiometry.tocsc() # convert to CSC format for faster matrix vector operations
return stoichiometry

def getReactionRates(self, T, P, Ci):
Expand Down Expand Up @@ -724,15 +705,8 @@ def getResidual(self, t, y, model, stoichiometry):
rxnRate = self.getReactionRates(P, V, T, Ni, model)

# Species balances

# can't take a zero-width slice of a sparse matrix, so special-case it:
if not model.core.reactions:
dNidt = numpy.zeros([len(model.core.species)])
else:
dNidt = ( stoichiometry[0:len(model.core.species), 0:len(model.core.reactions)] *
rxnRate[0:len(model.core.reactions)] ) # stoichiometry matrix is sparse
# old version for dense matrix:
# dNidt = numpy.dot(stoichiometry.todense()[0:len(model.core.species), 0:len(model.core.reactions)],rxnRate[0:len(model.core.reactions)])
dNidt = numpy.dot(stoichiometry[0:len(model.core.species), 0:len(model.core.reactions)],
rxnRate[0:len(model.core.reactions)])

# Energy balance (assume isothermal for now)
dTdt = 0.0
Expand Down Expand Up @@ -794,12 +768,7 @@ def getSpeciesFluxes(self, model, P, V, T, Ni, stoichiometry):
matrix for the model.
"""
rxnRates = self.getReactionRates(P, V, T, Ni, model)
return stoichiometry * rxnRates
# seems to have the same result as this
return numpy.dot(stoichiometry.todense(),rxnRates)
# which is the same as this
return numpy.dot(stoichiometry, rxnRates)
# in the old days of a dense stoichiometry matrix

def isModelValid(self, model, dNidt, criticalFlux):
"""
Expand Down

0 comments on commit e525b08

Please sign in to comment.