10#ifndef BELOS_MINRES_SOLMGR_HPP
11#define BELOS_MINRES_SOLMGR_HPP
31#ifdef BELOS_TEUCHOS_TIME_MONITOR
32#include "Teuchos_TimeMonitor.hpp"
35#include "Teuchos_StandardParameterEntryValidators.hpp"
83 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
89 typedef Teuchos::ScalarTraits<ScalarType> SCT;
90 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
91 typedef Teuchos::ScalarTraits< MagnitudeType > MST;
153 const Teuchos::RCP<Teuchos::ParameterList> &
params);
159 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
174 if (defaultParams_.is_null()) {
177 return defaultParams_;
194 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
195 return Teuchos::tuple (timerSolve_);
241 sTest_ = Teuchos::null;
242 parametersSet_ =
false;
254 problem_->setProblem ();
292 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
295 Teuchos::RCP<OutputManager<ScalarType> > printer_;
296 Teuchos::RCP<std::ostream> outputStream_;
304 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
309 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
314 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > convTest_;
319 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > impConvTest_;
324 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > expConvTest_;
330 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
335 Teuchos::RCP<StatusTest<ScalarType, MV, OP, DM> > debugStatusTest_;
340 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
343 Teuchos::RCP<Teuchos::ParameterList> params_;
346 MagnitudeType convtol_;
349 MagnitudeType achievedTol_;
373 Teuchos::RCP<Teuchos::Time> timerSolve_;
388 template<
class ScalarType,
class MV,
class OP,
class DM>
389 Teuchos::RCP<const Teuchos::ParameterList>
392 using Teuchos::ParameterList;
393 using Teuchos::parameterList;
396 using Teuchos::rcpFromRef;
397 using Teuchos::EnhancedNumberValidator;
398 typedef MagnitudeType MT;
403 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
404 "Relative residual tolerance that needs to be achieved by "
405 "the iterative solver, in order for the linear system to be "
406 "declared converged.",
408 pl->set (
"Maximum Iterations",
static_cast<int>(1000),
409 "Maximum number of iterations allowed for each right-hand "
412 pl->set (
"Num Blocks",
static_cast<int> (-1),
413 "Ignored, but permitted, for compatibility with other Belos "
415 pl->set (
"Block Size",
static_cast<int> (1),
416 "Number of vectors in each block. WARNING: The current "
417 "implementation of MINRES only accepts a block size of 1, "
418 "since it can only solve for 1 right-hand side at a time.",
421 "The type(s) of solver information that should "
422 "be written to the output stream.");
424 "What style is used for the solver information written "
425 "to the output stream.");
426 pl->set (
"Output Frequency",
static_cast<int>(-1),
427 "How often (in terms of number of iterations) intermediate "
428 "convergence information should be written to the output stream."
431 "A reference-counted pointer to the output stream where all "
432 "solver output is sent. The output stream defaults to stdout.");
433 pl->set (
"Timer Label", std::string(
"Belos"),
434 "The string to use as a prefix for the timer labels.");
441 template<
class ScalarType,
class MV,
class OP,
class DM>
451 parametersSet_ (
false)
457 template<
class ScalarType,
class MV,
class OP,
class DM>
460 const Teuchos::RCP<Teuchos::ParameterList>&
params) :
463 parametersSet_ (
false)
466 "MinresSolMgr: The version of the constructor "
467 "that takes a LinearProblem to solve was given a "
468 "null LinearProblem.");
472 template<
class ScalarType,
class MV,
class OP,
class DM>
478 "MINRES requires that you have provided a nonnull LinearProblem to the "
479 "solver manager, before you call the solve() method.");
482 "MINRES requires a LinearProblem object with a non-null operator (the "
486 "MINRES requires a LinearProblem object with a non-null right-hand side.");
489 "MINRES requires that before you give it a LinearProblem to solve, you "
490 "must first call the linear problem's setProblem() method.");
493 template<
class ScalarType,
class MV,
class OP,
class DM>
497 using Teuchos::ParameterList;
498 using Teuchos::parameterList;
501 using Teuchos::rcpFromRef;
503 using Teuchos::is_null;
508 if (params_.is_null()) {
512 pl->validateParametersAndSetDefaults (*params_);
518 blockSize_ =
pl->get<
int> (
"Block Size");
519 verbosity_ =
pl->get<
int> (
"Verbosity");
520 outputStyle_ =
pl->get<
int> (
"Output Style");
521 outputFreq_ =
pl->get<
int>(
"Output Frequency");
523 convtol_ =
pl->get<MagnitudeType> (
"Convergence Tolerance");
524 maxIters_ =
pl->get<
int> (
"Maximum Iterations");
532 const string newLabel =
pl->get<
string> (
"Timer Label");
534 if (
newLabel != label_ || timerSolve_.is_null()) {
536#ifdef BELOS_TEUCHOS_TIME_MONITOR
537 const string solveLabel = label_ +
": MinresSolMgr total solve time";
539 if (! timerSolve_.is_null()) {
540 Teuchos::TimeMonitor::clearCounter (label_);
541 timerSolve_ = Teuchos::null;
543 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (
solveLabel);
550 if (printer_.is_null()) {
555 printer_->setVerbosity (verbosity_);
557 printer_->setOStream (outputStream_);
569 impConvTest_.is_null() || expConvTest_.is_null();
573 if (impConvTest_.is_null()) {
575 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
580 impConvTest_->setTolerance (convtol_);
585 if (expConvTest_.is_null()) {
587 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
592 expConvTest_->setTolerance (convtol_);
603 convTest_ =
rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
611 if (maxIterTest_.is_null()) {
615 maxIterTest_->setMaxIters (maxIters_);
627 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
634 if (Teuchos::nonnull(debugStatusTest_)) {
635 sTest_ =
rcp (
new combo_type (combo_type::OR, sTest_, debugStatusTest_));
644 outputTest_ =
stoFactory.create (printer_, sTest_, outputFreq_,
647 outputTest_->setOutputFrequency (outputFreq_);
651 outputTest_->setSolverDesc (std::string (
" MINRES "));
654 parametersSet_ =
true;
656 if (verbosity_ &
Debug) {
659 std::ostream&
dbg = printer_->stream (
Debug);
660 dbg <<
"MINRES parameters:" << endl << params_ << endl;
665 template<
class ScalarType,
class MV,
class OP,
class DM>
670 using Teuchos::rcp_const_cast;
675 if (! parametersSet_) {
676 setParameters (params_);
678 std::ostream&
dbg = printer_->stream (
Debug);
680#ifdef BELOS_TEUCHOS_TIME_MONITOR
685 validateProblem (problem_);
688 outputTest_->reset();
693 const int numRHS2Solve = MVT::GetNumberVecs (*(problem_->getRHS()));
699 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
719 dbg <<
"-- Current right-hand side index being solved: "
725 outputTest_->resetNumCalls();
741 if (convTest_->getStatus() ==
Passed) {
742 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
743 <<
" iterations" << endl;
747 else if (maxIterTest_->getStatus() ==
Passed) {
749 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
750 <<
" iterations" << endl;
756 else if (Teuchos::nonnull(debugStatusTest_) &&
757 debugStatusTest_->getStatus() ==
Passed) {
770 "Belos::MinresSolMgr::solve(): iterations neither converged, "
771 "nor reached the maximum number of iterations " << maxIters_
772 <<
". That means something went wrong.");
778 achievedTol_ = MST::one();
779 Teuchos::RCP<MV>
X = problem_->getLHS();
780 MVT::MvInit( *
X, SCT::zero() );
781 printer_->stream(
Warnings) <<
"Belos::MinresSolMgr::solve(): Warning! NaN has been detected!"
785 catch (
const std::exception &
e) {
788 <<
"Error! Caught std::exception in MinresIter::iterate() at "
789 <<
"iteration " <<
minres_iter->getNumIters() << endl
798 problem_->setCurrLS();
802 numIters_ += maxIterTest_->getNumIters();
810#ifdef BELOS_TEUCHOS_TIME_MONITOR
815 Teuchos::TimeMonitor::summarize (printer_->stream (
TimingDetails));
829 const std::vector<MagnitudeType>*
pTestValues = expConvTest_->getTestValue();
834 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
835 "method returned NULL. Please report this bug to the Belos developers.");
837 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
838 "method returned a vector of length zero. Please report this bug to the "
839 "Belos developers.");
855 template<
class ScalarType,
class MV,
class OP,
class DM>
858 std::ostringstream
oss;
859 oss <<
"Belos::MinresSolMgr< "
860 << Teuchos::ScalarTraits<ScalarType>::name()
870#ifdef HAVE_BELOS_TPETRA
873#define BELOS_TPETRA_MINRESSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
874 BELOS_TPETRA_CALL(Belos::MinresSolMgr, SC, LO, GO, NT)
876#define BELOS_TPETRA_MINRESSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
877 BELOS_TPETRA_EXTERN_CALL(Belos::MinresSolMgr, SC, LO, GO, NT)
879TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_MINRESSOLMGR_EXTERN_CALL)
Belos header file which uses auto-configuration information to include necessary C++ headers.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
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.
Full specialization of Belos::DenseMatTraits for Teuchos::SerialDenseMatrix with ordinal type int and...
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
MINRES linear solver solution manager.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
void reset(const ResetType type) override
Reset the solver manager.
void setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &debugStatusTest) override
Set a debug status test, OR-combined into the top-level status test.
bool isLOADetected() const override
Whether a loss of accuracy was detected in the solver.
virtual ~MinresSolMgr()
Destructor.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Return the list of current parameters for this object.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
std::string description() const override
int getNumIters() const override
Get the iteration count for the most recent call to solve().
MinresSolMgr()
Default constructor.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return the list of default parameters for this object.
ReturnType solve() override
Iterate until the status test tells us to stop.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return the linear problem to be solved.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
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.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.