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