10#ifndef BELOS_BLOCK_CG_ITER_HPP
11#define BELOS_BLOCK_CG_ITER_HPP
29#include "Teuchos_ScalarTraits.hpp"
30#include "Teuchos_ParameterList.hpp"
31#include "Teuchos_TimeMonitor.hpp"
42 template <
class ScalarType,
class MV,
class DM>
78template<
class ScalarType,
class MV,
class OP,
class DM,
79 const bool lapackSupportsScalarType =
85 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
92 Teuchos::ParameterList & )
111 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState ()
const {
127 Teuchos::RCP<const MV>
162template<
class ScalarType,
class MV,
class OP,
class DM>
172 using SCT = Teuchos::ScalarTraits<ScalarType>;
187 Teuchos::ParameterList &
params );
241 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState()
const {
251 auto s = Teuchos::rcp_dynamic_cast<BlockCGIterationState<ScalarType,MV, DM> >(
state,
true);
301 Teuchos::ArrayView<MagnitudeType>
temp;
307 Teuchos::ArrayView<MagnitudeType>
temp;
320 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
321 const Teuchos::RCP<OutputManager<ScalarType> > om_;
322 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
323 const Teuchos::RCP<OrthoManager<ScalarType,MV,DM> > ortho_;
355 Teuchos::RCP<MV> AP_;
359 template<
class ScalarType,
class MV,
class OP,
class DM>
365 Teuchos::ParameterList&
params) :
375 int bs =
params.get(
"Block Size", 1);
379 template<
class ScalarType,
class MV,
class OP,
class DM>
385 (
blockSize <= 0, std::invalid_argument,
"Belos::BlockGmresIter::"
386 "setBlockSize: blockSize = " <<
blockSize <<
" <= 0.");
391 initialized_ =
false;
394 template <
class ScalarType,
class MV,
class OP,
class DM>
398 const char prefix[] =
"Belos::BlockCGIter::initialize: ";
401 Teuchos::RCP<const MV>
lhsMV = lp_->getLHS();
402 Teuchos::RCP<const MV>
rhsMV = lp_->getRHS();
410 const char errstr[] =
"Specified multivectors must have a consistent "
416 (MVT::GetGlobalLength(*
R_0) != MVT::GetGlobalLength(*R_),
419 (MVT::GetNumberVecs(*
R_0) != blockSize_,
425 MVT::Assign( *
R_0, *R_ );
430 if ( lp_->getLeftPrec() != Teuchos::null ) {
431 lp_->applyLeftPrec( *R_, *Z_ );
432 if ( lp_->getRightPrec() != Teuchos::null ) {
433 Teuchos::RCP<MV>
tmp2 = MVT::Clone( *Z_, blockSize_ );
434 lp_->applyRightPrec( *Z_, *
tmp2 );
438 else if ( lp_->getRightPrec() != Teuchos::null ) {
439 lp_->applyRightPrec( *R_, *Z_ );
444 MVT::Assign( *Z_, *P_ );
451 template<
class ScalarType,
class MV,
class OP,
class DM>
454 const char prefix[] =
"Belos::BlockCGIter::iterate: ";
460 if (initialized_ ==
false) {
465 Teuchos::RCP<DM> alpha = DMT::Create( blockSize_, blockSize_ );
466 Teuchos::RCP<DM> beta = DMT::Create( blockSize_, blockSize_ );
467 Teuchos::RCP<DM>
pAp = DMT::Create( blockSize_, blockSize_ );
470 Teuchos::RCP<DenseSolver<ScalarType,DM>>
lltSolver = DMT::createDenseSolver();
474 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
482 prefix <<
"Current linear system does not have the right number of vectors!" );
483 int rank = ortho_->normalize( *P_, Teuchos::null );
486 prefix <<
"Failed to compute initial block of orthonormal direction vectors.");
491 while (stest_->checkStatus(
this) !=
Passed) {
496 lp_->applyOp( *P_, *AP_ );
503 MVT::MvTransMv(
one, *P_, *R_, *alpha );
504 MVT::MvTransMv(
one, *P_, *AP_, *
pAp );
508 lltSolver->factorWithEquilibration(
true );
512 prefix <<
"Failed to compute Cholesky factorization using LAPACK routine POTRF.");
520 prefix <<
"Failed to compute alpha using Cholesky factorization (POTRS).");
524 lp_->updateSolution();
527 MVT::MvTimesMatAddMv( -
one, *AP_, *alpha,
one, *R_ );
530 if ( lp_->getLeftPrec() != Teuchos::null ) {
531 lp_->applyLeftPrec( *R_, *Z_ );
532 if ( lp_->getRightPrec() != Teuchos::null ) {
533 Teuchos::RCP<MV>
tmp = MVT::Clone( *Z_, blockSize_ );
534 lp_->applyRightPrec( *Z_, *
tmp );
538 else if ( lp_->getRightPrec() != Teuchos::null ) {
539 lp_->applyRightPrec( *R_, *Z_ );
551 MVT::MvTransMv( -
one, *AP_, *Z_, *beta );
557 prefix <<
"Failed to compute beta using Cholesky factorization (POTRS).");
560 Teuchos::RCP<MV>
Pnew = MVT::CloneCopy( *Z_ );
561 MVT::MvTimesMatAddMv(
one, *P_, *beta,
one, *
Pnew);
565 rank = ortho_->normalize( *P_, Teuchos::null );
568 prefix <<
"Failed to compute block of orthonormal direction vectors.");
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.
virtual ~BlockCGIter()=default
Destructor.
int getNumIters() const
Get the current iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
typename SCT::magnitudeType MagnitudeType
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation (NOT_IMPLEMENTED)
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
int getBlockSize() const
Get the block size to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Get the norms of the residuals native to the solver.
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation (NOT_IMPLEMENTED)
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
bool isInitialized()
States whether the solver has been initialized or not.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
void setDoCondEst(bool)
Sets whether or not to store the diagonal for condition estimation.
Stub implementation of BlockCGIter, for ScalarType types for which Teuchos::LAPACK does NOT have a va...
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void resetNumIters(int iter=0)
Reset the iteration count to iter.
BlockCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &, const Teuchos::RCP< OutputManager< ScalarType > > &, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &, const Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP, DM > > &, Teuchos::ParameterList &)
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
void iterate()
This method performs linear solver iterations until the status test indicates the need to stop or an ...
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
Teuchos::ScalarTraits< ScalarType > SCT
SCT::magnitudeType MagnitudeType
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
MultiVecTraits< ScalarType, MV, DM > MVT
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
void initializeCG(Teuchos::RCP< BlockCGIterationState< ScalarType, MV, DM > >, Teuchos::RCP< MV >)
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
int getNumIters() const
Get the current iteration count.
bool isInitialized()
States whether the solver has been initialized or not.
OperatorTraits< ScalarType, MV, OP > OPT
Structure to contain pointers to BlockCGIteration state variables.
BlockCGIterationState()=default
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
BlockCGIterationState(Teuchos::RCP< const MV > tmp)
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
virtual ~BlockCGIterationState()=default
CGIterateFailure is thrown when the CGIteration object is unable to compute the next iterate in the C...
CGIterationLAPACKFailure is thrown when a nonzero return value is passed back from an LAPACK routine.
CGIterationOrthoFailure is thrown when the CGIteration object is unable to compute independent direct...
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.
Type traits class that says whether Teuchos::LAPACK has a valid implementation for the given ScalarTy...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).