FV3 Bundle
IPCG.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_IPCG_H_
12 #define OOPS_ASSIMILATION_IPCG_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 IPCG.h
24  * \brief Inexact-Preconditioned Conjugate Gradients solver.
25  *
26  * Golub-Ye Inexact-Preconditioned Conjugate Gradients solver for Ax=b.
27  * (G.H. Golub and Q. Ye 1999/00, SIAM J. Sci. Comput. 21(4) 1305-1320.)
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. The preconditioner can be variable.
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$ F_k \approx (A)^{-1} \f$.
38  *
39  * On exit, x will contain the solution.
40  * The return value is the achieved reduction in residual norm.
41  *
42  * Iteration will stop if the maximum iteration limit "maxiter" is reached
43  * or if the residual norm reduces by a factor of "tolerance".
44  *
45  * VECTOR must implement:
46  * - dot_product
47  * - operator(=)
48  * - operator(+=),
49  * - operator(-=)
50  * - operator(*=) [double * VECTOR],
51  * - axpy
52  *
53  * AMATRIX and PMATRIX must implement a method:
54  * - void multiply(const VECTOR&, VECTOR&) const
55  *
56  * which applies the matrix to the first argument, and returns the
57  * matrix-vector product in the second. (Note: the const is optional, but
58  * recommended.)
59  */
60 
61 template <typename VECTOR, typename AMATRIX, typename PMATRIX>
62 double IPCG(VECTOR & x, const VECTOR & b,
63  const AMATRIX & A, const PMATRIX & precond,
64  const int maxiter, const double tolerance ) {
65  VECTOR ap(x);
66  VECTOR p(x);
67  VECTOR r(x);
68  VECTOR s(x);
69  VECTOR oldr(x);
70  VECTOR w(x);
71  VECTOR v(x); // required for re-orthogonalization
72  VECTOR z(x); // required for re-orthogonalization
73 
74  std::vector<VECTOR> vVEC; // required for re-orthogonalization
75  std::vector<VECTOR> zVEC; // required for re-orthogonalization
76 
77  // Initial residual r = b - Ax
78  r = b;
79  double xnrm2 = dot_product(x, x);
80  if (xnrm2 != 0) {
81  A.multiply(x, s);
82  r -= s;
83  }
84 
85  // s = precond r
86  precond.multiply(r, s);
87 
88  double dotRr0 = dot_product(r, r);
89  double dotSr0 = dot_product(r, s);
90  double normReduction = 1.0;
91  double rdots_old = dotSr0;
92  double rdots = dotSr0;
93 
94  v = r;
95  v *= 1/sqrt(dotSr0);
96  z = s;
97  z *= 1/sqrt(dotSr0);
98 
99  vVEC.push_back(v);
100  zVEC.push_back(z);
101 
102  Log::info() << std::endl;
103  for (int jiter = 0; jiter < maxiter; ++jiter) {
104  Log::info() << " IPCG Starting Iteration " << jiter+1 << std::endl;
105 
106  if (jiter == 0) {
107  p = s;
108  } else {
109  w = r;
110  w -= oldr; // w=r-oldr
111  double beta = dot_product(s, w)/rdots_old;
112  p *= beta;
113  p += s; // p = s + beta*p
114  }
115 
116  A.multiply(p, ap); // ap = Ap
117 
118  oldr = r;
119 
120  double alpha = rdots/dot_product(p, ap);
121 
122  x.axpy(alpha, p); // x = x + alpha*p;
123  r.axpy(-alpha, ap); // r = r - alpha*ap;
124 
125  // Re-orthogonalization
126  for (int iiter = 0; iiter < jiter; ++iiter) {
127  double proj = dot_product(r, zVEC[iiter]);
128  r.axpy(-proj, vVEC[iiter]);
129  }
130 
131  precond.multiply(r, s); // returns s as approximate solve of As=r
132 
133  rdots_old = rdots;
134  rdots = dot_product(r, s);
135 
136  v = r;
137  v *= 1/sqrt(rdots);
138  z = s;
139  z *= 1/sqrt(rdots);
140  vVEC.push_back(v);
141  zVEC.push_back(z);
142 
143  normReduction = sqrt(dot_product(r, r)/dotRr0);
144  Log::info() << "IPCG end of iteration " << jiter+1 << ". Norm reduction= "
145  << util::full_precision(normReduction) << std::endl << std::endl;
146 
147  if (normReduction < tolerance) {
148  Log::info() << "IPCG: Achieved required reduction in residual norm." << std::endl;
149  break;
150  }
151  }
152 
153  Log::info() << "IPCG: end" << std::endl;
154 
155  return normReduction;
156 }
157 
158 } // namespace oops
159 
160 #endif // OOPS_ASSIMILATION_IPCG_H_
double IPCG(VECTOR &x, const VECTOR &b, const AMATRIX &A, const PMATRIX &precond, const int maxiter, const double tolerance)
Definition: IPCG.h:62
real(r8), dimension(cast_m, cast_n) p
The namespace for the main oops code.
subroutine, public info(self)
real(fp), parameter, public tolerance