42#ifndef BELOS_CG_ITER_HPP
43#define BELOS_CG_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 );
183 (*norms)[0] = std::sqrt(Teuchos::ScalarTraits<ScalarType>::magnitude(
rHr_));
184 return Teuchos::null;
207 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
208 "Belos::CGIter::setBlockSize(): Cannot use a block size that is not one.");
225 typedef typename Teuchos::ArrayView<MagnitudeType>::size_type size_type;
226 if (
static_cast<size_type
> (
iter_) >=
diag_.size ()) {
240 typedef typename Teuchos::ArrayView<MagnitudeType>::size_type size_type;
261 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
lp_;
262 const Teuchos::RCP<OutputManager<ScalarType> >
om_;
263 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
stest_;
264 const Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
convTest_;
319 template<
class ScalarType,
class MV,
class OP>
324 Teuchos::ParameterList ¶ms ):
341 template <
class ScalarType,
class MV,
class OP>
347 Teuchos::RCP<const MV> lhsMV =
lp_->getLHS();
348 Teuchos::RCP<const MV> rhsMV =
lp_->getRHS();
349 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
357 if (
R_ == Teuchos::null) {
359 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
360 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
361 "Belos::CGIter::setStateSize(): linear problem does not specify multivectors to clone from.");
363 std::vector<int> index(1,0);
388 template <
class ScalarType,
class MV,
class OP>
396 "Belos::CGIter::initialize(): Cannot initialize state storage!");
400 std::string errstr(
"Belos::CGIter::initialize(): Specified multivectors must have a consistent length and width.");
402 if (newstate.
R != Teuchos::null) {
405 std::invalid_argument, errstr );
407 std::invalid_argument, errstr );
410 if (newstate.
R !=
R_) {
418 if (
lp_->getLeftPrec() != Teuchos::null ) {
419 lp_->applyLeftPrec( *
R_, *
Z_ );
420 if (
lp_->getRightPrec() != Teuchos::null ) {
422 lp_->applyRightPrec( *tmp, *
Z_ );
425 else if (
lp_->getRightPrec() != Teuchos::null ) {
426 lp_->applyRightPrec( *
R_, *
Z_ );
435 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
R == Teuchos::null,std::invalid_argument,
436 "Belos::CGIter::initialize(): CGIterationState does not have initial residual.");
446 template <
class ScalarType,
class MV,
class OP>
457 std::vector<ScalarType> alpha(1);
458 std::vector<ScalarType> beta(1);
459 std::vector<ScalarType> rHz(1), rHz_old(1), pAp(1);
460 Teuchos::SerialDenseMatrix<int,ScalarType> rHs( 1, 2 );
463 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
464 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
467 ScalarType pAp_old = one, beta_old = one, rHz_old2 = one;
470 Teuchos::RCP<MV> cur_soln_vec =
lp_->getCurrLHSVec();
474 "Belos::CGIter::iterate(): current linear system has more than one vector!" );
497 alpha[0] = rHz[0] / pAp[0];
502 "Belos::CGIter::iterate(): non-positive value for p^H*A*p encountered!" );
509 lp_->updateSolution();
522 if (
lp_->getLeftPrec() != Teuchos::null ) {
523 lp_->applyLeftPrec( *
R_, *
Z_ );
524 if (
lp_->getRightPrec() != Teuchos::null ) {
526 lp_->applyRightPrec( *tmp, *
Z_ );
529 else if (
lp_->getRightPrec() != Teuchos::null ) {
530 lp_->applyRightPrec( *
R_, *
Z_ );
543 beta[0] = rHz[0] / rHz_old[0];
550 diag_[
iter_-1] = Teuchos::ScalarTraits<ScalarType>::real((beta_old * beta_old * pAp_old + pAp[0]) / rHz_old[0]);
551 offdiag_[
iter_-2] = -Teuchos::ScalarTraits<ScalarType>::real(beta_old * pAp_old / (sqrt( rHz_old[0] * rHz_old2)));
554 diag_[
iter_-1] = Teuchos::ScalarTraits<ScalarType>::real(pAp[0] / rHz_old[0]);
556 rHz_old2 = rHz_old[0];
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.
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
bool foldConvergenceDetectionIntoAllreduce_
bool stateStorageInitialized_
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
SCT::magnitudeType MagnitudeType
OperatorTraits< ScalarType, MV, OP > OPT
virtual ~CGIter()
Destructor.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
bool assertPositiveDefiniteness_
Teuchos::ScalarTraits< ScalarType > SCT
CGIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
void initializeCG(CGIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void setStateSize()
Method for initalizing the state storage needed by CG.
int getNumIters() const
Get the current iteration count.
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > stest_
bool isInitialized()
States whether the solver has been initialized or not.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
Teuchos::ArrayRCP< MagnitudeType > diag_
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
int numEntriesForCondEst_
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation.
const Teuchos::RCP< OutputManager< ScalarType > > om_
void resetNumIters(int iter=0)
Reset the iteration count.
MultiVecTraits< ScalarType, MV > MVT
CGIter(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)
CGIter constructor with linear problem, solver utilities, and parameter list of solver options.
Teuchos::ArrayRCP< MagnitudeType > offdiag_
const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > lp_
const Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > convTest_
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation.
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,...
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 void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
Compute a vector b where the components are the individual dot-products of the i-th columns of A and ...
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.