42 #ifndef BELOS_MINRES_SOLMGR_HPP 43 #define BELOS_MINRES_SOLMGR_HPP 60 #include "Teuchos_BLAS.hpp" 61 #include "Teuchos_LAPACK.hpp" 62 #ifdef BELOS_TEUCHOS_TIME_MONITOR 63 #include "Teuchos_TimeMonitor.hpp" 66 #include "Teuchos_StandardParameterEntryValidators.hpp" 114 template<
class ScalarType,
class MV,
class OP>
120 typedef Teuchos::ScalarTraits<ScalarType> SCT;
121 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
122 typedef Teuchos::ScalarTraits< MagnitudeType > MT;
137 static Teuchos::RCP<const Teuchos::ParameterList> defaultParameters();
179 const Teuchos::RCP<Teuchos::ParameterList> ¶ms);
195 if (defaultParams_.is_null()) {
196 defaultParams_ = defaultParameters ();
198 return defaultParams_;
215 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
216 return Teuchos::tuple (timerSolve_);
252 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params);
263 problem_->setProblem ();
295 std::string description()
const;
301 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
304 Teuchos::RCP<OutputManager<ScalarType> > printer_;
305 Teuchos::RCP<std::ostream> outputStream_;
313 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
318 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
323 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
328 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > impConvTest_;
333 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_;
339 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
344 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
347 Teuchos::RCP<Teuchos::ParameterList> params_;
350 MagnitudeType convtol_;
353 MagnitudeType achievedTol_;
377 Teuchos::RCP<Teuchos::Time> timerSolve_;
392 template<
class ScalarType,
class MV,
class OP>
393 Teuchos::RCP<const Teuchos::ParameterList>
396 using Teuchos::ParameterList;
397 using Teuchos::parameterList;
400 using Teuchos::rcpFromRef;
401 using Teuchos::EnhancedNumberValidator;
402 typedef MagnitudeType MT;
403 typedef Teuchos::ScalarTraits<MT> MST;
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>
455 const Teuchos::RCP<Teuchos::ParameterList>& params) :
458 parametersSet_ (false)
460 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
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>
472 TEUCHOS_TEST_FOR_EXCEPTION(problem.is_null(),
474 "MINRES requires that you have provided a nonnull LinearProblem to the " 475 "solver manager, before you call the solve() method.");
476 TEUCHOS_TEST_FOR_EXCEPTION(problem->getOperator().is_null(),
478 "MINRES requires a LinearProblem object with a non-null operator (the " 480 TEUCHOS_TEST_FOR_EXCEPTION(problem->getRHS().is_null(),
482 "MINRES requires a LinearProblem object with a non-null right-hand side.");
483 TEUCHOS_TEST_FOR_EXCEPTION( ! problem->isProblemSet(),
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>
494 using Teuchos::ParameterList;
495 using Teuchos::parameterList;
498 using Teuchos::rcpFromRef;
500 using Teuchos::is_null;
505 if (params_.is_null()) {
508 RCP<ParameterList> pl = params;
509 pl->validateParametersAndSetDefaults (*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()) {
537 Teuchos::TimeMonitor::clearCounter (label_);
538 timerSolve_ = Teuchos::null;
540 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
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_) {
665 std::ostream& dbg = printer_->stream (
Debug);
667 #ifdef BELOS_TEUCHOS_TIME_MONITOR 668 Teuchos::TimeMonitor solveTimerMonitor (*timerSolve_);
669 #endif // BELOS_TEUCHOS_TIME_MONITOR 672 validateProblem (problem_);
675 outputTest_->reset();
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();
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);
744 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
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 778 Teuchos::TimeMonitor::summarize (printer_->stream (TimingDetails));
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();
796 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
797 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() " 798 "method returned NULL. Please report this bug to the Belos developers.");
799 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
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< " 823 << Teuchos::ScalarTraits<ScalarType>::name()
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.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
virtual ~MinresSolMgr()
Destructor.
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.
void reset(const ResetType type)
Reset the solver manager.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
A factory class for generating StatusTestOutput objects.
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.
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).
ResetType
How to reset the solver.
This subclass of std::exception may be thrown from the MinresSolMgr::solve() method.
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.
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.