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 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 //
42 
43 #ifndef ANASAZI_STATUS_TEST_RESNORM_HPP
44 #define ANASAZI_STATUS_TEST_RESNORM_HPP
45 
51 #include "AnasaziTypes.hpp"
52 #include "AnasaziStatusTest.hpp"
53 #include "Teuchos_ScalarTraits.hpp"
54 
55 namespace Anasazi {
56 
58 
59 
68  class ResNormNaNError : public AnasaziError {public:
69  ResNormNaNError(const std::string& what_arg) : AnasaziError(what_arg)
70  {}};
71 
73 
90  template <class ScalarType, class MV, class OP>
91  class StatusTestResNorm : public StatusTest<ScalarType,MV,OP> {
92 
93  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
94 
95  public:
96 
98 
99 
101  StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum = -1, ResType whichNorm = RES_ORTH, bool scaled = true, bool throwExceptionOnNaN = true);
102 
104  virtual ~StatusTestResNorm() {};
106 
108 
109 
113  TestStatus checkStatus( Eigensolver<ScalarType,MV,OP>* solver );
114 
116  TestStatus getStatus() const { return state_; }
117 
119  std::vector<int> whichVecs() const {
120  return ind_;
121  }
122 
124  int howMany() const {
125  return ind_.size();
126  }
127 
129 
131 
132 
138  void setQuorum(int quorum) {
139  state_ = Undefined;
140  quorum_ = quorum;
141  }
142 
145  int getQuorum() const {
146  return quorum_;
147  }
148 
152  void setTolerance(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol) {
153  state_ = Undefined;
154  tol_ = tol;
155  }
156 
158  typename Teuchos::ScalarTraits<ScalarType>::magnitudeType getTolerance() {return tol_;}
159 
164  void setWhichNorm(ResType whichNorm) {
165  state_ = Undefined;
166  whichNorm_ = whichNorm;
167  }
168 
170  ResType getWhichNorm() {return whichNorm_;}
171 
175  void setScale(bool relscale) {
176  state_ = Undefined;
177  scaled_ = relscale;
178  }
179 
181  bool getScale() {return scaled_;}
183 
185 
186 
192  void reset() {
193  ind_.resize(0);
194  state_ = Undefined;
195  }
196 
198 
203  void clearStatus() {
204  ind_.resize(0);
205  state_ = Undefined;
206  }
207 
209 
211 
212 
214  std::ostream& print(std::ostream& os, int indent = 0) const;
215 
217  private:
218  TestStatus state_;
219  MagnitudeType tol_;
220  std::vector<int> ind_;
221  int quorum_;
222  bool scaled_;
223  enum ResType whichNorm_;
224  bool throwExceptionOnNaN_;
225  };
226 
227 
228  template <class ScalarType, class MV, class OP>
229  StatusTestResNorm<ScalarType,MV,OP>::StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum, ResType whichNorm, bool scaled, bool throwExceptionOnNaN)
230  : state_(Undefined), tol_(tol), quorum_(quorum), scaled_(scaled), whichNorm_(whichNorm), throwExceptionOnNaN_(throwExceptionOnNaN)
231  {}
232 
233  template <class ScalarType, class MV, class OP>
235  {
236  typedef Teuchos::ScalarTraits<MagnitudeType> MT;
237 
238  std::vector<MagnitudeType> res;
239 
240  // get the eigenvector/ritz residuals norms (using the appropriate norm)
241  // get the eigenvalues/ritzvalues and ritz index as well
242  std::vector<Value<ScalarType> > vals = solver->getRitzValues();
243  switch (whichNorm_) {
244  case RES_2NORM:
245  res = solver->getRes2Norms();
246  // we want only the ritz values corresponding to our eigenvector residuals
247  vals.resize(res.size());
248  break;
249  case RES_ORTH:
250  res = solver->getResNorms();
251  // we want only the ritz values corresponding to our eigenvector residuals
252  vals.resize(res.size());
253  break;
254  case RITZRES_2NORM:
255  res = solver->getRitzRes2Norms();
256  break;
257  }
258 
259  // if appropriate, scale the norms by the magnitude of the eigenvalue estimate
260  if (scaled_) {
261  Teuchos::LAPACK<int,MagnitudeType> lapack;
262 
263  for (unsigned int i=0; i<res.size(); i++) {
264  MagnitudeType tmp = lapack.LAPY2(vals[i].realpart,vals[i].imagpart);
265  // scale by the newly computed magnitude of the ritz values
266  if ( tmp != MT::zero() ) {
267  res[i] /= tmp;
268  }
269  }
270  }
271 
272  // test the norms
273  int have = 0;
274  ind_.resize(res.size());
275  for (unsigned int i=0; i<res.size(); i++) {
276  TEUCHOS_TEST_FOR_EXCEPTION( MT::isnaninf(res[i]), ResNormNaNError,
277  "StatusTestResNorm::checkStatus(): residual norm is nan or inf" );
278  if (res[i] < tol_) {
279  ind_[have] = i;
280  have++;
281  }
282  }
283  ind_.resize(have);
284  int need = (quorum_ == -1) ? res.size() : quorum_;
285  state_ = (have >= need) ? Passed : Failed;
286  return state_;
287  }
288 
289 
290  template <class ScalarType, class MV, class OP>
291  std::ostream& StatusTestResNorm<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
292  {
293  std::string ind(indent,' ');
294  os << ind << "- StatusTestResNorm: ";
295  switch (state_) {
296  case Passed:
297  os << "Passed" << std::endl;
298  break;
299  case Failed:
300  os << "Failed" << std::endl;
301  break;
302  case Undefined:
303  os << "Undefined" << std::endl;
304  break;
305  }
306  os << ind << " (Tolerance,WhichNorm,Scaled,Quorum): "
307  << "(" << tol_;
308  switch (whichNorm_) {
309  case RES_ORTH:
310  os << ",RES_ORTH";
311  break;
312  case RES_2NORM:
313  os << ",RES_2NORM";
314  break;
315  case RITZRES_2NORM:
316  os << ",RITZRES_2NORM";
317  break;
318  }
319  os << "," << (scaled_ ? "true" : "false")
320  << "," << quorum_
321  << ")" << std::endl;
322 
323  if (state_ != Undefined) {
324  os << ind << " Which vectors: ";
325  if (ind_.size() > 0) {
326  for (unsigned int i=0; i<ind_.size(); i++) os << ind_[i] << " ";
327  os << std::endl;
328  }
329  else {
330  os << "[empty]" << std::endl;
331  }
332  }
333  return os;
334  }
335 
336 
337 } // end of Anasazi namespace
338 
339 #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.