42 #ifndef BELOS_BICGSTAB_SOLMGR_HPP
43 #define BELOS_BICGSTAB_SOLMGR_HPP
61 #ifdef BELOS_TEUCHOS_TIME_MONITOR
96 template<
class ScalarType,
class MV,
class OP>
160 return Teuchos::tuple(timerSolve_);
267 static constexpr
int maxIters_default_ = 1000;
268 static constexpr
bool showMaxResNormOnly_default_ =
false;
271 static constexpr
int outputFreq_default_ = -1;
272 static constexpr
int defQuorum_default_ = 1;
273 static constexpr
const char * resScale_default_ =
"Norm of Initial Residual";
274 static constexpr
const char * label_default_ =
"Belos";
277 MagnitudeType convtol_,achievedTol_;
278 int maxIters_, numIters_;
279 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
280 bool showMaxResNormOnly_;
281 std::string resScale_;
292 template<
class ScalarType,
class MV,
class OP>
294 outputStream_(Teuchos::rcpFromRef(std::cout)),
296 maxIters_(maxIters_default_),
298 verbosity_(verbosity_default_),
299 outputStyle_(outputStyle_default_),
300 outputFreq_(outputFreq_default_),
301 defQuorum_(defQuorum_default_),
302 showMaxResNormOnly_(showMaxResNormOnly_default_),
303 resScale_(resScale_default_),
304 label_(label_default_),
309 template<
class ScalarType,
class MV,
class OP>
314 outputStream_(Teuchos::rcpFromRef(std::cout)),
316 maxIters_(maxIters_default_),
318 verbosity_(verbosity_default_),
319 outputStyle_(outputStyle_default_),
320 outputFreq_(outputFreq_default_),
321 defQuorum_(defQuorum_default_),
322 showMaxResNormOnly_(showMaxResNormOnly_default_),
323 resScale_(resScale_default_),
324 label_(label_default_),
328 problem_.is_null (), std::invalid_argument,
329 "Belos::BiCGStabSolMgr two-argument constructor: "
330 "'problem' is null. You must supply a non-null Belos::LinearProblem "
331 "instance when calling this constructor.");
339 template<
class ScalarType,
class MV,
class OP>
343 using Teuchos::parameterList;
346 RCP<const ParameterList> defaultParams = getValidParameters();
349 if (params_.is_null()) {
350 params_ = parameterList (*defaultParams);
357 maxIters_ = params->
get(
"Maximum Iterations",maxIters_default_);
360 params_->set(
"Maximum Iterations", maxIters_);
361 if (maxIterTest_!=Teuchos::null)
362 maxIterTest_->setMaxIters( maxIters_ );
367 std::string tempLabel = params->
get(
"Timer Label", label_default_);
370 if (tempLabel != label_) {
372 params_->set(
"Timer Label", label_);
373 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
374 #ifdef BELOS_TEUCHOS_TIME_MONITOR
382 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
383 verbosity_ = params->
get(
"Verbosity", verbosity_default_);
385 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
389 params_->set(
"Verbosity", verbosity_);
390 if (printer_ != Teuchos::null)
391 printer_->setVerbosity(verbosity_);
396 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
397 outputStyle_ = params->
get(
"Output Style", outputStyle_default_);
399 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
403 params_->set(
"Output Style", outputStyle_);
404 outputTest_ = Teuchos::null;
409 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
412 params_->set(
"Output Stream", outputStream_);
413 if (printer_ != Teuchos::null)
414 printer_->setOStream( outputStream_ );
420 outputFreq_ = params->
get(
"Output Frequency", outputFreq_default_);
424 params_->set(
"Output Frequency", outputFreq_);
425 if (outputTest_ != Teuchos::null)
426 outputTest_->setOutputFrequency( outputFreq_ );
430 if (printer_ == Teuchos::null) {
439 if (params->
isParameter(
"Convergence Tolerance")) {
440 if (params->
isType<MagnitudeType> (
"Convergence Tolerance")) {
441 convtol_ = params->
get (
"Convergence Tolerance",
449 params_->set(
"Convergence Tolerance", convtol_);
450 if (convTest_ != Teuchos::null)
451 convTest_->setTolerance( convtol_ );
454 if (params->
isParameter(
"Show Maximum Residual Norm Only")) {
455 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
458 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
459 if (convTest_ != Teuchos::null)
460 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
464 bool newResTest =
false;
469 std::string tempResScale = resScale_;
470 bool implicitResidualScalingName =
false;
472 tempResScale = params->
get<std::string> (
"Residual Scaling");
474 else if (params->
isParameter (
"Implicit Residual Scaling")) {
475 tempResScale = params->
get<std::string> (
"Implicit Residual Scaling");
476 implicitResidualScalingName =
true;
480 if (resScale_ != tempResScale) {
482 resScale_ = tempResScale;
486 if (implicitResidualScalingName) {
487 params_->set (
"Implicit Residual Scaling", resScale_);
490 params_->set (
"Residual Scaling", resScale_);
493 if (! convTest_.is_null()) {
497 catch (std::exception& e) {
507 defQuorum_ = params->
get(
"Deflation Quorum", defQuorum_);
508 params_->set(
"Deflation Quorum", defQuorum_);
509 if (convTest_ != Teuchos::null)
510 convTest_->setQuorum( defQuorum_ );
516 if (maxIterTest_ == Teuchos::null)
520 if (convTest_ == Teuchos::null || newResTest) {
521 convTest_ =
Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
525 if (sTest_ == Teuchos::null || newResTest)
526 sTest_ =
Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
528 if (outputTest_ == Teuchos::null || newResTest) {
536 std::string solverDesc =
" Pseudo Block BiCGStab ";
537 outputTest_->setSolverDesc( solverDesc );
542 if (timerSolve_ == Teuchos::null) {
543 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
544 #ifdef BELOS_TEUCHOS_TIME_MONITOR
554 template<
class ScalarType,
class MV,
class OP>
559 using Teuchos::parameterList;
562 if (validParams_.is_null()) {
564 RCP<ParameterList> pl = parameterList ();
569 "The relative residual tolerance that needs to be achieved by the\n"
570 "iterative solver in order for the linera system to be declared converged.");
571 pl->set(
"Maximum Iterations", static_cast<int>(maxIters_default_),
572 "The maximum number of block iterations allowed for each\n"
573 "set of RHS solved.");
574 pl->set(
"Verbosity", static_cast<int>(verbosity_default_),
575 "What type(s) of solver information should be outputted\n"
576 "to the output stream.");
577 pl->set(
"Output Style", static_cast<int>(outputStyle_default_),
578 "What style is used for the solver information outputted\n"
579 "to the output stream.");
580 pl->set(
"Output Frequency", static_cast<int>(outputFreq_default_),
581 "How often convergence information should be outputted\n"
582 "to the output stream.");
583 pl->set(
"Deflation Quorum", static_cast<int>(defQuorum_default_),
584 "The number of linear systems that need to converge before\n"
585 "they are deflated. This number should be <= block size.");
586 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
587 "A reference-counted pointer to the output stream where all\n"
588 "solver output is sent.");
589 pl->set(
"Show Maximum Residual Norm Only", static_cast<bool>(showMaxResNormOnly_default_),
590 "When convergence information is printed, only show the maximum\n"
591 "relative residual norm when the block size is greater than one.");
592 pl->set(
"Implicit Residual Scaling", static_cast<const char *>(resScale_default_),
593 "The type of scaling used in the residual convergence test.");
599 pl->set(
"Residual Scaling", static_cast<const char *>(resScale_default_),
600 "The type of scaling used in the residual convergence test. This "
601 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
602 pl->set(
"Timer Label", static_cast<const char *>(label_default_),
603 "The string to use as a prefix for the timer labels.");
610 template<
class ScalarType,
class MV,
class OP>
617 setParameters (params_);
622 "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. "
623 "You must call setProblem() on the LinearProblem before you may solve it.");
625 (problem_->isLeftPrec (), std::logic_error,
"Belos::BiCGStabSolMgr::solve: "
626 "The left-preconditioned case has not yet been implemented. Please use "
627 "right preconditioning for now. If you need to use left preconditioning, "
628 "please contact the Belos developers. Left preconditioning is more "
629 "interesting in BiCGStab because whether it works depends on the initial "
630 "guess (e.g., an initial guess of all zeros might NOT work).");
634 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
635 int numCurrRHS = numRHS2Solve;
637 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
638 for (
int i=0; i<numRHS2Solve; ++i) {
639 currIdx[i] = startPtr+i;
644 problem_->setLSIndex( currIdx );
651 outputTest_->reset();
654 bool isConverged =
true;
664 #ifdef BELOS_TEUCHOS_TIME_MONITOR
669 while ( numRHS2Solve > 0 ) {
671 std::vector<int> convRHSIdx;
672 std::vector<int> currRHSIdx( currIdx );
673 currRHSIdx.resize(numCurrRHS);
676 bicgstab_iter->resetNumIters();
679 outputTest_->resetNumCalls();
682 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
687 bicgstab_iter->initializeBiCGStab(newState);
694 bicgstab_iter->iterate();
701 if ( convTest_->getStatus() ==
Passed ) {
708 if (convIdx.size() == currRHSIdx.size())
712 problem_->setCurrLS();
716 std::vector<int> unconvIdx(currRHSIdx.size());
717 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
719 for (
unsigned int j=0; j<convIdx.size(); ++j) {
720 if (currRHSIdx[i] == convIdx[j]) {
726 currIdx2[have] = currIdx2[i];
727 currRHSIdx[have++] = currRHSIdx[i];
730 currRHSIdx.resize(have);
731 currIdx2.resize(have);
734 problem_->setLSIndex( currRHSIdx );
737 std::vector<MagnitudeType> norms;
738 R_0 = MVT::CloneCopy( *(bicgstab_iter->getNativeResiduals(&norms)),currIdx2 );
739 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
744 bicgstab_iter->initializeBiCGStab(defstate);
752 else if ( maxIterTest_->getStatus() ==
Passed ) {
765 else if ( bicgstab_iter->breakdownDetected() ) {
769 "Belos::BiCGStabSolMgr::solve(): Warning! Solver has experienced a breakdown!" << std::endl;
782 "Belos::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
787 achievedTol_ = MT::one();
789 MVT::MvInit( *X, SCT::zero() );
790 printer_->stream(
Warnings) <<
"Belos::BiCGStabSolMgr::solve(): Warning! NaN has been detected!"
794 catch (
const std::exception &e) {
795 printer_->stream(
Errors) <<
"Error! Caught std::exception in BiCGStabIter::iterate() at iteration "
796 << bicgstab_iter->getNumIters() << std::endl
797 << e.what() << std::endl;
803 problem_->setCurrLS();
806 startPtr += numCurrRHS;
807 numRHS2Solve -= numCurrRHS;
809 if ( numRHS2Solve > 0 ) {
811 numCurrRHS = numRHS2Solve;
812 currIdx.resize( numCurrRHS );
813 currIdx2.resize( numCurrRHS );
814 for (
int i=0; i<numCurrRHS; ++i)
815 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
818 problem_->setLSIndex( currIdx );
821 currIdx.resize( numRHS2Solve );
833 #ifdef BELOS_TEUCHOS_TIME_MONITOR
842 numIters_ = maxIterTest_->getNumIters();
847 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
848 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
858 template<
class ScalarType,
class MV,
class OP>
861 std::ostringstream oss;
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
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.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
This class implements the pseudo-block BiCGStab iteration, where the basic BiCGStab algorithm is perf...
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.
Teuchos::RCP< const MV > R
The current residual.
ScaleType
The type of scaling to use on the residual norm value.
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)
A factory class for generating StatusTestOutput objects.
Structure to contain pointers to BiCGStabIteration state variables.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
An implementation of StatusTestResNorm using a family of residual norms.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
static const double convTol
Default convergence tolerance.
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
BiCGStabSolMgrLinearProblemFailure(const std::string &what_arg)
bool isParameter(const std::string &name) const
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 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)
BiCGStabSolMgr()
Empty constructor for BiCGStabSolMgr. This constructor takes no arguments and sets the default values...
virtual ~BiCGStabSolMgr()
Destructor.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
BiCGStabSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
ReturnType
Whether the Belos solve converged for all linear systems.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
The Belos::BiCGStabSolMgr provides a powerful and fully-featured solver manager over the pseudo-block...
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.
std::string description() const override
Method to return description of the block BiCGStab solver manager.
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.
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Default parameters common to most Belos solvers.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve...
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Belos concrete class for performing the pseudo-block BiCGStab iteration.