NiHu  2.0
GMRES.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2012, 2014 Kolja Brix <brix@igpm.rwth-aaachen.de>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_GMRES_H
12 #define EIGEN_GMRES_H
13 
14 #include <Eigen/IterativeLinearSolvers>
15 
16 #include <iostream>
17 
18 namespace Eigen {
19 
20 namespace internal {
21 
59 template<typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
60 bool gmres(const MatrixType & mat, const Rhs & rhs, Dest & x, const Preconditioner & precond,
61  Index &iters, const Index &restart, typename Dest::RealScalar & tol_error) {
62 
63  using std::sqrt;
64  using std::abs;
65 
66  typedef typename Dest::RealScalar RealScalar;
67  typedef typename Dest::Scalar Scalar;
68  typedef Matrix < Scalar, Dynamic, 1 > VectorType;
69  typedef Matrix < Scalar, Dynamic, Dynamic, ColMajor> FMatrixType;
70 
71  RealScalar tol = tol_error;
72  const Index maxIters = iters;
73  iters = 0;
74 
75  const Index m = mat.rows();
76 
77  // residual and preconditioned residual
78  VectorType p0 = rhs - mat*x;
79  VectorType r0 = precond.solve(p0);
80 
81  const RealScalar r0Norm = r0.norm();
82 
83  // is initial guess already good enough?
84  if(r0Norm == 0)
85  {
86  tol_error = 0;
87  return true;
88  }
89 
90  // storage for Hessenberg matrix and Householder data
91  FMatrixType H = FMatrixType::Zero(m, restart + 1);
92  VectorType w = VectorType::Zero(restart + 1);
93  VectorType tau = VectorType::Zero(restart + 1);
94 
95  // storage for Jacobi rotations
96  std::vector < JacobiRotation < Scalar > > G(restart);
97 
98  // storage for temporaries
99  VectorType t(m), v(m), workspace(m), x_new(m);
100 
101  // generate first Householder vector
102  Ref<VectorType> H0_tail = H.col(0).tail(m - 1);
103  RealScalar beta;
104  r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
105  w(0) = Scalar(beta);
106 
107  for (Index k = 1; k <= restart; ++k)
108  {
109  ++iters;
110 
111  v = VectorType::Unit(m, k - 1);
112 
113  // apply Householder reflections H_{1} ... H_{k-1} to v
114  // TODO: use a HouseholderSequence
115  for (Index i = k - 1; i >= 0; --i) {
116  v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
117  }
118 
119  // apply matrix M to v: v = mat * v;
120  t.noalias() = mat * v;
121  v = precond.solve(t);
122 
123  // apply Householder reflections H_{k-1} ... H_{1} to v
124  // TODO: use a HouseholderSequence
125  for (Index i = 0; i < k; ++i) {
126  v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
127  }
128 
129  if (v.tail(m - k).norm() != 0.0)
130  {
131  if (k <= restart)
132  {
133  // generate new Householder vector
134  Ref<VectorType> Hk_tail = H.col(k).tail(m - k - 1);
135  v.tail(m - k).makeHouseholder(Hk_tail, tau.coeffRef(k), beta);
136 
137  // apply Householder reflection H_{k} to v
138  v.tail(m - k).applyHouseholderOnTheLeft(Hk_tail, tau.coeffRef(k), workspace.data());
139  }
140  }
141 
142  if (k > 1)
143  {
144  for (Index i = 0; i < k - 1; ++i)
145  {
146  // apply old Givens rotations to v
147  v.applyOnTheLeft(i, i + 1, G[i].adjoint());
148  }
149  }
150 
151  if (k<m && v(k) != (Scalar) 0)
152  {
153  // determine next Givens rotation
154  G[k - 1].makeGivens(v(k - 1), v(k));
155 
156  // apply Givens rotation to v and w
157  v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
158  w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
159  }
160 
161  // insert coefficients into upper matrix triangle
162  H.col(k-1).head(k) = v.head(k);
163 
164  tol_error = abs(w(k)) / r0Norm;
165  std::cout << "iteration: " << iters << ", error: " << tol_error << std::endl;
166  bool stop = (k==m || tol_error < tol || iters == maxIters);
167 
168  if (stop || k == restart)
169  {
170  // solve upper triangular system
171  Ref<VectorType> y = w.head(k);
172  H.topLeftCorner(k, k).template triangularView <Upper>().solveInPlace(y);
173 
174  // use Horner-like scheme to calculate solution vector
175  x_new.setZero();
176  for (Index i = k - 1; i >= 0; --i)
177  {
178  x_new(i) += y(i);
179  // apply Householder reflection H_{i} to x_new
180  x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
181  }
182 
183  x += x_new;
184 
185  if(stop)
186  {
187  return true;
188  }
189  else
190  {
191  k=0;
192 
193  // reset data for restart
194  p0.noalias() = rhs - mat*x;
195  r0 = precond.solve(p0);
196 
197  // clear Hessenberg matrix and Householder data
198  H.setZero();
199  w.setZero();
200  tau.setZero();
201 
202  // generate first Householder vector
203  r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
204  w(0) = Scalar(beta);
205  }
206  }
207  }
208 
209  return false;
210 
211 }
212 
213 }
214 
215 template< typename _MatrixType,
216  typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
217 class GMRES;
218 
219 namespace internal {
220 
221 template< typename _MatrixType, typename _Preconditioner>
222 struct traits<GMRES<_MatrixType,_Preconditioner> >
223 {
224  typedef _MatrixType MatrixType;
225  typedef _Preconditioner Preconditioner;
226 };
227 
228 }
229 
264 template< typename _MatrixType, typename _Preconditioner>
265 class GMRES : public IterativeSolverBase<GMRES<_MatrixType,_Preconditioner> >
266 {
267  typedef IterativeSolverBase<GMRES> Base;
268  using Base::matrix;
269  using Base::m_error;
270  using Base::m_iterations;
271  using Base::m_info;
272  using Base::m_isInitialized;
273 
274 private:
275  Index m_restart;
276 
277 public:
278  using Base::_solve_impl;
279  typedef _MatrixType MatrixType;
280  typedef typename MatrixType::Scalar Scalar;
281  typedef typename MatrixType::RealScalar RealScalar;
282  typedef _Preconditioner Preconditioner;
283 
284 public:
285 
287  GMRES() : Base(), m_restart(30) {}
288 
299  template<typename MatrixDerived>
300  explicit GMRES(const EigenBase<MatrixDerived>& A) : Base(A.derived()), m_restart(30) {}
301 
302  ~GMRES() {}
303 
306  Index get_restart() { return m_restart; }
307 
311  void set_restart(const Index restart) { m_restart=restart; }
312 
314  template<typename Rhs,typename Dest>
315  void _solve_with_guess_impl(const Rhs& b, Dest& x) const
316  {
317  bool failed = false;
318  for(Index j=0; j<b.cols(); ++j)
319  {
320  m_iterations = Base::maxIterations();
321  m_error = Base::m_tolerance;
322 
323  typename Dest::ColXpr xj(x,j);
324  if(!internal::gmres(matrix(), b.col(j), xj, Base::m_preconditioner, m_iterations, m_restart, m_error))
325  failed = true;
326  }
327  m_info = failed ? NumericalIssue
328  : m_error <= Base::m_tolerance ? Success
329  : NoConvergence;
330  m_isInitialized = true;
331  }
332 
334  template<typename Rhs,typename Dest>
335  void _solve_impl(const Rhs& b, MatrixBase<Dest> &x) const
336  {
337  x = b;
338  if(x.squaredNorm() == 0) return; // Check Zero right hand side
339  _solve_with_guess_impl(b,x.derived());
340  }
341 
342 protected:
343 
344 };
345 
346 } // end namespace Eigen
347 
348 #endif // EIGEN_GMRES_H
NiHu::bessel::H
make_complex< T >::type H(T const &z)
H^(K)_nu(z) Bessel function.
Definition: math_functions.hpp:365
Eigen::GMRES
A GMRES solver for sparse square problems.
Definition: GMRES.h:217
Eigen::GMRES::GMRES
GMRES(const EigenBase< MatrixDerived > &A)
Definition: GMRES.h:300
Eigen::GMRES::get_restart
Index get_restart()
Definition: GMRES.h:306
Eigen::GMRES::GMRES
GMRES()
Definition: GMRES.h:287
Eigen::GMRES::set_restart
void set_restart(const Index restart)
Definition: GMRES.h:311