42 #ifndef BELOS_TFQMR_SOLMGR_HPP 43 #define BELOS_TFQMR_SOLMGR_HPP 61 #ifdef BELOS_TEUCHOS_TIME_MONITOR 62 #include "Teuchos_TimeMonitor.hpp" 93 template<
class ScalarType,
class MV,
class OP>
99 typedef Teuchos::ScalarTraits<ScalarType> SCT;
100 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
101 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
132 const Teuchos::RCP<Teuchos::ParameterList> &pl );
138 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
163 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
164 return Teuchos::tuple(timerSolve_);
198 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
244 bool checkStatusTest();
247 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
250 Teuchos::RCP<OutputManager<ScalarType> > printer_;
251 Teuchos::RCP<std::ostream> outputStream_;
254 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
255 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
256 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
257 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_, impConvTest_;
258 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
261 Teuchos::RCP<Teuchos::ParameterList> params_;
264 static constexpr
int maxIters_default_ = 1000;
265 static constexpr
bool expResTest_default_ =
false;
268 static constexpr
int outputFreq_default_ = -1;
269 static constexpr
const char * impResScale_default_ =
"Norm of Preconditioned Initial Residual";
270 static constexpr
const char * expResScale_default_ =
"Norm of Initial Residual";
271 static constexpr
const char * label_default_ =
"Belos";
273 #if defined(_WIN32) && defined(__clang__) 274 static constexpr std::ostream * outputStream_default_ =
275 __builtin_constant_p(reinterpret_cast<const std::ostream*>(&std::cout));
277 static constexpr std::ostream * outputStream_default_ = &std::cout;
281 MagnitudeType convtol_, impTolScale_, achievedTol_;
282 int maxIters_, numIters_;
283 int verbosity_, outputStyle_, outputFreq_;
286 std::string impResScale_, expResScale_;
290 Teuchos::RCP<Teuchos::Time> timerSolve_;
293 bool isSet_, isSTSet_;
298 template<
class ScalarType,
class MV,
class OP>
300 outputStream_(Teuchos::rcp(outputStream_default_,false)),
303 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
304 maxIters_(maxIters_default_),
306 verbosity_(verbosity_default_),
307 outputStyle_(outputStyle_default_),
308 outputFreq_(outputFreq_default_),
310 expResTest_(expResTest_default_),
311 impResScale_(impResScale_default_),
312 expResScale_(expResScale_default_),
313 label_(label_default_),
320 template<
class ScalarType,
class MV,
class OP>
323 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
325 outputStream_(Teuchos::rcp(outputStream_default_,false)),
328 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
329 maxIters_(maxIters_default_),
331 verbosity_(verbosity_default_),
332 outputStyle_(outputStyle_default_),
333 outputFreq_(outputFreq_default_),
335 expResTest_(expResTest_default_),
336 impResScale_(impResScale_default_),
337 expResScale_(expResScale_default_),
338 label_(label_default_),
342 TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument,
"Problem not given to solver manager.");
345 if ( !is_null(pl) ) {
350 template<
class ScalarType,
class MV,
class OP>
354 if (params_ == Teuchos::null) {
355 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
358 params->validateParameters(*getValidParameters());
362 if (params->isParameter(
"Maximum Iterations")) {
363 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
366 params_->set(
"Maximum Iterations", maxIters_);
367 if (maxIterTest_!=Teuchos::null)
368 maxIterTest_->setMaxIters( maxIters_ );
372 if (params->isParameter(
"Block Size")) {
373 blockSize_ = params->get(
"Block Size",1);
374 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ != 1, std::invalid_argument,
375 "Belos::TFQMRSolMgr: \"Block Size\" must be 1.");
378 params_->set(
"Block Size", blockSize_);
382 if (params->isParameter(
"Timer Label")) {
383 std::string tempLabel = params->get(
"Timer Label", label_default_);
386 if (tempLabel != label_) {
388 params_->set(
"Timer Label", label_);
389 std::string solveLabel = label_ +
": TFQMRSolMgr total solve time";
390 #ifdef BELOS_TEUCHOS_TIME_MONITOR 391 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
397 if (params->isParameter(
"Verbosity")) {
398 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
399 verbosity_ = params->get(
"Verbosity", verbosity_default_);
401 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
405 params_->set(
"Verbosity", verbosity_);
406 if (printer_ != Teuchos::null)
407 printer_->setVerbosity(verbosity_);
411 if (params->isParameter(
"Output Style")) {
412 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
413 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
415 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
419 params_->set(
"Output Style", outputStyle_);
424 if (params->isParameter(
"Output Stream")) {
425 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
428 params_->set(
"Output Stream", outputStream_);
429 if (printer_ != Teuchos::null)
430 printer_->setOStream( outputStream_ );
435 if (params->isParameter(
"Output Frequency")) {
436 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
440 params_->set(
"Output Frequency", outputFreq_);
441 if (outputTest_ != Teuchos::null)
442 outputTest_->setOutputFrequency( outputFreq_ );
446 if (printer_ == Teuchos::null) {
451 if (params->isParameter(
"Convergence Tolerance")) {
452 if (params->isType<MagnitudeType> (
"Convergence Tolerance")) {
453 convtol_ = params->get (
"Convergence Tolerance",
461 params_->set(
"Convergence Tolerance", convtol_);
466 if (params->isParameter(
"Implicit Tolerance Scale Factor")) {
467 if (params->isType<MagnitudeType> (
"Implicit Tolerance Scale Factor")) {
468 impTolScale_ = params->get (
"Implicit Tolerance Scale Factor",
473 impTolScale_ = params->get (
"Implicit Tolerance Scale Factor",
478 params_->set(
"Implicit Tolerance Scale Factor", impTolScale_);
483 if (params->isParameter(
"Implicit Residual Scaling")) {
484 std::string tempImpResScale = Teuchos::getParameter<std::string>( *params,
"Implicit Residual Scaling" );
487 if (impResScale_ != tempImpResScale) {
488 impResScale_ = tempImpResScale;
491 params_->set(
"Implicit Residual Scaling", impResScale_);
498 if (params->isParameter(
"Explicit Residual Scaling")) {
499 std::string tempExpResScale = Teuchos::getParameter<std::string>( *params,
"Explicit Residual Scaling" );
502 if (expResScale_ != tempExpResScale) {
503 expResScale_ = tempExpResScale;
506 params_->set(
"Explicit Residual Scaling", expResScale_);
513 if (params->isParameter(
"Explicit Residual Test")) {
514 expResTest_ = Teuchos::getParameter<bool>( *params,
"Explicit Residual Test" );
517 params_->set(
"Explicit Residual Test", expResTest_);
518 if (expConvTest_ == Teuchos::null) {
524 if (timerSolve_ == Teuchos::null) {
525 std::string solveLabel = label_ +
": TFQMRSolMgr total solve time";
526 #ifdef BELOS_TEUCHOS_TIME_MONITOR 527 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
537 template<
class ScalarType,
class MV,
class OP>
549 Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
550 Teuchos::rcp(
new StatusTestGenResNorm_t( impTolScale_*convtol_ ) );
552 impConvTest_ = tmpImpConvTest;
555 Teuchos::RCP<StatusTestGenResNorm_t> tmpExpConvTest =
556 Teuchos::rcp(
new StatusTestGenResNorm_t( convtol_ ) );
557 tmpExpConvTest->defineResForm( StatusTestGenResNorm_t::Explicit,
Belos::TwoNorm );
559 expConvTest_ = tmpExpConvTest;
562 convTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
567 Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
568 Teuchos::rcp(
new StatusTestGenResNorm_t( convtol_ ) );
570 impConvTest_ = tmpImpConvTest;
573 expConvTest_ = impConvTest_;
574 convTest_ = impConvTest_;
576 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
580 StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
584 std::string solverDesc =
" TFQMR ";
585 outputTest_->setSolverDesc( solverDesc );
595 template<
class ScalarType,
class MV,
class OP>
596 Teuchos::RCP<const Teuchos::ParameterList>
599 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
602 if(is_null(validPL)) {
603 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
608 "The relative residual tolerance that needs to be achieved by the\n" 609 "iterative solver in order for the linear system to be declared converged.");
611 "The scale factor used by the implicit residual test when explicit residual\n" 612 "testing is used. May enable faster convergence when TFQMR bound is too loose.");
613 pl->set(
"Maximum Iterations", static_cast<int>(maxIters_default_),
614 "The maximum number of block iterations allowed for each\n" 615 "set of RHS solved.");
616 pl->set(
"Verbosity", static_cast<int>(verbosity_default_),
617 "What type(s) of solver information should be outputted\n" 618 "to the output stream.");
619 pl->set(
"Output Style", static_cast<int>(outputStyle_default_),
620 "What style is used for the solver information outputted\n" 621 "to the output stream.");
622 pl->set(
"Output Frequency", static_cast<int>(outputFreq_default_),
623 "How often convergence information should be outputted\n" 624 "to the output stream.");
625 pl->set(
"Output Stream", Teuchos::rcp(outputStream_default_,
false),
626 "A reference-counted pointer to the output stream where all\n" 627 "solver output is sent.");
628 pl->set(
"Explicit Residual Test", static_cast<bool>(expResTest_default_),
629 "Whether the explicitly computed residual should be used in the convergence test.");
630 pl->set(
"Implicit Residual Scaling", static_cast<const char *>(impResScale_default_),
631 "The type of scaling used in the implicit residual convergence test.");
632 pl->set(
"Explicit Residual Scaling", static_cast<const char *>(expResScale_default_),
633 "The type of scaling used in the explicit residual convergence test.");
634 pl->set(
"Timer Label", static_cast<const char *>(label_default_),
635 "The string to use as a prefix for the timer labels.");
643 template<
class ScalarType,
class MV,
class OP>
650 setParameters(Teuchos::parameterList(*getValidParameters()));
654 "Belos::TFQMRSolMgr::solve(): Linear problem is not a valid object.");
657 "Belos::TFQMRSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
661 "Belos::TFQMRSolMgr::solve(): Linear problem and requested status tests are incompatible.");
666 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
667 int numCurrRHS = blockSize_;
669 std::vector<int> currIdx, currIdx2;
672 currIdx.resize( blockSize_ );
673 currIdx2.resize( blockSize_ );
674 for (
int i=0; i<numCurrRHS; ++i)
675 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
678 problem_->setLSIndex( currIdx );
682 Teuchos::ParameterList plist;
683 plist.set(
"Block Size",blockSize_);
686 outputTest_->reset();
689 bool isConverged =
true;
694 Teuchos::RCP<TFQMRIter<ScalarType,MV,OP> > tfqmr_iter =
699 #ifdef BELOS_TEUCHOS_TIME_MONITOR 700 Teuchos::TimeMonitor slvtimer(*timerSolve_);
703 while ( numRHS2Solve > 0 ) {
706 std::vector<int> convRHSIdx;
707 std::vector<int> currRHSIdx( currIdx );
708 currRHSIdx.resize(numCurrRHS);
711 tfqmr_iter->resetNumIters();
714 outputTest_->resetNumCalls();
717 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())), currIdx );
722 tfqmr_iter->initializeTFQMR(newstate);
728 tfqmr_iter->iterate();
735 if ( convTest_->getStatus() ==
Passed ) {
744 else if ( maxIterTest_->getStatus() ==
Passed ) {
758 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
759 "Belos::TFQMRSolMgr::solve(): Invalid return from TFQMRIter::iterate().");
762 catch (
const std::exception &e) {
763 printer_->stream(
Errors) <<
"Error! Caught std::exception in TFQMRIter::iterate() at iteration " 764 << tfqmr_iter->getNumIters() << std::endl
765 << e.what() << std::endl;
771 problem_->updateSolution( tfqmr_iter->getCurrentUpdate(), true );
774 problem_->setCurrLS();
777 startPtr += numCurrRHS;
778 numRHS2Solve -= numCurrRHS;
779 if ( numRHS2Solve > 0 ) {
780 numCurrRHS = blockSize_;
782 currIdx.resize( blockSize_ );
783 currIdx2.resize( blockSize_ );
784 for (
int i=0; i<numCurrRHS; ++i)
785 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
787 problem_->setLSIndex( currIdx );
790 tfqmr_iter->setBlockSize( blockSize_ );
793 currIdx.resize( numRHS2Solve );
804 #ifdef BELOS_TEUCHOS_TIME_MONITOR 809 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
813 numIters_ = maxIterTest_->getNumIters();
826 const std::vector<MagnitudeType>* pTestValues = NULL;
828 pTestValues = expConvTest_->getTestValue();
829 if (pTestValues == NULL || pTestValues->size() < 1) {
830 pTestValues = impConvTest_->getTestValue();
835 pTestValues = impConvTest_->getTestValue();
837 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
838 "Belos::TFQMRSolMgr::solve(): The implicit convergence test's " 839 "getTestValue() method returned NULL. Please report this bug to the " 840 "Belos developers.");
841 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
842 "Belos::TMQMRSolMgr::solve(): The implicit convergence test's " 843 "getTestValue() method returned a vector of length zero. Please report " 844 "this bug to the Belos developers.");
849 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
859 template<
class ScalarType,
class MV,
class OP>
862 std::ostringstream oss;
863 oss <<
"Belos::TFQMRSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
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 isLOADetected() const override
Whether loss of accuracy was detected during the last solve() invocation.
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...
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.
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.
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.
Pure virtual base class which describes the basic interface for a solver manager. ...
static const double impTolScale
"Implicit Tolerance Scale Factor"
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...
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.
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.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
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.