G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsConjugateGradient.h
Go to the documentation of this file.
1
14#pragma once
15
17
18namespace gismo
19{
20
28template<class T = real_t>
30{
31public:
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 {
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
95private:
96 using Base::m_mat;
97 using Base::m_precond;
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
The conjugate gradient method.
Definition gsConjugateGradient.h:30
bool step(VectorType &x)
Perform one step, requires initIteration.
Definition gsConjugateGradient.hpp:55
void setCalcEigenvalues(bool flag)
specify if you want to store data for eigenvalue estimation
Definition gsConjugateGradient.h:80
bool initIteration(const VectorType &rhs, VectorType &x)
Init the iteration.
Definition gsConjugateGradient.hpp:20
gsConjugateGradient & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition gsConjugateGradient.h:68
static uPtr make(const OperatorType &mat, const LinOpPtr &precond=LinOpPtr())
Make function using a matrix (operator) and optionally a preconditionner.
Definition gsConjugateGradient.h:55
gsConjugateGradient(const OperatorType &mat, const LinOpPtr &precond=LinOpPtr())
Constructor using a matrix (operator) and optionally a preconditionner.
Definition gsConjugateGradient.h:46
T getConditionNumber()
returns the condition number of the (preconditioned) system matrix
Definition gsConjugateGradient.hpp:87
std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition gsConjugateGradient.h:89
static gsOptionList defaultOptions()
Returns a list of default options.
Definition gsConjugateGradient.h:59
void getEigenvalues(VectorType &eigs)
returns the eigenvalues of the Lanczos matrix
Definition gsConjugateGradient.hpp:116
Abstract class for iterative solvers.
Definition gsIterativeSolver.h:27
virtual gsIterativeSolver & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition gsIterativeSolver.h:102
T m_rhs_norm
The norm of the right-hand-side.
Definition gsIterativeSolver.h:257
T m_error
The relative error as absolute_error/m_rhs_norm.
Definition gsIterativeSolver.h:258
LinOpPtr m_precond
The preconditioner.
Definition gsIterativeSolver.h:253
static gsOptionList defaultOptions()
Returns a list of default options.
Definition gsIterativeSolver.h:92
gsOptionList::Real m_tol
The tolerance for m_error to be reached.
Definition gsIterativeSolver.h:255
index_t m_num_iter
The number of iterations performed.
Definition gsIterativeSolver.h:256
index_t m_max_iters
The upper bound for the number of iterations.
Definition gsIterativeSolver.h:254
const LinOpPtr m_mat
The matrix/operator to be solved for.
Definition gsIterativeSolver.h:252
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Class which holds a list of parameters/options, and provides easy access to them.
Definition gsOptionList.h:33
bool askSwitch(const std::string &label, const bool &value=false) const
Reads value for option label from options.
Definition gsOptionList.cpp:128
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
Abstract class for iterative solvers.
The G+Smo namespace, containing all definitions for the library.