Anasazi  Version of the Day
AnasaziStatusTestResNorm.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 
30 #ifndef ANASAZI_STATUS_TEST_RESNORM_HPP
31 #define ANASAZI_STATUS_TEST_RESNORM_HPP
32 
39 #include "AnasaziStatusTest.hpp"
40 #include "Teuchos_ScalarTraits.hpp"
41 namespace Anasazi {
42 
44 
45 
54  class ResNormNaNError : public AnasaziError {public:
55  ResNormNaNError(const std::string& what_arg) : AnasaziError(what_arg)
56  {}};
57 
59 
76  template <class ScalarType, class MV, class OP>
77  class StatusTestResNorm : public StatusTest<ScalarType,MV,OP> {
78 
79  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
80 
81  public:
82 
86  enum ResType {
87  RES_ORTH,
88  RES_2NORM,
89  RITZRES_2NORM
90  };
91 
93 
94 
96  StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum = -1, ResType whichNorm = RES_ORTH, bool scaled = true, bool throwExceptionOnNaN = true);
97 
99  virtual ~StatusTestResNorm() {};
101 
103 
104 
108  TestStatus checkStatus( Eigensolver<ScalarType,MV,OP>* solver );
109 
111  TestStatus getStatus() const { return state_; }
112 
114  std::vector<int> whichVecs() const {
115  return ind_;
116  }
117 
119  int howMany() const {
120  return ind_.size();
121  }
122 
124 
126 
127 
133  void setQuorum(int quorum) {
134  state_ = Undefined;
135  quorum_ = quorum;
136  }
137 
140  int getQuorum() const {
141  return quorum_;
142  }
143 
148  state_ = Undefined;
149  tol_ = tol;
150  }
151 
154 
159  void setWhichNorm(ResType whichNorm) {
160  state_ = Undefined;
161  whichNorm_ = whichNorm;
162  }
163 
165  ResType getWhichNorm() {return whichNorm_;}
166 
170  void setScale(bool relscale) {
171  state_ = Undefined;
172  scaled_ = relscale;
173  }
174 
176  bool getScale() {return scaled_;}
178 
180 
181 
187  void reset() {
188  ind_.resize(0);
189  state_ = Undefined;
190  }
191 
193 
198  void clearStatus() {
199  ind_.resize(0);
200  state_ = Undefined;
201  }
202 
204 
206 
207 
209  std::ostream& print(std::ostream& os, int indent = 0) const;
210 
212  private:
213  TestStatus state_;
214  MagnitudeType tol_;
215  std::vector<int> ind_;
216  int quorum_;
217  bool scaled_;
218  ResType whichNorm_;
219  bool throwExceptionOnNaN_;
220  };
221 
222 
223  template <class ScalarType, class MV, class OP>
224  StatusTestResNorm<ScalarType,MV,OP>::StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum, ResType whichNorm, bool scaled, bool throwExceptionOnNaN)
225  : state_(Undefined), tol_(tol), quorum_(quorum), scaled_(scaled), whichNorm_(whichNorm), throwExceptionOnNaN_(throwExceptionOnNaN)
226  {}
227 
228  template <class ScalarType, class MV, class OP>
230  {
232 
233  std::vector<MagnitudeType> res;
234 
235  // get the eigenvector/ritz residuals norms (using the appropriate norm)
236  // get the eigenvalues/ritzvalues and ritz index as well
237  std::vector<Value<ScalarType> > vals = solver->getRitzValues();
238  switch (whichNorm_) {
239  case RES_2NORM:
240  res = solver->getRes2Norms();
241  // we want only the ritz values corresponding to our eigenvector residuals
242  vals.resize(res.size());
243  break;
244  case RES_ORTH:
245  res = solver->getResNorms();
246  // we want only the ritz values corresponding to our eigenvector residuals
247  vals.resize(res.size());
248  break;
249  case RITZRES_2NORM:
250  res = solver->getRitzRes2Norms();
251  break;
252  }
253 
254  // if appropriate, scale the norms by the magnitude of the eigenvalue estimate
255  if (scaled_) {
257 
258  for (unsigned int i=0; i<res.size(); i++) {
259  MagnitudeType tmp = lapack.LAPY2(vals[i].realpart,vals[i].imagpart);
260  // scale by the newly computed magnitude of the ritz values
261  if ( tmp != MT::zero() ) {
262  res[i] /= tmp;
263  }
264  }
265  }
266 
267  // test the norms
268  int have = 0;
269  ind_.resize(res.size());
270  for (unsigned int i=0; i<res.size(); i++) {
271  TEUCHOS_TEST_FOR_EXCEPTION( MT::isnaninf(res[i]), ResNormNaNError,
272  "StatusTestResNorm::checkStatus(): residual norm is nan or inf" );
273  if (res[i] < tol_) {
274  ind_[have] = i;
275  have++;
276  }
277  }
278  ind_.resize(have);
279  int need = (quorum_ == -1) ? res.size() : quorum_;
280  state_ = (have >= need) ? Passed : Failed;
281  return state_;
282  }
283 
284 
285  template <class ScalarType, class MV, class OP>
286  std::ostream& StatusTestResNorm<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
287  {
288  std::string ind(indent,' ');
289  os << ind << "- StatusTestResNorm: ";
290  switch (state_) {
291  case Passed:
292  os << "Passed" << std::endl;
293  break;
294  case Failed:
295  os << "Failed" << std::endl;
296  break;
297  case Undefined:
298  os << "Undefined" << std::endl;
299  break;
300  }
301  os << ind << " (Tolerance,WhichNorm,Scaled,Quorum): "
302  << "(" << tol_;
303  switch (whichNorm_) {
304  case RES_ORTH:
305  os << ",RES_ORTH";
306  break;
307  case RES_2NORM:
308  os << ",RES_2NORM";
309  break;
310  case RITZRES_2NORM:
311  os << ",RITZRES_2NORM";
312  break;
313  }
314  os << "," << (scaled_ ? "true" : "false")
315  << "," << quorum_
316  << ")" << std::endl;
317 
318  if (state_ != Undefined) {
319  os << ind << " Which vectors: ";
320  if (ind_.size() > 0) {
321  for (unsigned int i=0; i<ind_.size(); i++) os << ind_[i] << " ";
322  os << std::endl;
323  }
324  else {
325  os << "[empty]" << std::endl;
326  }
327  }
328  return os;
329  }
330 
331 
332 } // end of Anasazi namespace
333 
334 #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).
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.
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
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 ("not a number") is detect...
Teuchos::ScalarTraits< ScalarType >::magnitudeType getTolerance()
Get tolerance.
virtual std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRes2Norms()=0
int howMany() const
Get the number of vectors that passed the test.
TestStatus checkStatus(Eigensolver< ScalarType, MV, OP > *solver)
void setQuorum(int quorum)
Set quorum.
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.
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.
ScalarType LAPY2(const ScalarType x, const ScalarType y) const
void clearStatus()
Clears the results of the last status test.
Pure virtual base class for defining the status testing capabilities of Anasazi.
int getQuorum() const
Get quorum.