G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsConjugateGradient.h
Go to the documentation of this file.
1 
14 #pragma once
15 
17 
18 namespace gismo
19 {
20 
28 template<class T = real_t>
30 {
31 public:
32  typedef gsIterativeSolver<T> Base;
33 
34  typedef gsMatrix<T> VectorType;
35 
36  typedef typename Base::LinOpPtr LinOpPtr;
37 
38  typedef memory::shared_ptr<gsConjugateGradient> Ptr;
39  typedef memory::unique_ptr<gsConjugateGradient> uPtr;
40 
45  template< typename OperatorType >
46  explicit gsConjugateGradient( const OperatorType& mat,
47  const LinOpPtr& precond = LinOpPtr() )
48  : Base(mat, precond), m_calcEigenvals(false) {}
49 
54  template< typename OperatorType >
55  static uPtr make( const OperatorType& mat, const LinOpPtr& precond = LinOpPtr() )
56  { return uPtr( new gsConjugateGradient(mat, precond) ); }
57 
60  {
62  opt.addSwitch("CalcEigenvalues", "Additionally to solving the system,"
63  " CG computes the eigenvalues of the Lanczos matrix", false );
64  return opt;
65  }
66 
69  {
70  Base::setOptions(opt);
71  m_calcEigenvals = opt.askSwitch("CalcEigenvalues", m_calcEigenvals);
72  return *this;
73  }
74 
75  bool initIteration( const VectorType& rhs, VectorType& x );
76  bool step( VectorType& x );
77 
80  void setCalcEigenvalues( bool flag ) { m_calcEigenvals = flag ;}
81 
84 
86  void getEigenvalues( VectorType& eigs );
87 
89  std::ostream &print(std::ostream &os) const
90  {
91  os << "gsConjugateGradient\n";
92  return os;
93  }
94 
95 private:
96  using Base::m_mat;
97  using Base::m_precond;
98  using Base::m_max_iters;
99  using Base::m_tol;
100  using Base::m_num_iter;
101  using Base::m_rhs_norm;
102  using Base::m_error;
103 
104 
105  VectorType m_res;
106  VectorType m_update;
107  VectorType m_tmp;
108  T m_abs_new;
109 
110  bool m_calcEigenvals;
111 
112  std::vector<T> m_delta, m_gamma;
113 };
114 
115 } // namespace gismo
116 
117 #ifndef GISMO_BUILD_LIB
118 #include GISMO_HPP_HEADER(gsConjugateGradient.hpp)
119 #endif
const LinOpPtr m_mat
The matrix/operator to be solved for.
Definition: gsIterativeSolver.h:252
virtual gsIterativeSolver & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition: gsIterativeSolver.h:102
bool step(VectorType &x)
Perform one step, requires initIteration.
Definition: gsConjugateGradient.hpp:55
gsOptionList::Real m_tol
The tolerance for m_error to be reached.
Definition: gsIterativeSolver.h:255
The conjugate gradient method.
Definition: gsConjugateGradient.h:29
T getConditionNumber()
returns the condition number of the (preconditioned) system matrix
Definition: gsConjugateGradient.hpp:87
static uPtr make(const OperatorType &mat, const LinOpPtr &precond=LinOpPtr())
Make function using a matrix (operator) and optionally a preconditionner.
Definition: gsConjugateGradient.h:55
T m_rhs_norm
The norm of the right-hand-side.
Definition: gsIterativeSolver.h:257
bool initIteration(const VectorType &rhs, VectorType &x)
Init the iteration.
Definition: gsConjugateGradient.hpp:20
void getEigenvalues(VectorType &eigs)
returns the eigenvalues of the Lanczos matrix
Definition: gsConjugateGradient.hpp:116
static gsOptionList defaultOptions()
Returns a list of default options.
Definition: gsIterativeSolver.h:92
index_t m_max_iters
The upper bound for the number of iterations.
Definition: gsIterativeSolver.h:254
gsConjugateGradient(const OperatorType &mat, const LinOpPtr &precond=LinOpPtr())
Constructor using a matrix (operator) and optionally a preconditionner.
Definition: gsConjugateGradient.h:46
std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition: gsConjugateGradient.h:89
T m_error
The relative error as absolute_error/m_rhs_norm.
Definition: gsIterativeSolver.h:258
Abstract class for iterative solvers.
bool askSwitch(const std::string &label, const bool &value=false) const
Reads value for option label from options.
Definition: gsOptionList.cpp:128
Abstract class for iterative solvers.
Definition: gsIterativeSolver.h:26
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
void addSwitch(const std::string &label, const std::string &desc, const bool &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:235
void setCalcEigenvalues(bool flag)
specify if you want to store data for eigenvalue estimation
Definition: gsConjugateGradient.h:80
gsConjugateGradient & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition: gsConjugateGradient.h:68
static gsOptionList defaultOptions()
Returns a list of default options.
Definition: gsConjugateGradient.h:59
index_t m_num_iter
The number of iterations performed.
Definition: gsIterativeSolver.h:256
LinOpPtr m_precond
The preconditioner.
Definition: gsIterativeSolver.h:253