Anasazi  Version of the Day
AnasaziBlockDavidson.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 
33 #ifndef ANASAZI_BLOCK_DAVIDSON_HPP
34 #define ANASAZI_BLOCK_DAVIDSON_HPP
35 
36 #include "AnasaziTypes.hpp"
37 
38 #include "AnasaziEigensolver.hpp"
41 #include "Teuchos_ScalarTraits.hpp"
42 
44 #include "AnasaziSolverUtils.hpp"
45 
46 #include "Teuchos_LAPACK.hpp"
47 #include "Teuchos_BLAS.hpp"
50 #include "Teuchos_TimeMonitor.hpp"
51 
67 namespace Anasazi {
68 
70 
71 
76  template <class ScalarType, class MV>
82  int curDim;
109  BlockDavidsonState() : curDim(0), V(Teuchos::null),
110  X(Teuchos::null), KX(Teuchos::null), MX(Teuchos::null),
111  R(Teuchos::null), H(Teuchos::null),
112  T(Teuchos::null), KK(Teuchos::null) {}
113  };
114 
116 
118 
119 
132  class BlockDavidsonInitFailure : public AnasaziError {public:
133  BlockDavidsonInitFailure(const std::string& what_arg) : AnasaziError(what_arg)
134  {}};
135 
143  class BlockDavidsonOrthoFailure : public AnasaziError {public:
144  BlockDavidsonOrthoFailure(const std::string& what_arg) : AnasaziError(what_arg)
145  {}};
146 
148 
149 
150  template <class ScalarType, class MV, class OP>
151  class BlockDavidson : public Eigensolver<ScalarType,MV,OP> {
152  public:
154 
155 
165  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
168  Teuchos::ParameterList &params
169  );
170 
172  virtual ~BlockDavidson();
174 
175 
177 
178 
202  void iterate();
203 
237  void initialize(BlockDavidsonState<ScalarType,MV> newstate);
238 
242  void initialize();
243 
259  bool isInitialized() const;
260 
273  BlockDavidsonState<ScalarType,MV> getState() const;
274 
276 
277 
279 
280 
282  int getNumIters() const;
283 
285  void resetNumIters();
286 
294  Teuchos::RCP<const MV> getRitzVectors();
295 
301  std::vector<Value<ScalarType> > getRitzValues();
302 
303 
311  std::vector<int> getRitzIndex();
312 
313 
319  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getResNorms();
320 
321 
327  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getRes2Norms();
328 
329 
337  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getRitzRes2Norms();
338 
344  int getCurSubspaceDim() const;
345 
347  int getMaxSubspaceDim() const;
348 
350 
351 
353 
354 
356  void setStatusTest(Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test);
357 
359  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > getStatusTest() const;
360 
362  const Eigenproblem<ScalarType,MV,OP>& getProblem() const;
363 
373  void setBlockSize(int blockSize);
374 
376  int getBlockSize() const;
377 
390  void setAuxVecs(const Teuchos::Array<Teuchos::RCP<const MV> > &auxvecs);
391 
393  Teuchos::Array<Teuchos::RCP<const MV> > getAuxVecs() const;
394 
396 
398 
399 
409  void setSize(int blockSize, int numBlocks);
410 
412 
414 
415 
417  void currentStatus(std::ostream &os);
418 
420 
421  private:
422  //
423  // Convenience typedefs
424  //
430  typedef typename SCT::magnitudeType MagnitudeType;
431  const MagnitudeType ONE;
432  const MagnitudeType ZERO;
433  const MagnitudeType NANVAL;
434  //
435  // Internal structs
436  //
437  struct CheckList {
438  bool checkV;
439  bool checkX, checkMX, checkKX;
440  bool checkH, checkMH, checkKH;
441  bool checkR, checkQ;
442  bool checkKK;
443  CheckList() : checkV(false),
444  checkX(false),checkMX(false),checkKX(false),
445  checkH(false),checkMH(false),checkKH(false),
446  checkR(false),checkQ(false),checkKK(false) {};
447  };
448  //
449  // Internal methods
450  //
451  std::string accuracyCheck(const CheckList &chk, const std::string &where) const;
452  //
453  // Classes inputed through constructor that define the eigenproblem to be solved.
454  //
460  //
461  // Information obtained from the eigenproblem
462  //
466  bool hasM_;
467  //
468  // Internal timers
469  //
470  Teuchos::RCP<Teuchos::Time> timerOp_, timerMOp_, timerPrec_,
471  timerSortEval_, timerDS_,
472  timerLocal_, timerCompRes_,
473  timerOrtho_, timerInit_;
474  //
475  // Counters
476  //
477  int count_ApplyOp_, count_ApplyM_, count_ApplyPrec_;
478 
479  //
480  // Algorithmic parameters.
481  //
482  // blockSize_ is the solver block size; it controls the number of eigenvectors that
483  // we compute, the number of residual vectors that we compute, and therefore the number
484  // of vectors added to the basis on each iteration.
485  int blockSize_;
486  // numBlocks_ is the size of the allocated space for the Krylov basis, in blocks.
487  int numBlocks_;
488 
489  //
490  // Current solver state
491  //
492  // initialized_ specifies that the basis vectors have been initialized and the iterate() routine
493  // is capable of running; _initialize is controlled by the initialize() member method
494  // For the implications of the state of initialized_, please see documentation for initialize()
495  bool initialized_;
496  //
497  // curDim_ reflects how much of the current basis is valid
498  // NOTE: 0 <= curDim_ <= blockSize_*numBlocks_
499  // this also tells us how many of the values in theta_ are valid Ritz values
500  int curDim_;
501  //
502  // State Multivecs
503  // H_,KH_,MH_ will not own any storage
504  // H_ will occasionally point at the current block of vectors in the basis V_
505  // MH_,KH_ will occasionally point at MX_,KX_ when they are used as temporary storage
506  Teuchos::RCP<MV> X_, KX_, MX_, R_,
507  H_, KH_, MH_,
508  V_;
509  //
510  // Projected matrices
511  //
513  //
514  // auxiliary vectors
516  int numAuxVecs_;
517  //
518  // Number of iterations that have been performed.
519  int iter_;
520  //
521  // Current eigenvalues, residual norms
522  std::vector<MagnitudeType> theta_, Rnorms_, R2norms_;
523  //
524  // are the residual norms current with the residual?
525  bool Rnorms_current_, R2norms_current_;
526 
527  };
528 
531  //
532  // Implementations
533  //
536 
537 
539  // Constructor
540  template <class ScalarType, class MV, class OP>
544  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
547  Teuchos::ParameterList &params
548  ) :
549  ONE(Teuchos::ScalarTraits<MagnitudeType>::one()),
550  ZERO(Teuchos::ScalarTraits<MagnitudeType>::zero()),
551  NANVAL(Teuchos::ScalarTraits<MagnitudeType>::nan()),
552  // problem, tools
553  problem_(problem),
554  sm_(sorter),
555  om_(printer),
556  tester_(tester),
557  orthman_(ortho),
558  // timers, counters
559 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
560  timerOp_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Operation Op*x")),
561  timerMOp_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Operation M*x")),
562  timerPrec_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Operation Prec*x")),
563  timerSortEval_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Sorting eigenvalues")),
564  timerDS_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Direct solve")),
565  timerLocal_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Local update")),
566  timerCompRes_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Computing residuals")),
567  timerOrtho_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Orthogonalization")),
568  timerInit_(Teuchos::TimeMonitor::getNewTimer("Anasazi: BlockDavidson::Initialization")),
569 #endif
570  count_ApplyOp_(0),
571  count_ApplyM_(0),
572  count_ApplyPrec_(0),
573  // internal data
574  blockSize_(0),
575  numBlocks_(0),
576  initialized_(false),
577  curDim_(0),
578  auxVecs_( Teuchos::Array<Teuchos::RCP<const MV> >(0) ),
579  numAuxVecs_(0),
580  iter_(0),
581  Rnorms_current_(false),
582  R2norms_current_(false)
583  {
584  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,std::invalid_argument,
585  "Anasazi::BlockDavidson::constructor: user passed null problem pointer.");
586  TEUCHOS_TEST_FOR_EXCEPTION(sm_ == Teuchos::null,std::invalid_argument,
587  "Anasazi::BlockDavidson::constructor: user passed null sort manager pointer.");
588  TEUCHOS_TEST_FOR_EXCEPTION(om_ == Teuchos::null,std::invalid_argument,
589  "Anasazi::BlockDavidson::constructor: user passed null output manager pointer.");
590  TEUCHOS_TEST_FOR_EXCEPTION(tester_ == Teuchos::null,std::invalid_argument,
591  "Anasazi::BlockDavidson::constructor: user passed null status test pointer.");
592  TEUCHOS_TEST_FOR_EXCEPTION(orthman_ == Teuchos::null,std::invalid_argument,
593  "Anasazi::BlockDavidson::constructor: user passed null orthogonalization manager pointer.");
594  TEUCHOS_TEST_FOR_EXCEPTION(problem_->isProblemSet() == false, std::invalid_argument,
595  "Anasazi::BlockDavidson::constructor: problem is not set.");
596  TEUCHOS_TEST_FOR_EXCEPTION(problem_->isHermitian() == false, std::invalid_argument,
597  "Anasazi::BlockDavidson::constructor: problem is not hermitian.");
598 
599  // get the problem operators
600  Op_ = problem_->getOperator();
601  TEUCHOS_TEST_FOR_EXCEPTION(Op_ == Teuchos::null, std::invalid_argument,
602  "Anasazi::BlockDavidson::constructor: problem provides no operator.");
603  MOp_ = problem_->getM();
604  Prec_ = problem_->getPrec();
605  hasM_ = (MOp_ != Teuchos::null);
606 
607  // set the block size and allocate data
608  int bs = params.get("Block Size", problem_->getNEV());
609  int nb = params.get("Num Blocks", 2);
610  setSize(bs,nb);
611  }
612 
613 
615  // Destructor
616  template <class ScalarType, class MV, class OP>
618 
619 
621  // Set the block size
622  // This simply calls setSize(), modifying the block size while retaining the number of blocks.
623  template <class ScalarType, class MV, class OP>
625  {
626  setSize(blockSize,numBlocks_);
627  }
628 
629 
631  // Return the current auxiliary vectors
632  template <class ScalarType, class MV, class OP>
634  return auxVecs_;
635  }
636 
637 
638 
640  // return the current block size
641  template <class ScalarType, class MV, class OP>
643  return(blockSize_);
644  }
645 
646 
648  // return eigenproblem
649  template <class ScalarType, class MV, class OP>
651  return(*problem_);
652  }
653 
654 
656  // return max subspace dim
657  template <class ScalarType, class MV, class OP>
659  return blockSize_*numBlocks_;
660  }
661 
663  // return current subspace dim
664  template <class ScalarType, class MV, class OP>
666  if (!initialized_) return 0;
667  return curDim_;
668  }
669 
670 
672  // return ritz residual 2-norms
673  template <class ScalarType, class MV, class OP>
674  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
676  return this->getRes2Norms();
677  }
678 
679 
681  // return ritz index
682  template <class ScalarType, class MV, class OP>
684  std::vector<int> ret(curDim_,0);
685  return ret;
686  }
687 
688 
690  // return ritz values
691  template <class ScalarType, class MV, class OP>
692  std::vector<Value<ScalarType> > BlockDavidson<ScalarType,MV,OP>::getRitzValues() {
693  std::vector<Value<ScalarType> > ret(curDim_);
694  for (int i=0; i<curDim_; ++i) {
695  ret[i].realpart = theta_[i];
696  ret[i].imagpart = ZERO;
697  }
698  return ret;
699  }
700 
701 
703  // return pointer to ritz vectors
704  template <class ScalarType, class MV, class OP>
706  return X_;
707  }
708 
709 
711  // reset number of iterations
712  template <class ScalarType, class MV, class OP>
714  iter_=0;
715  }
716 
717 
719  // return number of iterations
720  template <class ScalarType, class MV, class OP>
722  return(iter_);
723  }
724 
725 
727  // return state pointers
728  template <class ScalarType, class MV, class OP>
731  state.curDim = curDim_;
732  state.V = V_;
733  state.X = X_;
734  state.KX = KX_;
735  if (hasM_) {
736  state.MX = MX_;
737  }
738  else {
739  state.MX = Teuchos::null;
740  }
741  state.R = R_;
742  state.H = H_;
743  state.KK = KK_;
744  if (curDim_ > 0) {
745  state.T = Teuchos::rcp(new std::vector<MagnitudeType>(theta_.begin(),theta_.begin()+curDim_) );
746  } else {
747  state.T = Teuchos::rcp(new std::vector<MagnitudeType>(0));
748  }
749  return state;
750  }
751 
752 
754  // Return initialized state
755  template <class ScalarType, class MV, class OP>
756  bool BlockDavidson<ScalarType,MV,OP>::isInitialized() const { return initialized_; }
757 
758 
760  // Set the block size and make necessary adjustments.
761  template <class ScalarType, class MV, class OP>
762  void BlockDavidson<ScalarType,MV,OP>::setSize (int blockSize, int numBlocks)
763  {
764  // time spent here counts towards timerInit_
765 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
766  Teuchos::TimeMonitor initimer( *timerInit_ );
767 #endif
768 
769  // This routine only allocates space; it doesn't not perform any computation
770  // any change in size will invalidate the state of the solver.
771 
772  TEUCHOS_TEST_FOR_EXCEPTION(blockSize < 1, std::invalid_argument, "Anasazi::BlockDavidson::setSize(blocksize,numblocks): blocksize must be strictly positive.");
773  TEUCHOS_TEST_FOR_EXCEPTION(numBlocks < 2, std::invalid_argument, "Anasazi::BlockDavidson::setSize(blocksize,numblocks): numblocks must be greater than one.");
774  if (blockSize == blockSize_ && numBlocks == numBlocks_) {
775  // do nothing
776  return;
777  }
778 
779  blockSize_ = blockSize;
780  numBlocks_ = numBlocks;
781 
783  // grab some Multivector to Clone
784  // in practice, getInitVec() should always provide this, but it is possible to use a
785  // Eigenproblem with nothing in getInitVec() by manually initializing with initialize();
786  // in case of that strange scenario, we will try to Clone from X_ first, then resort to getInitVec()
787  if (X_ != Teuchos::null) { // this is equivalent to blockSize_ > 0
788  tmp = X_;
789  }
790  else {
791  tmp = problem_->getInitVec();
792  TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
793  "Anasazi::BlockDavidson::setSize(): eigenproblem did not specify initial vectors to clone from.");
794  }
795 
796  TEUCHOS_TEST_FOR_EXCEPTION(numAuxVecs_+blockSize*static_cast<ptrdiff_t>(numBlocks) > MVText::GetGlobalLength(*tmp),std::invalid_argument,
797  "Anasazi::BlockDavidson::setSize(): max subspace dimension and auxilliary subspace too large.");
798 
799 
801  // blockSize dependent
802  //
803  // grow/allocate vectors
804  Rnorms_.resize(blockSize_,NANVAL);
805  R2norms_.resize(blockSize_,NANVAL);
806  //
807  // clone multivectors off of tmp
808  //
809  // free current allocation first, to make room for new allocation
810  X_ = Teuchos::null;
811  KX_ = Teuchos::null;
812  MX_ = Teuchos::null;
813  R_ = Teuchos::null;
814  V_ = Teuchos::null;
815 
816  om_->print(Debug," >> Allocating X_\n");
817  X_ = MVT::Clone(*tmp,blockSize_);
818  om_->print(Debug," >> Allocating KX_\n");
819  KX_ = MVT::Clone(*tmp,blockSize_);
820  if (hasM_) {
821  om_->print(Debug," >> Allocating MX_\n");
822  MX_ = MVT::Clone(*tmp,blockSize_);
823  }
824  else {
825  MX_ = X_;
826  }
827  om_->print(Debug," >> Allocating R_\n");
828  R_ = MVT::Clone(*tmp,blockSize_);
829 
831  // blockSize*numBlocks dependent
832  //
833  int newsd = blockSize_*numBlocks_;
834  theta_.resize(blockSize_*numBlocks_,NANVAL);
835  om_->print(Debug," >> Allocating V_\n");
836  V_ = MVT::Clone(*tmp,newsd);
838 
839  om_->print(Debug," >> done allocating.\n");
840 
841  initialized_ = false;
842  curDim_ = 0;
843  }
844 
845 
847  // Set the auxiliary vectors
848  template <class ScalarType, class MV, class OP>
850  typedef typename Teuchos::Array<Teuchos::RCP<const MV> >::iterator tarcpmv;
851 
852  // set new auxiliary vectors
853  auxVecs_ = auxvecs;
854  numAuxVecs_ = 0;
855  for (tarcpmv i=auxVecs_.begin(); i != auxVecs_.end(); ++i) {
856  numAuxVecs_ += MVT::GetNumberVecs(**i);
857  }
858 
859  // If the solver has been initialized, V is not necessarily orthogonal to new auxiliary vectors
860  if (numAuxVecs_ > 0 && initialized_) {
861  initialized_ = false;
862  }
863 
864  if (om_->isVerbosity( Debug ) ) {
865  CheckList chk;
866  chk.checkQ = true;
867  om_->print( Debug, accuracyCheck(chk, ": in setAuxVecs()") );
868  }
869  }
870 
871 
873  /* Initialize the state of the solver
874  *
875  * POST-CONDITIONS:
876  *
877  * V_ is orthonormal, orthogonal to auxVecs_, for first curDim_ vectors
878  * theta_ contains Ritz w.r.t. V_(1:curDim_)
879  * X is Ritz vectors w.r.t. V_(1:curDim_)
880  * KX = Op*X
881  * MX = M*X if hasM_
882  * R = KX - MX*diag(theta_)
883  *
884  */
885  template <class ScalarType, class MV, class OP>
887  {
888  // NOTE: memory has been allocated by setBlockSize(). Use setBlock below; do not Clone
889  // NOTE: Overall time spent in this routine is counted to timerInit_; portions will also be counted towards other primitives
890 
891 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
892  Teuchos::TimeMonitor inittimer( *timerInit_ );
893 #endif
894 
895  std::vector<int> bsind(blockSize_);
896  for (int i=0; i<blockSize_; ++i) bsind[i] = i;
897 
899 
900  // in BlockDavidson, V is primary
901  // the order of dependence follows like so.
902  // --init-> V,KK
903  // --ritz analysis-> theta,X
904  // --op apply-> KX,MX
905  // --compute-> R
906  //
907  // if the user specifies all data for a level, we will accept it.
908  // otherwise, we will generate the whole level, and all subsequent levels.
909  //
910  // the data members are ordered based on dependence, and the levels are
911  // partitioned according to the amount of work required to produce the
912  // items in a level.
913  //
914  // inconsistent multivectors widths and lengths will not be tolerated, and
915  // will be treated with exceptions.
916  //
917  // for multivector pointers in newstate which point directly (as opposed to indirectly, via a view) to
918  // multivectors in the solver, the copy will not be affected.
919 
920  // set up V and KK: get them from newstate if user specified them
921  // otherwise, set them manually
922  Teuchos::RCP<MV> lclV;
924 
925  if (newstate.V != Teuchos::null && newstate.KK != Teuchos::null) {
926  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*newstate.V) != MVText::GetGlobalLength(*V_), std::invalid_argument,
927  "Anasazi::BlockDavidson::initialize(newstate): Vector length of V not correct." );
928  TEUCHOS_TEST_FOR_EXCEPTION( newstate.curDim < blockSize_, std::invalid_argument,
929  "Anasazi::BlockDavidson::initialize(newstate): Rank of new state must be at least blockSize().");
930  TEUCHOS_TEST_FOR_EXCEPTION( newstate.curDim > blockSize_*numBlocks_, std::invalid_argument,
931  "Anasazi::BlockDavidson::initialize(newstate): Rank of new state must be less than getMaxSubspaceDim().");
932  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.V) < newstate.curDim, std::invalid_argument,
933  "Anasazi::BlockDavidson::initialize(newstate): Multivector for basis in new state must be as large as specified state rank.");
934 
935  curDim_ = newstate.curDim;
936  // pick an integral amount
937  curDim_ = (int)(curDim_ / blockSize_)*blockSize_;
938 
939  TEUCHOS_TEST_FOR_EXCEPTION( curDim_ != newstate.curDim, std::invalid_argument,
940  "Anasazi::BlockDavidson::initialize(newstate): Rank of new state must be a multiple of getBlockSize().");
941 
942  // check size of KK
943  TEUCHOS_TEST_FOR_EXCEPTION( newstate.KK->numRows() < curDim_ || newstate.KK->numCols() < curDim_, std::invalid_argument,
944  "Anasazi::BlockDavidson::initialize(newstate): Projected matrix in new state must be as large as specified state rank.");
945 
946  // put data in V
947  std::vector<int> nevind(curDim_);
948  for (int i=0; i<curDim_; ++i) nevind[i] = i;
949  if (newstate.V != V_) {
950  if (curDim_ < MVT::GetNumberVecs(*newstate.V)) {
951  newstate.V = MVT::CloneView(*newstate.V,nevind);
952  }
953  MVT::SetBlock(*newstate.V,nevind,*V_);
954  }
955  lclV = MVT::CloneViewNonConst(*V_,nevind);
956 
957  // put data into KK_
958  lclKK = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,curDim_,curDim_) );
959  if (newstate.KK != KK_) {
960  if (newstate.KK->numRows() > curDim_ || newstate.KK->numCols() > curDim_) {
961  newstate.KK = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*newstate.KK,curDim_,curDim_) );
962  }
963  lclKK->assign(*newstate.KK);
964  }
965  //
966  // make lclKK Hermitian in memory (copy the upper half to the lower half)
967  for (int j=0; j<curDim_-1; ++j) {
968  for (int i=j+1; i<curDim_; ++i) {
969  (*lclKK)(i,j) = SCT::conjugate((*lclKK)(j,i));
970  }
971  }
972  }
973  else {
974  // user did not specify one of V or KK
975  // get vectors from problem or generate something, projectAndNormalize
976  Teuchos::RCP<const MV> ivec = problem_->getInitVec();
977  TEUCHOS_TEST_FOR_EXCEPTION(ivec == Teuchos::null,std::invalid_argument,
978  "Anasazi::BlockDavdison::initialize(newstate): Eigenproblem did not specify initial vectors to clone from.");
979  // clear newstate so we won't use any data from it below
980  newstate.X = Teuchos::null;
981  newstate.MX = Teuchos::null;
982  newstate.KX = Teuchos::null;
983  newstate.R = Teuchos::null;
984  newstate.H = Teuchos::null;
985  newstate.T = Teuchos::null;
986  newstate.KK = Teuchos::null;
987  newstate.V = Teuchos::null;
988  newstate.curDim = 0;
989 
990  curDim_ = MVT::GetNumberVecs(*ivec);
991  // pick the largest multiple of blockSize_
992  curDim_ = (int)(curDim_ / blockSize_)*blockSize_;
993  if (curDim_ > blockSize_*numBlocks_) {
994  // user specified too many vectors... truncate
995  // this produces a full subspace, but that is okay
996  curDim_ = blockSize_*numBlocks_;
997  }
998  bool userand = false;
999  if (curDim_ == 0) {
1000  // we need at least blockSize_ vectors
1001  // use a random multivec: ignore everything from InitVec
1002  userand = true;
1003  curDim_ = blockSize_;
1004  }
1005 
1006  // get pointers into V,KV,MV
1007  // tmpVecs will be used below for M*V and K*V (not simultaneously)
1008  // lclV has curDim vectors
1009  // if there is space for lclV and tmpVecs in V_, point tmpVecs into V_
1010  // otherwise, we must allocate space for these products
1011  //
1012  // get pointer to first curDim vector in V_
1013  std::vector<int> dimind(curDim_);
1014  for (int i=0; i<curDim_; ++i) dimind[i] = i;
1015  lclV = MVT::CloneViewNonConst(*V_,dimind);
1016  if (userand) {
1017  // generate random vector data
1018  MVT::MvRandom(*lclV);
1019  }
1020  else {
1021  if (MVT::GetNumberVecs(*ivec) > curDim_) {
1022  ivec = MVT::CloneView(*ivec,dimind);
1023  }
1024  // assign ivec to first part of lclV
1025  MVT::SetBlock(*ivec,dimind,*lclV);
1026  }
1027  Teuchos::RCP<MV> tmpVecs;
1028  if (curDim_*2 <= blockSize_*numBlocks_) {
1029  // partition V_ = [lclV tmpVecs _leftover_]
1030  std::vector<int> block2(curDim_);
1031  for (int i=0; i<curDim_; ++i) block2[i] = curDim_+i;
1032  tmpVecs = MVT::CloneViewNonConst(*V_,block2);
1033  }
1034  else {
1035  // allocate space for tmpVecs
1036  tmpVecs = MVT::Clone(*V_,curDim_);
1037  }
1038 
1039  // compute M*lclV if hasM_
1040  if (hasM_) {
1041 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1042  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1043 #endif
1044  OPT::Apply(*MOp_,*lclV,*tmpVecs);
1045  count_ApplyM_ += curDim_;
1046  }
1047 
1048  // remove auxVecs from lclV and normalize it
1049  if (auxVecs_.size() > 0) {
1050 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1051  Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1052 #endif
1053 
1055  int rank = orthman_->projectAndNormalizeMat(*lclV,auxVecs_,dummyC,Teuchos::null,tmpVecs);
1057  "Anasazi::BlockDavidson::initialize(): Couldn't generate initial basis of full rank.");
1058  }
1059  else {
1060 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1061  Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1062 #endif
1063 
1064  int rank = orthman_->normalizeMat(*lclV,Teuchos::null,tmpVecs);
1066  "Anasazi::BlockDavidson::initialize(): Couldn't generate initial basis of full rank.");
1067  }
1068 
1069  // compute K*lclV: we are re-using tmpVecs to store the result
1070  {
1071 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1072  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1073 #endif
1074  OPT::Apply(*Op_,*lclV,*tmpVecs);
1075  count_ApplyOp_ += curDim_;
1076  }
1077 
1078  // generate KK
1079  lclKK = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,curDim_,curDim_) );
1080  MVT::MvTransMv(ONE,*lclV,*tmpVecs,*lclKK);
1081 
1082  // clear tmpVecs
1083  tmpVecs = Teuchos::null;
1084  }
1085 
1086  // X,theta require Ritz analysis; if we have to generate one of these, we might as well generate both
1087  if (newstate.X != Teuchos::null && newstate.T != Teuchos::null) {
1089  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): Size of X must be consistent with block size and length of V.");
1090  TEUCHOS_TEST_FOR_EXCEPTION((signed int)(newstate.T->size()) != curDim_,
1091  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): Size of T must be consistent with dimension of V.");
1092 
1093  if (newstate.X != X_) {
1094  MVT::SetBlock(*newstate.X,bsind,*X_);
1095  }
1096 
1097  std::copy(newstate.T->begin(),newstate.T->end(),theta_.begin());
1098  }
1099  else {
1100  // compute ritz vecs/vals
1101  Teuchos::SerialDenseMatrix<int,ScalarType> S(curDim_,curDim_);
1102  {
1103 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1104  Teuchos::TimeMonitor lcltimer( *timerDS_ );
1105 #endif
1106  int rank = curDim_;
1107  Utils::directSolver(curDim_, *lclKK, Teuchos::null, S, theta_, rank, 10);
1108  // we want all ritz values back
1110  "Anasazi::BlockDavidson::initialize(newstate): Not enough Ritz vectors to initialize algorithm.");
1111  }
1112  // sort ritz pairs
1113  {
1114 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1115  Teuchos::TimeMonitor lcltimer( *timerSortEval_ );
1116 #endif
1117 
1118  std::vector<int> order(curDim_);
1119  //
1120  // sort the first curDim_ values in theta_
1121  sm_->sort(theta_, Teuchos::rcpFromRef(order), curDim_); // don't catch exception
1122  //
1123  // apply the same ordering to the primitive ritz vectors
1124  Utils::permuteVectors(order,S);
1125  }
1126 
1127  // compute eigenvectors
1129  {
1130 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1131  Teuchos::TimeMonitor lcltimer( *timerLocal_ );
1132 #endif
1133 
1134  // X <- lclV*S
1135  MVT::MvTimesMatAddMv( ONE, *lclV, S1, ZERO, *X_ );
1136  }
1137  // we generated theta,X so we don't want to use the user's KX,MX
1138  newstate.KX = Teuchos::null;
1139  newstate.MX = Teuchos::null;
1140  }
1141 
1142  // done with local pointers
1143  lclV = Teuchos::null;
1144  lclKK = Teuchos::null;
1145 
1146  // set up KX
1147  if ( newstate.KX != Teuchos::null ) {
1148  TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.KX) != blockSize_,
1149  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): vector length of newstate.KX not correct." );
1151  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): newstate.KX must have at least block size vectors." );
1152  if (newstate.KX != KX_) {
1153  MVT::SetBlock(*newstate.KX,bsind,*KX_);
1154  }
1155  }
1156  else {
1157  // generate KX
1158  {
1159 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1160  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1161 #endif
1162  OPT::Apply(*Op_,*X_,*KX_);
1163  count_ApplyOp_ += blockSize_;
1164  }
1165  // we generated KX; we will generate R as well
1166  newstate.R = Teuchos::null;
1167  }
1168 
1169  // set up MX
1170  if (hasM_) {
1171  if ( newstate.MX != Teuchos::null ) {
1172  TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.MX) != blockSize_,
1173  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): vector length of newstate.MX not correct." );
1175  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): newstate.MX must have at least block size vectors." );
1176  if (newstate.MX != MX_) {
1177  MVT::SetBlock(*newstate.MX,bsind,*MX_);
1178  }
1179  }
1180  else {
1181  // generate MX
1182  {
1183 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1184  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1185 #endif
1186  OPT::Apply(*MOp_,*X_,*MX_);
1187  count_ApplyOp_ += blockSize_;
1188  }
1189  // we generated MX; we will generate R as well
1190  newstate.R = Teuchos::null;
1191  }
1192  }
1193  else {
1194  // the assignment MX_==X_ would be redundant; take advantage of this opportunity to debug a little
1195  TEUCHOS_TEST_FOR_EXCEPTION(MX_ != X_, std::logic_error, "Anasazi::BlockDavidson::initialize(): solver invariant not satisfied (MX==X).");
1196  }
1197 
1198  // set up R
1199  if (newstate.R != Teuchos::null) {
1200  TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*newstate.R) != blockSize_,
1201  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): vector length of newstate.R not correct." );
1203  std::invalid_argument, "Anasazi::BlockDavidson::initialize(newstate): newstate.R must have at least block size vectors." );
1204  if (newstate.R != R_) {
1205  MVT::SetBlock(*newstate.R,bsind,*R_);
1206  }
1207  }
1208  else {
1209 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1210  Teuchos::TimeMonitor lcltimer( *timerCompRes_ );
1211 #endif
1212 
1213  // form R <- KX - MX*T
1214  MVT::MvAddMv(ZERO,*KX_,ONE,*KX_,*R_);
1215  Teuchos::SerialDenseMatrix<int,ScalarType> T(blockSize_,blockSize_);
1216  T.putScalar(ZERO);
1217  for (int i=0; i<blockSize_; ++i) T(i,i) = theta_[i];
1218  MVT::MvTimesMatAddMv(-ONE,*MX_,T,ONE,*R_);
1219 
1220  }
1221 
1222  // R has been updated; mark the norms as out-of-date
1223  Rnorms_current_ = false;
1224  R2norms_current_ = false;
1225 
1226  // finally, we are initialized
1227  initialized_ = true;
1228 
1229  if (om_->isVerbosity( Debug ) ) {
1230  // Check almost everything here
1231  CheckList chk;
1232  chk.checkV = true;
1233  chk.checkX = true;
1234  chk.checkKX = true;
1235  chk.checkMX = true;
1236  chk.checkR = true;
1237  chk.checkQ = true;
1238  chk.checkKK = true;
1239  om_->print( Debug, accuracyCheck(chk, ": after initialize()") );
1240  }
1241 
1242  // Print information on current status
1243  if (om_->isVerbosity(Debug)) {
1244  currentStatus( om_->stream(Debug) );
1245  }
1246  else if (om_->isVerbosity(IterationDetails)) {
1247  currentStatus( om_->stream(IterationDetails) );
1248  }
1249  }
1250 
1251 
1253  // initialize the solver with default state
1254  template <class ScalarType, class MV, class OP>
1256  {
1258  initialize(empty);
1259  }
1260 
1261 
1263  // Perform BlockDavidson iterations until the StatusTest tells us to stop.
1264  template <class ScalarType, class MV, class OP>
1266  {
1267  //
1268  // Initialize solver state
1269  if (initialized_ == false) {
1270  initialize();
1271  }
1272 
1273  // as a data member, this would be redundant and require synchronization with
1274  // blockSize_ and numBlocks_; we'll use a constant here.
1275  const int searchDim = blockSize_*numBlocks_;
1276 
1278 
1279  //
1280  // The projected matrices are part of the state, but the eigenvectors are defined locally.
1281  // S = Local eigenvectors (size: searchDim * searchDim
1282  Teuchos::SerialDenseMatrix<int,ScalarType> S( searchDim, searchDim );
1283 
1284 
1286  // iterate until the status test tells us to stop.
1287  // also break if our basis is full
1288  while (tester_->checkStatus(this) != Passed && curDim_ < searchDim) {
1289 
1290  // Print information on current iteration
1291  if (om_->isVerbosity(Debug)) {
1292  currentStatus( om_->stream(Debug) );
1293  }
1294  else if (om_->isVerbosity(IterationDetails)) {
1295  currentStatus( om_->stream(IterationDetails) );
1296  }
1297 
1298  ++iter_;
1299 
1300  // get the current part of the basis
1301  std::vector<int> curind(blockSize_);
1302  for (int i=0; i<blockSize_; ++i) curind[i] = curDim_ + i;
1303  H_ = MVT::CloneViewNonConst(*V_,curind);
1304 
1305  // Apply the preconditioner on the residuals: H <- Prec*R
1306  // H = Prec*R
1307  if (Prec_ != Teuchos::null) {
1308 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1309  Teuchos::TimeMonitor lcltimer( *timerPrec_ );
1310 #endif
1311  OPT::Apply( *Prec_, *R_, *H_ ); // don't catch the exception
1312  count_ApplyPrec_ += blockSize_;
1313  }
1314  else {
1315  std::vector<int> bsind(blockSize_);
1316  for (int i=0; i<blockSize_; ++i) { bsind[i] = i; }
1317  MVT::SetBlock(*R_,bsind,*H_);
1318  }
1319 
1320  // Apply the mass matrix on H
1321  if (hasM_) {
1322  // use memory at MX_ for temporary storage
1323  MH_ = MX_;
1324 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1325  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1326 #endif
1327  OPT::Apply( *MOp_, *H_, *MH_); // don't catch the exception
1328  count_ApplyM_ += blockSize_;
1329  }
1330  else {
1331  MH_ = H_;
1332  }
1333 
1334  // Get a view of the previous vectors
1335  // this is used for orthogonalization and for computing V^H K H
1336  std::vector<int> prevind(curDim_);
1337  for (int i=0; i<curDim_; ++i) prevind[i] = i;
1338  Teuchos::RCP<const MV> Vprev = MVT::CloneView(*V_,prevind);
1339 
1340  // Orthogonalize H against the previous vectors and the auxiliary vectors, and normalize
1341  {
1342 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1343  Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1344 #endif
1345 
1346  Teuchos::Array<Teuchos::RCP<const MV> > against = auxVecs_;
1347  against.push_back(Vprev);
1349  int rank = orthman_->projectAndNormalizeMat(*H_,against,
1350  dummyC,
1351  Teuchos::null,MH_);
1353  "Anasazi::BlockDavidson::iterate(): unable to compute orthonormal basis for H.");
1354  }
1355 
1356  // Apply the stiffness matrix to H
1357  {
1358  // use memory at KX_ for temporary storage
1359  KH_ = KX_;
1360 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1361  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1362 #endif
1363  OPT::Apply( *Op_, *H_, *KH_); // don't catch the exception
1364  count_ApplyOp_ += blockSize_;
1365  }
1366 
1367  if (om_->isVerbosity( Debug ) ) {
1368  CheckList chk;
1369  chk.checkH = true;
1370  chk.checkMH = true;
1371  chk.checkKH = true;
1372  om_->print( Debug, accuracyCheck(chk, ": after ortho H") );
1373  }
1374  else if (om_->isVerbosity( OrthoDetails ) ) {
1375  CheckList chk;
1376  chk.checkH = true;
1377  chk.checkMH = true;
1378  chk.checkKH = true;
1379  om_->print( OrthoDetails, accuracyCheck(chk,": after ortho H") );
1380  }
1381 
1382  // compute next part of the projected matrices
1383  // this this in two parts
1385  // Vprev*K*H
1386  nextKK = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,curDim_,blockSize_,0,curDim_) );
1387  MVT::MvTransMv(ONE,*Vprev,*KH_,*nextKK);
1388  // H*K*H
1389  nextKK = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*KK_,blockSize_,blockSize_,curDim_,curDim_) );
1390  MVT::MvTransMv(ONE,*H_,*KH_,*nextKK);
1391  //
1392  // make sure that KK_ is Hermitian in memory
1393  nextKK = Teuchos::null;
1394  for (int i=curDim_; i<curDim_+blockSize_; ++i) {
1395  for (int j=0; j<i; ++j) {
1396  (*KK_)(i,j) = SCT::conjugate((*KK_)(j,i));
1397  }
1398  }
1399 
1400  // V has been extended, and KK has been extended. Update basis dim and release all pointers.
1401  curDim_ += blockSize_;
1402  H_ = KH_ = MH_ = Teuchos::null;
1403  Vprev = Teuchos::null;
1404 
1405  if (om_->isVerbosity( Debug ) ) {
1406  CheckList chk;
1407  chk.checkKK = true;
1408  om_->print( Debug, accuracyCheck(chk, ": after expanding KK") );
1409  }
1410 
1411  // Get pointer to complete basis
1412  curind.resize(curDim_);
1413  for (int i=0; i<curDim_; ++i) curind[i] = i;
1414  Teuchos::RCP<const MV> curV = MVT::CloneView(*V_,curind);
1415 
1416  // Perform spectral decomposition
1417  {
1418 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1419  Teuchos::TimeMonitor lcltimer(*timerDS_);
1420 #endif
1421  int nevlocal = curDim_;
1422  int info = Utils::directSolver(curDim_,*KK_,Teuchos::null,S,theta_,nevlocal,10);
1423  TEUCHOS_TEST_FOR_EXCEPTION(info != 0,std::logic_error,"Anasazi::BlockDavidson::iterate(): direct solve returned error code.");
1424  // we did not ask directSolver to perform deflation, so nevLocal better be curDim_
1425  TEUCHOS_TEST_FOR_EXCEPTION(nevlocal != curDim_,std::logic_error,"Anasazi::BlockDavidson::iterate(): direct solve did not compute all eigenvectors."); // this should never happen
1426  }
1427 
1428  // Sort ritz pairs
1429  {
1430 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1431  Teuchos::TimeMonitor lcltimer( *timerSortEval_ );
1432 #endif
1433 
1434  std::vector<int> order(curDim_);
1435  //
1436  // sort the first curDim_ values in theta_
1437  sm_->sort(theta_, Teuchos::rcp(&order,false), curDim_); // don't catch exception
1438  //
1439  // apply the same ordering to the primitive ritz vectors
1441  Utils::permuteVectors(order,curS);
1442  }
1443 
1444  // Create a view matrix of the first blockSize_ vectors
1445  Teuchos::SerialDenseMatrix<int,ScalarType> S1( Teuchos::View, S, curDim_, blockSize_ );
1446 
1447  // Compute the new Ritz vectors
1448  {
1449 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1450  Teuchos::TimeMonitor lcltimer( *timerLocal_ );
1451 #endif
1452  MVT::MvTimesMatAddMv(ONE,*curV,S1,ZERO,*X_);
1453  }
1454 
1455  // Apply the stiffness matrix for the Ritz vectors
1456  {
1457 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1458  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1459 #endif
1460  OPT::Apply( *Op_, *X_, *KX_); // don't catch the exception
1461  count_ApplyOp_ += blockSize_;
1462  }
1463  // Apply the mass matrix for the Ritz vectors
1464  if (hasM_) {
1465 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1466  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1467 #endif
1468  OPT::Apply(*MOp_,*X_,*MX_);
1469  count_ApplyM_ += blockSize_;
1470  }
1471  else {
1472  MX_ = X_;
1473  }
1474 
1475  // Compute the residual
1476  // R = KX - MX*diag(theta)
1477  {
1478 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1479  Teuchos::TimeMonitor lcltimer( *timerCompRes_ );
1480 #endif
1481 
1482  MVT::MvAddMv( ONE, *KX_, ZERO, *KX_, *R_ );
1483  Teuchos::SerialDenseMatrix<int,ScalarType> T( blockSize_, blockSize_ );
1484  for (int i = 0; i < blockSize_; ++i) {
1485  T(i,i) = theta_[i];
1486  }
1487  MVT::MvTimesMatAddMv( -ONE, *MX_, T, ONE, *R_ );
1488  }
1489 
1490  // R has been updated; mark the norms as out-of-date
1491  Rnorms_current_ = false;
1492  R2norms_current_ = false;
1493 
1494 
1495  // When required, monitor some orthogonalities
1496  if (om_->isVerbosity( Debug ) ) {
1497  // Check almost everything here
1498  CheckList chk;
1499  chk.checkV = true;
1500  chk.checkX = true;
1501  chk.checkKX = true;
1502  chk.checkMX = true;
1503  chk.checkR = true;
1504  om_->print( Debug, accuracyCheck(chk, ": after local update") );
1505  }
1506  else if (om_->isVerbosity( OrthoDetails )) {
1507  CheckList chk;
1508  chk.checkX = true;
1509  chk.checkKX = true;
1510  chk.checkMX = true;
1511  chk.checkR = true;
1512  om_->print( OrthoDetails, accuracyCheck(chk, ": after local update") );
1513  }
1514  } // end while (statusTest == false)
1515 
1516  } // end of iterate()
1517 
1518 
1519 
1521  // compute/return residual M-norms
1522  template <class ScalarType, class MV, class OP>
1523  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
1525  if (Rnorms_current_ == false) {
1526  // Update the residual norms
1527  orthman_->norm(*R_,Rnorms_);
1528  Rnorms_current_ = true;
1529  }
1530  return Rnorms_;
1531  }
1532 
1533 
1535  // compute/return residual 2-norms
1536  template <class ScalarType, class MV, class OP>
1537  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
1539  if (R2norms_current_ == false) {
1540  // Update the residual 2-norms
1541  MVT::MvNorm(*R_,R2norms_);
1542  R2norms_current_ = true;
1543  }
1544  return R2norms_;
1545  }
1546 
1548  // Set a new StatusTest for the solver.
1549  template <class ScalarType, class MV, class OP>
1551  TEUCHOS_TEST_FOR_EXCEPTION(test == Teuchos::null,std::invalid_argument,
1552  "Anasazi::BlockDavidson::setStatusTest() was passed a null StatusTest.");
1553  tester_ = test;
1554  }
1555 
1557  // Get the current StatusTest used by the solver.
1558  template <class ScalarType, class MV, class OP>
1560  return tester_;
1561  }
1562 
1563 
1565  // Check accuracy, orthogonality, and other debugging stuff
1566  //
1567  // bools specify which tests we want to run (instead of running more than we actually care about)
1568  //
1569  // we don't bother checking the following because they are computed explicitly:
1570  // H == Prec*R
1571  // KH == K*H
1572  //
1573  //
1574  // checkV : V orthonormal
1575  // orthogonal to auxvecs
1576  // checkX : X orthonormal
1577  // orthogonal to auxvecs
1578  // checkMX: check MX == M*X
1579  // checkKX: check KX == K*X
1580  // checkH : H orthonormal
1581  // orthogonal to V and H and auxvecs
1582  // checkMH: check MH == M*H
1583  // checkR : check R orthogonal to X
1584  // checkQ : check that auxiliary vectors are actually orthonormal
1585  // checkKK: check that KK is symmetric in memory
1586  //
1587  // TODO:
1588  // add checkTheta
1589  //
1590  template <class ScalarType, class MV, class OP>
1591  std::string BlockDavidson<ScalarType,MV,OP>::accuracyCheck( const CheckList &chk, const std::string &where ) const
1592  {
1593  using std::endl;
1594 
1595  std::stringstream os;
1596  os.precision(2);
1597  os.setf(std::ios::scientific, std::ios::floatfield);
1598 
1599  os << " Debugging checks: iteration " << iter_ << where << endl;
1600 
1601  // V and friends
1602  std::vector<int> lclind(curDim_);
1603  for (int i=0; i<curDim_; ++i) lclind[i] = i;
1605  if (initialized_) {
1606  lclV = MVT::CloneView(*V_,lclind);
1607  }
1608  if (chk.checkV && initialized_) {
1609  MagnitudeType err = orthman_->orthonormError(*lclV);
1610  os << " >> Error in V^H M V == I : " << err << endl;
1611  for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1612  err = orthman_->orthogError(*lclV,*auxVecs_[i]);
1613  os << " >> Error in V^H M Q[" << i << "] == 0 : " << err << endl;
1614  }
1615  Teuchos::SerialDenseMatrix<int,ScalarType> curKK(curDim_,curDim_);
1616  Teuchos::RCP<MV> lclKV = MVT::Clone(*V_,curDim_);
1617  OPT::Apply(*Op_,*lclV,*lclKV);
1618  MVT::MvTransMv(ONE,*lclV,*lclKV,curKK);
1619  Teuchos::SerialDenseMatrix<int,ScalarType> subKK(Teuchos::View,*KK_,curDim_,curDim_);
1620  curKK -= subKK;
1621  // dup the lower tri part
1622  for (int j=0; j<curDim_; ++j) {
1623  for (int i=j+1; i<curDim_; ++i) {
1624  curKK(i,j) = curKK(j,i);
1625  }
1626  }
1627  os << " >> Error in V^H K V == KK : " << curKK.normFrobenius() << endl;
1628  }
1629 
1630  // X and friends
1631  if (chk.checkX && initialized_) {
1632  MagnitudeType err = orthman_->orthonormError(*X_);
1633  os << " >> Error in X^H M X == I : " << err << endl;
1634  for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1635  err = orthman_->orthogError(*X_,*auxVecs_[i]);
1636  os << " >> Error in X^H M Q[" << i << "] == 0 : " << err << endl;
1637  }
1638  }
1639  if (chk.checkMX && hasM_ && initialized_) {
1640  MagnitudeType err = Utils::errorEquality(*X_, *MX_, MOp_);
1641  os << " >> Error in MX == M*X : " << err << endl;
1642  }
1643  if (chk.checkKX && initialized_) {
1644  MagnitudeType err = Utils::errorEquality(*X_, *KX_, Op_);
1645  os << " >> Error in KX == K*X : " << err << endl;
1646  }
1647 
1648  // H and friends
1649  if (chk.checkH && initialized_) {
1650  MagnitudeType err = orthman_->orthonormError(*H_);
1651  os << " >> Error in H^H M H == I : " << err << endl;
1652  err = orthman_->orthogError(*H_,*lclV);
1653  os << " >> Error in H^H M V == 0 : " << err << endl;
1654  err = orthman_->orthogError(*H_,*X_);
1655  os << " >> Error in H^H M X == 0 : " << err << endl;
1656  for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1657  err = orthman_->orthogError(*H_,*auxVecs_[i]);
1658  os << " >> Error in H^H M Q[" << i << "] == 0 : " << err << endl;
1659  }
1660  }
1661  if (chk.checkKH && initialized_) {
1662  MagnitudeType err = Utils::errorEquality(*H_, *KH_, Op_);
1663  os << " >> Error in KH == K*H : " << err << endl;
1664  }
1665  if (chk.checkMH && hasM_ && initialized_) {
1666  MagnitudeType err = Utils::errorEquality(*H_, *MH_, MOp_);
1667  os << " >> Error in MH == M*H : " << err << endl;
1668  }
1669 
1670  // R: this is not M-orthogonality, but standard Euclidean orthogonality
1671  if (chk.checkR && initialized_) {
1672  Teuchos::SerialDenseMatrix<int,ScalarType> xTx(blockSize_,blockSize_);
1673  MVT::MvTransMv(ONE,*X_,*R_,xTx);
1674  MagnitudeType err = xTx.normFrobenius();
1675  os << " >> Error in X^H R == 0 : " << err << endl;
1676  }
1677 
1678  // KK
1679  if (chk.checkKK && initialized_) {
1680  Teuchos::SerialDenseMatrix<int,ScalarType> SDMerr(curDim_,curDim_), lclKK(Teuchos::View,*KK_,curDim_,curDim_);
1681  for (int j=0; j<curDim_; ++j) {
1682  for (int i=0; i<curDim_; ++i) {
1683  SDMerr(i,j) = lclKK(i,j) - SCT::conjugate(lclKK(j,i));
1684  }
1685  }
1686  os << " >> Error in KK - KK^H == 0 : " << SDMerr.normFrobenius() << endl;
1687  }
1688 
1689  // Q
1690  if (chk.checkQ) {
1691  for (Array_size_type i=0; i<auxVecs_.size(); ++i) {
1692  MagnitudeType err = orthman_->orthonormError(*auxVecs_[i]);
1693  os << " >> Error in Q[" << i << "]^H M Q[" << i << "] == I : " << err << endl;
1694  for (Array_size_type j=i+1; j<auxVecs_.size(); ++j) {
1695  err = orthman_->orthogError(*auxVecs_[i],*auxVecs_[j]);
1696  os << " >> Error in Q[" << i << "]^H M Q[" << j << "] == 0 : " << err << endl;
1697  }
1698  }
1699  }
1700 
1701  os << endl;
1702 
1703  return os.str();
1704  }
1705 
1706 
1708  // Print the current status of the solver
1709  template <class ScalarType, class MV, class OP>
1710  void
1712  {
1713  using std::endl;
1714 
1715  os.setf(std::ios::scientific, std::ios::floatfield);
1716  os.precision(6);
1717  os <<endl;
1718  os <<"================================================================================" << endl;
1719  os << endl;
1720  os <<" BlockDavidson Solver Status" << endl;
1721  os << endl;
1722  os <<"The solver is "<<(initialized_ ? "initialized." : "not initialized.") << endl;
1723  os <<"The number of iterations performed is " <<iter_<<endl;
1724  os <<"The block size is " << blockSize_<<endl;
1725  os <<"The number of blocks is " << numBlocks_<<endl;
1726  os <<"The current basis size is " << curDim_<<endl;
1727  os <<"The number of auxiliary vectors is "<< numAuxVecs_ << endl;
1728  os <<"The number of operations Op*x is "<<count_ApplyOp_<<endl;
1729  os <<"The number of operations M*x is "<<count_ApplyM_<<endl;
1730  os <<"The number of operations Prec*x is "<<count_ApplyPrec_<<endl;
1731 
1732  os.setf(std::ios_base::right, std::ios_base::adjustfield);
1733 
1734  if (initialized_) {
1735  os << endl;
1736  os <<"CURRENT EIGENVALUE ESTIMATES "<<endl;
1737  os << std::setw(20) << "Eigenvalue"
1738  << std::setw(20) << "Residual(M)"
1739  << std::setw(20) << "Residual(2)"
1740  << endl;
1741  os <<"--------------------------------------------------------------------------------"<<endl;
1742  for (int i=0; i<blockSize_; ++i) {
1743  os << std::setw(20) << theta_[i];
1744  if (Rnorms_current_) os << std::setw(20) << Rnorms_[i];
1745  else os << std::setw(20) << "not current";
1746  if (R2norms_current_) os << std::setw(20) << R2norms_[i];
1747  else os << std::setw(20) << "not current";
1748  os << endl;
1749  }
1750  }
1751  os <<"================================================================================" << endl;
1752  os << endl;
1753  }
1754 
1755 } // End of namespace Anasazi
1756 
1757 #endif
1758 
1759 // End of file AnasaziBlockDavidson.hpp
virtual ~BlockDavidson()
Anasazi::BlockDavidson destructor.
bool isInitialized() const
Indicates whether the solver has been initialized or not.
static void MvRandom(MV &mv)
Replace the vectors in mv with random vectors.
Teuchos::RCP< const MV > H
The current preconditioned residual vectors.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
int getNumIters() const
Get the current iteration count.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRitzRes2Norms()
Get the 2-norms of the residuals.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
void setSize(int blockSize, int numBlocks)
Set the blocksize and number of blocks to be used by the iterative solver in solving this eigenproble...
This class defines the interface required by an eigensolver and status test class to compute solution...
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.
Declaration of basic traits for the multivector type.
T & get(const std::string &name, T def_value)
BlockDavidsonOrthoFailure is thrown when the orthogonalization manager is unable to orthogonalize the...
void currentStatus(std::ostream &os)
This method requests that the solver print out its current status to the given output stream...
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current eigenvectors and eigenvalues...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void setStatusTest(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Set a new StatusTest for the solver.
Virtual base class which defines basic traits for the operator type.
Teuchos::RCP< const MV > KX
The image of the current eigenvectors under K.
static Teuchos::ScalarTraits< ScalarType >::magnitudeType errorEquality(const MV &X, const MV &MX, Teuchos::RCP< const OP > M=Teuchos::null)
Return the maximum coefficient of the matrix scaled by the maximum coefficient of MX...
static void Apply(const OP &Op, const MV &x, MV &y)
Application method which performs operation y = Op*x. An OperatorError exception is thrown if there i...
BlockDavidsonInitFailure is thrown when the BlockDavidson solver is unable to generate an initial ite...
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > getStatusTest() const
Get the current StatusTest used by the solver.
An exception class parent to all Anasazi exceptions.
std::vector< Value< ScalarType > > getRitzValues()
Get the Ritz values for the previous iteration.
This class implements a Block Davidson iteration, a preconditioned iteration for solving linear Hermi...
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Teuchos::Array< Teuchos::RCP< const MV > > getAuxVecs() const
Get the auxiliary vectors for the solver.
Pure virtual base class which describes the basic interface to the iterative eigensolver.
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
Anasazi&#39;s templated, static class providing utilities for the solvers.
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
static ScalarType conjugate(ScalarType a)
Anasazi&#39;s templated virtual class for providing routines for orthogonalization and orthonormalization...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace. For BlockDavidson, this always returns n...
Output managers remove the need for the eigensolver to know any information about the required output...
Teuchos::RCP< const MV > V
The basis for the Krylov space.
static int directSolver(int size, const Teuchos::SerialDenseMatrix< int, ScalarType > &KK, Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > MM, Teuchos::SerialDenseMatrix< int, ScalarType > &EV, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &theta, int &nev, int esType=0)
Routine for computing the first NEV generalized eigenpairs of the Hermitian pencil (KK...
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
int getBlockSize() const
Get the blocksize used by the iterative solver.
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
void setAuxVecs(const Teuchos::Array< Teuchos::RCP< const MV > > &auxvecs)
Set the auxiliary vectors for the solver.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the eigenvalue problem.
iterator end()
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
int curDim
The current dimension of the solver.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
void initialize()
Initialize the solver with the initial vectors from the eigenproblem or random data.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
void push_back(const value_type &x)
Structure to contain pointers to BlockDavidson state variables.
static void permuteVectors(const int n, const std::vector< int > &perm, MV &Q, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *resids=0)
Permute the vectors in a multivector according to the permutation vector perm, and optionally the res...
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.
size_type size() const
Types and exceptions used within Anasazi solvers and interfaces.
std::vector< int > getRitzIndex()
Get the index used for extracting individual Ritz vectors from getRitzVectors().
void iterate()
This method performs BlockDavidson iterations until the status test indicates the need to stop or an ...
BlockDavidsonState< ScalarType, MV > getState() const
Get access to the current state of the eigensolver.
Anasazi&#39;s templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
A pure virtual class for defining the status tests for the Anasazi iterative solvers.
iterator begin()
BlockDavidson(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< SortManager< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > &sorter, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, const Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP > > &ortho, Teuchos::ParameterList &params)
BlockDavidson constructor with eigenproblem, solver utilities, and parameter list of solver options...
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRes2Norms()
Get the current residual 2-norms, computing the norms if they are not up-to-date with the current res...
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
void setBlockSize(int blockSize)
Set the blocksize.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
Teuchos::RCP< const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > T
The current Ritz values. This vector is a copy of the internal data.
void resetNumIters()
Reset the iteration count.
Class which provides internal utilities for the Anasazi solvers.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getResNorms()
Get the current residual norms, computing the norms if they are not up-to-date with the current resid...
Teuchos::RCP< const MV > getRitzVectors()
Get access to the current Ritz vectors.