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 //
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 #include "Teuchos_LAPACK.hpp"
55 
56 namespace Anasazi {
57 
59 
60 
69  class ResNormNaNError : public AnasaziError {public:
70  ResNormNaNError(const std::string& what_arg) : AnasaziError(what_arg)
71  {}};
72 
74 
91  template <class ScalarType, class MV, class OP>
92  class StatusTestResNorm : public StatusTest<ScalarType,MV,OP> {
93 
94  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
95 
96  public:
97 
99 
100 
102  StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum = -1, ResType whichNorm = RES_ORTH, bool scaled = true, bool throwExceptionOnNaN = true);
103 
105  virtual ~StatusTestResNorm() {};
107 
109 
110 
115 
117  TestStatus getStatus() const { return state_; }
118 
120  std::vector<int> whichVecs() const {
121  return ind_;
122  }
123 
125  int howMany() const {
126  return ind_.size();
127  }
128 
130 
132 
133 
139  void setQuorum(int quorum) {
140  state_ = Undefined;
141  quorum_ = quorum;
142  }
143 
146  int getQuorum() const {
147  return quorum_;
148  }
149 
154  state_ = Undefined;
155  tol_ = tol;
156  }
157 
160 
165  void setWhichNorm(ResType whichNorm) {
166  state_ = Undefined;
167  whichNorm_ = whichNorm;
168  }
169 
171  ResType getWhichNorm() {return whichNorm_;}
172 
176  void setScale(bool relscale) {
177  state_ = Undefined;
178  scaled_ = relscale;
179  }
180 
182  bool getScale() {return scaled_;}
184 
186 
187 
193  void reset() {
194  ind_.resize(0);
195  state_ = Undefined;
196  }
197 
199 
204  void clearStatus() {
205  ind_.resize(0);
206  state_ = Undefined;
207  }
208 
210 
212 
213 
215  std::ostream& print(std::ostream& os, int indent = 0) const;
216 
218  private:
219  TestStatus state_;
220  MagnitudeType tol_;
221  std::vector<int> ind_;
222  int quorum_;
223  bool scaled_;
224  enum ResType whichNorm_;
225  bool throwExceptionOnNaN_;
226  };
227 
228 
229  template <class ScalarType, class MV, class OP>
230  StatusTestResNorm<ScalarType,MV,OP>::StatusTestResNorm(typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol, int quorum, ResType whichNorm, bool scaled, bool throwExceptionOnNaN)
231  : state_(Undefined), tol_(tol), quorum_(quorum), scaled_(scaled), whichNorm_(whichNorm), throwExceptionOnNaN_(throwExceptionOnNaN)
232  {}
233 
234  template <class ScalarType, class MV, class OP>
236  {
238 
239  std::vector<MagnitudeType> res;
240 
241  // get the eigenvector/ritz residuals norms (using the appropriate norm)
242  // get the eigenvalues/ritzvalues and ritz index as well
243  std::vector<Value<ScalarType> > vals = solver->getRitzValues();
244  switch (whichNorm_) {
245  case RES_2NORM:
246  res = solver->getRes2Norms();
247  // we want only the ritz values corresponding to our eigenvector residuals
248  vals.resize(res.size());
249  break;
250  case RES_ORTH:
251  res = solver->getResNorms();
252  // we want only the ritz values corresponding to our eigenvector residuals
253  vals.resize(res.size());
254  break;
255  case RITZRES_2NORM:
256  res = solver->getRitzRes2Norms();
257  break;
258  }
259 
260  // if appropriate, scale the norms by the magnitude of the eigenvalue estimate
261  if (scaled_) {
263 
264  for (unsigned int i=0; i<res.size(); i++) {
265  MagnitudeType tmp = lapack.LAPY2(vals[i].realpart,vals[i].imagpart);
266  // scale by the newly computed magnitude of the ritz values
267  if ( tmp != MT::zero() ) {
268  res[i] /= tmp;
269  }
270  }
271  }
272 
273  // test the norms
274  int have = 0;
275  ind_.resize(res.size());
276  for (unsigned int i=0; i<res.size(); i++) {
277  TEUCHOS_TEST_FOR_EXCEPTION( MT::isnaninf(res[i]), ResNormNaNError,
278  "StatusTestResNorm::checkStatus(): residual norm is nan or inf" );
279  if (res[i] < tol_) {
280  ind_[have] = i;
281  have++;
282  }
283  }
284  ind_.resize(have);
285  int need = (quorum_ == -1) ? res.size() : quorum_;
286  state_ = (have >= need) ? Passed : Failed;
287  return state_;
288  }
289 
290 
291  template <class ScalarType, class MV, class OP>
292  std::ostream& StatusTestResNorm<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
293  {
294  std::string ind(indent,' ');
295  os << ind << "- StatusTestResNorm: ";
296  switch (state_) {
297  case Passed:
298  os << "Passed" << std::endl;
299  break;
300  case Failed:
301  os << "Failed" << std::endl;
302  break;
303  case Undefined:
304  os << "Undefined" << std::endl;
305  break;
306  }
307  os << ind << " (Tolerance,WhichNorm,Scaled,Quorum): "
308  << "(" << tol_;
309  switch (whichNorm_) {
310  case RES_ORTH:
311  os << ",RES_ORTH";
312  break;
313  case RES_2NORM:
314  os << ",RES_2NORM";
315  break;
316  case RITZRES_2NORM:
317  os << ",RITZRES_2NORM";
318  break;
319  }
320  os << "," << (scaled_ ? "true" : "false")
321  << "," << quorum_
322  << ")" << std::endl;
323 
324  if (state_ != Undefined) {
325  os << ind << " Which vectors: ";
326  if (ind_.size() > 0) {
327  for (unsigned int i=0; i<ind_.size(); i++) os << ind_[i] << " ";
328  os << std::endl;
329  }
330  else {
331  os << "[empty]" << std::endl;
332  }
333  }
334  return os;
335  }
336 
337 
338 } // end of Anasazi namespace
339 
340 #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.