10 #ifndef BELOS_TFQMR_SOLMGR_HPP
11 #define BELOS_TFQMR_SOLMGR_HPP
29 #ifdef BELOS_TEUCHOS_TIME_MONITOR
64 template<
class ScalarType,
class MV,
class OP>
135 return Teuchos::tuple(timerSolve_);
215 bool checkStatusTest();
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_;
257 bool isSet_, isSTSet_;
262 template<
class ScalarType,
class MV,
class OP>
264 outputStream_(Teuchos::rcpFromRef(std::cout)),
267 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
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_),
284 template<
class ScalarType,
class MV,
class OP>
289 outputStream_(Teuchos::rcpFromRef(std::cout)),
292 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
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_),
314 template<
class ScalarType,
class MV,
class OP>
318 if (params_ == Teuchos::null) {
327 maxIters_ = params->
get(
"Maximum Iterations",maxIters_default_);
330 params_->set(
"Maximum Iterations", maxIters_);
331 if (maxIterTest_!=Teuchos::null)
332 maxIterTest_->setMaxIters( maxIters_ );
337 blockSize_ = params->
get(
"Block Size",1);
339 "Belos::TFQMRSolMgr: \"Block Size\" must be 1.");
342 params_->set(
"Block Size", blockSize_);
347 std::string tempLabel = params->
get(
"Timer Label", label_default_);
350 if (tempLabel != label_) {
352 params_->set(
"Timer Label", label_);
353 std::string solveLabel = label_ +
": TFQMRSolMgr total solve time";
354 #ifdef BELOS_TEUCHOS_TIME_MONITOR
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_);
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_);
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_ );
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" );
451 if (impResScale_ != tempImpResScale) {
452 impResScale_ = tempImpResScale;
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" );
466 if (expResScale_ != tempExpResScale) {
467 expResScale_ = tempExpResScale;
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
501 template<
class ScalarType,
class MV,
class OP>
514 Teuchos::rcp(
new StatusTestGenResNorm_t( impTolScale_*convtol_ ) );
516 impConvTest_ = tmpImpConvTest;
521 tmpExpConvTest->defineResForm( StatusTestGenResNorm_t::Explicit,
Belos::TwoNorm );
523 expConvTest_ = tmpExpConvTest;
526 convTest_ =
Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
534 impConvTest_ = tmpImpConvTest;
537 expConvTest_ = impConvTest_;
538 convTest_ = impConvTest_;
540 sTest_ =
Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
544 StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
548 std::string solverDesc =
" TFQMR ";
549 outputTest_->setSolverDesc( solverDesc );
559 template<
class ScalarType,
class MV,
class OP>
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.");
607 template<
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()) );
631 int numCurrRHS = blockSize_;
633 std::vector<int> currIdx, currIdx2;
636 currIdx.resize( blockSize_ );
637 currIdx2.resize( blockSize_ );
638 for (
int i=0; i<numCurrRHS; ++i)
639 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
642 problem_->setLSIndex( currIdx );
647 plist.
set(
"Block Size",blockSize_);
650 outputTest_->reset();
653 bool isConverged =
true;
663 #ifdef BELOS_TEUCHOS_TIME_MONITOR
667 while ( numRHS2Solve > 0 ) {
670 std::vector<int> convRHSIdx;
671 std::vector<int> currRHSIdx( currIdx );
672 currRHSIdx.resize(numCurrRHS);
675 tfqmr_iter->resetNumIters();
678 outputTest_->resetNumCalls();
681 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())), currIdx );
686 tfqmr_iter->initializeTFQMR(newstate);
692 tfqmr_iter->iterate();
699 if ( convTest_->getStatus() ==
Passed ) {
708 else if ( maxIterTest_->getStatus() ==
Passed ) {
723 "Belos::TFQMRSolMgr::solve(): Invalid return from TFQMRIter::iterate().");
728 achievedTol_ = MT::one();
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 "
737 << tfqmr_iter->getNumIters() << std::endl
738 << e.what() << std::endl;
744 problem_->updateSolution( tfqmr_iter->getCurrentUpdate(), true );
747 problem_->setCurrLS();
750 startPtr += numCurrRHS;
751 numRHS2Solve -= numCurrRHS;
752 if ( numRHS2Solve > 0 ) {
753 numCurrRHS = blockSize_;
755 currIdx.resize( blockSize_ );
756 currIdx2.resize( blockSize_ );
757 for (
int i=0; i<numCurrRHS; ++i)
758 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
760 problem_->setLSIndex( currIdx );
763 tfqmr_iter->setBlockSize( blockSize_ );
766 currIdx.resize( numRHS2Solve );
777 #ifdef BELOS_TEUCHOS_TIME_MONITOR
786 numIters_ = maxIterTest_->getNumIters();
799 const std::vector<MagnitudeType>* pTestValues = NULL;
801 pTestValues = expConvTest_->getTestValue();
802 if (pTestValues == NULL || pTestValues->size() < 1) {
803 pTestValues = impConvTest_->getTestValue();
808 pTestValues = impConvTest_->getTestValue();
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.");
822 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
832 template<
class ScalarType,
class MV,
class OP>
835 std::ostringstream oss;
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Collection of types and exceptions used within the Belos solvers.
This class implements the preconditioned transpose-free QMR algorithm for solving non-Hermitian linea...
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
Class which manages the output and verbosity of the Belos solvers.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
bool is_null(const boost::shared_ptr< T > &p)
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
bool isLOADetected() const override
Whether loss of accuracy was detected during the last solve() invocation.
T & get(const std::string &name, T def_value)
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
An implementation of StatusTestResNorm using a family of residual norms.
Belos concrete class for generating iterations with the preconditioned tranpose-free QMR (TFQMR) meth...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Teuchos::RCP< const MV > R
The current residual basis.
static const double convTol
Default convergence tolerance.
Belos::StatusTest class for specifying a maximum number of iterations.
std::string description() const override
Method to return description of the TFQMR solver manager.
static std::string name()
A factory class for generating StatusTestOutput objects.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
bool isParameter(const std::string &name) const
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
static const double impTolScale
"Implicit Tolerance Scale Factor"
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
A linear system to solve, and its associated information.
Structure to contain pointers to TFQMRIter state variables.
Class which describes the linear problem to be solved by the iterative solver.
TFQMRSolMgr()
Empty constructor for TFQMRSolMgr. This constructor takes no arguments and sets the default values fo...
virtual ~TFQMRSolMgr()
Destructor.
ReturnType
Whether the Belos solve converged for all linear systems.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
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...
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
bool isType(const std::string &name) const
A class for extending the status testing capabilities of Belos via logical combinations.
TFQMRSolMgrLinearProblemFailure(const std::string &what_arg)
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
Default parameters common to most Belos solvers.
TFQMRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i.e.
Belos header file which uses auto-configuration information to include necessary C++ headers...
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.