Belos  Version of the Day
BelosStatusTestResNormOutput.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 //
42 
43 #ifndef BELOS_STATUS_TEST_RESNORM_OUTPUT_HPP
44 #define BELOS_STATUS_TEST_RESNORM_OUTPUT_HPP
45 
51 #include <vector>
52 #include "BelosConfigDefs.hpp"
53 #include "BelosTypes.hpp"
54 #include "BelosIteration.hpp"
55 
56 #include "BelosStatusTest.hpp"
57 #include "BelosStatusTestCombo.hpp"
61 
62 namespace Belos {
63 
73 template <class ScalarType, class MV, class OP>
74 class StatusTestResNormOutput : public StatusTestOutput<ScalarType,MV,OP> {
75 
80 
81  public:
83 
84 
100  StatusTestResNormOutput(const Teuchos::RCP<OutputManager<ScalarType> > &printer,
101  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test,
102  int mod = 1,
103  int printStates = Passed)
104  : printer_(printer),
105  state_(Undefined),
106  headerPrinted_(false),
107  stateTest_(printStates),
108  modTest_(mod),
109  lastNumIters_(-1),
110  comboType_(0),
111  numResTests_(0),
112  blockSize_(1),
113  currNumRHS_(0),
114  currLSNum_(0),
115  numLSDgts_(1),
116  numIterDgts_(1)
117  {
118  // Set the input test.
119  setChild(test);
120  }
121 
125 
127 
128 
146  {
147  TEUCHOS_TEST_FOR_EXCEPTION(iterTest_ == Teuchos::null,StatusTestError,"StatusTestResNormOutput::checkStatus(): iteration test pointer is null.");
148  TEUCHOS_TEST_FOR_EXCEPTION(resTestVec_.size() == 0,StatusTestError,"StatusTestResNormOutput::checkStatus(): residual test pointer is null.");
149  state_ = test_->checkStatus(solver);
150 
151  // Update some information for the header, if it has not printed or the linear system has changed.
152  LinearProblem<ScalarType,MV,OP> currProb = solver->getProblem();
153  //if (!headerPrinted_ || currLSNum_ != currProb.getLSNumber()) {
154  if (currLSNum_ != currProb.getLSNumber()) {
155  currLSNum_ = currProb.getLSNumber();
156  blockSize_ = solver->getBlockSize();
157  currIdx_ = currProb.getLSIndex();
158  currNumRHS_ = currIdx_.size();
159  numLSDgts_ = (int)std::floor((double)MVT::GetNumberVecs(*(currProb.getRHS())))+1;
160  numIterDgts_ = (int)std::floor(std::log10((double)iterTest_->getMaxIters()))+1;
161  }
162  // Print out current iteration information if it hasn't already been printed, or the status has changed
163  if (((iterTest_->getNumIters() % modTest_ == 0) && (iterTest_->getNumIters()!=lastNumIters_)) || (state_ == Passed)) {
164  lastNumIters_ = iterTest_->getNumIters();
165  if ( (state_ & stateTest_) == state_) {
166  if ( printer_->isVerbosity(StatusTestDetails) ) {
167  print( printer_->stream(StatusTestDetails) );
168  }
169  else if ( printer_->isVerbosity(Debug) ) {
170  print( printer_->stream(Debug) );
171  }
172  }
173  }
174 
175  return state_;
176  }
177 
180  return state_;
181  }
183 
184 
186 
187 
190  void setOutputManager(const Teuchos::RCP<OutputManager<ScalarType> > &printer) { printer_ = printer; }
191 
194  void setOutputFrequency(int mod) { modTest_ = mod; }
195 
200  void setChild(Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test) {
201 
202  // First check to see if this test is a combination test
203  Teuchos::RCP<StatusTestCombo_t> comboTest = Teuchos::rcp_dynamic_cast<StatusTestCombo_t>(test);
204  TEUCHOS_TEST_FOR_EXCEPTION(comboTest == Teuchos::null,StatusTestError,"StatusTestResNormOutput(): test must be a Belos::StatusTestCombo.");
205  std::vector<Teuchos::RCP<StatusTest<ScalarType,MV,OP> > > tmpVec = comboTest->getStatusTests();
206 
207  // Get the number of tests.
208  int numTests = tmpVec.size();
209 
210  // Find the maximum iteration and residual tests
211  for (int i=0; i<numTests; ++i) {
212 
213  // Check if this is a maximum iteration test.
214  Teuchos::RCP<StatusTestMaxIters_t> tmpItrTest = Teuchos::rcp_dynamic_cast<StatusTestMaxIters_t>(tmpVec[i]);
215  if (tmpItrTest != Teuchos::null) {
216  iterTest_ = tmpItrTest;
217  continue;
218  }
219 
220  // Check if this is a single residual test
221  Teuchos::RCP<StatusTestResNorm_t> tmpResTest = Teuchos::rcp_dynamic_cast<StatusTestResNorm_t>(tmpVec[i]);
222  // If the residual status test is a single test, put in the vector
223  if (tmpResTest != Teuchos::null) {
224  numResTests_ = 1;
225  resTestVec_.resize( numResTests_ );
226  resTestVec_[0] = tmpResTest;
227  continue;
228  }
229 
230  // Check if the residual test is a combination of several StatusTestResNorm objects.
231  Teuchos::RCP<StatusTestCombo_t> tmpComboTest = Teuchos::rcp_dynamic_cast<StatusTestCombo_t>(tmpVec[i]);
232  TEUCHOS_TEST_FOR_EXCEPTION(tmpComboTest == Teuchos::null,StatusTestError,"StatusTestResNormOutput(): test must be Belos::StatusTest[MaxIters|ResNorm|Combo].");
233  tmpVec = tmpComboTest->getStatusTests();
234  comboType_ = tmpComboTest->getComboType();
235  numResTests_ = tmpVec.size();
236  resTestVec_.resize( numResTests_ );
237  for (int j=0; j<numResTests_; ++j) {
238  tmpResTest = Teuchos::rcp_dynamic_cast<StatusTestResNorm_t>(tmpVec[j]);
239  TEUCHOS_TEST_FOR_EXCEPTION(tmpResTest == Teuchos::null,StatusTestError,"StatusTestResNormOutput(): test must be a vector of Belos::StatusTestResNorm.");
240  resTestVec_[j] = tmpResTest;
241  }
242  }
243 
244  // Keep the pointer to the new test and reset the state to Undefined.
245  test_ = test;
246  state_ = Undefined;
247 
248  }
249 
251  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > getChild() const {
252  return test_;
253  }
254 
257  void setSolverDesc(const std::string& solverDesc) { solverDesc_ = solverDesc; }
258 
261  void setPrecondDesc(const std::string& precondDesc) { precondDesc_ = precondDesc; }
263 
264 
266 
267 
272  void reset() {
273  state_ = Undefined;
274  test_->reset();
275  lastNumIters_ = -1;
276  headerPrinted_ = false;
277  }
278 
280 
283  void resetNumCalls() {}
284 
286 
288 
289 
291  void print(std::ostream& os, int indent = 0) const {
292  std::string ind(indent,' ');
293  std::string starLine(55,'*');
294  std::string starFront(5,'*');
295 
296  std::ios_base::fmtflags osFlags(os.flags());
297 
298  os.setf(std::ios::scientific, std::ios::floatfield);
299  os.precision(6);
300 
301  // Print header if this is the first call to this output status test.
302  if (!headerPrinted_) {
303  os << std::endl << ind << starLine << std::endl;
304  os << ind << starFront << " Belos Iterative Solver: " << solverDesc_ << std::endl;
305  if (precondDesc_ != "")
306  os << ind << starFront << " Preconditioner: " << precondDesc_ << std::endl;
307  os << ind << starFront << " Maximum Iterations: " << iterTest_->getMaxIters() << std::endl;
308  os << ind << starFront << " Block Size: " << blockSize_ << std::endl;
309  if (numResTests_ > 1) {
310  os << ind << starFront << " Residual Tests ("
311  << ((comboType_ == StatusTestCombo_t::OR) ? "OR" : (comboType_ == StatusTestCombo_t::AND) ? "AND" :"SEQ")
312  << "): " << std::endl;
313  } else {
314  os << ind << starFront << " Residual Test: " << std::endl;
315  }
316  for (int i=0; i<numResTests_; ++i) {
317  os << ind << starFront << " Test " << i+1 << " : " << resTestVec_[i]->description() << std::endl;
318  }
319  os << ind << starLine << std::endl;
320  headerPrinted_ = true;
321  }
322 
323  // Print out residuals for each residual test.
324  os.setf(std::ios_base::right, std::ios_base::adjustfield);
325  std::string ind2( 7 + numIterDgts_, ' ' );
326  os << ind << "Iter " << std::setw(numIterDgts_) << iterTest_->getNumIters() << ", ";
327  for (int i=0; i<currNumRHS_; ++i) {
328  if ( i > 0 && currIdx_[i]!=-1 ) {
329  // Put in space where 'Iter :' is in the previous lines
330  os << ind << ind2;
331  }
332  os << "[" << std::setw(numLSDgts_) << currIdx_[i]+1 << "] : ";
333  for (int j=0; j<numResTests_; ++j) {
334  if ( resTestVec_[j]->getStatus() != Undefined && currIdx_[i]!=-1 ) {
335  os << std::setw(15) << (*resTestVec_[j]->getTestValue())[currIdx_[i]];
336  } else {
337  os << std::setw(15) << "---";
338  }
339  }
340  os << std::endl;
341  }
342  // reset os format
343  os.flags(osFlags);
344  }
345 
347 
348  private:
349  // Output manager.
350  Teuchos::RCP<OutputManager<ScalarType> > printer_;
351 
352  // Overall status test.
353  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test_;
354 
355  // Iteration test (as passed in).
356  Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > iterTest_;
357 
359  std::vector<Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > > resTestVec_;
360 
361  std::string solverDesc_;
362  std::string precondDesc_;
363  std::vector<int> currIdx_;
364  StatusType state_;
365  mutable bool headerPrinted_;
366  int stateTest_, modTest_;
367  int lastNumIters_, comboType_;
368  int numResTests_, blockSize_;
369  int currNumRHS_, currLSNum_;
370  int numLSDgts_, numIterDgts_;
371 };
372 
373 } // end of Belos namespace
374 
375 #endif /* BELOS_STATUS_TEST_RESNORM_OUTPUT_HPP */
BelosConfigDefs.hpp
Belos header file which uses auto-configuration information to include necessary C++ headers.
Belos::LinearProblem::getLSNumber
int getLSNumber() const
The number of linear systems that have been set.
Definition: BelosLinearProblem.hpp:404
Belos::OutputManager
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
Definition: BelosIteration.hpp:64
BelosStatusTest.hpp
Pure virtual base class for defining the status testing capabilities of Belos.
Belos::StatusTestResNormOutput::checkStatus
StatusType checkStatus(Iteration< ScalarType, MV, OP > *solver)
Definition: BelosStatusTestResNormOutput.hpp:145
Belos::Iteration
Definition: BelosIteration.hpp:73
Belos::StatusTestResNormOutput::resetNumCalls
void resetNumCalls()
Informs the outputting status test that it should reset the number of calls to zero.
Definition: BelosStatusTestResNormOutput.hpp:283
Belos::StatusTestError
Exception thrown to signal error in a status test during Belos::StatusTest::checkStatus().
Definition: BelosStatusTest.hpp:73
Belos::StatusTestResNormOutput::StatusTestResNormOutput
StatusTestResNormOutput(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test, int mod=1, int printStates=Passed)
Constructor.
Definition: BelosStatusTestResNormOutput.hpp:100
BelosStatusTestResNorm.hpp
Belos::StatusTest abstract class for specifying a residual norm stopping criteria.
Belos::StatusTestMaxIters
A Belos::StatusTest class for specifying a maximum number of iterations.
Definition: BelosStatusTestMaxIters.hpp:63
Belos::Passed
Definition: BelosTypes.hpp:188
Belos::MultiVecTraits::GetNumberVecs
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Definition: BelosMultiVecTraits.hpp:218
Belos::StatusTestDetails
Definition: BelosTypes.hpp:264
BelosStatusTestOutput.hpp
Virtual base class for StatusTest that printing status tests.
Belos::LinearProblem
A linear system to solve, and its associated information.
Definition: BelosIteration.hpp:61
Belos::StatusTestResNormOutput::setOutputManager
void setOutputManager(const Teuchos::RCP< OutputManager< ScalarType > > &printer)
Set the output manager.
Definition: BelosStatusTestResNormOutput.hpp:190
Belos::StatusTestResNormOutput::setOutputFrequency
void setOutputFrequency(int mod)
Set how often the child test is printed.
Definition: BelosStatusTestResNormOutput.hpp:194
Belos::StatusTestResNormOutput::print
void print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
Definition: BelosStatusTestResNormOutput.hpp:291
Belos
Definition: Belos_Details_EBelosSolverType.cpp:45
Belos::StatusType
StatusType
Whether the StatusTest wants iteration to stop.
Definition: BelosTypes.hpp:188
BelosStatusTestMaxIters.hpp
Belos::StatusTest class for specifying a maximum number of iterations.
Belos::Undefined
Definition: BelosTypes.hpp:190
Belos::StatusTestResNormOutput
A special StatusTest for printing other status tests in a simple format.
Definition: BelosStatusTestResNormOutput.hpp:74
Belos::LinearProblem::getLSIndex
const std::vector< int > getLSIndex() const
(Zero-based) indices of the linear system(s) currently being solved.
Definition: BelosLinearProblem.hpp:396
Belos::StatusTestResNormOutput::getChild
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > getChild() const
Get child test.
Definition: BelosStatusTestResNormOutput.hpp:251
Belos::LinearProblem::getRHS
Teuchos::RCP< const MV > getRHS() const
A pointer to the right-hand side B.
Definition: BelosLinearProblem.hpp:326
Belos::StatusTestResNorm
An abstract class of StatusTest for stopping criteria using residual norms.
Definition: BelosStatusTestResNorm.hpp:62
Belos::Iteration::getBlockSize
virtual int getBlockSize() const =0
Get the blocksize to be used by the iterative solver in solving this linear problem.
Belos::StatusTestResNormOutput::getStatus
StatusType getStatus() const
Return the result of the most recent checkStatus call, or undefined if it has not been run.
Definition: BelosStatusTestResNormOutput.hpp:179
Belos::StatusTest
A pure virtual class for defining the status tests for the Belos iterative solvers.
Definition: BelosIteration.hpp:67
Belos::StatusTestCombo::OR
Definition: BelosStatusTestCombo.hpp:110
Belos::StatusTestCombo
A class for extending the status testing capabilities of Belos via logical combinations.
Definition: BelosStatusTestCombo.hpp:91
Belos::StatusTestResNormOutput::setChild
void setChild(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Set child test, which must be a combination of a Belos::StatusTestMaxIters AND a single or combinatio...
Definition: BelosStatusTestResNormOutput.hpp:200
Belos::Iteration::getProblem
virtual const LinearProblem< ScalarType, MV, OP > & getProblem() const =0
Get a constant reference to the linear problem.
BelosTypes.hpp
Collection of types and exceptions used within the Belos solvers.
Belos::Debug
Definition: BelosTypes.hpp:265
Belos::StatusTestOutput
A virtual base class for StatusTest that print other status tests.
Definition: BelosStatusTestOutput.hpp:72
Belos::StatusTestResNormOutput::~StatusTestResNormOutput
virtual ~StatusTestResNormOutput()
Destructor.
Definition: BelosStatusTestResNormOutput.hpp:123
Belos::StatusTestResNormOutput::reset
void reset()
Informs the status test that it should reset its internal configuration to the uninitialized state.
Definition: BelosStatusTestResNormOutput.hpp:272
Belos::StatusTestResNormOutput::setSolverDesc
void setSolverDesc(const std::string &solverDesc)
Set a short solver description for output clarity.
Definition: BelosStatusTestResNormOutput.hpp:257
BelosIteration.hpp
Pure virtual base class which describes the basic interface to the linear solver iteration.
Belos::MultiVecTraits
Traits class which defines basic operations on multivectors.
Definition: BelosMultiVecTraits.hpp:129
Belos::StatusTestCombo::AND
Definition: BelosStatusTestCombo.hpp:109
BelosStatusTestCombo.hpp
Belos::StatusTest for logically combining several status tests.
Belos::StatusTestResNormOutput::setPrecondDesc
void setPrecondDesc(const std::string &precondDesc)
Set a short preconditioner description for output clarity.
Definition: BelosStatusTestResNormOutput.hpp:261

Generated on Thu Feb 27 2020 16:06:46 for Belos by doxygen 1.8.16