10#ifndef BELOS_TFQMR_SOLMGR_HPP
11#define BELOS_TFQMR_SOLMGR_HPP
32#ifdef BELOS_TEUCHOS_TIME_MONITOR
33#include "Teuchos_TimeMonitor.hpp"
67 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int, ScalarType>>
73 typedef Teuchos::ScalarTraits<ScalarType> SCT;
74 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
75 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
106 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
112 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
137 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
138 return Teuchos::tuple(timerSolve_);
218 bool checkStatusTest();
221 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
224 Teuchos::RCP<OutputManager<ScalarType> > printer_;
225 Teuchos::RCP<std::ostream> outputStream_;
228 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
229 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
230 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > convTest_;
231 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > expConvTest_, impConvTest_;
232 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
235 Teuchos::RCP<Teuchos::ParameterList> params_;
238 static constexpr int maxIters_default_ = 1000;
239 static constexpr bool expResTest_default_ =
false;
242 static constexpr int outputFreq_default_ = -1;
243 static constexpr const char * impResScale_default_ =
"Norm of Preconditioned Initial Residual";
244 static constexpr const char * expResScale_default_ =
"Norm of Initial Residual";
245 static constexpr const char * label_default_ =
"Belos";
248 MagnitudeType convtol_, impTolScale_, achievedTol_;
249 int maxIters_, numIters_;
250 int verbosity_, outputStyle_, outputFreq_;
253 std::string impResScale_, expResScale_;
257 Teuchos::RCP<Teuchos::Time> timerSolve_;
260 bool isSet_, isSTSet_;
265template<
class ScalarType,
class MV,
class OP,
class DM>
271 maxIters_(maxIters_default_),
273 verbosity_(verbosity_default_),
274 outputStyle_(outputStyle_default_),
275 outputFreq_(outputFreq_default_),
277 expResTest_(expResTest_default_),
278 impResScale_(impResScale_default_),
279 expResScale_(expResScale_default_),
280 label_(label_default_),
287template<
class ScalarType,
class MV,
class OP,
class DM>
290 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
296 maxIters_(maxIters_default_),
298 verbosity_(verbosity_default_),
299 outputStyle_(outputStyle_default_),
300 outputFreq_(outputFreq_default_),
302 expResTest_(expResTest_default_),
303 impResScale_(impResScale_default_),
304 expResScale_(expResScale_default_),
305 label_(label_default_),
317template<
class ScalarType,
class MV,
class OP,
class DM>
321 if (params_ == Teuchos::null) {
322 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
325 params->validateParameters(*getValidParameters());
329 if (
params->isParameter(
"Maximum Iterations")) {
330 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
333 params_->set(
"Maximum Iterations", maxIters_);
334 if (maxIterTest_!=Teuchos::null)
335 maxIterTest_->setMaxIters( maxIters_ );
339 if (
params->isParameter(
"Block Size")) {
340 blockSize_ =
params->get(
"Block Size",1);
342 "Belos::TFQMRSolMgr: \"Block Size\" must be 1.");
345 params_->set(
"Block Size", blockSize_);
349 if (
params->isParameter(
"Timer Label")) {
355 params_->set(
"Timer Label", label_);
356 std::string
solveLabel = label_ +
": TFQMRSolMgr total solve time";
357#ifdef BELOS_TEUCHOS_TIME_MONITOR
358 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
364 if (
params->isParameter(
"Verbosity")) {
365 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
366 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
368 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
372 params_->set(
"Verbosity", verbosity_);
373 if (printer_ != Teuchos::null)
374 printer_->setVerbosity(verbosity_);
378 if (
params->isParameter(
"Output Style")) {
379 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
380 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
382 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
386 params_->set(
"Output Style", outputStyle_);
391 if (
params->isParameter(
"Output Stream")) {
392 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
395 params_->set(
"Output Stream", outputStream_);
396 if (printer_ != Teuchos::null)
397 printer_->setOStream( outputStream_ );
402 if (
params->isParameter(
"Output Frequency")) {
403 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
407 params_->set(
"Output Frequency", outputFreq_);
408 if (outputTest_ != Teuchos::null)
409 outputTest_->setOutputFrequency( outputFreq_ );
413 if (printer_ == Teuchos::null) {
418 if (
params->isParameter(
"Convergence Tolerance")) {
419 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
420 convtol_ =
params->get (
"Convergence Tolerance",
428 params_->set(
"Convergence Tolerance", convtol_);
433 if (
params->isParameter(
"Implicit Tolerance Scale Factor")) {
434 if (
params->isType<MagnitudeType> (
"Implicit Tolerance Scale Factor")) {
435 impTolScale_ =
params->get (
"Implicit Tolerance Scale Factor",
440 impTolScale_ =
params->get (
"Implicit Tolerance Scale Factor",
445 params_->set(
"Implicit Tolerance Scale Factor", impTolScale_);
450 if (
params->isParameter(
"Implicit Residual Scaling")) {
451 std::string
tempImpResScale = Teuchos::getParameter<std::string>( *
params,
"Implicit Residual Scaling" );
458 params_->set(
"Implicit Residual Scaling", impResScale_);
465 if (
params->isParameter(
"Explicit Residual Scaling")) {
466 std::string
tempExpResScale = Teuchos::getParameter<std::string>( *
params,
"Explicit Residual Scaling" );
473 params_->set(
"Explicit Residual Scaling", expResScale_);
480 if (
params->isParameter(
"Explicit Residual Test")) {
481 expResTest_ = Teuchos::getParameter<bool>( *
params,
"Explicit Residual Test" );
484 params_->set(
"Explicit Residual Test", expResTest_);
485 if (expConvTest_ == Teuchos::null) {
491 if (timerSolve_ == Teuchos::null) {
492 std::string
solveLabel = label_ +
": TFQMRSolMgr total solve time";
493#ifdef BELOS_TEUCHOS_TIME_MONITOR
494 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
504template<
class ScalarType,
class MV,
class OP,
class DM>
529 convTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
540 expConvTest_ = impConvTest_;
541 convTest_ = impConvTest_;
543 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
562template<
class ScalarType,
class MV,
class OP,
class DM>
563Teuchos::RCP<const Teuchos::ParameterList>
566 static Teuchos::RCP<const Teuchos::ParameterList>
validPL;
570 Teuchos::RCP<Teuchos::ParameterList>
pl = Teuchos::parameterList();
575 "The relative residual tolerance that needs to be achieved by the\n"
576 "iterative solver in order for the linear system to be declared converged.");
578 "The scale factor used by the implicit residual test when explicit residual\n"
579 "testing is used. May enable faster convergence when TFQMR bound is too loose.");
580 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
581 "The maximum number of block iterations allowed for each\n"
582 "set of RHS solved.");
583 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
584 "What type(s) of solver information should be outputted\n"
585 "to the output stream.");
586 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
587 "What style is used for the solver information outputted\n"
588 "to the output stream.");
589 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
590 "How often convergence information should be outputted\n"
591 "to the output stream.");
592 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
593 "A reference-counted pointer to the output stream where all\n"
594 "solver output is sent.");
595 pl->set(
"Explicit Residual Test",
static_cast<bool>(expResTest_default_),
596 "Whether the explicitly computed residual should be used in the convergence test.");
597 pl->set(
"Implicit Residual Scaling",
static_cast<const char *
>(impResScale_default_),
598 "The type of scaling used in the implicit residual convergence test.");
599 pl->set(
"Explicit Residual Scaling",
static_cast<const char *
>(expResScale_default_),
600 "The type of scaling used in the explicit residual convergence test.");
601 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
602 "The string to use as a prefix for the timer labels.");
610template<
class ScalarType,
class MV,
class OP,
class DM>
618 setParameters(Teuchos::parameterList(*getValidParameters()));
622 "Belos::TFQMRSolMgr::solve(): Linear problem is not a valid object.");
625 "Belos::TFQMRSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
629 "Belos::TFQMRSolMgr::solve(): Linear problem and requested status tests are incompatible.");
634 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
646 problem_->setLSIndex(
currIdx );
650 Teuchos::ParameterList
plist;
651 plist.set(
"Block Size",blockSize_);
654 outputTest_->reset();
662 Teuchos::RCP<TFQMRIter<ScalarType,MV,OP,DM> >
tfqmr_iter =
667#ifdef BELOS_TEUCHOS_TIME_MONITOR
668 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
682 outputTest_->resetNumCalls();
685 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())),
currIdx );
703 if ( convTest_->getStatus() ==
Passed ) {
712 else if ( maxIterTest_->getStatus() ==
Passed ) {
729 "Belos::TFQMRSolMgr::solve(): Invalid return from TFQMRIter::iterate().");
735 achievedTol_ = MT::one();
736 Teuchos::RCP<MV>
X = problem_->getLHS();
737 MVT::MvInit( *
X, SCT::zero() );
738 printer_->stream(
Warnings) <<
"Belos::TFQMRSolMgr::solve(): Warning! NaN has been detected!"
742 catch (
const std::exception &
e) {
744 printer_->stream(
Errors) <<
"Error! Caught std::exception in TFQMRIter::iterate() at iteration "
746 <<
e.what() << std::endl;
752 problem_->updateSolution(
tfqmr_iter->getCurrentUpdate(),
true );
755 problem_->setCurrLS();
768 problem_->setLSIndex(
currIdx );
785#ifdef BELOS_TEUCHOS_TIME_MONITOR
790 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
794 numIters_ = maxIterTest_->getNumIters();
819 "Belos::TFQMRSolMgr::solve(): The implicit convergence test's "
820 "getTestValue() method returned NULL. Please report this bug to the "
821 "Belos developers.");
823 "Belos::TMQMRSolMgr::solve(): The implicit convergence test's "
824 "getTestValue() method returned a vector of length zero. Please report "
825 "this bug to the Belos developers.");
840template<
class ScalarType,
class MV,
class OP,
class DM>
843 std::ostringstream
oss;
844 oss <<
"Belos::TFQMRSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
Belos header file which uses auto-configuration information to include necessary C++ headers.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
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...
Full specialization of Belos::DenseMatTraits for Teuchos::SerialDenseMatrix with ordinal type int and...
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 ...
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
bool isLOADetected() const override
Whether loss of accuracy was detected during the last solve() invocation.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
std::string description() const override
Method to return description of the TFQMR solver manager.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
TFQMRSolMgr()
Empty constructor for TFQMRSolMgr. This constructor takes no arguments and sets the default values fo...
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
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.
virtual ~TFQMRSolMgr()
Destructor.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
TFQMRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
TFQMRSolMgrLinearProblemFailure(const std::string &what_arg)
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.