Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziRTRSolMgr.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ANASAZI_RTR_SOLMGR_HPP
11 #define ANASAZI_RTR_SOLMGR_HPP
12 
18 #include "AnasaziConfigDefs.hpp"
19 #include "AnasaziTypes.hpp"
20 
21 #include "AnasaziEigenproblem.hpp"
22 #include "AnasaziSolverManager.hpp"
23 #include "AnasaziSolverUtils.hpp"
24 
25 #include "AnasaziIRTR.hpp"
26 #include "AnasaziSIRTR.hpp"
27 #include "AnasaziBasicSort.hpp"
34 #include "AnasaziOutputManager.hpp"
36 
37 #include "Teuchos_TimeMonitor.hpp"
38 #include "Teuchos_FancyOStream.hpp"
39 
49 namespace Anasazi {
50 
51 template<class ScalarType, class MV, class OP>
52 class RTRSolMgr : public SolverManager<ScalarType,MV,OP> {
53 
54  private:
58  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
60 
61  public:
62 
64 
65 
86 
88  virtual ~RTRSolMgr() {};
90 
92 
93 
96  return *problem_;
97  }
98 
105  return Teuchos::tuple(_timerSolve);
106  }
107 
109  int getNumIters() const {
110  return numIters_;
111  }
112 
113 
115 
117 
118 
127  ReturnType solve();
129 
130  private:
132  std::string whch_;
133  std::string ortho_;
134 
135  bool skinny_;
136  MagnitudeType convtol_;
137  int maxIters_;
138  bool relconvtol_;
139  enum ResType convNorm_;
140  int numIters_;
141  int numICGS_;
142  int blkSize_;
143 
144  Teuchos::RCP<Teuchos::Time> _timerSolve;
147 };
148 
149 
151 template<class ScalarType, class MV, class OP>
154  Teuchos::ParameterList &pl ) :
155  problem_(problem),
156  whch_("SR"),
157  skinny_(true),
158  convtol_(MT::prec()),
159  maxIters_(100),
160  relconvtol_(true),
161  numIters_(-1),
162 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
163  _timerSolve(Teuchos::TimeMonitor::getNewTimer("Anasazi: RTRSolMgr::solve()")),
164 #endif
165  pl_(pl)
166 {
167  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
168  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(), std::invalid_argument, "Problem not set.");
169  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isHermitian(), std::invalid_argument, "Problem not symmetric.");
170  TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument, "Problem does not contain initial vectors to clone from.");
171 
172  std::string strtmp;
173 
174  whch_ = pl_.get("Which","SR");
175  TEUCHOS_TEST_FOR_EXCEPTION(whch_ != "SR" && whch_ != "LR",
176  std::invalid_argument, "Anasazi::RTRSolMgr: Invalid sorting string. RTR solvers compute only LR or SR.");
177 
178  // convergence tolerance
179  convtol_ = pl_.get("Convergence Tolerance",convtol_);
180  relconvtol_ = pl_.get("Relative Convergence Tolerance",relconvtol_);
181  strtmp = pl_.get("Convergence Norm",std::string("2"));
182  if (strtmp == "2") {
183  convNorm_ = RES_2NORM;
184  }
185  else if (strtmp == "M") {
186  convNorm_ = RES_ORTH;
187  }
188  else {
189  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
190  "Anasazi::RTRSolMgr: Invalid Convergence Norm.");
191  }
192 
193 
194  // maximum number of (outer) iterations
195  maxIters_ = pl_.get("Maximum Iterations",maxIters_);
196 
197  // skinny solver or not
198  skinny_ = pl_.get("Skinny Solver",skinny_);
199 
200  // number if ICGS iterations
201  numICGS_ = pl_.get("Num ICGS",2);
202 
203  // block size
204  blkSize_ = pl_.get("Block Size", problem_->getNEV());
205 
206  // Create a formatted output stream to print to.
207  // See if user requests output processor.
208  int osProc = pl.get("Output Processor", 0);
209 
210  // If not passed in by user, it will be chosen based upon operator type.
212 
213  if (pl.isParameter("Output Stream")) {
214  osp = Teuchos::getParameter<Teuchos::RCP<Teuchos::FancyOStream> >(pl,"Output Stream");
215  }
216  else {
217  osp = OutputStreamTraits<OP>::getOutputStream (*problem_->getOperator(), osProc);
218  }
219 
220  int verbosity = Anasazi::Errors;
221  if (pl_.isParameter("Verbosity")) {
222  if (Teuchos::isParameterType<int>(pl_,"Verbosity")) {
223  verbosity = pl_.get("Verbosity", verbosity);
224  } else {
225  verbosity = (int)Teuchos::getParameter<Anasazi::MsgType>(pl_,"Verbosity");
226  }
227  }
228  printer_ = Teuchos::rcp( new OutputManager<ScalarType>(verbosity,osp) );
229 
230 }
231 
232 
233 // solve()
234 template<class ScalarType, class MV, class OP>
237 
238  using std::endl;
239 
240  // typedef SolverUtils<ScalarType,MV,OP> msutils; // unused
241 
242  const int nev = problem_->getNEV();
243 
244  // clear num iters
245  numIters_ = -1;
246 
247 #ifdef TEUCHOS_DEBUG
249  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(printer_->stream(Debug)));
250  out->setShowAllFrontMatter(false).setShowProcRank(true);
251  *out << "Entering Anasazi::RTRSolMgr::solve()\n";
252 #endif
253 
255  // Sort manager
257 
259  // Status tests
260  //
265  // maximum iters
266  if (maxIters_ > 0) {
267  maxtest = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>(maxIters_) );
268  }
269  else {
270  maxtest = Teuchos::null;
271  }
272  //
273  // convergence
274  convtest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(convtol_,nev,convNorm_,relconvtol_) );
275  ordertest = Teuchos::rcp( new StatusTestWithOrdering<ScalarType,MV,OP>(convtest,sorter,nev) );
276  //
277  // combo
279  alltests.push_back(ordertest);
280  if (maxtest != Teuchos::null) alltests.push_back(maxtest);
282  //
283  // printing StatusTest
285  if ( printer_->isVerbosity(Debug) ) {
286  outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer_,combotest,1,Passed+Failed+Undefined ) );
287  }
288  else {
289  outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer_,combotest,1,Passed ) );
290  }
291 
293  // Orthomanager
295  = Teuchos::rcp( new ICGSOrthoManager<ScalarType,MV,OP>(problem_->getM(),numICGS_) );
296 
298  // create an RTR solver
299  // leftmost or rightmost?
300  bool leftMost = true;
301  if (whch_ == "LR" || whch_ == "LM") {
302  leftMost = false;
303  }
304  pl_.set<bool>("Leftmost",leftMost);
306  if (skinny_ == false) {
307  // "hefty" IRTR
308  rtr_solver = Teuchos::rcp( new IRTR<ScalarType,MV,OP>(problem_,sorter,printer_,outputtest,ortho,pl_) );
309  }
310  else {
311  // "skinny" IRTR
312  rtr_solver = Teuchos::rcp( new SIRTR<ScalarType,MV,OP>(problem_,sorter,printer_,outputtest,ortho,pl_) );
313  }
314  // set any auxiliary vectors defined in the problem
315  Teuchos::RCP< const MV > probauxvecs = problem_->getAuxVecs();
316  if (probauxvecs != Teuchos::null) {
317  rtr_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(probauxvecs) );
318  }
319 
320  TEUCHOS_TEST_FOR_EXCEPTION(rtr_solver->getBlockSize() < problem_->getNEV(),std::logic_error,
321  "Anasazi::RTRSolMgr requires block size >= requested number of eigenvalues.");
322 
323  int numfound = 0;
324  Teuchos::RCP<MV> foundvecs;
325  std::vector<MagnitudeType> foundvals;
326 
327  // tell the solver to iterate
328  try {
329 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
330  Teuchos::TimeMonitor slvtimer(*_timerSolve);
331 #endif
332  rtr_solver->iterate();
333  numIters_ = rtr_solver->getNumIters();
334  }
335  catch (const std::exception &e) {
336  // we are a simple solver manager. we don't catch exceptions. set solution empty, then rethrow.
337  printer_->stream(Anasazi::Errors) << "Exception: " << e.what() << endl;
339  sol.numVecs = 0;
340  problem_->setSolution(sol);
341  throw;
342  }
343 
344  // check the status tests
345  if (convtest->getStatus() == Passed || (maxtest != Teuchos::null && maxtest->getStatus() == Passed))
346  {
347  int num = convtest->howMany();
348  if (num > 0) {
349  std::vector<int> ind = convtest->whichVecs();
350  // copy the converged eigenvectors
351  foundvecs = MVT::CloneCopy(*rtr_solver->getRitzVectors(),ind);
352  // copy the converged eigenvalues
353  foundvals.resize(num);
354  std::vector<Value<ScalarType> > all = rtr_solver->getRitzValues();
355  for (int i=0; i<num; i++) {
356  foundvals[i] = all[ind[i]].realpart;
357  }
358  numfound = num;
359  }
360  }
361  else {
362  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Anasazi::RTRSolMgr::solve(): solver returned without satisfy status test.");
363  }
364 
365  // create contiguous storage for all eigenvectors, eigenvalues
367  sol.numVecs = numfound;
368  sol.Evecs = foundvecs;
369  sol.Espace = sol.Evecs;
370  sol.Evals.resize(sol.numVecs);
371  for (int i=0; i<sol.numVecs; i++) {
372  sol.Evals[i].realpart = foundvals[i];
373  }
374  // all real eigenvalues: set index vectors [0,...,numfound-1]
375  sol.index.resize(numfound,0);
376 
377  // print final summary
378  rtr_solver->currentStatus(printer_->stream(FinalSummary));
379 
380  // print timing information
381 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
382  if ( printer_->isVerbosity( TimingDetails ) ) {
383  Teuchos::TimeMonitor::summarize( printer_->stream( TimingDetails ) );
384  }
385 #endif
386 
387  // send the solution to the eigenproblem
388  problem_->setSolution(sol);
389  printer_->stream(Debug) << "Returning " << sol.numVecs << " eigenpairs to eigenproblem." << endl;
390 
391  // return from SolMgr::solve()
392  if (sol.numVecs < nev) return Unconverged;
393  return Converged;
394 }
395 
396 
397 
398 
399 } // end Anasazi namespace
400 
401 #endif /* ANASAZI_RTR_SOLMGR_HPP */
Pure virtual base class which describes the basic interface for a solver manager. ...
std::vector< Value< ScalarType > > Evals
The computed eigenvalues.
ResType
Enumerated type used to specify which residual norm used by residual norm status tests.
A special StatusTest for printing other status tests.
This class defines the interface required by an eigensolver and status test class to compute solution...
RTRSolMgr(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, Teuchos::ParameterList &pl)
Basic constructor for RTRSolMgr.
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)
#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.
An implementation of the Anasazi::GenOrthoManager that performs orthogonalization using iterated clas...
Status test for forming logical combinations of other status tests.
The Anasazi::SolverManager is a templated virtual base class that defines the basic interface that an...
The Anasazi::RTRSolMgr provides a simple solver manager over the RTR eigensolver. For more informatio...
virtual ~RTRSolMgr()
Destructor.
Basic implementation of the Anasazi::SortManager class.
int getNumIters() const
Get the iteration count for the most recent call to solve.
Output managers remove the need for the eigensolver to know any information about the required output...
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
bool isParameter(const std::string &name) const
int numVecs
The number of computed eigenpairs.
ReturnType solve()
This method performs possibly repeated calls to the underlying eigensolver&#39;s iterate() routine until ...
Teuchos::RCP< MV > Espace
An orthonormal basis for the computed eigenspace.
Abstract class definition for Anasazi Output Managers.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract base class which defines the interface required by an eigensolver and status test class to c...
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="", const bool ignoreZeroTimers=false)
ReturnType
Enumerated type used to pass back information from a solver manager.
Output managers remove the need for the eigensolver to know any information about the required output...
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...
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
Struct for storing an eigenproblem solution.
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Return the eigenvalue problem.
A status test for testing the number of iterations.
Status test for testing the number of iterations.
void push_back(const value_type &x)
Special StatusTest for printing status tests.
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
Status test for forming logical combinations of other status tests.
Types and exceptions used within Anasazi solvers and interfaces.
Abstract class definition for Anasazi output stream.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
A status test for testing the norm of the eigenvectors residuals.
Basic implementation of the Anasazi::OrthoManager class.
Class which provides internal utilities for the Anasazi solvers.