9#include <kernel/solver/base.hpp>
10#include <kernel/util/dist.hpp>
11#include <kernel/util/property_map.hpp>
12#include <kernel/util/statistics.hpp>
22 template<
typename Matrix_>
23 const Dist::Comm* get_mat_comm(
const Matrix_& mat,
typename Matrix_::GateRowType*)
25 return mat.get_comm();
28 template<
typename Matrix_>
29 const Dist::Comm* get_mat_comm(
const Matrix_&, ...)
35 template<
typename Vector_>
36 const Dist::Comm* get_vec_comm(
const Vector_& vec,
typename Vector_::GateType*)
38 return vec.get_comm();
41 template<
typename Vector_>
42 const Dist::Comm* get_vec_comm(
const Vector_&, ...)
66 inline std::ostream& operator<<(std::ostream& os,
PlotMode mode)
75 return os <<
"summary";
79 return os <<
"-unknown-";
89 if(s.compare_no_case(
"none") == 0)
91 else if(s.compare_no_case(
"iter") == 0)
93 else if(s.compare_no_case(
"summary") == 0)
95 else if(s.compare_no_case(
"all") == 0)
98 is.setstate(std::ios_base::failbit);
195 template<
typename Vector_>
314 auto plot_mode_p = section->
get_entry(
"plot_mode");
315 if (plot_mode_p.second && !plot_mode_p.first.parse(this->_plot_mode))
316 throw ParseError(section_name +
".plot_mode", plot_mode_p.first,
"one of: none, iter, summary, all");
318 auto plot_name_p = section->
get_entry(
"plot_name");
319 if (plot_name_p.second)
320 this->_plot_name = plot_name_p.first;
322 auto plot_interval_p = section->
get_entry(
"plot_interval");
323 if (plot_interval_p.second && !plot_interval_p.first.parse(this->_plot_interval))
324 throw ParseError(section_name +
".plot_interval", plot_interval_p.first,
"an integer >= 0");
326 auto tol_abs_p = section->
get_entry(
"tol_abs");
327 if (tol_abs_p.second && !tol_abs_p.first.parse(this->_tol_abs))
328 throw ParseError(section_name +
".tol_abs", tol_abs_p.first,
"a floating point number");
330 auto tol_rel_p = section->
get_entry(
"tol_rel");
331 if (tol_rel_p.second && !tol_rel_p.first.parse(this->_tol_rel))
332 throw ParseError(section_name +
".tol_rel", tol_rel_p.first,
"a floating point number");
334 auto tol_abs_low_p = section->
get_entry(
"tol_abs_low");
335 if (tol_abs_low_p.second && !tol_abs_low_p.first.parse(this->_tol_abs_low))
336 throw ParseError(section_name +
".tol_abs_low", tol_abs_low_p.first,
"a floating point number");
338 auto div_abs_p = section->
get_entry(
"div_abs");
339 if (div_abs_p.second && !div_abs_p.first.parse(this->_div_abs))
340 throw ParseError(section_name +
".div_abs", div_abs_p.first,
"a floating point number");
342 auto div_rel_p = section->
get_entry(
"div_rel");
343 if (div_rel_p.second && !div_rel_p.first.parse(this->_div_rel))
344 throw ParseError(section_name +
".div_rel", div_rel_p.first,
"a floating point number");
346 auto stag_rate_p = section->
get_entry(
"stag_rate");
347 if (stag_rate_p.second && !stag_rate_p.first.parse(this->_stag_rate))
348 throw ParseError(section_name +
".stag_rate", stag_rate_p.first,
"a floating point number in range [0,1)");
350 auto max_iter_p = section->
get_entry(
"max_iter");
351 if (max_iter_p.second && !max_iter_p.first.parse(this->_max_iter))
352 throw ParseError(section_name +
".max_iter", max_iter_p.first,
"an integer >= 0");
354 auto min_iter_p = section->
get_entry(
"min_iter");
355 if (min_iter_p.second && !min_iter_p.first.parse(this->_min_iter))
356 throw ParseError(section_name +
".min_iter", min_iter_p.first,
"an integer >= 0");
358 auto min_stag_iter_p = section->
get_entry(
"min_stag_iter");
359 if (min_stag_iter_p.second && !min_stag_iter_p.first.parse(this->_min_stag_iter))
360 throw ParseError(section_name +
".min_stag_iter", min_stag_iter_p.first,
"an integer >= 0");
600 if(this->_def_init <=
Math::abs(this->_def_cur * Math::eps<DataType>()))
679 template<
typename Matrix_>
682 this->_comm = Intern::get_mat_comm(matrix,
nullptr);
694 this->_comm = Intern::get_vec_comm(vector,
nullptr);
708 return vec_def.norm2();
758 std::cout << line <<
"\n";
776 String msg = this->_plot_name
781 if(num_iter >
Index(0))
805 this->_def_init = this->_def_prev = this->_def_cur =
DataType(0);
806 this->_num_iter =
Index(0);
807 this->_num_stag_iter =
Index(0);
813 || (this->_min_stag_iter >
Index(0));
820 this->_def_init = this->_def_cur = this->_def_prev = this->
_calc_def_norm(vec_def, vec_sol);
821 Statistics::add_solver_expression(std::make_shared<ExpressionDefect>(this->
name(), this->_def_init, this->
get_num_iter()));
836 if(this->_def_init <=
Math::sqr(Math::eps<DataType>()))
877 if(num_iter >= this->_max_iter)
889 if(check_stag && (this->_min_stag_iter >
Index(0)))
892 if(def_cur >= this->_stag_rate * def_prev)
895 if(++this->_num_stag_iter >= this->_min_stag_iter)
901 this->_num_stag_iter =
Index(0);
936 || (this->_min_stag_iter >
Index(0));
942 Statistics::add_solver_expression(std::make_shared<ExpressionDefect>(this->
name(), this->_def_cur, this->
get_num_iter()));
950 this->
_plot_iter_line(this->_num_iter, this->_def_cur, this->_def_prev);
979 this->_def_cur = def_cur_norm;
980 Statistics::add_solver_expression(std::make_shared<ExpressionDefect>(this->
name(), this->_def_cur, this->
get_num_iter()));
987 this->
_plot_iter_line(this->_num_iter, this->_def_cur, this->_def_prev);
1004 const Vector_& vec_rhs,
1005 const Matrix_& DOXY(matrix),
1006 const Filter_& DOXY(filter))
1009 return solver.
correct(vec_sol, vec_rhs);
1024 template<
typename Vector_>
1073 const PropertyMap* section, std::shared_ptr<PrecondType> precond =
nullptr) :
1074 BaseClass(plot_name, section_name, section),
1139 template<
typename Filter_>
1142 XASSERTM(&vec_cor != &vec_def,
"vec_cor and vec_def must not refer to the same object");
1145 Statistics::add_solver_expression(std::make_shared<ExpressionCallPrecond>(this->
name(), this->_precond->name()));
1150 vec_cor.copy(vec_def);
1151 filter.filter_cor(vec_cor);
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
void print(std::ostream &os, const String &msg, int root=0) const
Prints a message line to an output stream.
Class for parser related errors.
A class organizing a tree of key-value pairs.
std::pair< String, bool > get_entry(String key) const
Retrieves a value by its key.
Abstract base-class for iterative solvers.
DataType get_div_abs() const
Returns the absolute divergence.
bool _force_def_norm_calc
whether skipping of defect computation is allowed or not
void set_plot_name(const String &plot_name)
Sets the plot name of the solver.
virtual void force_defect_norm_calc(bool force)
Forces the calculation of defect norms in every iteration.
IterativeSolver(const String &plot_name)
Protected constructor.
bool _plot_summary() const
Plot summary?
DataType _stag_rate
stagnation rate
virtual Status correct(VectorType &vec_sol, const VectorType &vec_rhs)=0
Solver correction method.
DataType _def_init
initial defect
Status get_status() const
Returns the status.
void set_plot_interval(const Index plot_interval)
Sets the interval between two plot outputs, if any.
String get_plot_name() const
Returns the plot name of the solver.
Index get_min_iter() const
Returns the minimal number of iterations.
Index _min_iter
minimum number of iterations
DataType get_tol_abs() const
Returns the absolute tolerance.
Index get_max_iter() const
Returns the maximum number of iterations.
void set_max_iter(Index max_iter)
Sets the maximum iteration count for the solver.
VectorType::DataType DataType
Floating point type.
Index _iter_digits
iteration count digits for plotting
Index get_num_iter() const
Returns number of performed iterations.
bool is_converged(const DataType def_cur) const
checks for convergence
void _set_comm(const Dist::Comm *comm)
Sets the communicator for the solver directly.
void set_tol_abs(DataType tol_abs)
Sets the absolute tolerance for the solver.
Status _status
current status of the solver
void set_stag_rate(DataType rate)
Sets the stagnation rate for the solver.
virtual Status _analyze_defect(Index num_iter, DataType def_cur, DataType def_prev, bool check_stag)
Internal function: analyze the current defect.
void _set_comm_by_matrix(const Matrix_ &matrix)
Sets the communicator for the solver from a matrix.
void _print_line(const String &line) const
Prints a line.
SolverBase< VectorType > BaseClass
The base class.
bool is_diverged() const
checks for divergence
String _plot_name
name of the solver in plots
const Dist::Comm * _comm
Communicator of the solver.
DataType get_tol_abs_low() const
Returns the lower absolute tolerance.
void set_min_iter(Index min_iter)
Sets the minimum iteration count for the solver.
DataType _div_rel
relative divergence parameter
bool _progress() const
Progress iteration?
DataType _def_prev
previous iteration defect
virtual void _plot_iter_line(Index num_iter, DataType def_cur, DataType def_prev)
Plots an iteration line.
DataType get_div_rel() const
Returns the relative divergence.
Vector_ VectorType
The vector type this solver can be applied to.
Index _plot_interval
plot output interval
virtual Status _update_defect(const DataType def_cur_norm)
Internal function: sets the new (next) defect norm.
Index get_min_stag_iter() const
Returns the minimum stagnation iteration count.
DataType _div_abs
absolute divergence parameter
virtual DataType _calc_def_norm(const VectorType &vec_def, const VectorType &vec_sol)
Computes the defect norm.
virtual Status _set_new_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the new (next) defect vector.
Index _num_iter
number of performed iterations
bool _plot_iter(Status st=Status::progress) const
Plot the current iteration?
bool is_diverged(const DataType def_cur) const
checks for divergence
DataType _tol_rel
relative tolerance parameter
void set_plot_mode(const PlotMode plot_mode)
Sets the plot mode of the solver.
void set_div_abs(DataType div_abs)
Sets the absolute divergence for the solver.
void set_tol_abs_low(DataType tol_abs_low)
Sets the lower absolute tolerance for the solver.
Index _num_stag_iter
number of consecutive stagnated iterations
DataType _def_cur
current defect
virtual void plot_summary() const
Plot a summary of the last solver run.
Index _max_iter
maximum number of iterations
DataType get_def_final() const
Returns the final defect.
DataType calc_defect_reduction() const
Computes the overall defect reduction factor: (defect_final / defect_inital)
DataType _tol_abs_low
absolute low tolerance parameter
virtual Status _set_initial_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the initial defect vector.
DataType calc_convergence_rate() const
Computes the overall convergence rate: (defect_final / defect_initial) ^ (1 / number of iterations)
bool is_converged() const
checks for convergence
void set_tol_rel(DataType tol_rel)
Sets the relative tolerance for the solver.
IterativeSolver(const String &plot_name, const String §ion_name, const PropertyMap *section)
Constructor using a PropertyMap.
void set_div_rel(DataType div_rel)
Sets the relative divergence for the solver.
virtual String get_summary() const
Returns a summary string.
void _set_comm_by_vector(const Vector_ &vector)
Sets the communicator for the solver from a vector.
DataType get_tol_rel() const
Returns the relative tolerance.
void set_min_stag_iter(Index min_iter)
Sets the minimum stagnate iteration count for the solver.
DataType _tol_abs
absolute tolerance parameter
DataType get_def_initial() const
Returns the initial defect.
DataType get_stag_rate() const
Returns the stagnation rate.
PlotMode _plot_mode
whether to plot something
Index _min_stag_iter
minimum number of stagnation iterations
Abstract base-class for preconditioned iterative solvers.
VectorType::DataType DataType
Floating point data type.
PreconditionedIterativeSolver(const String &plot_name, const String §ion_name, const PropertyMap *section, std::shared_ptr< PrecondType > precond=nullptr)
Constructor using a PropertyMap.
SolverBase< VectorType > PrecondType
the interface for the preconditioner
virtual void init_symbolic() override
Symbolic initialization method.
virtual void done_numeric() override
Numeric finalization method.
virtual ~PreconditionedIterativeSolver()
virtual destructor
IterativeSolver< VectorType > BaseClass
Our base class.
Vector_ VectorType
The vector type this solver can be applied to.
virtual void init_numeric() override
Numeric initialization method.
PreconditionedIterativeSolver(const String &plot_name, std::shared_ptr< PrecondType > precond=nullptr)
Constructor.
std::shared_ptr< PrecondType > _precond
the pointer to the preconditioner
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.
virtual void init_symbolic()
Symbolic initialization method.
virtual void init_numeric()
Numeric initialization method.
virtual void done_symbolic()
Symbolic finalization method.
virtual String name() const =0
Returns a descriptive string.
virtual void done_numeric()
Numeric finalization method.
String class implementation.
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
T_ abs(T_ x)
Returns the absolute value.
T_ pow(T_ x, T_ y)
Returns x raised to the power of y.
T_ sqr(T_ x)
Returns the square of a value.
T_ ilog10(T_ x)
Computes the integral base-10 logarithm of an integer, i.e. its number of non-zero decimal digits.
bool isfinite(T_ x)
Checks whether a value is finite.
std::istream & operator>>(std::istream &is, Pack::Type &t)
stream input operator for Pack::Type
bool status_success(Status status)
Status success check function.
PlotMode
Solver plot modes enumeration.
@ none
No plotting whatsoever.
@ iter
Plot every iteration (if applicable)
@ all
Plot every iteration (if applicable) and a summary.
@ summary
Plot a summary after each solver run.
Status
Solver status return codes enumeration.
@ success
solving successful (convergence criterion fulfilled)
@ progress
continue iteration (internal use only)
@ undefined
undefined status
@ stagnated
solver stagnated (stagnation criterion fulfilled)
@ max_iter
solver reached maximum iterations
@ diverged
solver diverged (divergence criterion fulfilled)
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
Status solve(SolverBase< Vector_ > &solver, Vector_ &vec_sol, const Vector_ &vec_rhs, const Matrix_ &matrix, const Filter_ &filter)
Solve linear system with initial solution guess.
String stringify_fp_fix(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in fixed-point notation.
String stringify(const T_ &item)
Converts an item into a String.
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
std::uint64_t Index
Index data type.