File: gmres.cpp

package info (click to toggle)
blitz%2B%2B 1%3A1.0.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,016 kB
  • sloc: cpp: 56,889; python: 1,939; fortran: 1,510; f90: 852; makefile: 828; sh: 309
file content (257 lines) | stat: -rw-r--r-- 8,445 bytes parent folder | download | duplicates (4)
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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/***************************************************************************
 * gmres.cpp  The GMRES iterative algorithm for solving large scale
 *            sparse or band-limited linear systems
 *
 * references: 
 * [1]   Youcef Saad and Martin H. Schultz, "GMRES: a generalized minimal 
 *       residual algorithm for solving nonsymmetric linear systems",
 *       SIAM J. Sci. Stat. Comput., Vol. 7, No. 3, July 1986, pp. 856--869
 * [2]   http://netlib2.cs.utk.edu/linalg/html_templates/Templates.html
 * [3]   http://www.netlib.org/utk/papers/templates/node29.html
 *
 * Also check http://www.netlib.org/utk/papers/iterative-survey/
 *
 *
 * Copyright (C) 2000-2006 Idesbald van den Bosch <vandenbosch.mailinglist@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Suggestions:          vandenbosch.mailinglist@gmail.com
 * Bugs:                 vandenbosch.mailinglist@gmail.com
 *
 * For more information, please see the ... Home Page:
 *
 ****************************************************************************/
#include <iostream>
#include <complex>
#include <cmath>
#include <blitz/array.h>

using namespace blitz;

double norm2 (const Array<complex<double>, 1>& x)
{
  return sqrt(abs(sum(x * conj(x))));
}

void checkMatrixVectorDimensions(const int M, // required number of A lines 
                                 const int N, // required number of A columns 
                                 const Array<complex<double>, 2>& A, 
                                 const Array<complex<double>, 1>& x)
// check dimensions in view of performing A*x or solving A*x = y 
{
  if ( A.extent(0) != M ) {
    cout << "checkMatrixVectorDimensions():" << endl;
    cout << "problem with A dimensions: A.extent(0) = " << A.extent(0) << endl;
    cout << "M = " << M << endl;
    exit(1);
  }
  if ( A.extent(1) != N ) {
    cout << "checkMatrixVectorDimensions():" << endl;
    cout << "problem with A dimensions: A.extent(1) = " << A.extent(1) << endl;
    cout << "N = " << N << endl;
    exit(1);
  }
  if ( x.extent(0) != N ) {
    cout << "checkMatrixVectorDimensions():" << endl;
    cout << "problem with x dimensions: x.extent(0) = " << x.extent(0) << endl;
    cout << "N = " << N << endl;
    exit(1);
  }
}

Array<complex<double>, 1> matrixMultiply (const Array<complex<double>, 2>& A,
                                          const Array<complex<double>, 1>& b)
{
  int i, j;
  const int M = A.extent(0), N = b.extent(0);
  // check dimensions
  checkMatrixVectorDimensions(M, N, A, b);
  Array<complex<double>, 1> result(M);
  result = 0.0;
  for (i=0 ; i<M ; i++) {
    for (j=0 ; j<N ; j++) {
      result(i) += A(i,j) * b(j);
    }
  }
  return result;
}

void triangleUpSolve(Array<complex<double>, 1>& x,
                     const Array<complex<double>, 2>& T,
                     const Array<complex<double>, 1>& y)
/*
 * This function computes the solution of a linear system
 * where T is a upper triangular matrix, i.e. T(i>j, j) = 0.0.
 */
{
  int i, j;
  const int N = y.extent(0);
  // check dimensions: T must be square
  checkMatrixVectorDimensions(N, N, T, y);
  for (i=N-1 ; i>-1 ; i--) {
    // x(i) = 1/T(i, i) * ( y(i) - sum(T(i, i+1:N-1) * x(i+1:N-1)) )
    x(i) = y(i);
    for (j=i+1 ; j<N ; j++) {
      x(i) -= T(i, j) * x(j);
    }
    x(i) /= T(i, i);
  }
}

void rotmat(complex<double>& cs, 
            complex<double>& sn, 
            const complex<double>& a, 
            const complex<double>& b)
// compute the Givens rotation matrix parameters for a and b
{
  complex<double> temp;
  if (b==0.0) {
    cs = 1.0;
    sn = 0.0;
  }
  else if ( abs(b) > abs(a) ) {
    temp = -a/b;
    sn = 1.0 / sqrt(1.0 + temp*temp);
    cs = temp * sn;
  }
  else {
    temp = -b/a;
    cs = 1.0 / sqrt(1.0 + temp*temp);
    sn = temp * cs;
  }
}

void gmres(Array<complex<double>, 1>& x, // INPUT, OUTPUT: initial guess, converged solution
           double & error, // OUTPUT: the error
           int & iter, // OUTPUT: number of iterations needed
           int & flag, // OUTPUT: success flag: 0 if OK
           const Array<complex<double>, 2>& A, // INPUT: complex matrix
           const Array<complex<double>, 1>& b, // INPUT: right-hand side
           const double tol, // INPUT: tolerance on solution
           const int RESTRT, // INPUT: restart number
           const int MAXITER) // INPUT: max number of iterations
/*************************************************************************
 * gmres solves the non-symmetric linear system Ax = b using the 
 * Generalized Minimum Residual method
 *
 * The flag output 0 indicates convergence within MAXITER
 * iterations. If no convergence within MAXITER iterations: flag = 1.
 *
 * Upon successful return, other output arguments have the following values:
 *
 *        x  --  approximate solution to A*x = b
 *     iter  --  the number of iterations performed before the
 *               tolerance was reached
 *    error  --  the residual after the final iteration
 **************************************************************************/
{
  Range all = Range::all();
  flag = 0;
  int i, k;
  const int N = b.extent(0), m = RESTRT; // size of the system
  // dimensions and other checks
  checkMatrixVectorDimensions(N, N, A, x);
  if (RESTRT < 1) {
    cout << "bad restart value. RESTRT = " << RESTRT << endl;
    exit(1);
  }
  if (MAXITER < 1) {
    cout << "bad maxiter value. MAXITER = " << MAXITER << endl;
    exit(1);
  }

  double bnorm2 = norm2(b), rnorm2;
  if (bnorm2==0.0) bnorm2 = 1.0;
  complex<double> temp;
  Array<complex<double>, 1> r(N);

  // r = precondSolve(M, b, A, x) 
  //   = M^(-1) * ( b-A*x )
  // this will be done in a later version of the code
  r = b - matrixMultiply(A, x);
  error = norm2(r) / bnorm2;
  if (error<tol) return;

  // workspaces definitions
  Array<complex<double>, 1> cs(m), sn(m), e1(m+1), s(m+1), w(N), y;
  Array<complex<double>, 2> V(N, m+1), H(m+1, m);
  e1 = 0.0;
  e1(0) = 1.0;

  for (iter=0 ; iter<MAXITER ; iter++) {
    // r = precondSolve(M, b, A, x) 
    //   = M^(-1) * ( b-A*x )
    // this will be done in a later version of the code
    r = b - matrixMultiply(A, x);
    rnorm2 = norm2(r);
    V(all, 0) = r/rnorm2;
    s = rnorm2 * e1;

    for (i=0 ; i<m ; i++){
      // if preconditioning: w = M^(-1) * (A*V(all, i))
      w = matrixMultiply(A, V(all, i));

      // construct orthonormal basis using Gram-Schmidt
      for (k=0 ; k<i+1 ; k++) {
        H(k, i) = sum(w * conj(V(all, k)));
        w -= H(k, i) * V(all, k);
      }
      H(i+1, i) = norm2( w );
      V(all, i+1) = w / H(i+1, i);

      // now we apply the GIVENS rotations
      for (k=0 ; k<i ; k++) {
        temp = conj(cs(k)) * H(k, i) - conj(sn(k)) * H(k+1, i);
        H(k+1, i) = sn(k) * H(k, i) + cs(k) * H(k+1, i);
        H(k, i) = temp;
      }

      // form i-th rotation matrix
      rotmat(cs(i), sn(i), H(i, i), H(i+1, i));
      H(i, i) = conj(cs(i)) * H(i, i) - conj(sn(i)) * H(i+1, i);
      H(i+1,i) = 0.0;

      // approximate residual norm
      temp = conj(cs(i)) * s(i) - conj(sn(i)) * s(i+1);
      s(i+1) = sn(i) * s(i) + cs(i) * s(i+1);
      s(i) = temp;
      error = abs(s(i+1)) / bnorm2;

      // update approximation x
      if ( error<=tol ) {
        y.resize(i+1); // Range: 0..i
        triangleUpSolve( y, H(Range(0, i), Range(0, i)), s(Range(0, i)) );
        x += matrixMultiply(V(all, Range(0, i)), y);
        return;
      }
    } // end for (i =...)
    
    if ( error<=tol ) return;
    y.resize(m); // Range: 0..m-1
    triangleUpSolve( y, H(Range(0, m-1), Range(0, m-1)), s(Range(0, m-1)) );
    x += matrixMultiply(V(all, Range(0, m-1)), y);
    // r = precondSolve(M, b, A, x) 
    //   = M^(-1) * ( b-A*x )
    // this will be done in a later version of the code
    r = b - matrixMultiply(A, x);
    // check convergence
    error = abs(s(m)) / bnorm2;
    if ( error<=tol ) return;
  } // end for (iter =...)

  // bad ending...
  if (error>tol) flag = 1;
}