Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziStatusTestResNorm.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 
11 #ifndef ANASAZI_STATUS_TEST_RESNORM_HPP
12 #define ANASAZI_STATUS_TEST_RESNORM_HPP
13 
19 #include "AnasaziTypes.hpp"
20 #include "AnasaziStatusTest.hpp"
21 #include "Teuchos_ScalarTraits.hpp"
22 #include "Teuchos_LAPACK.hpp"
23 
24 namespace Anasazi {
25 
27 
28 
37  class ResNormNaNError : public AnasaziError {public:
38  ResNormNaNError(const std::string& what_arg) : AnasaziError(what_arg)
39  {}};
40 
42 
59  template <class ScalarType, class MV, class OP>
60  class StatusTestResNorm : public StatusTest<ScalarType,MV,OP> {
61 
62  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
63 
64  public:
65 
67 
68 
70  StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum = -1, ResType whichNorm = RES_ORTH, bool scaled = true, bool throwExceptionOnNaN = true);
71 
73  virtual ~StatusTestResNorm() {};
75 
77 
78 
83 
85  TestStatus getStatus() const { return state_; }
86 
88  std::vector<int> whichVecs() const {
89  return ind_;
90  }
91 
93  int howMany() const {
94  return ind_.size();
95  }
96 
98 
100 
101 
107  void setQuorum(int quorum) {
108  state_ = Undefined;
109  quorum_ = quorum;
110  }
111 
114  int getQuorum() const {
115  return quorum_;
116  }
117 
122  state_ = Undefined;
123  tol_ = tol;
124  }
125 
128 
133  void setWhichNorm(ResType whichNorm) {
134  state_ = Undefined;
135  whichNorm_ = whichNorm;
136  }
137 
139  ResType getWhichNorm() {return whichNorm_;}
140 
144  void setScale(bool relscale) {
145  state_ = Undefined;
146  scaled_ = relscale;
147  }
148 
150  bool getScale() {return scaled_;}
152 
154 
155 
161  void reset() {
162  ind_.resize(0);
163  state_ = Undefined;
164  }
165 
167 
172  void clearStatus() {
173  ind_.resize(0);
174  state_ = Undefined;
175  }
176 
178 
180 
181 
183  std::ostream& print(std::ostream& os, int indent = 0) const;
184 
186  private:
187  TestStatus state_;
188  MagnitudeType tol_;
189  std::vector<int> ind_;
190  int quorum_;
191  bool scaled_;
192  enum ResType whichNorm_;
193  bool throwExceptionOnNaN_;
194  };
195 
196 
197  template <class ScalarType, class MV, class OP>
198  StatusTestResNorm<ScalarType,MV,OP>::StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum, ResType whichNorm, bool scaled, bool throwExceptionOnNaN)
199  : state_(Undefined), tol_(tol), quorum_(quorum), scaled_(scaled), whichNorm_(whichNorm), throwExceptionOnNaN_(throwExceptionOnNaN)
200  {}
201 
202  template <class ScalarType, class MV, class OP>
204  {
206 
207  std::vector<MagnitudeType> res;
208 
209  // get the eigenvector/ritz residuals norms (using the appropriate norm)
210  // get the eigenvalues/ritzvalues and ritz index as well
211  std::vector<Value<ScalarType> > vals = solver->getRitzValues();
212  switch (whichNorm_) {
213  case RES_2NORM:
214  res = solver->getRes2Norms();
215  // we want only the ritz values corresponding to our eigenvector residuals
216  vals.resize(res.size());
217  break;
218  case RES_ORTH:
219  res = solver->getResNorms();
220  // we want only the ritz values corresponding to our eigenvector residuals
221  vals.resize(res.size());
222  break;
223  case RITZRES_2NORM:
224  res = solver->getRitzRes2Norms();
225  break;
226  }
227 
228  // if appropriate, scale the norms by the magnitude of the eigenvalue estimate
229  if (scaled_) {
231 
232  for (unsigned int i=0; i<res.size(); i++) {
233  MagnitudeType tmp = lapack.LAPY2(vals[i].realpart,vals[i].imagpart);
234  // scale by the newly computed magnitude of the ritz values
235  if ( tmp != MT::zero() ) {
236  res[i] /= tmp;
237  }
238  }
239  }
240 
241  // test the norms
242  int have = 0;
243  ind_.resize(res.size());
244  for (unsigned int i=0; i<res.size(); i++) {
245  TEUCHOS_TEST_FOR_EXCEPTION( MT::isnaninf(res[i]), ResNormNaNError,
246  "StatusTestResNorm::checkStatus(): residual norm is nan or inf" );
247  if (res[i] < tol_) {
248  ind_[have] = i;
249  have++;
250  }
251  }
252  ind_.resize(have);
253  int need = (quorum_ == -1) ? res.size() : quorum_;
254  state_ = (have >= need) ? Passed : Failed;
255  return state_;
256  }
257 
258 
259  template <class ScalarType, class MV, class OP>
260  std::ostream& StatusTestResNorm<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
261  {
262  std::string ind(indent,' ');
263  os << ind << "- StatusTestResNorm: ";
264  switch (state_) {
265  case Passed:
266  os << "Passed" << std::endl;
267  break;
268  case Failed:
269  os << "Failed" << std::endl;
270  break;
271  case Undefined:
272  os << "Undefined" << std::endl;
273  break;
274  }
275  os << ind << " (Tolerance,WhichNorm,Scaled,Quorum): "
276  << "(" << tol_;
277  switch (whichNorm_) {
278  case RES_ORTH:
279  os << ",RES_ORTH";
280  break;
281  case RES_2NORM:
282  os << ",RES_2NORM";
283  break;
284  case RITZRES_2NORM:
285  os << ",RITZRES_2NORM";
286  break;
287  }
288  os << "," << (scaled_ ? "true" : "false")
289  << "," << quorum_
290  << ")" << std::endl;
291 
292  if (state_ != Undefined) {
293  os << ind << " Which vectors: ";
294  if (ind_.size() > 0) {
295  for (unsigned int i=0; i<ind_.size(); i++) os << ind_[i] << " ";
296  os << std::endl;
297  }
298  else {
299  os << "[empty]" << std::endl;
300  }
301  }
302  return os;
303  }
304 
305 
306 } // end of Anasazi namespace
307 
308 #endif /* ANASAZI_STATUS_TEST_RESNORM_HPP */
virtual std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getResNorms()=0
Get the current residual norms.
bool getScale()
Returns true if the test scales the norms by the eigenvalue estimates (relative scale).
ResType
Enumerated type used to specify which residual norm used by residual norm status tests.
virtual std::vector< Value< ScalarType > > getRitzValues()=0
Get the Ritz values from the previous iteration.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TestStatus getStatus() const
Return the result of the most recent checkStatus call, or undefined if it has not been run...
void setTolerance(typename Teuchos::ScalarTraits< ScalarType >::magnitudeType tol)
Set tolerance. This also resets the test status to Undefined.
An exception class parent to all Anasazi exceptions.
void reset()
Informs the status test that it should reset its internal configuration to the uninitialized state...
TestStatus
Enumerated type used to pass back information from a StatusTest.
std::ostream & print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
ResType getWhichNorm()
Return the residual norm used by the status test.
virtual ~StatusTestResNorm()
Destructor.
virtual std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRitzRes2Norms()=0
ResNormNaNError is thrown from StatusTestResNorm::checkStatus() when a NaN (&quot;not a number&quot;) is detect...
Teuchos::ScalarTraits< ScalarType >::magnitudeType getTolerance()
Get tolerance.
ScalarType LAPY2(const ScalarType &x, const ScalarType &y) const
virtual std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRes2Norms()=0
int howMany() const
Get the number of vectors that passed the test.
Types and exceptions used within Anasazi solvers and interfaces.
TestStatus checkStatus(Eigensolver< ScalarType, MV, OP > *solver)
void setQuorum(int quorum)
Set quorum.
Common interface of stopping criteria for Anasazi&#39;s solvers.
A status test for testing the norm of the eigenvectors residuals.
void setScale(bool relscale)
Instruct test to scale norms by eigenvalue estimates (relative scale). This also resets the test stat...
StatusTestResNorm(typename Teuchos::ScalarTraits< ScalarType >::magnitudeType tol, int quorum=-1, ResType whichNorm=RES_ORTH, bool scaled=true, bool throwExceptionOnNaN=true)
Constructor.
std::vector< int > whichVecs() const
Get the indices for the vectors that passed the test.
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
void setWhichNorm(ResType whichNorm)
Set the residual norm to be used by the status test.
void clearStatus()
Clears the results of the last status test.
Declaration and definition of Anasazi::StatusTest.
int getQuorum() const
Get quorum.