TBCI Numerical high perf. C++ Library  2.8.0
ilu0precond.h
Go to the documentation of this file.
1 
22 #ifndef TBCI_SOLVER_ILU0PRECOND_H
23 #define TBCI_SOLVER_ILU0PRECOND_H
24 
25 #include "tbci/solver/precond.h"
26 
28 
29 template <typename T> class Symm_BdMatrix;
30 
34 template <typename T>
35 class ILU0_Symm_BdMatrixPreconditioner : public Preconditioner_Sig<T, Symm_BdMatrix<T> >
36 {
37  public:
40  { update (A); }
42 
43 
44  // update function
45  void update (const Symm_BdMatrix<T> &A);
46  //void update (const MatrixType &A);
47 
48  // solve functions
49  TVector<T> solve (TVector<T> x) const;
50 
51  /* template <typename T> */
52  inline TVector<T> solve (const Vector<T> &v) const
53  {
54  TVector<T> tv(v);
55  return (solve(tv));
56  }
57 
58 
59  /* template <typename T> */
60  inline TVector<T> transSolve (const Vector<T> &v) const
61  { return solve(v); }
62  /* template <typename T> */
63  inline TVector<T> transSolve (TVector<T> tv) const
64  { return solve(tv); }
65 
66  private:
67  BVector<T> pivots;
68  const Symm_BdMatrix<T> *matrix;
69 
70  // copy Matrix varibales to local vars
72  unsigned int dimension;
73  BVector<T*> rowPtr;
74 };
75 
76 template<typename T>
78 {
79  matrix = &A;
80  if (pivots.size() != matrix->rows())
81  pivots.resize(matrix->rows());
82  if (UNLIKELY(pivots.size() == 0))
83  STD__ cerr << "\n NULL pointer encountered! \n" << STD__ flush;
84  for (unsigned int i=0; i<matrix->rows(); ++i)
85  pivots(i) = T(1.0)/(*matrix)(i,i);
86  // copy Matrix varibales to local vars
87  conf.resize(matrix->conf);
88  dimension = matrix->dimension;
89  rowPtr.resize(matrix->rowPtr);
90 }
91 
92 
93 template<typename T>
95 {
96  unsigned int* confPtr;
97  T* elementPtr;
99 
100  const unsigned int csize = conf.size();
101  // check for trivial solution
102  // Matrix consists of just the main diagonal
103  if (LIKELY(csize == 0)) {
104  for (unsigned int i=0; i<dimension; ++i)
105  y.setval(i) = pivots(i)*y.get(i);
106  return y;
107  }
108 
109 
110  // Otherwise solve via incompl. LU decomp.:
111  // LUy=x, Uy=z, L=D+L, U=I+D^(-1)*U
112  // We only generate one TVector, which will be written to twice
113 
114  // Lz=y-> (D + L)*z = y
115  // write to y instead of temporary vector (z)
116  for (int g = 0; g < (int)dimension; ++g) {
117  sum = (T)0.0;
118  int cnfi;
119  for (unsigned int i=0; (i<csize) && ((cnfi = conf(i)) <= g);) {
120  elementPtr = rowPtr(g - cnfi) + ++i;
121  sum += (*elementPtr) * y.get(g - cnfi);
122  }
123 
124  y.setval(g) = pivots(g) * (y.get(g) - sum);
125  }
126 
127 
128  // Ux=z -> (I + D^(-1)*U)*x = z
129  // take the values obtained above for the RHS (z)
130  // and overwrite step by step
131  for (int h = dimension-2; h >= 0; --h)
132  {
133  confPtr = conf.vecptr();
134  elementPtr = rowPtr(h) +1;
135 
136  sum = (T)0.0;
137  while (elementPtr != rowPtr(h+1)) {
138  sum += (*elementPtr) * y.get(h + (*confPtr));
139  confPtr++;
140  elementPtr++;
141  }
142 
143  y.setval(h) -= pivots(h) * sum;
144  }
145 
146  // presto!
147  return y;
148 }
149 
150 
151 // ********************************************************************************
152 // Now for general BdMatrices
153 // ********************************************************************************
154 
155 
156 template <typename T> class BdMatrix;
157 
161 template <typename T>
162 class ILU0_BdMatrixPreconditioner : public Preconditioner_Sig<T, BdMatrix<T> >
163 {
164  public:
167  { update(A); }
169 
170  // update function
171  void update (const BdMatrix<T> &A);
172 
173  // solve functions
174  TVector<T> solve (TVector<T> x) const;
175 
176  inline TVector<T> solve (const Vector<T> &v) const
177  {
178  TVector<T> tv(v);
179  return (solve(tv));
180  }
181 
182  /* template <typename T> */
183  inline TVector<T> transSolve (const Vector<T> &v) const
184  { return solve(v); }
185  /* template <typename T> */
186  inline TVector<T> transSolve ( TVector<T> tv) const
187  { return solve(tv); }
188 
189  private:
190  BVector<T> pivots;
191 
192  BVector<T*> ldiag, udiag;
193  BVector<unsigned int> bdconf;
194 
195  unsigned int dimension;
196 };
197 
198 template <typename T>
200 {
201  //copy matrix variables to private vars
202  dimension = A.dim;
203  udiag.resize(A.adiag); ldiag.resize(A.bdiag);
204 
205  // calculate Pivots
206  if (pivots.size() != dimension)
207  pivots.resize(dimension);
208  BCHK(pivots.size() == 0, VecErr, "ILU0_BdMPrecond::update: Null ptr", 0,);
209  for (unsigned i = 0; i < dimension; ++i)
210  pivots.set(i) = T(1.0) / A.get(i,i);
211 
212  // fill in configuration vectors for BandMatrix
213  bdconf.resize(A.diagconf);
214 }
215 
217 template <typename T>
219 {
221 
222  // check dimensions
223  BCHK(dimension != y.size(), VecErr, "ILU0_BdMPrecond::solve: Dims don't match", dimension, y);
224  // check trivial solution
225  // Matrix consists of main diagonal only
226  if (bdconf.size() == 0) {
227  for (unsigned int i=0; i<dimension; ++i)
228  y.setval(i) *= pivots.get(i);
229  return y;
230  }
231 
232  // Otherwise solve via incompl. LU decomposition:
233  // LUy=x, Uy=z, L=D+L, U=I+D^(-1)*U
234  // We only use the passed TVector, which will be written to twice
235 
236  // Lz=y-> (D + L)*z = y
237  // write to y instead of temporary Vector (z)
238  y.setval(0) = pivots.get(0) * (y.get(0));
239  int h;
240  const unsigned int bsize = bdconf.size();
241  for (h = 1; h < (int)dimension; ++h) {
242  sum = (T)0; int bdi;
243  for (unsigned int i=0; i < bsize && (bdi = bdconf.get(i)) <= h; ++i)
244  sum += (*(ldiag.get(bdi) + h-bdi)) * y.get (h-bdi);
245  y.setval(h) = pivots.get(h) * (y.get(h) - sum);
246  }
247 
248  // Ux=z -> (I + D^(-1)*U)*x = z
249  // for the RHS (z), take the values obtained above
250  // and overwrite y piecewise
251 
252  //y.setval(dimension-1) = y.get(dimension-1);
253  for (h = dimension-2; h >= 0; --h) {
254  sum = (T)0; int bdi;
255  for (unsigned int i=0; i < bsize && (bdi = bdconf.get(i))+h < (int)dimension; ++i)
256  sum += (*(udiag.get(bdi) + h)) * y.get(bdi + h);
257  y.setval(h) -= pivots.get(h) * sum;
258  }
259 
260  // presto!
261  return y;
262 }
263 
265 
266 #endif /* TBCI_SOLVER_ILU0PRECOND_H */
T sum(const FS_Vector< dims, T > &fv)
Definition: fs_vector.h:599
const Vector< T > const Vector< T > & x
Definition: LM_fit.h:97
Abstract base class for all Preconditioners.
Definition: precond.h:40
ILU0_Symm_BdMatrixPreconditioner(const Symm_BdMatrix< T > &A)
Definition: ilu0precond.h:39
T & setval(const unsigned long i) const
Definition: vector.h:192
provides basic Vector functionality but arithmetic operators (+=, - , *, /...).
Definition: bvector.h:67
const Vector< T > const Vector< T > const Vector< T > int T h
Definition: LM_fit.h:97
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
Definition: band_matrix.h:103
#define REGISTER
Definition: basics.h:108
#define NAMESPACE_TBCI
Definition: basics.h:317
unsigned int dim
Definition: band_matrix.h:109
TVector< T > solve(TVector< T > x) const
Solve routine as sketched in the Templates book, Fig. 3.2.
Definition: ilu0precond.h:218
tbci_traits< T >::const_refval_type get(const unsigned int, const unsigned int) const HOT
Definition: band_matrix.h:694
tbci_traits< T >::const_refval_type get(const unsigned long i) const
Definition: vector.h:190
#define MIN_ALIGN2
Definition: basics.h:424
BVector< T * > bdiag
pointers to upper and lower diagonals
Definition: band_matrix.h:112
#define BCHK(cond, exc, txt, ind, rtval)
Definition: basics.h:575
#define UNLIKELY(expr)
Definition: basics.h:101
void update(const Symm_BdMatrix< T > &A)
Definition: ilu0precond.h:77
Matrix class with optimized Matrix-Vector multiplication for symmetrical Matrices.
Definition: symm_bdmatrix.h:25
TVector< T > solve(const Vector< T > &v) const
Definition: ilu0precond.h:176
void resize(const T &, const unsigned int)
TVector< T > transSolve(TVector< T > tv) const
Definition: ilu0precond.h:63
void update(const BdMatrix< T > &A)
Definition: ilu0precond.h:199
ILU0_BdMatrixPreconditioner(const BdMatrix< T > &A)
Definition: ilu0precond.h:166
TVector< T > transSolve(const Vector< T > &v) const
Definition: ilu0precond.h:183
unsigned long size() const
Definition: vector.h:104
BVector< unsigned > diagconf
configuration
Definition: band_matrix.h:111
TVector< T > solve(TVector< T > x) const
Definition: ilu0precond.h:94
#define ALIGN2(v, x)
Definition: basics.h:443
int i
Definition: LM_fit.h:71
TVector< T > transSolve(const Vector< T > &v) const
Definition: ilu0precond.h:60
#define STD__
Definition: basics.h:338
Temporary Base Class Idiom: Class TVector is used for temporary variables.
Definition: bvector.h:52
TVector< T > transSolve(TVector< T > tv) const
Definition: ilu0precond.h:186
#define NAMESPACE_END
Definition: basics.h:323
Definition: bvector.h:54
const Vector< T > Vector< T > Vector< T > Vector< T > & y
Definition: LM_fit.h:172
#define T
Definition: bdmatlib.cc:20
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int int char v
&lt; find minimun of func on grid with resolution res
Definition: LM_fit.h:205
BVector< T * > adiag
Definition: band_matrix.h:112
#define LIKELY(expr)
branch prediction note that we sometimes on purpose mark the unlikely possibility likely and vice ver...
Definition: basics.h:100
TVector< T > solve(const Vector< T > &v) const
Definition: ilu0precond.h:52
exception class
Definition: bvector.h:31