10#ifndef BELOS_PSEUDO_BLOCK_CG_ITER_HPP
11#define BELOS_PSEUDO_BLOCK_CG_ITER_HPP
29#include "Teuchos_ScalarTraits.hpp"
30#include "Teuchos_ParameterList.hpp"
31#include "Teuchos_TimeMonitor.hpp"
53 template <
class ScalarType,
class MV,
class DM = DefaultDenseMatrix<
int,ScalarType>>
65 void initialize(Teuchos::RCP<const MV> tmp,
int _numVectors) {
67 this->
R = MVT::Clone( *tmp, _numVectors );
68 this->
Z = MVT::Clone( *tmp, _numVectors );
69 this->
P = MVT::Clone( *tmp, _numVectors );
70 this->
AP = MVT::Clone(*tmp, _numVectors );
75 bool matches(Teuchos::RCP<const MV> tmp,
int _numVectors=1)
const {
80 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
91 using SCT = Teuchos::ScalarTraits<ScalarType>;
105 Teuchos::ParameterList ¶ms );
167 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState()
const {
177 auto s = Teuchos::rcp_dynamic_cast<PseudoBlockCGIterationState<ScalarType,MV, DM> >(state,
true);
218 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
219 "Belos::PseudoBlockCGIter::setBlockSize(): Cannot use a block size that is not one.");
229 if (numEntriesForCondEst_ != 0) doCondEst_=val;
237 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
238 if (
static_cast<size_type
> (iter_) >= diag_.size ()) {
241 return diag_ (0, iter_);
252 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
253 if (
static_cast<size_type
> (iter_) >= offdiag_.size ()) {
256 return offdiag_ (0, iter_);
265 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
266 const Teuchos::RCP<OutputManager<ScalarType> > om_;
267 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
287 bool assertPositiveDefiniteness_;
290 Teuchos::ArrayRCP<MagnitudeType> diag_, offdiag_;
291 ScalarType pAp_old_, beta_old_, rHz_old2_;
292 int numEntriesForCondEst_;
308 Teuchos::RCP<MV> AP_;
314 template<
class ScalarType,
class MV,
class OP,
class DM>
318 Teuchos::ParameterList ¶ms ):
325 assertPositiveDefiniteness_( params.get(
"Assert Positive Definiteness", true) ),
326 numEntriesForCondEst_(params.get(
"Max Size For Condest",0) ),
334 template<
class ScalarType,
class MV,
class OP,
class DM>
338 Teuchos::RCP<const MV> lhsMV = lp_->getCurrLHSVec();
339 Teuchos::RCP<const MV> rhsMV = lp_->getCurrRHSVec();
340 TEUCHOS_TEST_FOR_EXCEPTION((lhsMV==Teuchos::null && rhsMV==Teuchos::null),std::invalid_argument,
341 "Belos::PseudoBlockCGIter::initialize(): Cannot initialize state storage!");
344 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
351 TEUCHOS_ASSERT(!newstate.is_null());
353 newstate->initialize(tmp, numRHS_);
357 if(numEntriesForCondEst_ > 0) {
358 diag_.resize(numEntriesForCondEst_);
359 offdiag_.resize(numEntriesForCondEst_-1);
362 std::string errstr(
"Belos::BlockPseudoCGIter::initialize(): Specified multivectors must have a consistent length and width.");
366 std::invalid_argument, errstr );
368 std::invalid_argument, errstr );
379 if ( lp_->getLeftPrec() != Teuchos::null ) {
380 lp_->applyLeftPrec( *R_, *Z_ );
381 if ( lp_->getRightPrec() != Teuchos::null ) {
382 Teuchos::RCP<MV> tmp1 =
MVT::Clone( *Z_, numRHS_ );
383 lp_->applyRightPrec( *Z_, *tmp1 );
387 else if ( lp_->getRightPrec() != Teuchos::null ) {
388 lp_->applyRightPrec( *R_, *Z_ );
403 template<
class ScalarType,
class MV,
class OP,
class DM>
415 std::vector<int> index(1);
416 std::vector<ScalarType> rHz( numRHS_ );
417 std::vector<ScalarType> rHz_old( numRHS_ );
418 std::vector<ScalarType> pAp( numRHS_ );
419 std::vector<ScalarType> beta( numRHS_ );
420 Teuchos::RCP<DM> alpha =
DMT::Create( numRHS_,numRHS_ );
423 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
424 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
427 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
432 if ( assertPositiveDefiniteness_ )
433 for (i=0; i<numRHS_; ++i)
434 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
436 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
441 while (stest_->checkStatus(
this) !=
Passed) {
447 lp_->applyOp( *P_, *AP_ );
452 for (i=0; i<numRHS_; ++i) {
453 if ( assertPositiveDefiniteness_ )
455 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(pAp[i]) <= zero,
457 "Belos::PseudoBlockCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
467 lp_->updateSolution();
471 for (i=0; i<numRHS_; ++i) {
482 if ( lp_->getLeftPrec() != Teuchos::null ) {
483 lp_->applyLeftPrec( *R_, *Z_ );
484 if ( lp_->getRightPrec() != Teuchos::null ) {
485 Teuchos::RCP<MV> tmp =
MVT::Clone( *Z_, numRHS_ );
486 lp_->applyRightPrec( *Z_, *tmp );
490 else if ( lp_->getRightPrec() != Teuchos::null ) {
491 lp_->applyRightPrec( *R_, *Z_ );
498 if ( assertPositiveDefiniteness_ )
499 for (i=0; i<numRHS_; ++i)
500 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
502 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
505 for (i=0; i<numRHS_; ++i) {
506 beta[i] = rHz[i] / rHz_old[i];
514 if (doCondEst_ && (iter_ - 1) < diag_.size()) {
516 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real((beta_old_ * beta_old_ * pAp_old_ + pAp[0]) / rHz_old[0]);
517 offdiag_[iter_-2] = -Teuchos::ScalarTraits<ScalarType>::real(beta_old_ * pAp_old_ / (sqrt( rHz_old[0] * rHz_old2_)));
520 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real(pAp[0] / rHz_old[0]);
522 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.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
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.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< MV > AP
The matrix A applied to current decent direction vector.
virtual bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
Teuchos::RCP< MV > P
The current decent direction vector.
Teuchos::RCP< MV > R
The current residual.
virtual void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
Teuchos::RCP< MV > Z
The current preconditioned residual.
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero,...
Virtual base class which defines basic traits for the multi-vector type.
static void SyncHostToDevice(DM &dm)
static ScalarType & Value(DM &dm, const int i, const int j)
Access a reference to the (i,j) entry of dm, e_i^T dm e_j.
static Teuchos::RCP< DM > Create()
Creates a new empty DM with no dimension.
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const DM &B, const ScalarType beta, MV &mv)
static void Assign(const MV &A, MV &mv)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
static ptrdiff_t GetGlobalLength(const MV &mv)
static void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static int GetNumberVecs(const MV &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...
PseudoBlockCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, Teuchos::ParameterList ¶ms)
PseudoBlockCGIter constructor with linear problem, solver utilities, and parameter list of solver opt...
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
void iterate()
This method performs CG iterations on each linear system until the status test indicates the need to ...
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation.
DenseMatTraits< ScalarType, DM > DMT
int getNumIters() const
Get the current iteration count.
void initializeCG(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > newstate, Teuchos::RCP< MV > R_0)
Initialize the solver to an iterate, providing a complete state.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation.
typename SCT::magnitudeType MagnitudeType
virtual ~PseudoBlockCGIter()=default
Destructor.
OperatorTraits< ScalarType, MV, OP > OPT
void setBlockSize(int blockSize)
Set the blocksize.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
MultiVecTraits< ScalarType, MV, DM > MVT
bool isInitialized()
States whether the solver has been initialized or not.
Structure to contain pointers to PseudoBlockCGIteration state variables.
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
PseudoBlockCGIterationState(Teuchos::RCP< const MV > tmp)
virtual ~PseudoBlockCGIterationState()=default
PseudoBlockCGIterationState()=default
A pure virtual class for defining the status tests for the Belos iterative solvers.