Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziBlockDavidsonSolMgr.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
29 #ifndef ANASAZI_BLOCKDAVIDSON_SOLMGR_HPP
30 #define ANASAZI_BLOCKDAVIDSON_SOLMGR_HPP
31 
36 #include "AnasaziConfigDefs.hpp"
37 #include "AnasaziTypes.hpp"
38 
39 #include "AnasaziEigenproblem.hpp"
40 #include "AnasaziSolverManager.hpp"
41 #include "AnasaziSolverUtils.hpp"
42 
43 #include "AnasaziBlockDavidson.hpp"
44 #include "AnasaziBasicSort.hpp"
52 #include "Teuchos_BLAS.hpp"
53 #include "Teuchos_LAPACK.hpp"
54 #include "Teuchos_TimeMonitor.hpp"
55 #ifdef TEUCHOS_DEBUG
56 # include <Teuchos_FancyOStream.hpp>
57 #endif
58 #ifdef HAVE_MPI
59 #include <mpi.h>
60 #endif
61 
62 
63 
77 namespace Anasazi {
78 
79 
112 template<class ScalarType, class MV, class OP>
113 class BlockDavidsonSolMgr : public SolverManager<ScalarType,MV,OP> {
114 
115  private:
120  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
122 
123  public:
124 
126 
127 
152 
154  virtual ~BlockDavidsonSolMgr() {};
156 
158 
159 
162  return *problem_;
163  }
164 
166  int getNumIters() const {
167  return numIters_;
168  }
169 
178  return Teuchos::tuple(_timerSolve, _timerRestarting, _timerLocking);
179  }
180 
182 
184 
185 
207  ReturnType solve();
208 
211 
214 
217 
220 
223 
226 
228 
229  private:
231 
232  std::string whch_, ortho_;
233 
234  MagnitudeType convtol_, locktol_;
235  int maxRestarts_;
236  bool useLocking_;
237  bool relconvtol_, rellocktol_;
238  int blockSize_, numBlocks_, numIters_;
239  int maxLocked_;
240  int lockQuorum_;
241  bool inSituRestart_;
242  int numRestartBlocks_;
243  enum StatusTestResNorm<ScalarType,MV,OP>::ResType convNorm_, lockNorm_;
244 
245  Teuchos::RCP<Teuchos::Time> _timerSolve, _timerRestarting, _timerLocking;
246 
250 
252 };
253 
254 
255 // Constructor
256 template<class ScalarType, class MV, class OP>
259  Teuchos::ParameterList &pl ) :
260  problem_(problem),
261  whch_("SR"),
262  ortho_("SVQB"),
263  convtol_(MT::prec()),
264  maxRestarts_(20),
265  useLocking_(false),
266  relconvtol_(true),
267  rellocktol_(true),
268  blockSize_(0),
269  numBlocks_(0),
270  numIters_(0),
271  maxLocked_(0),
272  lockQuorum_(1),
273  inSituRestart_(false),
274  numRestartBlocks_(1)
275 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
276  , _timerSolve(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidsonSolMgr::solve()")),
277  _timerRestarting(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidsonSolMgr restarting")),
278  _timerLocking(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidsonSolMgr locking"))
279 #endif
280 {
281  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
282  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(), std::invalid_argument, "Problem not set.");
283  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isHermitian(), std::invalid_argument, "Problem not symmetric.");
284  TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument, "Problem does not contain initial vectors to clone from.");
285 
286  std::string strtmp;
287 
288  // which values to solve for
289  whch_ = pl.get("Which",whch_);
290  TEUCHOS_TEST_FOR_EXCEPTION(whch_ != "SM" && whch_ != "LM" && whch_ != "SR" && whch_ != "LR",std::invalid_argument, "Invalid sorting string.");
291 
292  // which orthogonalization to use
293  ortho_ = pl.get("Orthogonalization",ortho_);
294  if (ortho_ != "DGKS" && ortho_ != "SVQB") {
295  ortho_ = "SVQB";
296  }
297 
298  // convergence tolerance
299  convtol_ = pl.get("Convergence Tolerance",convtol_);
300  relconvtol_ = pl.get("Relative Convergence Tolerance",relconvtol_);
301  strtmp = pl.get("Convergence Norm",std::string("2"));
302  if (strtmp == "2") {
304  }
305  else if (strtmp == "M") {
307  }
308  else {
309  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
310  "Anasazi::BlockDavidsonSolMgr: Invalid Convergence Norm.");
311  }
312 
313  // locking tolerance
314  useLocking_ = pl.get("Use Locking",useLocking_);
315  rellocktol_ = pl.get("Relative Locking Tolerance",rellocktol_);
316  // default: should be less than convtol_
317  locktol_ = convtol_/10;
318  locktol_ = pl.get("Locking Tolerance",locktol_);
319  strtmp = pl.get("Locking Norm",std::string("2"));
320  if (strtmp == "2") {
322  }
323  else if (strtmp == "M") {
325  }
326  else {
327  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
328  "Anasazi::BlockDavidsonSolMgr: Invalid Locking Norm.");
329  }
330 
331  // maximum number of restarts
332  maxRestarts_ = pl.get("Maximum Restarts",maxRestarts_);
333 
334  // block size: default is nev()
335  blockSize_ = pl.get("Block Size",problem_->getNEV());
336  TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
337  "Anasazi::BlockDavidsonSolMgr: \"Block Size\" must be strictly positive.");
338  numBlocks_ = pl.get("Num Blocks",2);
339  TEUCHOS_TEST_FOR_EXCEPTION(numBlocks_ <= 1, std::invalid_argument,
340  "Anasazi::BlockDavidsonSolMgr: \"Num Blocks\" must be >= 1.");
341 
342  // max locked: default is nev(), must satisfy maxLocked_ + blockSize_ >= nev
343  if (useLocking_) {
344  maxLocked_ = pl.get("Max Locked",problem_->getNEV());
345  }
346  else {
347  maxLocked_ = 0;
348  }
349  if (maxLocked_ == 0) {
350  useLocking_ = false;
351  }
352  TEUCHOS_TEST_FOR_EXCEPTION(maxLocked_ < 0, std::invalid_argument,
353  "Anasazi::BlockDavidsonSolMgr: \"Max Locked\" must be positive.");
354  TEUCHOS_TEST_FOR_EXCEPTION(maxLocked_ + blockSize_ < problem_->getNEV(),
355  std::invalid_argument,
356  "Anasazi::BlockDavidsonSolMgr: Not enough storage space for requested number of eigenpairs.");
357  TEUCHOS_TEST_FOR_EXCEPTION(static_cast<ptrdiff_t>(numBlocks_)*blockSize_ + maxLocked_ > MVText::GetGlobalLength(*problem_->getInitVec()),
358  std::invalid_argument,
359  "Anasazi::BlockDavidsonSolMgr: Potentially impossible orthogonality requests. Reduce basis size or locking size.");
360 
361  if (useLocking_) {
362  lockQuorum_ = pl.get("Locking Quorum",lockQuorum_);
363  TEUCHOS_TEST_FOR_EXCEPTION(lockQuorum_ <= 0,
364  std::invalid_argument,
365  "Anasazi::BlockDavidsonSolMgr: \"Locking Quorum\" must be strictly positive.");
366  }
367 
368  // restart size
369  numRestartBlocks_ = pl.get("Num Restart Blocks",numRestartBlocks_);
370  TEUCHOS_TEST_FOR_EXCEPTION(numRestartBlocks_ <= 0, std::invalid_argument,
371  "Anasazi::BlockDavidsonSolMgr: \"Num Restart Blocks\" must be strictly positive.");
372  TEUCHOS_TEST_FOR_EXCEPTION(numRestartBlocks_ >= numBlocks_, std::invalid_argument,
373  "Anasazi::BlockDavidsonSolMgr: \"Num Restart Blocks\" must be strictly less than \"Num Blocks\".");
374 
375  // restarting technique: V*Q or applyHouse(V,H,tau)
376  if (pl.isParameter("In Situ Restarting")) {
377  if (Teuchos::isParameterType<bool>(pl,"In Situ Restarting")) {
378  inSituRestart_ = pl.get("In Situ Restarting",inSituRestart_);
379  } else {
380  inSituRestart_ = ( Teuchos::getParameter<int>(pl,"In Situ Restarting") != 0 );
381  }
382  }
383 
384  // output stream
385  std::string fntemplate = "";
386  bool allProcs = false;
387  if (pl.isParameter("Output on all processors")) {
388  if (Teuchos::isParameterType<bool>(pl,"Output on all processors")) {
389  allProcs = pl.get("Output on all processors",allProcs);
390  } else {
391  allProcs = ( Teuchos::getParameter<int>(pl,"Output on all processors") != 0 );
392  }
393  }
394  fntemplate = pl.get("Output filename template",fntemplate);
395  int MyPID;
396 # ifdef HAVE_MPI
397  // Initialize MPI
398  int mpiStarted = 0;
399  MPI_Initialized(&mpiStarted);
400  if (mpiStarted) MPI_Comm_rank(MPI_COMM_WORLD, &MyPID);
401  else MyPID=0;
402 # else
403  MyPID = 0;
404 # endif
405  if (fntemplate != "") {
406  std::ostringstream MyPIDstr;
407  MyPIDstr << MyPID;
408  // replace %d in fntemplate with MyPID
409  int pos, start=0;
410  while ( (pos = fntemplate.find("%d",start)) != -1 ) {
411  fntemplate.replace(pos,2,MyPIDstr.str());
412  start = pos+2;
413  }
414  }
416  if (fntemplate != "") {
417  osp = Teuchos::rcp( new std::ofstream(fntemplate.c_str(),std::ios::out | std::ios::app) );
418  if (!*osp) {
419  osp = Teuchos::rcpFromRef(std::cout);
420  std::cout << "Anasazi::BlockDavidsonSolMgr::constructor(): Could not open file for write: " << fntemplate << std::endl;
421  }
422  }
423  else {
424  osp = Teuchos::rcpFromRef(std::cout);
425  }
426  // Output manager
427  int verbosity = Anasazi::Errors;
428  if (pl.isParameter("Verbosity")) {
429  if (Teuchos::isParameterType<int>(pl,"Verbosity")) {
430  verbosity = pl.get("Verbosity", verbosity);
431  } else {
432  verbosity = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,"Verbosity");
433  }
434  }
435  if (allProcs) {
436  // print on all procs
437  printer_ = Teuchos::rcp( new BasicOutputManager<ScalarType>(verbosity,osp,MyPID) );
438  }
439  else {
440  // print only on proc 0
441  printer_ = Teuchos::rcp( new BasicOutputManager<ScalarType>(verbosity,osp,0) );
442  }
443 }
444 
445 
446 // solve()
447 template<class ScalarType, class MV, class OP>
448 ReturnType
450 
451  typedef SolverUtils<ScalarType,MV,OP> msutils;
452 
453  const int nev = problem_->getNEV();
454 
455 #ifdef TEUCHOS_DEBUG
457  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(printer_->stream(Debug)));
458  out->setShowAllFrontMatter(false).setShowProcRank(true);
459  *out << "Entering Anasazi::BlockDavidsonSolMgr::solve()\n";
460 #endif
461 
463  // Sort manager
465 
467  // Status tests
468  //
469  // convergence
471  if (globalTest_ == Teuchos::null) {
472  convtest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(convtol_,nev,convNorm_,relconvtol_) );
473  }
474  else {
475  convtest = globalTest_;
476  }
478  = Teuchos::rcp( new StatusTestWithOrdering<ScalarType,MV,OP>(convtest,sorter,nev) );
479  // locking
481  if (useLocking_) {
482  if (lockingTest_ == Teuchos::null) {
483  locktest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(locktol_,lockQuorum_,lockNorm_,rellocktol_) );
484  }
485  else {
486  locktest = lockingTest_;
487  }
488  }
489  // for a non-short-circuited OR test, the order doesn't matter
491  alltests.push_back(ordertest);
492  if (locktest != Teuchos::null) alltests.push_back(locktest);
493  if (debugTest_ != Teuchos::null) alltests.push_back(debugTest_);
494 
497  // printing StatusTest
499  if ( printer_->isVerbosity(Debug) ) {
500  outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer_,combotest,1,Passed+Failed+Undefined ) );
501  }
502  else {
503  outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer_,combotest,1,Passed ) );
504  }
505 
507  // Orthomanager
509  if (ortho_=="SVQB") {
510  ortho = Teuchos::rcp( new SVQBOrthoManager<ScalarType,MV,OP>(problem_->getM()) );
511  } else if (ortho_=="DGKS") {
512  ortho = Teuchos::rcp( new BasicOrthoManager<ScalarType,MV,OP>(problem_->getM()) );
513  } else {
514  TEUCHOS_TEST_FOR_EXCEPTION(ortho_!="SVQB"&&ortho_!="DGKS",std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): Invalid orthogonalization type.");
515  }
516 
518  // Parameter list
520  plist.set("Block Size",blockSize_);
521  plist.set("Num Blocks",numBlocks_);
522 
524  // BlockDavidson solver
526  = Teuchos::rcp( new BlockDavidson<ScalarType,MV,OP>(problem_,sorter,printer_,outputtest,ortho,plist) );
527  // set any auxiliary vectors defined in the problem
528  Teuchos::RCP< const MV > probauxvecs = problem_->getAuxVecs();
529  if (probauxvecs != Teuchos::null) {
530  bd_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(probauxvecs) );
531  }
532 
534  // Storage
535  //
536  // lockvecs will contain eigenvectors that have been determined "locked" by the status test
537  int curNumLocked = 0;
538  Teuchos::RCP<MV> lockvecs;
539  // lockvecs is used to hold the locked eigenvectors, as well as for temporary storage when locking.
540  // when locking, we will lock some number of vectors numnew, where numnew <= maxlocked - curlocked
541  // we will produce numnew random vectors, which will go into the space with the new basis.
542  // we will also need numnew storage for the image of these random vectors under A and M;
543  // columns [curlocked+1,curlocked+numnew] will be used for this storage
544  if (maxLocked_ > 0) {
545  lockvecs = MVT::Clone(*problem_->getInitVec(),maxLocked_);
546  }
547  std::vector<MagnitudeType> lockvals;
548  //
549  // Restarting occurs under two scenarios: when the basis is full and after locking.
550  //
551  // For the former, a new basis of size blockSize*numRestartBlocks is generated using the current basis
552  // and the most significant primitive Ritz vectors (projected eigenvectors).
553  // [S,L] = eig(KK)
554  // S = [Sr St] // some for "r"estarting, some are "t"runcated
555  // newV = V*Sr
556  // KK_new = newV'*K*newV = Sr'*V'*K*V*Sr = Sr'*KK*Sr
557  // Therefore, the only multivector operation needed is for the generation of newV.
558  //
559  // * If the multiplication is explicit, it requires a workspace of blockSize*numRestartBlocks vectors.
560  // This space must be specifically allocated for that task, as we don't have any space of that size.
561  // It (workMV) will be allocated at the beginning of solve()
562  // * Optionally, the multiplication can be performed implicitly, via a Householder QR factorization of
563  // Sr. This can be done in situ, using the basis multivector contained in the solver. This requires
564  // that we cast away the const on the multivector returned from getState(). Workspace for this approach
565  // is a single vector. the solver's internal storage must be preserved (X,MX,KX,R), requiring us to
566  // allocate this vector.
567  //
568  // For the latter (restarting after locking), the new basis is the same size as existing basis. If numnew
569  // vectors are locked, they are deflated from the current basis and replaced with randomly generated
570  // vectors.
571  // [S,L] = eig(KK)
572  // S = [Sl Su] // partitioned: "l"ocked and "u"nlocked
573  // newL = V*Sl = X(locked)
574  // defV = V*Su
575  // augV = rand(numnew) // orthogonal to oldL,newL,defV,auxvecs
576  // newV = [defV augV]
577  // Kknew = newV'*K*newV = [Su'*KK*Su defV'*K*augV]
578  // [augV'*K*defV augV'*K*augV]
579  // locked = [oldL newL]
580  // Clearly, this operation is more complicated than the previous.
581  // Here is a list of the significant computations that need to be performed:
582  // - newL will be put into space in lockvecs, but will be copied from getState().X at the end
583  // - defV,augV will be stored in workspace the size of the current basis.
584  // - If inSituRestart==true, we compute defV in situ in bd_solver::V_ and
585  // put augV at the end of bd_solver::V_
586  // - If inSituRestart==false, we must have curDim vectors available for
587  // defV and augV; we will allocate a multivector (workMV) at the beginning of solve()
588  // for this purpose.
589  // - M*augV and K*augV are needed; they will be stored in lockvecs. As a result, newL will
590  // not be put into lockvecs until the end.
591  //
592  // Therefore, we must allocate workMV when ((maxRestarts_ > 0) || (useLocking_ == true)) && inSituRestart == false
593  // It will be allocated to size (numBlocks-1)*blockSize
594  //
595  Teuchos::RCP<MV> workMV;
596  if (inSituRestart_ == false) {
597  // we need storage space to restart, either if we may lock or if may restart after a full basis
598  if (useLocking_==true || maxRestarts_ > 0) {
599  workMV = MVT::Clone(*problem_->getInitVec(),(numBlocks_-1)*blockSize_);
600  }
601  else {
602  // we will never need to restart.
603  workMV = Teuchos::null;
604  }
605  }
606  else { // inSituRestart_ == true
607  // we will restart in situ, if we need to restart
608  // three situation remain:
609  // - never restart => no space needed
610  // - only restart for locking (i.e., never restart full) => no space needed
611  // - restart for full basis => need one vector
612  if (maxRestarts_ > 0) {
613  workMV = MVT::Clone(*problem_->getInitVec(),1);
614  }
615  else {
616  workMV = Teuchos::null;
617  }
618  }
619 
620  // some consts and utils
621  const ScalarType ONE = SCT::one();
622  const ScalarType ZERO = SCT::zero();
625 
626  // go ahead and initialize the solution to nothing in case we throw an exception
628  sol.numVecs = 0;
629  problem_->setSolution(sol);
630 
631  int numRestarts = 0;
632 
633  // enter solve() iterations
634  {
635 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
636  Teuchos::TimeMonitor slvtimer(*_timerSolve);
637 #endif
638 
639  // tell bd_solver to iterate
640  while (1) {
641  try {
642  bd_solver->iterate();
643 
645  //
646  // check user-specified debug test; if it passed, return an exception
647  //
649  if (debugTest_ != Teuchos::null && debugTest_->getStatus() == Passed) {
650  throw AnasaziError("Anasazi::BlockDavidsonSolMgr::solve(): User-specified debug status test returned Passed.");
651  }
653  //
654  // check convergence next
655  //
657  else if (ordertest->getStatus() == Passed ) {
658  // we have convergence
659  // ordertest->whichVecs() tells us which vectors from lockvecs and solver state are the ones we want
660  // ordertest->howMany() will tell us how many
661  break;
662  }
664  //
665  // check for restarting before locking: if we need to lock, it will happen after the restart
666  //
668  else if ( bd_solver->getCurSubspaceDim() == bd_solver->getMaxSubspaceDim() ) {
669 
670 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
671  Teuchos::TimeMonitor restimer(*_timerRestarting);
672 #endif
673 
674  if ( numRestarts >= maxRestarts_ ) {
675  break; // break from while(1){bd_solver->iterate()}
676  }
677  numRestarts++;
678 
679  printer_->stream(IterationDetails) << " Performing restart number " << numRestarts << " of " << maxRestarts_ << std::endl << std::endl;
680 
681  BlockDavidsonState<ScalarType,MV> state = bd_solver->getState();
682  int curdim = state.curDim;
683  int newdim = numRestartBlocks_*blockSize_;
684 
685 # ifdef TEUCHOS_DEBUG
686  {
687  std::vector<Value<ScalarType> > ritzvalues = bd_solver->getRitzValues();
688  *out << "Ritz values from solver:\n";
689  for (unsigned int i=0; i<ritzvalues.size(); i++) {
690  *out << ritzvalues[i].realpart << " ";
691  }
692  *out << "\n";
693  }
694 # endif
695 
696  //
697  // compute eigenvectors of the projected problem
699  std::vector<MagnitudeType> theta(curdim);
700  int rank = curdim;
701 # ifdef TEUCHOS_DEBUG
702  {
703  std::stringstream os;
704  os << "KK before HEEV...\n"
705  << *state.KK << "\n";
706  *out << os.str();
707  }
708 # endif
709  int info = msutils::directSolver(curdim,*state.KK,Teuchos::null,S,theta,rank,10);
710  TEUCHOS_TEST_FOR_EXCEPTION(info != 0 ,std::logic_error,
711  "Anasazi::BlockDavidsonSolMgr::solve(): error calling SolverUtils::directSolver."); // this should never happen
712  TEUCHOS_TEST_FOR_EXCEPTION(rank != curdim,std::logic_error,
713  "Anasazi::BlockDavidsonSolMgr::solve(): direct solve did not compute all eigenvectors."); // this should never happen
714 
715 # ifdef TEUCHOS_DEBUG
716  {
717  std::stringstream os;
718  *out << "Ritz values from heev(KK):\n";
719  for (unsigned int i=0; i<theta.size(); i++) *out << theta[i] << " ";
720  os << "\nRitz vectors from heev(KK):\n"
721  << S << "\n";
722  *out << os.str();
723  }
724 # endif
725 
726  //
727  // sort the eigenvalues (so that we can order the eigenvectors)
728  {
729  std::vector<int> order(curdim);
730  sorter->sort(theta,Teuchos::rcpFromRef(order),curdim);
731  //
732  // apply the same ordering to the primitive ritz vectors
733  msutils::permuteVectors(order,S);
734  }
735 # ifdef TEUCHOS_DEBUG
736  {
737  std::stringstream os;
738  *out << "Ritz values from heev(KK) after sorting:\n";
739  std::copy(theta.begin(), theta.end(), std::ostream_iterator<ScalarType>(*out, " "));
740  os << "\nRitz vectors from heev(KK) after sorting:\n"
741  << S << "\n";
742  *out << os.str();
743  }
744 # endif
745  //
746  // select the significant primitive ritz vectors
748 # ifdef TEUCHOS_DEBUG
749  {
750  std::stringstream os;
751  os << "Significant primitive Ritz vectors:\n"
752  << Sr << "\n";
753  *out << os.str();
754  }
755 # endif
756  //
757  // generate newKK = Sr'*KKold*Sr
758  Teuchos::SerialDenseMatrix<int,ScalarType> newKK(newdim,newdim);
759  {
760  Teuchos::SerialDenseMatrix<int,ScalarType> KKtmp(curdim,newdim),
761  KKold(Teuchos::View,*state.KK,curdim,curdim);
762  int teuchosRet;
763  // KKtmp = KKold*Sr
764  teuchosRet = KKtmp.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,KKold,Sr,ZERO);
765  TEUCHOS_TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
766  "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
767  // newKK = Sr'*KKtmp = Sr'*KKold*Sr
768  teuchosRet = newKK.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,Sr,KKtmp,ZERO);
769  TEUCHOS_TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
770  "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
771  // make it Hermitian in memory
772  for (int j=0; j<newdim-1; ++j) {
773  for (int i=j+1; i<newdim; ++i) {
774  newKK(i,j) = SCT::conjugate(newKK(j,i));
775  }
776  }
777  }
778 # ifdef TEUCHOS_DEBUG
779  {
780  std::stringstream os;
781  os << "Sr'*KK*Sr:\n"
782  << newKK << "\n";
783  *out << os.str();
784  }
785 # endif
786 
787  // prepare new state
789  rstate.curDim = newdim;
790  rstate.KK = Teuchos::rcpFromRef(newKK);
791  //
792  // we know that newX = newV*Sr(:,1:bS) = oldV*S(:1:bS) = oldX
793  // the restarting preserves the Ritz vectors and residual
794  // for the Ritz values, we want all of the values associated with newV.
795  // these have already been placed at the beginning of theta
796  rstate.X = state.X;
797  rstate.KX = state.KX;
798  rstate.MX = state.MX;
799  rstate.R = state.R;
800  rstate.T = Teuchos::rcp( new std::vector<MagnitudeType>(theta.begin(),theta.begin()+newdim) );
801 
802  if (inSituRestart_ == true) {
803 # ifdef TEUCHOS_DEBUG
804  *out << "Beginning in-situ restart...\n";
805 # endif
806  //
807  // get non-const pointer to solver's basis so we can work in situ
808  Teuchos::RCP<MV> solverbasis = Teuchos::rcp_const_cast<MV>(state.V);
809  //
810  // perform Householder QR of Sr = Q [D;0], where D is unit diag.
811  // WARNING: this will overwrite Sr; however, we do not need Sr anymore after this
812  std::vector<ScalarType> tau(newdim), work(newdim);
813  int geqrf_info;
814  lapack.GEQRF(curdim,newdim,Sr.values(),Sr.stride(),&tau[0],&work[0],work.size(),&geqrf_info);
815  TEUCHOS_TEST_FOR_EXCEPTION(geqrf_info != 0,std::logic_error,
816  "Anasazi::BlockDavidsonSolMgr::solve(): error calling GEQRF during restarting.");
817  if (printer_->isVerbosity(Debug)) {
819  for (int j=0; j<newdim; j++) {
820  R(j,j) = SCT::magnitude(R(j,j)) - 1.0;
821  for (int i=j+1; i<newdim; i++) {
822  R(i,j) = ZERO;
823  }
824  }
825  printer_->stream(Debug) << "||Triangular factor of Sr - I||: " << R.normFrobenius() << std::endl;
826  }
827  //
828  // perform implicit oldV*Sr
829  // this actually performs oldV*[Sr Su*M] = [newV truncV], for some unitary M
830  // we are actually interested in only the first newdim vectors of the result
831  {
832  std::vector<int> curind(curdim);
833  for (int i=0; i<curdim; i++) curind[i] = i;
834  Teuchos::RCP<MV> oldV = MVT::CloneViewNonConst(*solverbasis,curind);
835  msutils::applyHouse(newdim,*oldV,Sr,tau,workMV);
836  }
837  //
838  // put the new basis into the state for initialize()
839  // the new basis is contained in the the first newdim columns of solverbasis
840  // initialize() will recognize that pointer bd_solver.V_ == pointer rstate.V, and will neglect the copy.
841  rstate.V = solverbasis;
842  }
843  else { // inSituRestart == false)
844 # ifdef TEUCHOS_DEBUG
845  *out << "Beginning ex-situ restart...\n";
846 # endif
847  // newV = oldV*Sr, explicitly. workspace is in workMV
848  std::vector<int> curind(curdim), newind(newdim);
849  for (int i=0; i<curdim; i++) curind[i] = i;
850  for (int i=0; i<newdim; i++) newind[i] = i;
851  Teuchos::RCP<const MV> oldV = MVT::CloneView(*state.V,curind);
852  Teuchos::RCP<MV> newV = MVT::CloneViewNonConst(*workMV ,newind);
853 
854  MVT::MvTimesMatAddMv(ONE,*oldV,Sr,ZERO,*newV);
855  //
856  // put the new basis into the state for initialize()
857  rstate.V = newV;
858  }
859 
860  //
861  // send the new state to the solver
862  bd_solver->initialize(rstate);
863  } // end of restarting
865  //
866  // check locking if we didn't converge or restart
867  //
869  else if (locktest != Teuchos::null && locktest->getStatus() == Passed) {
870 
871 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
872  Teuchos::TimeMonitor lcktimer(*_timerLocking);
873 #endif
874 
875  //
876  // get current state
877  BlockDavidsonState<ScalarType,MV> state = bd_solver->getState();
878  const int curdim = state.curDim;
879 
880  //
881  // get number,indices of vectors to be locked
882  TEUCHOS_TEST_FOR_EXCEPTION(locktest->howMany() <= 0,std::logic_error,
883  "Anasazi::BlockDavidsonSolMgr::solve(): status test mistake: howMany() non-positive.");
884  TEUCHOS_TEST_FOR_EXCEPTION(locktest->howMany() != (int)locktest->whichVecs().size(),std::logic_error,
885  "Anasazi::BlockDavidsonSolMgr::solve(): status test mistake: howMany() not consistent with whichVecs().");
886  // we should have room to lock vectors; otherwise, locking should have been deactivated
887  TEUCHOS_TEST_FOR_EXCEPTION(curNumLocked == maxLocked_,std::logic_error,
888  "Anasazi::BlockDavidsonSolMgr::solve(): status test mistake: locking not deactivated.");
889  //
890  // don't lock more than maxLocked_; we didn't allocate enough space.
891  std::vector<int> tmp_vector_int;
892  if (curNumLocked + locktest->howMany() > maxLocked_) {
893  // just use the first of them
894  tmp_vector_int.insert(tmp_vector_int.begin(),locktest->whichVecs().begin(),locktest->whichVecs().begin()+maxLocked_-curNumLocked);
895  }
896  else {
897  tmp_vector_int = locktest->whichVecs();
898  }
899  const std::vector<int> lockind(tmp_vector_int);
900  const int numNewLocked = lockind.size();
901  //
902  // generate indices of vectors left unlocked
903  // curind = [0,...,curdim-1] = UNION( lockind, unlockind )
904  const int numUnlocked = curdim-numNewLocked;
905  tmp_vector_int.resize(curdim);
906  for (int i=0; i<curdim; i++) tmp_vector_int[i] = i;
907  const std::vector<int> curind(tmp_vector_int); // curind = [0 ... curdim-1]
908  tmp_vector_int.resize(numUnlocked);
909  std::set_difference(curind.begin(),curind.end(),lockind.begin(),lockind.end(),tmp_vector_int.begin());
910  const std::vector<int> unlockind(tmp_vector_int); // unlockind = [0 ... curdim-1] - lockind
911  tmp_vector_int.clear();
912 
913  //
914  // debug printing
915  if (printer_->isVerbosity(Debug)) {
916  printer_->print(Debug,"Locking vectors: ");
917  for (unsigned int i=0; i<lockind.size(); i++) {printer_->stream(Debug) << " " << lockind[i];}
918  printer_->print(Debug,"\n");
919  printer_->print(Debug,"Not locking vectors: ");
920  for (unsigned int i=0; i<unlockind.size(); i++) {printer_->stream(Debug) << " " << unlockind[i];}
921  printer_->print(Debug,"\n");
922  }
923 
924  //
925  // we need primitive ritz vectors/values:
926  // [S,L] = eig(oldKK)
927  //
928  // this will be partitioned as follows:
929  // locked: Sl = S(lockind) // we won't actually need Sl
930  // unlocked: Su = S(unlockind)
931  //
933  std::vector<MagnitudeType> theta(curdim);
934  {
935  int rank = curdim;
936  int info = msutils::directSolver(curdim,*state.KK,Teuchos::null,S,theta,rank,10);
937  TEUCHOS_TEST_FOR_EXCEPTION(info != 0 ,std::logic_error,
938  "Anasazi::BlockDavidsonSolMgr::solve(): error calling SolverUtils::directSolver."); // this should never happen
939  TEUCHOS_TEST_FOR_EXCEPTION(rank != curdim,std::logic_error,
940  "Anasazi::BlockDavidsonSolMgr::solve(): direct solve did not compute all eigenvectors."); // this should never happen
941  //
942  // sort the eigenvalues (so that we can order the eigenvectors)
943  std::vector<int> order(curdim);
944  sorter->sort(theta,Teuchos::rcpFromRef(order),curdim);
945  //
946  // apply the same ordering to the primitive ritz vectors
947  msutils::permuteVectors(order,S);
948  }
949  //
950  // select the unlocked ritz vectors
951  // the indexing in unlockind is relative to the ordered primitive ritz vectors
952  // (this is why we ordered theta,S above)
953  Teuchos::SerialDenseMatrix<int,ScalarType> Su(curdim,numUnlocked);
954  for (int i=0; i<numUnlocked; i++) {
955  blas.COPY(curdim, S[unlockind[i]], 1, Su[i], 1);
956  }
957 
958 
959  //
960  // newV has the following form:
961  // newV = [defV augV]
962  // - defV will be of size curdim - numNewLocked, and contain the generated basis: defV = oldV*Su
963  // - augV will be of size numNewLocked, and contain random directions to make up for the lost space
964  //
965  // we will need a pointer to defV below to generate the off-diagonal block of newKK
966  // go ahead and setup pointer to augV
967  //
968  Teuchos::RCP<MV> defV, augV;
969  if (inSituRestart_ == true) {
970  //
971  // get non-const pointer to solver's basis so we can work in situ
972  Teuchos::RCP<MV> solverbasis = Teuchos::rcp_const_cast<MV>(state.V);
973  //
974  // perform Householder QR of Su = Q [D;0], where D is unit diag.
975  // work on a copy of Su, since we need Su below to build newKK
977  std::vector<ScalarType> tau(numUnlocked), work(numUnlocked);
978  int info;
979  lapack.GEQRF(curdim,numUnlocked,copySu.values(),copySu.stride(),&tau[0],&work[0],work.size(),&info);
980  TEUCHOS_TEST_FOR_EXCEPTION(info != 0,std::logic_error,
981  "Anasazi::BlockDavidsonSolMgr::solve(): error calling GEQRF during restarting.");
982  if (printer_->isVerbosity(Debug)) {
983  Teuchos::SerialDenseMatrix<int,ScalarType> R(Teuchos::Copy,copySu,numUnlocked,numUnlocked);
984  for (int j=0; j<numUnlocked; j++) {
985  R(j,j) = SCT::magnitude(R(j,j)) - 1.0;
986  for (int i=j+1; i<numUnlocked; i++) {
987  R(i,j) = ZERO;
988  }
989  }
990  printer_->stream(Debug) << "||Triangular factor of Su - I||: " << R.normFrobenius() << std::endl;
991  }
992  //
993  // perform implicit oldV*Su
994  // this actually performs oldV*[Su Sl*M] = [defV lockV], for some unitary M
995  // we are actually interested in only the first numUnlocked vectors of the result
996  {
997  Teuchos::RCP<MV> oldV = MVT::CloneViewNonConst(*solverbasis,curind);
998  msutils::applyHouse(numUnlocked,*oldV,copySu,tau,workMV);
999  }
1000  std::vector<int> defind(numUnlocked), augind(numNewLocked);
1001  for (int i=0; i<numUnlocked ; i++) defind[i] = i;
1002  for (int i=0; i<numNewLocked; i++) augind[i] = numUnlocked+i;
1003  defV = MVT::CloneViewNonConst(*solverbasis,defind);
1004  augV = MVT::CloneViewNonConst(*solverbasis,augind);
1005  }
1006  else { // inSituRestart == false)
1007  // defV = oldV*Su, explicitly. workspace is in workMV
1008  std::vector<int> defind(numUnlocked), augind(numNewLocked);
1009  for (int i=0; i<numUnlocked ; i++) defind[i] = i;
1010  for (int i=0; i<numNewLocked; i++) augind[i] = numUnlocked+i;
1011  Teuchos::RCP<const MV> oldV = MVT::CloneView(*state.V,curind);
1012  defV = MVT::CloneViewNonConst(*workMV,defind);
1013  augV = MVT::CloneViewNonConst(*workMV,augind);
1014 
1015  MVT::MvTimesMatAddMv(ONE,*oldV,Su,ZERO,*defV);
1016  }
1017 
1018  //
1019  // lockvecs will be partitioned as follows:
1020  // lockvecs = [curlocked augTmp ...]
1021  // - augTmp will be used for the storage of M*augV and K*augV
1022  // later, the locked vectors (stored in state.X and referenced via const MV view newLocked)
1023  // will be moved into lockvecs on top of augTmp when it is no longer needed as workspace.
1024  // - curlocked will be used in orthogonalization of augV
1025  //
1026  // newL is the new locked vectors; newL = oldV*Sl = RitzVectors(lockind)
1027  // we will not produce them, but instead retrieve them from RitzVectors
1028  //
1029  Teuchos::RCP<const MV> curlocked, newLocked;
1030  Teuchos::RCP<MV> augTmp;
1031  {
1032  // setup curlocked
1033  if (curNumLocked > 0) {
1034  std::vector<int> curlockind(curNumLocked);
1035  for (int i=0; i<curNumLocked; i++) curlockind[i] = i;
1036  curlocked = MVT::CloneView(*lockvecs,curlockind);
1037  }
1038  else {
1039  curlocked = Teuchos::null;
1040  }
1041  // setup augTmp
1042  std::vector<int> augtmpind(numNewLocked);
1043  for (int i=0; i<numNewLocked; i++) augtmpind[i] = curNumLocked+i;
1044  augTmp = MVT::CloneViewNonConst(*lockvecs,augtmpind);
1045  // setup newLocked
1046  newLocked = MVT::CloneView(*bd_solver->getRitzVectors(),lockind);
1047  }
1048 
1049  //
1050  // generate augV and perform orthogonalization
1051  //
1052  MVT::MvRandom(*augV);
1053  //
1054  // orthogonalize it against auxvecs, defV, and all locked vectors (new and current)
1055  // use augTmp as storage for M*augV, if hasM
1056  {
1059  if (probauxvecs != Teuchos::null) against.push_back(probauxvecs);
1060  if (curlocked != Teuchos::null) against.push_back(curlocked);
1061  against.push_back(newLocked);
1062  against.push_back(defV);
1063  if (problem_->getM() != Teuchos::null) {
1064  OPT::Apply(*problem_->getM(),*augV,*augTmp);
1065  }
1066  ortho->projectAndNormalizeMat(*augV,against,dummyC,Teuchos::null,augTmp);
1067  }
1068 
1069  //
1070  // form newKK
1071  //
1072  // newKK = newV'*K*newV = [Su'*KK*Su defV'*K*augV]
1073  // [augV'*K*defV augV'*K*augV]
1074  //
1075  // first, generate the principal submatrix, the projection of K onto the unlocked portion of oldV
1076  //
1077  Teuchos::SerialDenseMatrix<int,ScalarType> newKK(curdim,curdim);
1078  {
1079  Teuchos::SerialDenseMatrix<int,ScalarType> KKtmp(curdim,numUnlocked),
1080  KKold(Teuchos::View,*state.KK,curdim,curdim),
1081  KK11(Teuchos::View,newKK,numUnlocked,numUnlocked);
1082  int teuchosRet;
1083  // KKtmp = KKold*Su
1084  teuchosRet = KKtmp.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,KKold,Su,ZERO);
1085  TEUCHOS_TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
1086  "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
1087  // KK11 = Su'*KKtmp = Su'*KKold*Su
1088  teuchosRet = KK11.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,Su,KKtmp,ZERO);
1089  TEUCHOS_TEST_FOR_EXCEPTION(teuchosRet != 0,std::logic_error,
1090  "Anasazi::BlockDavidsonSolMgr::solve(): Logic error calling SerialDenseMatrix::multiply.");
1091  }
1092  //
1093  // project the stiffness matrix on augV
1094  {
1095  OPT::Apply(*problem_->getOperator(),*augV,*augTmp);
1096  Teuchos::SerialDenseMatrix<int,ScalarType> KK12(Teuchos::View,newKK,numUnlocked,numNewLocked,0,numUnlocked),
1097  KK22(Teuchos::View,newKK,numNewLocked,numNewLocked,numUnlocked,numUnlocked);
1098  MVT::MvTransMv(ONE,*defV,*augTmp,KK12);
1099  MVT::MvTransMv(ONE,*augV,*augTmp,KK22);
1100  }
1101  //
1102  // done with defV,augV
1103  defV = Teuchos::null;
1104  augV = Teuchos::null;
1105  //
1106  // make it hermitian in memory (fill in KK21)
1107  for (int j=0; j<curdim; ++j) {
1108  for (int i=j+1; i<curdim; ++i) {
1109  newKK(i,j) = SCT::conjugate(newKK(j,i));
1110  }
1111  }
1112  //
1113  // we are done using augTmp as storage
1114  // put newLocked into lockvecs, new values into lockvals
1115  augTmp = Teuchos::null;
1116  {
1117  std::vector<Value<ScalarType> > allvals = bd_solver->getRitzValues();
1118  for (int i=0; i<numNewLocked; i++) {
1119  lockvals.push_back(allvals[lockind[i]].realpart);
1120  }
1121 
1122  std::vector<int> indlock(numNewLocked);
1123  for (int i=0; i<numNewLocked; i++) indlock[i] = curNumLocked+i;
1124  MVT::SetBlock(*newLocked,indlock,*lockvecs);
1125  newLocked = Teuchos::null;
1126 
1127  curNumLocked += numNewLocked;
1128  std::vector<int> curlockind(curNumLocked);
1129  for (int i=0; i<curNumLocked; i++) curlockind[i] = i;
1130  curlocked = MVT::CloneView(*lockvecs,curlockind);
1131  }
1132  // add locked vecs as aux vecs, along with aux vecs from problem
1133  // add lockvals to ordertest
1134  // disable locktest if curNumLocked == maxLocked
1135  {
1136  ordertest->setAuxVals(lockvals);
1137 
1139  if (probauxvecs != Teuchos::null) aux.push_back(probauxvecs);
1140  aux.push_back(curlocked);
1141  bd_solver->setAuxVecs(aux);
1142 
1143  if (curNumLocked == maxLocked_) {
1144  // disabled locking now
1145  combotest->removeTest(locktest);
1146  }
1147  }
1148 
1149  //
1150  // prepare new state
1152  rstate.curDim = curdim;
1153  if (inSituRestart_) {
1154  // data is already in the solver's memory
1155  rstate.V = state.V;
1156  }
1157  else {
1158  // data is in workspace and will be copied to solver memory
1159  rstate.V = workMV;
1160  }
1161  rstate.KK = Teuchos::rcpFromRef(newKK);
1162  //
1163  // pass new state to the solver
1164  bd_solver->initialize(rstate);
1165  } // end of locking
1167  //
1168  // we returned from iterate(), but none of our status tests Passed.
1169  // something is wrong, and it is probably our fault.
1170  //
1172  else {
1173  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): Invalid return from bd_solver::iterate().");
1174  }
1175  }
1176  catch (const AnasaziError &err) {
1177  printer_->stream(Errors)
1178  << "Anasazi::BlockDavidsonSolMgr::solve() caught unexpected exception from Anasazi::BlockDavidson::iterate() at iteration " << bd_solver->getNumIters() << std::endl
1179  << err.what() << std::endl
1180  << "Anasazi::BlockDavidsonSolMgr::solve() returning Unconverged with no solutions." << std::endl;
1181  return Unconverged;
1182  }
1183  }
1184 
1185  // clear temp space
1186  workMV = Teuchos::null;
1187 
1188  sol.numVecs = ordertest->howMany();
1189  if (sol.numVecs > 0) {
1190  sol.Evecs = MVT::Clone(*problem_->getInitVec(),sol.numVecs);
1191  sol.Espace = sol.Evecs;
1192  sol.Evals.resize(sol.numVecs);
1193  std::vector<MagnitudeType> vals(sol.numVecs);
1194 
1195  // copy them into the solution
1196  std::vector<int> which = ordertest->whichVecs();
1197  // indices between [0,blockSize) refer to vectors/values in the solver
1198  // indices between [-curNumLocked,-1] refer to locked vectors/values [0,curNumLocked)
1199  // everything has already been ordered by the solver; we just have to partition the two references
1200  std::vector<int> inlocked(0), insolver(0);
1201  for (unsigned int i=0; i<which.size(); i++) {
1202  if (which[i] >= 0) {
1203  TEUCHOS_TEST_FOR_EXCEPTION(which[i] >= blockSize_,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): positive indexing mistake from ordertest.");
1204  insolver.push_back(which[i]);
1205  }
1206  else {
1207  // sanity check
1208  TEUCHOS_TEST_FOR_EXCEPTION(which[i] < -curNumLocked,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): negative indexing mistake from ordertest.");
1209  inlocked.push_back(which[i] + curNumLocked);
1210  }
1211  }
1212 
1213  TEUCHOS_TEST_FOR_EXCEPTION(insolver.size() + inlocked.size() != (unsigned int)sol.numVecs,std::logic_error,"Anasazi::BlockDavidsonSolMgr::solve(): indexing mistake.");
1214 
1215  // set the vecs,vals in the solution
1216  if (insolver.size() > 0) {
1217  // set vecs
1218  int lclnum = insolver.size();
1219  std::vector<int> tosol(lclnum);
1220  for (int i=0; i<lclnum; i++) tosol[i] = i;
1221  Teuchos::RCP<const MV> v = MVT::CloneView(*bd_solver->getRitzVectors(),insolver);
1222  MVT::SetBlock(*v,tosol,*sol.Evecs);
1223  // set vals
1224  std::vector<Value<ScalarType> > fromsolver = bd_solver->getRitzValues();
1225  for (unsigned int i=0; i<insolver.size(); i++) {
1226  vals[i] = fromsolver[insolver[i]].realpart;
1227  }
1228  }
1229 
1230  // get the vecs,vals from locked storage
1231  if (inlocked.size() > 0) {
1232  int solnum = insolver.size();
1233  // set vecs
1234  int lclnum = inlocked.size();
1235  std::vector<int> tosol(lclnum);
1236  for (int i=0; i<lclnum; i++) tosol[i] = solnum + i;
1237  Teuchos::RCP<const MV> v = MVT::CloneView(*lockvecs,inlocked);
1238  MVT::SetBlock(*v,tosol,*sol.Evecs);
1239  // set vals
1240  for (unsigned int i=0; i<inlocked.size(); i++) {
1241  vals[i+solnum] = lockvals[inlocked[i]];
1242  }
1243  }
1244 
1245  // sort the eigenvalues and permute the eigenvectors appropriately
1246  {
1247  std::vector<int> order(sol.numVecs);
1248  sorter->sort(vals,Teuchos::rcpFromRef(order),sol.numVecs);
1249  // store the values in the Eigensolution
1250  for (int i=0; i<sol.numVecs; i++) {
1251  sol.Evals[i].realpart = vals[i];
1252  sol.Evals[i].imagpart = MT::zero();
1253  }
1254  // now permute the eigenvectors according to order
1255  msutils::permuteVectors(sol.numVecs,order,*sol.Evecs);
1256  }
1257 
1258  // setup sol.index, remembering that all eigenvalues are real so that index = {0,...,0}
1259  sol.index.resize(sol.numVecs,0);
1260  }
1261  }
1262 
1263  // print final summary
1264  bd_solver->currentStatus(printer_->stream(FinalSummary));
1265 
1266  // print timing information
1267 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1268  if ( printer_->isVerbosity( TimingDetails ) ) {
1269  Teuchos::TimeMonitor::summarize( printer_->stream( TimingDetails ) );
1270  }
1271 #endif
1272 
1273  problem_->setSolution(sol);
1274  printer_->stream(Debug) << "Returning " << sol.numVecs << " eigenpairs to eigenproblem." << std::endl;
1275 
1276  // get the number of iterations taken for this call to solve().
1277  numIters_ = bd_solver->getNumIters();
1278 
1279  if (sol.numVecs < nev) {
1280  return Unconverged; // return from BlockDavidsonSolMgr::solve()
1281  }
1282  return Converged; // return from BlockDavidsonSolMgr::solve()
1283 }
1284 
1285 
1286 template <class ScalarType, class MV, class OP>
1287 void
1289  const Teuchos::RCP< StatusTest<ScalarType,MV,OP> > &global)
1290 {
1291  globalTest_ = global;
1292 }
1293 
1294 template <class ScalarType, class MV, class OP>
1297 {
1298  return globalTest_;
1299 }
1300 
1301 template <class ScalarType, class MV, class OP>
1302 void
1305 {
1306  debugTest_ = debug;
1307 }
1308 
1309 template <class ScalarType, class MV, class OP>
1312 {
1313  return debugTest_;
1314 }
1315 
1316 template <class ScalarType, class MV, class OP>
1317 void
1319  const Teuchos::RCP< StatusTest<ScalarType,MV,OP> > &locking)
1320 {
1321  lockingTest_ = locking;
1322 }
1323 
1324 template <class ScalarType, class MV, class OP>
1327 {
1328  return lockingTest_;
1329 }
1330 
1331 } // end Anasazi namespace
1332 
1333 #endif /* ANASAZI_BLOCKDAVIDSON_SOLMGR_HPP */
ScalarType * values() const
Pure virtual base class which describes the basic interface for a solver manager. ...
std::vector< Value< ScalarType > > Evals
The computed eigenvalues.
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
void setLockingStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &locking)
Set the status test defining locking.
A special StatusTest for printing other status tests.
BlockDavidsonSolMgr(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, Teuchos::ParameterList &pl)
Basic constructor for BlockDavidsonSolMgr.
This class defines the interface required by an eigensolver and status test class to compute solution...
static ptrdiff_t GetGlobalLength(const MV &mv)
Teuchos::RCP< const MV > R
The current residual vectors.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > KK
The current projected K matrix.
An implementation of the Anasazi::SortManager that performs a collection of common sorting techniques...
basic_FancyOStream & setShowProcRank(const bool showProcRank)
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
void GEQRF(const OrdinalType m, const OrdinalType n, ScalarType *A, const OrdinalType lda, ScalarType *TAU, ScalarType *WORK, const OrdinalType lwork, OrdinalType *info) const
void COPY(const OrdinalType n, const ScalarType *x, const OrdinalType incx, ScalarType *y, const OrdinalType incy) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Virtual base class which defines basic traits for the operator type.
Teuchos::RCP< MV > Evecs
The computed eigenvectors.
Status test for forming logical combinations of other status tests.
Teuchos::RCP< const MV > KX
The image of the current eigenvectors under K.
The Anasazi::SolverManager is a templated virtual base class that defines the basic interface that an...
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > & getDebugStatusTest() const
Get the status test for debugging.
Basic implementation of the Anasazi::SortManager class.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using the SVQB iter...
An exception class parent to all Anasazi exceptions.
This class implements a Block Davidson iteration, a preconditioned iteration for solving linear Hermi...
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Implementation of the block Davidson method.
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
Anasazi&#39;s templated, static class providing utilities for the solvers.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
bool isParameter(const std::string &name) const
int numVecs
The number of computed eigenpairs.
int getNumIters() const
Get the iteration count for the most recent call to solve().
Basic output manager for sending information of select verbosity levels to the appropriate output str...
Teuchos::RCP< MV > Espace
An orthonormal basis for the computed eigenspace.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Anasazi&#39;s basic output manager for sending information of select verbosity levels to the appropriate ...
Abstract base class which defines the interface required by an eigensolver and status test class to c...
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
ReturnType
Enumerated type used to pass back information from a solver manager.
Teuchos::RCP< const MV > V
The basis for the Krylov space.
A status test for testing the norm of the eigenvectors residuals.
Traits class which defines basic operations on multivectors.
std::vector< int > index
An index into Evecs to allow compressed storage of eigenvectors for real, non-Hermitian problems...
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > & getLockingStatusTest() const
Get the status test defining locking.
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
Orthogonalization manager based on the SVQB technique described in &quot;A Block Orthogonalization Procedu...
Struct for storing an eigenproblem solution.
int curDim
The current dimension of the solver.
void push_back(const value_type &x)
Structure to contain pointers to BlockDavidson state variables.
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > & getGlobalStatusTest() const
Get the status test defining global convergence.
Special StatusTest for printing status tests.
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
void setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &debug)
Set the status test for debugging.
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.
Status test for forming logical combinations of other status tests.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using (potentially)...
Types and exceptions used within Anasazi solvers and interfaces.
The BlockDavidsonSolMgr provides a powerful solver manager over the BlockDavidson eigensolver...
A pure virtual class for defining the status tests for the Anasazi iterative solvers.
A status test for testing the norm of the eigenvectors residuals.
Basic implementation of the Anasazi::OrthoManager class.
void setGlobalStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &global)
Set the status test defining global convergence.
ReturnType solve()
This method performs possibly repeated calls to the underlying eigensolver&#39;s iterate() routine until ...
OrdinalType stride() const
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Return the eigenvalue problem.
Teuchos::RCP< const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > T
The current Ritz values. This vector is a copy of the internal data.
Class which provides internal utilities for the Anasazi solvers.