G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsPreconditioner.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <gsIO/gsOptionList.h>
17 
18 namespace gismo
19 {
20 
41 template<class T>
43 {
44 public:
45 
47  typedef memory::shared_ptr<gsPreconditionerOp> Ptr;
48 
50  typedef memory::unique_ptr<gsPreconditionerOp> uPtr;
51 
54 
57 
58  gsPreconditionerOp() : m_num_of_sweeps(1) {}
59 
65  virtual void step(const gsMatrix<T> & rhs, gsMatrix<T> & x) const = 0;
66 
76  virtual void stepT(const gsMatrix<T> & rhs, gsMatrix<T> & x) const
77  { step(rhs, x); } // Assume symmetry.
78 
79  void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
80  {
81  x.setZero(this->rows(),input.cols()); // we assume quadratic matrices
82  for ( index_t i = 0; i < m_num_of_sweeps; ++i )
83  step(input,x);
84  }
85 
87  virtual BasePtr underlyingOp() const = 0;
88 
91  {
92  GISMO_ASSERT ( n > 0, "Number of sweeps needs to be positive." );
93  m_num_of_sweeps = n;
94  }
95 
98  {
99  return m_num_of_sweeps;
100  }
101 
104  {
106  opt.addInt( "NumOfSweeps", "Number of sweeps to be applied in the member function apply", 1 );
107  return opt;
108  }
109 
111  virtual void setOptions(const gsOptionList & opt)
112  {
113  Base::setOptions(opt);
114  m_num_of_sweeps = opt.askInt( "NumOfSweeps", m_num_of_sweeps );
115  }
116 
122  {
123  gsMatrix<T> x, tmp;
124  x.setRandom(this->rows(),1);
125  for (index_t i=0; i<steps; ++i)
126  {
127  x.array() /= math::sqrt( x.col(0).dot(x.col(0)) );
128  underlyingOp()->apply(x,tmp);
129  x.setZero(this->rows(),1);
130  this->step(tmp,x);
131  }
132  return math::sqrt( x.col(0).dot(x.col(0)) );
133  }
134 
135 protected:
136  index_t m_num_of_sweeps;
137 
138 }; // gsPreconditionerOp
139 
150 template<class T>
151 class gsPreconditionerFromOp GISMO_FINAL : public gsPreconditionerOp<T>
152 {
153 public:
154 
156  typedef memory::shared_ptr<gsPreconditionerFromOp> Ptr;
157 
159  typedef memory::unique_ptr<gsPreconditionerFromOp> uPtr;
160 
163 
166 
173  gsPreconditionerFromOp( BasePtr underlying, BasePtr preconditioner, T tau = (T)1)
174  : m_underlying(give(underlying)), m_preconditioner(give(preconditioner)), m_tau(tau)
175  {
176  GISMO_ASSERT( m_underlying->rows() == m_underlying->cols()
177  && m_preconditioner->rows() == m_preconditioner->cols()
178  && m_underlying->rows() == m_preconditioner->rows(),
179  "The dimensions do not agree." );
180  }
181 
188  static uPtr make( BasePtr underlying, BasePtr preconditioner, T tau = (T)1)
189  { return uPtr( new gsPreconditionerFromOp(give(underlying), give(preconditioner), tau) ); }
190 
191  void step(const gsMatrix<T> & rhs, gsMatrix<T> & x) const
192  {
193  GISMO_ASSERT( m_underlying->rows() == x.rows() && x.rows() == rhs.rows() && x.cols() == rhs.cols(),
194  "The dimensions do not agree." );
195 
196  m_underlying->apply(x, m_res);
197  m_res -= rhs;
198  m_preconditioner->apply(m_res, m_corr);
199  x -= m_tau * m_corr;
200  }
201 
202  // virtual void stepT(const gsMatrix<T> & rhs, gsMatrix<T> & x) const { step( rhs, x); } // Assume symmetry.
203 
204  void apply(const gsMatrix<T> & rhs, gsMatrix<T> & x) const
205  {
206  GISMO_ASSERT( m_underlying->rows() == rhs.rows(), "The dimensions do not agree." );
207 
208  // special treatment for the first step
209  m_preconditioner->apply(rhs, x);
210  if (m_tau != (T)1)
211  x *= m_tau;
212 
213  for (index_t i=1; i<m_num_of_sweeps; ++i)
214  {
215  m_underlying->apply(x, m_res);
216  m_res -= rhs;
217  m_preconditioner->apply(m_res, m_corr);
218  x -= m_tau * m_corr;
219  }
220  }
221 
223  void setDamping(const T tau) { m_tau = tau; }
224 
226  T getDamping() const { return m_tau; }
227 
230  {
232  opt.addReal( "Damping", "Damping parameter of the operator preconditioner", 1 );
233  return opt;
234  }
235 
237  void setOptions(const gsOptionList & opt)
238  {
239  Base::setOptions(opt);
240  m_tau = opt.askReal( "Damping", m_tau );
241  }
242 
243  BasePtr underlyingOp() const { return m_underlying; }
244  index_t rows() const { return m_preconditioner->rows(); }
245  index_t cols() const { return m_preconditioner->cols(); }
246 
247 protected:
248  BasePtr m_underlying;
249  BasePtr m_preconditioner;
250  T m_tau;
251  mutable gsMatrix<T> m_res;
252  mutable gsMatrix<T> m_corr;
254 }; // gsPreconditionerFromOp
255 
256 } // namespace gismo
T estimateLargestEigenvalueOfPreconditionedSystem(index_t steps=10) const
Estimates the largest eigenvalue of .
Definition: gsPreconditioner.h:121
virtual void setOptions(const gsOptionList &opt)
Set options based on a gsOptionList object.
Definition: gsPreconditioner.h:111
void step(const gsMatrix< T > &rhs, gsMatrix< T > &x) const
Apply the method for given right hand side and current iterate.
Definition: gsPreconditioner.h:191
gsLinearOperator< T >::Ptr BasePtr
Base class.
Definition: gsPreconditioner.h:56
T getDamping() const
Get damping parameter.
Definition: gsPreconditioner.h:226
virtual index_t rows() const =0
Returns the number of rows of the operator.
static gsOptionList defaultOptions()
Get the default options as gsOptionList object.
Definition: gsLinearOperator.h:65
S give(S &x)
Definition: gsMemory.h:266
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsPreconditioner.h:79
#define index_t
Definition: gsConfig.h:32
index_t numOfSweeps()
Get the number of sweeps to be applied in the member function apply.
Definition: gsPreconditioner.h:97
virtual void setOptions(const gsOptionList &)
Set options based on a gsOptionList object.
Definition: gsLinearOperator.h:69
void setDamping(const T tau)
Set damping parameter.
Definition: gsPreconditioner.h:223
Simple class allowing to construct a preconditioner from a linear operator.
Definition: gsPreconditioner.h:151
gsLinearOperator< T > Base
Base class.
Definition: gsPreconditioner.h:53
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
gsLinearOperator< T > Base
Base class.
Definition: gsPreconditioner.h:162
void addInt(const std::string &label, const std::string &desc, const index_t &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:201
Provides a list of labeled parameters/options that can be set and accessed easily.
static uPtr make(BasePtr underlying, BasePtr preconditioner, T tau=(T) 1)
Make function returning a smart pointer.
Definition: gsPreconditioner.h:188
memory::unique_ptr< gsPreconditionerFromOp > uPtr
Unique pointer for gsLinearOperator.
Definition: gsPreconditioner.h:159
virtual BasePtr underlyingOp() const =0
Return the underlying operator .
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition: gsLinearOperator.h:33
virtual void step(const gsMatrix< T > &rhs, gsMatrix< T > &x) const =0
Apply the method for given right hand side and current iterate.
void setNumOfSweeps(index_t n)
Set the number of sweeps to be applied in the member function apply.
Definition: gsPreconditioner.h:90
memory::unique_ptr< gsLinearOperator > uPtr
Unique pointer for gsLinearOperator.
Definition: gsLinearOperator.h:36
memory::unique_ptr< gsPreconditionerOp > uPtr
Unique pointer for gsLinearOperator.
Definition: gsPreconditioner.h:50
Simple abstract class for perconditioners.
Definition: gsPreconditioner.h:42
gsLinearOperator< T >::Ptr BasePtr
Base class.
Definition: gsPreconditioner.h:165
Real askReal(const std::string &label, const Real &value=0) const
Reads value for option label from options.
Definition: gsOptionList.cpp:139
memory::shared_ptr< gsPreconditionerFromOp > Ptr
Shared pointer for gsLinearOperator.
Definition: gsPreconditioner.h:156
Simple abstract class for discrete operators.
Definition: gsLinearOperator.h:28
index_t askInt(const std::string &label, const index_t &value=0) const
Reads value for option label from options.
Definition: gsOptionList.cpp:117
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
virtual void stepT(const gsMatrix< T > &rhs, gsMatrix< T > &x) const
Apply the transposed variant of the method for given right hand side and current iterate.
Definition: gsPreconditioner.h:76
void apply(const gsMatrix< T > &rhs, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsPreconditioner.h:204
BasePtr underlyingOp() const
Return the underlying operator .
Definition: gsPreconditioner.h:243
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
static gsOptionList defaultOptions()
Get the default options as gsOptionList object.
Definition: gsPreconditioner.h:229
static gsOptionList defaultOptions()
Get the default options as gsOptionList object.
Definition: gsPreconditioner.h:103
index_t cols() const
Returns the number of columns of the operator.
Definition: gsPreconditioner.h:245
index_t rows() const
Returns the number of rows of the operator.
Definition: gsPreconditioner.h:244
Simple abstract class for (discrete) linear operators.
void setOptions(const gsOptionList &opt)
Set options based on a gsOptionList object.
Definition: gsPreconditioner.h:237
memory::shared_ptr< gsPreconditionerOp > Ptr
Shared pointer for gsLinearOperator.
Definition: gsPreconditioner.h:47