Loading...
Searching...
No Matches
pcg.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "identity_solver.hpp"
8#include "util/table.hpp"
9
10namespace terra::linalg::solvers {
11
12/// @brief Preconditioned Conjugate Gradient (PCG) iterative solver for symmetric positive definite linear systems.
13///
14/// See, e.g.,
15/// @code
16/// Elman, H. C., Silvester, D. J., & Wathen, A. J. (2014).
17/// Finite elements and fast iterative solvers: with applications in incompressible fluid dynamics.
18/// Oxford university press.
19/// @endcode
20///
21/// Satisfies the SolverLike concept (see solver.hpp).
22/// Supports optional preconditioning.
23/// @tparam OperatorT Operator type (must satisfy OperatorLike).
24/// @tparam PreconditionerT Preconditioner type (must satisfy SolverLike, defaults to IdentitySolver).
25template < OperatorLike OperatorT, SolverLike PreconditionerT = IdentitySolver< OperatorT > >
26class PCG
27{
28 public:
29 /// @brief Operator type to be solved.
30 using OperatorType = OperatorT;
31 /// @brief Solution vector type.
33 /// @brief Right-hand side vector type.
35 /// @brief Scalar type for computations.
36 using ScalarType = typename SolutionVectorType::ScalarType;
37
38 /// @brief Construct a PCG solver with default identity preconditioner.
39 /// @param params Iterative solver parameters.
40 /// @param statistics Shared pointer to statistics table.
41 /// @param tmps Temporary vectors for workspace. (At least 4 vectors are required.)
43 const std::shared_ptr< util::Table >& statistics,
44 const std::vector< SolutionVectorType >& tmps )
45 : PCG( params, statistics, tmps, IdentitySolver< OperatorT >() )
46 {}
47
48 /// @brief Construct a PCG solver with a custom preconditioner.
49 /// @param params Iterative solver parameters.
50 /// @param statistics Shared pointer to statistics table.
51 /// @param tmps Temporary vectors for workspace. (At least 4 vectors are required.)
52 /// @param preconditioner Preconditioner solver.
54 const std::shared_ptr< util::Table >& statistics,
55 const std::vector< SolutionVectorType >& tmps,
56 const PreconditionerT preconditioner )
57 : tag_( "pcg_solver" )
58 , params_( params )
59 , statistics_( statistics )
60 , tmps_( tmps )
61 , preconditioner_( preconditioner )
62 {
63 if ( tmps.size() < 4 )
64 {
65 throw std::runtime_error( "PCG: tmps.size() < 4. Need at least 4 tmp vectors." );
66 }
67 }
68
69 /// @brief Set a tag string for statistics output.
70 /// @param tag Tag string.
71 void set_tag( const std::string& tag ) { tag_ = tag; }
72
73 /// @brief Solve the linear system \f$ Ax = b \f$ using PCG.
74 /// Calls the iterative solver and updates statistics.
75 /// @param A Operator (matrix).
76 /// @param x Solution vector (output).
77 /// @param b Right-hand side vector (input).
79 {
80 auto& r_ = tmps_[0]; ///< Residual vector.
81 auto& p_ = tmps_[1]; ///< Search direction vector.
82 auto& ap_ = tmps_[2]; ///< Temporary vector for A*p.
83 auto& z_ = tmps_[3]; ///< Preconditioned residual vector.
84
85 apply( A, x, r_ );
86
87 lincomb( r_, { 1.0, -1.0 }, { b, r_ } );
88
89 solve( preconditioner_, A, z_, r_ );
90
91 assign( p_, z_ );
92
93 // TODO: should this be dot(z, z) instead or dot(r, r)?
94 const ScalarType initial_residual = std::sqrt( dot( r_, r_ ) );
95
96 if ( statistics_ )
97 {
98 statistics_->add_row(
99 { { "tag", tag_ },
100 { "iteration", 0 },
101 { "relative_residual", 1.0 },
102 { "absolute_residual", initial_residual } } );
103 }
104
105 if ( initial_residual < params_.absolute_residual_tolerance() )
106 {
107 return;
108 }
109
110 for ( int iteration = 1; iteration <= params_.max_iterations(); ++iteration )
111 {
112 const ScalarType alpha_num = dot( z_, r_ );
113
114 apply( A, p_, ap_ );
115 const ScalarType alpha_den = dot( ap_, p_ );
116
117 const ScalarType alpha = alpha_num / alpha_den;
118
119 lincomb( x, { 1.0, alpha }, { x, p_ } );
120 lincomb( r_, { 1.0, -alpha }, { r_, ap_ } );
121
122 // TODO: is this the correct term for the residual check?
123 const ScalarType absolute_residual = std::sqrt( dot( r_, r_ ) );
124
125 const ScalarType relative_residual = absolute_residual / initial_residual;
126
127 if ( statistics_ )
128 {
129 statistics_->add_row(
130 { { "tag", tag_ },
131 { "iteration", iteration },
132 { "relative_residual", relative_residual },
133 { "absolute_residual", absolute_residual } } );
134 }
135
136 if ( relative_residual <= params_.relative_residual_tolerance() )
137 {
138 return;
139 }
140
141 if ( absolute_residual < params_.absolute_residual_tolerance() )
142 {
143 return;
144 }
145
146 solve( preconditioner_, A, z_, r_ );
147
148 const ScalarType beta_num = dot( z_, r_ );
149 const ScalarType beta = beta_num / alpha_num;
150
151 lincomb( p_, { 1.0, beta }, { z_, p_ } );
152 }
153 }
154
155 private:
156 std::string tag_; ///< Tag for statistics output.
157
158 IterativeSolverParameters params_; ///< Solver parameters.
159
160 std::shared_ptr< util::Table > statistics_; ///< Statistics table.
161
162 std::vector< SolutionVectorType > tmps_; ///< Temporary workspace vectors.
163
164 PreconditionerT preconditioner_; ///< Preconditioner solver.
165};
166
167/// @brief Static assertion: PCG satisfies SolverLike concept.
168static_assert(
169 SolverLike<
170 PCG< linalg::detail::
171 DummyOperator< linalg::detail::DummyVector< double >, linalg::detail::DummyVector< double > > > > );
172
173} // namespace terra::linalg::solvers
Dummy vector class for concept checks and testing. Implements required vector operations as no-ops.
Definition vector.hpp:210
"Identity solver" for linear systems.
Definition identity_solver.hpp:21
Definition iterative_solver_info.hpp:7
double absolute_residual_tolerance() const
Definition iterative_solver_info.hpp:20
int max_iterations() const
Definition iterative_solver_info.hpp:18
double relative_residual_tolerance() const
Definition iterative_solver_info.hpp:19
Preconditioned Conjugate Gradient (PCG) iterative solver for symmetric positive definite linear syste...
Definition pcg.hpp:27
DstOf< OperatorType > RHSVectorType
Right-hand side vector type.
Definition pcg.hpp:34
SrcOf< OperatorType > SolutionVectorType
Solution vector type.
Definition pcg.hpp:32
void solve_impl(OperatorType &A, SolutionVectorType &x, const RHSVectorType &b)
Solve the linear system using PCG. Calls the iterative solver and updates statistics.
Definition pcg.hpp:78
void set_tag(const std::string &tag)
Set a tag string for statistics output.
Definition pcg.hpp:71
typename SolutionVectorType::ScalarType ScalarType
Scalar type for computations.
Definition pcg.hpp:36
PCG(const IterativeSolverParameters &params, const std::shared_ptr< util::Table > &statistics, const std::vector< SolutionVectorType > &tmps, const PreconditionerT preconditioner)
Construct a PCG solver with a custom preconditioner.
Definition pcg.hpp:53
OperatorT OperatorType
Operator type to be solved.
Definition pcg.hpp:30
PCG(const IterativeSolverParameters &params, const std::shared_ptr< util::Table > &statistics, const std::vector< SolutionVectorType > &tmps)
Construct a PCG solver with default identity preconditioner.
Definition pcg.hpp:42
Definition block_preconditioner_2x2.hpp:7
void solve(Solver &solver, Operator &A, SolutionVector &x, const RHSVector &b)
Solve a linear system using the given solver and operator. Calls the solver's solve_impl method.
Definition solver.hpp:51
void lincomb(Vector &y, const std::vector< ScalarOf< Vector > > &c, const std::vector< Vector > &x, const ScalarOf< Vector > &c0)
Compute a linear combination of vectors. Implements: .
Definition vector.hpp:72
Operator::SrcVectorType SrcOf
Alias for the source vector type of an operator.
Definition operator.hpp:145
ScalarOf< Vector > dot(const Vector &y, const Vector &x)
Compute the dot product of two vectors. Implements: .
Definition vector.hpp:118
void apply(LinearForm &L, typename LinearForm::DstVectorType &dst)
Apply a linear form and write to a destination vector.
Definition linear_form.hpp:37
Operator::DstVectorType DstOf
Alias for the destination vector type of an operator.
Definition operator.hpp:149
void assign(Vector &y, const ScalarOf< Vector > &c0)
Assign a scalar value to a vector. Implements: .
Definition vector.hpp:97