22#include <Eigen/Sparse>
34template <
typename Scalar>
68 template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
70 Eigen::Index& iters,
typename Dest::RealScalar& tol_error,
74 typedef typename Dest::RealScalar RealScalar;
75 typedef typename Dest::Scalar Scalar;
76 typedef Matrix<Scalar, Dynamic, 1> VectorType;
78 RealScalar tol = tol_error;
79 Index maxIters = iters;
83 VectorType residual = rhs - mat * x;
85 RealScalar rhsNorm2 = rhs.norm();
86 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
88 if (rhsNorm2 <= considerAsZero) {
94 RealScalar threshold = numext::maxi(tol * tol * rhsNorm2 * rhsNorm2, considerAsZero);
95 RealScalar residualNorm2 = residual.norm();
96 if (residualNorm2 * residualNorm2 < threshold) {
98 tol_error = (residualNorm2 / rhsNorm2);
103 double e_Pe_new = 0.0;
104 double e_Pe = x.squaredNorm();
108 p = precond.solve(residual);
111 VectorType z(n), tmp(n);
112 RealScalar absNew = numext::real(residual.dot(p));
115 while (i < maxIters) {
116 tmp.noalias() = mat * p;
118 Scalar alpha = absNew / d_Hd;
120 e_Pe_new = e_Pe + 2.0 * alpha * e_Pd + alpha * alpha * d_Pd;
122 if (d_Hd <= 0 || e_Pe_new >= _info.
Delta * _info.
Delta)
124 double tau = (-e_Pd + sqrt(e_Pd * e_Pd + d_Pd * (_info.
Delta * _info.
Delta - e_Pe))) / d_Pd;
136 residual -= alpha * tmp;
138 residualNorm2 = residual.norm();
141 residualNorm2 <= rhsNorm2 * std::min(rhsNorm2, _info.
kappa))
144 if (_info.
kappa < rhsNorm2)
150 if (residualNorm2 < threshold)
153 z = precond.solve(residual);
155 RealScalar absOld = absNew;
156 absNew = numext::real(residual.dot(z));
157 RealScalar beta = absNew / absOld;
159 e_Pd = beta * (e_Pd + alpha * d_Pd);
160 d_Pd = absNew + beta * beta * d_Pd;
165 tol_error = (residualNorm2 / rhsNorm2);
172template <
typename MatrixType,
int UpLo = Lower,
173 typename Preconditioner = DiagonalPreconditioner<typename MatrixType::Scalar> >
174class TruncatedConjugateGradient;
178 template <
typename MatrixType_,
int UpLo,
typename Preconditioner_>
194template <
typename M,
int upLo,
typename PC>
198 typedef IterativeSolverBase<TruncatedConjugateGradient>
Base;
200 :
Base(std::move(other)),
201 algInfo_{other.algInfo_} {}
206 using Base::m_isInitialized;
207 using Base::m_iterations;
209 mutable TCGInfo<typename M::RealScalar> algInfo_;
213 using Scalar =
typename MatrixType::Scalar;
248 template <
typename MatrixDerived>
250 :
Base(A.derived()) {}
255 template <
typename Rhs,
typename Dest>
257 typedef typename Base::MatrixWrapper MatrixWrapper;
258 typedef typename Base::ActualMatrixType ActualMatrixType;
261 TransposeInput = (!MatrixWrapper::MatrixFree) && (
UpLo == (Lower | Upper)) && (!MatrixType::IsRowMajor) &&
262 (!NumTraits<Scalar>::IsComplex)
264 typedef std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>,
const ActualMatrixType&>
266 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree,
UpLo == (Lower | Upper)),
267 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
268 typedef std::conditional_t<
UpLo == (Lower | Upper), RowMajorWrapper,
269 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>
272 m_iterations = Base::maxIterations();
273 m_error = Base::m_tolerance;
275 RowMajorWrapper row_mat(matrix());
278 m_info = m_error <= Base::m_tolerance ? Success : NoConvergence;
Definition: truncatedconjugategradient.hh:24
TCGStopReason
Definition: truncatedconjugategradient.hh:26
@ 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:69
Definition: truncatedconjugategradient.hh:36
Scalar kappa
Definition: truncatedconjugategradient.hh:39
Eigen::Index mininner
Definition: truncatedconjugategradient.hh:41
Eigen::Index maxinner
Definition: truncatedconjugategradient.hh:42
Scalar Delta
Definition: truncatedconjugategradient.hh:38
void initRuntimeOptions(int _num_dof_solve)
Definition: truncatedconjugategradient.hh:45
TCGStopReason stop_tCG
Definition: truncatedconjugategradient.hh:37
Scalar theta
Definition: truncatedconjugategradient.hh:40
Eigen::Index numInnerIter
Definition: truncatedconjugategradient.hh:43
Iterative solver for solving linear systems using the truncated conjugate gradient method.
Definition: truncatedconjugategradient.hh:196
IterativeSolverBase< TruncatedConjugateGradient > Base
Definition: truncatedconjugategradient.hh:198
@ UpLo
Definition: truncatedconjugategradient.hh:219
TruncatedConjugateGradient(TruncatedConjugateGradient &&other) noexcept
Definition: truncatedconjugategradient.hh:199
TruncatedConjugateGradient()
Definition: truncatedconjugategradient.hh:235
typename MatrixType::RealScalar RealScalar
Definition: truncatedconjugategradient.hh:214
PC Preconditioner
Definition: truncatedconjugategradient.hh:215
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition: truncatedconjugategradient.hh:256
TCGInfo< typename MatrixType::RealScalar > getInfo()
Get information about the truncated conjugate gradient algorithm.
Definition: truncatedconjugategradient.hh:227
typename MatrixType::Scalar Scalar
Definition: truncatedconjugategradient.hh:213
~TruncatedConjugateGradient()
Definition: truncatedconjugategradient.hh:252
void setInfo(TCGInfo< typename MatrixType::RealScalar > alginfo)
Set information about the truncated conjugate gradient algorithm.
Definition: truncatedconjugategradient.hh:233
M MatrixType
Definition: truncatedconjugategradient.hh:212
TruncatedConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition: truncatedconjugategradient.hh:249
Preconditioner_ Preconditioner
Definition: truncatedconjugategradient.hh:182
MatrixType_ MatrixType
Definition: truncatedconjugategradient.hh:181
Definition: concepts.hh:30