22#include <Eigen/Sparse>
33 template <
typename Scalar>
66 template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
68 Eigen::Index& iters,
typename Dest::RealScalar& tol_error,
72 typedef typename Dest::RealScalar RealScalar;
73 typedef typename Dest::Scalar Scalar;
74 typedef Matrix<Scalar, Dynamic, 1> VectorType;
76 RealScalar tol = tol_error;
77 Index maxIters = iters;
81 VectorType residual = rhs - mat * x;
83 RealScalar rhsNorm2 = rhs.norm();
84 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
86 if (rhsNorm2 <= considerAsZero) {
92 RealScalar threshold = numext::maxi(tol * tol * rhsNorm2 * rhsNorm2, considerAsZero);
93 RealScalar residualNorm2 = residual.norm();
94 if (residualNorm2 * residualNorm2 < threshold) {
96 tol_error = (residualNorm2 / rhsNorm2);
101 double e_Pe_new = 0.0;
102 double e_Pe = x.squaredNorm();
106 p = precond.solve(residual);
109 VectorType z(n), tmp(n);
110 RealScalar absNew = numext::real(residual.dot(p));
113 while (i < maxIters) {
114 tmp.noalias() = mat * p;
116 Scalar alpha = absNew / d_Hd;
118 e_Pe_new = e_Pe + 2.0 * alpha * e_Pd + alpha * alpha * d_Pd;
120 if (d_Hd <= 0 || e_Pe_new >= _info.
Delta * _info.
Delta)
122 double tau = (-e_Pd + sqrt(e_Pd * e_Pd + d_Pd * (_info.
Delta * _info.
Delta - e_Pe))) / d_Pd;
134 residual -= alpha * tmp;
136 residualNorm2 = residual.norm();
139 && residualNorm2 <= rhsNorm2 * std::min(rhsNorm2, _info.
kappa))
142 if (_info.
kappa < rhsNorm2)
148 if (residualNorm2 < threshold)
break;
150 z = precond.solve(residual);
152 RealScalar absOld = absNew;
153 absNew = numext::real(residual.dot(z));
154 RealScalar beta = absNew / absOld;
156 e_Pd = beta * (e_Pd + alpha * d_Pd);
157 d_Pd = absNew + beta * beta * d_Pd;
162 tol_error = (residualNorm2 / rhsNorm2);
169 template <
typename MatrixType,
int UpLo = Lower,
170 typename Preconditioner = DiagonalPreconditioner<typename MatrixType::Scalar> >
171 class TruncatedConjugateGradient;
175 template <
typename MatrixType_,
int UpLo,
typename Preconditioner_>
190 template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
192 :
public IterativeSolverBase<TruncatedConjugateGradient<MatrixType_, UpLo_, Preconditioner_> > {
194 typedef IterativeSolverBase<TruncatedConjugateGradient>
Base;
196 :
Base(std::move(other)), algInfo{other.algInfo} {}
201 using Base::m_isInitialized;
202 using Base::m_iterations;
204 mutable TCGInfo<typename MatrixType_::RealScalar> algInfo;
208 typedef typename MatrixType::Scalar
Scalar;
239 template <
typename MatrixDerived>
245 template <
typename Rhs,
typename Dest>
247 typedef typename Base::MatrixWrapper MatrixWrapper;
248 typedef typename Base::ActualMatrixType ActualMatrixType;
250 TransposeInput = (!MatrixWrapper::MatrixFree) && (
UpLo == (Lower | Upper)) && (!MatrixType::IsRowMajor)
251 && (!NumTraits<Scalar>::IsComplex)
253 typedef std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType
const&>
255 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree,
UpLo == (Lower | Upper)),
256 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
257 typedef std::conditional_t<
UpLo == (Lower | Upper), RowMajorWrapper,
258 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>
261 m_iterations = Base::maxIterations();
262 m_error = Base::m_tolerance;
264 RowMajorWrapper row_mat(matrix());
267 m_info = m_error <= Base::m_tolerance ? Success : NoConvergence;
Definition: truncatedconjugategradient.hh:24
TCGStopReason
Definition: truncatedconjugategradient.hh:25
@ reachedTargetResidualThetaSuperLinear
@ reachedTargetResidualKappaLinear
void truncated_conjugate_gradient(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Eigen::Index &iters, typename Dest::RealScalar &tol_error, TCGInfo< typename Dest::RealScalar > &_info)
Definition: truncatedconjugategradient.hh:67
Definition: truncatedconjugategradient.hh:34
Scalar kappa
Definition: truncatedconjugategradient.hh:37
Eigen::Index mininner
Definition: truncatedconjugategradient.hh:39
Eigen::Index maxinner
Definition: truncatedconjugategradient.hh:40
Scalar Delta
Definition: truncatedconjugategradient.hh:36
void initRuntimeOptions(int _num_dof_solve)
Definition: truncatedconjugategradient.hh:43
TCGStopReason stop_tCG
Definition: truncatedconjugategradient.hh:35
Scalar theta
Definition: truncatedconjugategradient.hh:38
Eigen::Index numInnerIter
Definition: truncatedconjugategradient.hh:41
Iterative solver for solving linear systems using the truncated conjugate gradient method.
Definition: truncatedconjugategradient.hh:192
TruncatedConjugateGradient()
Definition: truncatedconjugategradient.hh:227
@ UpLo
Definition: truncatedconjugategradient.hh:212
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition: truncatedconjugategradient.hh:246
TruncatedConjugateGradient(TruncatedConjugateGradient &&other) noexcept
Definition: truncatedconjugategradient.hh:195
TCGInfo< typename MatrixType::RealScalar > getInfo()
Get information about the truncated conjugate gradient algorithm.
Definition: truncatedconjugategradient.hh:219
Preconditioner_ Preconditioner
Definition: truncatedconjugategradient.hh:210
TruncatedConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition: truncatedconjugategradient.hh:240
void setInfo(TCGInfo< typename MatrixType::RealScalar > _alginfo)
Set information about the truncated conjugate gradient algorithm.
Definition: truncatedconjugategradient.hh:225
MatrixType::Scalar Scalar
Definition: truncatedconjugategradient.hh:208
MatrixType::RealScalar RealScalar
Definition: truncatedconjugategradient.hh:209
MatrixType_ MatrixType
Definition: truncatedconjugategradient.hh:207
~TruncatedConjugateGradient()
Definition: truncatedconjugategradient.hh:242
IterativeSolverBase< TruncatedConjugateGradient > Base
Definition: truncatedconjugategradient.hh:194
Preconditioner_ Preconditioner
Definition: truncatedconjugategradient.hh:178
MatrixType_ MatrixType
Definition: truncatedconjugategradient.hh:177
Definition: concepts.hh:25