Solves a symmetric system of linear equations with conjugate gradients (CG). More...

#include <cg.hh>

Inheritance diagram for concepts::CG< F >:
concepts::VecOperator< F > concepts::Operator< F >

Public Types

typedef Cmplxtype< F >::type c_type
 Real type of data type. More...
 
typedef Realtype< F >::type r_type
 Real type of data type. More...
 
typedef F type
 Type of data, e.g. matrix entries. More...
 

Public Member Functions

 CG (Operator< F > &A, Operator< F > &Minv, Real maxeps, int maxit=0, bool relres=0, bool throwing=true)
 Constructor. More...
 
 CG (Operator< F > &A, Real maxeps, int maxit=0, uint relres=false, bool throwing=true)
 Constructor. More...
 
virtual const uint dimX () const
 Returns the size of the image space of the operator (number of rows of the corresponding matrix) More...
 
virtual const uint dimY () const
 Returns the size of the source space of the operator (number of columns of the corresponding matrix) More...
 
Real epsilon () const
 Returns the residual. More...
 
uint iterations () const
 Returns the number of iterations. More...
 
void operator() ()
 Application method without second argument. Used for parallel solvers. More...
 
virtual void operator() (const Function< c_type > &fncY, Function< c_type > &fncX)
 Application operator for complex function fncY. More...
 
virtual void operator() (const Function< r_type > &fncY, Function< F > &fncX)
 Application operator for real function fncY. More...
 
void operator() (const Matrix< c_type > &mX, Matrix< c_type > &mY)
 Application method to complex matrices. Calls apply_() More...
 
void operator() (const Matrix< r_type > &mX, Matrix< F > &mY)
 Application method to real matrices. Calls function apply() More...
 
virtual void operator() (const Vector< c_type > &fncY, Vector< c_type > &fncX)
 Application operator for complex function fncY. More...
 
virtual void operator() (const Vector< r_type > &fncY, Vector< F > &fncX)
 Application operator for real vector fncY. More...
 
virtual void show_messages ()
 

Protected Member Functions

std::ostream & info (std::ostream &os) const
 

Protected Attributes

uint dimX_
 Dimension of image space and the source space. More...
 
uint dimY_
 

Private Member Functions

virtual void apply_ ()
 Intrinsic application method without argument. More...
 
virtual void apply_ (const Vector< F > &fncY, Vector< F > &fncX)
 Intrinsic application method, i.e. More...
 
Vector< Realcreate_index_vector ()
 function to generate vector of indices of non-zero rows of A_ More...
 

Private Attributes

Operator< F > & A_
 Operator which is solved. More...
 
Real eps_
 Current residual. More...
 
uint it_
 Number of iterations. More...
 
Vector< RealJ_
 Vector containing indices of non-zero rows of A_. More...
 
Real maxeps_
 Convergence criterion. More...
 
uint maxit_
 Maximal number of iterations until abortion. More...
 
int N_
 number of threads More...
 
bool parallel_
 flag for parallel version More...
 
int rank_
 thread number More...
 
bool relres_
 false: absolute residual, true: relative residual More...
 
bool throwing_
 false: best solution is given, when non converging true: exception is thrown, when non converging More...
 
Operator< F > * W_
 Optional preconditioner. More...
 

Detailed Description

template<class F>
class concepts::CG< F >

Solves a symmetric system of linear equations with conjugate gradients (CG).

Constructing an object of this class does not solve the given system. Use the application operator to solve the system. If you want to specify a starting vector for the cg iterations, set fncX before calling the application operator to this starting value. fncX also holds the result after the solve.

The application operator throws NoConvergence if the desired residual maxeps is not reached within the given number of iterations maxit.

Test:
test::CgTest
Examples
howToGetStarted.cc, hpFEM2d-simple.cc, hpFEM2d.cc, inhomDirichletBCs.cc, inhomNeumannBCs.cc, linearFEM1d-simple.cc, linearFEM1d.cc, and RobinBCs.cc.

Definition at line 39 of file cg.hh.

Member Typedef Documentation

◆ c_type

template<class F >
typedef Cmplxtype<F>::type concepts::VecOperator< F >::c_type
inherited

Real type of data type.

Definition at line 120 of file compositions.hh.

◆ r_type

template<class F >
typedef Realtype<F>::type concepts::VecOperator< F >::r_type
inherited

Real type of data type.

Definition at line 118 of file compositions.hh.

◆ type

template<class F >
typedef F concepts::Operator< F >::type
inherited

Type of data, e.g. matrix entries.

Definition at line 45 of file compositions.hh.

Constructor & Destructor Documentation

◆ CG() [1/2]

template<class F >
concepts::CG< F >::CG ( Operator< F > &  A,
Real  maxeps,
int  maxit = 0,
uint  relres = false,
bool  throwing = true 
)
inline

Constructor.

Parameters
AMatrix
maxepsMaximal residual
maxitMaximal number of iterations
relresRelative residual
throwingIn the case of non convergence an exception is thrown and the best solution is not given back.

Definition at line 49 of file cg.hh.

◆ CG() [2/2]

template<class F >
concepts::CG< F >::CG ( Operator< F > &  A,
Operator< F > &  Minv,
Real  maxeps,
int  maxit = 0,
bool  relres = 0,
bool  throwing = true 
)
inline

Constructor.

Parameters
AMatrix
MinvPreconditioner for A, ie. Minv should approximate $ A^-1 $ and it has to be symmetric positive definite
maxepsMaximal residual
maxitMaximal number of iterations
relresRelative residual
throwingIn the case of non convergence an exception is thrown and the best solution is not given back.

Definition at line 87 of file cg.hh.

Member Function Documentation

◆ apply_() [1/2]

template<class F >
virtual void concepts::CG< F >::apply_ ( )
privatevirtual

Intrinsic application method without argument.

Implements concepts::VecOperator< F >.

◆ apply_() [2/2]

template<class F >
virtual void concepts::CG< F >::apply_ ( const Vector< F > &  fncY,
Vector< F > &  fncX 
)
privatevirtual

Intrinsic application method, i.e.

real Operator and real Vector or complex Operator and real Vector.

Implements concepts::VecOperator< F >.

◆ create_index_vector()

template<class F >
Vector<Real> concepts::CG< F >::create_index_vector ( )
private

function to generate vector of indices of non-zero rows of A_

◆ dimX()

template<class F >
virtual const uint concepts::Operator< F >::dimX ( ) const
inlinevirtualinherited

Returns the size of the image space of the operator (number of rows of the corresponding matrix)

Definition at line 93 of file compositions.hh.

◆ dimY()

template<class F >
virtual const uint concepts::Operator< F >::dimY ( ) const
inlinevirtualinherited

Returns the size of the source space of the operator (number of columns of the corresponding matrix)

Definition at line 98 of file compositions.hh.

◆ epsilon()

template<class F >
Real concepts::CG< F >::epsilon ( ) const
inline

Returns the residual.

Calling this method makes only sence after a linear system has been solved.

Definition at line 123 of file cg.hh.

◆ info()

template<class F >
std::ostream& concepts::CG< F >::info ( std::ostream &  os) const
protectedvirtual

Reimplemented from concepts::VecOperator< F >.

◆ iterations()

template<class F >
uint concepts::CG< F >::iterations ( ) const
inline

Returns the number of iterations.

Calling this method makes only sense after a linear system has been solved.

Definition at line 118 of file cg.hh.

◆ operator()() [1/7]

template<class F >
void concepts::VecOperator< F >::operator() ( )
virtualinherited

Application method without second argument. Used for parallel solvers.

Reimplemented from concepts::Operator< F >.

◆ operator()() [2/7]

template<class F >
virtual void concepts::VecOperator< F >::operator() ( const Function< c_type > &  fncY,
Function< c_type > &  fncX 
)
virtualinherited

Application operator for complex function fncY.

Computes fncX = A(fncY) where A is this operator. fncX becomes complex.

In derived classes its enough to implement the operator() for complex Operator's. If a real counterpart is not implemented, the function fncY is splitted into real and imaginary part and the application operator for real functions is called for each. Then the result is combined.

If in a derived class the operator() for complex Operator's is not implemented, a exception is thrown from here.

Reimplemented from concepts::Operator< F >.

◆ operator()() [3/7]

template<class F >
virtual void concepts::VecOperator< F >::operator() ( const Function< r_type > &  fncY,
Function< F > &  fncX 
)
virtualinherited

Application operator for real function fncY.

Computes fncX = A(fncY) where A is this operator.

fncX becomes the type of the operator, for real data it becomes real, for complex data it becomes complex.

In derived classes its enough to implement the operator() for real Operator's. If a complex counterpart is not implemented, the function fncY is transformed to a complex function and then the application operator for complex functions is called.

If in a derived class the operator() for real Operator's is not implemented, a exception is thrown from here.

Reimplemented from concepts::Operator< F >.

◆ operator()() [4/7]

template<class F >
void concepts::VecOperator< F >::operator() ( const Matrix< c_type > &  mX,
Matrix< c_type > &  mY 
)
inherited

Application method to complex matrices. Calls apply_()

◆ operator()() [5/7]

template<class F >
void concepts::VecOperator< F >::operator() ( const Matrix< r_type > &  mX,
Matrix< F > &  mY 
)
inherited

Application method to real matrices. Calls function apply()

◆ operator()() [6/7]

template<class F >
virtual void concepts::VecOperator< F >::operator() ( const Vector< c_type > &  fncY,
Vector< c_type > &  fncX 
)
virtualinherited

Application operator for complex function fncY.

Computes fncX = A(fncY) where A is this operator. fncX becomes complex.

In derived classes its enough to implement the operator() for complex Operator's. If a real counterpart is not implemented, the vector fncY is splitted into real and imaginary part and the application operator for real vectors is called for each. Then the result is combined

If in a derived class the operator() for complex Operator's i not implemented, a exception is thrown from here.

◆ operator()() [7/7]

template<class F >
virtual void concepts::VecOperator< F >::operator() ( const Vector< r_type > &  fncY,
Vector< F > &  fncX 
)
virtualinherited

Application operator for real vector fncY.

Computes fncX = A(fncY) where A is this operator.

Type of fncX becomes that of the operator, for real data it becomes real, for complex data it becomes complex.

In derived classes its enough to implement the operator() for real Operator's. If a complex counterpart is not implemented, the vector fncY is transformed to a complex vector and then the application for complex vectors is called.

If in a derived class the operator() for real Operator's is not implemented, a exception is thrown from here.

◆ show_messages()

template<class F >
virtual void concepts::Operator< F >::show_messages ( )
inlinevirtualinherited

Reimplemented in concepts::Newton< F >, concepts::MumpsOverlap< F >, and concepts::Mumps< F >.

Definition at line 100 of file compositions.hh.

Member Data Documentation

◆ A_

template<class F >
Operator<F>& concepts::CG< F >::A_
private

Operator which is solved.

Definition at line 131 of file cg.hh.

◆ dimX_

template<class F >
uint concepts::Operator< F >::dimX_
protectedinherited

Dimension of image space and the source space.

Definition at line 104 of file compositions.hh.

◆ dimY_

template<class F >
uint concepts::Operator< F >::dimY_
protectedinherited

Definition at line 104 of file compositions.hh.

◆ eps_

template<class F >
Real concepts::CG< F >::eps_
private

Current residual.

Definition at line 138 of file cg.hh.

◆ it_

template<class F >
uint concepts::CG< F >::it_
private

Number of iterations.

Definition at line 140 of file cg.hh.

◆ J_

template<class F >
Vector<Real> concepts::CG< F >::J_
private

Vector containing indices of non-zero rows of A_.

Definition at line 158 of file cg.hh.

◆ maxeps_

template<class F >
Real concepts::CG< F >::maxeps_
private

Convergence criterion.

Definition at line 134 of file cg.hh.

◆ maxit_

template<class F >
uint concepts::CG< F >::maxit_
private

Maximal number of iterations until abortion.

Definition at line 136 of file cg.hh.

◆ N_

template<class F >
int concepts::CG< F >::N_
private

number of threads

Definition at line 149 of file cg.hh.

◆ parallel_

template<class F >
bool concepts::CG< F >::parallel_
private

flag for parallel version

Definition at line 155 of file cg.hh.

◆ rank_

template<class F >
int concepts::CG< F >::rank_
private

thread number

Definition at line 152 of file cg.hh.

◆ relres_

template<class F >
bool concepts::CG< F >::relres_
private

false: absolute residual, true: relative residual

Definition at line 142 of file cg.hh.

◆ throwing_

template<class F >
bool concepts::CG< F >::throwing_
private

false: best solution is given, when non converging true: exception is thrown, when non converging

Definition at line 146 of file cg.hh.

◆ W_

template<class F >
Operator<F>* concepts::CG< F >::W_
private

Optional preconditioner.

Definition at line 129 of file cg.hh.


The documentation for this class was generated from the following file:
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich