13 #include "sunlinsol/sunlinsol_band.h"
30 vector<sunindextype> data;
39BandMatrix::~BandMatrix()
50 data.resize(n*(2*kl + ku + 1));
51 ludata.resize(n*(2*kl + ku + 1));
56 m_lu_col_ptrs.resize(n);
57 size_t ldab = (2*kl + ku + 1);
58 for (
size_t j = 0; j < n; j++) {
60 m_lu_col_ptrs[j] = &
ludata[ldab * j];
71 m_ipiv{new PivData()},
76 m_lu_col_ptrs.resize(
m_n);
78 for (
size_t j = 0; j <
m_n; j++) {
80 m_lu_col_ptrs[j] = &
ludata[ldab * j];
84BandMatrix& BandMatrix::operator=(
const BandMatrix& y)
89 GeneralMatrix::operator=(y);
93 m_ipiv->data = y.m_ipiv->data;
97 m_lu_col_ptrs.resize(
m_n);
99 for (
size_t j = 0; j <
m_n; j++) {
101 m_lu_col_ptrs[j] = &
ludata[ldab * j];
112 data.resize(n*(2*kl + ku + 1));
113 ludata.resize(n*(2*kl + ku + 1));
117 m_lu_col_ptrs.resize(
m_n);
118 size_t ldab = (2 *
m_kl +
m_ku + 1);
119 for (
size_t j = 0; j < n; j++) {
121 m_lu_col_ptrs[j] = &
ludata[ldab * j];
128 std::fill(
data.begin(),
data.end(), v);
134 std::fill(
data.begin(),
data.end(), 0.0);
151 if (i + m_ku < j || i > j +
m_kl) {
159 if (i + m_ku < j || i > j +
m_kl) {
202 for (
size_t m = 0; m <
m_n; m++) {
204 size_t start = (m >=
m_kl) ? m -
m_kl : 0;
205 size_t stop = std::min(m +
m_ku + 1,
m_n);
206 for (
size_t j = start; j < stop; j++) {
207 sum +=
_value(m,j) * b[j];
215 for (
size_t n = 0; n <
m_n; n++) {
217 size_t start = (n >=
m_ku) ? n -
m_ku : 0;
218 size_t stop = std::min(n +
m_kl + 1,
m_n);
219 for (
size_t i = start; i < stop; i++) {
220 sum +=
_value(i,n) * b[i];
235 long int smu = nu + nl;
236 #if SUNDIALS_VERSION_MAJOR >= 6
237 m_info = SUNDlsMat_bandGBTRF(m_lu_col_ptrs.data(),
239 nu, nl, smu,
m_ipiv->data.data());
241 m_info = bandGBTRF(m_lu_col_ptrs.data(),
static_cast<long int>(
nColumns()),
242 nu, nl, smu,
m_ipiv->data.data());
247 "Factorization failed with DGBTRF error code {}.", m_info);
270 m_ipiv->data.data(), b, ldb, m_info);
274 long int smu = nu + nl;
275 double** a = m_lu_col_ptrs.data();
276 #if SUNDIALS_VERSION_MAJOR >= 6
277 SUNDlsMat_bandGBTRS(a,
static_cast<long int>(
nColumns()), smu, nl,
280 bandGBTRS(a,
static_cast<long int>(
nColumns()), smu, nl,
288 "Linear solve failed with DGBTRS error code {}.", m_info);
295 for (
size_t i = 0; i < m.
nRows(); i++) {
297 for (
size_t j = 1; j < m.nColumns(); j++) {
311 throw CanteraError(
"BandMatrix::rcond",
"matrix isn't factored correctly");
320 throw CanteraError(
"BandMatrix::rcond",
"DGBCON returned INFO = {}", rinfo);
324 throw CanteraError(
"BandMatrix::rcond",
"not implemented when LAPACK is missing");
336 for (
size_t j = 0; j <
m_n; j++) {
338 size_t start = (j >=
m_ku) ? j -
m_ku : 0;
339 size_t stop = std::min(j +
m_kl + 1,
m_n);
340 for (
size_t i = start; i < stop; i++) {
341 sum += std::abs(
_value(i,j));
350 valueSmall = 1.0E300;
351 size_t iSmall =
npos;
352 for (
size_t i = 0; i <
m_n; i++) {
354 size_t start = (i >
m_kl) ? i -
m_kl : 0;
355 size_t stop = std::min(i +
m_ku + 1,
m_n);
356 for (
size_t j = start; j < stop; j++) {
357 valueS = std::max(fabs(
_value(i,j)), valueS);
359 if (valueS < valueSmall) {
362 if (valueSmall == 0.0) {
372 valueSmall = 1.0E300;
373 size_t jSmall =
npos;
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 valueS = std::max(fabs(
_value(i,j)), valueS);
381 if (valueS < valueSmall) {
384 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.
void leftMult(const double *const b, double *const prod) const override
Multiply b*A and write result to prod.
double *const * colPts() override
Return a vector of const pointers to the columns.
double & operator()(size_t i, size_t j) override
Index into the (i,j) element.
vector< double > ludata
Factorized data.
size_t m_ku
Number of super diagonals of the matrix.
size_t m_kl
Number of subdiagonals of the matrix.
double m_zero
value of zero
double rcond(double a1norm) override
Returns an estimate of the inverse of the condition number for the matrix.
size_t nSubDiagonals() const
Number of subdiagonals.
void resize(size_t n, size_t kl, size_t ku, double v=0.0)
Resize the matrix problem.
size_t ldim() const
Return the number of rows of storage needed for the band storage.
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.
void bfill(double v=0.0)
Fill or zero the matrix.
BandMatrix()
Base Constructor.
size_t checkColumns(double &valueSmall) const override
Check to see if we have any zero columns in the Jacobian.
vector< double * > m_colPtrs
Vector of column pointers.
double * ptrColumn(size_t j) override
Return a pointer to the top of column j.
vector< double > work_
Extra dp work array needed - size = 3n.
int factor() override
Perform an LU decomposition, the LAPACK routine DGBTRF is used.
unique_ptr< PivData > m_ipiv
Pivot vector.
size_t nColumns() const
Number of columns.
void mult(const double *b, double *prod) const override
Multiply A*b and write result to prod.
size_t checkRows(double &valueSmall) const override
Check to see if we have any zero rows in the Jacobian.
size_t nSuperDiagonals() const
Number of superdiagonals.
size_t nRows() const override
Return the number of rows in the matrix.
vector< int > iwork_
Extra work array needed - size = n.
vector< double > data
Matrix data.
int solve(const double *const b, double *const x)
Solve the matrix problem Ax = b.
void zero() override
Zero the matrix elements.
size_t m_n
Number of rows and columns of the matrix.
double oneNorm() const override
Returns the one norm of the matrix.
double _value(size_t i, size_t j) const
Return the value of the (i,j) element for (i,j) within the bandwidth.
double & value(size_t i, size_t j)
Return a changeable reference to element (i,j).
int factorAlgorithm() const override
Returns the factor algorithm used.
Base class for exceptions thrown by Cantera classes.
int m_factored
Indicates whether the matrix is factored.
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...