42 #ifndef BELOS_STATUS_TEST_IMPRESNORM_H 43 #define BELOS_STATUS_TEST_IMPRESNORM_H 53 #include "Teuchos_as.hpp" 103 template <
class ScalarType,
class MV,
class OP>
107 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
MagnitudeType;
112 typedef Teuchos::ScalarTraits<ScalarType> STS;
113 typedef Teuchos::ScalarTraits<MagnitudeType> STM;
134 bool showMaxResNormOnly =
false);
180 tolerance_ = tolerance;
193 showMaxResNormOnly_ = showMaxResNormOnly;
226 void print(std::ostream& os,
int indent = 0)
const;
267 return currTolerance_;
271 const std::vector<MagnitudeType>*
getTestValue()
const {
return(&testvector_);};
301 std::ostringstream oss;
302 oss <<
"Belos::StatusTestImpResNorm<>: " << resFormStr();
303 oss <<
", tol = " << tolerance_;
315 std::string resFormStr()
const 317 std::ostringstream oss;
319 oss << ((resnormtype_==
OneNorm) ?
"1-Norm" : (resnormtype_==
TwoNorm) ?
"2-Norm" :
"Inf-Norm");
323 if (scaletype_!=
None)
330 oss <<
" (User Scale)";
333 oss << ((scalenormtype_==
OneNorm) ?
"1-Norm" : (resnormtype_==
TwoNorm) ?
"2-Norm" :
"Inf-Norm");
351 MagnitudeType tolerance_, currTolerance_;
357 bool showMaxResNormOnly_;
369 MagnitudeType scalevalue_;
372 std::vector<MagnitudeType> scalevector_;
375 std::vector<MagnitudeType> resvector_;
378 std::vector<MagnitudeType> testvector_;
381 Teuchos::RCP<MV> curSoln_;
384 std::vector<int> ind_;
396 std::vector<int> curLSIdx_;
405 bool firstcallCheckStatus_;
408 bool firstcallDefineResForm_;
411 bool firstcallDefineScaleForm_;
419 template <
class ScalarType,
class MV,
class OP>
422 : tolerance_(Tolerance),
423 currTolerance_(Tolerance),
425 showMaxResNormOnly_(showMaxResNormOnly),
434 firstcallCheckStatus_(true),
435 firstcallDefineResForm_(true),
436 firstcallDefineScaleForm_(true),
443 template <
class ScalarType,
class MV,
class OP>
447 template <
class ScalarType,
class MV,
class OP>
456 currTolerance_ = tolerance_;
457 firstcallCheckStatus_ =
true;
458 lossDetected_ =
false;
459 curSoln_ = Teuchos::null;
462 template <
class ScalarType,
class MV,
class OP>
465 TEUCHOS_TEST_FOR_EXCEPTION(firstcallDefineResForm_==
false,
StatusTestError,
466 "StatusTestResNorm::defineResForm(): The residual form has already been defined.");
467 firstcallDefineResForm_ =
false;
469 resnormtype_ = TypeOfNorm;
474 template <
class ScalarType,
class MV,
class OP>
476 MagnitudeType ScaleValue )
478 TEUCHOS_TEST_FOR_EXCEPTION(firstcallDefineScaleForm_==
false,
StatusTestError,
479 "StatusTestResNorm::defineScaleForm(): The scaling type has already been defined.");
480 firstcallDefineScaleForm_ =
false;
482 scaletype_ = TypeOfScaling;
483 scalenormtype_ = TypeOfNorm;
484 scalevalue_ = ScaleValue;
489 template <
class ScalarType,
class MV,
class OP>
496 const MagnitudeType zero = STM::zero ();
500 if (firstcallCheckStatus_) {
517 curBlksz_ = (int)curLSIdx_.size();
519 for (
int i=0; i<curBlksz_; ++i) {
520 if (curLSIdx_[i] > -1 && curLSIdx_[i] < numrhs_)
523 curNumRHS_ = validLS;
524 curSoln_ = Teuchos::null;
553 std::vector<MagnitudeType> tmp_resvector( curBlksz_ );
555 if (! residMV.is_null ()) {
558 MVT::MvNorm (*residMV, tmp_resvector, resnormtype_);
559 typename std::vector<int>::iterator p = curLSIdx_.begin();
560 for (
int i=0; p<curLSIdx_.end(); ++p, ++i) {
563 resvector_[*p] = tmp_resvector[i];
567 typename std::vector<int>::iterator p = curLSIdx_.begin();
568 for (
int i=0; p<curLSIdx_.end(); ++p, ++i) {
571 resvector_[*p] = tmp_resvector[i];
578 if (scalevector_.size () > 0) {
580 typename std::vector<int>::iterator p = curLSIdx_.begin();
581 for (; p<curLSIdx_.end(); ++p) {
585 if ( scalevector_[ *p ] != zero ) {
587 testvector_[ *p ] = resvector_[ *p ] / scalevector_[ *p ] / scalevalue_;
589 testvector_[ *p ] = resvector_[ *p ] / scalevalue_;
595 typename std::vector<int>::iterator p = curLSIdx_.begin();
596 for (; p<curLSIdx_.end(); ++p) {
599 testvector_[ *p ] = resvector_[ *p ] / scalevalue_;
612 ind_.resize( curLSIdx_.size() );
613 std::vector<int> lclInd( curLSIdx_.size() );
614 typename std::vector<int>::iterator p = curLSIdx_.begin();
615 for (
int i=0; p<curLSIdx_.end(); ++p, ++i) {
618 if (testvector_[ *p ] > currTolerance_) {
620 }
else if (testvector_[ *p ] <= currTolerance_) {
633 "StatusTestImpResNorm::checkStatus(): One or more of the current " 634 "implicit residual norms is NaN.");
652 std::vector<MagnitudeType> tmp_testvector (have);
653 MVT::MvNorm (*cur_res, tmp_resvector, resnormtype_);
656 if ( scalevector_.size() > 0 ) {
657 for (
int i=0; i<have; ++i) {
659 if ( scalevector_[ ind_[i] ] != zero ) {
661 tmp_testvector[ i ] = tmp_resvector[ lclInd[i] ] / scalevector_[ ind_[i] ] / scalevalue_;
663 tmp_testvector[ i ] = tmp_resvector[ lclInd[i] ] / scalevalue_;
668 for (
int i=0; i<have; ++i) {
669 tmp_testvector[ i ] = tmp_resvector[ lclInd[i] ] / scalevalue_;
680 for (
int i = 0; i < have; ++i) {
690 if (tmp_testvector[i] <= tolerance_) {
691 ind_[have2] = ind_[i];
697 const MagnitudeType diff = STM::magnitude (testvector_[ind_[i]] - tmp_testvector[i]);
698 if (diff > currTolerance_) {
706 lossDetected_ =
true;
707 ind_[have2] = ind_[i];
727 const MagnitudeType onePointFive = as<MagnitudeType>(3) / as<MagnitudeType> (2);
728 const MagnitudeType oneTenth = STM::one () / as<MagnitudeType> (10);
730 currTolerance_ = currTolerance_ - onePointFive * diff;
731 while (currTolerance_ < STM::zero ()) {
732 currTolerance_ += oneTenth * diff;
743 int need = (quorum_ == -1) ? curNumRHS_: quorum_;
750 template <
class ScalarType,
class MV,
class OP>
753 for (
int j = 0; j < indent; j ++)
758 os <<
", tol = " << tolerance_ << std::endl;
761 if(showMaxResNormOnly_ && curBlksz_ > 1) {
762 const MagnitudeType maxRelRes = *std::max_element(
763 testvector_.begin()+curLSIdx_[0],testvector_.begin()+curLSIdx_[curBlksz_-1]
765 for (
int j = 0; j < indent + 13; j ++)
767 os <<
"max{residual["<<curLSIdx_[0]<<
"..."<<curLSIdx_[curBlksz_-1]<<
"]} = " << maxRelRes
768 << ( maxRelRes <= tolerance_ ?
" <= " :
" > " ) << tolerance_ << std::endl;
771 for (
int i=0; i<numrhs_; i++ ) {
772 for (
int j = 0; j < indent + 13; j ++)
774 os <<
"residual [ " << i <<
" ] = " << testvector_[ i ];
775 os << ((testvector_[i]<tolerance_) ?
" < " : (testvector_[i]==tolerance_) ?
" == " : (testvector_[i]>tolerance_) ?
" > " :
" " ) << tolerance_ << std::endl;
782 template <
class ScalarType,
class MV,
class OP>
785 os << std::left << std::setw(13) << std::setfill(
'.');
792 os <<
"Unconverged (LoA)";
801 os << std::left << std::setfill(
' ');
805 template <
class ScalarType,
class MV,
class OP>
810 const MagnitudeType zero = STM::zero ();
811 const MagnitudeType one = STM::one ();
814 if (firstcallCheckStatus_) {
818 firstcallCheckStatus_ =
false;
821 Teuchos::RCP<const MV> rhs = lp.
getRHS();
823 scalevector_.resize( numrhs_ );
829 scalevector_.resize( numrhs_ );
830 MVT::MvNorm( *init_res, scalevector_, scalenormtype_ );
835 scalevector_.resize( numrhs_ );
836 MVT::MvNorm( *init_res, scalevector_, scalenormtype_ );
842 resvector_.resize( numrhs_ );
843 testvector_.resize( numrhs_ );
847 curBlksz_ = (int)curLSIdx_.size();
849 for (i=0; i<curBlksz_; ++i) {
850 if (curLSIdx_[i] > -1 && curLSIdx_[i] < numrhs_)
853 curNumRHS_ = validLS;
856 for (i=0; i<numrhs_; i++) { testvector_[i] = one; }
859 if (scalevalue_ == zero) {
Teuchos::RCP< MV > getSolution()
Returns the current solution estimate that was computed for the most recent residual test...
virtual ~StatusTestImpResNorm()
Destructor (virtual for memory safety).
const std::vector< MagnitudeType > * getTestValue() const
Returns the test value, , computed in most recent call to CheckStatus.
ScaleType
The type of scaling to use on the residual norm value.
Exception thrown to signal error in a status test during Belos::StatusTest::checkStatus().
Convergence test using the implicit residual norm(s), with an explicit residual norm(s) check for los...
StatusType checkStatus(Iteration< ScalarType, MV, OP > *iSolver)
Check convergence status: Passed, Failed, or Undefined.
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.
Declaration of basic traits for the multivector type.
An abstract class of StatusTest for stopping criteria using residual norms.
Teuchos::RCP< const MV > getRHS() const
A pointer to the right-hand side B.
StatusType
Whether the StatusTest wants iteration to stop.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
const std::vector< MagnitudeType > * getResNormValue() const
Returns the residual norm value, , computed in most recent call to CheckStatus.
Traits class which defines basic operations on multivectors.
int getQuorum() const
Returns the number of residuals that must pass the convergence test before Passed is returned...
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
The type of the magnitude (absolute value) of a ScalarType.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
Teuchos::RCP< const MV > getInitResVec() const
A pointer to the initial unpreconditioned residual vector.
int setShowMaxResNormOnly(bool showMaxResNormOnly)
Set whether the only maximum residual norm is displayed when the print() method is called...
const std::vector< MagnitudeType > * getScaledNormValue() const
Returns the scaled norm value, .
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
int getLSNumber() const
The number of linear systems that have been set.
int setQuorum(int quorum)
Sets the number of residuals that must pass the convergence test before Passed is returned...
StatusType firstCallCheckStatusSetup(Iteration< ScalarType, MV, OP > *iSolver)
Call to setup initial scaling vector.
MagnitudeType getCurrTolerance() const
Current convergence tolerance; may be changed to prevent loss of accuracy.
int defineResForm(NormType TypeOfNorm)
Define form of the residual, its norm and optional weighting vector.
virtual Teuchos::RCP< MV > getCurrentUpdate() const =0
Get the current update to the linear system.
Belos::StatusTest abstract class for specifying a residual norm stopping criteria.
bool getLOADetected() const
Returns a boolean indicating a loss of accuracy has been detected in computing the residual...
virtual const LinearProblem< ScalarType, MV, OP > & getProblem() const =0
Get a constant reference to the linear problem.
Teuchos::RCP< const MV > getInitPrecResVec() const
A pointer to the preconditioned initial residual vector.
NormType
The type of vector norm to compute.
const std::vector< int > getLSIndex() const
(Zero-based) indices of the linear system(s) currently being solved.
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. ...
StatusType getStatus() const
Return the result of the most recent CheckStatus call.
MagnitudeType getTolerance() const
"Original" convergence tolerance as set by user.
int defineScaleForm(ScaleType TypeOfScaling, NormType TypeOfNorm, MagnitudeType ScaleValue=Teuchos::ScalarTraits< MagnitudeType >::one())
Define form of the scaling, its norm, its optional weighting vector, or, alternatively, define an explicit value.
StatusTestImpResNorm(MagnitudeType Tolerance, int quorum=-1, bool showMaxResNormOnly=false)
Constructor.
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
std::string description() const
Method to return description of the maximum iteration status test.
void printStatus(std::ostream &os, StatusType type) const
Print message for each status specific to this stopping test.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
void reset()
Resets the internal configuration to the initial state.
void print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
std::vector< int > convIndices()
Returns the vector containing the indices of the residuals that passed the test.
virtual Teuchos::RCP< const MV > getNativeResiduals(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *norms) const =0
Get the residuals native to the solver.
bool getShowMaxResNormOnly()
Returns whether the only maximum residual norm is displayed when the print() method is called...