10#ifndef BELOS_STATUS_TEST_GEN_RESNORM_H
11#define BELOS_STATUS_TEST_GEN_RESNORM_H
46template <
class ScalarType,
class MV,
class OP>
52 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
137 int setQuorum(
int quorum) {quorum_ = quorum;
return(0);}
171 void print(std::ostream& os,
int indent = 0)
const;
183 Teuchos::RCP<MV>
getSolution() {
if (restype_==
Implicit) {
return Teuchos::null; }
else {
return curSoln_; } }
199 const std::vector<MagnitudeType>*
getTestValue()
const {
return(&testvector_);};
231 std::ostringstream oss;
232 oss <<
"Belos::StatusTestGenResNorm<>: " << resFormStr();
233 oss <<
", tol = " << tolerance_;
245 std::string resFormStr()
const
247 std::ostringstream oss;
249 oss << ((resnormtype_==
OneNorm) ?
"1-Norm" : (resnormtype_==
TwoNorm) ?
"2-Norm" :
"Inf-Norm");
250 oss << ((restype_==
Explicit) ?
" Exp" :
" Imp");
254 if (scaletype_!=
None)
261 oss <<
" (User Scale)";
264 oss << ((scalenormtype_==
OneNorm) ?
"1-Norm" : (resnormtype_==
TwoNorm) ?
"2-Norm" :
"Inf-Norm");
290 bool showMaxResNormOnly_;
308 std::vector<MagnitudeType> scalevector_;
311 std::vector<MagnitudeType> resvector_;
314 std::vector<MagnitudeType> testvector_;
317 std::vector<int> ind_;
320 Teuchos::RCP<MV> curSoln_;
332 std::vector<int> curLSIdx_;
341 bool firstcallCheckStatus_;
344 bool firstcallDefineResForm_;
347 bool firstcallDefineScaleForm_;
353template <
class ScalarType,
class MV,
class OP>
356 : tolerance_(Tolerance),
358 showMaxResNormOnly_(showMaxResNormOnly),
369 firstcallCheckStatus_(true),
370 firstcallDefineResForm_(true),
371 firstcallDefineScaleForm_(true)
377template <
class ScalarType,
class MV,
class OP>
381template <
class ScalarType,
class MV,
class OP>
390 firstcallCheckStatus_ =
true;
391 curSoln_ = Teuchos::null;
394template <
class ScalarType,
class MV,
class OP>
397 TEUCHOS_TEST_FOR_EXCEPTION(firstcallDefineResForm_==
false,
StatusTestError,
398 "StatusTestGenResNorm::defineResForm(): The residual form has already been defined.");
399 firstcallDefineResForm_ =
false;
401 restype_ = TypeOfResidual;
402 resnormtype_ = TypeOfNorm;
407template <
class ScalarType,
class MV,
class OP>
411 TEUCHOS_TEST_FOR_EXCEPTION(firstcallDefineScaleForm_==
false,
StatusTestError,
412 "StatusTestGenResNorm::defineScaleForm(): The scaling type has already been defined.");
413 firstcallDefineScaleForm_ =
false;
415 scaletype_ = TypeOfScaling;
416 scalenormtype_ = TypeOfNorm;
417 scalevalue_ = ScaleValue;
422template <
class ScalarType,
class MV,
class OP>
425 MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
428 if (firstcallCheckStatus_) {
444 curBlksz_ = (int)curLSIdx_.size();
446 for (
int i=0; i<curBlksz_; ++i) {
447 if (curLSIdx_[i] > -1 && curLSIdx_[i] < numrhs_)
450 curNumRHS_ = validLS;
451 curSoln_ = Teuchos::null;
457 if (status_==
Passed) {
return status_; }
465 std::vector<MagnitudeType> tmp_resvector( curBlksz_ );
467 if ( residMV != Teuchos::null ) {
469 MVT::MvNorm( *residMV, tmp_resvector, resnormtype_ );
470 typename std::vector<int>::iterator p = curLSIdx_.begin();
471 for (
int i=0; p<curLSIdx_.end(); ++p, ++i) {
474 resvector_[*p] = tmp_resvector[i];
477 typename std::vector<int>::iterator p = curLSIdx_.begin();
478 for (
int i=0; p<curLSIdx_.end(); ++p, ++i) {
481 resvector_[*p] = tmp_resvector[i];
494 MVT::MvNorm( *cur_res, tmp_resvector, resnormtype_ );
495 typename std::vector<int>::iterator p = curLSIdx_.begin();
496 for (
int i=0; p<curLSIdx_.end(); ++p, ++i) {
499 resvector_[*p] = tmp_resvector[i];
506 if ( scalevector_.size() > 0 ) {
507 typename std::vector<int>::iterator p = curLSIdx_.begin();
508 for (; p<curLSIdx_.end(); ++p) {
513 scalevector_[ *p ] != zero? resvector_[ *p ] / (scalevector_[ *p ] * scalevalue_) : resvector_[ *p ] / scalevalue_;
518 typename std::vector<int>::iterator p = curLSIdx_.begin();
519 for (; p<curLSIdx_.end(); ++p) {
522 testvector_[ *p ] = resvector_[ *p ] / scalevalue_;
528 ind_.resize( curLSIdx_.size() );
529 typename std::vector<int>::iterator p = curLSIdx_.begin();
530 for (; p<curLSIdx_.end(); ++p) {
534 if (testvector_[ *p ] > tolerance_) {
536 }
else if (testvector_[ *p ] <= tolerance_) {
542 TEUCHOS_TEST_FOR_EXCEPTION(
true,
StatusTestNaNError,
"StatusTestGenResNorm::checkStatus(): NaN has been detected.");
547 int need = (quorum_ == -1) ? curNumRHS_: quorum_;
554template <
class ScalarType,
class MV,
class OP>
557 for (
int j = 0; j < indent; j ++)
562 os <<
", tol = " << tolerance_ << std::endl;
565 if(showMaxResNormOnly_ && curBlksz_ > 1) {
567 testvector_.begin()+curLSIdx_[0],testvector_.begin()+curLSIdx_[curBlksz_-1]
569 for (
int j = 0; j < indent + 13; j ++)
571 os <<
"max{residual["<<curLSIdx_[0]<<
"..."<<curLSIdx_[curBlksz_-1]<<
"]} = " << maxRelRes
572 << ( maxRelRes <= tolerance_ ?
" <= " :
" > " ) << tolerance_ << std::endl;
575 for (
int i=0; i<numrhs_; i++ ) {
576 for (
int j = 0; j < indent + 13; j ++)
578 os <<
"residual [ " << i <<
" ] = " << testvector_[ i ];
579 os << ((testvector_[i]<tolerance_) ?
" < " : (testvector_[i]==tolerance_) ?
" == " : (testvector_[i]>tolerance_) ?
" > " :
" " ) << tolerance_ << std::endl;
586template <
class ScalarType,
class MV,
class OP>
589 os << std::left << std::setw(13) << std::setfill(
'.');
602 os << std::left << std::setfill(
' ');
606template <
class ScalarType,
class MV,
class OP>
610 MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
611 MagnitudeType one = Teuchos::ScalarTraits<MagnitudeType>::one();
614 if (firstcallCheckStatus_) {
618 firstcallCheckStatus_ =
false;
621 Teuchos::RCP<const MV> rhs = lp.
getRHS();
623 scalevector_.resize( numrhs_ );
629 scalevector_.resize( numrhs_ );
630 MVT::MvNorm( *init_res, scalevector_, scalenormtype_ );
635 scalevector_.resize( numrhs_ );
636 MVT::MvNorm( *init_res, scalevector_, scalenormtype_ );
642 resvector_.resize( numrhs_ );
643 testvector_.resize( numrhs_ );
647 curBlksz_ = (int)curLSIdx_.size();
649 for (i=0; i<curBlksz_; ++i) {
650 if (curLSIdx_[i] > -1 && curLSIdx_[i] < numrhs_)
653 curNumRHS_ = validLS;
656 for (i=0; i<numrhs_; i++) { testvector_[i] = one; }
659 if (scalevalue_ == zero) {
Class which describes the linear problem to be solved by the iterative solver.
Declaration of basic traits for the multivector type.
Belos::StatusTest abstract class for specifying a residual norm stopping criteria.
virtual Teuchos::RCP< const MV > getNativeResiduals(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *norms) const =0
virtual Teuchos::RCP< MV > getCurrentUpdate() const =0
Get the current update to the linear system.
virtual const LinearProblem< ScalarType, MV, OP > & getProblem() const =0
Get a constant reference to the linear problem.
int getLSNumber() const
The number of linear systems that have been set.
Teuchos::RCP< const MV > getInitPrecResVec() const
A pointer to the preconditioned initial residual vector.
const std::vector< int > & getLSIndex() const
(Zero-based) indices of the linear system(s) currently being solved.
Teuchos::RCP< const MV > getRHS() const
A pointer to the right-hand side B.
Teuchos::RCP< const MV > getInitResVec() const
A pointer to the initial unpreconditioned residual vector.
virtual void computeCurrResVec(MV *R, const MV *X=0, const MV *B=0) const
Compute a residual R for this operator given a solution X, and right-hand side B.
virtual Teuchos::RCP< MV > updateSolution(const Teuchos::RCP< MV > &update=Teuchos::null, bool updateLP=false, ScalarType scale=Teuchos::ScalarTraits< ScalarType >::one())
Compute the new solution to the linear system using the given update vector.
Traits class which defines basic operations on multivectors.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
Compute the norm of each individual vector of mv. Upon return, normvec[i] holds the value of ,...
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Exception thrown to signal error in a status test during Belos::StatusTest::checkStatus().
NormType getResNormType()
std::string description() const
Method to return description of the maximum iteration status test.
std::vector< int > convIndices()
Returns the std::vector containing the indices of the residuals that passed the test.
int setQuorum(int quorum)
int setShowMaxResNormOnly(bool showMaxResNormOnly)
Set whether the only maximum residual norm is displayed when the print() method is called.
const std::vector< MagnitudeType > * getTestValue() const
Returns the test value, , computed in most recent call to CheckStatus.
int defineScaleForm(ScaleType TypeOfScaling, NormType TypeOfNorm, MagnitudeType ScaleValue=Teuchos::ScalarTraits< MagnitudeType >::one())
Define form of the scaling, its norm, its optional weighting std::vector, or, alternatively,...
StatusType firstCallCheckStatusSetup(Iteration< ScalarType, MV, OP > *iSolver)
Call to setup initial scaling std::vector.
void reset()
Resets the internal configuration to the initial state.
virtual ~StatusTestGenResNorm()
Destructor.
Teuchos::ScalarTraits< ScalarType > SCT
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
bool getShowMaxResNormOnly()
Returns whether the only maximum residual norm is displayed when the print() method is called.
MagnitudeType getTolerance() const
Returns the value of the tolerance, , set in the constructor.
int defineResForm(ResType TypeOfResidual, NormType TypeOfNorm)
Define form of the residual, its norm and optional weighting std::vector.
StatusType checkStatus(Iteration< ScalarType, MV, OP > *iSolver)
Check convergence status: Passed, Failed, or Undefined.
StatusTestGenResNorm(MagnitudeType Tolerance, int quorum=-1, bool showMaxResNormOnly=false)
Constructor.
Teuchos::RCP< MV > getSolution()
StatusType getStatus() const
Return the result of the most recent CheckStatus call.
MultiVecTraits< ScalarType, MV > MVT
const std::vector< MagnitudeType > * getResNormValue() const
Returns the residual norm value, , computed in most recent call to CheckStatus.
ResType
Select how the residual std::vector is produced.
const std::vector< MagnitudeType > * getScaledNormValue() const
Returns the scaled norm value, .
void printStatus(std::ostream &os, StatusType type) const
Print message for each status specific to this stopping test.
bool getLOADetected() const
void print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
SCT::magnitudeType MagnitudeType
An abstract class of StatusTest for stopping criteria using residual norms.
NormType
The type of vector norm to compute.
StatusType
Whether the StatusTest wants iteration to stop.
ScaleType
The type of scaling to use on the residual norm value.
@ NormOfFullScaledPrecInitRes
@ NormOfFullScaledInitRes