Cantera  4.0.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(span<const double> b, span<double> prod) const override;
136 void leftMult(span<const double> b, span<double> 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(span<const double> b, span<double> 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(span<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 //! Check to see if we have any zero rows in the Jacobian
177 /*!
178 * This utility routine checks to see if any rows are zero. The smallest row
179 * is returned along with the largest coefficient in that row
180 *
181 * @param valueSmall OUTPUT value of the largest coefficient in the smallest row
182 * @return index of the row that is most nearly zero
183 */
184 size_t checkRows(double& valueSmall) const override;
185
186 //! Check to see if we have any zero columns in the Jacobian
187 /*!
188 * This utility routine checks to see if any columns are zero. The smallest
189 * column is returned along with the largest coefficient in that column
190 *
191 * @param valueSmall OUTPUT value of the largest coefficient in the smallest column
192 * @return index of the column that is most nearly zero
193 */
194 size_t checkColumns(double& valueSmall) const override;
195
196 //! LAPACK "info" flag after last factor/solve operation
197 int info() const { return m_info; };
198
199protected:
200 //! Matrix data
201 vector<double> data;
202
203 //! Factorized data
204 vector<double> ludata;
205 //! Pointers into #ludata used by the SUNDIALS solver
206 vector<double*> m_lu_col_ptrs;
207
208 //! Number of rows and columns of the matrix
209 size_t m_n = 0;
210
211 //! Number of subdiagonals of the matrix
212 size_t m_kl = 0;
213
214 //! Number of super diagonals of the matrix
215 size_t m_ku = 0;
216
217 //! value of zero
218 double m_zero = 0;
219
220 struct PivData; // pImpl wrapper class
221
222 //! Pivot vector
223 unique_ptr<PivData> m_ipiv;
224
225 //! Extra work array needed - size = n
226 vector<int> iwork_;
227
228 //! Extra dp work array needed - size = 3n
229 vector<double> work_;
230
231
232 int m_info = 0;
233};
234
235//! Utility routine to print out the matrix
236/*!
237 * @param s ostream to print the matrix out to
238 * @param m Matrix to be printed
239 * @returns a reference to the ostream
240 */
241std::ostream& operator<<(std::ostream& s, const BandMatrix& m);
242
243}
244
245#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
double & operator()(size_t i, size_t j) override
Index into the (i,j) element.
vector< double > ludata
Factorized data.
Definition BandMatrix.h:204
size_t m_ku
Number of super diagonals of the matrix.
Definition BandMatrix.h:215
size_t m_kl
Number of subdiagonals of the matrix.
Definition BandMatrix.h:212
void mult(span< const double > b, span< double > prod) const override
Multiply A*b and write result to prod.
double m_zero
value of zero
Definition BandMatrix.h:218
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.
void leftMult(span< const double > b, span< double > prod) const override
Multiply b*A and write result to prod.
void solve(span< const double > b, span< double > x)
Solve the matrix problem Ax = b.
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 > work_
Extra dp work array needed - size = 3n.
Definition BandMatrix.h:229
unique_ptr< PivData > m_ipiv
Pivot vector.
Definition BandMatrix.h:223
size_t nColumns() const
Number of columns.
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.
vector< double * > m_lu_col_ptrs
Pointers into ludata used by the SUNDIALS solver.
Definition BandMatrix.h:206
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:226
vector< double > data
Matrix data.
Definition BandMatrix.h:201
int info() const
LAPACK "info" flag after last factor/solve operation.
Definition BandMatrix.h:197
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:209
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:101