Cantera  2.0
DenseMatrix.h
Go to the documentation of this file.
1 /**
2  * @file DenseMatrix.h
3  * Headers for the %DenseMatrix object, which deals with dense rectangular matrices and
4  * description of the numerics groupings of objects
5  * (see \ref numerics and \link Cantera::DenseMatrix DenseMatrix \endlink) .
6  */
7 
8 // Copyright 2001 California Institute of Technology
9 
10 
11 #ifndef CT_DENSEMATRIX_H
12 #define CT_DENSEMATRIX_H
13 
14 #include "cantera/base/ct_defs.h"
15 #include "cantera/base/Array.h"
16 
17 namespace Cantera
18 {
19 /**
20  * @defgroup numerics Numerical Utilities within Cantera
21  *
22  * Cantera contains some capabilities for solving nonlinear equations and
23  * integrating both ODE and DAE equation systems in time. This section describes these
24  * capabilities.
25  *
26  */
27 
28 
29 //! Exception thrown when an LAPACK error is encountered associated with inverting or solving a matrix
30 /*!
31  * A named error condition is used so that the calling code may differentiate this type of error
32  * from other error conditions.
33  */
35 {
36 public:
37 
38  //! Constructor passes through to main Cantera error handler
39  /*!
40  * @param routine Name of calling routine
41  * @param msg Informative message
42  */
43  CELapackError(std::string routine, std::string msg) :
44  CanteraError(routine + " LAPACK ERROR", msg) {
45  }
46 
47 };
48 
49 //! A class for full (non-sparse) matrices with Fortran-compatible
50 //! data storage, which adds matrix operations to class Array2D.
51 /*!
52  * The dense matrix class adds matrix operations onto the Array2D class.
53  * These matrix operations are carried out by the appropriate BLAS and LAPACK routines
54  *
55  * Error handling from BLAS and LAPACK are handled via the following formulation.
56  * Depending on a variable, a singular matrix or other terminal error condition from
57  * LAPACK is handled by either throwing an exception of type, CELapackError, or by
58  * returning the error code condition to the calling routine.
59  *
60  * The int variable, m_useReturnErrorCode, determines which method is used.
61  * The default value of zero means that an exception is thrown. A value of 1
62  * means that a return code is used.
63  *
64  * Reporting of these LAPACK error conditions is handled by the class variable
65  * m_printLevel. The default is for no reporting. If m_printLevel is nonzero,
66  * the error condition is reported to Cantera's log file.
67  *
68  * @ingroup numerics
69  */
70 class DenseMatrix : public Array2D
71 {
72 
73 public:
74 
75  //! Default Constructor
76  DenseMatrix();
77 
78  //! Constructor.
79  /*!
80  * Create an \c n by \c m matrix, and initialize all elements to \c v.
81  *
82  * @param n New number of rows
83  * @param m New number of columns
84  * @param v Default fill value. defaults to zero.
85  */
86  DenseMatrix(size_t n, size_t m, doublereal v = 0.0);
87 
88  //! Copy constructor
89  /*!
90  * @param y Object to be copied
91  */
92  DenseMatrix(const DenseMatrix& y);
93 
94  //! Assignment operator
95  /*!
96  * @param y Object to be copied
97  */
99 
100  //! Destructor. Does nothing.
101  virtual ~DenseMatrix();
102 
103  //! Resize the matrix
104  /*!
105  * Resize the matrix to n rows by m cols.
106  *
107  * @param n New number of rows
108  * @param m New number of columns
109  * @param v Default fill value. defaults to zero.
110  */
111  void resize(size_t n, size_t m, doublereal v = 0.0);
112 
113  //! Return a vector of const pointers to the columns
114  /*!
115  * Note the value of the pointers are protected by their being const.
116  * However, the value of the matrix is open to being changed.
117  *
118  * @return returns a vector of pointers to the top of the columns
119  * of the matrices.
120  */
121  virtual doublereal* const* colPts();
122 
123  //! Return a const vector of const pointers to the columns
124  /*!
125  * Note, the jacobian can not be altered by this routine, and
126  * therefore the member function is const.
127  *
128  * @return returns a vector of pointers to the top of the columns
129  * of the matrices.
130  */
131  const doublereal* const* const_colPts() const;
132 
133  //! Multiply A*b and write result to \c prod.
134  /*!
135  *
136  * @param b input vector b with length N
137  * @param prod output output vector prod length = M
138  */
139  virtual void mult(const double* b, double* prod) const;
140 
141  //! Left-multiply the matrix by transpose(b), and write the result to prod.
142  /*!
143  * @param b left multiply by this vector. The length must be equal to n
144  * the number of rows in the matrix.
145  *
146  * @param prod Resulting vector. This is of length m, the number of columns
147  * in the matrix
148  */
149  virtual void leftMult(const double* const b, double* const prod) const;
150 
151  //! Return a changeable value of the pivot vector
152  /*!
153  * @return Returns a reference to the pivot vector as a vector_int
154  */
155  vector_int& ipiv();
156 
157  //! Return a changeable value of the pivot vector
158  /*!
159  * @return Returns a reference to the pivot vector as a vector_int
160  */
161  const vector_int& ipiv() const {
162  return m_ipiv;
163  }
164 
165 protected:
166 
167  //! Vector of pivots. Length is equal to the max of m and n.
169 
170  //! Vector of column pointers
171  std::vector<doublereal*> m_colPts;
172 
173 public:
174 
175  //! Error Handling Flag
176  /*!
177  * The default is to set this to 0. In this case, if a factorization is requested and can't be achieved,
178  * a CESingularMatrix exception is triggered. No return code is used, because an exception is thrown.
179  * If this is set to 1, then an exception is not thrown. Routines return with an error code, that is up
180  * to the calling routine to handle correctly. Negative return codes always throw an exception.
181  */
183 
184  //! Print Level
185  /*!
186  * Printing is done to the log file using the routine writelogf().
187  *
188  * Level of printing that is carried out. Only error conditions are printed out, if this value is nonzero.
189  */
191 
192 
193  // Listing of friend functions which are defined below
194 
195  friend int solve(DenseMatrix& A, double* b);
196  friend int solve(DenseMatrix& A, DenseMatrix& b);
197  friend int invert(DenseMatrix& A, int nn);
198 };
199 
200 //==================================================================================================================
201 
202 
203 //! Solve Ax = b. Array b is overwritten on exit with x.
204 /*!
205  * The solve class uses the LAPACK routine dgetrf to invert the m xy n matrix.
206  *
207  * The factorization has the form
208  * A = P * L * U
209  * where P is a permutation matrix, L is lower triangular with unit
210  * diagonal elements (lower trapezoidal if m > n), and U is upper
211  * triangular (upper trapezoidal if m < n).
212  *
213  * The system is then solved using the LAPACK routine dgetrs
214  *
215  * @param A Dense matrix to be factored
216  * @param b rhs to be solved.
217  */
218 int solve(DenseMatrix& A, double* b);
219 
220 //! Solve Ax = b for multiple right-hand-side vectors.
221 /*!
222  * @param A Dense matrix to be factored
223  * @param b Dense matrix of rhs's. Each column is a rhs
224  */
225 int solve(DenseMatrix& A, DenseMatrix& b);
226 
227 //! Multiply \c A*b and return the result in \c prod. Uses BLAS routine DGEMV.
228 /*!
229  * \f[
230  * prod_i = sum^N_{j = 1}{A_{ij} b_j}
231  * \f]
232  *
233  * @param A input Dense Matrix A with M rows and N columns
234  * @param b input vector b with length N
235  * @param prod output output vector prod length = M
236  */
237 void multiply(const DenseMatrix& A, const double* const b, double* const prod);
238 
239 //! Multiply \c A*b and add it to the result in \c prod. Uses BLAS routine DGEMV.
240 /*!
241  * \f[
242  * prod_i += sum^N_{j = 1}{A_{ij} b_j}
243  * \f]
244  *
245  * @param A input Dense Matrix A with M rows and N columns
246  * @param b input vector b with length N
247  * @param prod output output vector prod length = M
248  */
249 void increment(const DenseMatrix& A, const double* const b, double* const prod);
250 
251 //! invert A. A is overwritten with A^-1.
252 /*!
253  * @param A Invert the matrix A and store it back in place
254  *
255  * @param nn Size of A. This defaults to -1, which means that the number
256  * of rows is used as the default size of n
257  */
258 int invert(DenseMatrix& A, size_t nn=npos);
259 
260 }
261 
262 #endif
263 
264 
265