42#ifndef BELOS_MINRES_SOLMGR_HPP
43#define BELOS_MINRES_SOLMGR_HPP
60#ifdef BELOS_TEUCHOS_TIME_MONITOR
61#include "Teuchos_TimeMonitor.hpp"
64#include "Teuchos_StandardParameterEntryValidators.hpp"
112 template<
class ScalarType,
class MV,
class OP>
118 typedef Teuchos::ScalarTraits<ScalarType> SCT;
119 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
120 typedef Teuchos::ScalarTraits< MagnitudeType > MST;
182 const Teuchos::RCP<Teuchos::ParameterList> ¶ms);
188 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
203 if (defaultParams_.is_null()) {
206 return defaultParams_;
223 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
224 return Teuchos::tuple (timerSolve_);
260 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
override;
271 problem_->setProblem ();
309 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
312 Teuchos::RCP<OutputManager<ScalarType> > printer_;
313 Teuchos::RCP<std::ostream> outputStream_;
321 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
326 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
331 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
336 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > impConvTest_;
341 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_;
347 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
352 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
355 Teuchos::RCP<Teuchos::ParameterList> params_;
358 MagnitudeType convtol_;
361 MagnitudeType achievedTol_;
385 Teuchos::RCP<Teuchos::Time> timerSolve_;
400 template<
class ScalarType,
class MV,
class OP>
401 Teuchos::RCP<const Teuchos::ParameterList>
404 using Teuchos::ParameterList;
405 using Teuchos::parameterList;
408 using Teuchos::rcpFromRef;
409 using Teuchos::EnhancedNumberValidator;
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>
472 const Teuchos::RCP<Teuchos::ParameterList>& params) :
475 parametersSet_ (false)
477 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
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>
489 TEUCHOS_TEST_FOR_EXCEPTION(problem.is_null(),
491 "MINRES requires that you have provided a nonnull LinearProblem to the "
492 "solver manager, before you call the solve() method.");
493 TEUCHOS_TEST_FOR_EXCEPTION(problem->getOperator().is_null(),
495 "MINRES requires a LinearProblem object with a non-null operator (the "
497 TEUCHOS_TEST_FOR_EXCEPTION(problem->getRHS().is_null(),
499 "MINRES requires a LinearProblem object with a non-null right-hand side.");
500 TEUCHOS_TEST_FOR_EXCEPTION( ! problem->isProblemSet(),
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>
509 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
511 using Teuchos::ParameterList;
512 using Teuchos::parameterList;
515 using Teuchos::rcpFromRef;
517 using Teuchos::is_null;
522 if (params_.is_null()) {
523 params_ = parameterList (*getValidParameters());
525 RCP<ParameterList> pl = params;
526 pl->validateParametersAndSetDefaults (*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()) {
554 Teuchos::TimeMonitor::clearCounter (label_);
555 timerSolve_ = Teuchos::null;
557 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
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
685 Teuchos::TimeMonitor solveTimerMonitor (*timerSolve_);
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);
761 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
762 "Belos::MinresSolMgr::solve(): iterations neither converged, "
763 "nor reached the maximum number of iterations " << maxIters_
764 <<
". That means something went wrong.");
766 }
catch (
const std::exception &e) {
768 <<
"Error! Caught std::exception in MinresIter::iterate() at "
769 <<
"iteration " << minres_iter->getNumIters() << endl
778 problem_->setCurrLS();
782 numIters_ += maxIterTest_->getNumIters();
790#ifdef BELOS_TEUCHOS_TIME_MONITOR
795 Teuchos::TimeMonitor::summarize (printer_->stream (
TimingDetails));
809 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
810 if (pTestValues == NULL || pTestValues->size() < 1) {
811 pTestValues = impConvTest_->getTestValue();
813 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
814 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
815 "method returned NULL. Please report this bug to the Belos developers.");
816 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
817 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
818 "method returned a vector of length zero. Please report this bug to the "
819 "Belos developers.");
824 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
827 if (notConverged.size() > 0) {
835 template<
class ScalarType,
class MV,
class OP>
838 std::ostringstream oss;
839 oss <<
"Belos::MinresSolMgr< "
840 << Teuchos::ScalarTraits<ScalarType>::name()
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.
MINRES iteration implementation.
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.
Parent class to all Belos exceptions.
A linear system to solve, and its associated information.
This subclass of std::exception may be thrown from the MinresSolMgr::solve() method.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
MINRES linear solver solution manager.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return the linear problem to be solved.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Return the list of current parameters for this object.
MinresSolMgr()
Default constructor.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters to use when solving the linear problem.
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.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return the list of default parameters for this object.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
void reset(const ResetType type) override
Reset the solver manager.
std::string description() const override
int getNumIters() const override
Get the iteration count for the most recent call to solve().
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
virtual ~MinresSolMgr()
Destructor.
Traits class which defines basic operations on multivectors.
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...
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
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.
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Structure to contain pointers to MinresIteration state variables.
Teuchos::RCP< const MV > Y
The current residual.