14#include "../../Eigenvalues"
17#include "./InternalHeaderCheck.h"
21template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar> >
26template <
typename MatrixType_,
typename Preconditioner_>
27struct traits<DGMRES<MatrixType_, Preconditioner_> > {
28 using MatrixType = MatrixType_;
29 using Preconditioner = Preconditioner_;
40template <
typename VectorType,
typename IndexType>
41void sortWithPermutation(VectorType& vec, IndexType& perm,
typename IndexType::Scalar& ncut) {
42 eigen_assert(vec.size() == perm.size());
43 for (Index k = 0; k < ncut; k++) {
45 for (Index j = 0; j < vec.size() - 1; j++) {
46 if (vec(perm(j)) < vec(perm(j + 1))) {
47 std::swap(perm(j), perm(j + 1));
97template <
typename MatrixType_,
typename Preconditioner_>
103 using Base::m_isInitialized;
104 using Base::m_iterations;
105 using Base::m_tolerance;
109 using Base::_solve_impl;
110 using Base::_solve_with_guess_impl;
111 using MatrixType = MatrixType_;
112 using Scalar =
typename MatrixType::Scalar;
113 using StorageIndex =
typename MatrixType::StorageIndex;
114 using RealScalar =
typename MatrixType::RealScalar;
115 using ComplexScalar = internal::make_complex_t<Scalar>;
116 using Preconditioner = Preconditioner_;
125 : Base(), m_restart(30), m_neig(0), m_r(0), m_maxNeig(5), m_isDeflAllocated(false), m_isDeflInitialized(false) {}
137 template <
typename MatrixDerived>
144 m_isDeflAllocated(false),
145 m_isDeflInitialized(false) {}
148 template <
typename Rhs,
typename Dest>
149 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
150 EIGEN_STATIC_ASSERT(Rhs::ColsAtCompileTime == 1 || Dest::ColsAtCompileTime == 1,
151 YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
154 m_error = Base::m_tolerance;
156 dgmres(matrix(), b, x, Base::m_preconditioner);
174 if (neig + 1 > m_maxNeig) m_maxNeig = neig + 1;
189 template <
typename Rhs,
typename Dest>
190 void dgmres(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
const Preconditioner& precond)
const;
192 template <
typename Dest>
193 Index
dgmresCycle(
const MatrixType& mat,
const Preconditioner& precond, Dest& x, DenseVector& r0, RealScalar& beta,
194 const RealScalar& normRhs, Index& nbIts)
const;
196 Index dgmresComputeDeflationData(
const MatrixType& mat,
const Preconditioner& precond,
const Index& it,
197 StorageIndex& neig)
const;
199 template <
typename RhsType,
typename DestType>
200 Index dgmresApplyDeflation(
const RhsType& In, DestType& Out)
const;
204 void dgmresInitDeflation(Index& rows)
const;
205 mutable DenseMatrix m_V;
206 mutable DenseMatrix m_H;
207 mutable DenseMatrix m_Hes;
208 mutable Index m_restart;
209 mutable DenseMatrix m_U;
210 mutable DenseMatrix m_MU;
211 mutable DenseMatrix m_T;
213 mutable StorageIndex m_neig;
215 mutable Index m_maxNeig;
216 mutable RealScalar m_lambdaN;
217 mutable bool m_isDeflAllocated;
218 mutable bool m_isDeflInitialized;
221 mutable RealScalar m_smv;
222 mutable bool m_force;
230template <
typename MatrixType_,
typename Preconditioner_>
231template <
typename Rhs,
typename Dest>
233 const Preconditioner& precond)
const {
234 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
236 RealScalar normRhs = rhs.norm();
237 if (normRhs <= considerAsZero) {
245 m_isDeflInitialized =
false;
246 Index n = mat.rows();
249 m_H.resize(m_restart + 1, m_restart);
250 m_Hes.resize(m_restart, m_restart);
251 m_V.resize(n, m_restart + 1);
253 if (x.squaredNorm() == 0) x = precond.solve(rhs);
254 r0.noalias() = rhs - mat * x;
255 RealScalar beta = r0.norm();
257 m_error = beta / normRhs;
258 if (m_error < m_tolerance)
265 dgmresCycle(mat, precond, x, r0, beta, normRhs, nbIts);
269 r0.noalias() = rhs - mat * x;
274 m_iterations = nbIts;
287template <
typename MatrixType_,
typename Preconditioner_>
288template <
typename Dest>
290 DenseVector& r0, RealScalar& beta,
const RealScalar& normRhs,
291 Index& nbIts)
const {
293 DenseVector g(m_restart + 1);
296 m_V.col(0) = r0 / beta;
298 std::vector<JacobiRotation<Scalar> > gr(m_restart);
300 Index n = mat.rows();
301 DenseVector tv1(n), tv2(n);
302 while (m_info ==
NoConvergence && it < m_restart && nbIts < m_iterations) {
304 if (m_isDeflInitialized) {
305 dgmresApplyDeflation(m_V.col(it), tv1);
306 tv2 = precond.solve(tv1);
308 tv2 = precond.solve(m_V.col(it));
310 tv1.noalias() = mat * tv2;
314 for (Index i = 0; i <= it; ++i) {
315 coef = tv1.dot(m_V.col(i));
316 tv1 = tv1 - coef * m_V.col(i);
325 const bool happy_breakdown = numext::is_exactly_zero(coef);
326 if (!happy_breakdown) {
327 m_V.col(it + 1) = tv1 / coef;
329 m_H(it + 1, it) = coef;
332 for (Index i = 1; i <= it; ++i) {
333 m_H.col(it).applyOnTheLeft(i - 1, i, gr[i - 1].adjoint());
339 if (happy_breakdown && numext::is_exactly_zero(m_H(it, it))) {
345 gr[it].makeGivens(m_H(it, it), m_H(it + 1, it));
347 m_H.col(it).applyOnTheLeft(it, it + 1, gr[it].adjoint());
348 g.applyOnTheLeft(it, it + 1, gr[it].adjoint());
350 beta = numext::abs(g(it + 1));
351 m_error = beta / normRhs;
355 if (m_error < m_tolerance || happy_breakdown) {
364 DenseVector nrs = m_H.topLeftCorner(it, it).template triangularView<Upper>().solve(g.head(it));
367 if (m_isDeflInitialized) {
368 tv1.noalias() = m_V.leftCols(it) * nrs;
369 dgmresApplyDeflation(tv1, tv2);
370 x = x + precond.solve(tv2);
372 x = x + precond.solve(m_V.leftCols(it) * nrs);
375 if (nbIts < m_iterations && m_info == NoConvergence && m_neig > 0 && (m_r + m_neig) < m_maxNeig)
376 dgmresComputeDeflationData(mat, precond, it, m_neig);
380template <
typename MatrixType_,
typename Preconditioner_>
381void DGMRES<MatrixType_, Preconditioner_>::dgmresInitDeflation(Index& rows)
const {
382 m_U.resize(rows, m_maxNeig);
383 m_MU.resize(rows, m_maxNeig);
384 m_T.resize(m_maxNeig, m_maxNeig);
386 m_isDeflAllocated =
true;
389template <
typename MatrixType_,
typename Preconditioner_>
390inline typename DGMRES<MatrixType_, Preconditioner_>::ComplexVector DGMRES<MatrixType_, Preconditioner_>::schurValues(
391 const ComplexSchur<DenseMatrix>& schurofH)
const {
392 return schurofH.matrixT().diagonal();
395template <
typename MatrixType_,
typename Preconditioner_>
396inline typename DGMRES<MatrixType_, Preconditioner_>::ComplexVector DGMRES<MatrixType_, Preconditioner_>::schurValues(
398 const DenseMatrix& T = schurofH.matrixT();
400 ComplexVector eig(it);
403 if (T(j + 1, j) == Scalar(0)) {
404 eig(j) = ComplexScalar(T(j, j), RealScalar(0));
407 eig(j) = ComplexScalar(T(j, j), T(j + 1, j));
408 eig(j + 1) = ComplexScalar(T(j, j + 1), T(j + 1, j + 1));
412 if (j < it - 1) eig(j) = ComplexScalar(T(j, j), RealScalar(0));
416template <
typename MatrixType_,
typename Preconditioner_>
417Index DGMRES<MatrixType_, Preconditioner_>::dgmresComputeDeflationData(
const MatrixType& mat,
418 const Preconditioner& precond,
const Index& it,
419 StorageIndex& neig)
const {
422 bool computeU =
true;
423 DenseMatrix matrixQ(it, it);
424 matrixQ.setIdentity();
425 schurofH.computeFromHessenberg(m_Hes.topLeftCorner(it, it), matrixQ, computeU);
427 ComplexVector eig = this->schurValues(schurofH);
431 DenseRealVector modulEig(it);
432 for (Index j = 0; j < it; ++j) modulEig(j) = numext::abs(eig(j));
433 perm.setLinSpaced(it, 0, internal::convert_index<StorageIndex>(it - 1));
434 internal::sortWithPermutation(modulEig, perm, neig);
437 m_lambdaN = (std::max)(modulEig.maxCoeff(), m_lambdaN);
441 while (nbrEig < neig) {
442 if (eig(perm(it - nbrEig - 1)).imag() == RealScalar(0))
448 DenseMatrix Sr(it, nbrEig);
450 for (Index j = 0; j < nbrEig; j++) {
451 Sr.col(j) = schurofH.matrixU().col(perm(it - j - 1));
455 DenseMatrix X = m_V.leftCols(it) * Sr;
458 for (Index j = 0; j < nbrEig; j++)
459 for (Index k = 0; k < m_r; k++) X.col(j) = X.col(j) - (m_U.col(k).dot(X.col(j))) * m_U.col(k);
463 Index m = m_V.rows();
464 if (!m_isDeflAllocated) dgmresInitDeflation(m);
465 DenseMatrix MX(m, nbrEig);
467 for (Index j = 0; j < nbrEig; j++) {
468 tv1.noalias() = mat * X.col(j);
469 MX.col(j) = precond.solve(tv1);
473 m_T.block(m_r, m_r, nbrEig, nbrEig).noalias() = X.transpose() * MX;
475 m_T.block(0, m_r, m_r, nbrEig).noalias() = m_U.leftCols(m_r).transpose() * MX;
476 m_T.block(m_r, 0, nbrEig, m_r).noalias() = X.transpose() * m_MU.leftCols(m_r);
480 for (Index j = 0; j < nbrEig; j++) m_U.col(m_r + j) = X.col(j);
481 for (Index j = 0; j < nbrEig; j++) m_MU.col(m_r + j) = MX.col(j);
486 m_luT.compute(m_T.topLeftCorner(m_r, m_r));
489 m_isDeflInitialized =
true;
492template <
typename MatrixType_,
typename Preconditioner_>
493template <
typename RhsType,
typename DestType>
494Index DGMRES<MatrixType_, Preconditioner_>::dgmresApplyDeflation(
const RhsType& x, DestType& y)
const {
495 DenseVector x1 = m_U.leftCols(m_r).transpose() * x;
496 y = x + m_U.leftCols(m_r) * (m_lambdaN * m_luT.solve(x1) - x1);
Performs a complex Schur decomposition of a real or complex square matrix.
Definition ComplexSchur.h:60
A Restarted GMRES with deflation. This class implements a modification of the GMRES solver for sparse...
Definition DGMRES.h:98
Index dgmresCycle(const MatrixType &mat, const Preconditioner &precond, Dest &x, DenseVector &r0, RealScalar &beta, const RealScalar &normRhs, Index &nbIts) const
Perform one restart cycle of DGMRES.
Definition DGMRES.h:289
DGMRES(const EigenBase< MatrixDerived > &A)
Definition DGMRES.h:138
void setMaxEigenv(const Index maxNeig)
Definition DGMRES.h:185
DGMRES()
Definition DGMRES.h:124
Index restart() const
Definition DGMRES.h:162
void dgmres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond) const
Perform several cycles of restarted GMRES with modified Gram Schmidt,.
Definition DGMRES.h:232
void set_restart(const Index restart)
Definition DGMRES.h:167
void setEigenv(const Index neig)
Definition DGMRES.h:172
Index deflSize() const
Definition DGMRES.h:180
IterativeSolverBase()
Definition IterativeSolverBase.h:136
Index maxIterations() const
Definition IterativeSolverBase.h:245
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
LU decomposition of a matrix with partial pivoting, and related features.
Definition PartialPivLU.h:67
Derived & setZero(Index size)
Definition CwiseNullaryOp.h:536
Performs a real Schur decomposition of a square matrix.
Definition RealSchur.h:62
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34