10#ifndef BELOS_LSQR_SOLMGR_HPP
11#define BELOS_LSQR_SOLMGR_HPP
29#include "Teuchos_as.hpp"
31#ifdef BELOS_TEUCHOS_TIME_MONITOR
32#include "Teuchos_TimeMonitor.hpp"
185template<
class ScalarType,
class MV,
class OP,
186 const bool scalarTypeIsComplex = Teuchos::ScalarTraits<ScalarType>::isComplex>
189 Teuchos::ScalarTraits<ScalarType>::isComplex>
191 static const bool isComplex = Teuchos::ScalarTraits<ScalarType>::isComplex;
199 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
205 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
214template<
class ScalarType,
class MV,
class OP>
220 typedef Teuchos::ScalarTraits<ScalarType> STS;
221 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
222 typedef Teuchos::ScalarTraits<MagnitudeType> STM;
265 const Teuchos::RCP<Teuchos::ParameterList>& pl);
271 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
286 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const override;
299 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers ()
const {
300 return Teuchos::tuple (timerSolve_);
362 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
override;
374 problem_->setProblem ();
407 std::string description ()
const override;
414 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
416 Teuchos::RCP<OutputManager<ScalarType> > printer_;
418 Teuchos::RCP<std::ostream> outputStream_;
421 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
422 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
423 Teuchos::RCP<LSQRStatusTest<ScalarType,MV,OP> > convTest_;
424 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
427 Teuchos::RCP<Teuchos::ParameterList> params_;
434 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
437 MagnitudeType lambda_;
438 MagnitudeType relRhsErr_;
439 MagnitudeType relMatErr_;
440 MagnitudeType condMax_;
441 int maxIters_, termIterMax_;
442 int verbosity_, outputStyle_, outputFreq_;
446 MagnitudeType matCondNum_;
447 MagnitudeType matNorm_;
448 MagnitudeType resNorm_;
449 MagnitudeType matResNorm_;
453 Teuchos::RCP<Teuchos::Time> timerSolve_;
460template<
class ScalarType,
class MV,
class OP>
462 lambda_ (STM::zero ()),
463 relRhsErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
464 relMatErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
465 condMax_ (STM::one () / STM::eps ()),
472 matCondNum_ (STM::zero ()),
473 matNorm_ (STM::zero ()),
474 resNorm_ (STM::zero ()),
475 matResNorm_ (STM::zero ()),
480template<
class ScalarType,
class MV,
class OP>
483 const Teuchos::RCP<Teuchos::ParameterList>& pl) :
485 lambda_ (STM::zero ()),
486 relRhsErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
487 relMatErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
488 condMax_ (STM::one () / STM::eps ()),
495 matCondNum_ (STM::zero ()),
496 matNorm_ (STM::zero ()),
497 resNorm_ (STM::zero ()),
498 matResNorm_ (STM::zero ()),
509 if (! pl.is_null ()) {
515template<
class ScalarType,
class MV,
class OP>
516Teuchos::RCP<const Teuchos::ParameterList>
519 using Teuchos::ParameterList;
520 using Teuchos::parameterList;
523 using Teuchos::rcpFromRef;
526 if (validParams_.is_null ()) {
530 const MagnitudeType ten = Teuchos::as<MagnitudeType> (10);
531 const MagnitudeType sqrtEps = STM::squareroot (STM::eps());
533 const MagnitudeType lambda = STM::zero();
534 RCP<std::ostream> outputStream = rcpFromRef (std::cout);
535 const MagnitudeType relRhsErr = ten * sqrtEps;
536 const MagnitudeType relMatErr = ten * sqrtEps;
537 const MagnitudeType condMax = STM::one() / STM::eps();
538 const int maxIters = 1000;
539 const int termIterMax = 1;
542 const int outputFreq = -1;
543 const std::string label (
"Belos");
545 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
546 pl->set (
"Output Stream", outputStream,
"Teuchos::RCP<std::ostream> "
547 "(reference-counted pointer to the output stream) receiving "
548 "all solver output");
549 pl->set (
"Lambda", lambda,
"Damping parameter");
550 pl->set (
"Rel RHS Err", relRhsErr,
"Estimates the error in the data "
551 "defining the right-hand side");
552 pl->set (
"Rel Mat Err", relMatErr,
"Estimates the error in the data "
553 "defining the matrix.");
554 pl->set (
"Condition Limit", condMax,
"Bounds the estimated condition "
556 pl->set (
"Maximum Iterations", maxIters,
"Maximum number of iterations");
557 pl->set (
"Term Iter Max", termIterMax,
"The number of consecutive "
558 "iterations must that satisfy all convergence criteria in order "
559 "for LSQR to stop iterating");
560 pl->set (
"Verbosity", verbosity,
"Type(s) of solver information written to "
561 "the output stream");
562 pl->set (
"Output Style", outputStyle,
"Style of solver output");
563 pl->set (
"Output Frequency", outputFreq,
"Frequency at which information "
564 "is written to the output stream (-1 means \"not at all\")");
565 pl->set (
"Timer Label", label,
"String to use as a prefix for the timer "
567 pl->set (
"Block Size", 1,
"Block size parameter (currently, LSQR requires "
568 "this must always be 1)");
575template<
class ScalarType,
class MV,
class OP>
578setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
580 using Teuchos::isParameterType;
581 using Teuchos::getParameter;
583 using Teuchos::ParameterList;
584 using Teuchos::parameterList;
587 using Teuchos::rcp_dynamic_cast;
588 using Teuchos::rcpFromRef;
590 using Teuchos::TimeMonitor;
591 using Teuchos::Exceptions::InvalidParameter;
592 using Teuchos::Exceptions::InvalidParameterName;
593 using Teuchos::Exceptions::InvalidParameterType;
595 TEUCHOS_TEST_FOR_EXCEPTION
596 (params.is_null (), std::invalid_argument,
597 "Belos::LSQRSolMgr::setParameters: The input ParameterList is null.");
619 if (params->isParameter (
"Lambda")) {
620 lambda_ = params->get<MagnitudeType> (
"Lambda");
621 }
else if (params->isParameter (
"lambda")) {
622 lambda_ = params->get<MagnitudeType> (
"lambda");
626 if (params->isParameter (
"Maximum Iterations")) {
627 maxIters_ = params->get<
int> (
"Maximum Iterations");
629 TEUCHOS_TEST_FOR_EXCEPTION
630 (maxIters_ < 0, std::invalid_argument,
"Belos::LSQRSolMgr::setParameters: "
631 "\"Maximum Iterations\" = " << maxIters_ <<
" < 0.");
635 const std::string newLabel =
636 params->isParameter (
"Timer Label") ?
637 params->get<std::string> (
"Timer Label") :
641 if (newLabel != label_) {
645#ifdef BELOS_TEUCHOS_TIME_MONITOR
646 const std::string newSolveLabel = (newLabel !=
"") ?
647 (newLabel +
": Belos::LSQRSolMgr total solve time") :
648 std::string (
"Belos::LSQRSolMgr total solve time");
649 if (timerSolve_.is_null ()) {
651 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
660 const std::string oldSolveLabel = timerSolve_->name ();
662 if (oldSolveLabel != newSolveLabel) {
665 TimeMonitor::clearCounter (oldSolveLabel);
666 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
673 if (params->isParameter (
"Verbosity")) {
674 int newVerbosity = 0;
682 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
683 newVerbosity = params->get<
int> (
"Verbosity");
685 if (newVerbosity != verbosity_) {
686 verbosity_ = newVerbosity;
691 if (params->isParameter (
"Output Style")) {
692 outputStyle_ = params->get<
int> (
"Output Style");
699 if (params->isParameter (
"Output Stream")) {
700 outputStream_ = params->get<RCP<std::ostream> > (
"Output Stream");
707 if (outputStream_.is_null ()) {
708 outputStream_ = rcp (
new Teuchos::oblackholestream ());
713 if (params->isParameter (
"Output Frequency")) {
714 outputFreq_ = params->get<
int> (
"Output Frequency");
720 if (printer_.is_null ()) {
723 printer_->setVerbosity (verbosity_);
724 printer_->setOStream (outputStream_);
731 if (params->isParameter (
"Condition Limit")) {
732 condMax_ = params->get<MagnitudeType> (
"Condition Limit");
734 if (params->isParameter (
"Term Iter Max")) {
735 termIterMax_ = params->get<
int> (
"Term Iter Max");
737 if (params->isParameter (
"Rel RHS Err")) {
738 relRhsErr_ = params->get<MagnitudeType> (
"Rel RHS Err");
740 else if (params->isParameter (
"Convergence Tolerance")) {
743 relRhsErr_ = params->get<MagnitudeType> (
"Convergence Tolerance");
746 if (params->isParameter (
"Rel Mat Err")) {
747 relMatErr_ = params->get<MagnitudeType> (
"Rel Mat Err");
752 if (convTest_.is_null ()) {
755 relRhsErr_, relMatErr_));
757 convTest_->setCondLim (condMax_);
758 convTest_->setTermIterMax (termIterMax_);
759 convTest_->setRelRhsErr (relRhsErr_);
760 convTest_->setRelMatErr (relMatErr_);
767 if (maxIterTest_.is_null()) {
770 maxIterTest_->setMaxIters (maxIters_);
782 if (sTest_.is_null()) {
783 sTest_ = rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
786 if (outputTest_.is_null ()) {
790 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
793 const std::string solverDesc =
" LSQR ";
794 outputTest_->setSolverDesc (solverDesc);
798 outputTest_->setOutputManager (printer_);
799 outputTest_->setChild (sTest_);
800 outputTest_->setOutputFrequency (outputFreq_);
816template<
class ScalarType,
class MV,
class OP>
831 TEUCHOS_TEST_FOR_EXCEPTION
833 "Belos::LSQRSolMgr::solve: The linear problem to solve is null.");
834 TEUCHOS_TEST_FOR_EXCEPTION
836 "Belos::LSQRSolMgr::solve: The linear problem is not ready, "
837 "as its setProblem() method has not been called.");
838 TEUCHOS_TEST_FOR_EXCEPTION
841 "The current implementation of LSQR only knows how to solve problems "
842 "with one right-hand side, but the linear problem to solve has "
844 <<
" right-hand sides.");
860 std::vector<int> currRHSIdx (1, 0);
861 problem_->setLSIndex (currRHSIdx);
864 outputTest_->reset ();
868 bool isConverged =
false;
885 Teuchos::ParameterList plist;
892 plist.set (
"Lambda", lambda_);
895 RCP<iter_type> lsqr_iter =
896 rcp (
new iter_type (problem_, printer_, outputTest_, plist));
897#ifdef BELOS_TEUCHOS_TIME_MONITOR
898 Teuchos::TimeMonitor slvtimer (*timerSolve_);
902 lsqr_iter->resetNumIters ();
904 outputTest_->resetNumCalls ();
907 lsqr_iter->initializeLSQR (newstate);
910 lsqr_iter->iterate ();
920 TEUCHOS_TEST_FOR_EXCEPTION
921 (
true, std::logic_error,
"Belos::LSQRSolMgr::solve: "
922 "LSQRIteration::iterate returned without either the convergence test "
923 "or the maximum iteration count test passing. "
924 "Please report this bug to the Belos developers.");
926 }
catch (
const std::exception& e) {
928 <<
"Error! Caught std::exception in LSQRIter::iterate at iteration "
929 << lsqr_iter->getNumIters () << std::endl << e.what () << std::endl;
934 problem_->setCurrLS();
940#ifdef BELOS_TEUCHOS_TIME_MONITOR
949 numIters_ = maxIterTest_->getNumIters();
950 matCondNum_ = convTest_->getMatCondNum();
951 matNorm_ = convTest_->getMatNorm();
952 resNorm_ = convTest_->getResidNorm();
953 matResNorm_ = convTest_->getLSResidNorm();
963template<
class ScalarType,
class MV,
class OP>
966 std::ostringstream oss;
967 oss <<
"LSQRSolMgr<...," << STS::name () <<
">";
969 oss <<
"Lambda: " << lambda_;
970 oss <<
", condition number limit: " << condMax_;
971 oss <<
", relative RHS Error: " << relRhsErr_;
972 oss <<
", relative Matrix Error: " << relMatErr_;
973 oss <<
", maximum number of iterations: " << maxIters_;
974 oss <<
", termIterMax: " << termIterMax_;
Belos header file which uses auto-configuration information to include necessary C++ headers.
Belos concrete class that iterates LSQR.
IterationState contains the data that defines the state of the LSQR solver at any given time.
Belos::StatusTest class defining LSQR convergence.
Class which describes the linear problem to be solved by the iterative solver.
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::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.
BelosError(const std::string &what_arg)
Base class for Belos::SolverManager subclasses which normally can only compile for real ScalarType.
Implementation of the LSQR iteration.
LSQRSolMgrBlockSizeFailure is thrown when the linear problem has more than one RHS.
LSQRSolMgrBlockSizeFailure(const std::string &what_arg)
Belos::LSQRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
LSQRSolMgrLinearProblemFailure(const std::string &what_arg)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Get current linear problem being solved for in this object.
MagnitudeType getMatCondNum() const
Estimated matrix condition number from the last solve.
int getNumIters() const override
Iteration count from the last solve.
virtual ~LSQRSolMgr()
Destructor (declared virtual for memory safety of base classes).
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
void reset(const ResetType type) override
reset the solver manager as specified by the ResetType, informs the solver manager that the solver sh...
bool isLOADetected() const override
Whether a loss of accuracy was detected during the last solve.
MagnitudeType getMatResNorm() const
Estimate of (residual vector ) from the last solve.
MagnitudeType getResNorm() const
Estimated residual norm from the last solve.
MagnitudeType getMatNorm() const
Estimated matrix Frobenius norm from the last solve.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
LSQRSolMgr()
Empty constructor for LSQRSolMgr. This constructor takes no arguments and sets the default values for...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
LSQRSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
A Belos::StatusTest class for specifying convergence of LSQR. The outer status tests passes if an inn...
Traits class which defines basic operations on multivectors.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
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...
A class for extending the status testing capabilities of Belos via logical combinations.
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.
MsgType
Available message types recognized by the linear solvers.
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Structure to contain pointers to LSQRIteration state variables, ...