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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
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 
38 #include "AnasaziTypes.hpp"
39 #include "AnasaziStatusTest.hpp"
40 #include "Teuchos_ScalarTraits.hpp"
41 
42 namespace Anasazi {
43 
45 
46 
55  class ResNormNaNError : public AnasaziError {public:
56  ResNormNaNError(const std::string& what_arg) : AnasaziError(what_arg)
57  {}};
58 
60 
77  template <class ScalarType, class MV, class OP>
78  class StatusTestResNorm : public StatusTest<ScalarType,MV,OP> {
79 
80  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
81 
82  public:
83 
85 
86 
88  StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum = -1, ResType whichNorm = RES_ORTH, bool scaled = true, bool throwExceptionOnNaN = true);
89 
91  virtual ~StatusTestResNorm() {};
93 
95 
96 
101 
103  TestStatus getStatus() const { return state_; }
104 
106  std::vector<int> whichVecs() const {
107  return ind_;
108  }
109 
111  int howMany() const {
112  return ind_.size();
113  }
114 
116 
118 
119 
125  void setQuorum(int quorum) {
126  state_ = Undefined;
127  quorum_ = quorum;
128  }
129 
132  int getQuorum() const {
133  return quorum_;
134  }
135 
139  void setTolerance(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol) {
140  state_ = Undefined;
141  tol_ = tol;
142  }
143 
145  typename Teuchos::ScalarTraits<ScalarType>::magnitudeType getTolerance() {return tol_;}
146 
151  void setWhichNorm(ResType whichNorm) {
152  state_ = Undefined;
153  whichNorm_ = whichNorm;
154  }
155 
157  ResType getWhichNorm() {return whichNorm_;}
158 
162  void setScale(bool relscale) {
163  state_ = Undefined;
164  scaled_ = relscale;
165  }
166 
168  bool getScale() {return scaled_;}
170 
172 
173 
179  void reset() {
180  ind_.resize(0);
181  state_ = Undefined;
182  }
183 
185 
190  void clearStatus() {
191  ind_.resize(0);
192  state_ = Undefined;
193  }
194 
196 
198 
199 
201  std::ostream& print(std::ostream& os, int indent = 0) const;
202 
204  private:
205  TestStatus state_;
206  MagnitudeType tol_;
207  std::vector<int> ind_;
208  int quorum_;
209  bool scaled_;
210  enum ResType whichNorm_;
211  bool throwExceptionOnNaN_;
212  };
213 
214 
215  template <class ScalarType, class MV, class OP>
216  StatusTestResNorm<ScalarType,MV,OP>::StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum, ResType whichNorm, bool scaled, bool throwExceptionOnNaN)
217  : state_(Undefined), tol_(tol), quorum_(quorum), scaled_(scaled), whichNorm_(whichNorm), throwExceptionOnNaN_(throwExceptionOnNaN)
218  {}
219 
220  template <class ScalarType, class MV, class OP>
222  {
223  typedef Teuchos::ScalarTraits<MagnitudeType> MT;
224 
225  std::vector<MagnitudeType> res;
226 
227  // get the eigenvector/ritz residuals norms (using the appropriate norm)
228  // get the eigenvalues/ritzvalues and ritz index as well
229  std::vector<Value<ScalarType> > vals = solver->getRitzValues();
230  switch (whichNorm_) {
231  case RES_2NORM:
232  res = solver->getRes2Norms();
233  // we want only the ritz values corresponding to our eigenvector residuals
234  vals.resize(res.size());
235  break;
236  case RES_ORTH:
237  res = solver->getResNorms();
238  // we want only the ritz values corresponding to our eigenvector residuals
239  vals.resize(res.size());
240  break;
241  case RITZRES_2NORM:
242  res = solver->getRitzRes2Norms();
243  break;
244  }
245 
246  // if appropriate, scale the norms by the magnitude of the eigenvalue estimate
247  if (scaled_) {
248  Teuchos::LAPACK<int,MagnitudeType> lapack;
249 
250  for (unsigned int i=0; i<res.size(); i++) {
251  MagnitudeType tmp = lapack.LAPY2(vals[i].realpart,vals[i].imagpart);
252  // scale by the newly computed magnitude of the ritz values
253  if ( tmp != MT::zero() ) {
254  res[i] /= tmp;
255  }
256  }
257  }
258 
259  // test the norms
260  int have = 0;
261  ind_.resize(res.size());
262  for (unsigned int i=0; i<res.size(); i++) {
263  TEUCHOS_TEST_FOR_EXCEPTION( MT::isnaninf(res[i]), ResNormNaNError,
264  "StatusTestResNorm::checkStatus(): residual norm is nan or inf" );
265  if (res[i] < tol_) {
266  ind_[have] = i;
267  have++;
268  }
269  }
270  ind_.resize(have);
271  int need = (quorum_ == -1) ? res.size() : quorum_;
272  state_ = (have >= need) ? Passed : Failed;
273  return state_;
274  }
275 
276 
277  template <class ScalarType, class MV, class OP>
278  std::ostream& StatusTestResNorm<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
279  {
280  std::string ind(indent,' ');
281  os << ind << "- StatusTestResNorm: ";
282  switch (state_) {
283  case Passed:
284  os << "Passed" << std::endl;
285  break;
286  case Failed:
287  os << "Failed" << std::endl;
288  break;
289  case Undefined:
290  os << "Undefined" << std::endl;
291  break;
292  }
293  os << ind << " (Tolerance,WhichNorm,Scaled,Quorum): "
294  << "(" << tol_;
295  switch (whichNorm_) {
296  case RES_ORTH:
297  os << ",RES_ORTH";
298  break;
299  case RES_2NORM:
300  os << ",RES_2NORM";
301  break;
302  case RITZRES_2NORM:
303  os << ",RITZRES_2NORM";
304  break;
305  }
306  os << "," << (scaled_ ? "true" : "false")
307  << "," << quorum_
308  << ")" << std::endl;
309 
310  if (state_ != Undefined) {
311  os << ind << " Which vectors: ";
312  if (ind_.size() > 0) {
313  for (unsigned int i=0; i<ind_.size(); i++) os << ind_[i] << " ";
314  os << std::endl;
315  }
316  else {
317  os << "[empty]" << std::endl;
318  }
319  }
320  return os;
321  }
322 
323 
324 } // end of Anasazi namespace
325 
326 #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.
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...
std::ostream & print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
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...
ResType getWhichNorm()
Return the residual norm used by the status test.
virtual ~StatusTestResNorm()
Destructor.
TestStatus getStatus() const
Return the result of the most recent checkStatus call, or undefined if it has not been run...
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.
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.
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.
std::vector< int > whichVecs() const
Get the indices for the vectors that passed the test.