00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef ANASAZI_BLOCKDAVIDSON_SOLMGR_HPP
00030 #define ANASAZI_BLOCKDAVIDSON_SOLMGR_HPP
00031
00036 #include "AnasaziConfigDefs.hpp"
00037 #include "AnasaziTypes.hpp"
00038
00039 #include "AnasaziEigenproblem.hpp"
00040 #include "AnasaziSolverManager.hpp"
00041 #include "AnasaziSolverUtils.hpp"
00042
00043 #include "AnasaziBlockDavidson.hpp"
00044 #include "AnasaziBasicSort.hpp"
00045 #include "AnasaziSVQBOrthoManager.hpp"
00046 #include "AnasaziStatusTestMaxIters.hpp"
00047 #include "AnasaziStatusTestResNorm.hpp"
00048 #include "AnasaziStatusTestOrderedResNorm.hpp"
00049 #include "AnasaziStatusTestCombo.hpp"
00050 #include "AnasaziStatusTestOutput.hpp"
00051 #include "AnasaziBasicOutputManager.hpp"
00052 #include "Teuchos_BLAS.hpp"
00053 #include "Teuchos_LAPACK.hpp"
00054
00055
00076 namespace Anasazi {
00077
00078 template<class ScalarType, class MV, class OP>
00079 class BlockDavidsonSolMgr : public SolverManager<ScalarType,MV,OP> {
00080
00081 private:
00082 typedef MultiVecTraits<ScalarType,MV> MVT;
00083 typedef OperatorTraits<ScalarType,MV,OP> OPT;
00084 typedef Teuchos::ScalarTraits<ScalarType> SCT;
00085 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
00086 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
00087
00088 public:
00089
00091
00092
00110 BlockDavidsonSolMgr( const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
00111 Teuchos::ParameterList &pl );
00112
00114 virtual ~BlockDavidsonSolMgr() {};
00116
00118
00119
00120 const Eigenproblem<ScalarType,MV,OP>& getProblem() const {
00121 return *problem_;
00122 }
00123
00125
00127
00128
00150 ReturnType solve();
00152
00153 private:
00154 Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > problem_;
00155
00156 std::string whch_;
00157
00158 MagnitudeType convtol_, locktol_;
00159 int maxRestarts_;
00160 bool useLocking_;
00161 bool relconvtol_, rellocktol_;
00162 int blockSize_, numBlocks_;
00163 int maxLocked_;
00164 int verbosity_;
00165 int lockQuorum_;
00166 bool inSituRestart_;
00167 int numRestartBlocks_;
00168 };
00169
00170
00171
00172 template<class ScalarType, class MV, class OP>
00173 BlockDavidsonSolMgr<ScalarType,MV,OP>::BlockDavidsonSolMgr(
00174 const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
00175 Teuchos::ParameterList &pl ) :
00176 problem_(problem),
00177 whch_("SR"),
00178 convtol_(0),
00179 locktol_(0),
00180 maxRestarts_(20),
00181 useLocking_(false),
00182 relconvtol_(true),
00183 rellocktol_(true),
00184 blockSize_(0),
00185 numBlocks_(0),
00186 maxLocked_(0),
00187 verbosity_(Anasazi::Errors),
00188 lockQuorum_(1),
00189 inSituRestart_(false),
00190 numRestartBlocks_(1)
00191 {
00192 TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
00193 TEST_FOR_EXCEPTION(!problem_->isProblemSet(), std::invalid_argument, "Problem not set.");
00194 TEST_FOR_EXCEPTION(!problem_->isHermitian(), std::invalid_argument, "Problem not symmetric.");
00195 TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null, std::invalid_argument, "Problem does not contain initial vectors to clone from.");
00196
00197
00198 whch_ = pl.get("Which",whch_);
00199 TEST_FOR_EXCEPTION(whch_ != "SM" && whch_ != "LM" && whch_ != "SR" && whch_ != "LR",std::invalid_argument, "Invalid sorting string.");
00200
00201
00202 convtol_ = pl.get("Convergence Tolerance",MT::prec());
00203 relconvtol_ = pl.get("Relative Convergence Tolerance",relconvtol_);
00204
00205
00206 useLocking_ = pl.get("Use Locking",useLocking_);
00207 rellocktol_ = pl.get("Relative Locking Tolerance",rellocktol_);
00208 locktol_ = pl.get("Locking Tolerance",convtol_/10.0);
00209
00210
00211 maxRestarts_ = pl.get("Maximum Restarts",maxRestarts_);
00212
00213
00214 blockSize_ = pl.get("Block Size",problem_->getNEV());
00215 TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
00216 "Anasazi::BlockDavidsonSolMgr: \"Block Size\" must be strictly positive.");
00217 numBlocks_ = pl.get("Num Blocks",2);
00218 TEST_FOR_EXCEPTION(numBlocks_ <= 1, std::invalid_argument,
00219 "Anasazi::BlockDavidsonSolMgr: \"Num Blocks\" must be >= 1.");
00220
00221
00222 if (useLocking_) {
00223 maxLocked_ = pl.get("Max Locked",problem_->getNEV());
00224 }
00225 else {
00226 maxLocked_ = 0;
00227 }
00228 if (maxLocked_ == 0) {
00229 useLocking_ = false;
00230 }
00231 TEST_FOR_EXCEPTION(maxLocked_ < 0, std::invalid_argument,
00232 "Anasazi::BlockDavidsonSolMgr: \"Max Locked\" must be positive.");
00233 TEST_FOR_EXCEPTION(maxLocked_ + blockSize_ < problem_->getNEV(),
00234 std::invalid_argument,
00235 "Anasazi::BlockDavidsonSolMgr: Not enough storage space for requested number of eigenpairs.");
00236 TEST_FOR_EXCEPTION(numBlocks_*blockSize_ + maxLocked_ > MVT::GetVecLength(*problem_->getInitVec()),
00237 std::invalid_argument,
00238 "Anasazi::BlockDavidsonSolMgr: Potentially impossible orthogonality requests. Reduce basis size or locking size.");
00239
00240 if (useLocking_) {
00241 lockQuorum_ = pl.get("Locking Quorum",lockQuorum_);
00242 TEST_FOR_EXCEPTION(lockQuorum_ <= 0,
00243 std::invalid_argument,
00244 "Anasazi::BlockDavidsonSolMgr: \"Locking Quorum\" must be strictly positive.");
00245 }
00246
00247
00248 if (pl.isParameter("Verbosity")) {
00249 if (Teuchos::isParameterType<int>(pl,"Verbosity")) {
00250 verbosity_ = pl.get("Verbosity", verbosity_);
00251 } else {
00252 verbosity_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,"Verbosity");
00253 }
00254 }
00255
00256
00257 numRestartBlocks_ = pl.get("Num Restart Blocks",numRestartBlocks_);
00258 TEST_FOR_EXCEPTION(numRestartBlocks_ <= 0, std::invalid_argument,
00259 "Anasazi::BlockDavidsonSolMgr: \"Num Restart Blocks\" must be strictly positive.");
00260 TEST_FOR_EXCEPTION(numRestartBlocks_ >= numBlocks_, std::invalid_argument,
00261 "Anasazi::BlockDavidsonSolMgr: \"Num Restart Blocks\" must be strictly less than \"Num Blocks\".");
00262
00263
00264 if (pl.isParameter("In Situ Restarting")) {
00265 if (Teuchos::isParameterType<bool>(pl,"In Situ Restarting")) {
00266 inSituRestart_ = pl.get("In Situ Restarting",inSituRestart_);
00267 } else {
00268 inSituRestart_ = (bool)Teuchos::getParameter<int>(pl,"In Situ Restarting");
00269 }
00270 }
00271 }
00272
00273
00274
00275 template<class ScalarType, class MV, class OP>
00276 ReturnType
00277 BlockDavidsonSolMgr<ScalarType,MV,OP>::solve() {
00278
00279 typedef SolverUtils<ScalarType,MV,OP> msutils;
00280
00281 const int nev = problem_->getNEV();
00282
00284
00285 Teuchos::RCP<BasicSort<ScalarType,MV,OP> > sorter = Teuchos::rcp( new BasicSort<ScalarType,MV,OP>(whch_) );
00286
00288
00289 Teuchos::RCP<BasicOutputManager<ScalarType> > printer = Teuchos::rcp( new BasicOutputManager<ScalarType>(verbosity_) );
00290
00292
00293
00294
00295 Teuchos::RCP<StatusTestOrderedResNorm<ScalarType,MV,OP> > convtest
00296 = Teuchos::rcp( new StatusTestOrderedResNorm<ScalarType,MV,OP>(sorter,convtol_,nev,StatusTestOrderedResNorm<ScalarType,MV,OP>::RES_ORTH,relconvtol_) );
00297
00298 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > locktest;
00299 if (useLocking_) {
00300 locktest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(locktol_,lockQuorum_,StatusTestResNorm<ScalarType,MV,OP>::RES_ORTH,rellocktol_) );
00301 }
00302
00303 Teuchos::Array<Teuchos::RCP<StatusTest<ScalarType,MV,OP> > > alltests;
00304
00305 alltests.push_back(convtest);
00306 if (locktest != Teuchos::null) alltests.push_back(locktest);
00307
00308 Teuchos::RCP<StatusTestCombo<ScalarType,MV,OP> > combotest
00309 = Teuchos::rcp( new StatusTestCombo<ScalarType,MV,OP>( StatusTestCombo<ScalarType,MV,OP>::OR, alltests) );
00310
00311 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputtest
00312 = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer,combotest,1,Passed ) );
00313
00315
00316 Teuchos::RCP<SVQBOrthoManager<ScalarType,MV,OP> > ortho
00317 = Teuchos::rcp( new SVQBOrthoManager<ScalarType,MV,OP>(problem_->getM()) );
00318
00320
00321 Teuchos::ParameterList plist;
00322 plist.set("Block Size",blockSize_);
00323 plist.set("Num Blocks",numBlocks_);
00324
00326
00327 Teuchos::RCP<BlockDavidson<ScalarType,MV,OP> > bd_solver
00328 = Teuchos::rcp( new BlockDavidson<ScalarType,MV,OP>(problem_,sorter,printer,outputtest,ortho,plist) );
00329
00330 Teuchos::RCP< const MV > probauxvecs = problem_->getAuxVecs();
00331 if (probauxvecs != Teuchos::null) {
00332 bd_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(probauxvecs) );
00333 }
00334
00336
00337
00338 int curNumLocked = 0;
00339 Teuchos::RCP<MV> lockvecs;
00340
00341
00342
00343
00344
00345 if (maxLocked_ > 0) {
00346 lockvecs = MVT::Clone(*problem_->getInitVec(),maxLocked_);
00347 }
00348 std::vector<MagnitudeType> lockvals;
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 Teuchos::RCP<MV> workMV;
00397 if (inSituRestart_ == false) {
00398
00399 if (useLocking_==true || maxRestarts_ > 0) {
00400 workMV = MVT::Clone(*problem_->getInitVec(),(numBlocks_-1)*blockSize_);
00401 }
00402 else {
00403
00404 workMV = Teuchos::null;
00405 }
00406 }
00407 else {
00408
00409
00410
00411
00412
00413 if (maxRestarts_ > 0) {
00414 workMV = MVT::Clone(*problem_->getInitVec(),1);
00415 }
00416 else {
00417 workMV = Teuchos::null;
00418 }
00419 }
00420
00421
00422 const ScalarType ONE = SCT::one();
00423 const ScalarType ZERO = SCT::zero();
00424 Teuchos::LAPACK<int,ScalarType> lapack;
00425 Teuchos::BLAS<int,ScalarType> blas;
00426
00427
00428 Eigensolution<ScalarType,MV> sol;
00429 sol.numVecs = 0;
00430 problem_->setSolution(sol);
00431
00432 int numRestarts = 0;
00433
00434
00435 while (1) {
00436 try {
00437 bd_solver->iterate();
00438
00440
00441
00442
00444 if (convtest->getStatus() == Passed ) {
00445
00446
00447
00448 break;
00449 }
00451
00452
00453
00455 else if ( bd_solver->getCurSubspaceDim() == bd_solver->getMaxSubspaceDim() ) {
00456
00457 if ( numRestarts >= maxRestarts_ ) {
00458 break;
00459 }
00460 numRestarts++;
00461
00462 printer->stream(IterationDetails) << " Performing restart number " << numRestarts << " of " << maxRestarts_ << std::endl << std::endl;
00463
00464 BlockDavidsonState<ScalarType,MV> state = bd_solver->getState();
00465 int curdim = state.curDim;
00466 int newdim = numRestartBlocks_*blockSize_;
00467
00468
00469
00470 Teuchos::SerialDenseMatrix<int,ScalarType> S(curdim,curdim);
00471 std::vector<MagnitudeType> theta(curdim);
00472 int rank = curdim;
00473 int info = msutils::directSolver(curdim,*state.KK,Teuchos::null,S,theta,rank,10);
00474 TEST_FOR_EXCEPTION(info != 0 ,std::logic_error,
00475 "Anasazi::BlockDavidsonSolMgr::solve(): error calling SolverUtils::directSolver.");
00476 TEST_FOR_EXCEPTION(rank != curdim,std::logic_error,
00477 "Anasazi::BlockDavidsonSolMgr::solve(): direct solve did not compute all eigenvectors.");
00478
00479
00480
00481 {
00482 std::vector<int> order(curdim);
00483 sorter->sort(bd_solver.get(),curdim,theta,&order);
00484
00485
00486 msutils::permuteVectors(order,S);
00487 }
00488
00489
00490 Teuchos::SerialDenseMatrix<int,ScalarType> Sr(Teuchos::View,S,curdim,newdim);
00491
00492
00493 Teuchos::SerialDenseMatrix<int,ScalarType> newKK(newdim,newdim);
00494 {
00495 Teuchos::SerialDenseMatrix<int,ScalarType> KKtmp(curdim,newdim),
00496 KKold(Teuchos::View,*state.KK,curdim,curdim);
00497 int teuchosRet;
00498
00499 teuchosRet = KKtmp.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,KKold,Sr,ZERO);
00500 TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
00501 "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
00502
00503 teuchosRet = newKK.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,Sr,KKtmp,ZERO);
00504 TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
00505 "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
00506
00507 for (int j=0; j<newdim; ++j) {
00508 for (int i=j+1; i<newdim; ++i) {
00509 newKK(i,j) = SCT::conjugate(newKK(j,i));
00510 }
00511 }
00512 }
00513
00514
00515 BlockDavidsonState<ScalarType,MV> rstate;
00516 rstate.curDim = newdim;
00517 rstate.KK = Teuchos::rcp( &newKK, false );
00518
00519
00520
00521
00522
00523 rstate.X = state.X;
00524 rstate.KX = state.KX;
00525 rstate.MX = state.MX;
00526 rstate.R = state.R;
00527 rstate.T = Teuchos::rcp( new std::vector<MagnitudeType>(&theta[0],&theta[newdim]) );
00528
00529 if (inSituRestart_ == true) {
00530
00531
00532 Teuchos::RCP<MV> solverbasis = Teuchos::rcp_const_cast<MV>(state.V);
00533
00534
00535
00536 std::vector<ScalarType> tau(newdim), work(newdim);
00537 int info;
00538 lapack.GEQRF(curdim,newdim,Sr.values(),Sr.stride(),&tau[0],&work[0],work.size(),&info);
00539 TEST_FOR_EXCEPTION(info != 0,std::logic_error,
00540 "Anasazi::BlockDavidsonSolMgr::solve(): error calling GEQRF during restarting.");
00541 if (printer->isVerbosity(Debug)) {
00542 Teuchos::SerialDenseMatrix<int,ScalarType> R(Teuchos::Copy,Sr,newdim,newdim);
00543 for (int j=0; j<newdim; j++) {
00544 R(j,j) = SCT::magnitude(R(j,j)) - 1.0;
00545 for (int i=j+1; i<newdim; i++) {
00546 R(i,j) = ZERO;
00547 }
00548 }
00549 printer->stream(Debug) << "||Triangular factor of Sr - I||: " << R.normFrobenius() << std::endl;
00550 }
00551
00552
00553
00554
00555 {
00556 std::vector<int> curind(curdim);
00557 for (int i=0; i<curdim; i++) curind[i] = i;
00558 Teuchos::RCP<MV> oldV = MVT::CloneView(*solverbasis,curind);
00559 msutils::applyHouse(newdim,*oldV,Sr,tau,workMV);
00560 }
00561
00562
00563
00564
00565 rstate.V = solverbasis;
00566 }
00567 else {
00568
00569 std::vector<int> curind(curdim), newind(newdim);
00570 for (int i=0; i<curdim; i++) curind[i] = i;
00571 for (int i=0; i<newdim; i++) newind[i] = i;
00572 Teuchos::RCP<const MV> oldV = MVT::CloneView(*state.V,curind);
00573 Teuchos::RCP<MV> newV = MVT::CloneView(*workMV ,newind);
00574
00575 MVT::MvTimesMatAddMv(ONE,*oldV,Sr,ZERO,*newV);
00576
00577
00578 rstate.V = newV;
00579 }
00580
00581
00582
00583 bd_solver->initialize(rstate);
00584 }
00586
00587
00588
00590 else if (locktest != Teuchos::null && locktest->getStatus() == Passed) {
00591
00592
00593
00594 BlockDavidsonState<ScalarType,MV> state = bd_solver->getState();
00595 const int curdim = state.curDim;
00596
00597
00598
00599 TEST_FOR_EXCEPTION(locktest->howMany() <= 0,std::logic_error,
00600 "Anasazi::BlockDavidsonSolMgr::solve(): status test mistake.");
00601 TEST_FOR_EXCEPTION(locktest->howMany() != (int)locktest->whichVecs().size(),std::logic_error,
00602 "Anasazi::BlockDavidsonSolMgr::solve(): status test mistake.");
00603
00604
00605 std::vector<int> tmp_vector_int;
00606 if (curNumLocked + locktest->howMany() > maxLocked_) {
00607
00608 tmp_vector_int.insert(tmp_vector_int.begin(),locktest->whichVecs().begin(),locktest->whichVecs().begin()+maxLocked_-curNumLocked);
00609 }
00610 else {
00611 tmp_vector_int = locktest->whichVecs();
00612 }
00613 const std::vector<int> lockind(tmp_vector_int);
00614 const int numNewLocked = lockind.size();
00615
00616
00617
00618 const int numUnlocked = curdim-numNewLocked;
00619 tmp_vector_int.resize(curdim);
00620 for (int i=0; i<curdim; i++) tmp_vector_int[i] = i;
00621 const std::vector<int> curind(tmp_vector_int);
00622 tmp_vector_int.resize(numUnlocked);
00623 set_difference(curind.begin(),curind.end(),lockind.begin(),lockind.end(),tmp_vector_int.begin());
00624 const std::vector<int> unlockind(tmp_vector_int);
00625 tmp_vector_int.clear();
00626
00627
00628
00629 if (printer->isVerbosity(Debug)) {
00630 printer->print(Debug,"Locking vectors: ");
00631 for (unsigned int i=0; i<lockind.size(); i++) {printer->stream(Debug) << " " << lockind[i];}
00632 printer->print(Debug,"\n");
00633 }
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 Teuchos::SerialDenseMatrix<int,ScalarType> S(curdim,curdim);
00644 std::vector<MagnitudeType> theta(curdim);
00645 {
00646 int rank = curdim;
00647 int info = msutils::directSolver(curdim,*state.KK,Teuchos::null,S,theta,rank,10);
00648 TEST_FOR_EXCEPTION(info != 0 ,std::logic_error,
00649 "Anasazi::BlockDavidsonSolMgr::solve(): error calling SolverUtils::directSolver.");
00650 TEST_FOR_EXCEPTION(rank != curdim,std::logic_error,
00651 "Anasazi::BlockDavidsonSolMgr::solve(): direct solve did not compute all eigenvectors.");
00652
00653
00654 std::vector<int> order(curdim);
00655 sorter->sort(bd_solver.get(),curdim,theta,&order);
00656
00657
00658 msutils::permuteVectors(order,S);
00659 }
00660
00661
00662
00663
00664 Teuchos::SerialDenseMatrix<int,ScalarType> Su(curdim,numUnlocked);
00665 for (int i=0; i<numUnlocked; i++) {
00666 blas.COPY(curdim, S[unlockind[i]], 1, Su[i], 1);
00667 }
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 Teuchos::RCP<MV> defV, augV;
00680 if (inSituRestart_ == true) {
00681
00682
00683 Teuchos::RCP<MV> solverbasis = Teuchos::rcp_const_cast<MV>(state.V);
00684
00685
00686
00687 Teuchos::SerialDenseMatrix<int,ScalarType> copySu(Su);
00688 std::vector<ScalarType> tau(numUnlocked), work(numUnlocked);
00689 int info;
00690 lapack.GEQRF(curdim,numUnlocked,copySu.values(),copySu.stride(),&tau[0],&work[0],work.size(),&info);
00691 TEST_FOR_EXCEPTION(info != 0,std::logic_error,
00692 "Anasazi::BlockDavidsonSolMgr::solve(): error calling GEQRF during restarting.");
00693 if (printer->isVerbosity(Debug)) {
00694 Teuchos::SerialDenseMatrix<int,ScalarType> R(Teuchos::Copy,copySu,numUnlocked,numUnlocked);
00695 for (int j=0; j<numUnlocked; j++) {
00696 R(j,j) = SCT::magnitude(R(j,j)) - 1.0;
00697 for (int i=j+1; i<numUnlocked; i++) {
00698 R(i,j) = ZERO;
00699 }
00700 }
00701 printer->stream(Debug) << "||Triangular factor of Su - I||: " << R.normFrobenius() << std::endl;
00702 }
00703
00704
00705
00706
00707 {
00708 Teuchos::RCP<MV> oldV = MVT::CloneView(*solverbasis,curind);
00709 msutils::applyHouse(numUnlocked,*oldV,copySu,tau,workMV);
00710 }
00711 std::vector<int> defind(numUnlocked), augind(numNewLocked);
00712 for (int i=0; i<numUnlocked ; i++) defind[i] = i;
00713 for (int i=0; i<numNewLocked; i++) augind[i] = numUnlocked+i;
00714 defV = MVT::CloneView(*solverbasis,defind);
00715 augV = MVT::CloneView(*solverbasis,augind);
00716 }
00717 else {
00718
00719 std::vector<int> defind(numUnlocked), augind(numNewLocked);
00720 for (int i=0; i<numUnlocked ; i++) defind[i] = i;
00721 for (int i=0; i<numNewLocked; i++) augind[i] = numUnlocked+i;
00722 Teuchos::RCP<const MV> oldV = MVT::CloneView(*state.V,curind);
00723 defV = MVT::CloneView(*workMV,defind);
00724 augV = MVT::CloneView(*workMV,augind);
00725
00726 MVT::MvTimesMatAddMv(ONE,*oldV,Su,ZERO,*defV);
00727 }
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740 Teuchos::RCP<const MV> curlocked, newLocked;
00741 Teuchos::RCP<MV> augTmp;
00742 {
00743
00744 if (curNumLocked > 0) {
00745 std::vector<int> curlockind(curNumLocked);
00746 for (int i=0; i<curNumLocked; i++) curlockind[i] = i;
00747 curlocked = MVT::CloneView(*lockvecs,curlockind);
00748 }
00749 else {
00750 curlocked = Teuchos::null;
00751 }
00752
00753 std::vector<int> augtmpind(numNewLocked);
00754 for (int i=0; i<numNewLocked; i++) augtmpind[i] = curNumLocked+i;
00755 augTmp = MVT::CloneView(*lockvecs,augtmpind);
00756
00757 newLocked = MVT::CloneView(*bd_solver->getRitzVectors(),lockind);
00758 }
00759
00760
00761
00762
00763 MVT::MvRandom(*augV);
00764
00765
00766
00767 {
00768 Teuchos::Array<Teuchos::RCP<const MV> > against;
00769 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > dummy;
00770 if (probauxvecs != Teuchos::null) against.push_back(probauxvecs);
00771 if (curlocked != Teuchos::null) against.push_back(curlocked);
00772 against.push_back(newLocked);
00773 against.push_back(defV);
00774 if (problem_->getM() != Teuchos::null) {
00775 OPT::Apply(*problem_->getM(),*augV,*augTmp);
00776 }
00777 ortho->projectAndNormalizeMat(*augV,augTmp,dummy,Teuchos::null,against);
00778 }
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788 Teuchos::SerialDenseMatrix<int,ScalarType> newKK(curdim,curdim);
00789 {
00790 Teuchos::SerialDenseMatrix<int,ScalarType> KKtmp(curdim,numUnlocked),
00791 KKold(Teuchos::View,*state.KK,curdim,curdim),
00792 KK11(Teuchos::View,newKK,numUnlocked,numUnlocked);
00793 int teuchosRet;
00794
00795 teuchosRet = KKtmp.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,KKold,Su,ZERO);
00796 TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
00797 "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
00798
00799 teuchosRet = KK11.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,Su,KKtmp,ZERO);
00800 TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
00801 "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
00802 }
00803
00804
00805 {
00806 OPT::Apply(*problem_->getOperator(),*augV,*augTmp);
00807 Teuchos::SerialDenseMatrix<int,ScalarType> KK12(Teuchos::View,newKK,numUnlocked,numNewLocked,0,numUnlocked),
00808 KK22(Teuchos::View,newKK,numNewLocked,numNewLocked,numUnlocked,numUnlocked);
00809 MVT::MvTransMv(ONE,*defV,*augTmp,KK12);
00810 MVT::MvTransMv(ONE,*augV,*augTmp,KK22);
00811 }
00812
00813
00814 defV = Teuchos::null;
00815 augV = Teuchos::null;
00816
00817
00818 for (int j=0; j<curdim; ++j) {
00819 for (int i=j+1; i<curdim; ++i) {
00820 newKK(i,j) = SCT::conjugate(newKK(j,i));
00821 }
00822 }
00823
00824
00825
00826 augTmp = Teuchos::null;
00827 {
00828 std::vector<Value<ScalarType> > allvals = bd_solver->getRitzValues();
00829 for (int i=0; i<numNewLocked; i++) {
00830 lockvals.push_back(allvals[lockind[i]].realpart);
00831 }
00832
00833 std::vector<int> indlock(numNewLocked);
00834 for (int i=0; i<numNewLocked; i++) indlock[i] = curNumLocked+i;
00835 MVT::SetBlock(*newLocked,indlock,*lockvecs);
00836 newLocked = Teuchos::null;
00837
00838 curNumLocked += numNewLocked;
00839 std::vector<int> curlockind(curNumLocked);
00840 for (int i=0; i<curNumLocked; i++) curlockind[i] = i;
00841 curlocked = MVT::CloneView(*lockvecs,curlockind);
00842 }
00843
00844
00845
00846 {
00847 convtest->setAuxVals(lockvals);
00848
00849 Teuchos::Array< Teuchos::RCP<const MV> > aux;
00850 if (probauxvecs != Teuchos::null) aux.push_back(probauxvecs);
00851 aux.push_back(curlocked);
00852 bd_solver->setAuxVecs(aux);
00853
00854 if (curNumLocked == maxLocked_) {
00855
00856 locktest->setQuorum(blockSize_+1);
00857 }
00858 }
00859
00860
00861
00862 BlockDavidsonState<ScalarType,MV> rstate;
00863 rstate.curDim = curdim;
00864 if (inSituRestart_) {
00865
00866 rstate.V = state.V;
00867 }
00868 else {
00869
00870 rstate.V = workMV;
00871 }
00872 rstate.KK = Teuchos::rcp( &newKK, false );
00873
00874
00875 bd_solver->initialize(rstate);
00876 }
00878
00879
00880
00881
00883 else {
00884 TEST_FOR_EXCEPTION(true,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): Invalid return from bd_solver::iterate().");
00885 }
00886 }
00887 catch (std::exception e) {
00888 printer->stream(Errors) << "Error! Caught exception in BlockDavidson::iterate() at iteration " << bd_solver->getNumIters() << std::endl
00889 << e.what() << std::endl;
00890 throw;
00891 }
00892 }
00893
00894
00895 workMV = Teuchos::null;
00896
00897 sol.numVecs = convtest->howMany();
00898 if (sol.numVecs > 0) {
00899 sol.Evecs = MVT::Clone(*problem_->getInitVec(),sol.numVecs);
00900 sol.Espace = sol.Evecs;
00901 sol.Evals.resize(sol.numVecs);
00902 std::vector<MagnitudeType> vals(sol.numVecs);
00903
00904
00905 std::vector<int> which = convtest->whichVecs();
00906
00907
00908
00909 std::vector<int> inlocked(0), insolver(0);
00910 for (unsigned int i=0; i<which.size(); i++) {
00911 if (which[i] < blockSize_) {
00912 insolver.push_back(which[i]);
00913 }
00914 else {
00915
00916 TEST_FOR_EXCEPTION(which[i] >= curNumLocked+blockSize_,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): indexing mistake.");
00917 inlocked.push_back(which[i] - blockSize_);
00918 }
00919 }
00920
00921 TEST_FOR_EXCEPTION(insolver.size() + inlocked.size() != (unsigned int)sol.numVecs,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): indexing mistake.");
00922
00923
00924 if (insolver.size() > 0) {
00925
00926 int lclnum = insolver.size();
00927 std::vector<int> tosol(lclnum);
00928 for (int i=0; i<lclnum; i++) tosol[i] = i;
00929 Teuchos::RCP<const MV> v = MVT::CloneView(*bd_solver->getRitzVectors(),insolver);
00930 MVT::SetBlock(*v,tosol,*sol.Evecs);
00931
00932 std::vector<Value<ScalarType> > fromsolver = bd_solver->getRitzValues();
00933 for (unsigned int i=0; i<insolver.size(); i++) {
00934 vals[i] = fromsolver[insolver[i]].realpart;
00935 }
00936 }
00937
00938
00939 if (inlocked.size() > 0) {
00940 int solnum = insolver.size();
00941
00942 int lclnum = inlocked.size();
00943 std::vector<int> tosol(lclnum);
00944 for (int i=0; i<lclnum; i++) tosol[i] = solnum + i;
00945 Teuchos::RCP<const MV> v = MVT::CloneView(*lockvecs,inlocked);
00946 MVT::SetBlock(*v,tosol,*sol.Evecs);
00947
00948 for (unsigned int i=0; i<inlocked.size(); i++) {
00949 vals[i+solnum] = lockvals[inlocked[i]];
00950 }
00951 }
00952
00953
00954 {
00955 std::vector<int> order(sol.numVecs);
00956 sorter->sort(bd_solver.get(), sol.numVecs, vals, &order );
00957
00958 for (int i=0; i<sol.numVecs; i++) {
00959 sol.Evals[i].realpart = vals[i];
00960 sol.Evals[i].imagpart = MT::zero();
00961 }
00962
00963 msutils::permuteVectors(sol.numVecs,order,*sol.Evecs);
00964 }
00965
00966
00967 sol.index.resize(sol.numVecs,0);
00968 }
00969
00970
00971 bd_solver->currentStatus(printer->stream(FinalSummary));
00972
00973
00974 Teuchos::TimeMonitor::summarize(printer->stream(TimingDetails));
00975
00976 problem_->setSolution(sol);
00977 printer->stream(Debug) << "Returning " << sol.numVecs << " eigenpairs to eigenproblem." << std::endl;
00978
00979 if (sol.numVecs < nev) {
00980 return Unconverged;
00981 }
00982 return Converged;
00983 }
00984
00985
00986 }
00987
00988 #endif