42 #ifndef BELOS_MINRES_SOLMGR_HPP 43 #define BELOS_MINRES_SOLMGR_HPP 62 #ifdef BELOS_TEUCHOS_TIME_MONITOR 66 #include "Teuchos_StandardParameterEntryValidators.hpp" 114 template<
class ScalarType,
class MV,
class OP>
200 if (defaultParams_.
is_null()) {
203 return defaultParams_;
221 return Teuchos::tuple (timerSolve_);
268 problem_->setProblem ();
355 MagnitudeType convtol_;
358 MagnitudeType achievedTol_;
397 template<
class ScalarType,
class MV,
class OP>
402 using Teuchos::parameterList;
405 using Teuchos::rcpFromRef;
407 typedef MagnitudeType
MT;
411 RCP<ParameterList> pl = parameterList (
"MINRES");
413 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
414 "Relative residual tolerance that needs to be achieved by " 415 "the iterative solver, in order for the linear system to be " 416 "declared converged.",
417 rcp (
new EnhancedNumberValidator<MT> (MST::zero(), MST::rmax())));
418 pl->set (
"Maximum Iterations", static_cast<int>(1000),
419 "Maximum number of iterations allowed for each right-hand " 421 rcp (
new EnhancedNumberValidator<int> (0, INT_MAX)));
422 pl->set (
"Num Blocks", static_cast<int> (-1),
423 "Ignored, but permitted, for compatibility with other Belos " 425 pl->set (
"Block Size", static_cast<int> (1),
426 "Number of vectors in each block. WARNING: The current " 427 "implementation of MINRES only accepts a block size of 1, " 428 "since it can only solve for 1 right-hand side at a time.",
429 rcp (
new EnhancedNumberValidator<int> (1, 1)));
431 "The type(s) of solver information that should " 432 "be written to the output stream.");
434 "What style is used for the solver information written " 435 "to the output stream.");
436 pl->set (
"Output Frequency", static_cast<int>(-1),
437 "How often (in terms of number of iterations) intermediate " 438 "convergence information should be written to the output stream." 440 pl->set (
"Output Stream", rcpFromRef(std::cout),
441 "A reference-counted pointer to the output stream where all " 442 "solver output is sent. The output stream defaults to stdout.");
443 pl->set (
"Timer Label", std::string(
"Belos"),
444 "The string to use as a prefix for the timer labels.");
451 template<
class ScalarType,
class MV,
class OP>
461 parametersSet_ (false)
467 template<
class ScalarType,
class MV,
class OP>
473 parametersSet_ (false)
476 "MinresSolMgr: The version of the constructor " 477 "that takes a LinearProblem to solve was given a " 478 "null LinearProblem.");
482 template<
class ScalarType,
class MV,
class OP>
489 "MINRES requires that you have provided a nonnull LinearProblem to the " 490 "solver manager, before you call the solve() method.");
493 "MINRES requires a LinearProblem object with a non-null operator (the " 497 "MINRES requires a LinearProblem object with a non-null right-hand side.");
500 "MINRES requires that before you give it a LinearProblem to solve, you " 501 "must first call the linear problem's setProblem() method.");
504 template<
class ScalarType,
class MV,
class OP>
510 using Teuchos::parameterList;
513 using Teuchos::rcpFromRef;
520 if (params_.is_null()) {
521 params_ = parameterList (*getValidParameters());
523 RCP<ParameterList> pl = params;
530 blockSize_ = pl->get<
int> (
"Block Size");
531 verbosity_ = pl->get<
int> (
"Verbosity");
532 outputStyle_ = pl->get<
int> (
"Output Style");
533 outputFreq_ = pl->get<
int>(
"Output Frequency");
534 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
535 convtol_ = pl->get<MagnitudeType> (
"Convergence Tolerance");
536 maxIters_ = pl->get<
int> (
"Maximum Iterations");
544 const string newLabel = pl->get<
string> (
"Timer Label");
546 if (newLabel != label_ || timerSolve_.is_null()) {
548 #ifdef BELOS_TEUCHOS_TIME_MONITOR 549 const string solveLabel = label_ +
": MinresSolMgr total solve time";
551 if (! timerSolve_.is_null()) {
553 timerSolve_ = Teuchos::null;
556 #endif // BELOS_TEUCHOS_TIME_MONITOR 561 bool recreatedPrinter =
false;
562 if (printer_.is_null()) {
564 recreatedPrinter =
true;
567 printer_->setVerbosity (verbosity_);
569 printer_->setOStream (outputStream_);
580 const bool allocatedConvergenceTests =
581 impConvTest_.is_null() || expConvTest_.is_null();
585 if (impConvTest_.is_null()) {
586 impConvTest_ =
rcp (
new res_norm_type (convtol_));
587 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
592 impConvTest_->setTolerance (convtol_);
597 if (expConvTest_.is_null()) {
598 expConvTest_ =
rcp (
new res_norm_type (convtol_));
599 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
604 expConvTest_->setTolerance (convtol_);
610 bool needToRecreateFullStatusTest = sTest_.is_null();
614 if (convTest_.is_null() || allocatedConvergenceTests) {
615 convTest_ =
rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
616 needToRecreateFullStatusTest =
true;
623 if (maxIterTest_.is_null()) {
625 needToRecreateFullStatusTest =
true;
627 maxIterTest_->setMaxIters (maxIters_);
638 if (needToRecreateFullStatusTest) {
639 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
646 if (outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
648 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
651 outputTest_->setOutputFrequency (outputFreq_);
655 outputTest_->setSolverDesc (std::string (
" MINRES "));
658 parametersSet_ =
true;
660 if (verbosity_ &
Debug) {
663 std::ostream& dbg = printer_->stream (
Debug);
664 dbg <<
"MINRES parameters:" << endl << params_ << endl;
669 template<
class ScalarType,
class MV,
class OP>
674 using Teuchos::rcp_const_cast;
677 if (! parametersSet_) {
678 setParameters (params_);
680 std::ostream& dbg = printer_->stream (
Debug);
682 #ifdef BELOS_TEUCHOS_TIME_MONITOR 684 #endif // BELOS_TEUCHOS_TIME_MONITOR 687 validateProblem (problem_);
690 outputTest_->reset();
695 const int numRHS2Solve = MVT::GetNumberVecs (*(problem_->getRHS()));
700 RCP<iter_type> minres_iter =
701 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
707 std::vector<int> notConverged;
708 std::vector<int> currentIndices(1);
713 for (
int currentRHS = 0; currentRHS < numRHS2Solve; ++currentRHS) {
718 currentIndices[0] = currentRHS;
719 problem_->setLSIndex (currentIndices);
721 dbg <<
"-- Current right-hand side index being solved: " 722 << currentRHS << endl;
725 minres_iter->resetNumIters();
727 outputTest_->resetNumCalls();
733 newstate.
Y = MVT::CloneViewNonConst (*(rcp_const_cast<MV> (problem_->getInitResVec())), currentIndices);
734 minres_iter->initializeMinres (newstate);
740 minres_iter->iterate();
743 if (convTest_->getStatus() ==
Passed) {
744 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
745 <<
" iterations" << endl;
749 else if (maxIterTest_->getStatus() ==
Passed) {
750 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
751 <<
" iterations" << endl;
753 notConverged.push_back (currentRHS);
760 "Belos::MinresSolMgr::solve(): iterations neither converged, " 761 "nor reached the maximum number of iterations " << maxIters_
762 <<
". That means something went wrong.");
764 }
catch (
const std::exception &e) {
766 <<
"Error! Caught std::exception in MinresIter::iterate() at " 767 <<
"iteration " << minres_iter->getNumIters() << endl
776 problem_->setCurrLS();
780 numIters_ += maxIterTest_->getNumIters();
788 #ifdef BELOS_TEUCHOS_TIME_MONITOR 795 #endif // BELOS_TEUCHOS_TIME_MONITOR 807 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
808 if (pTestValues == NULL || pTestValues->size() < 1) {
809 pTestValues = impConvTest_->getTestValue();
812 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() " 813 "method returned NULL. Please report this bug to the Belos developers.");
815 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() " 816 "method returned a vector of length zero. Please report this bug to the " 817 "Belos developers.");
822 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
825 if (notConverged.size() > 0) {
833 template<
class ScalarType,
class MV,
class OP>
836 std::ostringstream oss;
837 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...
ReturnType solve()
Iterate until the status test tells us to stop.
Class which manages the output and verbosity of the Belos solvers.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
virtual ~MinresSolMgr()
Destructor.
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
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.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
std::string description() const
An implementation of StatusTestResNorm using a family of residual norms.
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return the list of default parameters for this object.
void reset(const ResetType type)
Reset the solver manager.
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return the linear problem to be solved.
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)
int getNumIters() const
Get the iteration count for the most recent call to solve().
MINRES linear solver solution manager.
bool isLOADetected() const
Whether a loss of accuracy was detected in the solver.
MINRES iteration implementation.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
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
Return the list of current parameters for this object.
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...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters to use when solving the linear problem.