33 #ifndef ANASAZI_BLOCK_DAVIDSON_HPP
34 #define ANASAZI_BLOCK_DAVIDSON_HPP
41 #include "Teuchos_ScalarTraits.hpp"
46 #include "Teuchos_LAPACK.hpp"
47 #include "Teuchos_BLAS.hpp"
48 #include "Teuchos_SerialDenseMatrix.hpp"
49 #include "Teuchos_ParameterList.hpp"
50 #include "Teuchos_TimeMonitor.hpp"
76 template <
class ScalarType,
class MV>
87 Teuchos::RCP<const MV>
V;
89 Teuchos::RCP<const MV>
X;
91 Teuchos::RCP<const MV>
KX;
93 Teuchos::RCP<const MV>
MX;
95 Teuchos::RCP<const MV>
R;
100 Teuchos::RCP<const MV>
H;
102 Teuchos::RCP<const std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> >
T;
108 Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> >
KK;
110 X(Teuchos::null),
KX(Teuchos::null),
MX(Teuchos::null),
111 R(Teuchos::null),
H(Teuchos::null),
112 T(Teuchos::null),
KK(Teuchos::null) {}
150 template <
class ScalarType,
class MV,
class OP>
164 const Teuchos::RCP<
SortManager<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > &sorter,
168 Teuchos::ParameterList ¶ms
319 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
getResNorms();
327 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
getRes2Norms();
337 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
getRitzRes2Norms();
359 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
getStatusTest()
const;
390 void setAuxVecs(
const Teuchos::Array<Teuchos::RCP<const MV> > &auxvecs);
393 Teuchos::Array<Teuchos::RCP<const MV> >
getAuxVecs()
const;
409 void setSize(
int blockSize,
int numBlocks);
429 typedef Teuchos::ScalarTraits<ScalarType> SCT;
430 typedef typename SCT::magnitudeType MagnitudeType;
431 const MagnitudeType ONE;
432 const MagnitudeType ZERO;
433 const MagnitudeType NANVAL;
439 bool checkX, checkMX, checkKX;
440 bool checkH, checkMH, checkKH;
443 CheckList() : checkV(
false),
444 checkX(
false),checkMX(
false),checkKX(
false),
445 checkH(
false),checkMH(
false),checkKH(
false),
446 checkR(
false),checkQ(
false),checkKK(
false) {};
451 std::string accuracyCheck(
const CheckList &chk,
const std::string &where)
const;
455 const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > problem_;
456 const Teuchos::RCP<SortManager<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > sm_;
457 const Teuchos::RCP<OutputManager<ScalarType> > om_;
458 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > tester_;
459 const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > orthman_;
463 Teuchos::RCP<const OP> Op_;
464 Teuchos::RCP<const OP> MOp_;
465 Teuchos::RCP<const OP> Prec_;
470 Teuchos::RCP<Teuchos::Time> timerOp_, timerMOp_, timerPrec_,
471 timerSortEval_, timerDS_,
472 timerLocal_, timerCompRes_,
473 timerOrtho_, timerInit_;
477 int count_ApplyOp_, count_ApplyM_, count_ApplyPrec_;
506 Teuchos::RCP<MV> X_, KX_, MX_, R_,
512 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > KK_;
515 Teuchos::Array<Teuchos::RCP<const MV> > auxVecs_;
522 std::vector<MagnitudeType> theta_, Rnorms_, R2norms_;
525 bool Rnorms_current_, R2norms_current_;
540 template <
class ScalarType,
class MV,
class OP>
543 const Teuchos::RCP<
SortManager<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > &sorter,
547 Teuchos::ParameterList ¶ms
549 ONE(Teuchos::ScalarTraits<MagnitudeType>::one()),
550 ZERO(Teuchos::ScalarTraits<MagnitudeType>::zero()),
551 NANVAL(Teuchos::ScalarTraits<MagnitudeType>::nan()),
559 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
560 timerOp_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Operation Op*x")),
561 timerMOp_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Operation M*x")),
562 timerPrec_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Operation Prec*x")),
563 timerSortEval_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Sorting eigenvalues")),
564 timerDS_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Direct solve")),
565 timerLocal_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Local update")),
566 timerCompRes_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Computing residuals")),
567 timerOrtho_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Orthogonalization")),
568 timerInit_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockDavidson::Initialization")),
578 auxVecs_( Teuchos::Array<Teuchos::RCP<const MV> >(0) ),
581 Rnorms_current_(false),
582 R2norms_current_(false)
584 TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,std::invalid_argument,
585 "Anasazi::BlockDavidson::constructor: user passed null problem pointer.");
586 TEUCHOS_TEST_FOR_EXCEPTION(sm_ == Teuchos::null,std::invalid_argument,
587 "Anasazi::BlockDavidson::constructor: user passed null sort manager pointer.");
588 TEUCHOS_TEST_FOR_EXCEPTION(om_ == Teuchos::null,std::invalid_argument,
589 "Anasazi::BlockDavidson::constructor: user passed null output manager pointer.");
590 TEUCHOS_TEST_FOR_EXCEPTION(tester_ == Teuchos::null,std::invalid_argument,
591 "Anasazi::BlockDavidson::constructor: user passed null status test pointer.");
592 TEUCHOS_TEST_FOR_EXCEPTION(orthman_ == Teuchos::null,std::invalid_argument,
593 "Anasazi::BlockDavidson::constructor: user passed null orthogonalization manager pointer.");
594 TEUCHOS_TEST_FOR_EXCEPTION(problem_->isProblemSet() ==
false, std::invalid_argument,
595 "Anasazi::BlockDavidson::constructor: problem is not set.");
596 TEUCHOS_TEST_FOR_EXCEPTION(problem_->isHermitian() ==
false, std::invalid_argument,
597 "Anasazi::BlockDavidson::constructor: problem is not hermitian.");
600 Op_ = problem_->getOperator();
601 TEUCHOS_TEST_FOR_EXCEPTION(Op_ == Teuchos::null, std::invalid_argument,
602 "Anasazi::BlockDavidson::constructor: problem provides no operator.");
603 MOp_ = problem_->getM();
604 Prec_ = problem_->getPrec();
605 hasM_ = (MOp_ != Teuchos::null);
608 int bs = params.get(
"Block Size", problem_->getNEV());
609 int nb = params.get(
"Num Blocks", 2);
616 template <
class ScalarType,
class MV,
class OP>
623 template <
class ScalarType,
class MV,
class OP>
626 setSize(blockSize,numBlocks_);
632 template <
class ScalarType,
class MV,
class OP>
641 template <
class ScalarType,
class MV,
class OP>
649 template <
class ScalarType,
class MV,
class OP>
657 template <
class ScalarType,
class MV,
class OP>
659 return blockSize_*numBlocks_;
664 template <
class ScalarType,
class MV,
class OP>
666 if (!initialized_)
return 0;
673 template <
class ScalarType,
class MV,
class OP>
674 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
676 return this->getRes2Norms();
682 template <
class ScalarType,
class MV,
class OP>
684 std::vector<int> ret(curDim_,0);
691 template <
class ScalarType,
class MV,
class OP>
693 std::vector<Value<ScalarType> > ret(curDim_);
694 for (
int i=0; i<curDim_; ++i) {
695 ret[i].realpart = theta_[i];
696 ret[i].imagpart = ZERO;
704 template <
class ScalarType,
class MV,
class OP>
712 template <
class ScalarType,
class MV,
class OP>
720 template <
class ScalarType,
class MV,
class OP>
728 template <
class ScalarType,
class MV,
class OP>
739 state.
MX = Teuchos::null;
745 state.
T = Teuchos::rcp(
new std::vector<MagnitudeType>(theta_.begin(),theta_.begin()+curDim_) );
747 state.
T = Teuchos::rcp(
new std::vector<MagnitudeType>(0));
755 template <
class ScalarType,
class MV,
class OP>
761 template <
class ScalarType,
class MV,
class OP>
765 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
766 Teuchos::TimeMonitor initimer( *timerInit_ );
772 TEUCHOS_TEST_FOR_EXCEPTION(blockSize < 1, std::invalid_argument,
"Anasazi::BlockDavidson::setSize(blocksize,numblocks): blocksize must be strictly positive.");
773 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks < 2, std::invalid_argument,
"Anasazi::BlockDavidson::setSize(blocksize,numblocks): numblocks must be greater than one.");
774 if (blockSize == blockSize_ && numBlocks == numBlocks_) {
779 blockSize_ = blockSize;
780 numBlocks_ = numBlocks;
782 Teuchos::RCP<const MV> tmp;
787 if (X_ != Teuchos::null) {
791 tmp = problem_->getInitVec();
792 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
793 "Anasazi::BlockDavidson::setSize(): eigenproblem did not specify initial vectors to clone from.");
796 TEUCHOS_TEST_FOR_EXCEPTION(numAuxVecs_+blockSize*static_cast<ptrdiff_t>(numBlocks) > MVText::GetGlobalLength(*tmp),std::invalid_argument,
797 "Anasazi::BlockDavidson::setSize(): max subspace dimension and auxilliary subspace too large.");
804 Rnorms_.resize(blockSize_,NANVAL);
805 R2norms_.resize(blockSize_,NANVAL);
816 om_->print(
Debug,
" >> Allocating X_\n");
817 X_ = MVT::Clone(*tmp,blockSize_);
818 om_->print(
Debug,
" >> Allocating KX_\n");
819 KX_ = MVT::Clone(*tmp,blockSize_);
821 om_->print(
Debug,
" >> Allocating MX_\n");
822 MX_ = MVT::Clone(*tmp,blockSize_);
827 om_->print(
Debug,
" >> Allocating R_\n");
828 R_ = MVT::Clone(*tmp,blockSize_);
833 int newsd = blockSize_*numBlocks_;
834 theta_.resize(blockSize_*numBlocks_,NANVAL);
835 om_->print(
Debug,
" >> Allocating V_\n");
836 V_ = MVT::Clone(*tmp,newsd);
837 KK_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(newsd,newsd) );
839 om_->print(
Debug,
" >> done allocating.\n");
841 initialized_ =
false;
848 template <
class ScalarType,
class MV,
class OP>
850 typedef typename Teuchos::Array<Teuchos::RCP<const MV> >::iterator tarcpmv;
855 for (tarcpmv i=auxVecs_.begin(); i != auxVecs_.end(); ++i) {
856 numAuxVecs_ += MVT::GetNumberVecs(**i);
860 if (numAuxVecs_ > 0 && initialized_) {
861 initialized_ =
false;
864 if (om_->isVerbosity(
Debug ) ) {
867 om_->print(
Debug, accuracyCheck(chk,
": in setAuxVecs()") );
885 template <
class ScalarType,
class MV,
class OP>
891 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
892 Teuchos::TimeMonitor inittimer( *timerInit_ );
895 std::vector<int> bsind(blockSize_);
896 for (
int i=0; i<blockSize_; ++i) bsind[i] = i;
898 Teuchos::BLAS<int,ScalarType> blas;
922 Teuchos::RCP<MV> lclV;
923 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > lclKK;
925 if (newstate.
V != Teuchos::null && newstate.
KK != Teuchos::null) {
926 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*newstate.
V) != MVText::GetGlobalLength(*V_), std::invalid_argument,
927 "Anasazi::BlockDavidson::initialize(newstate): Vector length of V not correct." );
928 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
curDim < blockSize_, std::invalid_argument,
929 "Anasazi::BlockDavidson::initialize(newstate): Rank of new state must be at least blockSize().");
930 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
curDim > blockSize_*numBlocks_, std::invalid_argument,
931 "Anasazi::BlockDavidson::initialize(newstate): Rank of new state must be less than getMaxSubspaceDim().");
932 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.
V) < newstate.
curDim, std::invalid_argument,
933 "Anasazi::BlockDavidson::initialize(newstate): Multivector for basis in new state must be as large as specified state rank.");
935 curDim_ = newstate.
curDim;
937 curDim_ = (int)(curDim_ / blockSize_)*blockSize_;
939 TEUCHOS_TEST_FOR_EXCEPTION( curDim_ != newstate.
curDim, std::invalid_argument,
940 "Anasazi::BlockDavidson::initialize(newstate): Rank of new state must be a multiple of getBlockSize().");
943 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
KK->numRows() < curDim_ || newstate.
KK->numCols() < curDim_, std::invalid_argument,
944 "Anasazi::BlockDavidson::initialize(newstate): Projected matrix in new state must be as large as specified state rank.");
947 std::vector<int> nevind(curDim_);
948 for (
int i=0; i<curDim_; ++i) nevind[i] = i;
949 if (newstate.
V != V_) {
950 if (curDim_ < MVT::GetNumberVecs(*newstate.
V)) {
951 newstate.
V = MVT::CloneView(*newstate.
V,nevind);
953 MVT::SetBlock(*newstate.
V,nevind,*V_);
955 lclV = MVT::CloneViewNonConst(*V_,nevind);
958 lclKK = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,curDim_,curDim_) );
959 if (newstate.
KK != KK_) {
960 if (newstate.
KK->numRows() > curDim_ || newstate.
KK->numCols() > curDim_) {
961 newstate.
KK = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*newstate.
KK,curDim_,curDim_) );
963 lclKK->assign(*newstate.
KK);
967 for (
int j=0; j<curDim_-1; ++j) {
968 for (
int i=j+1; i<curDim_; ++i) {
969 (*lclKK)(i,j) = SCT::conjugate((*lclKK)(j,i));
976 Teuchos::RCP<const MV> ivec = problem_->getInitVec();
977 TEUCHOS_TEST_FOR_EXCEPTION(ivec == Teuchos::null,std::invalid_argument,
978 "Anasazi::BlockDavdison::initialize(newstate): Eigenproblem did not specify initial vectors to clone from.");
980 newstate.
X = Teuchos::null;
981 newstate.
MX = Teuchos::null;
982 newstate.
KX = Teuchos::null;
983 newstate.
R = Teuchos::null;
984 newstate.
H = Teuchos::null;
985 newstate.
T = Teuchos::null;
986 newstate.
KK = Teuchos::null;
987 newstate.
V = Teuchos::null;
990 curDim_ = MVT::GetNumberVecs(*ivec);
992 curDim_ = (int)(curDim_ / blockSize_)*blockSize_;
993 if (curDim_ > blockSize_*numBlocks_) {
996 curDim_ = blockSize_*numBlocks_;
998 bool userand =
false;
1003 curDim_ = blockSize_;
1013 std::vector<int> dimind(curDim_);
1014 for (
int i=0; i<curDim_; ++i) dimind[i] = i;
1015 lclV = MVT::CloneViewNonConst(*V_,dimind);
1018 MVT::MvRandom(*lclV);
1021 if (MVT::GetNumberVecs(*ivec) > curDim_) {
1022 ivec = MVT::CloneView(*ivec,dimind);
1025 MVT::SetBlock(*ivec,dimind,*lclV);
1027 Teuchos::RCP<MV> tmpVecs;
1028 if (curDim_*2 <= blockSize_*numBlocks_) {
1030 std::vector<int> block2(curDim_);
1031 for (
int i=0; i<curDim_; ++i) block2[i] = curDim_+i;
1032 tmpVecs = MVT::CloneViewNonConst(*V_,block2);
1036 tmpVecs = MVT::Clone(*V_,curDim_);
1041 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1042 Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1044 OPT::Apply(*MOp_,*lclV,*tmpVecs);
1045 count_ApplyM_ += curDim_;
1049 if (auxVecs_.size() > 0) {
1050 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1051 Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1054 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > dummyC;
1055 int rank = orthman_->projectAndNormalizeMat(*lclV,auxVecs_,dummyC,Teuchos::null,tmpVecs);
1057 "Anasazi::BlockDavidson::initialize(): Couldn't generate initial basis of full rank.");
1060 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1061 Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1064 int rank = orthman_->normalizeMat(*lclV,Teuchos::null,tmpVecs);
1066 "Anasazi::BlockDavidson::initialize(): Couldn't generate initial basis of full rank.");
1071 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1072 Teuchos::TimeMonitor lcltimer( *timerOp_ );
1074 OPT::Apply(*Op_,*lclV,*tmpVecs);
1075 count_ApplyOp_ += curDim_;
1079 lclKK = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,curDim_,curDim_) );
1080 MVT::MvTransMv(ONE,*lclV,*tmpVecs,*lclKK);
1083 tmpVecs = Teuchos::null;
1087 if (newstate.
X != Teuchos::null && newstate.
T != Teuchos::null) {
1088 TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.
X) != blockSize_ || MVText::GetGlobalLength(*newstate.
X) != MVText::GetGlobalLength(*X_),
1089 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): Size of X must be consistent with block size and length of V.");
1090 TEUCHOS_TEST_FOR_EXCEPTION((
signed int)(newstate.
T->size()) != curDim_,
1091 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): Size of T must be consistent with dimension of V.");
1093 if (newstate.
X != X_) {
1094 MVT::SetBlock(*newstate.
X,bsind,*X_);
1097 std::copy(newstate.
T->begin(),newstate.
T->end(),theta_.begin());
1101 Teuchos::SerialDenseMatrix<int,ScalarType> S(curDim_,curDim_);
1103 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1104 Teuchos::TimeMonitor lcltimer( *timerDS_ );
1107 Utils::directSolver(curDim_, *lclKK, Teuchos::null, S, theta_, rank, 10);
1110 "Anasazi::BlockDavidson::initialize(newstate): Not enough Ritz vectors to initialize algorithm.");
1114 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1115 Teuchos::TimeMonitor lcltimer( *timerSortEval_ );
1118 std::vector<int> order(curDim_);
1121 sm_->sort(theta_, Teuchos::rcpFromRef(order), curDim_);
1124 Utils::permuteVectors(order,S);
1128 Teuchos::SerialDenseMatrix<int,ScalarType> S1(Teuchos::View,S,curDim_,blockSize_);
1130 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1131 Teuchos::TimeMonitor lcltimer( *timerLocal_ );
1135 MVT::MvTimesMatAddMv( ONE, *lclV, S1, ZERO, *X_ );
1138 newstate.
KX = Teuchos::null;
1139 newstate.
MX = Teuchos::null;
1143 lclV = Teuchos::null;
1144 lclKK = Teuchos::null;
1147 if ( newstate.
KX != Teuchos::null ) {
1148 TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.
KX) != blockSize_,
1149 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): vector length of newstate.KX not correct." );
1150 TEUCHOS_TEST_FOR_EXCEPTION(MVText::GetGlobalLength(*newstate.
KX) != MVText::GetGlobalLength(*X_),
1151 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): newstate.KX must have at least block size vectors." );
1152 if (newstate.
KX != KX_) {
1153 MVT::SetBlock(*newstate.
KX,bsind,*KX_);
1159 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1160 Teuchos::TimeMonitor lcltimer( *timerOp_ );
1162 OPT::Apply(*Op_,*X_,*KX_);
1163 count_ApplyOp_ += blockSize_;
1166 newstate.
R = Teuchos::null;
1171 if ( newstate.
MX != Teuchos::null ) {
1172 TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.
MX) != blockSize_,
1173 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): vector length of newstate.MX not correct." );
1174 TEUCHOS_TEST_FOR_EXCEPTION(MVText::GetGlobalLength(*newstate.
MX) != MVText::GetGlobalLength(*X_),
1175 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): newstate.MX must have at least block size vectors." );
1176 if (newstate.
MX != MX_) {
1177 MVT::SetBlock(*newstate.
MX,bsind,*MX_);
1183 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1184 Teuchos::TimeMonitor lcltimer( *timerOp_ );
1186 OPT::Apply(*MOp_,*X_,*MX_);
1187 count_ApplyOp_ += blockSize_;
1190 newstate.
R = Teuchos::null;
1195 TEUCHOS_TEST_FOR_EXCEPTION(MX_ != X_, std::logic_error,
"Anasazi::BlockDavidson::initialize(): solver invariant not satisfied (MX==X).");
1199 if (newstate.
R != Teuchos::null) {
1200 TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.
R) != blockSize_,
1201 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): vector length of newstate.R not correct." );
1202 TEUCHOS_TEST_FOR_EXCEPTION(MVText::GetGlobalLength(*newstate.
R) != MVText::GetGlobalLength(*X_),
1203 std::invalid_argument,
"Anasazi::BlockDavidson::initialize(newstate): newstate.R must have at least block size vectors." );
1204 if (newstate.
R != R_) {
1205 MVT::SetBlock(*newstate.
R,bsind,*R_);
1209 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1210 Teuchos::TimeMonitor lcltimer( *timerCompRes_ );
1214 MVT::MvAddMv(ZERO,*KX_,ONE,*KX_,*R_);
1215 Teuchos::SerialDenseMatrix<int,ScalarType> T(blockSize_,blockSize_);
1217 for (
int i=0; i<blockSize_; ++i) T(i,i) = theta_[i];
1218 MVT::MvTimesMatAddMv(-ONE,*MX_,T,ONE,*R_);
1223 Rnorms_current_ =
false;
1224 R2norms_current_ =
false;
1227 initialized_ =
true;
1229 if (om_->isVerbosity(
Debug ) ) {
1239 om_->print(
Debug, accuracyCheck(chk,
": after initialize()") );
1243 if (om_->isVerbosity(
Debug)) {
1244 currentStatus( om_->stream(
Debug) );
1254 template <
class ScalarType,
class MV,
class OP>
1264 template <
class ScalarType,
class MV,
class OP>
1269 if (initialized_ ==
false) {
1275 const int searchDim = blockSize_*numBlocks_;
1277 Teuchos::BLAS<int,ScalarType> blas;
1282 Teuchos::SerialDenseMatrix<int,ScalarType> S( searchDim, searchDim );
1288 while (tester_->checkStatus(
this) !=
Passed && curDim_ < searchDim) {
1291 if (om_->isVerbosity(
Debug)) {
1292 currentStatus( om_->stream(
Debug) );
1301 std::vector<int> curind(blockSize_);
1302 for (
int i=0; i<blockSize_; ++i) curind[i] = curDim_ + i;
1303 H_ = MVT::CloneViewNonConst(*V_,curind);
1307 if (Prec_ != Teuchos::null) {
1308 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1309 Teuchos::TimeMonitor lcltimer( *timerPrec_ );
1311 OPT::Apply( *Prec_, *R_, *H_ );
1312 count_ApplyPrec_ += blockSize_;
1315 std::vector<int> bsind(blockSize_);
1316 for (
int i=0; i<blockSize_; ++i) { bsind[i] = i; }
1317 MVT::SetBlock(*R_,bsind,*H_);
1324 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1325 Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1327 OPT::Apply( *MOp_, *H_, *MH_);
1328 count_ApplyM_ += blockSize_;
1336 std::vector<int> prevind(curDim_);
1337 for (
int i=0; i<curDim_; ++i) prevind[i] = i;
1338 Teuchos::RCP<const MV> Vprev = MVT::CloneView(*V_,prevind);
1342 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1343 Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1346 Teuchos::Array<Teuchos::RCP<const MV> > against = auxVecs_;
1347 against.push_back(Vprev);
1348 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > dummyC;
1349 int rank = orthman_->projectAndNormalizeMat(*H_,against,
1353 "Anasazi::BlockDavidson::iterate(): unable to compute orthonormal basis for H.");
1360 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1361 Teuchos::TimeMonitor lcltimer( *timerOp_ );
1363 OPT::Apply( *Op_, *H_, *KH_);
1364 count_ApplyOp_ += blockSize_;
1367 if (om_->isVerbosity(
Debug ) ) {
1372 om_->print(
Debug, accuracyCheck(chk,
": after ortho H") );
1379 om_->print(
OrthoDetails, accuracyCheck(chk,
": after ortho H") );
1384 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > nextKK;
1386 nextKK = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,curDim_,blockSize_,0,curDim_) );
1387 MVT::MvTransMv(ONE,*Vprev,*KH_,*nextKK);
1389 nextKK = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,blockSize_,blockSize_,curDim_,curDim_) );
1390 MVT::MvTransMv(ONE,*H_,*KH_,*nextKK);
1393 nextKK = Teuchos::null;
1394 for (
int i=curDim_; i<curDim_+blockSize_; ++i) {
1395 for (
int j=0; j<i; ++j) {
1396 (*KK_)(i,j) = SCT::conjugate((*KK_)(j,i));
1401 curDim_ += blockSize_;
1402 H_ = KH_ = MH_ = Teuchos::null;
1403 Vprev = Teuchos::null;
1405 if (om_->isVerbosity(
Debug ) ) {
1408 om_->print(
Debug, accuracyCheck(chk,
": after expanding KK") );
1412 curind.resize(curDim_);
1413 for (
int i=0; i<curDim_; ++i) curind[i] = i;
1414 Teuchos::RCP<const MV> curV = MVT::CloneView(*V_,curind);
1418 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1419 Teuchos::TimeMonitor lcltimer(*timerDS_);
1421 int nevlocal = curDim_;
1422 int info = Utils::directSolver(curDim_,*KK_,Teuchos::null,S,theta_,nevlocal,10);
1423 TEUCHOS_TEST_FOR_EXCEPTION(info != 0,std::logic_error,
"Anasazi::BlockDavidson::iterate(): direct solve returned error code.");
1425 TEUCHOS_TEST_FOR_EXCEPTION(nevlocal != curDim_,std::logic_error,
"Anasazi::BlockDavidson::iterate(): direct solve did not compute all eigenvectors.");
1430 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1431 Teuchos::TimeMonitor lcltimer( *timerSortEval_ );
1434 std::vector<int> order(curDim_);
1437 sm_->sort(theta_, Teuchos::rcp(&order,
false), curDim_);
1440 Teuchos::SerialDenseMatrix<int,ScalarType> curS(Teuchos::View,S,curDim_,curDim_);
1441 Utils::permuteVectors(order,curS);
1445 Teuchos::SerialDenseMatrix<int,ScalarType> S1( Teuchos::View, S, curDim_, blockSize_ );
1449 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1450 Teuchos::TimeMonitor lcltimer( *timerLocal_ );
1452 MVT::MvTimesMatAddMv(ONE,*curV,S1,ZERO,*X_);
1457 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1458 Teuchos::TimeMonitor lcltimer( *timerOp_ );
1460 OPT::Apply( *Op_, *X_, *KX_);
1461 count_ApplyOp_ += blockSize_;
1465 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1466 Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1468 OPT::Apply(*MOp_,*X_,*MX_);
1469 count_ApplyM_ += blockSize_;
1478 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1479 Teuchos::TimeMonitor lcltimer( *timerCompRes_ );
1482 MVT::MvAddMv( ONE, *KX_, ZERO, *KX_, *R_ );
1483 Teuchos::SerialDenseMatrix<int,ScalarType> T( blockSize_, blockSize_ );
1484 for (
int i = 0; i < blockSize_; ++i) {
1487 MVT::MvTimesMatAddMv( -ONE, *MX_, T, ONE, *R_ );
1491 Rnorms_current_ =
false;
1492 R2norms_current_ =
false;
1496 if (om_->isVerbosity(
Debug ) ) {
1504 om_->print(
Debug, accuracyCheck(chk,
": after local update") );
1512 om_->print(
OrthoDetails, accuracyCheck(chk,
": after local update") );
1522 template <
class ScalarType,
class MV,
class OP>
1523 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
1525 if (Rnorms_current_ ==
false) {
1527 orthman_->norm(*R_,Rnorms_);
1528 Rnorms_current_ =
true;
1536 template <
class ScalarType,
class MV,
class OP>
1537 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
1539 if (R2norms_current_ ==
false) {
1541 MVT::MvNorm(*R_,R2norms_);
1542 R2norms_current_ =
true;
1549 template <
class ScalarType,
class MV,
class OP>
1551 TEUCHOS_TEST_FOR_EXCEPTION(test == Teuchos::null,std::invalid_argument,
1552 "Anasazi::BlockDavidson::setStatusTest() was passed a null StatusTest.");
1558 template <
class ScalarType,
class MV,
class OP>
1590 template <
class ScalarType,
class MV,
class OP>
1595 std::stringstream os;
1597 os.setf(std::ios::scientific, std::ios::floatfield);
1599 os <<
" Debugging checks: iteration " << iter_ << where << endl;
1602 std::vector<int> lclind(curDim_);
1603 for (
int i=0; i<curDim_; ++i) lclind[i] = i;
1604 Teuchos::RCP<const MV> lclV;
1606 lclV = MVT::CloneView(*V_,lclind);
1608 if (chk.checkV && initialized_) {
1609 MagnitudeType err = orthman_->orthonormError(*lclV);
1610 os <<
" >> Error in V^H M V == I : " << err << endl;
1611 for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1612 err = orthman_->orthogError(*lclV,*auxVecs_[i]);
1613 os <<
" >> Error in V^H M Q[" << i <<
"] == 0 : " << err << endl;
1615 Teuchos::SerialDenseMatrix<int,ScalarType> curKK(curDim_,curDim_);
1616 Teuchos::RCP<MV> lclKV = MVT::Clone(*V_,curDim_);
1617 OPT::Apply(*Op_,*lclV,*lclKV);
1618 MVT::MvTransMv(ONE,*lclV,*lclKV,curKK);
1619 Teuchos::SerialDenseMatrix<int,ScalarType> subKK(Teuchos::View,*KK_,curDim_,curDim_);
1622 for (
int j=0; j<curDim_; ++j) {
1623 for (
int i=j+1; i<curDim_; ++i) {
1624 curKK(i,j) = curKK(j,i);
1627 os <<
" >> Error in V^H K V == KK : " << curKK.normFrobenius() << endl;
1631 if (chk.checkX && initialized_) {
1632 MagnitudeType err = orthman_->orthonormError(*X_);
1633 os <<
" >> Error in X^H M X == I : " << err << endl;
1634 for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1635 err = orthman_->orthogError(*X_,*auxVecs_[i]);
1636 os <<
" >> Error in X^H M Q[" << i <<
"] == 0 : " << err << endl;
1639 if (chk.checkMX && hasM_ && initialized_) {
1640 MagnitudeType err = Utils::errorEquality(*X_, *MX_, MOp_);
1641 os <<
" >> Error in MX == M*X : " << err << endl;
1643 if (chk.checkKX && initialized_) {
1644 MagnitudeType err = Utils::errorEquality(*X_, *KX_, Op_);
1645 os <<
" >> Error in KX == K*X : " << err << endl;
1649 if (chk.checkH && initialized_) {
1650 MagnitudeType err = orthman_->orthonormError(*H_);
1651 os <<
" >> Error in H^H M H == I : " << err << endl;
1652 err = orthman_->orthogError(*H_,*lclV);
1653 os <<
" >> Error in H^H M V == 0 : " << err << endl;
1654 err = orthman_->orthogError(*H_,*X_);
1655 os <<
" >> Error in H^H M X == 0 : " << err << endl;
1656 for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1657 err = orthman_->orthogError(*H_,*auxVecs_[i]);
1658 os <<
" >> Error in H^H M Q[" << i <<
"] == 0 : " << err << endl;
1661 if (chk.checkKH && initialized_) {
1662 MagnitudeType err = Utils::errorEquality(*H_, *KH_, Op_);
1663 os <<
" >> Error in KH == K*H : " << err << endl;
1665 if (chk.checkMH && hasM_ && initialized_) {
1666 MagnitudeType err = Utils::errorEquality(*H_, *MH_, MOp_);
1667 os <<
" >> Error in MH == M*H : " << err << endl;
1671 if (chk.checkR && initialized_) {
1672 Teuchos::SerialDenseMatrix<int,ScalarType> xTx(blockSize_,blockSize_);
1673 MVT::MvTransMv(ONE,*X_,*R_,xTx);
1674 MagnitudeType err = xTx.normFrobenius();
1675 os <<
" >> Error in X^H R == 0 : " << err << endl;
1679 if (chk.checkKK && initialized_) {
1680 Teuchos::SerialDenseMatrix<int,ScalarType> SDMerr(curDim_,curDim_), lclKK(Teuchos::View,*KK_,curDim_,curDim_);
1681 for (
int j=0; j<curDim_; ++j) {
1682 for (
int i=0; i<curDim_; ++i) {
1683 SDMerr(i,j) = lclKK(i,j) - SCT::conjugate(lclKK(j,i));
1686 os <<
" >> Error in KK - KK^H == 0 : " << SDMerr.normFrobenius() << endl;
1691 for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1692 MagnitudeType err = orthman_->orthonormError(*auxVecs_[i]);
1693 os <<
" >> Error in Q[" << i <<
"]^H M Q[" << i <<
"] == I : " << err << endl;
1694 for (Array_size_type j=i+1; j<auxVecs_.size(); ++j) {
1695 err = orthman_->orthogError(*auxVecs_[i],*auxVecs_[j]);
1696 os <<
" >> Error in Q[" << i <<
"]^H M Q[" << j <<
"] == 0 : " << err << endl;
1709 template <
class ScalarType,
class MV,
class OP>
1715 os.setf(std::ios::scientific, std::ios::floatfield);
1718 os <<
"================================================================================" << endl;
1720 os <<
" BlockDavidson Solver Status" << endl;
1722 os <<
"The solver is "<<(initialized_ ?
"initialized." :
"not initialized.") << endl;
1723 os <<
"The number of iterations performed is " <<iter_<<endl;
1724 os <<
"The block size is " << blockSize_<<endl;
1725 os <<
"The number of blocks is " << numBlocks_<<endl;
1726 os <<
"The current basis size is " << curDim_<<endl;
1727 os <<
"The number of auxiliary vectors is "<< numAuxVecs_ << endl;
1728 os <<
"The number of operations Op*x is "<<count_ApplyOp_<<endl;
1729 os <<
"The number of operations M*x is "<<count_ApplyM_<<endl;
1730 os <<
"The number of operations Prec*x is "<<count_ApplyPrec_<<endl;
1732 os.setf(std::ios_base::right, std::ios_base::adjustfield);
1736 os <<
"CURRENT EIGENVALUE ESTIMATES "<<endl;
1737 os << std::setw(20) <<
"Eigenvalue"
1738 << std::setw(20) <<
"Residual(M)"
1739 << std::setw(20) <<
"Residual(2)"
1741 os <<
"--------------------------------------------------------------------------------"<<endl;
1742 for (
int i=0; i<blockSize_; ++i) {
1743 os << std::setw(20) << theta_[i];
1744 if (Rnorms_current_) os << std::setw(20) << Rnorms_[i];
1745 else os << std::setw(20) <<
"not current";
1746 if (R2norms_current_) os << std::setw(20) << R2norms_[i];
1747 else os << std::setw(20) <<
"not current";
1751 os <<
"================================================================================" << endl;
virtual ~BlockDavidson()
Anasazi::BlockDavidson destructor.
bool isInitialized() const
Indicates whether the solver has been initialized or not.
Teuchos::RCP< const MV > H
The current preconditioned residual vectors.
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
int getNumIters() const
Get the current iteration count.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRitzRes2Norms()
Get the 2-norms of the residuals.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
void setSize(int blockSize, int numBlocks)
Set the blocksize and number of blocks to be used by the iterative solver in solving this eigenproble...
This class defines the interface required by an eigensolver and status test class to compute solution...
Teuchos::RCP< const MV > R
The current residual vectors.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > KK
The current projected K matrix.
Declaration of basic traits for the multivector type.
BlockDavidsonOrthoFailure is thrown when the orthogonalization manager is unable to orthogonalize the...
void currentStatus(std::ostream &os)
This method requests that the solver print out its current status to the given output stream...
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current eigenvectors and eigenvalues...
void setStatusTest(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Set a new StatusTest for the solver.
Virtual base class which defines basic traits for the operator type.
Teuchos::RCP< const MV > KX
The image of the current eigenvectors under K.
BlockDavidsonInitFailure is thrown when the BlockDavidson solver is unable to generate an initial ite...
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > getStatusTest() const
Get the current StatusTest used by the solver.
An exception class parent to all Anasazi exceptions.
std::vector< Value< ScalarType > > getRitzValues()
Get the Ritz values for the previous iteration.
This class implements a Block Davidson iteration, a preconditioned iteration for solving linear Hermi...
Teuchos::Array< Teuchos::RCP< const MV > > getAuxVecs() const
Get the auxiliary vectors for the solver.
Pure virtual base class which describes the basic interface to the iterative eigensolver.
Anasazi's templated, static class providing utilities for the solvers.
Anasazi's templated virtual class for providing routines for orthogonalization and orthonormalization...
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace. For BlockDavidson, this always returns n...
Output managers remove the need for the eigensolver to know any information about the required output...
Teuchos::RCP< const MV > V
The basis for the Krylov space.
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
int getBlockSize() const
Get the blocksize used by the iterative solver.
void setAuxVecs(const Teuchos::Array< Teuchos::RCP< const MV > > &auxvecs)
Set the auxiliary vectors for the solver.
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the eigenvalue problem.
int curDim
The current dimension of the solver.
void initialize()
Initialize the solver with the initial vectors from the eigenproblem or random data.
Structure to contain pointers to BlockDavidson state variables.
Teuchos::RCP< const MV > X
The current eigenvectors.
Teuchos::RCP< const MV > MX
The image of the current eigenvectors under M, or Teuchos::null if M was not specified.
Types and exceptions used within Anasazi solvers and interfaces.
std::vector< int > getRitzIndex()
Get the index used for extracting individual Ritz vectors from getRitzVectors().
void iterate()
This method performs BlockDavidson iterations until the status test indicates the need to stop or an ...
BlockDavidsonState< ScalarType, MV > getState() const
Get access to the current state of the eigensolver.
Anasazi's templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
A pure virtual class for defining the status tests for the Anasazi iterative solvers.
BlockDavidson(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< SortManager< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > &sorter, 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)
BlockDavidson constructor with eigenproblem, solver utilities, and parameter list of solver options...
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRes2Norms()
Get the current residual 2-norms, computing the norms if they are not up-to-date with the current res...
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
void setBlockSize(int blockSize)
Set the blocksize.
Teuchos::RCP< const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > T
The current Ritz values. This vector is a copy of the internal data.
void resetNumIters()
Reset the iteration count.
Class which provides internal utilities for the Anasazi solvers.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getResNorms()
Get the current residual norms, computing the norms if they are not up-to-date with the current resid...
Teuchos::RCP< const MV > getRitzVectors()
Get access to the current Ritz vectors.