Cantera  2.1.2
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(const std::string& routine, const 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 public:
73  //! Default Constructor
74  DenseMatrix();
75 
76  //! Constructor.
77  /*!
78  * Create an \c n by \c m matrix, and initialize all elements to \c v.
79  *
80  * @param n New number of rows
81  * @param m New number of columns
82  * @param v Default fill value. defaults to zero.
83  */
84  DenseMatrix(size_t n, size_t m, doublereal v = 0.0);
85 
86  //! Copy constructor
87  /*!
88  * @param y Object to be copied
89  */
90  DenseMatrix(const DenseMatrix& y);
91 
92  //! Assignment operator
93  /*!
94  * @param y Object to be copied
95  */
97 
98  //! Resize the matrix
99  /*!
100  * Resize the matrix to n rows by m cols.
101  *
102  * @param n New number of rows
103  * @param m New number of columns
104  * @param v Default fill value. defaults to zero.
105  */
106  void resize(size_t n, size_t m, doublereal v = 0.0);
107 
108  virtual doublereal* const* colPts();
109 
110  //! Return a const vector of const pointers to the columns
111  /*!
112  * Note, the jacobian can not be altered by this routine, and
113  * therefore the member function is const.
114  *
115  * @return returns a vector of pointers to the top of the columns
116  * of the matrices.
117  */
118  const doublereal* const* const_colPts() const;
119 
120  virtual void mult(const double* b, double* prod) const;
121 
122  //! Multiply A*B and write result to \c prod.
123  /*!
124  * @param b input DenseMatrix B of size NxN
125  * @param prod output output DenseMatrix prod size NxN
126  */
127  virtual void mult(const DenseMatrix& b, DenseMatrix& prod) const;
128 
129  //! Left-multiply the matrix by transpose(b), and write the result to prod.
130  /*!
131  * @param b left multiply by this vector. The length must be equal to n
132  * the number of rows in the matrix.
133  * @param prod Resulting vector. This is of length m, the number of columns
134  * in the matrix
135  */
136  virtual void leftMult(const double* const b, double* const prod) const;
137 
138  //! Return a changeable value of the pivot vector
139  /*!
140  * @return Returns a reference to the pivot vector as a vector_int
141  */
142  vector_int& ipiv();
143 
144  //! Return a changeable value of the pivot vector
145  /*!
146  * @return Returns a reference to the pivot vector as a vector_int
147  */
148  const vector_int& ipiv() const {
149  return m_ipiv;
150  }
151 
152 protected:
153  //! Vector of pivots. Length is equal to the max of m and n.
155 
156  //! Vector of column pointers
157  std::vector<doublereal*> m_colPts;
158 
159 public:
160  //! Error Handling Flag
161  /*!
162  * The default is to set this to 0. In this case, if a factorization is requested and can't be achieved,
163  * a CESingularMatrix exception is triggered. No return code is used, because an exception is thrown.
164  * If this is set to 1, then an exception is not thrown. Routines return with an error code, that is up
165  * to the calling routine to handle correctly. Negative return codes always throw an exception.
166  */
168 
169  //! Print Level
170  /*!
171  * Printing is done to the log file using the routine writelogf().
172  *
173  * Level of printing that is carried out. Only error conditions are printed out, if this value is nonzero.
174  */
176 
177  // Listing of friend functions which are defined below
178 
179  friend int solve(DenseMatrix& A, double* b);
180  friend int solve(DenseMatrix& A, DenseMatrix& b);
181  friend int invert(DenseMatrix& A, int nn);
182 };
183 
184 
185 //! Solve Ax = b. Array b is overwritten on exit with x.
186 /*!
187  * The solve class uses the LAPACK routine dgetrf to invert the m xy n matrix.
188  *
189  * The factorization has the form
190  * A = P * L * U
191  * where P is a permutation matrix, L is lower triangular with unit
192  * diagonal elements (lower trapezoidal if m > n), and U is upper
193  * triangular (upper trapezoidal if m < n).
194  *
195  * The system is then solved using the LAPACK routine dgetrs
196  *
197  * @param A Dense matrix to be factored
198  * @param b rhs to be solved.
199  */
200 int solve(DenseMatrix& A, double* b);
201 
202 //! Solve Ax = b for multiple right-hand-side vectors.
203 /*!
204  * @param A Dense matrix to be factored
205  * @param b Dense matrix of rhs's. Each column is a rhs
206  */
207 int solve(DenseMatrix& A, DenseMatrix& b);
208 
209 //! Multiply \c A*b and return the result in \c prod. Uses BLAS routine DGEMV.
210 /*!
211  * \f[
212  * prod_i = sum^N_{j = 1}{A_{ij} b_j}
213  * \f]
214  *
215  * @param A input Dense Matrix A with M rows and N columns
216  * @param b input vector b with length N
217  * @param prod output output vector prod length = M
218  */
219 void multiply(const DenseMatrix& A, const double* const b, double* const prod);
220 
221 //! Multiply \c A*b and add it to the result in \c prod. Uses BLAS routine DGEMV.
222 /*!
223  * \f[
224  * prod_i += sum^N_{j = 1}{A_{ij} b_j}
225  * \f]
226  *
227  * @param A input Dense Matrix A with M rows and N columns
228  * @param b input vector b with length N
229  * @param prod output output vector prod length = M
230  */
231 void increment(const DenseMatrix& A, const double* const b, double* const prod);
232 
233 //! invert A. A is overwritten with A^-1.
234 /*!
235  * @param A Invert the matrix A and store it back in place
236  *
237  * @param nn Size of A. This defaults to -1, which means that the number
238  * of rows is used as the default size of n
239  */
240 int invert(DenseMatrix& A, size_t nn=npos);
241 
242 }
243 
244 #endif
const doublereal *const * const_colPts() const
Return a const vector of const pointers to the columns.
Definition: DenseMatrix.cpp:88
DenseMatrix & operator=(const DenseMatrix &y)
Assignment operator.
Definition: DenseMatrix.cpp:55
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:148
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
vector_int m_ipiv
Vector of pivots. Length is equal to the max of m and n.
Definition: DenseMatrix.h:154
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
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:43
int m_printLevel
Print Level.
Definition: DenseMatrix.h:175
int solve(DenseMatrix &A, double *b)
Solve Ax = b. Array b is overwritten on exit with x.
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:33
Header file for class Cantera::Array2D.
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:167
void resize(size_t n, size_t m, doublereal v=0.0)
Resize the matrix.
Definition: DenseMatrix.cpp:71
std::vector< doublereal * > m_colPts
Vector of column pointers.
Definition: DenseMatrix.h:157
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:68
virtual void leftMult(const double *const b, double *const prod) const
Left-multiply the matrix by transpose(b), and write the result to prod.
Exception thrown when an LAPACK error is encountered associated with inverting or solving a matrix...
Definition: DenseMatrix.h:34
DenseMatrix()
Default Constructor.
Definition: DenseMatrix.cpp:17
int m_useReturnErrorCode
Error Handling Flag.
Definition: DenseMatrix.h:167
friend int solve(DenseMatrix &A, double *b)
Solve Ax = b. Array b is overwritten on exit with x.
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:70