10#ifndef BELOS_MINRES_SOLMGR_HPP
11#define BELOS_MINRES_SOLMGR_HPP
28#ifdef BELOS_TEUCHOS_TIME_MONITOR
29#include "Teuchos_TimeMonitor.hpp"
32#include "Teuchos_StandardParameterEntryValidators.hpp"
80 template<
class ScalarType,
class MV,
class OP>
86 typedef Teuchos::ScalarTraits<ScalarType> SCT;
87 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
88 typedef Teuchos::ScalarTraits< MagnitudeType > MST;
150 const Teuchos::RCP<Teuchos::ParameterList> ¶ms);
156 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
171 if (defaultParams_.is_null()) {
174 return defaultParams_;
191 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
192 return Teuchos::tuple (timerSolve_);
228 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
override;
239 problem_->setProblem ();
277 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
280 Teuchos::RCP<OutputManager<ScalarType> > printer_;
281 Teuchos::RCP<std::ostream> outputStream_;
289 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
294 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
299 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
304 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > impConvTest_;
309 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_;
315 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
320 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
323 Teuchos::RCP<Teuchos::ParameterList> params_;
326 MagnitudeType convtol_;
329 MagnitudeType achievedTol_;
353 Teuchos::RCP<Teuchos::Time> timerSolve_;
368 template<
class ScalarType,
class MV,
class OP>
369 Teuchos::RCP<const Teuchos::ParameterList>
372 using Teuchos::ParameterList;
373 using Teuchos::parameterList;
376 using Teuchos::rcpFromRef;
377 using Teuchos::EnhancedNumberValidator;
378 typedef MagnitudeType MT;
381 RCP<ParameterList> pl = parameterList (
"MINRES");
383 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
384 "Relative residual tolerance that needs to be achieved by "
385 "the iterative solver, in order for the linear system to be "
386 "declared converged.",
387 rcp (
new EnhancedNumberValidator<MT> (MST::zero(), MST::rmax())));
388 pl->set (
"Maximum Iterations",
static_cast<int>(1000),
389 "Maximum number of iterations allowed for each right-hand "
391 rcp (
new EnhancedNumberValidator<int> (0, INT_MAX)));
392 pl->set (
"Num Blocks",
static_cast<int> (-1),
393 "Ignored, but permitted, for compatibility with other Belos "
395 pl->set (
"Block Size",
static_cast<int> (1),
396 "Number of vectors in each block. WARNING: The current "
397 "implementation of MINRES only accepts a block size of 1, "
398 "since it can only solve for 1 right-hand side at a time.",
399 rcp (
new EnhancedNumberValidator<int> (1, 1)));
401 "The type(s) of solver information that should "
402 "be written to the output stream.");
404 "What style is used for the solver information written "
405 "to the output stream.");
406 pl->set (
"Output Frequency",
static_cast<int>(-1),
407 "How often (in terms of number of iterations) intermediate "
408 "convergence information should be written to the output stream."
410 pl->set (
"Output Stream", rcpFromRef(std::cout),
411 "A reference-counted pointer to the output stream where all "
412 "solver output is sent. The output stream defaults to stdout.");
413 pl->set (
"Timer Label", std::string(
"Belos"),
414 "The string to use as a prefix for the timer labels.");
421 template<
class ScalarType,
class MV,
class OP>
431 parametersSet_ (false)
437 template<
class ScalarType,
class MV,
class OP>
440 const Teuchos::RCP<Teuchos::ParameterList>& params) :
443 parametersSet_ (false)
445 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
446 "MinresSolMgr: The version of the constructor "
447 "that takes a LinearProblem to solve was given a "
448 "null LinearProblem.");
452 template<
class ScalarType,
class MV,
class OP>
457 TEUCHOS_TEST_FOR_EXCEPTION(problem.is_null(),
459 "MINRES requires that you have provided a nonnull LinearProblem to the "
460 "solver manager, before you call the solve() method.");
461 TEUCHOS_TEST_FOR_EXCEPTION(problem->getOperator().is_null(),
463 "MINRES requires a LinearProblem object with a non-null operator (the "
465 TEUCHOS_TEST_FOR_EXCEPTION(problem->getRHS().is_null(),
467 "MINRES requires a LinearProblem object with a non-null right-hand side.");
468 TEUCHOS_TEST_FOR_EXCEPTION( ! problem->isProblemSet(),
470 "MINRES requires that before you give it a LinearProblem to solve, you "
471 "must first call the linear problem's setProblem() method.");
474 template<
class ScalarType,
class MV,
class OP>
477 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
479 using Teuchos::ParameterList;
480 using Teuchos::parameterList;
483 using Teuchos::rcpFromRef;
485 using Teuchos::is_null;
490 if (params_.is_null()) {
493 RCP<ParameterList> pl = params;
494 pl->validateParametersAndSetDefaults (*params_);
500 blockSize_ = pl->get<
int> (
"Block Size");
501 verbosity_ = pl->get<
int> (
"Verbosity");
502 outputStyle_ = pl->get<
int> (
"Output Style");
503 outputFreq_ = pl->get<
int>(
"Output Frequency");
504 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
505 convtol_ = pl->get<MagnitudeType> (
"Convergence Tolerance");
506 maxIters_ = pl->get<
int> (
"Maximum Iterations");
514 const string newLabel = pl->get<
string> (
"Timer Label");
516 if (newLabel != label_ || timerSolve_.is_null()) {
518#ifdef BELOS_TEUCHOS_TIME_MONITOR
519 const string solveLabel = label_ +
": MinresSolMgr total solve time";
521 if (! timerSolve_.is_null()) {
522 Teuchos::TimeMonitor::clearCounter (label_);
523 timerSolve_ = Teuchos::null;
525 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
531 bool recreatedPrinter =
false;
532 if (printer_.is_null()) {
534 recreatedPrinter =
true;
537 printer_->setVerbosity (verbosity_);
539 printer_->setOStream (outputStream_);
550 const bool allocatedConvergenceTests =
551 impConvTest_.is_null() || expConvTest_.is_null();
555 if (impConvTest_.is_null()) {
556 impConvTest_ = rcp (
new res_norm_type (convtol_));
557 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
562 impConvTest_->setTolerance (convtol_);
567 if (expConvTest_.is_null()) {
568 expConvTest_ = rcp (
new res_norm_type (convtol_));
569 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
574 expConvTest_->setTolerance (convtol_);
580 bool needToRecreateFullStatusTest = sTest_.is_null();
584 if (convTest_.is_null() || allocatedConvergenceTests) {
585 convTest_ = rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
586 needToRecreateFullStatusTest =
true;
593 if (maxIterTest_.is_null()) {
595 needToRecreateFullStatusTest =
true;
597 maxIterTest_->setMaxIters (maxIters_);
608 if (needToRecreateFullStatusTest) {
609 sTest_ = rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
616 if (outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
618 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
621 outputTest_->setOutputFrequency (outputFreq_);
625 outputTest_->setSolverDesc (std::string (
" MINRES "));
628 parametersSet_ =
true;
630 if (verbosity_ &
Debug) {
633 std::ostream& dbg = printer_->stream (
Debug);
634 dbg <<
"MINRES parameters:" << endl << params_ << endl;
639 template<
class ScalarType,
class MV,
class OP>
644 using Teuchos::rcp_const_cast;
647 if (! parametersSet_) {
650 std::ostream& dbg = printer_->stream (
Debug);
652#ifdef BELOS_TEUCHOS_TIME_MONITOR
653 Teuchos::TimeMonitor solveTimerMonitor (*timerSolve_);
657 validateProblem (problem_);
660 outputTest_->reset();
670 RCP<iter_type> minres_iter =
671 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
677 std::vector<int> notConverged;
678 std::vector<int> currentIndices(1);
683 for (
int currentRHS = 0; currentRHS < numRHS2Solve; ++currentRHS) {
688 currentIndices[0] = currentRHS;
689 problem_->setLSIndex (currentIndices);
691 dbg <<
"-- Current right-hand side index being solved: "
692 << currentRHS << endl;
695 minres_iter->resetNumIters();
697 outputTest_->resetNumCalls();
704 minres_iter->initializeMinres (newstate);
710 minres_iter->iterate();
713 if (convTest_->getStatus() ==
Passed) {
714 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
715 <<
" iterations" << endl;
719 else if (maxIterTest_->getStatus() ==
Passed) {
720 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
721 <<
" iterations" << endl;
723 notConverged.push_back (currentRHS);
729 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
730 "Belos::MinresSolMgr::solve(): iterations neither converged, "
731 "nor reached the maximum number of iterations " << maxIters_
732 <<
". That means something went wrong.");
737 achievedTol_ = MST::one();
738 Teuchos::RCP<MV> X = problem_->getLHS();
740 printer_->stream(
Warnings) <<
"Belos::MinresSolMgr::solve(): Warning! NaN has been detected!"
744 catch (
const std::exception &e) {
746 <<
"Error! Caught std::exception in MinresIter::iterate() at "
747 <<
"iteration " << minres_iter->getNumIters() << endl
756 problem_->setCurrLS();
760 numIters_ += maxIterTest_->getNumIters();
768#ifdef BELOS_TEUCHOS_TIME_MONITOR
773 Teuchos::TimeMonitor::summarize (printer_->stream (
TimingDetails));
787 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
788 if (pTestValues == NULL || pTestValues->size() < 1) {
789 pTestValues = impConvTest_->getTestValue();
791 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
792 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
793 "method returned NULL. Please report this bug to the Belos developers.");
794 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
795 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
796 "method returned a vector of length zero. Please report this bug to the "
797 "Belos developers.");
802 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
805 if (notConverged.size() > 0) {
813 template<
class ScalarType,
class MV,
class OP>
816 std::ostringstream oss;
817 oss <<
"Belos::MinresSolMgr< "
818 << 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)
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.
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 void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
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.