13 #if CT_SUNDIALS_USE_LAPACK
14 #if CT_SUNDIALS_VERSION >= 30
15 #include "sunlinsol/sunlinsol_lapackband.h"
17 #include "cvodes/cvodes_lapack.h"
20 #if CT_SUNDIALS_VERSION >= 30
21 #include "sunlinsol/sunlinsol_band.h"
23 #include "cvodes/cvodes_dense.h"
24 #include "cvodes/cvodes_band.h"
39 struct BandMatrix::PivData {
42 #elif CT_SUNDIALS_VERSION >= 30
43 std::vector<sunindextype> data;
44 #elif CT_SUNDIALS_VERSION >= 25
45 std::vector<long int> data;
47 std::vector<int> data;
51 BandMatrix::BandMatrix() :
56 m_ipiv{new PivData()},
61 BandMatrix::~BandMatrix()
71 m_ipiv{new PivData()},
74 data.resize(n*(2*kl + ku + 1));
75 ludata.resize(n*(2*kl + ku + 1));
80 m_lu_col_ptrs.resize(n);
81 size_t ldab = (2*kl + ku + 1);
82 for (
size_t j = 0; j < n; j++) {
84 m_lu_col_ptrs[j] = &
ludata[ldab * j];
94 m_ipiv{new PivData()},
104 m_lu_col_ptrs.resize(
m_n);
106 for (
size_t j = 0; j <
m_n; j++) {
108 m_lu_col_ptrs[j] = &
ludata[ldab * j];
112 BandMatrix& BandMatrix::operator=(
const BandMatrix& y)
117 GeneralMatrix::operator=(y);
121 m_ipiv->data = y.m_ipiv->data;
125 m_lu_col_ptrs.resize(
m_n);
126 size_t ldab = (2 *
m_kl +
m_ku + 1);
127 for (
size_t j = 0; j <
m_n; j++) {
129 m_lu_col_ptrs[j] = &
ludata[ldab * j];
140 data.resize(n*(2*kl + ku + 1));
141 ludata.resize(n*(2*kl + ku + 1));
145 m_lu_col_ptrs.resize(
m_n);
146 size_t ldab = (2 *
m_kl +
m_ku + 1);
147 for (
size_t j = 0; j < n; j++) {
149 m_lu_col_ptrs[j] = &
ludata[ldab * j];
156 std::fill(
data.begin(),
data.end(), v);
162 std::fill(
data.begin(),
data.end(), 0.0);
179 if (i + m_ku < j || i > j +
m_kl) {
187 if (i + m_ku < j || i > j +
m_kl) {
230 for (
size_t m = 0; m <
m_n; m++) {
232 size_t start = (m >=
m_kl) ? m -
m_kl : 0;
233 size_t stop = std::min(m +
m_ku + 1,
m_n);
234 for (
size_t j = start; j < stop; j++) {
235 sum +=
_value(m,j) * b[j];
243 for (
size_t n = 0; n <
m_n; n++) {
245 size_t start = (n >=
m_ku) ? n -
m_ku : 0;
246 size_t stop = std::min(n +
m_kl + 1,
m_n);
247 for (
size_t i = start; i < stop; i++) {
248 sum +=
_value(i,n) * b[i];
263 long int smu = nu + nl;
264 m_info = bandGBTRF(m_lu_col_ptrs.data(),
static_cast<long int>(
nColumns()),
265 nu, nl, smu,
m_ipiv->data.data());
269 "Factorization failed with DGBTRF error code {}.", m_info);
292 m_ipiv->data.data(), b, ldb, m_info);
296 long int smu = nu + nl;
297 double** a = m_lu_col_ptrs.data();
298 bandGBTRS(a,
static_cast<long int>(
nColumns()), smu, nl,
m_ipiv->data.data(), b);
304 "Linear solve failed with DGBTRS error code {}.", m_info);
333 for (
size_t i = 0; i < m.
nRows(); i++) {
335 for (
size_t j = 1; j < m.nColumns(); j++) {
349 throw CanteraError(
"BandMatrix::rcond",
"matrix isn't factored correctly");
358 throw CanteraError(
"BandMatrix::rcond",
"DGBCON returned INFO = {}", rinfo);
362 throw CanteraError(
"BandMatrix::rcond",
"not implemented when LAPACK is missing");
374 for (
size_t j = 0; j <
m_n; j++) {
376 size_t start = (j >=
m_ku) ? j -
m_ku : 0;
377 size_t stop = std::min(j +
m_kl + 1,
m_n);
378 for (
size_t i = start; i < stop; i++) {
379 sum += std::abs(
_value(i,j));
388 valueSmall = 1.0E300;
389 size_t iSmall =
npos;
390 for (
size_t i = 0; i <
m_n; i++) {
392 size_t start = (i >
m_kl) ? i -
m_kl : 0;
393 size_t stop = std::min(i +
m_ku + 1,
m_n);
394 for (
size_t j = start; j < stop; j++) {
395 valueS = std::max(fabs(
_value(i,j)), valueS);
397 if (valueS < valueSmall) {
400 if (valueSmall == 0.0) {
410 valueSmall = 1.0E300;
411 size_t jSmall =
npos;
412 for (
size_t j = 0; j <
m_n; j++) {
414 size_t start = (j >
m_ku) ? j -
m_ku : 0;
415 size_t stop = std::min(j +
m_kl + 1,
m_n);
416 for (
size_t i = start; i < stop; i++) {
417 valueS = std::max(fabs(
_value(i,j)), valueS);
419 if (valueS < valueSmall) {
422 if (valueSmall == 0.0) {
Declarations for the class BandMatrix which is a child class of GeneralMatrix for banded matrices han...
A class for banded matrices, involving matrix inversion processes.
virtual void zero()
Zero the matrix elements.
virtual doublereal * ptrColumn(size_t j)
Return a pointer to the top of column j.
doublereal _value(size_t i, size_t j) const
Return the value of the (i,j) element for (i,j) within the bandwidth.
size_t m_ku
Number of super diagonals of the matrix.
size_t m_kl
Number of subdiagonals of the matrix.
vector_fp ludata
Factorized data.
virtual size_t checkRows(doublereal &valueSmall) const
Check to see if we have any zero rows in the Jacobian.
std::vector< doublereal * > m_colPtrs
Vector of column pointers.
size_t nSubDiagonals() const
Number of subdiagonals.
size_t ldim() const
Return the number of rows of storage needed for the band storage.
int factor()
Perform an LU decomposition, the LAPACK routine DGBTRF is used.
size_t index(size_t i, size_t j) const
Returns the location in the internal 1D array corresponding to the (i,j) element in the banded array.
virtual void mult(const doublereal *b, doublereal *prod) const
Multiply A*b and write result to prod.
virtual size_t nRows() const
Return the number of rows in the matrix.
virtual doublereal rcond(doublereal a1norm)
Returns an estimate of the inverse of the condition number for the matrix.
vector_int iwork_
Extra work array needed - size = n.
virtual vector_fp::iterator begin()
Returns an iterator for the start of the band storage data.
std::unique_ptr< PivData > m_ipiv
Pivot vector.
void bfill(doublereal v=0.0)
Fill or zero the matrix.
BandMatrix()
Base Constructor.
size_t nColumns() const
Number of columns.
doublereal m_zero
value of zero
doublereal & operator()(size_t i, size_t j)
Index into the (i,j) element.
size_t nSuperDiagonals() const
Number of superdiagonals.
vector_fp work_
Extra dp work array needed - size = 3n.
int solve(const doublereal *const b, doublereal *const x)
Solve the matrix problem Ax = b.
virtual int factorAlgorithm() const
Returns the factor algorithm used.
vector_fp::iterator end()
Returns an iterator for the end of the band storage data.
void resize(size_t n, size_t kl, size_t ku, doublereal v=0.0)
Resize the matrix problem.
virtual size_t checkColumns(doublereal &valueSmall) const
Check to see if we have any zero columns in the Jacobian.
virtual void leftMult(const doublereal *const b, doublereal *const prod) const
Multiply b*A and write result to prod.
virtual doublereal *const * colPts()
Return a vector of const pointers to the columns.
doublereal & value(size_t i, size_t j)
Return a changeable reference to element (i,j).
size_t m_n
Number of rows and columns of the matrix.
vector_fp data
Matrix data.
virtual doublereal oneNorm() const
Returns the one norm of the matrix.
Base class for exceptions thrown by Cantera classes.
int m_factored
Indicates whether the matrix is factored.
const size_t npos
index returned by functions to indicate "no position"
std::vector< int > vector_int
Vector of ints.
Namespace for the Cantera kernel.
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...