7#ifndef KERNEL_SOLVER_DESCENT_HPP
8#define KERNEL_SOLVER_DESCENT_HPP 1
11#include <kernel/solver/iterative.hpp>
37 return os <<
"steepest";
40 return os <<
"defect";
67 typedef Matrix_ MatrixType;
68 typedef Filter_ FilterType;
69 typedef typename MatrixType::VectorTypeR VectorType;
70 typedef typename MatrixType::DataType DataType;
101 explicit Descent(
const MatrixType& matrix,
const FilterType& filter,
102 DescentVariant variant, std::shared_ptr<PrecondType> precond =
nullptr) :
135 const MatrixType& matrix,
const FilterType& filter,
136 std::shared_ptr<PrecondType> precond =
nullptr) :
137 BaseClass(
"Descent", section_name, section, precond),
145 auto variant_p = section->
query(
"variant");
146 if(!variant_p.second)
147 throw ParseError(
"Descent: config section is missing the mandatory variant!");
148 if(variant_p.first.compare_no_case(
"steepest") == 0)
150 else if(variant_p.first.compare_no_case(
"defect") == 0)
153 throw ParseError(
"Descent: invalid variant: " + variant_p.first);
167 _vec_r = this->_system_matrix.create_vector_r();
168 _vec_p = this->_system_matrix.create_vector_r();
169 _vec_q = this->_system_matrix.create_vector_r();
176 this->_vec_q.clear();
177 this->_vec_p.clear();
178 this->_vec_r.clear();
182 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def)
override
185 this->_vec_r.copy(vec_def);
191 this->
_status = _apply_intern(vec_cor);
200 virtual Status correct(VectorType& vec_sol,
const VectorType& vec_rhs)
override
203 this->_system_matrix.apply(this->_vec_r, vec_sol, vec_rhs, -DataType(1));
204 this->_system_filter.filter_def(this->_vec_r);
207 this->
_status = _apply_intern(vec_sol);
217 virtual Status _apply_intern(VectorType& vec_sol)
219 IterationStats pre_iter(*
this);
220 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->
name()));
222 const MatrixType& matrix(this->_system_matrix);
223 const FilterType& filter(this->_system_filter);
224 VectorType& vec_r(this->_vec_r);
225 VectorType& vec_p(this->_vec_p);
226 VectorType& vec_q(this->_vec_q);
234 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->
name(), status, this->
get_num_iter()));
253 IterationStats stat(*
this);
256 matrix.apply(vec_q, vec_p);
257 filter.filter_def(vec_q);
260 DataType gamma = DataType(1), omega = DataType(1);
261 switch(this->_variant)
264 gamma = omega = DataType(1);
269 gamma = vec_r.dot(vec_p);
270 omega = vec_q.dot(vec_p);
275 gamma = vec_r.dot(vec_q);
276 omega = vec_q.dot(vec_q);
281 DataType alpha = gamma / omega;
285 vec_sol.axpy(vec_p, vec_sol, alpha);
289 vec_r.axpy(vec_q, vec_r, -alpha);
296 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->
name(), status, this->
get_num_iter()));
334 template<
typename Matrix_,
typename Filter_>
336 const Matrix_& matrix,
const Filter_& filter,
DescentVariant variant,
339 return std::make_shared<Descent<Matrix_, Filter_>>(matrix, filter, variant, precond);
363 template<
typename Matrix_,
typename Filter_>
364 inline std::shared_ptr<Descent<Matrix_, Filter_>>
new_pmr(
366 const Matrix_& matrix,
const Filter_& filter,
369 return std::make_shared<Descent<Matrix_, Filter_>>(section_name, section, matrix, filter, precond);
Class for parser related errors.
A class organizing a tree of key-value pairs.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
(Preconditioned) Descent solver implementation
Descent(const String §ion_name, const PropertyMap *section, const MatrixType &matrix, const FilterType &filter, std::shared_ptr< PrecondType > precond=nullptr)
Constructor using a PropertyMap.
virtual void init_symbolic() override
Symbolic initialization method.
VectorType _vec_r
temporary vectors
const MatrixType & _system_matrix
the matrix for the solver
virtual void done_symbolic() override
Symbolic finalization method.
DescentVariant _variant
the chosen descent type
const FilterType & _system_filter
the filter for the solver
virtual String name() const override
Returns a descriptive string.
Descent(const MatrixType &matrix, const FilterType &filter, DescentVariant variant, std::shared_ptr< PrecondType > precond=nullptr)
Constructor.
void set_plot_name(const String &plot_name)
Sets the plot name of the solver.
Index get_num_iter() const
Returns number of performed iterations.
Status _status
current status of the solver
void _set_comm_by_matrix(const Matrix_ &matrix)
Sets the communicator for the solver from a matrix.
virtual Status _set_new_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the new (next) defect vector.
virtual void plot_summary() const
Plot a summary of the last solver run.
virtual Status _set_initial_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the initial defect vector.
Abstract base-class for preconditioned iterative solvers.
virtual void init_symbolic() override
Symbolic initialization method.
bool _apply_precond(VectorType &vec_cor, const VectorType &vec_def, const Filter_ &filter)
Applies the preconditioner onto a defect vector.
virtual void done_symbolic() override
Symbolic finalization method.
Polymorphic solver interface.
String class implementation.
@ defect
left-preconditioned defect minimization
@ steepest
symmetrically preconditioned steepest descent
@ fixed
simple fixed Richardson iteration
std::shared_ptr< Descent< Matrix_, Filter_ > > new_descent(const Matrix_ &matrix, const Filter_ &filter, DescentVariant variant, std::shared_ptr< SolverBase< typename Matrix_::VectorTypeL > > precond=nullptr)
Creates a new Descent solver object.
Status
Solver status return codes enumeration.
@ progress
continue iteration (internal use only)
@ undefined
undefined status
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
std::shared_ptr< Descent< Matrix_, Filter_ > > new_pmr(const String §ion_name, const PropertyMap *section, const Matrix_ &matrix, const Filter_ &filter, std::shared_ptr< SolverBase< typename Matrix_::VectorTypeL > > precond=nullptr)
Creates a new Descent solver object using a PropertyMap.
String stringify(const T_ &item)
Converts an item into a String.