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 > MT;
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;
411 typedef Teuchos::ScalarTraits<MT> MST;
414 RCP<ParameterList> pl = parameterList (
"MINRES");
416 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
417 "Relative residual tolerance that needs to be achieved by "
418 "the iterative solver, in order for the linear system to be "
419 "declared converged.",
420 rcp (
new EnhancedNumberValidator<MT> (MST::zero(), MST::rmax())));
421 pl->set (
"Maximum Iterations",
static_cast<int>(1000),
422 "Maximum number of iterations allowed for each right-hand "
424 rcp (
new EnhancedNumberValidator<int> (0, INT_MAX)));
425 pl->set (
"Num Blocks",
static_cast<int> (-1),
426 "Ignored, but permitted, for compatibility with other Belos "
428 pl->set (
"Block Size",
static_cast<int> (1),
429 "Number of vectors in each block. WARNING: The current "
430 "implementation of MINRES only accepts a block size of 1, "
431 "since it can only solve for 1 right-hand side at a time.",
432 rcp (
new EnhancedNumberValidator<int> (1, 1)));
434 "The type(s) of solver information that should "
435 "be written to the output stream.");
437 "What style is used for the solver information written "
438 "to the output stream.");
439 pl->set (
"Output Frequency",
static_cast<int>(-1),
440 "How often (in terms of number of iterations) intermediate "
441 "convergence information should be written to the output stream."
443 pl->set (
"Output Stream", rcpFromRef(std::cout),
444 "A reference-counted pointer to the output stream where all "
445 "solver output is sent. The output stream defaults to stdout.");
446 pl->set (
"Timer Label", std::string(
"Belos"),
447 "The string to use as a prefix for the timer labels.");
454 template<
class ScalarType,
class MV,
class OP>
464 parametersSet_ (false)
470 template<
class ScalarType,
class MV,
class OP>
473 const Teuchos::RCP<Teuchos::ParameterList>& params) :
476 parametersSet_ (false)
478 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
479 "MinresSolMgr: The version of the constructor "
480 "that takes a LinearProblem to solve was given a "
481 "null LinearProblem.");
485 template<
class ScalarType,
class MV,
class OP>
490 TEUCHOS_TEST_FOR_EXCEPTION(problem.is_null(),
492 "MINRES requires that you have provided a nonnull LinearProblem to the "
493 "solver manager, before you call the solve() method.");
494 TEUCHOS_TEST_FOR_EXCEPTION(problem->getOperator().is_null(),
496 "MINRES requires a LinearProblem object with a non-null operator (the "
498 TEUCHOS_TEST_FOR_EXCEPTION(problem->getRHS().is_null(),
500 "MINRES requires a LinearProblem object with a non-null right-hand side.");
501 TEUCHOS_TEST_FOR_EXCEPTION( ! problem->isProblemSet(),
503 "MINRES requires that before you give it a LinearProblem to solve, you "
504 "must first call the linear problem's setProblem() method.");
507 template<
class ScalarType,
class MV,
class OP>
510 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
512 using Teuchos::ParameterList;
513 using Teuchos::parameterList;
516 using Teuchos::rcpFromRef;
518 using Teuchos::is_null;
523 if (params_.is_null()) {
524 params_ = parameterList (*getValidParameters());
526 RCP<ParameterList> pl = params;
527 pl->validateParametersAndSetDefaults (*params_);
533 blockSize_ = pl->get<
int> (
"Block Size");
534 verbosity_ = pl->get<
int> (
"Verbosity");
535 outputStyle_ = pl->get<
int> (
"Output Style");
536 outputFreq_ = pl->get<
int>(
"Output Frequency");
537 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
538 convtol_ = pl->get<MagnitudeType> (
"Convergence Tolerance");
539 maxIters_ = pl->get<
int> (
"Maximum Iterations");
547 const string newLabel = pl->get<
string> (
"Timer Label");
549 if (newLabel != label_ || timerSolve_.is_null()) {
551 #ifdef BELOS_TEUCHOS_TIME_MONITOR
552 const string solveLabel = label_ +
": MinresSolMgr total solve time";
554 if (! timerSolve_.is_null()) {
555 Teuchos::TimeMonitor::clearCounter (label_);
556 timerSolve_ = Teuchos::null;
558 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
564 bool recreatedPrinter =
false;
565 if (printer_.is_null()) {
567 recreatedPrinter =
true;
570 printer_->setVerbosity (verbosity_);
572 printer_->setOStream (outputStream_);
583 const bool allocatedConvergenceTests =
584 impConvTest_.is_null() || expConvTest_.is_null();
588 if (impConvTest_.is_null()) {
589 impConvTest_ = rcp (
new res_norm_type (convtol_));
590 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
595 impConvTest_->setTolerance (convtol_);
600 if (expConvTest_.is_null()) {
601 expConvTest_ = rcp (
new res_norm_type (convtol_));
602 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
607 expConvTest_->setTolerance (convtol_);
613 bool needToRecreateFullStatusTest = sTest_.is_null();
617 if (convTest_.is_null() || allocatedConvergenceTests) {
618 convTest_ = rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
619 needToRecreateFullStatusTest =
true;
626 if (maxIterTest_.is_null()) {
628 needToRecreateFullStatusTest =
true;
630 maxIterTest_->setMaxIters (maxIters_);
641 if (needToRecreateFullStatusTest) {
642 sTest_ = rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
649 if (outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
651 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
654 outputTest_->setOutputFrequency (outputFreq_);
658 outputTest_->setSolverDesc (std::string (
" MINRES "));
661 parametersSet_ =
true;
663 if (verbosity_ &
Debug) {
666 std::ostream& dbg = printer_->stream (
Debug);
667 dbg <<
"MINRES parameters:" << endl << params_ << endl;
672 template<
class ScalarType,
class MV,
class OP>
677 using Teuchos::rcp_const_cast;
680 if (! parametersSet_) {
681 setParameters (params_);
683 std::ostream& dbg = printer_->stream (
Debug);
685 #ifdef BELOS_TEUCHOS_TIME_MONITOR
686 Teuchos::TimeMonitor solveTimerMonitor (*timerSolve_);
690 validateProblem (problem_);
693 outputTest_->reset();
698 const int numRHS2Solve = MVT::GetNumberVecs (*(problem_->getRHS()));
703 RCP<iter_type> minres_iter =
704 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
710 std::vector<int> notConverged;
711 std::vector<int> currentIndices(1);
716 for (
int currentRHS = 0; currentRHS < numRHS2Solve; ++currentRHS) {
721 currentIndices[0] = currentRHS;
722 problem_->setLSIndex (currentIndices);
724 dbg <<
"-- Current right-hand side index being solved: "
725 << currentRHS << endl;
728 minres_iter->resetNumIters();
730 outputTest_->resetNumCalls();
736 newstate.
Y = MVT::CloneViewNonConst (*(rcp_const_cast<MV> (problem_->getInitResVec())), currentIndices);
737 minres_iter->initializeMinres (newstate);
743 minres_iter->iterate();
746 if (convTest_->getStatus() ==
Passed) {
747 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
748 <<
" iterations" << endl;
752 else if (maxIterTest_->getStatus() ==
Passed) {
753 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
754 <<
" iterations" << endl;
756 notConverged.push_back (currentRHS);
762 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
763 "Belos::MinresSolMgr::solve(): iterations neither converged, "
764 "nor reached the maximum number of iterations " << maxIters_
765 <<
". That means something went wrong.");
767 }
catch (
const std::exception &e) {
769 <<
"Error! Caught std::exception in MinresIter::iterate() at "
770 <<
"iteration " << minres_iter->getNumIters() << endl
779 problem_->setCurrLS();
783 numIters_ += maxIterTest_->getNumIters();
791 #ifdef BELOS_TEUCHOS_TIME_MONITOR
796 Teuchos::TimeMonitor::summarize (printer_->stream (
TimingDetails));
810 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
811 if (pTestValues == NULL || pTestValues->size() < 1) {
812 pTestValues = impConvTest_->getTestValue();
814 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
815 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
816 "method returned NULL. Please report this bug to the Belos developers.");
817 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
818 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
819 "method returned a vector of length zero. Please report this bug to the "
820 "Belos developers.");
825 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
828 if (notConverged.size() > 0) {
836 template<
class ScalarType,
class MV,
class OP>
839 std::ostringstream oss;
840 oss <<
"Belos::MinresSolMgr< "
841 << 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.
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.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
bool isLOADetected() const override
Whether a loss of accuracy was detected in the solver.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Return the list of current parameters for this object.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return the linear problem to be solved.
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.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
std::string description() const override
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return the list of default parameters for this object.
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.