Cantera  3.3.0a1
Loading...
Searching...
No Matches
BandMatrix.h
Go to the documentation of this file.
1/**
2 * @file BandMatrix.h
3 * Declarations for the class BandMatrix
4 * which is a child class of GeneralMatrix for banded matrices handled by solvers
5 * (see class @ref numerics and @link Cantera::BandMatrix BandMatrix@endlink).
6 */
7
8// This file is part of Cantera. See License.txt in the top-level directory or
9// at https://cantera.org/license.txt for license and copyright information.
10
11#ifndef CT_BANDMATRIX_H
12#define CT_BANDMATRIX_H
13
14#include "GeneralMatrix.h"
15
16namespace Cantera
17{
18
19//! A class for banded matrices, involving matrix inversion processes.
20//! The class is based upon the LAPACK banded storage matrix format.
21/*!
22 * An important issue with this class is that it stores both the original data
23 * and the LU factorization of the data. This means that the banded matrix
24 * typically will take up twice the room that it is expected to take.
25 *
26 * QR factorizations of banded matrices are not included in the original LAPACK
27 * work. Add-ons are available. However, they are not included here. Instead we
28 * just use the stock LU decompositions.
29 *
30 * This class is a derived class of the base class GeneralMatrix. However,
31 * within the oneD directory, the class is used as is, without reference to the
32 * GeneralMatrix base type.
33 *
34 * @ingroup matrices
35 */
37{
38public:
39 //! Base Constructor
40 /*!
41 * Create an @c 0 by @c 0 matrix, and initialize all elements to @c 0.
42 */
43 BandMatrix();
45
46 //! Creates a banded matrix and sets all elements to zero
47 /*!
48 * Create an @c n by @c n banded matrix, and initialize all elements to @c v.
49 *
50 * @param n size of the square matrix
51 * @param kl band size on the lower portion of the matrix
52 * @param ku band size on the upper portion of the matrix
53 * @param v initial value of all matrix components.
54 */
55 BandMatrix(size_t n, size_t kl, size_t ku, double v = 0.0);
56
57 BandMatrix(const BandMatrix& y);
58 BandMatrix& operator=(const BandMatrix& y);
59
60 //! Resize the matrix problem
61 /*!
62 * All data is lost
63 *
64 * @param n size of the square matrix
65 * @param kl band size on the lower portion of the matrix
66 * @param ku band size on the upper portion of the matrix
67 * @param v initial value of all matrix components.
68 */
69 void resize(size_t n, size_t kl, size_t ku, double v = 0.0);
70
71 //! Fill or zero the matrix
72 /*!
73 * @param v Fill value, defaults to zero.
74 */
75 void bfill(double v = 0.0);
76
77 double& operator()(size_t i, size_t j) override;
78 double operator()(size_t i, size_t j) const override;
79
80 //! Return a changeable reference to element (i,j).
81 /*!
82 * Since this method may alter the element value, it may need to be
83 * refactored, so the flag m_factored is set to false.
84 *
85 * @param i row
86 * @param j column
87 * @returns a reference to the value of the matrix entry
88 */
89 double& value(size_t i, size_t j);
90
91 //! Return the value of element (i,j).
92 /*!
93 * This method does not alter the array.
94 * @param i row
95 * @param j column
96 * @returns the value of the matrix entry
97 */
98 double value(size_t i, size_t j) const;
99
100 //! Returns the location in the internal 1D array corresponding to the (i,j)
101 //! element in the banded array
102 /*!
103 * @param i row
104 * @param j column
105 * @returns the index of the matrix entry
106 */
107 size_t index(size_t i, size_t j) const;
108
109 //! Return the value of the (i,j) element for (i,j) within the bandwidth.
110 /*!
111 * For efficiency, this method does not check that (i,j) are within the
112 * bandwidth; it is up to the calling program to insure that this is true.
113 *
114 * @param i row
115 * @param j column
116 * @returns the value of the matrix entry
117 */
118 double _value(size_t i, size_t j) const;
119
120 size_t nRows() const override;
121
122 //! Number of columns
123 size_t nColumns() const;
124
125 //! Number of subdiagonals
126 size_t nSubDiagonals() const;
127
128 //! Number of superdiagonals
129 size_t nSuperDiagonals() const;
130
131 //! Return the number of rows of storage needed for the band storage
132 size_t ldim() const;
133
134 //! Multiply A*b and write result to @c prod.
135 void mult(const double* b, double* prod) const override;
136 void leftMult(const double* const b, double* const prod) const override;
137
138 //! Perform an LU decomposition, the LAPACK routine DGBTRF is used.
139 //! The factorization is saved in ludata.
140 void factor() override;
141
142 //! Solve the matrix problem Ax = b
143 /*!
144 * @param b INPUT RHS of the problem
145 * @param x OUTPUT solution to the problem
146 */
147 void solve(const double* const b, double* const x);
148
149 //! Solve the matrix problem Ax = b
150 /*!
151 * @param b INPUT RHS of the problem
152 * OUTPUT solution to the problem
153 * @param nrhs Number of right hand sides to solve
154 * @param ldb Leading dimension of `b`. Default is nColumns()
155 */
156 void solve(double* b, size_t nrhs=1, size_t ldb=0) override;
157
158 void zero() override;
159
160 //! Returns an estimate of the inverse of the condition number for the matrix
161 /*!
162 * The matrix must have been previously factored using the LU algorithm
163 *
164 * @param a1norm Norm of the matrix
165 * @returns the inverse of the condition number
166 */
167 double rcond(double a1norm) override;
168
169 //! Returns the factor algorithm used. This method will always return 0
170 //! (LU) for band matrices.
171 int factorAlgorithm() const override;
172
173 //! Returns the one norm of the matrix
174 double oneNorm() const override;
175
176 //! Return a pointer to the top of column j
177 /*!
178 * Column values are assumed to be contiguous in memory (LAPACK band matrix
179 * structure)
180 *
181 * On entry, the matrix A in band storage, in rows KL+1 to 2*KL+KU+1; rows 1
182 * to KL of the array need not be set. The j-th column of A is stored in the
183 * j-th column of the array AB as follows:
184 *
185 * AB(KL + KU + 1 + i - j,j) = A(i,j) for max(1, j - KU) <= i <= min(m, j + KL)
186 *
187 * This routine returns the position of AB(1,j) (fortran-1 indexing) in the
188 * above format
189 *
190 * So to address the (i,j) position, you use the following indexing:
191 *
192 * double *colP_j = matrix.ptrColumn(j);
193 * double a_i_j = colP_j[kl + ku + i - j];
194 *
195 * @param j Value of the column
196 * @returns a pointer to the top of the column
197 */
198 double* ptrColumn(size_t j) override;
199
200 //! Return a vector of const pointers to the columns
201 /*!
202 * Note the value of the pointers are protected by their being const.
203 * However, the value of the matrix is open to being changed.
204 *
205 * @returns a vector of pointers to the top of the columns of the matrices.
206 */
207 double* const* colPts() override;
208
209 //! Check to see if we have any zero rows in the Jacobian
210 /*!
211 * This utility routine checks to see if any rows are zero. The smallest row
212 * is returned along with the largest coefficient in that row
213 *
214 * @param valueSmall OUTPUT value of the largest coefficient in the smallest row
215 * @return index of the row that is most nearly zero
216 */
217 size_t checkRows(double& valueSmall) const override;
218
219 //! Check to see if we have any zero columns in the Jacobian
220 /*!
221 * This utility routine checks to see if any columns are zero. The smallest
222 * column is returned along with the largest coefficient in that column
223 *
224 * @param valueSmall OUTPUT value of the largest coefficient in the smallest column
225 * @return index of the column that is most nearly zero
226 */
227 size_t checkColumns(double& valueSmall) const override;
228
229 //! LAPACK "info" flag after last factor/solve operation
230 int info() const { return m_info; };
231
232protected:
233 //! Matrix data
234 vector<double> data;
235
236 //! Factorized data
237 vector<double> ludata;
238
239 //! Number of rows and columns of the matrix
240 size_t m_n = 0;
241
242 //! Number of subdiagonals of the matrix
243 size_t m_kl = 0;
244
245 //! Number of super diagonals of the matrix
246 size_t m_ku = 0;
247
248 //! value of zero
249 double m_zero = 0;
250
251 struct PivData; // pImpl wrapper class
252
253 //! Pivot vector
254 unique_ptr<PivData> m_ipiv;
255
256 //! Vector of column pointers
257 vector<double*> m_colPtrs;
258 vector<double*> m_lu_col_ptrs;
259
260 //! Extra work array needed - size = n
261 vector<int> iwork_;
262
263 //! Extra dp work array needed - size = 3n
264 vector<double> work_;
265
266 int m_info = 0;
267};
268
269//! Utility routine to print out the matrix
270/*!
271 * @param s ostream to print the matrix out to
272 * @param m Matrix to be printed
273 * @returns a reference to the ostream
274 */
275std::ostream& operator<<(std::ostream& s, const BandMatrix& m);
276
277}
278
279#endif
Declarations for the class GeneralMatrix which is a virtual base class for matrices handled by solver...
A class for banded matrices, involving matrix inversion processes.
Definition BandMatrix.h:37
void solve(const double *const b, double *const x)
Solve the matrix problem Ax = b.
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.
Definition BandMatrix.h:237
size_t m_ku
Number of super diagonals of the matrix.
Definition BandMatrix.h:246
size_t m_kl
Number of subdiagonals of the matrix.
Definition BandMatrix.h:243
double m_zero
value of zero
Definition BandMatrix.h:249
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.
Definition BandMatrix.h:257
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.
Definition BandMatrix.h:264
unique_ptr< PivData > m_ipiv
Pivot vector.
Definition BandMatrix.h:254
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.
Definition BandMatrix.h:261
vector< double > data
Matrix data.
Definition BandMatrix.h:234
int info() const
LAPACK "info" flag after last factor/solve operation.
Definition BandMatrix.h:230
void factor() override
Perform an LU decomposition, the LAPACK routine DGBTRF is used.
void zero() override
Zero the matrix elements.
size_t m_n
Number of rows and columns of the matrix.
Definition BandMatrix.h:240
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.
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition Array.cpp:100