42 #ifndef BELOS_MINRES_SOLMGR_HPP
43 #define BELOS_MINRES_SOLMGR_HPP
60 #ifdef BELOS_TEUCHOS_TIME_MONITOR
64 #include "Teuchos_StandardParameterEntryValidators.hpp"
112 template<
class ScalarType,
class MV,
class OP>
203 if (defaultParams_.
is_null()) {
206 return defaultParams_;
224 return Teuchos::tuple (timerSolve_);
271 problem_->setProblem ();
358 MagnitudeType convtol_;
361 MagnitudeType achievedTol_;
400 template<
class ScalarType,
class MV,
class OP>
405 using Teuchos::parameterList;
408 using Teuchos::rcpFromRef;
410 typedef MagnitudeType MT;
413 RCP<ParameterList> pl = parameterList (
"MINRES");
415 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
416 "Relative residual tolerance that needs to be achieved by "
417 "the iterative solver, in order for the linear system to be "
418 "declared converged.",
419 rcp (
new EnhancedNumberValidator<MT> (MST::zero(), MST::rmax())));
420 pl->set (
"Maximum Iterations", static_cast<int>(1000),
421 "Maximum number of iterations allowed for each right-hand "
423 rcp (
new EnhancedNumberValidator<int> (0, INT_MAX)));
424 pl->set (
"Num Blocks", static_cast<int> (-1),
425 "Ignored, but permitted, for compatibility with other Belos "
427 pl->set (
"Block Size", static_cast<int> (1),
428 "Number of vectors in each block. WARNING: The current "
429 "implementation of MINRES only accepts a block size of 1, "
430 "since it can only solve for 1 right-hand side at a time.",
431 rcp (
new EnhancedNumberValidator<int> (1, 1)));
433 "The type(s) of solver information that should "
434 "be written to the output stream.");
436 "What style is used for the solver information written "
437 "to the output stream.");
438 pl->set (
"Output Frequency", static_cast<int>(-1),
439 "How often (in terms of number of iterations) intermediate "
440 "convergence information should be written to the output stream."
442 pl->set (
"Output Stream", rcpFromRef(std::cout),
443 "A reference-counted pointer to the output stream where all "
444 "solver output is sent. The output stream defaults to stdout.");
445 pl->set (
"Timer Label", std::string(
"Belos"),
446 "The string to use as a prefix for the timer labels.");
453 template<
class ScalarType,
class MV,
class OP>
463 parametersSet_ (false)
469 template<
class ScalarType,
class MV,
class OP>
475 parametersSet_ (false)
478 "MinresSolMgr: The version of the constructor "
479 "that takes a LinearProblem to solve was given a "
480 "null LinearProblem.");
484 template<
class ScalarType,
class MV,
class OP>
491 "MINRES requires that you have provided a nonnull LinearProblem to the "
492 "solver manager, before you call the solve() method.");
495 "MINRES requires a LinearProblem object with a non-null operator (the "
499 "MINRES requires a LinearProblem object with a non-null right-hand side.");
502 "MINRES requires that before you give it a LinearProblem to solve, you "
503 "must first call the linear problem's setProblem() method.");
506 template<
class ScalarType,
class MV,
class OP>
512 using Teuchos::parameterList;
515 using Teuchos::rcpFromRef;
522 if (params_.is_null()) {
523 params_ = parameterList (*getValidParameters());
525 RCP<ParameterList> pl = params;
532 blockSize_ = pl->get<
int> (
"Block Size");
533 verbosity_ = pl->get<
int> (
"Verbosity");
534 outputStyle_ = pl->get<
int> (
"Output Style");
535 outputFreq_ = pl->get<
int>(
"Output Frequency");
536 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
537 convtol_ = pl->get<MagnitudeType> (
"Convergence Tolerance");
538 maxIters_ = pl->get<
int> (
"Maximum Iterations");
546 const string newLabel = pl->get<
string> (
"Timer Label");
548 if (newLabel != label_ || timerSolve_.is_null()) {
550 #ifdef BELOS_TEUCHOS_TIME_MONITOR
551 const string solveLabel = label_ +
": MinresSolMgr total solve time";
553 if (! timerSolve_.is_null()) {
555 timerSolve_ = Teuchos::null;
558 #endif // BELOS_TEUCHOS_TIME_MONITOR
563 bool recreatedPrinter =
false;
564 if (printer_.is_null()) {
566 recreatedPrinter =
true;
569 printer_->setVerbosity (verbosity_);
571 printer_->setOStream (outputStream_);
582 const bool allocatedConvergenceTests =
583 impConvTest_.is_null() || expConvTest_.is_null();
587 if (impConvTest_.is_null()) {
588 impConvTest_ =
rcp (
new res_norm_type (convtol_));
589 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
594 impConvTest_->setTolerance (convtol_);
599 if (expConvTest_.is_null()) {
600 expConvTest_ =
rcp (
new res_norm_type (convtol_));
601 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
606 expConvTest_->setTolerance (convtol_);
612 bool needToRecreateFullStatusTest = sTest_.is_null();
616 if (convTest_.is_null() || allocatedConvergenceTests) {
617 convTest_ =
rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
618 needToRecreateFullStatusTest =
true;
625 if (maxIterTest_.is_null()) {
627 needToRecreateFullStatusTest =
true;
629 maxIterTest_->setMaxIters (maxIters_);
640 if (needToRecreateFullStatusTest) {
641 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
648 if (outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
650 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
653 outputTest_->setOutputFrequency (outputFreq_);
657 outputTest_->setSolverDesc (std::string (
" MINRES "));
660 parametersSet_ =
true;
662 if (verbosity_ &
Debug) {
665 std::ostream& dbg = printer_->stream (Debug);
666 dbg <<
"MINRES parameters:" << endl << params_ << endl;
671 template<
class ScalarType,
class MV,
class OP>
676 using Teuchos::rcp_const_cast;
679 if (! parametersSet_) {
680 setParameters (params_);
682 std::ostream& dbg = printer_->stream (
Debug);
684 #ifdef BELOS_TEUCHOS_TIME_MONITOR
686 #endif // BELOS_TEUCHOS_TIME_MONITOR
689 validateProblem (problem_);
692 outputTest_->reset();
697 const int numRHS2Solve = MVT::GetNumberVecs (*(problem_->getRHS()));
702 RCP<iter_type> minres_iter =
703 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
709 std::vector<int> notConverged;
710 std::vector<int> currentIndices(1);
715 for (
int currentRHS = 0; currentRHS < numRHS2Solve; ++currentRHS) {
720 currentIndices[0] = currentRHS;
721 problem_->setLSIndex (currentIndices);
723 dbg <<
"-- Current right-hand side index being solved: "
724 << currentRHS << endl;
727 minres_iter->resetNumIters();
729 outputTest_->resetNumCalls();
735 newstate.
Y = MVT::CloneViewNonConst (*(rcp_const_cast<MV> (problem_->getInitResVec())), currentIndices);
736 minres_iter->initializeMinres (newstate);
742 minres_iter->iterate();
745 if (convTest_->getStatus() ==
Passed) {
746 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
747 <<
" iterations" << endl;
751 else if (maxIterTest_->getStatus() ==
Passed) {
752 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
753 <<
" iterations" << endl;
755 notConverged.push_back (currentRHS);
762 "Belos::MinresSolMgr::solve(): iterations neither converged, "
763 "nor reached the maximum number of iterations " << maxIters_
764 <<
". That means something went wrong.");
769 achievedTol_ = MST::one();
771 MVT::MvInit( *X, SCT::zero() );
772 printer_->stream(
Warnings) <<
"Belos::MinresSolMgr::solve(): Warning! NaN has been detected!"
776 catch (
const std::exception &e) {
778 <<
"Error! Caught std::exception in MinresIter::iterate() at "
779 <<
"iteration " << minres_iter->getNumIters() << endl
788 problem_->setCurrLS();
792 numIters_ += maxIterTest_->getNumIters();
800 #ifdef BELOS_TEUCHOS_TIME_MONITOR
807 #endif // BELOS_TEUCHOS_TIME_MONITOR
819 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
820 if (pTestValues == NULL || pTestValues->size() < 1) {
821 pTestValues = impConvTest_->getTestValue();
824 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
825 "method returned NULL. Please report this bug to the Belos developers.");
827 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
828 "method returned a vector of length zero. Please report this bug to the "
829 "Belos developers.");
834 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
837 if (notConverged.size() > 0) {
845 template<
class ScalarType,
class MV,
class OP>
848 std::ostringstream oss;
849 oss <<
"Belos::MinresSolMgr< "
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...
Class which manages the output and verbosity of the Belos solvers.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
virtual ~MinresSolMgr()
Destructor.
bool is_null(const std::shared_ptr< T > &p)
int getNumIters() const override
Get the iteration count for the most recent call to solve().
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
An implementation of StatusTestResNorm using a family of residual norms.
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return the linear problem to be solved.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters to use when solving the linear problem.
This subclass of std::exception may be thrown from the MinresSolMgr::solve() method.
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)
MINRES linear solver solution manager.
MINRES iteration implementation.
std::string description() const override
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return the list of default parameters for this object.
Structure to contain pointers to MinresIteration state variables.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
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 > getCurrentParameters() const override
Return the list of current parameters for this object.
void reset(const ResetType type) override
Reset the solver manager.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
Teuchos::RCP< const MV > Y
The current residual.
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.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
A class for extending the status testing capabilities of Belos via logical combinations.
MinresSolMgr()
Default constructor.
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
Belos header file which uses auto-configuration information to include necessary C++ headers...
ReturnType solve() override
Iterate until the status test tells us to stop.
bool isLOADetected() const override
Whether a loss of accuracy was detected in the solver.