Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
precon.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib
4 // File precon.h
5 // Declaration of tmplate class TPreconditioner
6 // Preconditioner for iterative matrix solution methods
7 // ==========================================================================
8 
9 #ifndef __PRECON_H
10 #define __PRECON_H
11 
12 #include "vector.h"
13 #include "matrix.h"
14 #include "dgmatrix.h"
15 #include "crmatrix.h"
16 #include "crmatrix_cm.h"
17 #ifdef HAVE_ILU
18 #include "ilupack.h"
19 #endif // HAVE_ILU
20 
22 
23 typedef enum {
32 } PreconType;
34 
35 // ==========================================================================
36 // class TPreconditioner
37 
38 template<class MT> class TPreconditioner {
39 public:
40  TPreconditioner() {}
41  virtual ~TPreconditioner() {}
42 
43  virtual PreconType Type() = 0;
44 
45  virtual void Reset (const TMatrix<MT> *A) = 0;
46  // Reset preconditioner for matrix A
47 
48  virtual void Apply (const TVector<MT> &r, TVector<MT> &s)=0;
49  // Apply preconditioner to r and return the result in s
50  // e.g. s = M^-1 r for a preconditioner matrix M
51  virtual void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s) const =0;
52 
53 
54  static TPreconditioner *Create (PreconType type);
55  // create a new preconditioner of type 'type' and return pointer to it
56 };
57 
58 // ==========================================================================
59 // class TPrecon_Null
60 // Dummy preconditioner (M = I)
61 
62 template<class MT> class TPrecon_Null: public TPreconditioner<MT> {
63 public:
64  TPrecon_Null() {}
65  PreconType Type() { return PRECON_NULL; }
66  void Reset (const TMatrix<MT>*) {}
67  void Apply (const TVector<MT> &r, TVector<MT> &s) { s = r; }
68  void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s) const
69  { ERROR_UNDEF; };
70 
71 };
72 
73 // ==========================================================================
74 // class TPrecon_Diag
75 // diagonal preconditioner (M = diag(A))
76 
77 template<class MT> class TPrecon_Diag: public TPreconditioner<MT> {
78 public:
79  TPrecon_Diag() {}
80  PreconType Type() { return PRECON_DIAG; }
81  void Reset (const TMatrix<MT> *A);
82  void ResetFromDiagonal (const TVector<MT> &diag);
83  void Apply (const TVector<MT> &r, TVector<MT> &s);
84  void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s) const;
85 
86 private:
87  TVector<MT> idiag;
88 };
89 
90 // ==========================================================================
91 // class TPrecon_IC
92 // Incomplete Cholesky preconditioner
93 
94 template<class MT> class TPrecon_IC: public TPreconditioner<MT> {
95 public:
96  TPrecon_IC() {}
97  PreconType Type() { return PRECON_ICH; }
98  void Reset (const TMatrix<MT> *A);
99  void Apply (const TVector<MT> &r, TVector<MT> &s);
100  void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s) const
101  { ERROR_UNDEF; };
102 
103 private:
105  TVector<MT>d;
106 };
107 
108 // ==========================================================================
109 // class TPrecon_DILU
110 // D-ILU: Simple incomplete LU preconditioner where only diagonal elements
111 // are modified
112 
113 template<class MT> class TPrecon_DILU: public TPreconditioner<MT> {
114 public:
115  TPrecon_DILU() {}
116  PreconType Type() { return PRECON_DILU; }
117  void Reset (const TMatrix<MT> *);
118  void Apply (const TVector<MT> &r, TVector<MT> &s);
119  void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s) const
120  { ERROR_UNDEF; };
121 
122 private:
123  int dim; // problem dimension
124  TVector<MT>ipivot; // inverse pivots
125  const TMatrix<MT> *A; // pointer to matrix
126 };
127 
128 // ==========================================================================
129 // class TPrecon_ILU
130 // ILU: incomplete LU preconditioner using ILUPACK
131 
132 #ifdef HAVE_ILU
133 
134 template<class MT> class TPrecon_ILU: public TPreconditioner<MT> {
135 public:
136  TPrecon_ILU() {}
137  PreconType Type() { return PRECON_ILU; }
138  void Reset (const TMatrix<MT> *){ ERROR_UNDEF; }
139  void Reset (TCompRowMatrix<MT> &, int matching, char *ordering, double droptol, int condest, int elbow);
140  void Apply (const TVector<MT> &r, TVector<MT> &s);
141  void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s)const;
142  ~TPrecon_ILU();
143 private:
144  ilu_doublecomplex *rhs, *sol;
145  Zmat A;
146  ZAMGlevelmat PRE;
147  ZILUPACKparam param;
148 };
149 
150 #endif // HAVE_ILU
151 
152 // ==========================================================================
153 // class TPrecon_CG_Multigrid
154 // Uses multigrid CG solution as preconditioner
155 // Suitable for SPD systems
156 
157 template<class MT> class TPrecon_CG_Multigrid: public TPreconditioner<MT> {
158 public:
161  TPreconditioner<MT> **pprecon, int nlvl, int nngs = 2, int nnmg = 2) :
162  A(AA), P(PP), R(RR), precon(pprecon), maxlevel(nlvl), ngs(nngs),
163  nmg(nnmg) {}
164 
165  PreconType Type() { return PRECON_CG_MULTIGRID; }
166 
167  void Reset (const TMatrix<MT> *);
168  // _A points to an *array* of nlvl system matrices, from finest to
169  // coarsest grid
170 
171  void Apply (const TVector<MT> &r, TVector<MT> &s);
172  void Apply (const TDenseMatrix<MT> &r, TDenseMatrix<MT> &s) const
173  { ERROR_UNDEF; };
174 
175 
176 private:
177  const TCompRowMatrix<MT> *A; // pointer to system matrix array
178  TCompRowMatrix<MT> **P; // interpolator matrix array
179  TCompRowMatrix<MT> **R; // restrictor matrix array
180  TPreconditioner<MT> **precon; // coarse-level CG preconditioner
181  int maxlevel, ngs, nmg;
182  double MGM (const TVector<MT> &r, TVector<MT> &x, int level) const;
183  void Smoother (const TCompRowMatrix<MT> &A, const TVector<MT> &r,
184  TVector<MT> &x, int itermax) const;
185 };
186 
187 // ==========================================================================
188 // template typedefs
189 
195 
201 
207 
213 
219 
220 #ifdef HAVE_ILU
221 typedef TPrecon_ILU<double> RPrecon_ILU;
222 typedef TPrecon_ILU<std::complex<double> > CPrecon_ILU;
223 typedef TPrecon_ILU<std::complex<float> > SCPrecon_ILU;
224 #endif // HAVE_ILU
225 
231 
232 
233 // ==========================================================================
234 // Specialised preconditioner: single-precision complex compressed-row
235 // matrix (requires separate implementation to work with double-precision
236 // vectors)
237 
239 public:
241  virtual ~SCPreconditionerMixed() {}
242 
243  virtual PreconType Type() = 0;
244 
245  virtual void Reset (const SCCompRowMatrixMixed *A) = 0;
246  // Reset preconditioner for matrix A
247 
248  virtual void Apply (const CVector &r, CVector &s)=0;
249  // Apply preconditioner to r and return the result in s
250  // e.g. s = M^-1 r for a preconditioner matrix M
251 
252  inline static SCPreconditionerMixed *Create (PreconType type);
253  // create a new preconditioner of type 'type' and return pointer to it
254 };
255 
257 public:
258  SCPreconMixed_Null() {}
259  PreconType Type() { return PRECON_NULL; }
260  void Reset (const SCCompRowMatrixMixed*) {}
261  void Apply (const CVector &r, CVector &s) { s = r; }
262 };
263 
265 public:
266  SCPreconMixed_Diag() {}
267  PreconType Type() { return PRECON_DIAG; }
268  inline void Reset (const SCCompRowMatrixMixed *A);
269  inline void Apply (const CVector &r, CVector &s);
270 
271 private:
272  CVector idiag;
273 };
274 
276 public:
277  SCPreconMixed_DILU() {}
278  PreconType Type() { return PRECON_DILU; }
279  inline void Reset (const SCCompRowMatrixMixed *);
280  inline void Apply (const CVector &r, CVector &s);
281 
282 private:
283  int dim; // problem dimension
284  CVector ipivot; // inverse pivots
285  const SCCompRowMatrixMixed *A; // pointer to matrix
286 };
287 
288 #endif // !__PRECON_H
PreconType
Definition: precon.h:23
Definition: crmatrix_cm.h:37
Definition: gsmatrix.h:48
Definition: precon.h:113
diagonal incomplete LU
Definition: precon.h:27
Definition: precon.h:77
smoothed aggregation (CUDA/CUSP only)
Definition: precon.h:31
Definition: precon.h:264
multigrid
Definition: precon.h:28
Definition: precon.h:157
no preconditioner
Definition: precon.h:24
incomplete Choleski
Definition: precon.h:26
Compressed-row sparse matrix class.
Definition: cdmatrix.h:27
incomplete LU
Definition: precon.h:29
Virtual base class for all matrix types (dense and sparse)
Definition: matrix.h:43
diagonal (Jacobi) preconditioner
Definition: precon.h:25
approximate inverse (CUDA/CUSP only)
Definition: precon.h:30
Dense matrix class.
Definition: crmatrix.h:38
Definition: crmatrix.h:37
Definition: precon.h:238
Definition: precon.h:275
Definition: precon.h:256
Definition: precon.h:62