Cantera  3.2.0
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 /*!
140 * The factorization is saved in ludata.
141 *
142 * @returns a success flag. 0 indicates a success; ~0 indicates some
143 * error occurred, see the LAPACK documentation
144 * @deprecated After %Cantera 3.2, the return type will be `void`
145 */
146 int factor() override;
147
148 //! Solve the matrix problem Ax = b
149 /*!
150 * @param b INPUT RHS of the problem
151 * @param x OUTPUT solution to the problem
152 * @return a success flag. 0 indicates a success; ~0 indicates some error
153 * occurred, see the LAPACK documentation
154 * @deprecated After %Cantera 3.2, the return type will be `void`
155 */
156 int solve(const double* const b, double* const x);
157
158 //! Solve the matrix problem Ax = b
159 /*!
160 * @param b INPUT RHS of the problem
161 * OUTPUT solution to the problem
162 * @param nrhs Number of right hand sides to solve
163 * @param ldb Leading dimension of `b`. Default is nColumns()
164 * @returns a success flag. 0 indicates a success; ~0 indicates some error
165 * occurred, see the LAPACK documentation
166 * @deprecated After %Cantera 3.2, the return type will be `void`
167 */
168 int solve(double* b, size_t nrhs=1, size_t ldb=0) override;
169
170 void zero() override;
171
172 //! Returns an estimate of the inverse of the condition number for the matrix
173 /*!
174 * The matrix must have been previously factored using the LU algorithm
175 *
176 * @param a1norm Norm of the matrix
177 * @returns the inverse of the condition number
178 */
179 double rcond(double a1norm) override;
180
181 //! Returns the factor algorithm used. This method will always return 0
182 //! (LU) for band matrices.
183 int factorAlgorithm() const override;
184
185 //! Returns the one norm of the matrix
186 double oneNorm() const override;
187
188 //! Return a pointer to the top of column j
189 /*!
190 * Column values are assumed to be contiguous in memory (LAPACK band matrix
191 * structure)
192 *
193 * On entry, the matrix A in band storage, in rows KL+1 to 2*KL+KU+1; rows 1
194 * to KL of the array need not be set. The j-th column of A is stored in the
195 * j-th column of the array AB as follows:
196 *
197 * AB(KL + KU + 1 + i - j,j) = A(i,j) for max(1, j - KU) <= i <= min(m, j + KL)
198 *
199 * This routine returns the position of AB(1,j) (fortran-1 indexing) in the
200 * above format
201 *
202 * So to address the (i,j) position, you use the following indexing:
203 *
204 * double *colP_j = matrix.ptrColumn(j);
205 * double a_i_j = colP_j[kl + ku + i - j];
206 *
207 * @param j Value of the column
208 * @returns a pointer to the top of the column
209 */
210 double* ptrColumn(size_t j) override;
211
212 //! Return a vector of const pointers to the columns
213 /*!
214 * Note the value of the pointers are protected by their being const.
215 * However, the value of the matrix is open to being changed.
216 *
217 * @returns a vector of pointers to the top of the columns of the matrices.
218 */
219 double* const* colPts() override;
220
221 //! Check to see if we have any zero rows in the Jacobian
222 /*!
223 * This utility routine checks to see if any rows are zero. The smallest row
224 * is returned along with the largest coefficient in that row
225 *
226 * @param valueSmall OUTPUT value of the largest coefficient in the smallest row
227 * @return index of the row that is most nearly zero
228 */
229 size_t checkRows(double& valueSmall) const override;
230
231 //! Check to see if we have any zero columns in the Jacobian
232 /*!
233 * This utility routine checks to see if any columns are zero. The smallest
234 * column is returned along with the largest coefficient in that column
235 *
236 * @param valueSmall OUTPUT value of the largest coefficient in the smallest column
237 * @return index of the column that is most nearly zero
238 */
239 size_t checkColumns(double& valueSmall) const override;
240
241 //! LAPACK "info" flag after last factor/solve operation
242 int info() const { return m_info; };
243
244protected:
245 //! Matrix data
246 vector<double> data;
247
248 //! Factorized data
249 vector<double> ludata;
250
251 //! Number of rows and columns of the matrix
252 size_t m_n = 0;
253
254 //! Number of subdiagonals of the matrix
255 size_t m_kl = 0;
256
257 //! Number of super diagonals of the matrix
258 size_t m_ku = 0;
259
260 //! value of zero
261 double m_zero = 0;
262
263 struct PivData; // pImpl wrapper class
264
265 //! Pivot vector
266 unique_ptr<PivData> m_ipiv;
267
268 //! Vector of column pointers
269 vector<double*> m_colPtrs;
270 vector<double*> m_lu_col_ptrs;
271
272 //! Extra work array needed - size = n
273 vector<int> iwork_;
274
275 //! Extra dp work array needed - size = 3n
276 vector<double> work_;
277
278 int m_info = 0;
279};
280
281//! Utility routine to print out the matrix
282/*!
283 * @param s ostream to print the matrix out to
284 * @param m Matrix to be printed
285 * @returns a reference to the ostream
286 */
287std::ostream& operator<<(std::ostream& s, const BandMatrix& m);
288
289}
290
291#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 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:249
size_t m_ku
Number of super diagonals of the matrix.
Definition BandMatrix.h:258
size_t m_kl
Number of subdiagonals of the matrix.
Definition BandMatrix.h:255
double m_zero
value of zero
Definition BandMatrix.h:261
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:269
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:276
int factor() override
Perform an LU decomposition, the LAPACK routine DGBTRF is used.
unique_ptr< PivData > m_ipiv
Pivot vector.
Definition BandMatrix.h:266
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:273
vector< double > data
Matrix data.
Definition BandMatrix.h:246
int info() const
LAPACK "info" flag after last factor/solve operation.
Definition BandMatrix.h:242
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.
Definition BandMatrix.h:252
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