11#ifndef BELOS_GMRES_POLY_SOLMGR_HPP
12#define BELOS_GMRES_POLY_SOLMGR_HPP
26#include "Teuchos_as.hpp"
27#ifdef BELOS_TEUCHOS_TIME_MONITOR
28#include "Teuchos_TimeMonitor.hpp"
121template<
class ScalarType,
class MV,
class OP>
125 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
127 typedef Teuchos::ScalarTraits<MagnitudeType> MTS;
162 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
168 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
215 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
216 return Teuchos::tuple(timerPoly_);
254 problem_->setProblem ();
255 poly_Op_ = Teuchos::null;
296 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
299 Teuchos::RCP<std::ostream> outputStream_;
302 Teuchos::RCP<Teuchos::ParameterList> params_;
303 Teuchos::RCP<Teuchos::ParameterList> outerParams_;
306 static constexpr int maxDegree_default_ = 25;
308 static constexpr const char * label_default_ =
"Belos";
309 static constexpr const char * outerSolverType_default_ =
"";
310 static constexpr const char * polyType_default_ =
"Arnoldi";
311 static constexpr const char * orthoType_default_ =
"ICGS";
312 static constexpr bool addRoots_default_ =
true;
313 static constexpr bool dampPoly_default_ =
false;
314 static constexpr bool randomRHS_default_ =
true;
317 MagnitudeType polyTol_, achievedTol_;
318 int maxDegree_, numIters_;
320 bool hasOuterSolver_;
324 std::string polyType_;
325 std::string outerSolverType_;
326 std::string orthoType_;
330 Teuchos::RCP<gmres_poly_t> poly_Op_;
334 Teuchos::RCP<Teuchos::Time> timerPoly_;
341 mutable Teuchos::RCP<const Teuchos::ParameterList> validPL_;
345template<
class ScalarType,
class MV,
class OP>
349 achievedTol_(MTS::
zero()),
350 maxDegree_ (maxDegree_default_),
352 verbosity_ (verbosity_default_),
353 hasOuterSolver_ (
false),
354 randomRHS_ (randomRHS_default_),
355 damp_ (dampPoly_default_),
356 addRoots_ (addRoots_default_),
357 polyType_ (polyType_default_),
358 outerSolverType_ (outerSolverType_default_),
359 orthoType_ (orthoType_default_),
361 label_ (label_default_),
367template<
class ScalarType,
class MV,
class OP>
370 const Teuchos::RCP<Teuchos::ParameterList> &
pl) :
374 maxDegree_ (maxDegree_default_),
376 verbosity_ (verbosity_default_),
377 hasOuterSolver_ (
false),
378 randomRHS_ (randomRHS_default_),
379 damp_ (dampPoly_default_),
380 addRoots_ (addRoots_default_),
381 polyType_ (polyType_default_),
382 outerSolverType_ (outerSolverType_default_),
383 orthoType_ (orthoType_default_),
385 label_ (label_default_),
390 problem_.is_null (), std::invalid_argument,
391 "Belos::GmresPolySolMgr: The given linear problem is null. "
392 "Please call this constructor with a nonnull LinearProblem argument, "
393 "or call the constructor that does not take a LinearProblem.");
397 if (!
pl.is_null ()) {
403template<
class ScalarType,
class MV,
class OP>
404Teuchos::RCP<const Teuchos::ParameterList>
407 if (validPL_.is_null ()) {
408 Teuchos::RCP<Teuchos::ParameterList>
pl = Teuchos::parameterList ();
412 pl->set(
"Polynomial Type",
static_cast<const char *
>(polyType_default_),
413 "The type of GMRES polynomial that is used as a preconditioner: Roots, Arnoldi, or Gmres.");
415 "The relative residual tolerance that used to construct the GMRES polynomial.");
416 pl->set(
"Maximum Degree",
static_cast<int>(maxDegree_default_),
417 "The maximum degree allowed for any GMRES polynomial.");
418 pl->set(
"Outer Solver",
static_cast<const char *
>(outerSolverType_default_),
419 "The outer solver that this polynomial is used to precondition.");
420 pl->set(
"Outer Solver Params", Teuchos::ParameterList(),
421 "Parameter list for the outer solver.");
422 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
423 "What type(s) of solver information should be outputted\n"
424 "to the output stream.");
425 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
426 "A reference-counted pointer to the output stream where all\n"
427 "solver output is sent.");
428 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
429 "The string to use as a prefix for the timer labels.");
430 pl->set(
"Orthogonalization",
static_cast<const char *
>(orthoType_default_),
431 "The type of orthogonalization to use to generate polynomial: DGKS, ICGS, or IMGS.");
432 pl->set(
"Random RHS",
static_cast<bool>(randomRHS_default_),
433 "Add roots to polynomial for stability.");
434 pl->set(
"Add Roots",
static_cast<bool>(addRoots_default_),
435 "Add roots to polynomial for stability.");
436 pl->set(
"Damp Poly",
static_cast<bool>(dampPoly_default_),
437 "Damp polynomial for ill-conditioned problems.");
444template<
class ScalarType,
class MV,
class OP>
449 if (params_.is_null ()) {
450 params_ = Teuchos::parameterList (*getValidParameters ());
453 params->validateParameters (*getValidParameters (),0);
457 if (
params->isParameter(
"Polynomial Type")) {
458 polyType_ =
params->get(
"Polynomial Type", polyType_default_);
462 params_->set(
"Polynomial Type", polyType_);
465 if (
params->isParameter(
"Outer Solver")) {
466 outerSolverType_ =
params->get(
"Outer Solver", outerSolverType_default_);
470 params_->set(
"Outer Solver", outerSolverType_);
473 if (
params->isSublist(
"Outer Solver Params")) {
474 outerParams_ = Teuchos::parameterList(
params->get<Teuchos::ParameterList>(
"Outer Solver Params") );
478 if (
params->isParameter(
"Maximum Degree")) {
479 maxDegree_ =
params->get(
"Maximum Degree",maxDegree_default_);
483 params_->set(
"Maximum Degree", maxDegree_);
486 if (
params->isParameter(
"Timer Label")) {
492#ifdef BELOS_TEUCHOS_TIME_MONITOR
493 std::string
polyLabel = label_ +
": GmresPolyOp creation time";
494 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(
polyLabel);
500 params_->set(
"Timer Label", label_);
503 if (
params->isParameter(
"Orthogonalization")) {
508 std::ostringstream
os;
509 os <<
"Belos::GCRODRSolMgr: Invalid orthogonalization name \""
511 <<
"for the \"Orthogonalization\" name parameter: ";
513 throw std::invalid_argument (
os.str());
520 params_->set(
"Orthogonalization", orthoType_);
523 if (
params->isParameter(
"Verbosity")) {
524 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
525 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
527 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
532 params_->set(
"Verbosity", verbosity_);
535 if (
params->isParameter(
"Output Stream")) {
536 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
540 params_->set(
"Output Stream", outputStream_);
544 if (
params->isParameter(
"Polynomial Tolerance")) {
545 if (
params->isType<MagnitudeType> (
"Polynomial Tolerance")) {
546 polyTol_ =
params->get (
"Polynomial Tolerance",
555 params_->set(
"Polynomial Tolerance", polyTol_);
558 if (
params->isParameter(
"Random RHS")) {
559 randomRHS_ =
params->get(
"Random RHS",randomRHS_default_);
563 params_->set(
"Random RHS", randomRHS_);
567 if (
params->isParameter(
"Damped Poly")) {
568 damp_ =
params->get(
"Damped Poly",dampPoly_default_);
571 params_->set(
"Damped Poly", damp_);
574 if (
params->isParameter(
"Add Roots")) {
575 addRoots_ =
params->get(
"Add Roots",addRoots_default_);
579 params_->set(
"Add Roots", addRoots_);
582#ifdef BELOS_TEUCHOS_TIME_MONITOR
583 if (timerPoly_ == Teuchos::null) {
584 std::string
polyLabel = label_ +
": GmresPolyOp creation time";
585 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(
polyLabel);
590 if (outerSolverType_ !=
"") {
591 hasOuterSolver_ =
true;
599template<
class ScalarType,
class MV,
class OP>
604 using Teuchos::rcp_const_cast;
614 setParameters (Teuchos::parameterList (*getValidParameters ()));
619 "Belos::GmresPolySolMgr::solve: The linear problem has not been set yet, "
620 "or was set to null. Please call setProblem() with a nonnull input before "
625 "Belos::GmresPolySolMgr::solve: The linear problem is not ready. Please "
626 "call setProblem() on the LinearProblem object before calling solve().");
630 if (!poly_dim_ && maxDegree_) {
631#ifdef BELOS_TEUCHOS_TIME_MONITOR
632 Teuchos::TimeMonitor
slvtimer(*timerPoly_);
634 poly_Op_ = Teuchos::rcp(
new gmres_poly_t( problem_, params_ ) );
635 poly_dim_ = poly_Op_->polyDegree();
638 "Belos::GmresPolyOp: Failed to generate polynomial that satisfied requirements.");
643 if (hasOuterSolver_ && maxDegree_) {
650 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
659 std::string
solverLabel = label_ +
": Hybrid Gmres";
663 if (problem_->getLeftPrec() != Teuchos::null)
669 if (problem_->getInitResVec() != Teuchos::null)
676 numIters_ =
solver->getNumIters();
677 loaDetected_ =
solver->isLOADetected();
678 achievedTol_ =
solver->achievedTol();
681 else if (hasOuterSolver_) {
687 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
689 solver->setProblem( problem_ );
692 numIters_ =
solver->getNumIters();
693 loaDetected_ =
solver->isLOADetected();
694 achievedTol_ =
solver->achievedTol();
697 else if (maxDegree_) {
700 poly_Op_->ApplyPoly( *problem_->getRHS(), *problem_->getLHS() );
701 achievedTol_ = MTS::one();
709template<
class ScalarType,
class MV,
class OP>
712 std::ostringstream
out;
714 out <<
"\"Belos::GmresPolySolMgr\": {"
715 <<
"ScalarType: " << Teuchos::TypeNameTraits<ScalarType>::name ()
716 <<
", Poly Degree: " << poly_dim_
717 <<
", Poly Max Degree: " << maxDegree_
718 <<
", Poly Tol: " << polyTol_;
Belos header file which uses auto-configuration information to include necessary C++ headers.
Defines the GMRES polynomial operator hybrid-GMRES iterative linear solver.
Class which describes the linear problem to be solved by the iterative solver.
Pure virtual base class which describes the basic interface for a solver manager.
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
GmresPolySolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
GmresPolySolMgrLinearProblemFailure(const std::string &what_arg)
GmresPolySolMgrPolynomialFailure is thrown when their is a problem generating the GMRES polynomial fo...
GmresPolySolMgrPolynomialFailure(const std::string &what_arg)
The GMRES polynomial can be created in conjunction with any standard preconditioner.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
std::string description() const override
Method to return description of the hybrid block GMRES solver manager.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Get current linear problem being solved for in this object.
virtual ~GmresPolySolMgr()
Destructor.
GmresPolySolMgr()
Empty constructor for GmresPolySolMgr. This constructor takes no arguments and sets the default value...
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
void reset(const ResetType type) override
Reset the solver.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
A linear system to solve, and its associated information.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double polyTol
Relative residual tolerance for matrix polynomial construction.