Skip to content

Commit

Permalink
[Python] Simplify and generalize conversion of sparse matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Aug 30, 2022
1 parent 2ec692c commit 5b2132a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 84 deletions.
18 changes: 0 additions & 18 deletions include/cantera/cython/kinetics_utils.h
Expand Up @@ -53,26 +53,12 @@ inline void sparseCscData(const Eigen::SparseMatrix<double>& mat,
}
}

// Function which passes sparse matrix
#define SPARSE_MATRIX(PREFIX, CLASS_NAME, FUNC_NAME) \
inline Eigen::SparseMatrix<double> PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object) \
{ return object->FUNC_NAME(); }

#define KIN_SPARSE_MATRIX(FUNC_NAME) SPARSE_MATRIX(kin, Kinetics, FUNC_NAME)
#define KIN_1D(FUNC_NAME) ARRAY_FUNC(kin, Kinetics, FUNC_NAME)

KIN_SPARSE_MATRIX(reactantStoichCoeffs)
KIN_SPARSE_MATRIX(productStoichCoeffs)
KIN_SPARSE_MATRIX(revProductStoichCoeffs)

KIN_1D(getFwdRatesOfProgress)
KIN_1D(getRevRatesOfProgress)
KIN_1D(getNetRatesOfProgress)

KIN_SPARSE_MATRIX(fwdRatesOfProgress_ddX)
KIN_SPARSE_MATRIX(revRatesOfProgress_ddX)
KIN_SPARSE_MATRIX(netRatesOfProgress_ddX)

KIN_1D(getFwdRateConstants_ddT)
KIN_1D(getFwdRateConstants_ddP)
KIN_1D(getFwdRateConstants_ddC)
Expand Down Expand Up @@ -106,10 +92,6 @@ KIN_1D(getCreationRates)
KIN_1D(getDestructionRates)
KIN_1D(getNetProductionRates)

KIN_SPARSE_MATRIX(creationRates_ddX)
KIN_SPARSE_MATRIX(destructionRates_ddX)
KIN_SPARSE_MATRIX(netProductionRates_ddX)

KIN_1D(getCreationRates_ddT)
KIN_1D(getDestructionRates_ddT)
KIN_1D(getNetProductionRates_ddT)
Expand Down
28 changes: 14 additions & 14 deletions interfaces/cython/cantera/kinetics.pxd
Expand Up @@ -51,6 +51,19 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
void getDerivativeSettings(CxxAnyMap&) except +translate_exception
void setDerivativeSettings(CxxAnyMap&) except +translate_exception

# Kinetics sparse matrices
CxxSparseMatrix reactantStoichCoeffs() except +translate_exception
CxxSparseMatrix productStoichCoeffs() except +translate_exception
CxxSparseMatrix revProductStoichCoeffs() except +translate_exception

CxxSparseMatrix fwdRatesOfProgress_ddX() except +translate_exception
CxxSparseMatrix revRatesOfProgress_ddX() except +translate_exception
CxxSparseMatrix netRatesOfProgress_ddX() except +translate_exception

CxxSparseMatrix creationRates_ddX() except +translate_exception
CxxSparseMatrix destructionRates_ddX() except +translate_exception
CxxSparseMatrix netProductionRates_ddX() except +translate_exception


cdef extern from "cantera/kinetics/InterfaceKinetics.h":
cdef cppclass CxxInterfaceKinetics "Cantera::InterfaceKinetics":
Expand Down Expand Up @@ -113,18 +126,6 @@ cdef extern from "cantera/cython/kinetics_utils.h":
cdef void kin_getDestructionRates_ddC(CxxKinetics*, double*) except +translate_exception
cdef void kin_getNetProductionRates_ddC(CxxKinetics*, double*) except +translate_exception

# Kinetics sparse matrices
cdef CxxSparseMatrix kin_reactantStoichCoeffs(CxxKinetics*) except +translate_exception
cdef CxxSparseMatrix kin_productStoichCoeffs(CxxKinetics*) except +translate_exception
cdef CxxSparseMatrix kin_revProductStoichCoeffs(CxxKinetics*) except +translate_exception

cdef CxxSparseMatrix kin_fwdRatesOfProgress_ddX(CxxKinetics*) except +translate_exception
cdef CxxSparseMatrix kin_revRatesOfProgress_ddX(CxxKinetics*) except +translate_exception
cdef CxxSparseMatrix kin_netRatesOfProgress_ddX(CxxKinetics*) except +translate_exception

cdef CxxSparseMatrix kin_creationRates_ddX(CxxKinetics*) except +translate_exception
cdef CxxSparseMatrix kin_destructionRates_ddX(CxxKinetics*) except +translate_exception
cdef CxxSparseMatrix kin_netProductionRates_ddX(CxxKinetics*) except +translate_exception


ctypedef void (*kineticsMethod1d)(CxxKinetics*, double*) except +translate_exception
Expand All @@ -138,5 +139,4 @@ cdef class InterfaceKinetics(Kinetics):

cdef np.ndarray get_species_array(Kinetics kin, kineticsMethod1d method)
cdef np.ndarray get_reaction_array(Kinetics kin, kineticsMethod1d method)
cdef np.ndarray get_dense(Kinetics kin, kineticsMethodSparse method)
cdef tuple get_sparse(Kinetics kin, kineticsMethodSparse method)
cdef get_from_sparse(CxxSparseMatrix&, int, int)
81 changes: 29 additions & 52 deletions interfaces/cython/cantera/kinetics.pyx
Expand Up @@ -31,11 +31,10 @@ cdef np.ndarray get_reaction_array(Kinetics kin, kineticsMethod1d method):
method(kin.kinetics, &data[0])
return data

cdef np.ndarray get_dense(Kinetics kin, kineticsMethodSparse method):
cdef CxxSparseMatrix smat = method(kin.kinetics)
cdef np.ndarray get_dense(CxxSparseMatrix& smat):
cdef size_t length = smat.nonZeros()
if length == 0:
return np.zeros((kin.n_reactions, 0))
return np.zeros((smat.rows(), 0))

# index/value triplets
cdef np.ndarray[int, ndim=1, mode="c"] rows = np.empty(length, dtype=c_int)
Expand All @@ -48,10 +47,7 @@ cdef np.ndarray get_dense(Kinetics kin, kineticsMethodSparse method):
out[rows[i], cols[i]] = data[i]
return out

cdef tuple get_sparse(Kinetics kin, kineticsMethodSparse method):
# retrieve sparse matrix
cdef CxxSparseMatrix smat = method(kin.kinetics)

cdef get_sparse(CxxSparseMatrix& smat):
# pointers to values and inner indices of CSC storage
cdef size_t length = smat.nonZeros()
cdef np.ndarray[np.double_t, ndim=1] value = np.empty(length)
Expand All @@ -64,6 +60,14 @@ cdef tuple get_sparse(Kinetics kin, kineticsMethodSparse method):
CxxSparseCscData(smat, &value[0], &inner[0], &outer[0])
return value, inner, outer

cdef get_from_sparse(CxxSparseMatrix& smat, int rows, int cols):
if _utils._USE_SPARSE:
tup = get_sparse(smat)
return _utils._scipy_sparse.csc_matrix(tup, shape=(rows, cols))
else:
return get_dense(smat)


cdef class Kinetics(_SolutionBase):
"""
Instances of class `Kinetics` are responsible for evaluating reaction rates
Expand Down Expand Up @@ -271,11 +275,8 @@ cdef class Kinetics(_SolutionBase):
Method was changed to a property in Cantera 3.0.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_reactantStoichCoeffs)
shape = self.n_total_species, self.n_reactions
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_reactantStoichCoeffs)
return get_from_sparse(self.kinetics.reactantStoichCoeffs(),
self.n_total_species, self.n_reactions)

property reactant_stoich_coeffs3:
"""
Expand Down Expand Up @@ -308,11 +309,8 @@ cdef class Kinetics(_SolutionBase):
Method was changed to a property in Cantera 3.0.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_productStoichCoeffs)
shape = self.n_total_species, self.n_reactions
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_productStoichCoeffs)
return get_from_sparse(self.kinetics.productStoichCoeffs(),
self.n_total_species, self.n_reactions)

property product_stoich_coeffs3:
"""
Expand Down Expand Up @@ -341,11 +339,8 @@ cdef class Kinetics(_SolutionBase):
For sparse output, set ``ct.use_sparse(True)``.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_revProductStoichCoeffs)
shape = self.n_total_species, self.n_reactions
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_revProductStoichCoeffs)
return get_from_sparse(self.kinetics.revProductStoichCoeffs(),
self.n_total_species, self.n_reactions)

property forward_rates_of_progress:
"""
Expand Down Expand Up @@ -519,11 +514,8 @@ cdef class Kinetics(_SolutionBase):
may be changed or removed without notice.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_fwdRatesOfProgress_ddX)
shape = self.n_reactions, self.n_total_species
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_fwdRatesOfProgress_ddX)
return get_from_sparse(self.kinetics.fwdRatesOfProgress_ddX(),
self.n_reactions, self.n_total_species)

property reverse_rates_of_progress_ddT:
"""
Expand Down Expand Up @@ -565,11 +557,8 @@ cdef class Kinetics(_SolutionBase):
may be changed or removed without notice.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_revRatesOfProgress_ddX)
shape = self.n_reactions, self.n_total_species
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_revRatesOfProgress_ddX)
return get_from_sparse(self.kinetics.revRatesOfProgress_ddX(),
self.n_reactions, self.n_total_species)

property net_rates_of_progress_ddT:
"""
Expand Down Expand Up @@ -611,11 +600,8 @@ cdef class Kinetics(_SolutionBase):
may be changed or removed without notice.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_netRatesOfProgress_ddX)
shape = self.n_reactions, self.n_total_species
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_netRatesOfProgress_ddX)
return get_from_sparse(self.kinetics.netRatesOfProgress_ddX(),
self.n_reactions, self.n_total_species)

property creation_rates_ddT:
"""
Expand Down Expand Up @@ -657,11 +643,8 @@ cdef class Kinetics(_SolutionBase):
may be changed or removed without notice.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_creationRates_ddX)
shape = self.n_total_species, self.n_total_species
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_creationRates_ddX)
return get_from_sparse(self.kinetics.creationRates_ddX(),
self.n_total_species, self.n_total_species)

property destruction_rates_ddT:
"""
Expand Down Expand Up @@ -703,11 +686,8 @@ cdef class Kinetics(_SolutionBase):
may be changed or removed without notice.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_destructionRates_ddX)
shape = self.n_total_species, self.n_total_species
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_destructionRates_ddX)
return get_from_sparse(self.kinetics.destructionRates_ddX(),
self.n_total_species, self.n_total_species)

property net_production_rates_ddT:
"""
Expand Down Expand Up @@ -749,11 +729,8 @@ cdef class Kinetics(_SolutionBase):
may be changed or removed without notice.
"""
def __get__(self):
if _utils._USE_SPARSE:
tup = get_sparse(self, kin_netProductionRates_ddX)
shape = self.n_total_species, self.n_total_species
return _utils._scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_netProductionRates_ddX)
return get_from_sparse(self.kinetics.netProductionRates_ddX(),
self.n_total_species, self.n_total_species)

property delta_enthalpy:
"""Change in enthalpy for each reaction [J/kmol]."""
Expand Down

0 comments on commit 5b2132a

Please sign in to comment.