45 #ifndef THYRA_GENERAL_SOLVE_CRITERIA_BELOS_STATUS_TEST_DEF_HPP
46 #define THYRA_GENERAL_SOLVE_CRITERIA_BELOS_STATUS_TEST_DEF_HPP
48 #include "Thyra_GeneralSolveCriteriaBelosStatusTest.hpp"
49 #include "Thyra_VectorSpaceBase.hpp"
50 #include "Thyra_MultiVectorBase.hpp"
51 #include "Thyra_VectorBase.hpp"
52 #include "Thyra_MultiVectorStdOps.hpp"
53 #include "Thyra_VectorStdOps.hpp"
66 template<
class Scalar>
68 :convergenceTestFrequency_(-1),
72 lastRtnStatus_(Belos::Undefined)
78 template<
class Scalar>
80 const SolveCriteria<Scalar> &solveCriteria,
81 const int convergenceTestFrequency
86 typedef ScalarTraits<ScalarMag> SMT;
93 nonnull(solveCriteria.denominatorReductionFunc) );
97 solveCriteria_ = solveCriteria;
98 convergenceTestFrequency_ = convergenceTestFrequency;
102 compute_r_ = solveCriteria.solveMeasureType.contains(SOLVE_MEASURE_NORM_RESIDUAL);
104 compute_x_ = (compute_r_ ||
105 solveCriteria.solveMeasureType.contains(SOLVE_MEASURE_NORM_SOLUTION));
110 template<
class Scalar>
111 ArrayView<const typename ScalarTraits<Scalar>::magnitudeType>
114 return lastAchievedTol_;
121 template <
class Scalar>
124 Belos::Iteration<Scalar,MV,OP> *iSolver
134 const int currIter = iSolver->getNumIters();
136 if (currIter == 0 || currIter % convergenceTestFrequency_ != 0) {
138 return Belos::Undefined;
144 const Belos::LinearProblem<Scalar,MV,OP>& lp = iSolver->getProblem();
145 const int numRhs = lp.getRHS()->domain()->dim();
148 if (solveCriteria_.solveMeasureType.contains(SOLVE_MEASURE_NORM_RHS)) {
153 if (solveCriteria_.solveMeasureType.contains(SOLVE_MEASURE_NORM_INIT_RESIDUAL)) {
160 RCP<MV> X_update = iSolver->getCurrentUpdate();
161 X = lp.updateSolution(X_update);
167 R = createMembers(lp.getOperator()->range(), X->domain());
168 lp.computeCurrResVec(&*R, &*X);
173 lastNumerator_.resize(numRhs);
174 lastDenominator_.resize(numRhs);
176 for (
int j = 0; j < numRhs; ++j) {
179 lastNumerator_[j] = computeReductionFunctional(
180 solveCriteria_.solveMeasureType.numerator,
181 solveCriteria_.numeratorReductionFunc.ptr(),
183 lastDenominator_[j] = computeReductionFunctional(
184 solveCriteria_.solveMeasureType.denominator,
185 solveCriteria_.denominatorReductionFunc.ptr(),
191 bool systemsAreConverged =
true;
192 lastAchievedTol_.resize(numRhs);
194 for (
int j = 0; j < numRhs; ++j) {
195 const ScalarMag convRatio = lastNumerator_[j] / lastDenominator_[j];
196 lastAchievedTol_[j] = convRatio;
197 const bool sys_converged_j = (convRatio <= solveCriteria_.requestedTol);
199 printRhsStatus(currIter, j, *out);
201 if (!sys_converged_j) {
202 systemsAreConverged =
false;
206 lastRtnStatus_ = (systemsAreConverged ? Belos::Passed : Belos::Failed);
207 lastCurrIter_ = currIter;
209 return lastRtnStatus_;
213 template <
class Scalar>
217 return lastRtnStatus_;
221 template <
class Scalar>
226 lastNumerator_.clear();
227 lastDenominator_.clear();
228 lastAchievedTol_.clear();
230 lastRtnStatus_ = Belos::Undefined;
234 template <
class Scalar>
236 std::ostream& os,
int indent
239 const int numRhs = lastNumerator_.size();
240 for (
int j = 0; j < numRhs; ++j) {
241 printRhsStatus(lastCurrIter_, j, os, indent);
249 template <
class Scalar>
252 ESolveMeasureNormType measureType,
253 const Ptr<
const ReductionFunctional<Scalar> > &reductFunc,
254 const Ptr<
const VectorBase<Scalar> > &x,
255 const Ptr<
const VectorBase<Scalar> > &r
258 typedef ScalarTraits<ScalarMag> SMT;
260 Ptr<const VectorBase<Scalar> > v;
261 switch(measureType) {
262 case SOLVE_MEASURE_ONE:
265 case SOLVE_MEASURE_NORM_RESIDUAL:
268 case SOLVE_MEASURE_NORM_SOLUTION:
271 case SOLVE_MEASURE_NORM_INIT_RESIDUAL:
273 case SOLVE_MEASURE_NORM_RHS:
277 if (rtn >= SMT::zero()) {
280 else if (
nonnull(v) && rtn < SMT::zero()) {
282 rtn = reductFunc->reduce(*v);
293 template <
class Scalar>
296 const int currIter,
const int j, std::ostream &out,
300 const ScalarMag convRatio = lastNumerator_[j] / lastDenominator_[j];
301 const bool sys_converged_j = (convRatio <= solveCriteria_.requestedTol);
302 for (
int i = 0; i < indent; ++i) { out <<
" "; }
304 <<
"["<<currIter<<
"] "
305 <<
"gN(vN("<<j<<
"))/gD(vD("<<j<<
")) = "
306 << lastNumerator_[j] <<
"/" << lastDenominator_[j] <<
" = "
307 << convRatio <<
" <= " << solveCriteria_.requestedTol <<
" : "
308 << (sys_converged_j ?
" true" :
"false")
316 #endif // THYRA_GENERAL_SOLVE_CRITERIA_BELOS_STATUS_TEST_DEF_HPP
GeneralSolveCriteriaBelosStatusTest()
ArrayView< const ScalarMag > achievedTol() const
virtual Belos::StatusType getStatus() const
#define TEUCHOS_ASSERT_INEQUALITY(val1, comp, val2)
void printRhsStatus(const int currIter, const int j, std::ostream &out, int indent=0) const
Thyra specializations of MultiVecTraits and OperatorTraits.
virtual Belos::StatusType checkStatus(Belos::Iteration< Scalar, MV, OP > *iSolver)
#define TEUCHOS_IF_ELSE_DEBUG_ASSERT()
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
#define TEUCHOS_SWITCH_DEFAULT_DEBUG_ASSERT()
ScalarTraits< Scalar >::magnitudeType ScalarMag
ScalarMag computeReductionFunctional(ESolveMeasureNormType measureType, const Ptr< const ReductionFunctional< Scalar > > &reductFunc, const Ptr< const VectorBase< Scalar > > &x, const Ptr< const VectorBase< Scalar > > &r) const
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
TypeTo as(const TypeFrom &t)
bool nonnull(const boost::shared_ptr< T > &p)
void setSolveCriteria(const SolveCriteria< Scalar > &solveCriteria, const int convergenceTestFrequency)
#define TEUCHOS_ASSERT(assertion_test)
virtual void print(std::ostream &os, int indent) const