Belos Version of the Day
Loading...
Searching...
No Matches
BelosLSQRStatusTest.hpp
Go to the documentation of this file.
1/*
2// @HEADER
3// *****************************************************************************
4// Belos: Block Linear Solvers Package
5//
6// Copyright 2004-2016 NTESS and the Belos contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10*/
11
12#ifndef BELOS_LSQR_STATUS_TEST_HPP
13#define BELOS_LSQR_STATUS_TEST_HPP
14
20#include "BelosStatusTest.hpp"
21#include "BelosLSQRIter.hpp"
22#include "BelosTypes.hpp"
23
30namespace Belos {
31
32
33template <class ScalarType, class MV, class OP, class DM = Belos::DefaultDenseMatrix<int, ScalarType>>
34class LSQRStatusTest: public Belos::StatusTest<ScalarType,MV,OP,DM> {
35
36public:
37
38 // Convenience typedefs
39 typedef Teuchos::ScalarTraits<ScalarType> SCT;
40 typedef typename SCT::magnitudeType MagnitudeType;
42
44
45
47
53 int term_iter_max = 1,
56
58 virtual ~LSQRStatusTest();
60
62
63
65
68
70 Belos::StatusType getStatus() const {return(status_);}
71
73
75
76
78 void reset();
79
82 condMax_ = condMax;
83 rcondMin_ = (condMax > 0) ? (Teuchos::ScalarTraits< MagnitudeType >::one() / condMax) : Teuchos::ScalarTraits< MagnitudeType >::eps();
84 return(0);}
85
87 term_iter_max_ = term_iter_max;
88 if (term_iter_max_ < 1)
89 term_iter_max_ = 1;
90 return(0);}
91
93 rel_rhs_err_ = rel_rhs_err;
94 return(0);}
95
97 rel_mat_err_ = rel_mat_err;
98 return(0);}
99
101
103
104
106 MagnitudeType getCondMaxLim() const {return(condMax_);}
107
109 int getTermIterMax() const {return(term_iter_max_);}
110
112 MagnitudeType getRelRhsErr() const {return(rel_rhs_err_);}
113
115 MagnitudeType getMatErr() const {return(rel_mat_err_);}
116
118 MagnitudeType getMatCondNum() const {return(matCondNum_);}
119
121 MagnitudeType getMatNorm() const {return(matNorm_);}
122
124 int getTermIter() const { return term_iter_; }
125
127 MagnitudeType getResidNorm() const {return(resNorm_);}
128
130 MagnitudeType getLSResidNorm() const {return(matResNorm_);}
132
133
135
136
138 void print(std::ostream& os, int indent = 0) const;
139
141 void printStatus(std::ostream& os, Belos::StatusType type) const;
142
144
147
152
155
157 std::string description() const
158 {
159 std::ostringstream oss;
160 oss << "LSQRStatusTest<>: [ limit of condition number = " << condMax_ << " ]";
161 return oss.str();
162 }
164
165private:
166
168
169
171 MagnitudeType condMax_;
172
174 int term_iter_max_;
175
177 MagnitudeType rel_rhs_err_;
178
180 MagnitudeType rel_mat_err_;
181
183 MagnitudeType rcondMin_;
184
186 Belos::StatusType status_;
187
188 // term_iter_ records the number of consecutive "successful" iterations.
189 // convergence requires that term_iter_max consecutive iterates satisfy the other convergence tests
190 int term_iter_;
191
192 // condition number of the operator
193 MagnitudeType matCondNum_;
194
195 // Frobenius norm of the operator
196 MagnitudeType matNorm_;
197
198 // residual norm for the linear system
199 MagnitudeType resNorm_;
200
201 // least squares residual, operator^Transpose * residual
202 MagnitudeType matResNorm_;
203
205
206};
207
208template <class ScalarType, class MV, class OP, class DM>
211 int term_iter_max /* = 1 */,
212 MagnitudeType rel_rhs_err /* = 0 */,
213 MagnitudeType rel_mat_err /* = 0 */)
214 : condMax_(condMax),
215 term_iter_max_ (term_iter_max),
216 rel_rhs_err_ (rel_rhs_err),
217 rel_mat_err_ (rel_mat_err),
218 rcondMin_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() ),
219 status_ (Belos::Undefined),
220 term_iter_ (0),
221 matCondNum_ ( Teuchos::ScalarTraits<MagnitudeType>::one() ),
222 matNorm_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() ),
223 resNorm_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() ),
224 matResNorm_ ( Teuchos::ScalarTraits<MagnitudeType>::zero() )
225{}
226
227template <class ScalarType, class MV, class OP, class DM>
230
231template <class ScalarType, class MV, class OP, class DM>
236
237template <class ScalarType, class MV, class OP, class DM>
239{
240 const MagnitudeType MTzero = Teuchos::ScalarTraits<MagnitudeType>::zero();
241 const MagnitudeType MTone = Teuchos::ScalarTraits<MagnitudeType>::one();
242 if (condMax_ > MTzero )
243 {
244 rcondMin_ = MTone / condMax_;
245 }
246 else
247 {
248 rcondMin_ = Teuchos::ScalarTraits< MagnitudeType >::eps();
249 }
250
251 bool termIterFlag = false;
255 //
256 // LSQR solves a least squares problem. A converged preconditioned residual norm
257 // suffices for convergence, but is not necessary. LSQR sometimes returns a larger
258 // relative residual norm than what would have been returned by a linear solver.
259 // This section evaluates three stopping criteria. In the Solver Manager, this test
260 // is combined with a generic number of iteration test.
261 // If the linear system includes a preconditioner, then the least squares problem
262 // is solved for the preconditioned linear system. Preconditioning changes the least
263 // squares problem (in the sense of changing the norms), and the solution depends
264 // on the preconditioner in this sense.
265 // In the context of Linear Least Squares problems, preconditioning refers
266 // to the regularization matrix. Here the regularization matrix is always a scalar
267 // multiple of the identity (standard form least squres).
268 // The "loss of accuracy" concept is not yet implemented here, becuase it is unclear
269 // what this means for linear least squares. LSQR solves an inconsistent system
270 // in a least-squares sense. "Loss of accuracy" would correspond to
271 // the difference between the preconditioned residual and the unpreconditioned residual.
272 //
273
274 std::cout << " X " << state.sol_norm
275 << " b-AX " << state.resid_norm
276 << " Atr " << state.mat_resid_norm
277 << " A " << state.frob_mat_norm
278 << " cond " << state.mat_cond_num
279 << " relResNorm " << state.resid_norm/state.bnorm
280 << " LS " << state.mat_resid_norm /( state.resid_norm * state.frob_mat_norm )
281 << std::endl;
282
283 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
284 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
285 ScalarType stop_crit_1 = zero; // b = 0, done
286 if( state.bnorm > zero )
287 {
288 stop_crit_1 = state.resid_norm / state.bnorm;
289 }
291 if( state.frob_mat_norm > zero && state.resid_norm > zero )
292 {
293 stop_crit_2 = (state.resid_norm > zero) ? state.mat_resid_norm / (state.frob_mat_norm * state.resid_norm) : zero;
294 }
295 else
296 {
297 if( state.resid_norm == zero )
298 {
300 }
301 else
302 {
303 stop_crit_2 = one; // Initial mat_norm always vanishes
304 }
305 }
306 ScalarType stop_crit_3 = one / state.mat_cond_num;
307 ScalarType resid_tol = rel_rhs_err_ + rel_mat_err_ * state.frob_mat_norm * state.sol_norm / state.bnorm;
308 ScalarType resid_tol_mach = Teuchos::ScalarTraits< MagnitudeType >::eps() + Teuchos::ScalarTraits< MagnitudeType >::eps() * state.frob_mat_norm * state.sol_norm / state.bnorm;
309
310 // The expected use case for our users is that the linear system will almost
311 // always be compatible, but occasionally may not be. However, some users
312 // may use LSQR for more general cases. This is why we include the full
313 // suite of tests, for both compatible and incompatible systems.
314 //
315 // Users will have to be educated that sometimes they will get an answer X
316 // that does _not_ satisfy the linear system AX=B, but _does_ satisfy the
317 // corresponding least-squares problem. Perhaps the solution manager should
318 // provide them with a way to find out.
319
320 // stop_crit_1 is for compatible linear systems.
321 // stop_crit_2 is for incompatible linear systems.
322 // stop_crit_3 is for either compatible or incompatible linear systems.
323
324 // Have we met any of the stopping criteria?
325 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()) {
326 termIterFlag = true;
327
328 if (stop_crit_1 <= resid_tol )
329 std::cout << "Conv: stop_crit_1 " << stop_crit_1 << " resid_tol " << resid_tol << std::endl;
330
332 std::cout << "Conv: stop_crit_1 " << stop_crit_1 << " resid_tol_mach " << resid_tol_mach << std::endl;
333
334 if (stop_crit_2 <= rel_mat_err_ )
335 std::cout << "Conv: stop_crit_2 " << stop_crit_2 << " rel_mat_err " << rel_mat_err_ << std::endl;
336
337 if (stop_crit_2 <= Teuchos::ScalarTraits< MagnitudeType >::eps() )
338 std::cout << "Conv: stop_crit_2 " << stop_crit_2 << " eps " << Teuchos::ScalarTraits< MagnitudeType >::eps() << std::endl;
339
340 if (stop_crit_3 <= rcondMin_ )
341 std::cout << "Conv: stop_crit_3 " << stop_crit_3 << " rcondMin_ " << rcondMin_ << std::endl;
342
343 if (stop_crit_3 <= Teuchos::ScalarTraits< MagnitudeType >::eps() )
344 std::cout << "Conv: stop_crit_3 " << stop_crit_3 << " eps " << Teuchos::ScalarTraits< MagnitudeType >::eps() << std::endl;
345 }
346
347 // update number of consecutive successful iterations
348 if (!termIterFlag) {
349 term_iter_ = 0;
350 } else {
351 term_iter_++;
352 }
353 status_ = (term_iter_ < term_iter_max_) ? Belos::Failed : Belos::Passed;
354
355 matCondNum_ = state.mat_cond_num; // information that defined convergence
356 matNorm_ = state.frob_mat_norm; // in accessible variables
357 resNorm_ = state.resid_norm;
358 matResNorm_ = state.mat_resid_norm;
359
360 return status_;
361}
362
363template <class ScalarType, class MV, class OP, class DM>
365{
366 for (int j = 0; j < indent; j++)
367 os << ' ';
368 printStatus(os, status_);
369 os << "limit of condition number = " << condMax_ << std::endl;
370 os << "limit of condition number = " << condMax_ << std::endl;
371}
372
373template <class ScalarType, class MV, class OP, class DM>
375{
376 os << std::left << std::setw(13) << std::setfill('.');
377 switch (type) {
378 case Belos::Passed:
379 os << "Passed";
380 break;
381 case Belos::Failed:
382 os << "Failed";
383 break;
384 case Belos::Undefined:
385 default:
386 os << "Undefined";
387 break;
388 }
389 os << std::left << std::setfill(' ');
390 return;
391}
392
393} // end Belos namespace
394
395
396#endif /* BELOS_LSQR_STATUS_TEST_HPP */
Belos concrete class that iterates LSQR.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
A Belos::StatusTest class for specifying convergence of LSQR. The outer status tests passes if an inn...
MagnitudeType getResidNorm() const
Returns the value of the observed norm of the residual r = b-Ax.
Teuchos::ScalarTraits< ScalarType > SCT
Belos::MultiVecTraits< ScalarType, MV, DM > MVT
virtual ~LSQRStatusTest()
Destructor.
MagnitudeType getLSResidNorm() const
Returns the value of the observed norm of the Least Squares residual A^T r.
SCT::magnitudeType MagnitudeType
int setRelRhsErr(MagnitudeType rel_rhs_err)
Belos::StatusType firstCallCheckStatusSetup(Belos::Iteration< ScalarType, MV, OP, DM > *iSolver)
Called in checkStatus exactly once, on the first call to checkStatus.
void printStatus(std::ostream &os, Belos::StatusType type) const
Print message for each status specific to this stopping test.
std::string description() const
Method to return description of the maximum iteration status test
void reset()
Resets the status test to the initial internal state.
int getTermIterMax() const
Returns the number of successful convergent iterations required set in the constructor.
int getTermIter() const
!Returns the current number of successful iterations from the most recent StatusTest call.
Belos::StatusType getStatus() const
Return the result of the most recent CheckStatus call.
MagnitudeType getRelRhsErr() const
Returns the value of the estimate of the relative error in the data defining b set in the constructor...
MagnitudeType getMatNorm() const
Returns the value of the observed (Frobenius) norm of A.
int setRelMatErr(MagnitudeType rel_mat_err)
LSQRStatusTest(MagnitudeType condMax=0.0, int term_iter_max=1, MagnitudeType rel_rhs_err=0.0, MagnitudeType rel_mat_err=0.0)
Constructor.
Belos::StatusType checkStatus(Belos::Iteration< ScalarType, MV, OP, DM > *iSolver)
Check convergence status of the iterative solver: Unconverged, Converged, Failed.
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)
MagnitudeType getCondMaxLim() const
Returns the value of the upper limit of the condition number of Abar set in the constructor.
void print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
int setCondLim(MagnitudeType condMax)
Set the tolerances.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
A pure virtual class for defining the status tests for the Belos iterative solvers.
StatusType
Whether the StatusTest wants iteration to stop.

Generated for Belos by doxygen 1.9.8