42#ifndef BELOS_FIXEDPOINT_SOLMGR_HPP
43#define BELOS_FIXEDPOINT_SOLMGR_HPP
62#ifdef BELOS_TEUCHOS_TIME_MONITOR
63# include "Teuchos_TimeMonitor.hpp"
90 template<
class ScalarType,
class MV,
class OP>
96 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
97 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
MagnitudeType;
98 typedef Teuchos::ScalarTraits<MagnitudeType>
MT;
130 const Teuchos::RCP<Teuchos::ParameterList> &pl );
136 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
161 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
191 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
205 std::string solverDesc =
" Fixed Point ";
253 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
264 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
sTest_;
270 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> >
convTest_;
324template<
class ScalarType,
class MV,
class OP>
342template<
class ScalarType,
class MV,
class OP>
345 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
360 TEUCHOS_TEST_FOR_EXCEPTION(
problem_.is_null(), std::invalid_argument,
361 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
366 if (! pl.is_null()) {
371template<
class ScalarType,
class MV,
class OP>
374setParameters (
const Teuchos::RCP<Teuchos::ParameterList> ¶ms)
377 if (
params_ == Teuchos::null) {
385 if (params->isParameter(
"Maximum Iterations")) {
395 if (params->isParameter(
"Block Size")) {
397 TEUCHOS_TEST_FOR_EXCEPTION(
blockSize_ <= 0, std::invalid_argument,
398 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
405 if (params->isParameter(
"Timer Label")) {
406 std::string tempLabel = params->get(
"Timer Label",
label_default_);
409 if (tempLabel !=
label_) {
412 std::string solveLabel =
label_ +
": FixedPointSolMgr total solve time";
413#ifdef BELOS_TEUCHOS_TIME_MONITOR
414 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
420 if (params->isParameter(
"Verbosity")) {
421 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
424 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
434 if (params->isParameter(
"Output Style")) {
435 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
438 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
447 if (params->isParameter(
"Output Stream")) {
448 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
458 if (params->isParameter(
"Output Frequency")) {
478 if (params->isParameter(
"Convergence Tolerance")) {
479 if (params->isType<
MagnitudeType> (
"Convergence Tolerance")) {
480 convtol_ = params->get (
"Convergence Tolerance",
493 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
494 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
512 if (
sTest_ == Teuchos::null)
523 std::string solverDesc =
" Fixed Point ";
530 std::string solveLabel =
label_ +
": FixedPointSolMgr total solve time";
531#ifdef BELOS_TEUCHOS_TIME_MONITOR
532 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
541template<
class ScalarType,
class MV,
class OP>
542Teuchos::RCP<const Teuchos::ParameterList>
545 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
548 if(is_null(validPL)) {
549 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
554 "The relative residual tolerance that needs to be achieved by the\n"
555 "iterative solver in order for the linear system to be declared converged.");
557 "The maximum number of block iterations allowed for each\n"
558 "set of RHS solved.");
560 "The number of vectors in each block.");
562 "What type(s) of solver information should be outputted\n"
563 "to the output stream.");
565 "What style is used for the solver information outputted\n"
566 "to the output stream.");
568 "How often convergence information should be outputted\n"
569 "to the output stream.");
570 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
571 "A reference-counted pointer to the output stream where all\n"
572 "solver output is sent.");
574 "When convergence information is printed, only show the maximum\n"
575 "relative residual norm when the block size is greater than one.");
577 "The string to use as a prefix for the timer labels.");
585template<
class ScalarType,
class MV,
class OP>
589 using Teuchos::rcp_const_cast;
590 using Teuchos::rcp_dynamic_cast;
600 TEUCHOS_TEST_FOR_EXCEPTION( !
problem_->isProblemSet(),
602 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
603 "has not been called.");
610 std::vector<int> currIdx, currIdx2;
613 for (
int i=0; i<numCurrRHS; ++i)
614 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
616 { currIdx[i] = -1; currIdx2[i] = i; }
623 Teuchos::ParameterList plist;
631 bool isConverged =
true;
636 RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
641#ifdef BELOS_TEUCHOS_TIME_MONITOR
645 while ( numRHS2Solve > 0 ) {
648 std::vector<int> convRHSIdx;
649 std::vector<int> currRHSIdx( currIdx );
650 currRHSIdx.resize(numCurrRHS);
653 block_fp_iter->resetNumIters();
664 block_fp_iter->initializeFixedPoint(newstate);
670 block_fp_iter->iterate();
678 std::vector<int> convIdx =
convTest_->convIndices();
683 if (convIdx.size() == currRHSIdx.size())
693 std::vector<int> unconvIdx(currRHSIdx.size());
694 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
696 for (
unsigned int j=0; j<convIdx.size(); ++j) {
697 if (currRHSIdx[i] == convIdx[j]) {
703 currIdx2[have] = currIdx2[i];
704 currRHSIdx[have++] = currRHSIdx[i];
709 currRHSIdx.resize(have);
710 currIdx2.resize(have);
716 std::vector<MagnitudeType> norms;
717 R_0 =
MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
718 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
721 block_fp_iter->setBlockSize( have );
726 block_fp_iter->initializeFixedPoint(defstate);
741 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
742 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
743 "the maximum iteration count test passed. Please report this bug "
744 "to the Belos developers.");
747 catch (
const std::exception &e) {
749 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at "
750 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
751 << e.what() << std::endl;
761 startPtr += numCurrRHS;
762 numRHS2Solve -= numCurrRHS;
763 if ( numRHS2Solve > 0 ) {
769 for (
int i=0; i<numCurrRHS; ++i)
770 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
772 { currIdx[i] = -1; currIdx2[i] = i; }
781 currIdx.resize( numRHS2Solve );
792#ifdef BELOS_TEUCHOS_TIME_MONITOR
808 const std::vector<MagnitudeType>* pTestValues =
convTest_->getTestValue();
810 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
811 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
812 "method returned NULL. Please report this bug to the Belos developers.");
814 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
815 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
816 "method returned a vector of length zero. Please report this bug to the "
817 "Belos developers.");
822 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
832template<
class ScalarType,
class MV,
class OP>
835 std::ostringstream oss;
836 oss <<
"Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
Belos concrete class for performing the conjugate-gradient (CG) iteration.
Belos header file which uses auto-configuration information to include necessary C++ headers.
Belos concrete class for performing fixed point iteration iteration.
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::StatusTestResNorm for specifying general residual norm stopping criteria.
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)
This class implements the preconditioned fixed point iteration.
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
FixedPointSolMgrLinearProblemFailure(const std::string &what_arg)
MagnitudeType achievedTol_
Tolerance achieved by the last solve() invocation.
FixedPointSolMgr()
Empty constructor for FixedPointSolMgr. This constructor takes no arguments and sets the default valu...
static constexpr int blockSize_default_
std::string description() const override
Method to return description of the block CG solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
static constexpr int outputFreq_default_
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Teuchos::RCP< Teuchos::ParameterList > params_
Current parameter list.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::ScalarTraits< MagnitudeType > MT
Teuchos::RCP< std::ostream > outputStream_
Output stream to which the output manager prints.
int numIters_
Number of iterations taken by the last solve() invocation.
bool isSet_
Whether or not the parameters have been set (via setParameters()).
void replaceUserConvStatusTest(const Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP > > &userConvStatusTest)
Set user-defined convergence status test.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::RCP< OutputManager< ScalarType > > printer_
Output manager, that handles printing of different kinds of messages.
static constexpr int verbosity_default_
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
Output "status test" that controls all the other status tests.
Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP > > convTest_
Convergence stopping criterion.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
std::string label_
Prefix label for all the timers.
MagnitudeType convtol_
Convergence tolerance (read from parameter list).
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
The linear problem to solve.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
static constexpr int outputStyle_default_
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< Teuchos::Time > timerSolve_
Solve timer.
static constexpr bool showMaxResNormOnly_default_
static constexpr const char * label_default_
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
Aggregate stopping criterion.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
int maxIters_
Maximum iteration count (read from parameter list).
MultiVecTraits< ScalarType, MV > MVT
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
static constexpr int maxIters_default_
virtual ~FixedPointSolMgr()
Destructor.
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::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
Maximum iteration count stopping criterion.
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
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).
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...
SolverManager()
Empty constructor.
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
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.
An abstract class of StatusTest for stopping criteria using residual norms.
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double convTol
Default convergence tolerance.
Structure to contain pointers to FixedPointIteration state variables.
Teuchos::RCP< const MV > R
The current residual.