10 #ifndef BELOS_LSQR_SOLMGR_HPP
11 #define BELOS_LSQR_SOLMGR_HPP
31 #ifdef BELOS_TEUCHOS_TIME_MONITOR
185 template<
class ScalarType,
class MV,
class OP,
189 Teuchos::ScalarTraits<ScalarType>::isComplex>
214 template<
class ScalarType,
class MV,
class OP>
300 return Teuchos::tuple (timerSolve_);
374 problem_->setProblem ();
407 std::string description ()
const override;
460 template<
class ScalarType,
class MV,
class OP>
462 lambda_ (
STM::zero ()),
465 condMax_ (
STM::one () /
STM::eps ()),
468 verbosity_ (Belos::
Errors),
472 matCondNum_ (
STM::zero ()),
473 matNorm_ (
STM::zero ()),
474 resNorm_ (
STM::zero ()),
475 matResNorm_ (
STM::zero ()),
480 template<
class ScalarType,
class MV,
class OP>
485 lambda_ (
STM::zero ()),
488 condMax_ (
STM::one () /
STM::eps ()),
491 verbosity_ (Belos::
Errors),
495 matCondNum_ (
STM::zero ()),
496 matNorm_ (
STM::zero ()),
497 resNorm_ (
STM::zero ()),
498 matResNorm_ (
STM::zero ()),
515 template<
class ScalarType,
class MV,
class OP>
520 using Teuchos::parameterList;
523 using Teuchos::rcpFromRef;
526 if (validParams_.is_null ()) {
534 RCP<std::ostream> outputStream = rcpFromRef (std::cout);
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)");
575 template<
class ScalarType,
class MV,
class OP>
580 using Teuchos::isParameterType;
581 using Teuchos::getParameter;
584 using Teuchos::parameterList;
587 using Teuchos::rcp_dynamic_cast;
588 using Teuchos::rcpFromRef;
596 (params.
is_null (), std::invalid_argument,
597 "Belos::LSQRSolMgr::setParameters: The input ParameterList is null.");
598 RCP<const ParameterList> defaultParams = getValidParameters ();
627 maxIters_ = params->
get<
int> (
"Maximum Iterations");
630 (maxIters_ < 0, std::invalid_argument,
"Belos::LSQRSolMgr::setParameters: "
631 "\"Maximum Iterations\" = " << maxIters_ <<
" < 0.");
635 const std::string newLabel =
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);
669 #endif // BELOS_TEUCHOS_TIME_MONITOR
674 int newVerbosity = 0;
683 newVerbosity = params->
get<
int> (
"Verbosity");
685 if (newVerbosity != verbosity_) {
686 verbosity_ = newVerbosity;
692 outputStyle_ = params->
get<
int> (
"Output Style");
700 outputStream_ = params->
get<RCP<std::ostream> > (
"Output Stream");
707 if (outputStream_.is_null ()) {
714 outputFreq_ = params->
get<
int> (
"Output Frequency");
720 if (printer_.is_null ()) {
723 printer_->setVerbosity (verbosity_);
724 printer_->setOStream (outputStream_);
735 termIterMax_ = params->
get<
int> (
"Term Iter Max");
740 else if (params->
isParameter (
"Convergence Tolerance")) {
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_);
816 template<
class ScalarType,
class MV,
class OP>
828 this->setParameters (Teuchos::parameterList (* (getValidParameters ())));
833 "Belos::LSQRSolMgr::solve: The linear problem to solve is null.");
836 "Belos::LSQRSolMgr::solve: The linear problem is not ready, "
837 "as its setProblem() method has not been called.");
839 (MVT::GetNumberVecs (*(problem_->getRHS ())) != 1,
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 "
843 << MVT::GetNumberVecs (* (problem_->getRHS ()))
844 <<
" right-hand sides.");
860 std::vector<int> currRHSIdx (1, 0);
861 problem_->setLSIndex (currRHSIdx);
864 outputTest_->reset ();
868 bool isConverged =
false;
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
902 lsqr_iter->resetNumIters ();
904 outputTest_->resetNumCalls ();
907 lsqr_iter->initializeLSQR (newstate);
910 lsqr_iter->iterate ();
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
946 #endif // BELOS_TEUCHOS_TIME_MONITOR
949 numIters_ = maxIterTest_->getNumIters();
950 matCondNum_ = convTest_->getMatCondNum();
951 matNorm_ = convTest_->getMatNorm();
952 resNorm_ = convTest_->getResidNorm();
953 matResNorm_ = convTest_->getLSResidNorm();
963 template<
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_;
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...
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
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.
OperatorTraits< ScalarType, MV, OP > OPT
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
Teuchos::ScalarTraits< MagnitudeType > STM
Details::RealSolverManager< ScalarType, MV, OP, isComplex > base_type
LSQRSolMgrBlockSizeFailure is thrown when the linear problem has more than one RHS.
T & get(ParameterList &l, const std::string &name)
MsgType
Available message types recognized by the linear solvers.
#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.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< Teuchos::Time > timerSolve_
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...
Teuchos::RCP< Teuchos::ParameterList > params_
Current parameter list.
bool isParameter(const std::string &name) const
Teuchos::ScalarTraits< ScalarType > STS
A Belos::StatusTest class for specifying convergence of LSQR. The outer status tests passes if an inn...
static const bool isComplex
Teuchos::RCP< OutputManager< ScalarType > > printer_
The output manager.
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::RCP< LSQRStatusTest< ScalarType, MV, OP > > convTest_
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. ...
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
The "master" status test (that includes all status tests).
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)
bool is_null(const RCP< T > &p)
MagnitudeType matCondNum_
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.
Teuchos::RCP< const Teuchos::ParameterList > validParams_
Default parameter list.
ReturnType
Whether the Belos solve converged for all linear systems.
TypeTo as(const TypeFrom &t)
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< std::ostream > outputStream_
Output stream to which to write status output.
MultiVecTraits< ScalarType, MV > MVT
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.
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...
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
The linear problem to solve.
MagnitudeType matResNorm_
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Get current linear problem being solved for in this object.
LSQRSolMgrLinearProblemFailure(const std::string &what_arg)
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_