42 #ifndef BELOS_LSQR_SOLMGR_HPP
43 #define BELOS_LSQR_SOLMGR_HPP
63 #ifdef BELOS_TEUCHOS_TIME_MONITOR
217 template<
class ScalarType,
class MV,
class OP,
221 Teuchos::ScalarTraits<ScalarType>::isComplex>
246 template<
class ScalarType,
class MV,
class OP>
332 return Teuchos::tuple (timerSolve_);
406 problem_->setProblem ();
439 std::string description ()
const override;
469 MagnitudeType lambda_;
470 MagnitudeType relRhsErr_;
471 MagnitudeType relMatErr_;
472 MagnitudeType condMax_;
473 int maxIters_, termIterMax_;
474 int verbosity_, outputStyle_, outputFreq_;
478 MagnitudeType matCondNum_;
479 MagnitudeType matNorm_;
480 MagnitudeType resNorm_;
481 MagnitudeType matResNorm_;
492 template<
class ScalarType,
class MV,
class OP>
494 lambda_ (
STM::zero ()),
495 relRhsErr_ (Teuchos::
as<MagnitudeType> (10) *
STM::squareroot (
STM::eps ())),
496 relMatErr_ (Teuchos::
as<MagnitudeType> (10) *
STM::squareroot (
STM::eps ())),
497 condMax_ (
STM::one () /
STM::eps ()),
500 verbosity_ (Belos::
Errors),
504 matCondNum_ (
STM::zero ()),
505 matNorm_ (
STM::zero ()),
506 resNorm_ (
STM::zero ()),
507 matResNorm_ (
STM::zero ()),
512 template<
class ScalarType,
class MV,
class OP>
517 lambda_ (
STM::zero ()),
518 relRhsErr_ (Teuchos::
as<MagnitudeType> (10) *
STM::squareroot (
STM::eps ())),
519 relMatErr_ (Teuchos::
as<MagnitudeType> (10) *
STM::squareroot (
STM::eps ())),
520 condMax_ (
STM::one () /
STM::eps ()),
523 verbosity_ (Belos::
Errors),
527 matCondNum_ (
STM::zero ()),
528 matNorm_ (
STM::zero ()),
529 resNorm_ (
STM::zero ()),
530 matResNorm_ (
STM::zero ()),
547 template<
class ScalarType,
class MV,
class OP>
552 using Teuchos::parameterList;
555 using Teuchos::rcpFromRef;
558 if (validParams_.is_null ()) {
562 const MagnitudeType ten = Teuchos::as<MagnitudeType> (10);
563 const MagnitudeType sqrtEps = STM::squareroot (STM::eps());
565 const MagnitudeType lambda = STM::zero();
566 RCP<std::ostream> outputStream = rcpFromRef (std::cout);
567 const MagnitudeType relRhsErr = ten * sqrtEps;
568 const MagnitudeType relMatErr = ten * sqrtEps;
569 const MagnitudeType condMax = STM::one() / STM::eps();
570 const int maxIters = 1000;
571 const int termIterMax = 1;
574 const int outputFreq = -1;
575 const std::string label (
"Belos");
577 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
578 pl->set (
"Output Stream", outputStream,
"Teuchos::RCP<std::ostream> "
579 "(reference-counted pointer to the output stream) receiving "
580 "all solver output");
581 pl->set (
"Lambda", lambda,
"Damping parameter");
582 pl->set (
"Rel RHS Err", relRhsErr,
"Estimates the error in the data "
583 "defining the right-hand side");
584 pl->set (
"Rel Mat Err", relMatErr,
"Estimates the error in the data "
585 "defining the matrix.");
586 pl->set (
"Condition Limit", condMax,
"Bounds the estimated condition "
588 pl->set (
"Maximum Iterations", maxIters,
"Maximum number of iterations");
589 pl->set (
"Term Iter Max", termIterMax,
"The number of consecutive "
590 "iterations must that satisfy all convergence criteria in order "
591 "for LSQR to stop iterating");
592 pl->set (
"Verbosity", verbosity,
"Type(s) of solver information written to "
593 "the output stream");
594 pl->set (
"Output Style", outputStyle,
"Style of solver output");
595 pl->set (
"Output Frequency", outputFreq,
"Frequency at which information "
596 "is written to the output stream (-1 means \"not at all\")");
597 pl->set (
"Timer Label", label,
"String to use as a prefix for the timer "
599 pl->set (
"Block Size", 1,
"Block size parameter (currently, LSQR requires "
600 "this must always be 1)");
607 template<
class ScalarType,
class MV,
class OP>
612 using Teuchos::isParameterType;
613 using Teuchos::getParameter;
616 using Teuchos::parameterList;
619 using Teuchos::rcp_dynamic_cast;
620 using Teuchos::rcpFromRef;
628 (params.
is_null (), std::invalid_argument,
629 "Belos::LSQRSolMgr::setParameters: The input ParameterList is null.");
630 RCP<const ParameterList> defaultParams = getValidParameters ();
652 lambda_ = params->
get<MagnitudeType> (
"Lambda");
654 lambda_ = params->
get<MagnitudeType> (
"lambda");
659 maxIters_ = params->
get<
int> (
"Maximum Iterations");
662 (maxIters_ < 0, std::invalid_argument,
"Belos::LSQRSolMgr::setParameters: "
663 "\"Maximum Iterations\" = " << maxIters_ <<
" < 0.");
667 const std::string newLabel =
669 params->
get<std::string> (
"Timer Label") :
673 if (newLabel != label_) {
677 #ifdef BELOS_TEUCHOS_TIME_MONITOR
678 const std::string newSolveLabel = (newLabel !=
"") ?
679 (newLabel +
": Belos::LSQRSolMgr total solve time") :
680 std::string (
"Belos::LSQRSolMgr total solve time");
681 if (timerSolve_.is_null ()) {
683 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
692 const std::string oldSolveLabel = timerSolve_->name ();
694 if (oldSolveLabel != newSolveLabel) {
697 TimeMonitor::clearCounter (oldSolveLabel);
698 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
701 #endif // BELOS_TEUCHOS_TIME_MONITOR
706 int newVerbosity = 0;
715 newVerbosity = params->
get<
int> (
"Verbosity");
717 if (newVerbosity != verbosity_) {
718 verbosity_ = newVerbosity;
724 outputStyle_ = params->
get<
int> (
"Output Style");
732 outputStream_ = params->
get<RCP<std::ostream> > (
"Output Stream");
739 if (outputStream_.is_null ()) {
746 outputFreq_ = params->
get<
int> (
"Output Frequency");
752 if (printer_.is_null ()) {
755 printer_->setVerbosity (verbosity_);
756 printer_->setOStream (outputStream_);
764 condMax_ = params->
get<MagnitudeType> (
"Condition Limit");
767 termIterMax_ = params->
get<
int> (
"Term Iter Max");
770 relRhsErr_ = params->
get<MagnitudeType> (
"Rel RHS Err");
772 else if (params->
isParameter (
"Convergence Tolerance")) {
775 relRhsErr_ = params->
get<MagnitudeType> (
"Convergence Tolerance");
779 relMatErr_ = params->
get<MagnitudeType> (
"Rel Mat Err");
784 if (convTest_.is_null ()) {
787 relRhsErr_, relMatErr_));
789 convTest_->setCondLim (condMax_);
790 convTest_->setTermIterMax (termIterMax_);
791 convTest_->setRelRhsErr (relRhsErr_);
792 convTest_->setRelMatErr (relMatErr_);
799 if (maxIterTest_.is_null()) {
802 maxIterTest_->setMaxIters (maxIters_);
814 if (sTest_.is_null()) {
815 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
818 if (outputTest_.is_null ()) {
822 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
825 const std::string solverDesc =
" LSQR ";
826 outputTest_->setSolverDesc (solverDesc);
830 outputTest_->setOutputManager (printer_);
831 outputTest_->setChild (sTest_);
832 outputTest_->setOutputFrequency (outputFreq_);
848 template<
class ScalarType,
class MV,
class OP>
860 this->setParameters (Teuchos::parameterList (* (getValidParameters ())));
865 "Belos::LSQRSolMgr::solve: The linear problem to solve is null.");
868 "Belos::LSQRSolMgr::solve: The linear problem is not ready, "
869 "as its setProblem() method has not been called.");
871 (MVT::GetNumberVecs (*(problem_->getRHS ())) != 1,
873 "The current implementation of LSQR only knows how to solve problems "
874 "with one right-hand side, but the linear problem to solve has "
875 << MVT::GetNumberVecs (* (problem_->getRHS ()))
876 <<
" right-hand sides.");
892 std::vector<int> currRHSIdx (1, 0);
893 problem_->setLSIndex (currRHSIdx);
896 outputTest_->reset ();
900 bool isConverged =
false;
924 plist.
set (
"Lambda", lambda_);
927 RCP<iter_type> lsqr_iter =
928 rcp (
new iter_type (problem_, printer_, outputTest_, plist));
929 #ifdef BELOS_TEUCHOS_TIME_MONITOR
934 lsqr_iter->resetNumIters ();
936 outputTest_->resetNumCalls ();
939 lsqr_iter->initializeLSQR (newstate);
942 lsqr_iter->iterate ();
953 (
true, std::logic_error,
"Belos::LSQRSolMgr::solve: "
954 "LSQRIteration::iterate returned without either the convergence test "
955 "or the maximum iteration count test passing. "
956 "Please report this bug to the Belos developers.");
958 }
catch (
const std::exception& e) {
960 <<
"Error! Caught std::exception in LSQRIter::iterate at iteration "
961 << lsqr_iter->getNumIters () << std::endl << e.what () << std::endl;
966 problem_->setCurrLS();
972 #ifdef BELOS_TEUCHOS_TIME_MONITOR
978 #endif // BELOS_TEUCHOS_TIME_MONITOR
981 numIters_ = maxIterTest_->getNumIters();
982 matCondNum_ = convTest_->getMatCondNum();
983 matNorm_ = convTest_->getMatNorm();
984 resNorm_ = convTest_->getResidNorm();
985 matResNorm_ = convTest_->getLSResidNorm();
995 template<
class ScalarType,
class MV,
class OP>
998 std::ostringstream oss;
999 oss <<
"LSQRSolMgr<...," << STS::name () <<
">";
1001 oss <<
"Lambda: " << lambda_;
1002 oss <<
", condition number limit: " << condMax_;
1003 oss <<
", relative RHS Error: " << relRhsErr_;
1004 oss <<
", relative Matrix Error: " << relMatErr_;
1005 oss <<
", maximum number of iterations: " << maxIters_;
1006 oss <<
", termIterMax: " << termIterMax_;
void reset(const ResetType type) override
reset the solver manager as specified by the ResetType, informs the solver manager that the solver sh...
Collection of types and exceptions used within the Belos solvers.
IterationState contains the data that defines the state of the LSQR solver at any given time...
Belos concrete class that iterates LSQR.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
Class which manages the output and verbosity of the Belos solvers.
LSQRSolMgrBlockSizeFailure(const std::string &what_arg)
MagnitudeType getMatCondNum() const
Estimated matrix condition number from the last solve.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
LSQRSolMgrBlockSizeFailure is thrown when the linear problem has more than one RHS.
T & get(const std::string &name, T def_value)
MsgType
Available message types recognized by the linear solvers.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
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 during the last solve.
MagnitudeType getMatNorm() const
Estimated matrix Frobenius norm from the last solve.
Base class for Belos::SolverManager subclasses which normally can only compile for real ScalarType...
Belos::StatusTest class for specifying a maximum number of iterations.
Belos::StatusTest class defining LSQR convergence.
A factory class for generating StatusTestOutput objects.
LSQRSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
Belos::LSQRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
bool isParameter(const std::string &name) const
A Belos::StatusTest class for specifying a maximum number of iterations.
int getNumIters() const override
Iteration count from the last solve.
ResetType
How to reset the solver.
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. ...
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="", const bool ignoreZeroTimers=false)
A linear system to solve, and its associated information.
virtual ~LSQRSolMgr()
Destructor (declared virtual for memory safety of base classes).
Class which describes the linear problem to be solved by the iterative solver.
MagnitudeType getResNorm() const
Estimated residual norm from the last solve.
ReturnType
Whether the Belos solve converged for all linear systems.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
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.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
TypeTo as(const TypeFrom &t)
A class for extending the status testing capabilities of Belos via logical combinations.
MagnitudeType getMatResNorm() const
Estimate of (residual vector ) from the last solve.
LSQR method (for linear systems and linear least-squares problems).
Class which defines basic traits for the operator type.
Implementation of the LSQR iteration.
Parent class to all Belos exceptions.
Structure to contain pointers to LSQRIteration state variables, ...
Belos header file which uses auto-configuration information to include necessary C++ headers...
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Get current linear problem being solved for in this object.
LSQRSolMgrLinearProblemFailure(const std::string &what_arg)