OOPS
PCG.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #ifndef OOPS_ASSIMILATION_PCG_H_
12 #define OOPS_ASSIMILATION_PCG_H_
13 
14 #include <cmath>
15 #include <vector>
16 
17 #include "oops/util/dot_product.h"
18 #include "oops/util/formats.h"
19 #include "oops/util/Logger.h"
20 
21 namespace oops {
22 
23 /*! \file PCG.h
24  * \brief Preconditioned Conjugate Gradients solver.
25  *
26  * This solver is based on the standard Preconditioned Conjugate
27  * Gradients solver for Ax=b
28  *
29  * A must be square, symmetric, positive definite.
30  * A preconditioner must be supplied that, given a vector q, returns an
31  * approximate solution of Ap=q.
32  *
33  * On entry:
34  * - x = starting point, \f$ x_0 \f$.
35  * - b = right hand side.
36  * - A = \f$ A \f$.
37  * - precond = preconditioner \f$ P \approx (A)^{-1} \f$.
38  *
39  * On exit, x will contain the solution \f$ x \f$
40 
41  * The return value is the achieved reduction in preconditioned residual norm.
42  *
43  * Iteration will stop if the maximum iteration limit "maxiter" is reached
44  * or if the preconditioned residual norm reduces by a factor of "tolerance".
45  *
46  * VECTOR must implement:
47  * - dot_product
48  * - operator(=)
49  * - operator(+=),
50  * - operator(-=)
51  * - operator(*=) [double * VECTOR],
52  * - axpy
53  *
54  * Each of AMATRIX and PMATRIX must implement a method:
55  * - void multiply(const VECTOR&, VECTOR&) const
56  *
57  * which applies the matrix to the first argument, and returns the
58  * matrix-vector product in the second. (Note: the const is optional, but
59  * recommended.)
60  */
61 
62 template <typename VECTOR, typename AMATRIX, typename PMATRIX>
63 double PCG(VECTOR & x, const VECTOR & b,
64  const AMATRIX & A, const PMATRIX & precond,
65  const int maxiter, const double tolerance ) {
66  VECTOR ap(x);
67  VECTOR p(x);
68  VECTOR r(x);
69  VECTOR s(x);
70  VECTOR v(x);
71  VECTOR z(x);
72 
73  std::vector<VECTOR> vVEC; // required for re-orthogonalization
74  std::vector<VECTOR> zVEC; // required for re-orthogonalization
75  // reserve space to avoid extra copies
76  vVEC.reserve(maxiter+1);
77  zVEC.reserve(maxiter+1);
78 
79  // Initial residual r = b - Ax
80  r = b;
81  double xnrm2 = dot_product(x, x);
82  if (xnrm2 > 0.0) {
83  A.multiply(x, s);
84  r -= s;
85  }
86 
87  // s = precond r
88  precond.multiply(r, s);
89 
90  double dotRr0 = dot_product(r, s);
91  double normReduction = 1.0;
92  double rdots = dotRr0;
93  double rdots_old = 0.0;
94 
95  v = r;
96  v *= 1/sqrt(dotRr0);
97  z = s;
98  z *= 1/sqrt(dotRr0);
99 
100  vVEC.push_back(v);
101  zVEC.push_back(z);
102 
103  Log::info() << std::endl;
104  for (int jiter = 0; jiter < maxiter; ++jiter) {
105  Log::info() << " PCG Starting Iteration " << jiter+1 << std::endl;
106 
107  if (jiter == 0) {
108  p = s;
109  } else {
110  double beta = dot_product(s, r)/rdots_old;
111  Log::info() << "PCG beta = " << beta << std::endl;
112 
113  p *= beta;
114  p += s; // p = s + beta*p
115  }
116 
117  A.multiply(p, ap); // ap = A*p
118 
119  double alpha = rdots/dot_product(p, ap);
120 
121  x.axpy(alpha, p); // x = x + alpha*p
122  r.axpy(-alpha, ap); // r = r - alpha*ap
123 
124  // Re-orthogonalization
125  for (int iiter = 0; iiter < jiter; ++iiter) {
126  double proj = dot_product(r, zVEC[iiter]);
127  r.axpy(-proj, vVEC[iiter]);
128  }
129 
130  precond.multiply(r, s);
131 
132  rdots_old = rdots;
133  rdots = dot_product(r, s);
134 
135  v = r;
136  v *= 1/sqrt(rdots);
137  z = s;
138  z *= 1/sqrt(rdots);
139  vVEC.push_back(v);
140  zVEC.push_back(z);
141 
142  normReduction = sqrt(rdots/dotRr0);
143 
144  Log::info() << "PCG end of iteration " << jiter+1 << ". Norm reduction= "
145  << util::full_precision(normReduction) << std::endl << std::endl;
146 
147  if (normReduction < tolerance) {
148  Log::info() << "PCG: Achieved required reduction in residual norm." << std::endl;
149  break;
150  }
151  }
152 
153  Log::info() << "PCG: end" << std::endl;
154 
155  return normReduction;
156 }
157 
158 } // namespace oops
159 
160 #endif // OOPS_ASSIMILATION_PCG_H_
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::PCG
double PCG(VECTOR &x, const VECTOR &b, const AMATRIX &A, const PMATRIX &precond, const int maxiter, const double tolerance)
Definition: PCG.h:63