10#ifndef BELOS_PSEUDO_BLOCK_CG_ITER_HPP
11#define BELOS_PSEUDO_BLOCK_CG_ITER_HPP
28#include "Teuchos_Assert.hpp"
29#include "Teuchos_SerialDenseMatrix.hpp"
30#include "Teuchos_SerialDenseVector.hpp"
31#include "Teuchos_ScalarTraits.hpp"
32#include "Teuchos_ParameterList.hpp"
33#include "Teuchos_TimeMonitor.hpp"
55 template <
class ScalarType,
class MV>
67 void initialize(Teuchos::RCP<const MV> tmp,
int _numVectors) {
69 this->
R = MVT::Clone( *tmp, _numVectors );
70 this->
Z = MVT::Clone( *tmp, _numVectors );
71 this->
P = MVT::Clone( *tmp, _numVectors );
72 this->
AP = MVT::Clone(*tmp, _numVectors );
77 bool matches(Teuchos::RCP<const MV> tmp,
int _numVectors=1)
const {
82 template<
class ScalarType,
class MV,
class OP>
92 using SCT = Teuchos::ScalarTraits<ScalarType>;
106 Teuchos::ParameterList ¶ms );
168 Teuchos::RCP<CGIterationStateBase<ScalarType,MV> >
getState()
const {
178 auto s = Teuchos::rcp_dynamic_cast<PseudoBlockCGIterationState<ScalarType,MV> >(state,
true);
219 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
220 "Belos::PseudoBlockCGIter::setBlockSize(): Cannot use a block size that is not one.");
230 if (numEntriesForCondEst_ != 0) doCondEst_=val;
238 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
239 if (
static_cast<size_type
> (iter_) >= diag_.size ()) {
242 return diag_ (0, iter_);
253 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
254 if (
static_cast<size_type
> (iter_) >= offdiag_.size ()) {
257 return offdiag_ (0, iter_);
266 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
267 const Teuchos::RCP<OutputManager<ScalarType> > om_;
268 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
288 bool assertPositiveDefiniteness_;
291 Teuchos::ArrayRCP<MagnitudeType> diag_, offdiag_;
292 ScalarType pAp_old_, beta_old_, rHz_old2_;
293 int numEntriesForCondEst_;
309 Teuchos::RCP<MV> AP_;
315 template<
class ScalarType,
class MV,
class OP>
319 Teuchos::ParameterList ¶ms ):
326 assertPositiveDefiniteness_( params.get(
"Assert Positive Definiteness", true) ),
327 numEntriesForCondEst_(params.get(
"Max Size For Condest",0) ),
335 template <
class ScalarType,
class MV,
class OP>
339 Teuchos::RCP<const MV> lhsMV = lp_->getCurrLHSVec();
340 Teuchos::RCP<const MV> rhsMV = lp_->getCurrRHSVec();
341 TEUCHOS_TEST_FOR_EXCEPTION((lhsMV==Teuchos::null && rhsMV==Teuchos::null),std::invalid_argument,
342 "Belos::PseudoBlockCGIter::initialize(): Cannot initialize state storage!");
345 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
352 TEUCHOS_ASSERT(!newstate.is_null());
354 newstate->initialize(tmp, numRHS_);
358 if(numEntriesForCondEst_ > 0) {
359 diag_.resize(numEntriesForCondEst_);
360 offdiag_.resize(numEntriesForCondEst_-1);
363 std::string errstr(
"Belos::BlockPseudoCGIter::initialize(): Specified multivectors must have a consistent length and width.");
368 std::invalid_argument, errstr );
370 std::invalid_argument, errstr );
381 if ( lp_->getLeftPrec() != Teuchos::null ) {
382 lp_->applyLeftPrec( *R_, *Z_ );
383 if ( lp_->getRightPrec() != Teuchos::null ) {
384 Teuchos::RCP<MV> tmp1 =
MVT::Clone( *Z_, numRHS_ );
385 lp_->applyRightPrec( *Z_, *tmp1 );
389 else if ( lp_->getRightPrec() != Teuchos::null ) {
390 lp_->applyRightPrec( *R_, *Z_ );
405 template <
class ScalarType,
class MV,
class OP>
417 std::vector<int> index(1);
418 std::vector<ScalarType> rHz( numRHS_ );
419 std::vector<ScalarType> rHz_old( numRHS_ );
420 std::vector<ScalarType> pAp( numRHS_ );
421 std::vector<ScalarType> beta( numRHS_ );
422 Teuchos::SerialDenseMatrix<int, ScalarType> alpha( numRHS_,numRHS_ );
425 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
426 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
429 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
434 if ( assertPositiveDefiniteness_ )
435 for (i=0; i<numRHS_; ++i)
436 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
438 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
443 while (stest_->checkStatus(
this) !=
Passed) {
449 lp_->applyOp( *P_, *AP_ );
454 for (i=0; i<numRHS_; ++i) {
455 if ( assertPositiveDefiniteness_ )
457 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(pAp[i]) <= zero,
459 "Belos::PseudoBlockCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
461 alpha(i,i) = rHz[i] / pAp[i];
468 lp_->updateSolution();
472 for (i=0; i<numRHS_; ++i) {
483 if ( lp_->getLeftPrec() != Teuchos::null ) {
484 lp_->applyLeftPrec( *R_, *Z_ );
485 if ( lp_->getRightPrec() != Teuchos::null ) {
486 Teuchos::RCP<MV> tmp =
MVT::Clone( *Z_, numRHS_ );
487 lp_->applyRightPrec( *Z_, *tmp );
491 else if ( lp_->getRightPrec() != Teuchos::null ) {
492 lp_->applyRightPrec( *R_, *Z_ );
499 if ( assertPositiveDefiniteness_ )
500 for (i=0; i<numRHS_; ++i)
501 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
503 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
506 for (i=0; i<numRHS_; ++i) {
507 beta[i] = rHz[i] / rHz_old[i];
515 if (doCondEst_ && (iter_ - 1) < diag_.size()) {
517 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real((beta_old_ * beta_old_ * pAp_old_ + pAp[0]) / rHz_old[0]);
518 offdiag_[iter_-2] = -Teuchos::ScalarTraits<ScalarType>::real(beta_old_ * pAp_old_ / (sqrt( rHz_old[0] * rHz_old2_)));
521 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real(pAp[0] / rHz_old[0]);
523 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 > R
The current residual.
Teuchos::RCP< MV > P
The current decent direction vector.
virtual void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
virtual bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
Teuchos::RCP< MV > Z
The current preconditioned residual.
Teuchos::RCP< MV > AP
The matrix A applied to current decent direction vector.
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 MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
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< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow 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 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...
typename SCT::magnitudeType MagnitudeType
void resetNumIters(int iter=0)
Reset the iteration count.
OperatorTraits< ScalarType, MV, OP > OPT
virtual ~PseudoBlockCGIter()=default
Destructor.
Teuchos::ScalarTraits< ScalarType > SCT
int getNumIters() const
Get the current iteration count.
Teuchos::RCP< CGIterationStateBase< ScalarType, MV > > getState() const
Get the current state of the linear solver.
bool isInitialized()
States whether the solver has been initialized or not.
PseudoBlockCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, Teuchos::ParameterList ¶ms)
PseudoBlockCGIter constructor with linear problem, solver utilities, and parameter list of solver opt...
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation.
void initializeCG(Teuchos::RCP< CGIterationStateBase< ScalarType, MV > > newstate, Teuchos::RCP< MV > R_0)
Initialize the solver to an iterate, providing a complete state.
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation.
MultiVecTraits< ScalarType, MV > MVT
void iterate()
This method performs CG iterations on each linear system until the status test indicates the need to ...
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV > > state)
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
void setBlockSize(int blockSize)
Set the blocksize.
Structure to contain pointers to PseudoBlockCGIteration state variables.
PseudoBlockCGIterationState()=default
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
virtual ~PseudoBlockCGIterationState()=default
PseudoBlockCGIterationState(Teuchos::RCP< const MV > tmp)
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
A pure virtual class for defining the status tests for the Belos iterative solvers.