~ubuntu-branches/ubuntu/trusty/rheolef/trusty-proposed

« back to all changes in this revision

Viewing changes to skit/lib/cgs.h

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2010-06-12 09:08:59 UTC
  • Revision ID: james.westby@ubuntu.com-20100612090859-8gpm2gc7j3ab43et
Tags: upstream-5.89
ImportĀ upstreamĀ versionĀ 5.89

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ifndef _SKIT_CGS_H
 
2
# define _SKIT_CGS_H
 
3
///
 
4
/// This file is part of Rheolef.
 
5
///
 
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
7
///
 
8
/// Rheolef is free software; you can redistribute it and/or modify
 
9
/// it under the terms of the GNU General Public License as published by
 
10
/// the Free Software Foundation; either version 2 of the License, or
 
11
/// (at your option) any later version.
 
12
///
 
13
/// Rheolef is distributed in the hope that it will be useful,
 
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
/// GNU General Public License for more details.
 
17
///
 
18
/// You should have received a copy of the GNU General Public License
 
19
/// along with Rheolef; if not, write to the Free Software
 
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
/// 
 
22
/// =========================================================================
 
23
/*Class:cgs
 
24
NAME: cgs - cgs-conjugate gradient method 
 
25
SYNOPSIS:
 
26
    
 
27
    "int cgs" ("const Matrix &A, Vector &x, const Vector &b",
 
28
    |   const Preconditioner &M, int &max_iter, Real &tol);
 
29
 
 
30
EXAMPLE:
 
31
 
 
32
  The simplest call to 'cgs' has the folling form:
 
33
 
 
34
  |    int status = cgs(a, x, b, EYE, 100, 1e-7);
 
35
 
 
36
DESCRIPTION:       
 
37
 
 
38
 'cgs' solves the unsymmetric linear system Ax = b 
 
39
 using the conjugate gradient squared method.
 
40
 
 
41
  The return value indicates convergence within max_iter (input)
 
42
  iterations (0), or no convergence within max_iter iterations (1).
 
43
  Upon successful return, output arguments have the following values:
 
44
 
 
45
    "x"  approximate solution to Ax = b
 
46
 
 
47
    "max_iter" the number of iterations performed before the tolerance was reached
 
48
 
 
49
    "tol" the residual after the final iteration
 
50
 
 
51
SEE ALSO:
 
52
 
 
53
  "cg", "bicg", "bichstab", class "csr", class "vec"
 
54
 
 
55
NOTE: 
 
56
 
 
57
  'cgs' is an iterative template routine.
 
58
 
 
59
  'cgs' follows the algorithm described on p. 26 in
 
60
 
 
61
        Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods, 
 
62
        2nd Edition, 
 
63
        R. Barrett, M. Berry, T. F. Chan, J. Demmel, J. Donato, J. Dongarra, V. Eijkhout,
 
64
        R. Pozo, C. Romine, H. Van der Vorst,
 
65
        SIAM, 1994, "ftp.netlib.org/templates/templates.ps".
 
66
 
 
67
  The present implementation is inspired from "IML++ 1.2" iterative method library,
 
68
  "http://math.nist.gov/iml++".
 
69
 
 
70
AUTHOR: 
 
71
 
 
72
    Pierre Saramito
 
73
    | Pierre.Saramito@imag.fr
 
74
    LMC-IMAG, 38041 Grenoble cedex 9, France
 
75
 
 
76
DATE: 
 
77
    
 
78
    12 march 1997
 
79
 
 
80
METHODS: @cgs
 
81
End:
 
82
*/
 
83
// ========== [ method implementation ] ====================================
 
84
//<cgs:
 
85
 
 
86
template < class Matrix, class Vector, class Preconditioner, class Real >
 
87
int 
 
88
cgs(const Matrix &A, Vector &x, const Vector &b,
 
89
    const Preconditioner &M, int &max_iter, Real &tol)
 
90
{
 
91
  Real resid;
 
92
  Real rho_1, rho_2=0, alpha, beta;
 
93
  Vector p, phat, q, qhat, vhat, u, uhat;
 
94
 
 
95
  Real normb = norm(b);
 
96
  Vector r = b - A*x;
 
97
  Vector rtilde = r;
 
98
 
 
99
  if (normb == Real(0))
 
100
    normb = 1;
 
101
  
 
102
  if ((resid = norm(r) / normb) <= tol) {
 
103
    tol = resid;
 
104
    max_iter = 0;
 
105
    return 0;
 
106
  }
 
107
 
 
108
  for (int i = 1; i <= max_iter; i++) {
 
109
    rho_1 = dot(rtilde, r);
 
110
    if (rho_1 == Real(0)) {
 
111
      tol = norm(r) / normb;
 
112
      return 2;
 
113
    }
 
114
    if (i == 1) {
 
115
      u = r;
 
116
      p = u;
 
117
    } else {
 
118
      beta = rho_1 / rho_2;
 
119
      u = r + beta * q;
 
120
      p = u + beta * (q + beta * p);
 
121
    }
 
122
    phat = M.solve(p);
 
123
    vhat = A*phat;
 
124
    alpha = rho_1 / dot(rtilde, vhat);
 
125
    q = u - alpha * vhat;
 
126
    uhat = M.solve(u + q);
 
127
    x += alpha * uhat;
 
128
    qhat = A * uhat;
 
129
    r -= alpha * qhat;
 
130
    rho_2 = rho_1;
 
131
    if ((resid = norm(r) / normb) < tol) {
 
132
      tol = resid;
 
133
      max_iter = i;
 
134
      return 0;
 
135
    }
 
136
  }
 
137
  
 
138
  tol = resid;
 
139
  return 1;
 
140
}
 
141
//>cgs:
 
142
#endif // _SKIT_CGS_H