25template<
class T=real_t>
29 typedef memory::shared_ptr<gsIterativeSolver> Ptr;
30 typedef memory::unique_ptr<gsIterativeSolver> uPtr;
42 const LinOpPtr& precond)
70 template<
typename Derived>
72 const LinOpPtr& precond)
73 :
m_mat(makeMatrixOp(mat.derived())),
95 opt.
addInt (
"MaxIterations" ,
"Maximum number of iterations", 1000 );
96 opt.
addReal (
"Tolerance" ,
"Tolerance for the error criteria on the "
97 "relative residual error", 1e-10 );
138 error_history.resize(1,1);
144 std::vector<T> tmp_error_hist;
145 tmp_error_hist.clear();
147 tmp_error_hist.push_back(
m_error);
156 tmp_error_hist.push_back(
m_error);
161 tmp_error_hist.push_back(
m_error);
175 "Iterative solvers only work for single column right-hand side." );
177 "The right-hand side does not match the matrix: "
178 << rhs.rows() <<
"!="<<
m_mat->rows() );
186 x.setZero(rhs.rows(),rhs.cols());
192 x.setZero(rhs.rows(), rhs.cols());
196 "Iterative solvers only work for single right-hand side and solution." );
198 "The initial guess does not match the matrix: "
199 << x.rows() <<
"!="<<
m_mat->cols() );
238 virtual std::ostream &
print(std::ostream &os)
const = 0;
243 std::ostringstream os;
245 os <<
" Tolerance : " <<
tolerance() <<
"\n";
246 os <<
" Solver error : " <<
error() <<
"\n";
265{
return b.
print(os); }
268template <
class SolverType>
271 typedef typename SolverType::ScalarType T;
275 typedef memory::shared_ptr<gsIterativeSolverOp>
Ptr;
278 typedef memory::unique_ptr<gsIterativeSolverOp>
uPtr;
281 template<
class MatrixType>
283 : m_solver(matrix, preconditioner) {}
286 template<
class MatrixType>
287 static uPtr make(
const MatrixType& matrix,
const LinOpPtr& preconditioner = LinOpPtr())
295 res.setZero(input.rows(), input.cols());
296 m_solver.solve(input, res);
297 gsDebug << ( (m_solver.error() < m_solver.tolerance())
298 ?
"gsIterativeSolverOp reached desired error bound after "
299 :
"msIterativeSolverOp did not reach desired error bound within " )
300 << m_solver.iterations() <<
" iterations.\n";
301 gsDebug << input.rows() <<
"; " << input.norm() <<
"; " << res.norm() <<
"\n";
309 SolverType&
solver() {
return m_solver; }
312 const SolverType&
solver()
const {
return m_solver; }
315 mutable SolverType m_solver;
Creates a mapped object or data pointer to a vector without copying data.
Definition gsAsMatrix.h:239
static uPtr make(index_t dim)
Make function returning a smart pointer.
Definition gsLinearOperator.h:132
This wrapper class allows gsIterativeSolver to be used as gsLinearOperator.
Definition gsIterativeSolver.h:270
const SolverType & solver() const
Access the solver class.
Definition gsIterativeSolver.h:312
index_t rows() const
Returns the number of rows of the operator.
Definition gsIterativeSolver.h:304
void apply(const gsMatrix< T > &input, gsMatrix< T > &res) const
apply the operator on the input vector and store the result in x
Definition gsIterativeSolver.h:293
gsIterativeSolverOp(const MatrixType &matrix, const LinOpPtr &preconditioner=LinOpPtr())
Constructor taking the underlying matrix/operator and the preconditioner.
Definition gsIterativeSolver.h:282
static uPtr make(const MatrixType &matrix, const LinOpPtr &preconditioner=LinOpPtr())
Make function taking the underlying matrix/operator and the preconditioner.
Definition gsIterativeSolver.h:287
static uPtr make(SolverType solver)
Make function taking a matrix OR a shared pointer.
Definition gsIterativeSolver.h:291
index_t cols() const
Returns the number of columns of the operator.
Definition gsIterativeSolver.h:306
SolverType & solver()
Access the solver class.
Definition gsIterativeSolver.h:309
memory::shared_ptr< gsIterativeSolverOp > Ptr
Shared pointer for gsIterativeSolverOp.
Definition gsIterativeSolver.h:275
memory::unique_ptr< gsIterativeSolverOp > uPtr
Unique pointer for gsIterativeSolverOp.
Definition gsIterativeSolver.h:278
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
index_t iterations() const
The number of iterations needed to reach the error criteria.
Definition gsIterativeSolver.h:226
virtual void finalizeIteration(VectorType &)
Some post-processing might be required.
Definition gsIterativeSolver.h:205
void setMaxIterations(index_t max_iters)
Set the maximum number of iterations (default: 1000)
Definition gsIterativeSolver.h:220
T m_rhs_norm
The norm of the right-hand-side.
Definition gsIterativeSolver.h:257
T tolerance() const
The chosen tolerance for the error criteria on the relative residual error.
Definition gsIterativeSolver.h:235
T m_error
The relative error as absolute_error/m_rhs_norm.
Definition gsIterativeSolver.h:258
void setTolerance(T tol)
Set the tolerance for the error criteria on the relative residual error (default: 1e-10)
Definition gsIterativeSolver.h:223
void solve(const VectorType &rhs, VectorType &x)
Solves the linear system and stores the solution in x.
Definition gsIterativeSolver.h:114
virtual std::ostream & print(std::ostream &os) const =0
Prints the object as a string.
gsIterativeSolver(const LinOpPtr &mat, const LinOpPtr &precond)
Contructor using a linear operator to be solved for and a preconditioner.
Definition gsIterativeSolver.h:41
void solveDetailed(const VectorType &rhs, VectorType &x, VectorType &error_history)
Solves the linear system and stores the solution in x and records the error histroy.
Definition gsIterativeSolver.h:134
index_t size() const
Returns the size of the linear system.
Definition gsIterativeSolver.h:208
LinOpPtr underlying() const
Get the underlying matrix/operator to be solved for.
Definition gsIterativeSolver.h:217
virtual bool step(VectorType &x)=0
Perform one step, requires initIteration.
LinOpPtr m_precond
The preconditioner.
Definition gsIterativeSolver.h:253
std::ostream & operator<<(std::ostream &os, const gsIterativeSolver< T > &b)
Print (as string) operator for iterative solvers.
Definition gsIterativeSolver.h:264
LinOpPtr preconditioner() const
Get the preconditioner.
Definition gsIterativeSolver.h:214
void setPreconditioner(const LinOpPtr &precond)
Set the preconditionner.
Definition gsIterativeSolver.h:211
T error() const
The relative residual error of the current iterate.
Definition gsIterativeSolver.h:232
gsIterativeSolver(const gsEigen::EigenBase< Derived > &mat, const LinOpPtr &precond)
Contructor using any dense or sparse matrix and a preconditioner.
Definition gsIterativeSolver.h:71
static gsOptionList defaultOptions()
Returns a list of default options.
Definition gsIterativeSolver.h:92
virtual bool initIteration(const VectorType &rhs, VectorType &x)
Init the iteration.
Definition gsIterativeSolver.h:172
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
virtual std::string detail() const
Prints the object as a string with extended details.
Definition gsIterativeSolver.h:241
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
Simple abstract class for discrete operators.
Definition gsLinearOperator.h:29
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition gsLinearOperator.h:33
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
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
Real askReal(const std::string &label, const Real &value=0) const
Reads value for option label from options.
Definition gsOptionList.cpp:139
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
index_t askInt(const std::string &label, const index_t &value=0) const
Reads value for option label from options.
Definition gsOptionList.cpp:117
#define index_t
Definition gsConfig.h:32
#define gsDebug
Definition gsDebug.h:61
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
Handles shared library creation and other class attributes.
This is the main header file that collects wrappers of Eigen for linear algebra.
Simple adapter classes to use matrices or linear solvers as gsLinearOperators.
Provides a list of labeled parameters/options that can be set and accessed easily.
unique_ptr< T > make_unique(T *x)
Definition gsMemory.h:198
The G+Smo namespace, containing all definitions for the library.