10#ifndef BELOS_BICGSTAB_SOLMGR_HPP
11#define BELOS_BICGSTAB_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;
96 const Teuchos::RCP<Teuchos::ParameterList> &pl );
102 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
127 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
128 return Teuchos::tuple(timerSolve_);
165 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
212 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
215 Teuchos::RCP<OutputManager<ScalarType> > printer_;
216 Teuchos::RCP<std::ostream> outputStream_;
219 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
220 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
221 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
222 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
225 Teuchos::RCP<Teuchos::ParameterList> params_;
232 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
235 static constexpr int maxIters_default_ = 1000;
236 static constexpr bool showMaxResNormOnly_default_ =
false;
239 static constexpr int outputFreq_default_ = -1;
240 static constexpr int defQuorum_default_ = 1;
241 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
242 static constexpr const char * label_default_ =
"Belos";
245 MagnitudeType convtol_,achievedTol_;
246 int maxIters_, numIters_;
247 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
248 bool showMaxResNormOnly_;
249 std::string resScale_;
253 Teuchos::RCP<Teuchos::Time> timerSolve_;
260template<
class ScalarType,
class MV,
class OP>
262 outputStream_(Teuchos::rcpFromRef(std::cout)),
264 maxIters_(maxIters_default_),
266 verbosity_(verbosity_default_),
267 outputStyle_(outputStyle_default_),
268 outputFreq_(outputFreq_default_),
269 defQuorum_(defQuorum_default_),
270 showMaxResNormOnly_(showMaxResNormOnly_default_),
271 resScale_(resScale_default_),
272 label_(label_default_),
277template<
class ScalarType,
class MV,
class OP>
280 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
282 outputStream_(Teuchos::rcpFromRef(std::cout)),
284 maxIters_(maxIters_default_),
286 verbosity_(verbosity_default_),
287 outputStyle_(outputStyle_default_),
288 outputFreq_(outputFreq_default_),
289 defQuorum_(defQuorum_default_),
290 showMaxResNormOnly_(showMaxResNormOnly_default_),
291 resScale_(resScale_default_),
292 label_(label_default_),
295 TEUCHOS_TEST_FOR_EXCEPTION(
296 problem_.is_null (), std::invalid_argument,
297 "Belos::BiCGStabSolMgr two-argument constructor: "
298 "'problem' is null. You must supply a non-null Belos::LinearProblem "
299 "instance when calling this constructor.");
301 if (! pl.is_null ()) {
307template<
class ScalarType,
class MV,
class OP>
310 using Teuchos::ParameterList;
311 using Teuchos::parameterList;
317 if (params_.is_null()) {
318 params_ = parameterList (*defaultParams);
320 params->validateParameters (*defaultParams);
324 if (params->isParameter(
"Maximum Iterations")) {
325 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
328 params_->set(
"Maximum Iterations", maxIters_);
329 if (maxIterTest_!=Teuchos::null)
330 maxIterTest_->setMaxIters( maxIters_ );
334 if (params->isParameter(
"Timer Label")) {
335 std::string tempLabel = params->get(
"Timer Label", label_default_);
338 if (tempLabel != label_) {
340 params_->set(
"Timer Label", label_);
341 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
342#ifdef BELOS_TEUCHOS_TIME_MONITOR
343 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
349 if (params->isParameter(
"Verbosity")) {
350 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
351 verbosity_ = params->get(
"Verbosity", verbosity_default_);
353 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
357 params_->set(
"Verbosity", verbosity_);
358 if (printer_ != Teuchos::null)
359 printer_->setVerbosity(verbosity_);
363 if (params->isParameter(
"Output Style")) {
364 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
365 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
367 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
371 params_->set(
"Output Style", outputStyle_);
372 outputTest_ = Teuchos::null;
376 if (params->isParameter(
"Output Stream")) {
377 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
380 params_->set(
"Output Stream", outputStream_);
381 if (printer_ != Teuchos::null)
382 printer_->setOStream( outputStream_ );
387 if (params->isParameter(
"Output Frequency")) {
388 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
392 params_->set(
"Output Frequency", outputFreq_);
393 if (outputTest_ != Teuchos::null)
394 outputTest_->setOutputFrequency( outputFreq_ );
398 if (printer_ == Teuchos::null) {
407 if (params->isParameter(
"Convergence Tolerance")) {
408 if (params->isType<MagnitudeType> (
"Convergence Tolerance")) {
409 convtol_ = params->get (
"Convergence Tolerance",
417 params_->set(
"Convergence Tolerance", convtol_);
418 if (convTest_ != Teuchos::null)
419 convTest_->setTolerance( convtol_ );
422 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
423 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
426 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
427 if (convTest_ != Teuchos::null)
428 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
432 bool newResTest =
false;
437 std::string tempResScale = resScale_;
438 bool implicitResidualScalingName =
false;
439 if (params->isParameter (
"Residual Scaling")) {
440 tempResScale = params->get<std::string> (
"Residual Scaling");
442 else if (params->isParameter (
"Implicit Residual Scaling")) {
443 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
444 implicitResidualScalingName =
true;
448 if (resScale_ != tempResScale) {
450 resScale_ = tempResScale;
454 if (implicitResidualScalingName) {
455 params_->set (
"Implicit Residual Scaling", resScale_);
458 params_->set (
"Residual Scaling", resScale_);
461 if (! convTest_.is_null()) {
465 catch (std::exception& e) {
474 if (params->isParameter(
"Deflation Quorum")) {
475 defQuorum_ = params->get(
"Deflation Quorum", defQuorum_);
476 params_->set(
"Deflation Quorum", defQuorum_);
477 if (convTest_ != Teuchos::null)
478 convTest_->setQuorum( defQuorum_ );
484 if (maxIterTest_ == Teuchos::null)
488 if (convTest_ == Teuchos::null || newResTest) {
489 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
493 if (sTest_ == Teuchos::null || newResTest)
494 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
496 if (outputTest_ == Teuchos::null || newResTest) {
504 std::string solverDesc =
" Pseudo Block BiCGStab ";
505 outputTest_->setSolverDesc( solverDesc );
510 if (timerSolve_ == Teuchos::null) {
511 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
512#ifdef BELOS_TEUCHOS_TIME_MONITOR
513 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
522template<
class ScalarType,
class MV,
class OP>
523Teuchos::RCP<const Teuchos::ParameterList>
526 using Teuchos::ParameterList;
527 using Teuchos::parameterList;
530 if (validParams_.is_null()) {
532 RCP<ParameterList> pl = parameterList ();
537 "The relative residual tolerance that needs to be achieved by the\n"
538 "iterative solver in order for the linera system to be declared converged.");
539 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
540 "The maximum number of block iterations allowed for each\n"
541 "set of RHS solved.");
542 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
543 "What type(s) of solver information should be outputted\n"
544 "to the output stream.");
545 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
546 "What style is used for the solver information outputted\n"
547 "to the output stream.");
548 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
549 "How often convergence information should be outputted\n"
550 "to the output stream.");
551 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
552 "The number of linear systems that need to converge before\n"
553 "they are deflated. This number should be <= block size.");
554 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
555 "A reference-counted pointer to the output stream where all\n"
556 "solver output is sent.");
557 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
558 "When convergence information is printed, only show the maximum\n"
559 "relative residual norm when the block size is greater than one.");
560 pl->set(
"Implicit Residual Scaling",
static_cast<const char *
>(resScale_default_),
561 "The type of scaling used in the residual convergence test.");
567 pl->set(
"Residual Scaling",
static_cast<const char *
>(resScale_default_),
568 "The type of scaling used in the residual convergence test. This "
569 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
570 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
571 "The string to use as a prefix for the timer labels.");
578template<
class ScalarType,
class MV,
class OP>
588 TEUCHOS_TEST_FOR_EXCEPTION
590 "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. "
591 "You must call setProblem() on the LinearProblem before you may solve it.");
592 TEUCHOS_TEST_FOR_EXCEPTION
593 (problem_->isLeftPrec (), std::logic_error,
"Belos::BiCGStabSolMgr::solve: "
594 "The left-preconditioned case has not yet been implemented. Please use "
595 "right preconditioning for now. If you need to use left preconditioning, "
596 "please contact the Belos developers. Left preconditioning is more "
597 "interesting in BiCGStab because whether it works depends on the initial "
598 "guess (e.g., an initial guess of all zeros might NOT work).");
603 int numCurrRHS = numRHS2Solve;
605 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
606 for (
int i=0; i<numRHS2Solve; ++i) {
607 currIdx[i] = startPtr+i;
612 problem_->setLSIndex( currIdx );
616 Teuchos::ParameterList plist;
619 outputTest_->reset();
622 bool isConverged =
true;
627 Teuchos::RCP<BiCGStabIter<ScalarType,MV,OP> > bicgstab_iter
632#ifdef BELOS_TEUCHOS_TIME_MONITOR
633 Teuchos::TimeMonitor slvtimer(*timerSolve_);
637 while ( numRHS2Solve > 0 ) {
639 std::vector<int> convRHSIdx;
640 std::vector<int> currRHSIdx( currIdx );
641 currRHSIdx.resize(numCurrRHS);
644 bicgstab_iter->resetNumIters();
647 outputTest_->resetNumCalls();
650 Teuchos::RCP<MV> R_0 =
MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
655 bicgstab_iter->initializeBiCGStab(newState);
662 bicgstab_iter->iterate();
669 if ( convTest_->getStatus() ==
Passed ) {
672 std::vector<int> convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP> >(convTest_)->convIndices();
676 if (convIdx.size() == currRHSIdx.size())
680 problem_->setCurrLS();
684 std::vector<int> unconvIdx(currRHSIdx.size());
685 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
687 for (
unsigned int j=0; j<convIdx.size(); ++j) {
688 if (currRHSIdx[i] == convIdx[j]) {
694 currIdx2[have] = currIdx2[i];
695 currRHSIdx[have++] = currRHSIdx[i];
698 currRHSIdx.resize(have);
699 currIdx2.resize(have);
702 problem_->setLSIndex( currRHSIdx );
705 std::vector<MagnitudeType> norms;
706 R_0 =
MVT::CloneCopy( *(bicgstab_iter->getNativeResiduals(&norms)),currIdx2 );
707 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
712 bicgstab_iter->initializeBiCGStab(defstate);
720 else if ( maxIterTest_->getStatus() ==
Passed ) {
733 else if ( bicgstab_iter->breakdownDetected() ) {
737 "Belos::BiCGStabSolMgr::solve(): Warning! Solver has experienced a breakdown!" << std::endl;
749 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
750 "Belos::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
755 achievedTol_ = MT::one();
756 Teuchos::RCP<MV> X = problem_->getLHS();
758 printer_->stream(
Warnings) <<
"Belos::BiCGStabSolMgr::solve(): Warning! NaN has been detected!"
762 catch (
const std::exception &e) {
763 printer_->stream(
Errors) <<
"Error! Caught std::exception in BiCGStabIter::iterate() at iteration "
764 << bicgstab_iter->getNumIters() << std::endl
765 << e.what() << std::endl;
771 problem_->setCurrLS();
774 startPtr += numCurrRHS;
775 numRHS2Solve -= numCurrRHS;
777 if ( numRHS2Solve > 0 ) {
779 numCurrRHS = numRHS2Solve;
780 currIdx.resize( numCurrRHS );
781 currIdx2.resize( numCurrRHS );
782 for (
int i=0; i<numCurrRHS; ++i)
783 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
786 problem_->setLSIndex( currIdx );
789 currIdx.resize( numRHS2Solve );
801#ifdef BELOS_TEUCHOS_TIME_MONITOR
806 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
810 numIters_ = maxIterTest_->getNumIters();
815 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
816 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
826template<
class ScalarType,
class MV,
class OP>
829 std::ostringstream oss;
830 oss <<
"Belos::BiCGStabSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
Belos concrete class for performing the pseudo-block BiCGStab iteration.
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.
Collection of types and exceptions used within the Belos solvers.
BelosError(const std::string &what_arg)
This class implements the pseudo-block BiCGStab iteration, where the basic BiCGStab algorithm is perf...
BiCGStabSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
BiCGStabSolMgrLinearProblemFailure(const std::string &what_arg)
int getNumIters() const override
Get the iteration count for the most recent call to solve().
virtual ~BiCGStabSolMgr()
Destructor.
BiCGStabSolMgr()
Empty constructor for BiCGStabSolMgr. This constructor takes no arguments and sets the default values...
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
std::string description() const override
Method to return description of the block BiCGStab solver manager.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
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.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
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.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Class which defines basic traits for the operator type.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
SolverManager()
Empty constructor.
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
A Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > create(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test, int mod, int printStates)
Create the StatusTestOutput object specified by the outputStyle.
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.
ScaleType
The type of scaling to use on the residual norm value.
ResetType
How to reset the solver.
Structure to contain pointers to BiCGStabIteration state variables.
Teuchos::RCP< const MV > R
The current residual.
Default parameters common to most Belos solvers.
static const double convTol
Default convergence tolerance.