9#include <kernel/solver/iterative.hpp>
10#include <kernel/solver/krylov_orthogonalizer.hpp>
89 public PreconditionedIterativeSolver<typename Matrix_::VectorTypeR>
92 typedef Matrix_ MatrixType;
93 typedef Filter_ FilterType;
94 typedef typename MatrixType::VectorTypeR VectorType;
95 typedef typename MatrixType::DataType DataType;
96 typedef PreconditionedIterativeSolver<VectorType> BaseClass;
98 typedef SolverBase<VectorType> PrecondType;
99 typedef KrylovSubspaceOrthogonalizer<VectorType> OrthoType;
103 const MatrixType& _system_matrix;
105 const FilterType& _system_filter;
109 DataType _inner_res_scale;
114 std::unique_ptr<OrthoType> _subspace;
117 std::vector<DataType> _c, _s, _q;
119 std::vector<std::vector<DataType>> _h;
140 explicit GMRES(
const MatrixType& matrix,
const FilterType& filter, Index krylov_dim,
141 DataType inner_res_scale = DataType(0), std::shared_ptr<PrecondType> precond =
nullptr) :
142 BaseClass(
"GMRES(" +
stringify(krylov_dim) +
")", precond),
143 _system_matrix(matrix),
144 _system_filter(filter),
145 _krylov_dim(krylov_dim)
148 this->_force_def_norm_calc =
true;
151 this->_set_comm_by_matrix(matrix);
152 set_inner_res_scale(inner_res_scale);
155 explicit GMRES(
const String& section_name,
const PropertyMap* section,
156 const MatrixType& matrix,
const FilterType& filter, std::shared_ptr<PrecondType> precond =
nullptr) :
157 BaseClass(
"GMRES", section_name, section, precond),
158 _system_matrix(matrix),
159 _system_filter(filter),
160 _inner_res_scale(DataType(0))
163 this->_force_def_norm_calc =
true;
166 this->_set_comm_by_matrix(matrix);
169 auto inner_res_scale_p = section->query(
"inner_res_scale");
170 if(inner_res_scale_p.second && (!inner_res_scale_p.first.parse(this->_inner_res_scale) || (this->_inner_res_scale < DataType(0))))
171 throw ParseError(section_name +
".inner_res_scale", inner_res_scale_p.first,
"a non-negative float");
174 auto krylov_dim_p = section->query(
"krylov_dim");
175 if(!krylov_dim_p.second)
176 throw ParseError(
"GMRES config section is missing the mandatory krylov_dim!");
178 if(!krylov_dim_p.first.parse(this->_krylov_dim) || (this->_krylov_dim <=
Index(0)))
179 throw ParseError(section_name +
".krylov_dim", krylov_dim_p.first,
"a positive integer");
182 auto ortho_p = section->query(
"orthogonalizer");
185 if(ortho_p.first ==
"mgs")
186 this->_ortho_type = KrylovOrthogonalizerType::modified_gram_schmidt;
187 else if(ortho_p.first ==
"cgs")
188 this->_ortho_type = KrylovOrthogonalizerType::classical_gram_schmidt;
189 else if(ortho_p.first ==
"cgs2")
190 this->_ortho_type = KrylovOrthogonalizerType::cgs2;
192 throw ParseError(section_name +
".orthogonalizer", ortho_p.first,
"one of: mgs, cgs, cgs2");
195 this->set_plot_name(
"GMRES("+
stringify(_krylov_dim)+
")");
206 virtual String name()
const override
217 virtual void force_defect_norm_calc(
bool DOXY(force))
override
222 virtual void init_symbolic()
override
224 BaseClass::init_symbolic();
226 _c.reserve(_krylov_dim);
227 _s.reserve(_krylov_dim);
228 _q.reserve(_krylov_dim + 1);
229 _h.resize(_krylov_dim);
231 for(Index i(0); i < _krylov_dim; ++i)
233 _h.at(i).resize(i+1);
236 _vec_w = this->_system_matrix.create_vector_r();
237 _subspace = _create_orthogonalizer(_vec_w);
240 virtual void done_symbolic()
override
244 BaseClass::done_symbolic();
254 virtual void set_krylov_dim(Index krylov_dim)
257 _krylov_dim = krylov_dim;
266 virtual void set_inner_res_scale(DataType inner_res_scale)
268 XASSERT(inner_res_scale >= DataType(0));
269 _inner_res_scale = inner_res_scale;
280 virtual void set_orthogonalizer(KrylovOrthogonalizerType ortho_type)
282 _ortho_type = ortho_type;
286 virtual Status apply(VectorType& vec_sol,
const VectorType& vec_rhs)
override
289 this->_subspace->at(0).copy(vec_rhs);
295 this->_status = _apply_intern(vec_sol, vec_rhs);
296 this->plot_summary();
297 return this->_status;
301 virtual Status correct(VectorType& vec_sol,
const VectorType& vec_rhs)
override
304 this->_system_matrix.apply(this->_subspace->at(0), vec_sol, vec_rhs, -DataType(1));
305 this->_system_filter.filter_def(this->_subspace->at(0));
308 this->_status = _apply_intern(vec_sol, vec_rhs);
309 this->plot_summary();
310 return this->_status;
314 virtual Status _apply_intern(VectorType& vec_sol,
const VectorType& vec_rhs)
316 IterationStats pre_iter(*
this);
317 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
318 const MatrixType& matrix(this->_system_matrix);
319 const FilterType& filter(this->_system_filter);
322 Status status = this->_set_initial_defect(this->_subspace->at(0), vec_sol);
326 const Index krylov_dim = this->_subspace->get_krylov_dim();
329 while(status == Status::progress)
331 IterationStats stat(*
this);
336 _q.push_back(this->_def_cur);
339 this->_subspace->at(0).scale(this->_subspace->at(0), DataType(1) / _q.back());
343 while(i < krylov_dim)
346 if(!this->_apply_precond(this->_vec_w, this->_subspace->at(i), filter))
349 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
350 return Status::aborted;
355 matrix.apply(this->_subspace->at(i+1), this->_vec_w);
356 filter.filter_def(this->_subspace->at(i+1));
359 DataType alpha = this->_subspace->orthogonalize(this->_h.at(i).data(), i+1);
362 this->_subspace->at(i+1).scale(this->_subspace->at(i+1), DataType(1) / alpha);
365 for(Index k(0); k < i; ++k)
367 DataType t(this->_h.at(i).at(k));
368 this->_h.at(i).at(k ) = this->_c.at(k) * t + this->_s.at(k) * this->_h.at(i).at(k+1);
369 this->_h.at(i).at(k+1) = this->_s.at(k) * t - this->_c.at(k) * this->_h.at(i).at(k+1);
373 DataType beta = Math::sqrt(Math::sqr(this->_h.at(i).at(i)) + Math::sqr(alpha));
376 _s.push_back(alpha / beta);
377 _c.push_back(this->_h.at(i).at(i) / beta);
379 this->_h.at(i).at(i) = beta;
380 this->_q.push_back(this->_s.back() * this->_q.at(i));
381 this->_q.at(i) *= this->_c.back();
384 if(++i < this->_krylov_dim)
387 DataType def_cur = Math::abs(this->_q.back());
390 if((def_cur > this->_div_abs) || (def_cur > (this->_div_rel * this->_def_init)))
394 if(!(this->_num_iter < this->_min_iter))
397 if((def_cur <= _inner_res_scale * this->_tol_abs) &&
398 ((def_cur <= _inner_res_scale * (this->_tol_rel * this->_def_init)) || (def_cur <= _inner_res_scale * this->_tol_abs_low) ))
402 if(this->_num_iter >= this->_max_iter)
411 this->_def_cur = def_cur;
414 if(this->_plot_iter())
416 String msg = this->_plot_name
419 this->_print_line(msg);
424 if(this->_plot_iter())
427 String msg = this->_plot_name
432 this->_print_line(msg);
435 Index n = Math::min(i, krylov_dim);
438 for(Index k(n); k > 0;)
441 this->_q.at(k) /= this->_h.at(k).at(k);
442 for(Index j(k); j > 0;)
445 this->_q.at(j) -= this->_h.at(k).at(j) * this->_q.at(k);
450 this->_vec_w.format();
453 this->_subspace->correct_vector(this->_vec_w, this->_q.data(), n, DataType(1));
456 if(!this->_apply_precond(this->_subspace->at(0), this->_vec_w, filter))
459 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
460 return Status::aborted;
462 vec_sol.axpy(this->_subspace->at(0));
465 matrix.apply(this->_subspace->at(0), vec_sol, vec_rhs, -DataType(1));
466 filter.filter_def(this->_subspace->at(0));
469 status = this->_set_new_defect(this->_subspace->at(0), vec_sol);
474 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), status, this->get_num_iter()));
478 std::unique_ptr<OrthoType> _create_orthogonalizer(
const VectorType& vec_w)
482 case KrylovOrthogonalizerType::classical_gram_schmidt:
483 return std::make_unique<ClassicGramSchmidtOrthogonalizer<VectorType>>(vec_w, this->_krylov_dim);
484 case KrylovOrthogonalizerType::cgs2:
485 return std::make_unique<CGS2Orthogonalizer<VectorType>>(vec_w, this->_krylov_dim);
486 case KrylovOrthogonalizerType::modified_gram_schmidt:
488 return std::make_unique<ModifiedGramSchmidtOrthogonalizer<VectorType>>(vec_w, this->_krylov_dim);
515 template<
typename Matrix_,
typename Filter_>
516 inline std::shared_ptr<GMRES<Matrix_, Filter_>> new_gmres(
517 const Matrix_& matrix,
const Filter_& filter,
Index krylov_dim,
518 typename Matrix_::DataType inner_res_scale =
typename Matrix_::DataType(0),
519 std::shared_ptr<SolverBase<typename Matrix_::VectorTypeL>> precond =
nullptr)
521 return std::make_shared<GMRES<Matrix_, Filter_>>(matrix, filter, krylov_dim, inner_res_scale, precond);
545 template<
typename Matrix_,
typename Filter_>
546 inline std::shared_ptr<GMRES<Matrix_, Filter_>> new_gmres(
547 const String& section_name,
const PropertyMap* section,
548 const Matrix_& matrix,
const Filter_& filter,
549 std::shared_ptr<SolverBase<typename Matrix_::VectorTypeL>> precond =
nullptr)
551 return std::make_shared<GMRES<Matrix_, Filter_>>(section_name, section, matrix, filter, precond);
#define XASSERT(expr)
Assertion macro definition.
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
Status
Solver status return codes enumeration.
KrylovOrthogonalizerType
Enumeration of the available Krylov subspace orthogonalization strategies.
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.