G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsGradientMethod.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<gsGradientMethod> Ptr;
39  typedef memory::unique_ptr<gsGradientMethod> uPtr;
40 
48  template< typename OperatorType >
49  explicit gsGradientMethod( const OperatorType& mat,
50  const LinOpPtr& precond = LinOpPtr())
51  : Base(mat, precond), m_adapt_step_size(true), m_step_size(0) {}
52 
58  template< typename OperatorType >
59  explicit gsGradientMethod( const OperatorType& mat,
60  const LinOpPtr& precond,
61  T step_size )
62  : Base(mat, precond), m_adapt_step_size(false), m_step_size(step_size) {}
63 
71  template< typename OperatorType >
72  static uPtr make( const OperatorType& mat,
73  const LinOpPtr& precond = LinOpPtr())
74  { return uPtr( new gsGradientMethod(mat, precond) ); }
75 
81  template< typename OperatorType >
82  static uPtr make( const OperatorType& mat,
83  const LinOpPtr& precond,
84  T step_size )
85  { return uPtr( new gsGradientMethod(mat, precond, step_size) ); }
86 
88  bool adaptiveStepSize() const { return m_adapt_step_size; }
89 
91  T stepSize() const { return m_step_size; }
92 
95  void setAdaptiveStepSize() { m_adapt_step_size = true; m_step_size = 0; }
96 
98  void setStepSize(T step_size) { m_adapt_step_size = false; m_step_size = (gsOptionList::Real)step_size; }
99 
102  {
104  opt.addSwitch("AdaptiveStepSize", "Adaptive step sizes (to minimize residual)", true );
105  opt.addReal ("StepSize", "Step size (requires AdaptiveStepSize to be false)" , 0 );
106  return opt;
107  }
108 
111  {
112  Base::setOptions(opt);
113  m_adapt_step_size = opt.askSwitch("AdaptiveStepSize", m_adapt_step_size);
114  m_step_size = opt.askReal ("StepSize", m_step_size );
115  if (m_adapt_step_size) m_step_size = 0; // for consistency of output, see above
116  return *this;
117  }
118 
119  bool initIteration( const VectorType& rhs, VectorType& x );
120  bool step( VectorType& x );
121 
123  std::ostream &print(std::ostream &os) const
124  {
125  os << "gsGradientMethod\n";
126  return os;
127  }
128 
129 private:
130  using Base::m_mat;
131  using Base::m_precond;
132  using Base::m_max_iters;
133  using Base::m_tol;
134  using Base::m_num_iter;
135  using Base::m_rhs_norm;
136  using Base::m_error;
137 
138  VectorType m_res;
139  VectorType m_tmp;
140  VectorType m_update;
141  bool m_adapt_step_size;
142  gsOptionList::Real m_step_size;
143 
144 };
145 
146 } // namespace gismo
147 
148 #ifndef GISMO_BUILD_LIB
149 #include GISMO_HPP_HEADER(gsGradientMethod.hpp)
150 #endif
const LinOpPtr m_mat
The matrix/operator to be solved for.
Definition: gsIterativeSolver.h:252
T stepSize() const
Returns the chosen step size.
Definition: gsGradientMethod.h:91
virtual gsIterativeSolver & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition: gsIterativeSolver.h:102
bool adaptiveStepSize() const
Returns true iff adaptive step sizes are activated.
Definition: gsGradientMethod.h:88
gsGradientMethod & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition: gsGradientMethod.h:110
gsOptionList::Real m_tol
The tolerance for m_error to be reached.
Definition: gsIterativeSolver.h:255
bool step(VectorType &x)
Perform one step, requires initIteration.
Definition: gsGradientMethod.hpp:33
static uPtr make(const OperatorType &mat, const LinOpPtr &precond, T step_size)
Constructor using a matrix (operator) and optionally a preconditionner.
Definition: gsGradientMethod.h:82
T m_rhs_norm
The norm of the right-hand-side.
Definition: gsIterativeSolver.h:257
void setStepSize(T step_size)
Set the step size.
Definition: gsGradientMethod.h:98
static uPtr make(const OperatorType &mat, const LinOpPtr &precond=LinOpPtr())
Constructor using a matrix (operator) and optionally a preconditionner.
Definition: gsGradientMethod.h:72
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
The gradient method.
Definition: gsGradientMethod.h:29
gsGradientMethod(const OperatorType &mat, const LinOpPtr &precond, T step_size)
Constructor using a matrix (operator) and optionally a preconditionner.
Definition: gsGradientMethod.h:59
T m_error
The relative error as absolute_error/m_rhs_norm.
Definition: gsIterativeSolver.h:258
Real askReal(const std::string &label, const Real &value=0) const
Reads value for option label from options.
Definition: gsOptionList.cpp:139
Abstract class for iterative solvers.
bool initIteration(const VectorType &rhs, VectorType &x)
Init the iteration.
Definition: gsGradientMethod.hpp:18
void addReal(const std::string &label, const std::string &desc, const Real &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:211
gsGradientMethod(const OperatorType &mat, const LinOpPtr &precond=LinOpPtr())
Constructor using a matrix (operator) and optionally a preconditionner.
Definition: gsGradientMethod.h:49
void setAdaptiveStepSize()
Activate adaptive step size. Then in each step, the step size is chosen such that the norm of the res...
Definition: gsGradientMethod.h:95
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
std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition: gsGradientMethod.h:123
index_t m_num_iter
The number of iterations performed.
Definition: gsIterativeSolver.h:256
static gsOptionList defaultOptions()
Returns a list of default options.
Definition: gsGradientMethod.h:101
LinOpPtr m_precond
The preconditioner.
Definition: gsIterativeSolver.h:253