Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosStatusTestLogResNorm.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 
11 #ifndef BELOS_STATUS_TEST_LOGRESNORM_HPP
12 #define BELOS_STATUS_TEST_LOGRESNORM_HPP
13 
19 #include "BelosStatusTest.hpp"
20 #include "Teuchos_ScalarTraits.hpp"
21 #include "Teuchos_RCP.hpp"
22 
32 namespace Belos {
33 
34 template <class ScalarType, class MV, class OP>
35 class StatusTestLogResNorm: public StatusTest<ScalarType,MV,OP> {
36 
37 public:
40 
41 private:
43 
46 
47  public:
48 
50 
51 
53  StatusTestLogResNorm(int maxIters);
54 
56  virtual ~StatusTestLogResNorm() {};
58 
60 
61 
63 
67 
69  StatusType getStatus() const {return(Undefined);}
70 
72 
74 
75 
77  void reset();
78 
80  void setMaxIters(int maxIters) { maxIters_ = maxIters; logResNorm_.reserve( maxIters_ ); }
81 
83 
85 
86 
88  int getMaxIters() const { return(maxIters_); }
89 
91  int getNumIters() const { return(nIters_); }
92 
94  const std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>&
95  getLogResNorm() const { return(logResNorm_); }
96 
98 
100 
101 
103  void print(std::ostream& os, int indent = 0) const;
104 
106  void printStatus(std::ostream& os, StatusType type) const;
107 
109 
112 
114  std::string description() const
115  {
116  std::ostringstream oss;
117  oss << "Belos::StatusTestLogResNorm<>: [ " << getNumIters() << " / " << getMaxIters() << " ]";
118  return oss.str();
119  }
121 
122 private:
123 
125 
126  int maxIters_;
128 
130  int nIters_;
131 
133  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> logResNorm_;
134 
136 
137 };
138 
139  template <class ScalarType, class MV, class OP>
141  {
142  if (maxIters < 1)
143  maxIters_ = 1;
144  else
145  maxIters_ = maxIters;
146 
147  logResNorm_.reserve( maxIters_ );
148 
149  nIters_ = 0;
150  }
151 
152  template <class ScalarType, class MV, class OP>
154  {
155  // Check that this solve is a single-vector, single-block.
156  const LinearProblem<ScalarType,MV,OP>& lp = iSolver->getProblem ();
157  int blkSize = lp.getLSIndex().size();
158  int numRHS = MVT::GetNumberVecs( *(lp.getRHS()) );
159 
160  int currIters = iSolver->getNumIters();
161 
162  if ( (numRHS==1) && (blkSize==1) && (currIters!=nIters_) )
163  {
164  std::vector<MagnitudeType> tmp_resvector( 1 );
165  Teuchos::RCP<const MV> residMV = iSolver->getNativeResiduals (&tmp_resvector);
166  if (! residMV.is_null ())
167  {
168  // We got a multivector back. Compute the norms explicitly.
169  MVT::MvNorm (*residMV, tmp_resvector, TwoNorm);
170  }
171 
172  logResNorm_.push_back( tmp_resvector[0] );
173  nIters_ = currIters;
174  }
175 
176  return Undefined;
177  }
178 
179  template <class ScalarType, class MV, class OP>
181  {
182  nIters_ = 0;
183  logResNorm_.clear();
184  logResNorm_.reserve( maxIters_ );
185  }
186 
187  template <class ScalarType, class MV, class OP>
188  void StatusTestLogResNorm<ScalarType,MV,OP>::print(std::ostream& os, int indent) const
189  {
190  for (int j = 0; j < indent; j ++)
191  os << ' ';
192  printStatus(os, Undefined);
193  os << "Logging Absolute Residual 2-Norm" << std::endl;
194  }
195 
196  template <class ScalarType, class MV, class OP>
198  {
199  os << std::left << std::setw(13) << std::setfill('.');
200  os << "**";
201  os << std::left << std::setfill(' ');
202  return;
203  }
204 
205 } // end Belos namespace
206 
207 #endif /* BELOS_STATUS_TEST_LOGRESNORM_HPP */
virtual Teuchos::RCP< const MV > getNativeResiduals(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *norms) const =0
Get the residuals native to the solver.
void setMaxIters(int maxIters)
Sets the maximum number of iterations allowed so internal storage can be resized. ...
StatusTestLogResNorm(int maxIters)
Constructor.
void print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
Pure virtual base class for defining the status testing capabilities of Belos.
const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > & getLogResNorm() const
Returns the log of the absolute residual norm from the iteration.
StatusType getStatus() const
Return the result of the most recent CheckStatus call.
void printStatus(std::ostream &os, StatusType type) const
Print message for each status specific to this stopping test.
A Belos::StatusTest debugging class for storing the absolute residual norms generated during a solve...
Teuchos::RCP< const MV > getRHS() const
A pointer to the right-hand side B.
A pure virtual class for defining the status tests for the Belos iterative solvers.
int getNumIters() const
Returns the current number of iterations from the most recent StatusTest call.
StatusType
Whether the StatusTest wants iteration to stop.
Definition: BelosTypes.hpp:157
Traits class which defines basic operations on multivectors.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
The type of the magnitude (absolute value) of a ScalarType.
const std::vector< int > & getLSIndex() const
(Zero-based) indices of the linear system(s) currently being solved.
std::string description() const
Method to return description of the debugging status test.
StatusType checkStatus(Iteration< ScalarType, MV, OP > *iSolver)
Check convergence status of the iterative solver: Unconverged, Converged, Failed. ...
virtual int getNumIters() const =0
Get the current iteration count.
A linear system to solve, and its associated information.
virtual ~StatusTestLogResNorm()
Destructor.
virtual const LinearProblem< ScalarType, MV, OP > & getProblem() const =0
Get a constant reference to the linear problem.
int getMaxIters() const
Returns the maximum number of iterations set in the constructor.
void reset()
Resets the status test to the initial internal state.
bool is_null() const

Generated on Thu Oct 24 2024 09:25:34 for Belos by doxygen 1.8.5