42 #ifndef BELOS_MINRES_SOLMGR_HPP
43 #define BELOS_MINRES_SOLMGR_HPP
62 #ifdef BELOS_TEUCHOS_TIME_MONITOR
66 #include "Teuchos_StandardParameterEntryValidators.hpp"
114 template<
class ScalarType,
class MV,
class OP>
195 if (defaultParams_.
is_null()) {
198 return defaultParams_;
216 return Teuchos::tuple (timerSolve_);
263 problem_->setProblem ();
350 MagnitudeType convtol_;
353 MagnitudeType achievedTol_;
392 template<
class ScalarType,
class MV,
class OP>
397 using Teuchos::parameterList;
400 using Teuchos::rcpFromRef;
402 typedef MagnitudeType
MT;
406 RCP<ParameterList> pl = parameterList (
"MINRES");
408 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
409 "Relative residual tolerance that needs to be achieved by "
410 "the iterative solver, in order for the linear system to be "
411 "declared converged.",
412 rcp (
new EnhancedNumberValidator<MT> (MST::zero(), MST::rmax())));
413 pl->set (
"Maximum Iterations", static_cast<int>(1000),
414 "Maximum number of iterations allowed for each right-hand "
416 rcp (
new EnhancedNumberValidator<int> (0, INT_MAX)));
417 pl->set (
"Block Size", static_cast<int>(1),
418 "Number of vectors in each block. WARNING: The current "
419 "implementation of MINRES only accepts a block size of 1, "
420 "since it can only solve for 1 right-hand side at a time.",
421 rcp (
new EnhancedNumberValidator<int> (1, 1)));
423 "The type(s) of solver information that should "
424 "be written to the output stream.");
426 "What style is used for the solver information written "
427 "to the output stream.");
428 pl->set (
"Output Frequency", static_cast<int>(-1),
429 "How often (in terms of number of iterations) intermediate "
430 "convergence information should be written to the output stream."
432 pl->set (
"Output Stream", rcpFromRef(std::cout),
433 "A reference-counted pointer to the output stream where all "
434 "solver output is sent. The output stream defaults to stdout.");
435 pl->set (
"Timer Label", std::string(
"Belos"),
436 "The string to use as a prefix for the timer labels.");
443 template<
class ScalarType,
class MV,
class OP>
446 parametersSet_ (false)
452 template<
class ScalarType,
class MV,
class OP>
458 parametersSet_ (false)
461 "MinresSolMgr: The version of the constructor "
462 "that takes a LinearProblem to solve was given a "
463 "null LinearProblem.");
467 template<
class ScalarType,
class MV,
class OP>
474 "MINRES requires that you have provided a nonnull LinearProblem to the "
475 "solver manager, before you call the solve() method.");
478 "MINRES requires a LinearProblem object with a non-null operator (the "
482 "MINRES requires a LinearProblem object with a non-null right-hand side.");
485 "MINRES requires that before you give it a LinearProblem to solve, you "
486 "must first call the linear problem's setProblem() method.");
489 template<
class ScalarType,
class MV,
class OP>
495 using Teuchos::parameterList;
498 using Teuchos::rcpFromRef;
505 if (params_.is_null()) {
506 params_ = parameterList (*getValidParameters());
508 RCP<ParameterList> pl = params;
515 blockSize_ = pl->get<
int> (
"Block Size");
516 verbosity_ = pl->get<
int> (
"Verbosity");
517 outputStyle_ = pl->get<
int> (
"Output Style");
518 outputFreq_ = pl->get<
int>(
"Output Frequency");
519 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
520 convtol_ = pl->get<MagnitudeType> (
"Convergence Tolerance");
521 maxIters_ = pl->get<
int> (
"Maximum Iterations");
529 const string newLabel = pl->get<
string> (
"Timer Label");
531 if (newLabel != label_ || timerSolve_.is_null()) {
533 #ifdef BELOS_TEUCHOS_TIME_MONITOR
534 const string solveLabel = label_ +
": MinresSolMgr total solve time";
536 if (! timerSolve_.is_null()) {
538 timerSolve_ = Teuchos::null;
541 #endif // BELOS_TEUCHOS_TIME_MONITOR
546 bool recreatedPrinter =
false;
547 if (printer_.is_null()) {
549 recreatedPrinter =
true;
552 printer_->setVerbosity (verbosity_);
554 printer_->setOStream (outputStream_);
565 const bool allocatedConvergenceTests =
566 impConvTest_.is_null() || expConvTest_.is_null();
570 if (impConvTest_.is_null()) {
571 impConvTest_ =
rcp (
new res_norm_type (convtol_));
572 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
577 impConvTest_->setTolerance (convtol_);
582 if (expConvTest_.is_null()) {
583 expConvTest_ =
rcp (
new res_norm_type (convtol_));
584 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
589 expConvTest_->setTolerance (convtol_);
595 bool needToRecreateFullStatusTest = sTest_.is_null();
599 if (convTest_.is_null() || allocatedConvergenceTests) {
600 convTest_ =
rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
601 needToRecreateFullStatusTest =
true;
608 if (maxIterTest_.is_null()) {
610 needToRecreateFullStatusTest =
true;
612 maxIterTest_->setMaxIters (maxIters_);
623 if (needToRecreateFullStatusTest) {
624 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
631 if (outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
633 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
636 outputTest_->setOutputFrequency (outputFreq_);
640 outputTest_->setSolverDesc (std::string (
" MINRES "));
643 parametersSet_ =
true;
645 if (verbosity_ &
Debug) {
648 std::ostream& dbg = printer_->stream (Debug);
649 dbg <<
"MINRES parameters:" << endl << params_ << endl;
654 template<
class ScalarType,
class MV,
class OP>
659 using Teuchos::rcp_const_cast;
662 if (! parametersSet_) {
663 setParameters (params_);
665 std::ostream& dbg = printer_->stream (
Debug);
667 #ifdef BELOS_TEUCHOS_TIME_MONITOR
669 #endif // BELOS_TEUCHOS_TIME_MONITOR
672 validateProblem (problem_);
675 outputTest_->reset();
680 const int numRHS2Solve = MVT::GetNumberVecs (*(problem_->getRHS()));
685 RCP<iter_type> minres_iter =
686 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
692 std::vector<int> notConverged;
693 std::vector<int> currentIndices(1);
698 for (
int currentRHS = 0; currentRHS < numRHS2Solve; ++currentRHS) {
703 currentIndices[0] = currentRHS;
704 problem_->setLSIndex (currentIndices);
706 dbg <<
"-- Current right-hand side index being solved: "
707 << currentRHS << endl;
710 minres_iter->resetNumIters();
712 outputTest_->resetNumCalls();
718 newstate.
Y = MVT::CloneViewNonConst (*(rcp_const_cast<MV> (problem_->getInitResVec())), currentIndices);
719 minres_iter->initializeMinres (newstate);
725 minres_iter->iterate();
728 if (convTest_->getStatus() ==
Passed) {
729 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
730 <<
" iterations" << endl;
734 else if (maxIterTest_->getStatus() ==
Passed) {
735 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
736 <<
" iterations" << endl;
738 notConverged.push_back (currentRHS);
745 "Belos::MinresSolMgr::solve(): iterations neither converged, "
746 "nor reached the maximum number of iterations " << maxIters_
747 <<
". That means something went wrong.");
749 }
catch (
const std::exception &e) {
751 <<
"Error! Caught std::exception in MinresIter::iterate() at "
752 <<
"iteration " << minres_iter->getNumIters() << endl
761 problem_->setCurrLS();
765 numIters_ += maxIterTest_->getNumIters();
773 #ifdef BELOS_TEUCHOS_TIME_MONITOR
780 #endif // BELOS_TEUCHOS_TIME_MONITOR
792 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
793 if (pTestValues == NULL || pTestValues->size() < 1) {
794 pTestValues = impConvTest_->getTestValue();
797 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
798 "method returned NULL. Please report this bug to the Belos developers.");
800 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
801 "method returned a vector of length zero. Please report this bug to the "
802 "Belos developers.");
807 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
810 if (notConverged.size() > 0) {
818 template<
class ScalarType,
class MV,
class OP>
821 std::ostringstream oss;
822 oss <<
"Belos::MinresSolMgr< "
Collection of types and exceptions used within the Belos solvers.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
ReturnType solve()
Iterate until the status test tells us to stop.
Class which manages the output and verbosity of the Belos solvers.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
bool is_null(const boost::shared_ptr< T > &p)
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
virtual ~MinresSolMgr()
Destructor.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
An implementation of StatusTestResNorm using a family of residual norms.
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
void reset(const ResetType type)
Reset the solver manager.
A factory class for generating StatusTestOutput objects.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
This subclass of std::exception may be thrown from the MinresSolMgr::solve() method.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
MINRES linear solver solution manager.
MINRES iteration implementation.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
Structure to contain pointers to MinresIteration state variables.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Return the list of current parameters for this object.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
bool isLOADetected() const
Whether a loss of accuracy was detected in the solver.
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
ReturnType
Whether the Belos solve converged for all linear systems.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
std::string description() const
Teuchos::RCP< const MV > Y
The current residual.
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.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
A class for extending the status testing capabilities of Belos via logical combinations.
MinresSolMgr()
Default constructor.
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
int getNumIters() const
Get the iteration count for the most recent call to solve().
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return the list of default parameters for this object.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return the linear problem to be solved.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters to use when solving the linear problem.