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_);
242 problem_->setProblem ();
280 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
283 Teuchos::RCP<OutputManager<ScalarType> > printer_;
284 Teuchos::RCP<std::ostream> outputStream_;
292 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
297 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
302 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > convTest_;
307 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > impConvTest_;
312 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > expConvTest_;
318 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
323 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
326 Teuchos::RCP<Teuchos::ParameterList> params_;
329 MagnitudeType convtol_;
332 MagnitudeType achievedTol_;
356 Teuchos::RCP<Teuchos::Time> timerSolve_;
371 template<
class ScalarType,
class MV,
class OP,
class DM>
372 Teuchos::RCP<const Teuchos::ParameterList>
375 using Teuchos::ParameterList;
376 using Teuchos::parameterList;
379 using Teuchos::rcpFromRef;
380 using Teuchos::EnhancedNumberValidator;
381 typedef MagnitudeType MT;
386 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
387 "Relative residual tolerance that needs to be achieved by "
388 "the iterative solver, in order for the linear system to be "
389 "declared converged.",
391 pl->set (
"Maximum Iterations",
static_cast<int>(1000),
392 "Maximum number of iterations allowed for each right-hand "
395 pl->set (
"Num Blocks",
static_cast<int> (-1),
396 "Ignored, but permitted, for compatibility with other Belos "
398 pl->set (
"Block Size",
static_cast<int> (1),
399 "Number of vectors in each block. WARNING: The current "
400 "implementation of MINRES only accepts a block size of 1, "
401 "since it can only solve for 1 right-hand side at a time.",
404 "The type(s) of solver information that should "
405 "be written to the output stream.");
407 "What style is used for the solver information written "
408 "to the output stream.");
409 pl->set (
"Output Frequency",
static_cast<int>(-1),
410 "How often (in terms of number of iterations) intermediate "
411 "convergence information should be written to the output stream."
414 "A reference-counted pointer to the output stream where all "
415 "solver output is sent. The output stream defaults to stdout.");
416 pl->set (
"Timer Label", std::string(
"Belos"),
417 "The string to use as a prefix for the timer labels.");
424 template<
class ScalarType,
class MV,
class OP,
class DM>
434 parametersSet_ (
false)
440 template<
class ScalarType,
class MV,
class OP,
class DM>
443 const Teuchos::RCP<Teuchos::ParameterList>&
params) :
446 parametersSet_ (
false)
449 "MinresSolMgr: The version of the constructor "
450 "that takes a LinearProblem to solve was given a "
451 "null LinearProblem.");
455 template<
class ScalarType,
class MV,
class OP,
class DM>
461 "MINRES requires that you have provided a nonnull LinearProblem to the "
462 "solver manager, before you call the solve() method.");
465 "MINRES requires a LinearProblem object with a non-null operator (the "
469 "MINRES requires a LinearProblem object with a non-null right-hand side.");
472 "MINRES requires that before you give it a LinearProblem to solve, you "
473 "must first call the linear problem's setProblem() method.");
476 template<
class ScalarType,
class MV,
class OP,
class DM>
480 using Teuchos::ParameterList;
481 using Teuchos::parameterList;
484 using Teuchos::rcpFromRef;
486 using Teuchos::is_null;
491 if (params_.is_null()) {
495 pl->validateParametersAndSetDefaults (*params_);
501 blockSize_ =
pl->get<
int> (
"Block Size");
502 verbosity_ =
pl->get<
int> (
"Verbosity");
503 outputStyle_ =
pl->get<
int> (
"Output Style");
504 outputFreq_ =
pl->get<
int>(
"Output Frequency");
506 convtol_ =
pl->get<MagnitudeType> (
"Convergence Tolerance");
507 maxIters_ =
pl->get<
int> (
"Maximum Iterations");
515 const string newLabel =
pl->get<
string> (
"Timer Label");
517 if (
newLabel != label_ || timerSolve_.is_null()) {
519#ifdef BELOS_TEUCHOS_TIME_MONITOR
520 const string solveLabel = label_ +
": MinresSolMgr total solve time";
522 if (! timerSolve_.is_null()) {
523 Teuchos::TimeMonitor::clearCounter (label_);
524 timerSolve_ = Teuchos::null;
526 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (
solveLabel);
533 if (printer_.is_null()) {
538 printer_->setVerbosity (verbosity_);
540 printer_->setOStream (outputStream_);
552 impConvTest_.is_null() || expConvTest_.is_null();
556 if (impConvTest_.is_null()) {
558 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
563 impConvTest_->setTolerance (convtol_);
568 if (expConvTest_.is_null()) {
570 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
575 expConvTest_->setTolerance (convtol_);
586 convTest_ =
rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
594 if (maxIterTest_.is_null()) {
598 maxIterTest_->setMaxIters (maxIters_);
610 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
619 outputTest_ =
stoFactory.create (printer_, sTest_, outputFreq_,
622 outputTest_->setOutputFrequency (outputFreq_);
626 outputTest_->setSolverDesc (std::string (
" MINRES "));
629 parametersSet_ =
true;
631 if (verbosity_ &
Debug) {
634 std::ostream&
dbg = printer_->stream (
Debug);
635 dbg <<
"MINRES parameters:" << endl << params_ << endl;
640 template<
class ScalarType,
class MV,
class OP,
class DM>
645 using Teuchos::rcp_const_cast;
650 if (! parametersSet_) {
651 setParameters (params_);
653 std::ostream&
dbg = printer_->stream (
Debug);
655#ifdef BELOS_TEUCHOS_TIME_MONITOR
660 validateProblem (problem_);
663 outputTest_->reset();
668 const int numRHS2Solve = MVT::GetNumberVecs (*(problem_->getRHS()));
674 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
694 dbg <<
"-- Current right-hand side index being solved: "
700 outputTest_->resetNumCalls();
716 if (convTest_->getStatus() ==
Passed) {
717 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
718 <<
" iterations" << endl;
722 else if (maxIterTest_->getStatus() ==
Passed) {
724 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
725 <<
" iterations" << endl;
735 "Belos::MinresSolMgr::solve(): iterations neither converged, "
736 "nor reached the maximum number of iterations " << maxIters_
737 <<
". That means something went wrong.");
743 achievedTol_ = MST::one();
744 Teuchos::RCP<MV>
X = problem_->getLHS();
745 MVT::MvInit( *
X, SCT::zero() );
746 printer_->stream(
Warnings) <<
"Belos::MinresSolMgr::solve(): Warning! NaN has been detected!"
750 catch (
const std::exception &
e) {
753 <<
"Error! Caught std::exception in MinresIter::iterate() at "
754 <<
"iteration " <<
minres_iter->getNumIters() << endl
763 problem_->setCurrLS();
767 numIters_ += maxIterTest_->getNumIters();
775#ifdef BELOS_TEUCHOS_TIME_MONITOR
780 Teuchos::TimeMonitor::summarize (printer_->stream (
TimingDetails));
794 const std::vector<MagnitudeType>*
pTestValues = expConvTest_->getTestValue();
799 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
800 "method returned NULL. Please report this bug to the Belos developers.");
802 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
803 "method returned a vector of length zero. Please report this bug to the "
804 "Belos developers.");
820 template<
class ScalarType,
class MV,
class OP,
class DM>
823 std::ostringstream
oss;
824 oss <<
"Belos::MinresSolMgr< "
825 << Teuchos::ScalarTraits<ScalarType>::name()
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.
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.