Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosLSQRStatusTest.hpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Belos: Block Linear Solvers Package
6 // Copyright 2004 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef BELOS_LSQR_STATUS_TEST_HPP
45 #define BELOS_LSQR_STATUS_TEST_HPP
46 
52 #include "BelosStatusTest.hpp"
53 #include "BelosLSQRIter.hpp"
54 
61 namespace Belos {
62 
63 
64 template <class ScalarType, class MV, class OP>
65 class LSQRStatusTest: public Belos::StatusTest<ScalarType,MV,OP> {
66 
67 public:
68 
69  // Convenience typedefs
73 
75 
76 
78 
83  LSQRStatusTest( MagnitudeType condMax = 0.0,
84  int term_iter_max = 1,
85  MagnitudeType rel_rhs_err = 0.0,
86  MagnitudeType rel_mat_err = 0.0 );
87 
89  virtual ~LSQRStatusTest();
91 
93 
94 
96 
99 
101  Belos::StatusType getStatus() const {return(status_);}
102 
104 
106 
107 
109  void reset();
110 
112  int setCondLim(MagnitudeType condMax) {
113  condMax_ = condMax;
115  return(0);}
116 
117  int setTermIterMax(int term_iter_max) {
118  term_iter_max_ = term_iter_max;
119  if (term_iter_max_ < 1)
120  term_iter_max_ = 1;
121  return(0);}
122 
123  int setRelRhsErr(MagnitudeType rel_rhs_err) {
124  rel_rhs_err_ = rel_rhs_err;
125  return(0);}
126 
127  int setRelMatErr(MagnitudeType rel_mat_err) {
128  rel_mat_err_ = rel_mat_err;
129  return(0);}
130 
132 
134 
135 
137  MagnitudeType getCondMaxLim() const {return(condMax_);}
138 
140  int getTermIterMax() const {return(term_iter_max_);}
141 
143  MagnitudeType getRelRhsErr() const {return(rel_rhs_err_);}
144 
146  MagnitudeType getMatErr() const {return(rel_mat_err_);}
147 
149  MagnitudeType getMatCondNum() const {return(matCondNum_);}
150 
152  MagnitudeType getMatNorm() const {return(matNorm_);}
153 
155  int getTermIter() const { return term_iter_; }
156 
158  MagnitudeType getResidNorm() const {return(resNorm_);}
159 
161  MagnitudeType getLSResidNorm() const {return(matResNorm_);}
163 
164 
166 
167 
169  void print(std::ostream& os, int indent = 0) const;
170 
172  void printStatus(std::ostream& os, Belos::StatusType type) const;
173 
175 
178 
183 
186 
188  std::string description() const
189  {
190  std::ostringstream oss;
191  oss << "LSQRStatusTest<>: [ limit of condition number = " << condMax_ << " ]";
192  return oss.str();
193  }
195 
196 private:
197 
199 
200 
202  MagnitudeType condMax_;
203 
205  int term_iter_max_;
206 
208  MagnitudeType rel_rhs_err_;
209 
211  MagnitudeType rel_mat_err_;
212 
214  MagnitudeType rcondMin_;
215 
217  Belos::StatusType status_;
218 
219  // term_iter_ records the number of consecutive "successful" iterations.
220  // convergence requires that term_iter_max consecutive iterates satisfy the other convergence tests
221  int term_iter_;
222 
223  // condition number of the operator
224  MagnitudeType matCondNum_;
225 
226  // Frobenius norm of the operator
227  MagnitudeType matNorm_;
228 
229  // residual norm for the linear system
230  MagnitudeType resNorm_;
231 
232  // least squares residual, operator^Transpose * residual
233  MagnitudeType matResNorm_;
234 
236 
237 };
238 
239 template <class ScalarType, class MV, class OP>
240 LSQRStatusTest<ScalarType,MV,OP>::LSQRStatusTest( MagnitudeType condMax /* = 0 */, int term_iter_max /* = 1 */, MagnitudeType rel_rhs_err /* = 0 */, MagnitudeType rel_mat_err /* = 0 */)
241  : condMax_(condMax),
242  term_iter_max_ (term_iter_max),
243  rel_rhs_err_ (rel_rhs_err),
244  rel_mat_err_ (rel_mat_err),
245  status_ (Belos::Undefined),
246  term_iter_ (0),
247  matCondNum_ ( Teuchos::ScalarTraits<MagnitudeType>::one() ),
248  matNorm_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() ),
249  resNorm_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() ),
250  matResNorm_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() )
251 {}
252 
253 template <class ScalarType, class MV, class OP>
255 {}
256 
257 template <class ScalarType, class MV, class OP>
259 {
260  status_ = Belos::Undefined;
261 }
262 
263 template <class ScalarType, class MV, class OP>
265 {
268  if (condMax_ > MTzero )
269  {
270  rcondMin_ = MTone / condMax_;
271  }
272  else
273  {
275  }
276 
277  bool termIterFlag = false;
278  LSQRIter<ScalarType,MV,OP>* solver = dynamic_cast< LSQRIter<ScalarType,MV,OP>* > (iSolver);
280  //
281  // LSQR solves a least squares problem. A converged preconditioned residual norm
282  // suffices for convergence, but is not necessary. LSQR sometimes returns a larger
283  // relative residual norm than what would have been returned by a linear solver.
284  // This section evaluates three stopping criteria. In the Solver Manager, this test
285  // is combined with a generic number of iteration test.
286  // If the linear system includes a preconditioner, then the least squares problem
287  // is solved for the preconditioned linear system. Preconditioning changes the least
288  // squares problem (in the sense of changing the norms), and the solution depends
289  // on the preconditioner in this sense.
290  // In the context of Linear Least Squares problems, preconditioning refers
291  // to the regularization matrix. Here the regularization matrix is always a scalar
292  // multiple of the identity (standard form least squres).
293  // The "loss of accuracy" concept is not yet implemented here, becuase it is unclear
294  // what this means for linear least squares. LSQR solves an inconsistent system
295  // in a least-squares sense. "Loss of accuracy" would correspond to
296  // the difference between the preconditioned residual and the unpreconditioned residual.
297  //
298 
299  std::cout << " X " << state.sol_norm
300  << " b-AX " << state.resid_norm
301  << " Atr " << state.mat_resid_norm
302  << " A " << state.frob_mat_norm
303  << " cond " << state.mat_cond_num
304  << " relResNorm " << state.resid_norm/state.bnorm
305  << " LS " << state.mat_resid_norm /( state.resid_norm * state.frob_mat_norm )
306  << std::endl;
307 
309  const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
310  ScalarType stop_crit_1 = zero; // b = 0, done
311  if( state.bnorm > zero )
312  {
313  stop_crit_1 = state.resid_norm / state.bnorm;
314  }
315  ScalarType stop_crit_2 = zero;
316  if( state.frob_mat_norm > zero && state.resid_norm > zero )
317  {
318  stop_crit_2 = (state.resid_norm > zero) ? state.mat_resid_norm / (state.frob_mat_norm * state.resid_norm) : zero;
319  }
320  else
321  {
322  if( state.resid_norm == zero )
323  {
324  stop_crit_2 = zero;
325  }
326  else
327  {
328  stop_crit_2 = one; // Initial mat_norm always vanishes
329  }
330  }
331  ScalarType stop_crit_3 = one / state.mat_cond_num;
332  ScalarType resid_tol = rel_rhs_err_ + rel_mat_err_ * state.frob_mat_norm * state.sol_norm / state.bnorm;
333  ScalarType resid_tol_mach = Teuchos::ScalarTraits< MagnitudeType >::eps() + Teuchos::ScalarTraits< MagnitudeType >::eps() * state.frob_mat_norm * state.sol_norm / state.bnorm;
334 
335  // The expected use case for our users is that the linear system will almost
336  // always be compatible, but occasionally may not be. However, some users
337  // may use LSQR for more general cases. This is why we include the full
338  // suite of tests, for both compatible and incompatible systems.
339  //
340  // Users will have to be educated that sometimes they will get an answer X
341  // that does _not_ satisfy the linear system AX=B, but _does_ satisfy the
342  // corresponding least-squares problem. Perhaps the solution manager should
343  // provide them with a way to find out.
344 
345  // stop_crit_1 is for compatible linear systems.
346  // stop_crit_2 is for incompatible linear systems.
347  // stop_crit_3 is for either compatible or incompatible linear systems.
348 
349  // Have we met any of the stopping criteria?
350  if (stop_crit_1 <= resid_tol || stop_crit_2 <= rel_mat_err_ || stop_crit_3 <= rcondMin_ || stop_crit_1 <= resid_tol_mach || stop_crit_2 <= Teuchos::ScalarTraits< MagnitudeType >::eps() || stop_crit_3 <= Teuchos::ScalarTraits< MagnitudeType >::eps()) {
351  termIterFlag = true;
352 
353  if (stop_crit_1 <= resid_tol )
354  std::cout << "Conv: stop_crit_1 " << stop_crit_1 << " resid_tol " << resid_tol << std::endl;
355 
356  if (stop_crit_1 <= resid_tol_mach )
357  std::cout << "Conv: stop_crit_1 " << stop_crit_1 << " resid_tol_mach " << resid_tol_mach << std::endl;
358 
359  if (stop_crit_2 <= rel_mat_err_ )
360  std::cout << "Conv: stop_crit_2 " << stop_crit_2 << " rel_mat_err " << rel_mat_err_ << std::endl;
361 
362  if (stop_crit_2 <= Teuchos::ScalarTraits< MagnitudeType >::eps() )
363  std::cout << "Conv: stop_crit_2 " << stop_crit_2 << " eps " << Teuchos::ScalarTraits< MagnitudeType >::eps() << std::endl;
364 
365  if (stop_crit_3 <= rcondMin_ )
366  std::cout << "Conv: stop_crit_3 " << stop_crit_3 << " rcondMin_ " << rcondMin_ << std::endl;
367 
368  if (stop_crit_3 <= Teuchos::ScalarTraits< MagnitudeType >::eps() )
369  std::cout << "Conv: stop_crit_3 " << stop_crit_3 << " eps " << Teuchos::ScalarTraits< MagnitudeType >::eps() << std::endl;
370  }
371 
372  // update number of consecutive successful iterations
373  if (!termIterFlag) {
374  term_iter_ = 0;
375  } else {
376  term_iter_++;
377  }
378  status_ = (term_iter_ < term_iter_max_) ? Belos::Failed : Belos::Passed;
379 
380  matCondNum_ = state.mat_cond_num; // information that defined convergence
381  matNorm_ = state.frob_mat_norm; // in accessible variables
382  resNorm_ = state.resid_norm;
383  matResNorm_ = state.mat_resid_norm;
384 
385  return status_;
386 }
387 
388 template <class ScalarType, class MV, class OP>
389 void LSQRStatusTest<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
390 {
391  for (int j = 0; j < indent; j++)
392  os << ' ';
393  printStatus(os, status_);
394  os << "limit of condition number = " << condMax_ << std::endl;
395  os << "limit of condition number = " << condMax_ << std::endl;
396 }
397 
398 template <class ScalarType, class MV, class OP>
400 {
401  os << std::left << std::setw(13) << std::setfill('.');
402  switch (type) {
403  case Belos::Passed:
404  os << "Passed";
405  break;
406  case Belos::Failed:
407  os << "Failed";
408  break;
409  case Belos::Undefined:
410  default:
411  os << "Undefined";
412  break;
413  }
414  os << std::left << std::setfill(' ');
415  return;
416 }
417 
418 } // end Belos namespace
419 
420 
421 #endif /* BELOS_LSQR_STATUS_TEST_HPP */
Teuchos::ScalarTraits< ScalarType > SCT
Belos concrete class that iterates LSQR.
MagnitudeType getMatCondNum() const
Returns the value of the observed condition number of Abar.
MagnitudeType getMatErr() const
Returns the value of the estimate of the relative error in the data defining A set in the constructor...
int setTermIterMax(int term_iter_max)
void printStatus(std::ostream &os, Belos::StatusType type) const
Print message for each status specific to this stopping test.
static magnitudeType eps()
void print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
MagnitudeType getResidNorm() const
Returns the value of the observed norm of the residual r = b-Ax.
Pure virtual base class for defining the status testing capabilities of Belos.
MagnitudeType getMatNorm() const
Returns the value of the observed (Frobenius) norm of A.
A pure virtual class for defining the status tests for the Belos iterative solvers.
StatusType
Whether the StatusTest wants iteration to stop.
Definition: BelosTypes.hpp:183
std::string description() const
Method to return description of the maximum iteration status test.
Traits class which defines basic operations on multivectors.
Belos::StatusType getStatus() const
Return the result of the most recent CheckStatus call.
SCT::magnitudeType MagnitudeType
Belos::StatusType checkStatus(Belos::Iteration< ScalarType, MV, OP > *iSolver)
Check convergence status of the iterative solver: Unconverged, Converged, Failed. ...
Belos::MultiVecTraits< ScalarType, MV > MVT
Belos::StatusType firstCallCheckStatusSetup(Belos::Iteration< ScalarType, MV, OP > *iSolver)
Called in checkStatus exactly once, on the first call to checkStatus.
int setCondLim(MagnitudeType condMax)
Set the tolerances.
MagnitudeType getRelRhsErr() const
Returns the value of the estimate of the relative error in the data defining b set in the constructor...
int getTermIter() const
!Returns the current number of successful iterations from the most recent StatusTest call...
LSQRStatusTest(MagnitudeType condMax=0.0, int term_iter_max=1, MagnitudeType rel_rhs_err=0.0, MagnitudeType rel_mat_err=0.0)
Constructor.
int getTermIterMax() const
Returns the number of successful convergent iterations required set in the constructor.
int setRelMatErr(MagnitudeType rel_mat_err)
MagnitudeType getCondMaxLim() const
Returns the value of the upper limit of the condition number of Abar set in the constructor.
LSQRIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
Implementation of the LSQR iteration.
void reset()
Resets the status test to the initial internal state.
virtual ~LSQRStatusTest()
Destructor.
int setRelRhsErr(MagnitudeType rel_rhs_err)
Structure to contain pointers to LSQRIteration state variables, ...
MagnitudeType getLSResidNorm() const
Returns the value of the observed norm of the Least Squares residual A^T r.