BiCGStab2.hh 5.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "Preconditioner.h"

namespace AMDiS
{

  template<typename VectorType>
  BiCGStab2<VectorType>::BiCGStab2(::std::string name)
    : OEMSolver<VectorType>(name),
      r(NULL), rstar(NULL), u(NULL), v(NULL), s(NULL), w(NULL), t(NULL),
      xmin(NULL)
  {}

  template<typename VectorType>
  BiCGStab2<VectorType>::~BiCGStab2()
  {}

  template<typename VectorType>
  void BiCGStab2<VectorType>::init()
  {
    r     = this->vectorCreator->create();
    rstar = this->vectorCreator->create();
    u     = this->vectorCreator->create();
    v     = this->vectorCreator->create();
    s     = this->vectorCreator->create();
    w     = this->vectorCreator->create();
    t     = this->vectorCreator->create();
    xmin  = this->vectorCreator->create();
  }

  template<typename VectorType>
  void BiCGStab2<VectorType>::exit()
  {
    this->vectorCreator->free(r);
    this->vectorCreator->free(rstar);
    this->vectorCreator->free(u);
    this->vectorCreator->free(v);
    this->vectorCreator->free(s);
    this->vectorCreator->free(w);
    this->vectorCreator->free(t);
    this->vectorCreator->free(xmin);
  }

  template<typename VectorType>
  int BiCGStab2<VectorType>::solveSystem(MatVecMultiplier<VectorType> *mv,
					 VectorType *x, VectorType *b)
  {
    FUNCNAME("BiCGStab2::solveSystem()");

    double res, old_res = -1.0;
    double rho0, alpha, omega1, omega2, rho1, beta, gamma, mu, nu, tau;
    int    iter;

    const double TOL = 1e-30;

    /*------------------------------------------------------------------------*/
    /*---  Initalization  ----------------------------------------------------*/
    /*------------------------------------------------------------------------*/

    *u = *b;
    if (this->leftPrecon)
      this->leftPrecon->precon(u);
    double normB = norm(u);

    if (normB < TOL) {
      INFO(this->info, 2)("b == 0; x = 0 is the solution of the linear system\n");
      setValue(*x, 0.0);
      this->residual = 0.0;
      return(0);
    }
    
    double save_tolerance = this->tolerance;
    if (this->relative)
      this->tolerance *= normB;

    *xmin = *x;
    int imin = 0;

    // r = b - Ax
    mv->matVec(NoTranspose, *x, *r);
    *r *= -1.0;
    *r += *b;

    if (this->leftPrecon) 
      this->leftPrecon->precon(r);

    /*---  check initial residual  -------------------------------------------*/

    res = norm(r);

    START_INFO();
    if (SOLVE_INFO(0, res, &old_res)) {
      if (this->relative)
	this->tolerance = save_tolerance;
      return(0);
    }

    double normrmin = res;

    // setting for the method
    *rstar  = *r;
    *rstar *= 1.0 / res;

    rho0 = 1.0; 
    alpha = 0.0; 
    omega2 = 1.0;
    setValue(*u, 0.0);

    /*------------------------------------------------------------------------*/
    /*---  Iteration  --------------------------------------------------------*/
    /*------------------------------------------------------------------------*/

    for (iter = 1; iter <= this->max_iter; iter++) {
      rho0 *= -omega2;

      /*---  even BiCG step  -------------------------------------------------*/
      
      // updating u
      rho1 = *r * *rstar;
      beta = alpha * rho1 / rho0;
      rho0 = rho1;
      *u *= -beta;
      *u += *r;
      
      // computing v
      mv->matVec(NoTranspose, *u, *v);
      if (this->leftPrecon) 
	this->leftPrecon->precon(v);
      
      // Updating x and r
      gamma = *v * *rstar;
      alpha = rho0 / gamma;
      axpy(alpha, *u, *x);
      axpy(-alpha, *v, *r);
      
      // computing s
      mv->matVec(NoTranspose, *r, *s);
      if (this->leftPrecon) 
	this->leftPrecon->precon(s);
      
      /*---  odd BiCG step  --------------------------------------------------*/
      
      // updating v
      rho1 = *s * *rstar;
      beta = alpha * rho1 / rho0;
      rho0 = rho1;
      *v *= -beta;
      *v += *s;
      
      // computing w
      mv->matVec(NoTranspose, *v, *w);
      if (this->leftPrecon) 
	this->leftPrecon->precon(w);
      
      // updating u, r and s
      gamma = *w * *rstar;
      alpha = rho0 / gamma;
      *u *= -beta;
      *u += *r;
      axpy(-alpha, *v, *r);
      axpy(-alpha, *w, *s);
161

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
      // computing t
      mv->matVec(NoTranspose, *s, *t);
      if (this->leftPrecon) 
	this->leftPrecon->precon(t);
      
      /*---  CGR(2) part  ----------------------------------------------------*/
      
      // computing constants
      omega1  = *r * *s;
      mu      = *s * *s;
      nu      = *s * *t;
      tau     = *t * *t;
      omega2  = *r * *t;
      tau    -= nu * nu / mu;
      omega2 -= nu * omega1 / mu;
      omega2 /= tau;
      omega1 -= nu * omega2;
      omega1 /= mu;
      
      // updating x
      axpy(omega1, *r, *x);
      axpy(omega2, *s, *x);
      axpy(alpha, *u, *x);
      
      // updating r
      axpy(-omega1, *s, *r);
      axpy(-omega2, *t, *r);
      /*---  checking accuracy  ----------------------------------------------*/
      
      res = norm(r);
      if (SOLVE_INFO(iter, res, &old_res) == 1) {
	if (this->relative)
	  this->tolerance = save_tolerance;
	return(iter);
      }

      // update minimal norm quantities
      if (res < normrmin) {	
	normrmin = res;
	*xmin    = *x;
	imin     = iter;
      } else if (res > normrmin * 1e+6) {
	INFO(this->info,2)("Linear solver diverges.\n");
	INFO(this->info,2)("Current iteration: %d.\n", iter);
	INFO(this->info,2)("Current residual: %e.\n", res);
	break;
      }
      
      // updating u
      axpy(-omega1, *v, *u);
      axpy(-omega2, *w, *u);
    }
    
    // returned solution is first with minimal residual
216
217
    *x = *xmin;
    iter = imin;
218
219
220
221
222
223
224
225
226
227
228
    this->residual = normrmin;
    
    if (this->relative) 
      this->tolerance = save_tolerance;

    INFO(this->info,2)("The minimal norm was %e; it was achieved in iteration %d.\n",
		       this->residual, iter);

    return(iter);
  }
}