42 #ifndef BELOS_PCPG_ITER_HPP
43 #define BELOS_PCPG_ITER_HPP
87 template <
class ScalarType,
class MV>
124 R(Teuchos::null),
Z(Teuchos::null),
125 P(Teuchos::null),
AP(Teuchos::null),
126 U(Teuchos::null),
C(Teuchos::null),
184 template<
class ScalarType,
class MV,
class OP>
316 if (!initialized_)
return 0;
322 if (!initialized_)
return 0;
346 "Belos::PCPGIter::setBlockSize(): Cannot use a block size that is not one.");
350 void setSize(
int savedBlocks );
392 bool stateStorageInitialized_;
437 template<
class ScalarType,
class MV,
class OP>
449 stateStorageInitialized_(false),
450 keepDiagonal_(false),
451 initDiagonal_(false),
459 "Belos::PCPGIter::constructor: mandatory parameter \"Saved Blocks\" is not specified.");
460 int rb = Teuchos::getParameter<int>(params,
"Saved Blocks");
463 keepDiagonal_ = params.get(
"Keep Diagonal",
false);
466 initDiagonal_ = params.get(
"Initialize Diagonal",
false);
474 template <
class ScalarType,
class MV,
class OP>
480 TEUCHOS_TEST_FOR_EXCEPTION(savedBlocks <= 0, std::invalid_argument,
"Belos::PCPGIter::setSize() was passed a non-positive argument for \"Num Saved Blocks\".");
482 if ( savedBlocks_ != savedBlocks) {
483 stateStorageInitialized_ =
false;
484 savedBlocks_ = savedBlocks;
485 initialized_ =
false;
494 template <
class ScalarType,
class MV,
class OP>
497 stateStorageInitialized_ =
false;
498 initialized_ =
false;
506 template <
class ScalarType,
class MV,
class OP>
509 if (!stateStorageInitialized_) {
514 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
521 int newsd = savedBlocks_ ;
526 if (Z_ == Teuchos::null) {
528 Z_ = MVT::Clone( *tmp, 1 );
530 if (P_ == Teuchos::null) {
532 P_ = MVT::Clone( *tmp, 1 );
534 if (AP_ == Teuchos::null) {
536 AP_ = MVT::Clone( *tmp, 1 );
539 if (C_ == Teuchos::null) {
544 "Belos::PCPGIter::setStateSize(): linear problem does not specify multivectors to clone from.");
546 "Belos::PCPGIter::setStateSize(): prevUdim not zero and C is null.");
547 C_ = MVT::Clone( *tmp, savedBlocks_ );
551 if (MVT::GetNumberVecs(*C_) < savedBlocks_ ) {
553 C_ = MVT::Clone( *tmp, savedBlocks_ );
556 if (U_ == Teuchos::null) {
559 "Belos::PCPGIter::setStateSize(): prevUdim not zero and U is null.");
560 U_ = MVT::Clone( *tmp, savedBlocks_ );
564 if (MVT::GetNumberVecs(*U_) < savedBlocks_ ) {
566 U_ = MVT::Clone( *tmp, savedBlocks_ );
570 if (D_ == Teuchos::null) {
574 D_->shape( newsd, newsd );
577 if (D_->numRows() < newsd || D_->numCols() < newsd) {
578 D_->shapeUninitialized( newsd, newsd );
583 stateStorageInitialized_ =
true;
590 template <
class ScalarType,
class MV,
class OP>
595 "Belos::PCPGIter::initialize(): Cannot initialize state storage!");
599 std::string errstr(
"Belos::PCPGIter::initialize(): Specified multivectors must have a consistent length and width.");
601 if (newstate.
R != Teuchos::null){
604 if (newstate.
U == Teuchos::null){
610 prevUdim_ = newstate.
curDim;
611 if (newstate.
C == Teuchos::null){
612 std::vector<int> index(prevUdim_);
613 for (
int i=0; i< prevUdim_; ++i)
616 newstate.
C = MVT::Clone( *newstate.
U, prevUdim_ );
618 lp_->apply( *Ukeff, *Ckeff );
620 curDim_ = prevUdim_ ;
624 if (!stateStorageInitialized_)
631 newstate.
curDim = curDim_;
635 std::vector<int> zero_index(1);
637 if ( lp_->getLeftPrec() != Teuchos::null ) {
638 lp_->applyLeftPrec( *R_, *Z_ );
639 MVT::SetBlock( *Z_, zero_index , *P_ );
642 MVT::SetBlock( *R_, zero_index, *P_ );
645 std::vector<int> nextind(1);
646 nextind[0] = curDim_;
648 MVT::SetBlock( *P_, nextind, *newstate.
U );
651 newstate.
curDim = curDim_;
654 std::invalid_argument, errstr );
655 if (newstate.
U != U_) {
660 std::invalid_argument, errstr );
661 if (newstate.
C != C_) {
668 "Belos::PCPGIter::initialize(): PCPGStateIterState does not have initial kernel R_0.");
678 template <
class ScalarType,
class MV,
class OP>
684 if (initialized_ ==
false) {
696 std::cout <<
" Iterate Warning: begin from nonzero iter_ ?" << std::endl;
699 std::vector<int> prevInd;
705 prevInd.resize( prevUdim_ );
706 for(
int i=0; i<prevUdim_ ; i++) prevInd[i] = i;
708 Uprev = MVT::CloneView(*U_, prevInd);
709 Cprev = MVT::CloneView(*C_, prevInd);
717 "Belos::CGIter::iterate(): current linear system has more than one std::vector!" );
721 "Belos::CGIter::iterate(): mistake in initialization !" );
728 std::vector<int> curind(1);
729 std::vector<ScalarType> rnorm(MVT::GetNumberVecs(*cur_soln_vec));
732 curind[0] = curDim_ - 1;
733 P = MVT::CloneViewNonConst(*U_,curind);
734 MVT::MvTransMv( one, *Cprev, *P, CZ );
735 MVT::MvTimesMatAddMv( -one, *Uprev, CZ, one, *P );
738 MVT::MvTransMv( one, *Cprev, *P, CZ );
739 std::cout <<
" Input CZ post ortho " << std::endl;
740 CZ.
print( std::cout );
742 if( curDim_ == savedBlocks_ ){
743 std::vector<int> zero_index(1);
745 MVT::SetBlock( *P, zero_index, *P_ );
751 MVT::MvTransMv( one, *R_, *Z_, rHz );
756 while (stest_->checkStatus(
this) !=
Passed ) {
761 curind[0] = curDim_ - 1;
763 MVT::MvNorm(*R_, rnorm);
764 std::cout << iter_ <<
" " << curDim_ <<
" " << rnorm[0] << std::endl;
766 if( prevUdim_ + iter_ < savedBlocks_ ){
767 P = MVT::CloneView(*U_,curind);
768 AP = MVT::CloneViewNonConst(*C_,curind);
769 lp_->applyOp( *P, *AP );
770 MVT::MvTransMv( one, *P, *AP, pAp );
772 if( prevUdim_ + iter_ == savedBlocks_ ){
773 AP = MVT::CloneViewNonConst(*C_,curind);
774 lp_->applyOp( *P_, *AP );
775 MVT::MvTransMv( one, *P_, *AP, pAp );
777 lp_->applyOp( *P_, *AP_ );
778 MVT::MvTransMv( one, *P_, *AP_, pAp );
782 if( keepDiagonal_ && prevUdim_ + iter_ <= savedBlocks_ )
783 (*D_)(iter_ -1 ,iter_ -1 ) = pAp(0,0);
787 "Belos::CGIter::iterate(): non-positive value for p^H*A*p encountered!" );
790 alpha(0,0) = rHz(0,0) / pAp(0,0);
794 "Belos::CGIter::iterate(): non-positive value for alpha encountered!" );
797 if( curDim_ < savedBlocks_ ){
798 MVT::MvAddMv( one, *cur_soln_vec, alpha(0,0), *P, *cur_soln_vec );
800 MVT::MvAddMv( one, *cur_soln_vec, alpha(0,0), *P_, *cur_soln_vec );
806 rHz_old(0,0) = rHz(0,0);
810 if( prevUdim_ + iter_ <= savedBlocks_ ){
811 MVT::MvAddMv( one, *R_, -alpha(0,0), *AP, *R_ );
814 MVT::MvAddMv( one, *R_, -alpha(0,0), *AP_, *R_ );
819 if ( lp_->getLeftPrec() != Teuchos::null ) {
820 lp_->applyLeftPrec( *R_, *Z_ );
825 MVT::MvTransMv( one, *R_, *Z_, rHz );
827 beta(0,0) = rHz(0,0) / rHz_old(0,0);
829 if( curDim_ < savedBlocks_ ){
831 curind[0] = curDim_ - 1;
833 MVT::MvAddMv( one, *Z_, beta(0,0), *P, *Pnext );
835 MVT::MvTransMv( one, *Cprev, *Z_, CZ );
836 MVT::MvTimesMatAddMv( -one, *Uprev, CZ, one, *Pnext );
838 std::cout <<
" Check CZ " << std::endl;
839 MVT::MvTransMv( one, *Cprev, *Pnext, CZ );
840 CZ.
print( std::cout );
844 if( curDim_ == savedBlocks_ ){
845 std::vector<int> zero_index(1);
847 MVT::SetBlock( *Pnext, zero_index, *P_ );
849 Pnext = Teuchos::null;
851 MVT::MvAddMv( one, *Z_, beta(0,0), *P_, *P_ );
853 MVT::MvTransMv( one, *Cprev, *Z_, CZ );
854 MVT::MvTimesMatAddMv( -one, *Uprev, CZ, one, *P_ );
857 std::cout <<
" Check CZ " << std::endl;
858 MVT::MvTransMv( one, *Cprev, *P_, CZ );
859 CZ.
print( std::cout );
868 TEUCHOS_TEST_FOR_EXCEPTION( AP != Teuchos::null || P != Teuchos::null, std::logic_error,
"Loop recurrence violated. Please contact Belos team.");
870 if( prevUdim_ + iter_ < savedBlocks_ ) --curDim_;
PCPGIterateFailure is thrown when the PCPGIter object breaks down.
Collection of types and exceptions used within the Belos solvers.
int prevUdim
Number of block columns in matrices C and U before current iteration.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
bool isInitialized()
States whether the solver has been initialized or not.
Class which manages the output and verbosity of the Belos solvers.
virtual void print(std::ostream &os) const
Teuchos::RCP< MV > R
The current residual.
PCPGIterState< ScalarType, MV > getState() const
Get the current state of the linear solver.
Pure virtual base class for defining the status testing capabilities of Belos.
Teuchos::RCP< MV > C
C = AU, U spans recycled subspace.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
int getCurSubspaceDim() const
Get the current dimension of the whole seed subspace.
Declaration of basic traits for the multivector type.
void setSize(int savedBlocks)
Set the maximum number of saved or recycled blocks used by the iterative solver.
int curDim
The current dimension of the reduction.
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::RCP< MV > P
The current decent direction std::vector.
Teuchos::RCP< MV > AP
The matrix A applied to current decent direction std::vector.
An extension of the MultiVecTraits class that adds a new vector length method.
Structure to contain pointers to PCPGIter state variables.
OperatorTraits< ScalarType, MV, OP > OPT
int getPrevSubspaceDim() const
Get the dimension of the search subspace used to solve the current solution to the linear problem...
A pure virtual class for defining the status tests for the Belos iterative solvers.
int getBlockSize() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
Class which defines basic traits for the operator type.
PCPGIterLAPACKFailure is thrown when a nonzero return value is passed back from an LAPACK routine...
PCPGIterLAPACKFailure(const std::string &what_arg)
MultiVecTraitsExt< ScalarType, MV > MVText
Traits class which defines basic operations on multivectors.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > D
The current Hessenberg matrix.
bool isParameter(const std::string &name) const
virtual ~PCPGIter()
Destructor.
void setBlockSize(int blockSize)
Get the blocksize to be used by the iterative solver in solving this linear problem.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int getNumIters() const
Get the current iteration count.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
void initialize()
Initialize the solver with the initial vectors from the linear problem. An exception is thrown if ini...
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
PCPGIterOrthoFailure(const std::string &what_arg)
int getNumRecycledBlocks() const
Get the maximum number of recycled blocks 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.
SCT::magnitudeType MagnitudeType
Teuchos::RCP< MV > U
The recycled subspace.
void resetState()
tell the Iterator to "reset" itself; delete and rebuild the seed space.
PCPGIterOrthoFailure is thrown when the PCPGIter object is unable to compute independent direction ve...
int reshape(OrdinalType numRows, OrdinalType numCols)
void iterate()
PCPGIter iterates CG until the status test either requests a stop or detects an error. In the latter case, std::exception is thrown.
PCPGIterateFailure(const std::string &what_arg)
Teuchos::RCP< MV > Z
The current preconditioned residual.
Class which defines basic traits for the operator type.
PCPGIter(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< MatOrthoManager< ScalarType, MV, OP > > &ortho, Teuchos::ParameterList ¶ms)
PCPGIter constructor with linear problem, solver utilities, and parameter list of solver options...
Parent class to all Belos exceptions.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
This class implements the PCPG iteration, where a single-std::vector Krylov subspace is constructed...
Belos header file which uses auto-configuration information to include necessary C++ headers...
MultiVecTraits< ScalarType, MV > MVT
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system solution?.
PCPGIterInitFailure is thrown when the PCPGIter object is unable to generate an initial iterate in th...
PCPGIterInitFailure(const std::string &what_arg)
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::ScalarTraits< ScalarType > SCT