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 {
223 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
260 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
override;
309 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
321 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
sTest_;
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;
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>
469 template<
class ScalarType,
class MV,
class OP>
472 const Teuchos::RCP<Teuchos::ParameterList>& params) :
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;
525 RCP<ParameterList> pl = params;
526 pl->validateParametersAndSetDefaults (*
params_);
536 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
538 maxIters_ = pl->get<
int> (
"Maximum Iterations");
546 const string newLabel = pl->get<
string> (
"Timer Label");
550#ifdef BELOS_TEUCHOS_TIME_MONITOR
551 const string solveLabel =
label_ +
": MinresSolMgr total solve time";
554 Teuchos::TimeMonitor::clearCounter (
label_);
557 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
563 bool recreatedPrinter =
false;
566 recreatedPrinter =
true;
582 const bool allocatedConvergenceTests =
612 bool needToRecreateFullStatusTest =
sTest_.is_null();
616 if (
convTest_.is_null() || allocatedConvergenceTests) {
618 needToRecreateFullStatusTest =
true;
627 needToRecreateFullStatusTest =
true;
640 if (needToRecreateFullStatusTest) {
648 if (
outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
657 outputTest_->setSolverDesc (std::string (
" MINRES "));
666 dbg <<
"MINRES parameters:" << endl <<
params_ << endl;
671 template<
class ScalarType,
class MV,
class OP>
676 using Teuchos::rcp_const_cast;
684#ifdef BELOS_TEUCHOS_TIME_MONITOR
685 Teuchos::TimeMonitor solveTimerMonitor (*
timerSolve_);
702 RCP<iter_type> minres_iter =
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();
736 minres_iter->initializeMinres (newstate);
742 minres_iter->iterate();
746 dbg <<
"---- Converged after " <<
maxIterTest_->getNumIters()
747 <<
" iterations" << endl;
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
790#ifdef BELOS_TEUCHOS_TIME_MONITOR
809 const std::vector<MagnitudeType>* pTestValues =
expConvTest_->getTestValue();
810 if (pTestValues == NULL || pTestValues->size() < 1) {
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.
BelosError(const std::string &what_arg)
This subclass of std::exception may be thrown from the MinresSolMgr::solve() method.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return the linear problem to be solved.
Teuchos::RCP< OutputManager< ScalarType > > printer_
Output manager.
int outputFreq_
Current frequency of output.
Teuchos::ScalarTraits< ScalarType > SCT
int verbosity_
Current output verbosity.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > expConvTest_
The explicit residual norm test.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Return the list of current parameters for this object.
int maxIters_
Maximum number of iterations before stopping.
std::string label_
Timer label.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
Teuchos::ScalarTraits< MagnitudeType > MST
MagnitudeType convtol_
Current relative residual 2-norm convergence tolerance.
int numIters_
Current number of iterations.
MinresSolMgr()
Default constructor.
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > convTest_
The combined status test for convergence.
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.
int blockSize_
Current block size (i.e., number of right-hand sides): always 1 (one).
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
The status test for maximum iteration count.
bool isLOADetected() const override
Whether a loss of accuracy was detected in the solver.
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
Linear problem to solve.
bool parametersSet_
Whether the solver manager's parameters have been set.
Teuchos::RCP< Teuchos::ParameterList > params_
List of current parameters.
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.
int outputStyle_
Current output style.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
The "status test" that handles output.
MagnitudeType achievedTol_
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< const Teuchos::ParameterList > defaultParams_
List of default parameters.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
static void validateProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Validate the given linear problem.
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > impConvTest_
The implicit (a.k.a.
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
The full status test.
void reset(const ResetType type) override
Reset the solver manager.
MultiVecTraits< ScalarType, MV > MVT
Teuchos::RCP< Teuchos::Time > timerSolve_
Total time to solution.
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.
Teuchos::RCP< std::ostream > outputStream_
virtual ~MinresSolMgr()
Destructor.
Traits class which defines basic operations on multivectors.
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 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.
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.