TBCI Numerical high perf. C++ Library 2.8.0
ilu0precond.h
Go to the documentation of this file.
1
21
22#ifndef TBCI_SOLVER_ILU0PRECOND_H
23#define TBCI_SOLVER_ILU0PRECOND_H
24
25#include "tbci/solver/precond.h"
26
28
29template <typename T> class Symm_BdMatrix;
30
34template <typename T>
35class ILU0_Symm_BdMatrixPreconditioner : public Preconditioner_Sig<T, Symm_BdMatrix<T> >
36{
37 public:
42
43
44 // update function
45 void update (const Symm_BdMatrix<T> &A);
46 //void update (const MatrixType &A);
47
48 // solve functions
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
76template<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
93template<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
156template <typename T> class BdMatrix;
157
161template <typename T>
162class ILU0_BdMatrixPreconditioner : public Preconditioner_Sig<T, BdMatrix<T> >
163{
164 public:
169
170 // update function
171 void update (const BdMatrix<T> &A);
172
173 // solve functions
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;
194
195 unsigned int dimension;
196};
197
198template <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
217template <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 */
const Vector< T > const Vector< T > & x
Definition LM_fit.h:97
const Vector< T > const Vector< T > const Vector< T > int T h
Definition LM_fit.h:99
int i
Definition LM_fit.h:71
doublereal y
Definition TOMS_707.C:27
#define STD__
Definition basics.h:338
#define BCHK(cond, exc, txt, ind, rtval)
Definition basics.h:575
#define LIKELY(expr)
branch prediction note that we sometimes on purpose mark the unlikely possibility likely and vice ver...
Definition basics.h:100
#define MIN_ALIGN2
Definition basics.h:424
#define NAMESPACE_END
Definition basics.h:323
#define ALIGN2(v, x)
Definition basics.h:443
#define NAMESPACE_TBCI
Definition basics.h:317
#define UNLIKELY(expr)
Definition basics.h:101
#define REGISTER
Definition basics.h:108
#define T
Definition bdmatlib.cc:20
provides basic Vector functionality but arithmetic operators (+=, - , *, /...).
Definition bvector.h:68
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
tbci_traits< T >::const_refval_type get(const unsigned int, const unsigned int) const HOT
BVector< T * > adiag
BVector< T * > bdiag
pointers to upper and lower diagonals
unsigned int dim
BVector< unsigned > diagconf
configuration
TVector< T > solve(const Vector< T > &v) const
TVector< T > transSolve(TVector< T > tv) const
ILU0_BdMatrixPreconditioner(const BdMatrix< T > &A)
TVector< T > solve(TVector< T > x) const
Solve routine as sketched in the Templates book, Fig. 3.2.
TVector< T > transSolve(const Vector< T > &v) const
void update(const BdMatrix< T > &A)
TVector< T > transSolve(const Vector< T > &v) const
Definition ilu0precond.h:60
ILU0_Symm_BdMatrixPreconditioner(const Symm_BdMatrix< T > &A)
Definition ilu0precond.h:39
TVector< T > solve(TVector< T > x) const
Definition ilu0precond.h:94
TVector< T > transSolve(TVector< T > tv) const
Definition ilu0precond.h:63
void update(const Symm_BdMatrix< T > &A)
Definition ilu0precond.h:77
TVector< T > solve(const Vector< T > &v) const
Definition ilu0precond.h:52
Matrix class with optimized Matrix-Vector multiplication for symmetrical Matrices.
void resize(const T &, const unsigned int)
Temporary Base Class Idiom: Class TVector is used for temporary variables.
Definition vector.h:73
exception class
Definition bvector.h:32
T sum(const FS_Vector< dims, T > &fv)
Definition fs_vector.h:599