33 #ifndef ANASAZI_BLOCK_KRYLOV_SCHUR_HPP
34 #define ANASAZI_BLOCK_KRYLOV_SCHUR_HPP
41 #include "Teuchos_ScalarTraits.hpp"
42 #include "AnasaziHelperTraits.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"
75 template <
class ScalarType,
class MulVec>
83 Teuchos::RCP<const MulVec>
V;
89 Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> >
H;
91 Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> >
S;
93 Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> >
Q;
95 H(Teuchos::null),
S(Teuchos::null),
133 template <
class ScalarType,
class MV,
class OP>
151 const Teuchos::RCP<
SortManager<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > &sorter,
155 Teuchos::ParameterList ¶ms
273 std::vector<Value<ScalarType> > ret = ritzValues_;
274 ret.resize(ritzIndex_.size());
289 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
getResNorms() {
290 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> ret(0);
298 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
getRes2Norms() {
299 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> ret(0);
307 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
getRitzRes2Norms() {
308 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> ret = ritzResiduals_;
309 ret.resize(ritzIndex_.size());
322 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
getStatusTest()
const;
333 void setSize(
int blockSize,
int numBlocks);
359 if (!initialized_)
return 0;
364 int getMaxSubspaceDim()
const {
return (problem_->isHermitian()?blockSize_*numBlocks_:blockSize_*numBlocks_+1); }
379 void setAuxVecs(
const Teuchos::Array<Teuchos::RCP<const MV> > &auxvecs);
382 Teuchos::Array<Teuchos::RCP<const MV> >
getAuxVecs()
const {
return auxVecs_;}
429 typedef Teuchos::ScalarTraits<ScalarType> SCT;
430 typedef typename SCT::magnitudeType MagnitudeType;
431 typedef typename std::vector<ScalarType>::iterator STiter;
432 typedef typename std::vector<MagnitudeType>::iterator MTiter;
433 const MagnitudeType MT_ONE;
434 const MagnitudeType MT_ZERO;
435 const MagnitudeType NANVAL;
436 const ScalarType ST_ONE;
437 const ScalarType ST_ZERO;
445 CheckList() : checkV(false), checkArn(false), checkAux(false) {};
450 std::string accuracyCheck(
const CheckList &chk,
const std::string &where)
const;
451 void sortSchurForm( Teuchos::SerialDenseMatrix<int,ScalarType>& H,
452 Teuchos::SerialDenseMatrix<int,ScalarType>& Q,
453 std::vector<int>& order );
457 const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > problem_;
458 const Teuchos::RCP<SortManager<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > sm_;
459 const Teuchos::RCP<OutputManager<ScalarType> > om_;
460 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > tester_;
461 const Teuchos::RCP<OrthoManager<ScalarType,MV> > orthman_;
465 Teuchos::RCP<const OP> Op_;
469 Teuchos::RCP<Teuchos::Time> timerOp_, timerSortRitzVal_,
470 timerCompSF_, timerSortSF_,
471 timerCompRitzVec_, timerOrtho_;
505 Teuchos::RCP<MV> ritzVectors_, V_;
511 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > H_;
516 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > schurH_;
517 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > Q_;
520 Teuchos::Array<Teuchos::RCP<const MV> > auxVecs_;
527 bool ritzVecsCurrent_, ritzValsCurrent_, schurCurrent_;
530 std::vector<Value<ScalarType> > ritzValues_;
531 std::vector<MagnitudeType> ritzResiduals_;
534 std::vector<int> ritzIndex_;
535 std::vector<int> ritzOrder_;
544 template <
class ScalarType,
class MV,
class OP>
547 const Teuchos::RCP<
SortManager<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > &sorter,
551 Teuchos::ParameterList ¶ms
553 MT_ONE(Teuchos::ScalarTraits<MagnitudeType>::one()),
554 MT_ZERO(Teuchos::ScalarTraits<MagnitudeType>::zero()),
555 NANVAL(Teuchos::ScalarTraits<MagnitudeType>::nan()),
556 ST_ONE(Teuchos::ScalarTraits<ScalarType>::one()),
557 ST_ZERO(Teuchos::ScalarTraits<ScalarType>::zero()),
565 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
566 timerOp_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockKrylovSchur::Operation Op*x")),
567 timerSortRitzVal_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockKrylovSchur::Sorting Ritz values")),
568 timerCompSF_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockKrylovSchur::Computing Schur form")),
569 timerSortSF_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockKrylovSchur::Sorting Schur form")),
570 timerCompRitzVec_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockKrylovSchur::Computing Ritz vectors")),
571 timerOrtho_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: BlockKrylovSchur::Orthogonalization")),
581 auxVecs_( Teuchos::Array<Teuchos::RCP<const MV> >(0) ),
584 ritzVecsCurrent_(false),
585 ritzValsCurrent_(false),
586 schurCurrent_(false),
589 TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,std::invalid_argument,
590 "Anasazi::BlockKrylovSchur::constructor: user specified null problem pointer.");
591 TEUCHOS_TEST_FOR_EXCEPTION(sm_ == Teuchos::null,std::invalid_argument,
592 "Anasazi::BlockKrylovSchur::constructor: user passed null sort manager pointer.");
593 TEUCHOS_TEST_FOR_EXCEPTION(om_ == Teuchos::null,std::invalid_argument,
594 "Anasazi::BlockKrylovSchur::constructor: user passed null output manager pointer.");
595 TEUCHOS_TEST_FOR_EXCEPTION(tester_ == Teuchos::null,std::invalid_argument,
596 "Anasazi::BlockKrylovSchur::constructor: user passed null status test pointer.");
597 TEUCHOS_TEST_FOR_EXCEPTION(orthman_ == Teuchos::null,std::invalid_argument,
598 "Anasazi::BlockKrylovSchur::constructor: user passed null orthogonalization manager pointer.");
599 TEUCHOS_TEST_FOR_EXCEPTION(problem_->isProblemSet() ==
false, std::invalid_argument,
600 "Anasazi::BlockKrylovSchur::constructor: user specified problem is not set.");
601 TEUCHOS_TEST_FOR_EXCEPTION(sorter == Teuchos::null,std::invalid_argument,
602 "Anasazi::BlockKrylovSchur::constructor: user specified null sort manager pointer.");
603 TEUCHOS_TEST_FOR_EXCEPTION(printer == Teuchos::null,std::invalid_argument,
604 "Anasazi::BlockKrylovSchur::constructor: user specified null output manager pointer.");
605 TEUCHOS_TEST_FOR_EXCEPTION(tester == Teuchos::null,std::invalid_argument,
606 "Anasazi::BlockKrylovSchur::constructor: user specified null status test pointer.");
607 TEUCHOS_TEST_FOR_EXCEPTION(ortho == Teuchos::null,std::invalid_argument,
608 "Anasazi::BlockKrylovSchur::constructor: user specified null ortho manager pointer.");
611 Op_ = problem_->getOperator();
614 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Step Size"), std::invalid_argument,
615 "Anasazi::BlockKrylovSchur::constructor: mandatory parameter 'Step Size' is not specified.");
616 int ss = params.get(
"Step Size",numBlocks_);
620 int bs = params.get(
"Block Size", 1);
621 int nb = params.get(
"Num Blocks", 3*problem_->getNEV());
626 int numRitzVecs = params.get(
"Number of Ritz Vectors", 0);
630 numRitzPrint_ = params.get(
"Print Number of Ritz Values", bs);
637 template <
class ScalarType,
class MV,
class OP>
640 setSize(blockSize,numBlocks_);
646 template <
class ScalarType,
class MV,
class OP>
649 TEUCHOS_TEST_FOR_EXCEPTION(stepSize <= 0, std::invalid_argument,
"Anasazi::BlockKrylovSchur::setStepSize(): new step size must be positive and non-zero.");
650 stepSize_ = stepSize;
655 template <
class ScalarType,
class MV,
class OP>
661 TEUCHOS_TEST_FOR_EXCEPTION(numRitzVecs < 0, std::invalid_argument,
"Anasazi::BlockKrylovSchur::setNumRitzVectors(): number of Ritz vectors to compute must be positive.");
664 if (numRitzVecs != numRitzVecs_) {
666 ritzVectors_ = Teuchos::null;
667 ritzVectors_ = MVT::Clone(*V_, numRitzVecs);
669 ritzVectors_ = Teuchos::null;
671 numRitzVecs_ = numRitzVecs;
672 ritzVecsCurrent_ =
false;
678 template <
class ScalarType,
class MV,
class OP>
684 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0 || blockSize <= 0, std::invalid_argument,
"Anasazi::BlockKrylovSchur::setSize was passed a non-positive argument.");
685 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks < 3, std::invalid_argument,
"Anasazi::BlockKrylovSchur::setSize(): numBlocks must be at least three.");
686 if (blockSize == blockSize_ && numBlocks == numBlocks_) {
691 blockSize_ = blockSize;
692 numBlocks_ = numBlocks;
694 Teuchos::RCP<const MV> tmp;
700 if (problem_->getInitVec() != Teuchos::null) {
701 tmp = problem_->getInitVec();
705 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
706 "Anasazi::BlockKrylovSchur::setSize(): eigenproblem did not specify initial vectors to clone from.");
714 if (problem_->isHermitian()) {
715 newsd = blockSize_*numBlocks_;
717 newsd = blockSize_*numBlocks_+1;
720 TEUCHOS_TEST_FOR_EXCEPTION(static_cast<ptrdiff_t>(newsd) > MVText::GetGlobalLength(*tmp),std::invalid_argument,
721 "Anasazi::BlockKrylovSchur::setSize(): maximum basis size is larger than problem dimension.");
723 ritzValues_.resize(newsd);
724 ritzResiduals_.resize(newsd,MT_ONE);
725 ritzOrder_.resize(newsd);
727 V_ = MVT::Clone(*tmp,newsd+blockSize_);
728 H_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(newsd+blockSize_,newsd) );
729 Q_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(newsd,newsd) );
731 initialized_ =
false;
738 template <
class ScalarType,
class MV,
class OP>
740 typedef typename Teuchos::Array<Teuchos::RCP<const MV> >::iterator tarcpmv;
745 if (om_->isVerbosity(
Debug ) ) {
749 om_->print(
Debug, accuracyCheck(chk,
": in setAuxVecs()") );
753 for (tarcpmv i=auxVecs_.begin(); i != auxVecs_.end(); i++) {
754 numAuxVecs_ += MVT::GetNumberVecs(**i);
758 if (numAuxVecs_ > 0 && initialized_) {
759 initialized_ =
false;
772 template <
class ScalarType,
class MV,
class OP>
777 std::vector<int> bsind(blockSize_);
778 for (
int i=0; i<blockSize_; i++) bsind[i] = i;
786 std::string errstr(
"Anasazi::BlockKrylovSchur::initialize(): specified multivectors must have a consistent length and width.");
790 if (newstate.
V != Teuchos::null && newstate.
H != Teuchos::null) {
794 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*newstate.
V) != MVText::GetGlobalLength(*V_),
795 std::invalid_argument, errstr );
796 if (newstate.
V != V_) {
797 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.
V) < blockSize_,
798 std::invalid_argument, errstr );
799 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.
V) > getMaxSubspaceDim(),
800 std::invalid_argument, errstr );
802 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
curDim > getMaxSubspaceDim(),
803 std::invalid_argument, errstr );
805 curDim_ = newstate.
curDim;
806 int lclDim = MVT::GetNumberVecs(*newstate.
V);
809 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
H->numRows() < curDim_ || newstate.
H->numCols() < curDim_, std::invalid_argument, errstr);
811 if (curDim_ == 0 && lclDim > blockSize_) {
812 om_->stream(
Warnings) <<
"Anasazi::BlockKrylovSchur::initialize(): the solver was initialized with a kernel of " << lclDim << std::endl
813 <<
"The block size however is only " << blockSize_ << std::endl
814 <<
"The last " << lclDim - blockSize_ <<
" vectors of the kernel will be overwritten on the first call to iterate()." << std::endl;
819 if (newstate.
V != V_) {
820 std::vector<int> nevind(lclDim);
821 for (
int i=0; i<lclDim; i++) nevind[i] = i;
822 MVT::SetBlock(*newstate.
V,nevind,*V_);
826 if (newstate.
H != H_) {
827 H_->putScalar( ST_ZERO );
828 Teuchos::SerialDenseMatrix<int,ScalarType> newH(Teuchos::View,*newstate.
H,curDim_+blockSize_,curDim_);
829 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > lclH;
830 lclH = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*H_,curDim_+blockSize_,curDim_) );
834 lclH = Teuchos::null;
841 Teuchos::RCP<const MV> ivec = problem_->getInitVec();
842 TEUCHOS_TEST_FOR_EXCEPTION(ivec == Teuchos::null,std::invalid_argument,
843 "Anasazi::BlockKrylovSchur::initialize(): eigenproblem did not specify initial vectors to clone from.");
845 int lclDim = MVT::GetNumberVecs(*ivec);
846 bool userand =
false;
847 if (lclDim < blockSize_) {
855 std::vector<int> dimind2(lclDim);
856 for (
int i=0; i<lclDim; i++) { dimind2[i] = i; }
859 Teuchos::RCP<MV> newV1 = MVT::CloneViewNonConst(*V_,dimind2);
862 MVT::SetBlock(*ivec,dimind2,*newV1);
865 dimind2.resize(blockSize_-lclDim);
866 for (
int i=0; i<blockSize_-lclDim; i++) { dimind2[i] = lclDim + i; }
869 Teuchos::RCP<MV> newV2 = MVT::CloneViewNonConst(*V_,dimind2);
870 MVT::MvRandom(*newV2);
874 Teuchos::RCP<MV> newV1 = MVT::CloneViewNonConst(*V_,bsind);
877 Teuchos::RCP<const MV> ivecV = MVT::CloneView(*ivec,bsind);
880 MVT::SetBlock(*ivecV,bsind,*newV1);
884 Teuchos::RCP<MV> newV = MVT::CloneViewNonConst(*V_,bsind);
887 if (auxVecs_.size() > 0) {
888 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
889 Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
892 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > dummy;
893 int rank = orthman_->projectAndNormalize(*newV,auxVecs_);
895 "Anasazi::BlockKrylovSchur::initialize(): couldn't generate initial basis of full rank." );
898 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
899 Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
902 int rank = orthman_->normalize(*newV);
904 "Anasazi::BlockKrylovSchur::initialize(): couldn't generate initial basis of full rank." );
911 newV = Teuchos::null;
915 ritzVecsCurrent_ =
false;
916 ritzValsCurrent_ =
false;
917 schurCurrent_ =
false;
922 if (om_->isVerbosity(
Debug ) ) {
928 om_->print(
Debug, accuracyCheck(chk,
": after initialize()") );
932 if (om_->isVerbosity(
Debug)) {
933 currentStatus( om_->stream(
Debug) );
943 template <
class ScalarType,
class MV,
class OP>
953 template <
class ScalarType,
class MV,
class OP>
959 if (initialized_ ==
false) {
965 int searchDim = blockSize_*numBlocks_;
966 if (problem_->isHermitian() ==
false) {
975 while (tester_->checkStatus(
this) !=
Passed && curDim_+blockSize_ <= searchDim) {
980 int lclDim = curDim_ + blockSize_;
983 std::vector<int> curind(blockSize_);
984 for (
int i=0; i<blockSize_; i++) { curind[i] = lclDim + i; }
985 Teuchos::RCP<MV> Vnext = MVT::CloneViewNonConst(*V_,curind);
989 for (
int i=0; i<blockSize_; i++) { curind[i] = curDim_ + i; }
990 Teuchos::RCP<const MV> Vprev = MVT::CloneView(*V_,curind);
996 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
997 Teuchos::TimeMonitor lcltimer( *timerOp_ );
999 OPT::Apply(*Op_,*Vprev,*Vnext);
1000 count_ApplyOp_ += blockSize_;
1004 Vprev = Teuchos::null;
1008 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1009 Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1013 std::vector<int> prevind(lclDim);
1014 for (
int i=0; i<lclDim; i++) { prevind[i] = i; }
1015 Vprev = MVT::CloneView(*V_,prevind);
1016 Teuchos::Array<Teuchos::RCP<const MV> > AVprev(1, Vprev);
1019 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
1020 subH = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
1021 ( Teuchos::View,*H_,lclDim,blockSize_,0,curDim_ ) );
1022 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > AsubH;
1023 AsubH.append( subH );
1026 if (auxVecs_.size() > 0) {
1027 for (Array_size_type i=0; i<auxVecs_.size(); i++) {
1028 AVprev.append( auxVecs_[i] );
1029 AsubH.append( Teuchos::null );
1036 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
1037 subR = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
1038 ( Teuchos::View,*H_,blockSize_,blockSize_,lclDim,curDim_ ) );
1039 int rank = orthman_->projectAndNormalize(*Vnext,AVprev,AsubH,subR);
1046 "Anasazi::BlockKrylovSchur::iterate(): couldn't generate basis of full rank.");
1052 Vnext = Teuchos::null;
1053 curDim_ += blockSize_;
1055 ritzVecsCurrent_ =
false;
1056 ritzValsCurrent_ =
false;
1057 schurCurrent_ =
false;
1060 if (!(iter_%stepSize_)) {
1061 computeRitzValues();
1065 if (om_->isVerbosity(
Debug ) ) {
1069 chk.checkArn =
true;
1070 om_->print(
Debug, accuracyCheck(chk,
": after local update") );
1075 om_->print(
OrthoDetails, accuracyCheck(chk,
": after local update") );
1079 if (om_->isVerbosity(
Debug)) {
1080 currentStatus( om_->stream(
Debug) );
1104 template <
class ScalarType,
class MV,
class OP>
1107 std::stringstream os;
1109 os.setf(std::ios::scientific, std::ios::floatfield);
1112 os <<
" Debugging checks: iteration " << iter_ << where << std::endl;
1115 std::vector<int> lclind(curDim_);
1116 for (
int i=0; i<curDim_; i++) lclind[i] = i;
1117 std::vector<int> bsind(blockSize_);
1118 for (
int i=0; i<blockSize_; i++) { bsind[i] = curDim_ + i; }
1120 Teuchos::RCP<const MV> lclV,lclF;
1121 Teuchos::RCP<MV> lclAV;
1123 lclV = MVT::CloneView(*V_,lclind);
1124 lclF = MVT::CloneView(*V_,bsind);
1128 tmp = orthman_->orthonormError(*lclV);
1129 os <<
" >> Error in V^H M V == I : " << tmp << std::endl;
1131 tmp = orthman_->orthonormError(*lclF);
1132 os <<
" >> Error in F^H M F == I : " << tmp << std::endl;
1134 tmp = orthman_->orthogError(*lclV,*lclF);
1135 os <<
" >> Error in V^H M F == 0 : " << tmp << std::endl;
1137 for (Array_size_type i=0; i<auxVecs_.size(); i++) {
1139 tmp = orthman_->orthogError(*lclV,*auxVecs_[i]);
1140 os <<
" >> Error in V^H M Aux[" << i <<
"] == 0 : " << tmp << std::endl;
1142 tmp = orthman_->orthogError(*lclF,*auxVecs_[i]);
1143 os <<
" >> Error in F^H M Aux[" << i <<
"] == 0 : " << tmp << std::endl;
1151 lclAV = MVT::Clone(*V_,curDim_);
1153 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1154 Teuchos::TimeMonitor lcltimer( *timerOp_ );
1156 OPT::Apply(*Op_,*lclV,*lclAV);
1160 Teuchos::SerialDenseMatrix<int,ScalarType> subH(Teuchos::View,*H_,curDim_,curDim_);
1161 MVT::MvTimesMatAddMv( -ST_ONE, *lclV, subH, ST_ONE, *lclAV );
1164 Teuchos::SerialDenseMatrix<int,ScalarType> curB(Teuchos::View,*H_,
1165 blockSize_,curDim_, curDim_ );
1166 MVT::MvTimesMatAddMv( -ST_ONE, *lclF, curB, ST_ONE, *lclAV );
1169 std::vector<MagnitudeType> arnNorms( curDim_ );
1170 orthman_->norm( *lclAV, arnNorms );
1172 for (
int i=0; i<curDim_; i++) {
1173 os <<
" >> Error in Krylov-Schur factorization (R = AV-VS-FB^H), ||R[" << i <<
"]|| : " << arnNorms[i] << std::endl;
1179 for (Array_size_type i=0; i<auxVecs_.size(); i++) {
1180 tmp = orthman_->orthonormError(*auxVecs_[i]);
1181 os <<
" >> Error in Aux[" << i <<
"]^H M Aux[" << i <<
"] == I : " << tmp << std::endl;
1182 for (Array_size_type j=i+1; j<auxVecs_.size(); j++) {
1183 tmp = orthman_->orthogError(*auxVecs_[i],*auxVecs_[j]);
1184 os <<
" >> Error in Aux[" << i <<
"]^H M Aux[" << j <<
"] == 0 : " << tmp << std::endl;
1205 template <
class ScalarType,
class MV,
class OP>
1213 if (!ritzValsCurrent_) {
1216 computeSchurForm(
false );
1233 template <
class ScalarType,
class MV,
class OP>
1236 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1237 Teuchos::TimeMonitor LocalTimer(*timerCompRitzVec_);
1240 TEUCHOS_TEST_FOR_EXCEPTION(numRitzVecs_==0, std::invalid_argument,
1241 "Anasazi::BlockKrylovSchur::computeRitzVectors(): no Ritz vectors were required from this solver.");
1243 TEUCHOS_TEST_FOR_EXCEPTION(curDim_ < numRitzVecs_, std::invalid_argument,
1244 "Anasazi::BlockKrylovSchur::computeRitzVectors(): the current subspace is not large enough to compute the number of requested Ritz vectors.");
1248 if (curDim_ && initialized_) {
1251 if (!ritzVecsCurrent_) {
1254 if (!schurCurrent_) {
1257 computeSchurForm(
true );
1262 TEUCHOS_TEST_FOR_EXCEPTION(ritzIndex_[numRitzVecs_-1]==1, std::logic_error,
1263 "Anasazi::BlockKrylovSchur::computeRitzVectors(): the number of required Ritz vectors splits a complex conjugate pair.");
1265 Teuchos::LAPACK<int,ScalarType> lapack;
1266 Teuchos::LAPACK<int,MagnitudeType> lapack_mag;
1276 std::vector<int> curind( curDim_ );
1277 for (
int i=0; i<curDim_; i++) { curind[i] = i; }
1278 Teuchos::RCP<const MV> Vtemp = MVT::CloneView( *V_, curind );
1279 if (problem_->isHermitian()) {
1281 Teuchos::SerialDenseMatrix<int,ScalarType> subQ( Teuchos::View, *Q_, curDim_, numRitzVecs_ );
1284 MVT::MvTimesMatAddMv( ST_ONE, *Vtemp, subQ, ST_ZERO, *ritzVectors_ );
1289 Teuchos::SerialDenseMatrix<int,ScalarType> subQ( Teuchos::View, *Q_, curDim_, curDim_ );
1292 Teuchos::RCP<MV> tmpritzVectors_ = MVT::Clone( *V_, curDim_ );
1295 MVT::MvTimesMatAddMv( ST_ONE, *Vtemp, subQ, ST_ZERO, *tmpritzVectors_ );
1303 int lwork = 3*curDim_;
1304 std::vector<ScalarType> work( lwork );
1305 std::vector<MagnitudeType> rwork( curDim_ );
1309 ScalarType vl[ ldvl ];
1310 Teuchos::SerialDenseMatrix<int,ScalarType> copyQ( Teuchos::Copy, *Q_, curDim_, curDim_ );
1311 lapack.TREVC( side, curDim_, schurH_->values(), schurH_->stride(), vl, ldvl,
1312 copyQ.values(), copyQ.stride(), curDim_, &mm, &work[0], &rwork[0], &info );
1313 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
1314 "Anasazi::BlockKrylovSchur::computeRitzVectors(): TREVC(n==" << curDim_ <<
") returned info " << info <<
" != 0.");
1317 Teuchos::SerialDenseMatrix<int,ScalarType> subCopyQ( Teuchos::View, copyQ, curDim_, numRitzVecs_ );
1320 curind.resize( numRitzVecs_ );
1321 Teuchos::RCP<MV> view_ritzVectors = MVT::CloneViewNonConst( *ritzVectors_, curind );
1322 MVT::MvTimesMatAddMv( ST_ONE, *tmpritzVectors_, subCopyQ, ST_ZERO, *view_ritzVectors );
1325 std::vector<MagnitudeType> ritzNrm( numRitzVecs_ );
1326 MVT::MvNorm( *view_ritzVectors, ritzNrm );
1329 tmpritzVectors_ = Teuchos::null;
1330 view_ritzVectors = Teuchos::null;
1333 ScalarType ritzScale = ST_ONE;
1334 for (
int i=0; i<numRitzVecs_; i++) {
1337 if (ritzIndex_[i] == 1 ) {
1338 ritzScale = ST_ONE/lapack_mag.LAPY2(ritzNrm[i],ritzNrm[i+1]);
1339 std::vector<int> newind(2);
1340 newind[0] = i; newind[1] = i+1;
1341 tmpritzVectors_ = MVT::CloneCopy( *ritzVectors_, newind );
1342 view_ritzVectors = MVT::CloneViewNonConst( *ritzVectors_, newind );
1343 MVT::MvAddMv( ritzScale, *tmpritzVectors_, ST_ZERO, *tmpritzVectors_, *view_ritzVectors );
1350 std::vector<int> newind(1);
1352 tmpritzVectors_ = MVT::CloneCopy( *ritzVectors_, newind );
1353 view_ritzVectors = MVT::CloneViewNonConst( *ritzVectors_, newind );
1354 MVT::MvAddMv( ST_ONE/ritzNrm[i], *tmpritzVectors_, ST_ZERO, *tmpritzVectors_, *view_ritzVectors );
1361 ritzVecsCurrent_ =
true;
1370 template <
class ScalarType,
class MV,
class OP>
1372 TEUCHOS_TEST_FOR_EXCEPTION(test == Teuchos::null,std::invalid_argument,
1373 "Anasazi::BlockKrylovSchur::setStatusTest() was passed a null StatusTest.");
1379 template <
class ScalarType,
class MV,
class OP>
1397 template <
class ScalarType,
class MV,
class OP>
1401 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1402 Teuchos::TimeMonitor LocalTimer(*timerCompSF_);
1409 if (!schurCurrent_) {
1413 if (!ritzValsCurrent_) {
1414 Teuchos::LAPACK<int,ScalarType> lapack;
1415 Teuchos::LAPACK<int,MagnitudeType> lapack_mag;
1416 Teuchos::BLAS<int,ScalarType> blas;
1417 Teuchos::BLAS<int,MagnitudeType> blas_mag;
1420 Teuchos::SerialDenseMatrix<int,ScalarType> subQ( Teuchos::View, *Q_, curDim_, curDim_ );
1423 schurH_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, *H_, curDim_, curDim_ ) );
1437 int lwork = 3*curDim_;
1438 std::vector<ScalarType> work( lwork );
1439 std::vector<MagnitudeType> rwork( curDim_ );
1440 std::vector<MagnitudeType> tmp_rRitzValues( curDim_ );
1441 std::vector<MagnitudeType> tmp_iRitzValues( curDim_ );
1442 std::vector<int> bwork( curDim_ );
1443 int info = 0, sdim = 0;
1445 lapack.GEES( jobvs,curDim_, schurH_->values(), schurH_->stride(), &sdim, &tmp_rRitzValues[0],
1446 &tmp_iRitzValues[0], subQ.values(), subQ.stride(), &work[0], lwork,
1447 &rwork[0], &bwork[0], &info );
1449 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
1450 "Anasazi::BlockKrylovSchur::computeSchurForm(): GEES(n==" << curDim_ <<
") returned info " << info <<
" != 0.");
1466 Teuchos::SerialDenseMatrix<int,ScalarType> curB(Teuchos::View, *H_,
1467 blockSize_, curDim_, curDim_ );
1470 Teuchos::SerialDenseMatrix<int,ScalarType> subB( blockSize_, curDim_ );
1471 blas.GEMM( Teuchos::NO_TRANS, Teuchos::NO_TRANS, blockSize_, curDim_, curDim_, ST_ONE,
1472 curB.values(), curB.stride(), subQ.values(), subQ.stride(),
1473 ST_ZERO, subB.values(), subB.stride() );
1477 ScalarType* b_ptr = subB.values();
1478 if (problem_->isHermitian()) {
1482 for (
int i=0; i<curDim_ ; i++) {
1483 ritzResiduals_[i] = blas.NRM2(blockSize_, b_ptr + i*blockSize_, 1);
1492 ScalarType vl[ ldvl ];
1493 Teuchos::SerialDenseMatrix<int,ScalarType> S( curDim_, curDim_ );
1494 lapack.TREVC( side, curDim_, schurH_->values(), schurH_->stride(), vl, ldvl,
1495 S.values(), S.stride(), curDim_, &mm, &work[0], &rwork[0], &info );
1497 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
1498 "Anasazi::BlockKrylovSchur::computeSchurForm(): TREVC(n==" << curDim_ <<
") returned info " << info <<
" != 0.");
1506 Teuchos::SerialDenseMatrix<int,ScalarType> ritzRes( blockSize_, curDim_ );
1507 blas.GEMM( Teuchos::NO_TRANS, Teuchos::NO_TRANS, blockSize_, curDim_, curDim_, ST_ONE,
1508 subB.values(), subB.stride(), S.values(), S.stride(),
1509 ST_ZERO, ritzRes.values(), ritzRes.stride() );
1543 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1544 Teuchos::TimeMonitor LocalTimer2(*timerSortRitzVal_);
1547 if (problem_->isHermitian()) {
1550 sm_->sort(tmp_rRitzValues, Teuchos::rcpFromRef(ritzOrder_), curDim_);
1552 while ( i < curDim_ ) {
1554 ritzValues_[i].set(tmp_rRitzValues[i], MT_ZERO);
1555 ritzIndex_.push_back(0);
1562 sm_->sort(tmp_rRitzValues, tmp_iRitzValues, Teuchos::rcpFromRef(ritzOrder_) , curDim_);
1567 std::vector<MagnitudeType> ritz2( curDim_ );
1568 for (i=0; i<curDim_; i++) { ritz2[i] = ritzResiduals_[ ritzOrder_[i] ]; }
1569 blas_mag.COPY( curDim_, &ritz2[0], 1, &ritzResiduals_[0], 1 );
1572 ritzValsCurrent_ =
true;
1588 sortSchurForm( *schurH_, *Q_, ritzOrder_ );
1591 schurCurrent_ =
true;
1602 template <
class ScalarType,
class MV,
class OP>
1604 Teuchos::SerialDenseMatrix<int,ScalarType>& Q,
1605 std::vector<int>& order )
1608 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1609 Teuchos::TimeMonitor LocalTimer(*timerSortSF_);
1620 int i = 0, nevtemp = 0;
1622 std::vector<int> offset2( curDim_ );
1623 std::vector<int> order2( curDim_ );
1626 Teuchos::LAPACK<int,ScalarType> lapack;
1627 int lwork = 3*curDim_;
1628 std::vector<ScalarType> work( lwork );
1630 while (i < curDim_) {
1631 if ( ritzIndex_[i] != 0 ) {
1632 offset2[nevtemp] = 0;
1633 for (
int j=i; j<curDim_; j++) {
1634 if (order[j] > order[i]) { offset2[nevtemp]++; }
1636 order2[nevtemp] = order[i];
1639 offset2[nevtemp] = 0;
1640 for (
int j=i; j<curDim_; j++) {
1641 if (order[j] > order[i]) { offset2[nevtemp]++; }
1643 order2[nevtemp] = order[i];
1648 ScalarType *ptr_h = H.values();
1649 ScalarType *ptr_q = Q.values();
1650 int ldh = H.stride(), ldq = Q.stride();
1652 for (i=nevtemp-1; i>=0; i--) {
1653 lapack.TREXC( compq, curDim_, ptr_h, ldh, ptr_q, ldq, order2[i]+1+offset2[i],
1654 1, &work[0], &info );
1655 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
1656 "Anasazi::BlockKrylovSchur::computeSchurForm(): TREXC(n==" << curDim_ <<
") returned info " << info <<
" != 0.");
1662 template <
class ScalarType,
class MV,
class OP>
1667 os.setf(std::ios::scientific, std::ios::floatfield);
1669 os <<
"================================================================================" << endl;
1671 os <<
" BlockKrylovSchur Solver Status" << endl;
1673 os <<
"The solver is "<<(initialized_ ?
"initialized." :
"not initialized.") << endl;
1674 os <<
"The number of iterations performed is " <<iter_<<endl;
1675 os <<
"The block size is " << blockSize_<<endl;
1676 os <<
"The number of blocks is " << numBlocks_<<endl;
1677 os <<
"The current basis size is " << curDim_<<endl;
1678 os <<
"The number of auxiliary vectors is " << numAuxVecs_ << endl;
1679 os <<
"The number of operations Op*x is "<<count_ApplyOp_<<endl;
1681 os.setf(std::ios_base::right, std::ios_base::adjustfield);
1685 os <<
"CURRENT RITZ VALUES "<<endl;
1686 if (ritzIndex_.size() != 0) {
1687 int numPrint = (curDim_ < numRitzPrint_? curDim_: numRitzPrint_);
1688 if (problem_->isHermitian()) {
1689 os << std::setw(20) <<
"Ritz Value"
1690 << std::setw(20) <<
"Ritz Residual"
1692 os <<
"--------------------------------------------------------------------------------"<<endl;
1693 for (
int i=0; i<numPrint; i++) {
1694 os << std::setw(20) << ritzValues_[i].realpart
1695 << std::setw(20) << ritzResiduals_[i]
1699 os << std::setw(24) <<
"Ritz Value"
1700 << std::setw(30) <<
"Ritz Residual"
1702 os <<
"--------------------------------------------------------------------------------"<<endl;
1703 for (
int i=0; i<numPrint; i++) {
1705 os << std::setw(15) << ritzValues_[i].realpart;
1706 if (ritzValues_[i].imagpart < MT_ZERO) {
1707 os <<
" - i" << std::setw(15) << Teuchos::ScalarTraits<MagnitudeType>::magnitude(ritzValues_[i].imagpart);
1709 os <<
" + i" << std::setw(15) << ritzValues_[i].imagpart;
1711 os << std::setw(20) << ritzResiduals_[i] << endl;
1715 os << std::setw(20) <<
"[ NONE COMPUTED ]" << endl;
1719 os <<
"================================================================================" << endl;
void computeRitzValues()
Compute the Ritz values using the current Krylov factorization.
BlockKrylovSchurOrthoFailure is thrown when the orthogonalization manager is unable to generate ortho...
Teuchos::Array< Teuchos::RCP< const MV > > getAuxVecs() const
Get the auxiliary vectors for the solver.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > H
The current Hessenberg matrix.
BlockKrylovSchurState< ScalarType, MV > getState() const
Get the current state of the eigensolver.
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
void setStepSize(int stepSize)
Set the step size.
bool isRitzValsCurrent() const
Get the status of the Ritz values currently stored in the eigensolver.
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the eigenvalue problem.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRitzRes2Norms()
Get the current Ritz residual 2-norms.
Teuchos::RCP< const MV > getRitzVectors()
Get the Ritz vectors.
This class defines the interface required by an eigensolver and status test class to compute solution...
virtual ~BlockKrylovSchur()
BlockKrylovSchur destructor.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this eigenproblem.
Declaration of basic traits for the multivector type.
bool isInitialized() const
Indicates whether the solver has been initialized or not.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getResNorms()
Get the current residual norms.
void setAuxVecs(const Teuchos::Array< Teuchos::RCP< const MV > > &auxvecs)
Set the auxiliary vectors for the solver.
void iterate()
This method performs Block Krylov-Schur iterations until the status test indicates the need to stop o...
Virtual base class which defines basic traits for the operator type.
static void scaleRitzVectors(const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &iRV, Teuchos::SerialDenseMatrix< int, ScalarType > *S)
Helper function for correctly scaling the eigenvectors of the projected eigenproblem.
Structure to contain pointers to BlockKrylovSchur state variables.
An exception class parent to all Anasazi exceptions.
void setStatusTest(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Set a new StatusTest for the solver.
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current eigenvectors and eigenvalues...
int getNumIters() const
Get the current iteration count.
Pure virtual base class which describes the basic interface to the iterative eigensolver.
bool isRitzVecsCurrent() const
Get the status of the Ritz vectors currently stored in the eigensolver.
void initialize()
Initialize the solver with the initial vectors from the eigenproblem or random data.
void computeRitzVectors()
Compute the Ritz vectors using the current Krylov factorization.
int getStepSize() const
Get the step size.
void setNumRitzVectors(int numRitzVecs)
Set the number of Ritz vectors to compute.
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > getStatusTest() const
Get the current StatusTest used by the solver.
void computeSchurForm(const bool sort=true)
Compute the Schur form of the projected eigenproblem from the current Krylov factorization.
static void sortRitzValues(const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &rRV, const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &iRV, std::vector< Value< ScalarType > > *RV, std::vector< int > *RO, std::vector< int > *RI)
Helper function for correctly storing the Ritz values when the eigenproblem is non-Hermitian.
bool isSchurCurrent() const
Get the status of the Schur form currently stored in the eigensolver.
BlockKrylovSchur(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< OrthoManager< ScalarType, MV > > &ortho, Teuchos::ParameterList ¶ms)
BlockKrylovSchur constructor with eigenproblem, solver utilities, and parameter list of solver option...
Output managers remove the need for the eigensolver to know any information about the required output...
void resetNumIters()
Reset the iteration count.
Templated virtual class for providing orthogonalization/orthonormalization methods.
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
int getNumRitzVectors() const
Get the number of Ritz vectors to compute.
std::vector< Value< ScalarType > > getRitzValues()
Get the Ritz values.
std::vector< int > getRitzIndex()
Get the Ritz index vector.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRes2Norms()
Get the current residual 2-norms.
Teuchos::RCP< const MulVec > V
The current Krylov basis.
BlockKrylovSchurInitFailure is thrown when the BlockKrylovSchur solver is unable to generate an initi...
Types and exceptions used within Anasazi solvers and interfaces.
void currentStatus(std::ostream &os)
This method requests that the solver print out its current status to screen.
Anasazi's templated virtual class for providing routines for orthogonalization and orthonormalization...
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > Q
The current Schur vectors of the valid part of H.
static void computeRitzResiduals(const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &iRV, const Teuchos::SerialDenseMatrix< int, ScalarType > &S, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *RR)
Helper function for correctly computing the Ritz residuals of the projected eigenproblem.
void setBlockSize(int blockSize)
Set the blocksize.
This class implements the block Krylov-Schur iteration, for solving linear eigenvalue problems...
Anasazi's templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
void setSize(int blockSize, int numBlocks)
Set the blocksize and number of blocks to be used by the iterative solver in solving this eigenproble...
int curDim
The current dimension of the reduction.
A pure virtual class for defining the status tests for the Anasazi iterative solvers.
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > S
The current Schur form reduction of the valid part of H.