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>
82 template<
class ScalarType,
class MV,
class OP>
92 using SCT = Teuchos::ScalarTraits<ScalarType>;
106 Teuchos::ParameterList &
params );
168 Teuchos::RCP<CGIterationStateBase<ScalarType,MV> >
getState()
const {
178 auto s = Teuchos::rcp_dynamic_cast<PseudoBlockCGIterationState<ScalarType,MV> >(
state,
true);
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_;
293 int numEntriesForCondEst_;
309 Teuchos::RCP<MV> AP_;
315 template<
class ScalarType,
class MV,
class OP>
319 Teuchos::ParameterList &
params ):
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();
342 "Belos::PseudoBlockCGIter::initialize(): Cannot initialize state storage!");
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 );
375 MVT::Assign( *
R_0, *R_ );
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_ );
393 MVT::Assign( *R_, *Z_ );
395 MVT::Assign( *Z_, *P_ );
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();
432 MVT::MvDot( *R_, *Z_,
rHz );
434 if ( assertPositiveDefiniteness_ )
435 for (
i=0;
i<numRHS_; ++
i)
438 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
443 while (stest_->checkStatus(
this) !=
Passed) {
449 lp_->applyOp( *P_, *AP_ );
452 MVT::MvDot( *P_, *AP_,
pAp );
454 for (
i=0;
i<numRHS_; ++
i) {
455 if ( assertPositiveDefiniteness_ )
459 "Belos::PseudoBlockCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
468 lp_->updateSolution();
472 for (
i=0;
i<numRHS_; ++
i) {
478 MVT::MvTimesMatAddMv( -
one, *AP_, alpha,
one, *R_ );
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_ );
498 MVT::MvDot( *R_, *Z_,
rHz );
499 if ( assertPositiveDefiniteness_ )
500 for (
i=0;
i<numRHS_; ++
i)
503 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
506 for (
i=0;
i<numRHS_; ++
i) {
509 Teuchos::RCP<const MV>
Z_i = MVT::CloneView( *Z_, index );
510 Teuchos::RCP<MV>
P_i = MVT::CloneViewNonConst( *P_, index );
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]);
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,...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
This class implements the pseudo-block CG iteration, where the basic CG algorithm is performed on all...
typename SCT::magnitudeType MagnitudeType
void resetNumIters(int iter=0)
Reset the iteration count.
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.
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
Get the norms of the residuals native to the solver.
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