00001 #ifndef DUNE_PRECONDITIONERS_HH
00002 #define DUNE_PRECONDITIONERS_HH
00003
00004 #include<cmath>
00005 #include<complex>
00006 #include<iostream>
00007 #include<iomanip>
00008 #include<string>
00009
00010 #include"solvercategory.hh"
00011 #include "istlexception.hh"
00012 #include "matrixutils.hh"
00013 #include "io.hh"
00014 #include "gsetc.hh"
00015 #include "ilu.hh"
00016
00017
00018 namespace Dune {
00056
00066
00067 template<class X, class Y>
00068 class Preconditioner {
00069 public:
00071 typedef X domain_type;
00073 typedef Y range_type;
00075 typedef typename X::field_type field_type;
00076
00092 virtual void pre (X& x, Y& b) = 0;
00093
00104 virtual void apply (X& v, const Y& d) = 0;
00105
00114 virtual void post (X& x) = 0;
00115
00116
00117 virtual ~Preconditioner () {}
00118 };
00119
00120
00121
00122
00123
00124
00125
00132 template<class M, class X, class Y, int l=1>
00133 class SeqSSOR : public Preconditioner<X,Y> {
00134 public:
00136 typedef M matrix_type;
00138 typedef X domain_type;
00140 typedef Y range_type;
00142 typedef typename X::field_type field_type;
00143
00144
00145 enum {
00147 category=SolverCategory::sequential};
00148
00156 SeqSSOR (const M& A, int n, field_type w)
00157 : _A_(A), _n(n), _w(w)
00158 {
00159 CheckIfDiagonalPresent<l>::check(_A_);
00160 }
00161
00167 virtual void pre (X& x, Y& b) {}
00168
00174 virtual void apply (X& v, const Y& d)
00175 {
00176 for (int i=0; i<_n; i++){
00177 bsorf(_A_,v,d,_w,BL<l>());
00178 bsorb(_A_,v,d,_w,BL<l>());
00179 }
00180 }
00181
00187 virtual void post (X& x) {}
00188
00189 private:
00191 const M& _A_;
00193 int _n;
00195 field_type _w;
00196 };
00197
00198
00199
00206 template<class M, class X, class Y, int l=1>
00207 class SeqSOR : public Preconditioner<X,Y> {
00208 public:
00210 typedef M matrix_type;
00212 typedef X domain_type;
00214 typedef Y range_type;
00216 typedef typename X::field_type field_type;
00217
00218
00219 enum {
00221 category=SolverCategory::sequential
00222 };
00223
00231 SeqSOR (const M& A, int n, field_type w)
00232 : _A_(A), _n(n), _w(w)
00233 {
00234 CheckIfDiagonalPresent<l>::check(_A_);
00235 }
00236
00242 virtual void pre (X& x, Y& b) {}
00243
00249 virtual void apply (X& v, const Y& d)
00250 {
00251 this->template apply<true>(v,d);
00252 }
00253
00262 template<bool forward>
00263 void apply(X& v, const Y& d)
00264 {
00265 if(forward)
00266 for (int i=0; i<_n; i++){
00267 bsorf(_A_,v,d,_w,BL<l>());
00268 }
00269 else
00270 for (int i=0; i<_n; i++){
00271 bsorb(_A_,v,d,_w,BL<l>());
00272 }
00273 }
00274
00280 virtual void post (X& x) {}
00281
00282 private:
00284 const M& _A_;
00286 int _n;
00288 field_type _w;
00289 };
00290
00291
00297 template<class M, class X, class Y, int l=1>
00298 class SeqGS : public Preconditioner<X,Y> {
00299 public:
00301 typedef M matrix_type;
00303 typedef X domain_type;
00305 typedef Y range_type;
00307 typedef typename X::field_type field_type;
00308
00309
00310 enum {
00312 category=SolverCategory::sequential
00313 };
00314
00322 SeqGS (const M& A, int n, field_type w)
00323 : _A_(A), _n(n), _w(w)
00324 {
00325 CheckIfDiagonalPresent<l>::check(_A_);
00326 }
00327
00333 virtual void pre (X& x, Y& b) {}
00334
00340 virtual void apply (X& v, const Y& d)
00341 {
00342 for (int i=0; i<_n; i++){
00343 dbgs(_A_,v,d,_w,BL<l>());
00344 }
00345 }
00346
00352 virtual void post (X& x) {}
00353
00354 private:
00356 const M& _A_;
00358 int _n;
00360 field_type _w;
00361 };
00362
00363
00369 template<class M, class X, class Y, int l=1>
00370 class SeqJac : public Preconditioner<X,Y> {
00371 public:
00373 typedef M matrix_type;
00375 typedef X domain_type;
00377 typedef Y range_type;
00379 typedef typename X::field_type field_type;
00380
00381
00382 enum {
00384 category=SolverCategory::sequential
00385 };
00386
00394 SeqJac (const M& A, int n, field_type w)
00395 : _A_(A), _n(n), _w(w)
00396 {
00397 CheckIfDiagonalPresent<l>::check(_A_);
00398 }
00399
00405 virtual void pre (X& x, Y& b) {}
00406
00412 virtual void apply (X& v, const Y& d)
00413 {
00414 for (int i=0; i<_n; i++){
00415 dbjac(_A_,v,d,_w,BL<l>());
00416 }
00417 }
00418
00424 virtual void post (X& x) {}
00425
00426 private:
00428 const M& _A_;
00430 int _n;
00432 field_type _w;
00433 };
00434
00435
00436
00443 template<class M, class X, class Y>
00444 class SeqILU0 : public Preconditioner<X,Y> {
00445 public:
00447 typedef typename Dune::remove_const<M>::type matrix_type;
00449 typedef X domain_type;
00451 typedef Y range_type;
00453 typedef typename X::field_type field_type;
00454
00455
00456 enum {
00458 category=SolverCategory::sequential
00459 };
00460
00467 SeqILU0 (const M& A, field_type w)
00468 : ILU(A)
00469 {
00470 _w =w;
00471 bilu0_decomposition(ILU);
00472 }
00473
00479 virtual void pre (X& x, Y& b) {}
00480
00486 virtual void apply (X& v, const Y& d)
00487 {
00488 bilu_backsolve(ILU,v,d);
00489 v *= _w;
00490 }
00491
00497 virtual void post (X& x) {}
00498
00499 private:
00501 field_type _w;
00503 matrix_type ILU;
00504 };
00505
00506
00513 template<class M, class X, class Y>
00514 class SeqILUn : public Preconditioner<X,Y> {
00515 public:
00517 typedef typename Dune::remove_const<M>::type matrix_type;
00519 typedef X domain_type;
00521 typedef Y range_type;
00523 typedef typename X::field_type field_type;
00524
00525
00526 enum {
00528 category=SolverCategory::sequential
00529 };
00530
00538 SeqILUn (const M& A, int n, field_type w)
00539 : ILU(A.N(),A.M(),M::row_wise)
00540 {
00541 _n = n;
00542 _w = w;
00543 bilu_decomposition(A,n,ILU);
00544 }
00545
00551 virtual void pre (X& x, Y& b) {}
00552
00558 virtual void apply (X& v, const Y& d)
00559 {
00560 bilu_backsolve(ILU,v,d);
00561 v *= _w;
00562 }
00563
00569 virtual void post (X& x) {}
00570
00571 private:
00573 matrix_type ILU;
00575 int _n;
00577 field_type _w;
00578 };
00579
00580
00581
00587 template<class X, class Y>
00588 class Richardson : public Preconditioner<X,Y> {
00589 public:
00591 typedef X domain_type;
00593 typedef Y range_type;
00595 typedef typename X::field_type field_type;
00596
00597
00598 enum {
00600 category=SolverCategory::sequential
00601 };
00602
00610 Richardson (field_type w=1.0)
00611 {
00612 _w = w;
00613 }
00614
00620 virtual void pre (X& x, Y& b) {}
00621
00627 virtual void apply (X& v, const Y& d)
00628 {
00629 v = d;
00630 v *= _w;
00631 }
00632
00638 virtual void post (X& x) {}
00639
00640 private:
00642 field_type _w;
00643 };
00644
00645
00646
00649 }
00650
00651 #endif