42#ifndef BELOS_CG_SINGLE_RED_ITER_HPP
43#define BELOS_CG_SINGLE_RED_ITER_HPP
60#include "Teuchos_SerialDenseMatrix.hpp"
61#include "Teuchos_SerialDenseVector.hpp"
62#include "Teuchos_ScalarTraits.hpp"
63#include "Teuchos_ParameterList.hpp"
64#include "Teuchos_TimeMonitor.hpp"
78template<
class ScalarType,
class MV,
class OP>
88 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
103 Teuchos::ParameterList ¶ms );
201 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
202 "Belos::CGSingleRedIter::setBlockSize(): Cannot use a block size that is not one.");
213 Teuchos::ArrayView<MagnitudeType> temp;
219 Teuchos::ArrayView<MagnitudeType> temp;
236 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
lp_;
237 const Teuchos::RCP<OutputManager<ScalarType> >
om_;
238 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
stest_;
239 const Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
convTest_;
294 template<
class ScalarType,
class MV,
class OP>
299 Teuchos::ParameterList ¶ms ):
313 template <
class ScalarType,
class MV,
class OP>
319 Teuchos::RCP<const MV> lhsMV =
lp_->getLHS();
320 Teuchos::RCP<const MV> rhsMV =
lp_->getRHS();
321 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
329 if (
R_ == Teuchos::null) {
331 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
332 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
333 "Belos::CGSingleRedIter::setStateSize(): linear problem does not specify multivectors to clone from.");
337 std::vector<int> index2(2,0);
338 std::vector<int> index(1,0);
380 template <
class ScalarType,
class MV,
class OP>
388 "Belos::CGSingleRedIter::initialize(): Cannot initialize state storage!");
392 std::string errstr(
"Belos::CGSingleRedIter::initialize(): Specified multivectors must have a consistent length and width.");
394 if (newstate.
R != Teuchos::null) {
397 std::invalid_argument, errstr );
399 std::invalid_argument, errstr );
402 if (newstate.
R !=
R_) {
410 if (
lp_->getLeftPrec() != Teuchos::null ) {
411 lp_->applyLeftPrec( *
R_, *
Z_ );
412 if (
lp_->getRightPrec() != Teuchos::null ) {
414 lp_->applyRightPrec( *
Z_, *tmp );
418 else if (
lp_->getRightPrec() != Teuchos::null ) {
419 lp_->applyRightPrec( *
R_, *
Z_ );
434 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
R == Teuchos::null,std::invalid_argument,
435 "Belos::CGSingleRedIter::initialize(): CGIterationState does not have initial residual.");
445 template <
class ScalarType,
class MV,
class OP>
446 Teuchos::RCP<const MV>
449 (*norms)[0] = std::sqrt(Teuchos::ScalarTraits<ScalarType>::magnitude(
rHz_));
450 return Teuchos::null;
452 (*norms)[0] = std::sqrt(Teuchos::ScalarTraits<ScalarType>::magnitude(
rHr_));
453 return Teuchos::null;
461 template <
class ScalarType,
class MV,
class OP>
472 Teuchos::SerialDenseMatrix<int,ScalarType> sHz( 2, 1 );
473 Teuchos::SerialDenseMatrix<int,ScalarType> sHt( 2, 2 );
474 ScalarType rHz_old, alpha, beta, delta;
477 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
478 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
481 Teuchos::RCP<MV> cur_soln_vec =
lp_->getCurrLHSVec();
485 "Belos::CGSingleRedIter::iterate(): current linear system has more than one vector!" );
499 if ((Teuchos::ScalarTraits<ScalarType>::magnitude(delta) < Teuchos::ScalarTraits<ScalarType>::eps()) &&
502 alpha =
rHz_ / delta;
506 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
527 if (
lp_->getLeftPrec() != Teuchos::null ) {
528 lp_->applyLeftPrec( *
R_, *
Z_ );
529 if (
lp_->getRightPrec() != Teuchos::null ) {
531 lp_->applyRightPrec( *tmp, *
Z_ );
534 else if (
lp_->getRightPrec() != Teuchos::null ) {
535 lp_->applyRightPrec( *
R_, *
Z_ );
562 beta =
rHz_ / rHz_old;
563 alpha =
rHz_ / (delta - (beta*
rHz_ / alpha));
567 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
602 if (
lp_->getLeftPrec() != Teuchos::null ) {
603 lp_->applyLeftPrec( *
R_, *
Z_ );
604 if (
lp_->getRightPrec() != Teuchos::null ) {
606 lp_->applyRightPrec( *tmp, *
Z_ );
609 else if (
lp_->getRightPrec() != Teuchos::null ) {
610 lp_->applyRightPrec( *
R_, *
Z_ );
627 beta =
rHz_ / rHz_old;
628 alpha =
rHz_ / (delta - (beta*
rHz_ / alpha));
632 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
Pure virtual base class which augments the basic interface for a conjugate gradient linear solver ite...
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Class which manages the output and verbosity of the Belos solvers.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
CGIterateFailure is thrown when the CGIteration object is unable to compute the next iterate in the C...
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero,...
virtual ~CGSingleRedIter()
Destructor.
MultiVecTraits< ScalarType, MV > MVT
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void initializeCG(CGIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
Teuchos::ScalarTraits< ScalarType > SCT
CGIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > lp_
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
bool foldConvergenceDetectionIntoAllreduce_
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > stest_
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation (NOT_IMPLEMENTED).
OperatorTraits< ScalarType, MV, OP > OPT
void resetNumIters(int iter=0)
Reset the iteration count.
void setDoCondEst(bool)
Sets whether or not to store the diagonal for condition estimation.
CGSingleRedIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, const Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > &convTester, Teuchos::ParameterList ¶ms)
CGSingleRedIter constructor with linear problem, solver utilities, and parameter list of solver optio...
bool stateStorageInitialized_
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
void setStateSize()
Method for initalizing the state storage needed by CG.
const Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > convTest_
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation (NOT_IMPLEMENTED).
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
bool isInitialized()
States whether the solver has been initialized or not.
SCT::magnitudeType MagnitudeType
int getNumIters() const
Get the current iteration count.
const Teuchos::RCP< OutputManager< ScalarType > > om_
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
Traits class which defines basic operations on multivectors.
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
static ptrdiff_t GetGlobalLength(const MV &mv)
Return the number of rows in the given multivector mv.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
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 void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void Assign(const MV &A, MV &mv)
mv := A
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...
An implementation of StatusTestResNorm using a family of residual norms.
A pure virtual class for defining the status tests for the Belos iterative solvers.
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< const MV > P
The current decent direction vector.
Teuchos::RCP< const MV > R
The current residual.
Teuchos::RCP< const MV > Z
The current preconditioned residual.
Teuchos::RCP< const MV > AP
The matrix A applied to current decent direction vector.