10#ifndef BELOS_TFQMR_SOLMGR_HPP
11#define BELOS_TFQMR_SOLMGR_HPP
29#ifdef BELOS_TEUCHOS_TIME_MONITOR
30#include "Teuchos_TimeMonitor.hpp"
64 template<
class ScalarType,
class MV,
class OP>
70 typedef Teuchos::ScalarTraits<ScalarType> SCT;
71 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
72 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
103 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
109 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
134 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
135 return Teuchos::tuple(timerSolve_);
215 bool checkStatusTest();
218 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
221 Teuchos::RCP<OutputManager<ScalarType> > printer_;
222 Teuchos::RCP<std::ostream> outputStream_;
225 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
226 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
227 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
228 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_, impConvTest_;
229 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
232 Teuchos::RCP<Teuchos::ParameterList> params_;
235 static constexpr int maxIters_default_ = 1000;
236 static constexpr bool expResTest_default_ =
false;
239 static constexpr int outputFreq_default_ = -1;
240 static constexpr const char * impResScale_default_ =
"Norm of Preconditioned Initial Residual";
241 static constexpr const char * expResScale_default_ =
"Norm of Initial Residual";
242 static constexpr const char * label_default_ =
"Belos";
245 MagnitudeType convtol_, impTolScale_, achievedTol_;
246 int maxIters_, numIters_;
247 int verbosity_, outputStyle_, outputFreq_;
250 std::string impResScale_, expResScale_;
254 Teuchos::RCP<Teuchos::Time> timerSolve_;
257 bool isSet_, isSTSet_;
262template<
class ScalarType,
class MV,
class OP>
268 maxIters_(maxIters_default_),
270 verbosity_(verbosity_default_),
271 outputStyle_(outputStyle_default_),
272 outputFreq_(outputFreq_default_),
274 expResTest_(expResTest_default_),
275 impResScale_(impResScale_default_),
276 expResScale_(expResScale_default_),
277 label_(label_default_),
284template<
class ScalarType,
class MV,
class OP>
287 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
293 maxIters_(maxIters_default_),
295 verbosity_(verbosity_default_),
296 outputStyle_(outputStyle_default_),
297 outputFreq_(outputFreq_default_),
299 expResTest_(expResTest_default_),
300 impResScale_(impResScale_default_),
301 expResScale_(expResScale_default_),
302 label_(label_default_),
314template<
class ScalarType,
class MV,
class OP>
318 if (params_ == Teuchos::null) {
319 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
322 params->validateParameters(*getValidParameters());
326 if (
params->isParameter(
"Maximum Iterations")) {
327 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
330 params_->set(
"Maximum Iterations", maxIters_);
331 if (maxIterTest_!=Teuchos::null)
332 maxIterTest_->setMaxIters( maxIters_ );
336 if (
params->isParameter(
"Block Size")) {
337 blockSize_ =
params->get(
"Block Size",1);
339 "Belos::TFQMRSolMgr: \"Block Size\" must be 1.");
342 params_->set(
"Block Size", blockSize_);
346 if (
params->isParameter(
"Timer Label")) {
352 params_->set(
"Timer Label", label_);
353 std::string
solveLabel = label_ +
": TFQMRSolMgr total solve time";
354#ifdef BELOS_TEUCHOS_TIME_MONITOR
355 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
361 if (
params->isParameter(
"Verbosity")) {
362 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
363 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
365 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
369 params_->set(
"Verbosity", verbosity_);
370 if (printer_ != Teuchos::null)
371 printer_->setVerbosity(verbosity_);
375 if (
params->isParameter(
"Output Style")) {
376 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
377 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
379 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
383 params_->set(
"Output Style", outputStyle_);
388 if (
params->isParameter(
"Output Stream")) {
389 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
392 params_->set(
"Output Stream", outputStream_);
393 if (printer_ != Teuchos::null)
394 printer_->setOStream( outputStream_ );
399 if (
params->isParameter(
"Output Frequency")) {
400 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
404 params_->set(
"Output Frequency", outputFreq_);
405 if (outputTest_ != Teuchos::null)
406 outputTest_->setOutputFrequency( outputFreq_ );
410 if (printer_ == Teuchos::null) {
415 if (
params->isParameter(
"Convergence Tolerance")) {
416 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
417 convtol_ =
params->get (
"Convergence Tolerance",
425 params_->set(
"Convergence Tolerance", convtol_);
430 if (
params->isParameter(
"Implicit Tolerance Scale Factor")) {
431 if (
params->isType<MagnitudeType> (
"Implicit Tolerance Scale Factor")) {
432 impTolScale_ =
params->get (
"Implicit Tolerance Scale Factor",
437 impTolScale_ =
params->get (
"Implicit Tolerance Scale Factor",
442 params_->set(
"Implicit Tolerance Scale Factor", impTolScale_);
447 if (
params->isParameter(
"Implicit Residual Scaling")) {
448 std::string
tempImpResScale = Teuchos::getParameter<std::string>( *
params,
"Implicit Residual Scaling" );
455 params_->set(
"Implicit Residual Scaling", impResScale_);
462 if (
params->isParameter(
"Explicit Residual Scaling")) {
463 std::string
tempExpResScale = Teuchos::getParameter<std::string>( *
params,
"Explicit Residual Scaling" );
470 params_->set(
"Explicit Residual Scaling", expResScale_);
477 if (
params->isParameter(
"Explicit Residual Test")) {
478 expResTest_ = Teuchos::getParameter<bool>( *
params,
"Explicit Residual Test" );
481 params_->set(
"Explicit Residual Test", expResTest_);
482 if (expConvTest_ == Teuchos::null) {
488 if (timerSolve_ == Teuchos::null) {
489 std::string
solveLabel = label_ +
": TFQMRSolMgr total solve time";
490#ifdef BELOS_TEUCHOS_TIME_MONITOR
491 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
501template<
class ScalarType,
class MV,
class OP>
526 convTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
537 expConvTest_ = impConvTest_;
538 convTest_ = impConvTest_;
540 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
559template<
class ScalarType,
class MV,
class OP>
560Teuchos::RCP<const Teuchos::ParameterList>
563 static Teuchos::RCP<const Teuchos::ParameterList>
validPL;
567 Teuchos::RCP<Teuchos::ParameterList>
pl = Teuchos::parameterList();
572 "The relative residual tolerance that needs to be achieved by the\n"
573 "iterative solver in order for the linear system to be declared converged.");
575 "The scale factor used by the implicit residual test when explicit residual\n"
576 "testing is used. May enable faster convergence when TFQMR bound is too loose.");
577 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
578 "The maximum number of block iterations allowed for each\n"
579 "set of RHS solved.");
580 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
581 "What type(s) of solver information should be outputted\n"
582 "to the output stream.");
583 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
584 "What style is used for the solver information outputted\n"
585 "to the output stream.");
586 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
587 "How often convergence information should be outputted\n"
588 "to the output stream.");
589 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
590 "A reference-counted pointer to the output stream where all\n"
591 "solver output is sent.");
592 pl->set(
"Explicit Residual Test",
static_cast<bool>(expResTest_default_),
593 "Whether the explicitly computed residual should be used in the convergence test.");
594 pl->set(
"Implicit Residual Scaling",
static_cast<const char *
>(impResScale_default_),
595 "The type of scaling used in the implicit residual convergence test.");
596 pl->set(
"Explicit Residual Scaling",
static_cast<const char *
>(expResScale_default_),
597 "The type of scaling used in the explicit residual convergence test.");
598 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
599 "The string to use as a prefix for the timer labels.");
607template<
class ScalarType,
class MV,
class OP>
614 setParameters(Teuchos::parameterList(*getValidParameters()));
618 "Belos::TFQMRSolMgr::solve(): Linear problem is not a valid object.");
621 "Belos::TFQMRSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
625 "Belos::TFQMRSolMgr::solve(): Linear problem and requested status tests are incompatible.");
630 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
642 problem_->setLSIndex(
currIdx );
646 Teuchos::ParameterList
plist;
647 plist.set(
"Block Size",blockSize_);
650 outputTest_->reset();
658 Teuchos::RCP<TFQMRIter<ScalarType,MV,OP> >
tfqmr_iter =
663#ifdef BELOS_TEUCHOS_TIME_MONITOR
664 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
678 outputTest_->resetNumCalls();
681 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())),
currIdx );
699 if ( convTest_->getStatus() ==
Passed ) {
708 else if ( maxIterTest_->getStatus() ==
Passed ) {
723 "Belos::TFQMRSolMgr::solve(): Invalid return from TFQMRIter::iterate().");
728 achievedTol_ = MT::one();
729 Teuchos::RCP<MV>
X = problem_->getLHS();
730 MVT::MvInit( *
X, SCT::zero() );
731 printer_->stream(
Warnings) <<
"Belos::TFQMRSolMgr::solve(): Warning! NaN has been detected!"
735 catch (
const std::exception &
e) {
736 printer_->stream(
Errors) <<
"Error! Caught std::exception in TFQMRIter::iterate() at iteration "
738 <<
e.what() << std::endl;
744 problem_->updateSolution(
tfqmr_iter->getCurrentUpdate(),
true );
747 problem_->setCurrLS();
760 problem_->setLSIndex(
currIdx );
777#ifdef BELOS_TEUCHOS_TIME_MONITOR
782 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
786 numIters_ = maxIterTest_->getNumIters();
811 "Belos::TFQMRSolMgr::solve(): The implicit convergence test's "
812 "getTestValue() method returned NULL. Please report this bug to the "
813 "Belos developers.");
815 "Belos::TMQMRSolMgr::solve(): The implicit convergence test's "
816 "getTestValue() method returned a vector of length zero. Please report "
817 "this bug to the Belos developers.");
832template<
class ScalarType,
class MV,
class OP>
835 std::ostringstream
oss;
836 oss <<
"Belos::TFQMRSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class which describes the basic interface for a solver manager.
Belos::StatusTest for logically combining several status tests.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Belos concrete class for generating iterations with the preconditioned tranpose-free QMR (TFQMR) meth...
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
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 ...
TFQMRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
TFQMRSolMgrLinearProblemFailure(const std::string &what_arg)
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
virtual ~TFQMRSolMgr()
Destructor.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
std::string description() const override
Method to return description of the TFQMR solver manager.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
TFQMRSolMgr()
Empty constructor for TFQMRSolMgr. This constructor takes no arguments and sets the default values fo...
bool isLOADetected() const override
Whether loss of accuracy was detected during the last solve() invocation.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
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 impTolScale
"Implicit Tolerance Scale Factor"
static const double convTol
Default convergence tolerance.