43#ifndef BELOS_GMRES_POLY_SOLMGR_HPP
44#define BELOS_GMRES_POLY_SOLMGR_HPP
58#include "Teuchos_as.hpp"
59#ifdef BELOS_TEUCHOS_TIME_MONITOR
60#include "Teuchos_TimeMonitor.hpp"
151template<
class ScalarType,
class MV,
class OP>
155 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
MagnitudeType;
157 typedef Teuchos::ScalarTraits<MagnitudeType>
MTS;
192 const Teuchos::RCP<Teuchos::ParameterList> &pl );
198 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
245 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
268 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
326 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
371 mutable Teuchos::RCP<const Teuchos::ParameterList>
validPL_;
375template<
class ScalarType,
class MV,
class OP>
377 outputStream_ (Teuchos::rcpFromRef(std::cout)),
379 achievedTol_(
MTS::zero()),
380 maxDegree_ (maxDegree_default_),
382 verbosity_ (verbosity_default_),
383 hasOuterSolver_ (false),
384 randomRHS_ (randomRHS_default_),
385 damp_ (dampPoly_default_),
386 addRoots_ (addRoots_default_),
387 polyType_ (polyType_default_),
388 outerSolverType_ (outerSolverType_default_),
389 orthoType_ (orthoType_default_),
391 label_ (label_default_),
397template<
class ScalarType,
class MV,
class OP>
400 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
402 outputStream_ (Teuchos::rcpFromRef(std::cout)),
404 maxDegree_ (maxDegree_default_),
406 verbosity_ (verbosity_default_),
407 hasOuterSolver_ (false),
408 randomRHS_ (randomRHS_default_),
409 damp_ (dampPoly_default_),
410 addRoots_ (addRoots_default_),
411 polyType_ (polyType_default_),
412 outerSolverType_ (outerSolverType_default_),
413 orthoType_ (orthoType_default_),
415 label_ (label_default_),
419 TEUCHOS_TEST_FOR_EXCEPTION(
420 problem_.is_null (), std::invalid_argument,
421 "Belos::GmresPolySolMgr: The given linear problem is null. "
422 "Please call this constructor with a nonnull LinearProblem argument, "
423 "or call the constructor that does not take a LinearProblem.");
427 if (! pl.is_null ()) {
433template<
class ScalarType,
class MV,
class OP>
434Teuchos::RCP<const Teuchos::ParameterList>
437 if (validPL_.is_null ()) {
438 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList ();
442 pl->set(
"Polynomial Type",
static_cast<const char *
>(polyType_default_),
443 "The type of GMRES polynomial that is used as a preconditioner: Roots, Arnoldi, or Gmres.");
445 "The relative residual tolerance that used to construct the GMRES polynomial.");
446 pl->set(
"Maximum Degree",
static_cast<int>(maxDegree_default_),
447 "The maximum degree allowed for any GMRES polynomial.");
448 pl->set(
"Outer Solver",
static_cast<const char *
>(outerSolverType_default_),
449 "The outer solver that this polynomial is used to precondition.");
450 pl->set(
"Outer Solver Params", Teuchos::ParameterList(),
451 "Parameter list for the outer solver.");
452 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
453 "What type(s) of solver information should be outputted\n"
454 "to the output stream.");
455 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
456 "A reference-counted pointer to the output stream where all\n"
457 "solver output is sent.");
458 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
459 "The string to use as a prefix for the timer labels.");
460 pl->set(
"Orthogonalization",
static_cast<const char *
>(orthoType_default_),
461 "The type of orthogonalization to use to generate polynomial: DGKS, ICGS, or IMGS.");
462 pl->set(
"Random RHS",
static_cast<bool>(randomRHS_default_),
463 "Add roots to polynomial for stability.");
464 pl->set(
"Add Roots",
static_cast<bool>(addRoots_default_),
465 "Add roots to polynomial for stability.");
466 pl->set(
"Damp Poly",
static_cast<bool>(dampPoly_default_),
467 "Damp polynomial for ill-conditioned problems.");
474template<
class ScalarType,
class MV,
class OP>
476setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
479 if (params_.is_null ()) {
480 params_ = Teuchos::parameterList (*getValidParameters ());
483 params->validateParameters (*getValidParameters (),0);
487 if (params->isParameter(
"Polynomial Type")) {
488 polyType_ = params->get(
"Polynomial Type", polyType_default_);
492 params_->set(
"Polynomial Type", polyType_);
495 if (params->isParameter(
"Outer Solver")) {
496 outerSolverType_ = params->get(
"Outer Solver", outerSolverType_default_);
500 params_->set(
"Outer Solver", outerSolverType_);
503 if (params->isSublist(
"Outer Solver Params")) {
504 outerParams_ = Teuchos::parameterList( params->get<Teuchos::ParameterList>(
"Outer Solver Params") );
508 if (params->isParameter(
"Maximum Degree")) {
509 maxDegree_ = params->get(
"Maximum Degree",maxDegree_default_);
513 params_->set(
"Maximum Degree", maxDegree_);
516 if (params->isParameter(
"Timer Label")) {
517 std::string tempLabel = params->get(
"Timer Label", label_default_);
520 if (tempLabel != label_) {
522#ifdef BELOS_TEUCHOS_TIME_MONITOR
523 std::string polyLabel = label_ +
": GmresPolyOp creation time";
524 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(polyLabel);
530 params_->set(
"Timer Label", label_);
533 if (params->isParameter(
"Orthogonalization")) {
534 std::string tempOrthoType = params->get(
"Orthogonalization",orthoType_default_);
538 std::ostringstream os;
539 os <<
"Belos::GCRODRSolMgr: Invalid orthogonalization name \""
540 << tempOrthoType <<
"\". The following are valid options "
541 <<
"for the \"Orthogonalization\" name parameter: ";
543 throw std::invalid_argument (os.str());
545 if (tempOrthoType != orthoType_) {
546 orthoType_ = tempOrthoType;
550 params_->set(
"Orthogonalization", orthoType_);
553 if (params->isParameter(
"Verbosity")) {
554 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
555 verbosity_ = params->get(
"Verbosity", verbosity_default_);
557 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
562 params_->set(
"Verbosity", verbosity_);
565 if (params->isParameter(
"Output Stream")) {
566 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
570 params_->set(
"Output Stream", outputStream_);
574 if (params->isParameter(
"Polynomial Tolerance")) {
575 if (params->isType<
MagnitudeType> (
"Polynomial Tolerance")) {
576 polyTol_ = params->get (
"Polynomial Tolerance",
585 params_->set(
"Polynomial Tolerance", polyTol_);
588 if (params->isParameter(
"Random RHS")) {
589 randomRHS_ = params->get(
"Random RHS",randomRHS_default_);
593 params_->set(
"Random RHS", randomRHS_);
597 if (params->isParameter(
"Damped Poly")) {
598 damp_ = params->get(
"Damped Poly",dampPoly_default_);
601 params_->set(
"Damped Poly", damp_);
604 if (params->isParameter(
"Add Roots")) {
605 addRoots_ = params->get(
"Add Roots",addRoots_default_);
609 params_->set(
"Add Roots", addRoots_);
612#ifdef BELOS_TEUCHOS_TIME_MONITOR
613 if (timerPoly_ == Teuchos::null) {
614 std::string polyLabel = label_ +
": GmresPolyOp creation time";
615 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(polyLabel);
620 if (outerSolverType_ !=
"") {
621 hasOuterSolver_ =
true;
629template<
class ScalarType,
class MV,
class OP>
634 using Teuchos::rcp_const_cast;
644 setParameters (Teuchos::parameterList (*getValidParameters ()));
647 TEUCHOS_TEST_FOR_EXCEPTION(
649 "Belos::GmresPolySolMgr::solve: The linear problem has not been set yet, "
650 "or was set to null. Please call setProblem() with a nonnull input before "
653 TEUCHOS_TEST_FOR_EXCEPTION(
655 "Belos::GmresPolySolMgr::solve: The linear problem is not ready. Please "
656 "call setProblem() on the LinearProblem object before calling solve().");
660 if (!poly_dim_ && maxDegree_) {
661#ifdef BELOS_TEUCHOS_TIME_MONITOR
662 Teuchos::TimeMonitor slvtimer(*timerPoly_);
664 poly_Op_ = Teuchos::rcp(
new gmres_poly_t( problem_, params_ ) );
665 poly_dim_ = poly_Op_->polyDegree();
668 "Belos::GmresPolyOp: Failed to generate polynomial that satisfied requirements.");
673 if (hasOuterSolver_ && maxDegree_) {
678 RCP<SolverManager<ScalarType, MultiVec<ScalarType>,
Operator<ScalarType> > > solver = factory.
create( outerSolverType_, outerParams_ );
679 TEUCHOS_TEST_FOR_EXCEPTION( solver == Teuchos::null, std::invalid_argument,
680 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
684 RCP<gmres_poly_mv_t> new_lhs = rcp(
new gmres_poly_mv_t( problem_->getLHS() ) );
685 RCP<gmres_poly_mv_t> new_rhs = rcp(
new gmres_poly_mv_t( rcp_const_cast<MV>( problem_->getRHS() ) ) );
686 RCP<gmres_poly_t> A = rcp(
new gmres_poly_t( problem_ ) );
689 std::string solverLabel = label_ +
": Hybrid Gmres";
690 newProblem->setLabel(solverLabel);
693 if (problem_->getLeftPrec() != Teuchos::null)
694 newProblem->setLeftPrec( poly_Op_ );
696 newProblem->setRightPrec( poly_Op_ );
699 if (problem_->getInitResVec() != Teuchos::null)
700 newProblem->setInitResVec( rcp(
new gmres_poly_mv_t( rcp_const_cast<MV>( problem_->getInitResVec() ) ) ) );
701 newProblem->setProblem();
703 solver->setProblem( newProblem );
705 ret = solver->solve();
706 numIters_ = solver->getNumIters();
707 loaDetected_ = solver->isLOADetected();
708 achievedTol_ = solver->achievedTol();
711 else if (hasOuterSolver_) {
715 RCP<SolverManager<ScalarType, MV, OP> > solver = factory.create( outerSolverType_, outerParams_ );
716 TEUCHOS_TEST_FOR_EXCEPTION( solver == Teuchos::null, std::invalid_argument,
717 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
719 solver->setProblem( problem_ );
721 ret = solver->solve();
722 numIters_ = solver->getNumIters();
723 loaDetected_ = solver->isLOADetected();
724 achievedTol_ = solver->achievedTol();
727 else if (maxDegree_) {
730 poly_Op_->ApplyPoly( *problem_->getRHS(), *problem_->getLHS() );
731 achievedTol_ = MTS::one();
739template<
class ScalarType,
class MV,
class OP>
742 std::ostringstream out;
744 out <<
"\"Belos::GmresPolySolMgr\": {"
745 <<
"ScalarType: " << Teuchos::TypeNameTraits<ScalarType>::name ()
746 <<
", Poly Degree: " << poly_dim_
747 <<
", Poly Max Degree: " << maxDegree_
748 <<
", 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.
Belos's class for applying the GMRES polynomial operator that is used by the hybrid-GMRES linear solv...
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.
static constexpr const char * orthoType_default_
Teuchos::RCP< std::ostream > outputStream_
static constexpr const char * label_default_
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.
static constexpr bool randomRHS_default_
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.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
Teuchos::ScalarTraits< MagnitudeType > MTS
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Get current linear problem being solved for in this object.
virtual ~GmresPolySolMgr()
Destructor.
static constexpr const char * outerSolverType_default_
Teuchos::RCP< Teuchos::ParameterList > outerParams_
GmresPolySolMgr()
Empty constructor for GmresPolySolMgr. This constructor takes no arguments and sets the default value...
MagnitudeType achievedTol_
Belos::GmresPolyOp< ScalarType, MV, OP > gmres_poly_t
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
std::string outerSolverType_
static constexpr int verbosity_default_
Teuchos::RCP< Teuchos::ParameterList > params_
Teuchos::RCP< const Teuchos::ParameterList > validPL_
Cached default (valid) parameters.
Belos::GmresPolyMv< ScalarType, MV > gmres_poly_mv_t
static constexpr bool dampPoly_default_
void reset(const ResetType type) override
Reset the solver.
static constexpr int maxDegree_default_
Teuchos::RCP< Teuchos::Time > timerPoly_
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::RCP< gmres_poly_t > poly_Op_
static constexpr const char * polyType_default_
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
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...
static constexpr bool addRoots_default_
virtual Teuchos::RCP< solver_base_type > create(const std::string &solverName, const Teuchos::RCP< Teuchos::ParameterList > &solverParams)
Create, configure, and return the specified solver.
A linear system to solve, and its associated information.
Interface for multivectors used by Belos' linear solvers.
Alternative run-time polymorphic interface for operators.
Enumeration of all valid Belos (Mat)OrthoManager classes.
std::ostream & printValidNames(std::ostream &out) const
Print all recognized MatOrthoManager names to the given ostream.
bool isValidName(const std::string &name) const
Whether this factory recognizes the MatOrthoManager with the given name.
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.
typename ::Belos::Impl::SolverFactorySelector< SC, MV, OP >::type SolverFactory
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double polyTol
Relative residual tolerance for matrix polynomial construction.