BelosStatusTestSimpleOutput.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //       Belos: Block Linear Solvers Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 // @HEADER
00028 //
00029 
00030 #ifndef BELOS_STATUS_TEST_SIMPLE_OUTPUT_HPP
00031 #define BELOS_STATUS_TEST_SIMPLE_OUTPUT_HPP
00032 
00038 #include <vector>
00039 #include "BelosConfigDefs.hpp"
00040 #include "BelosTypes.hpp"
00041 #include "BelosIteration.hpp"
00042 
00043 #include "BelosStatusTest.hpp"
00044 #include "BelosStatusTestCombo.hpp"
00045 #include "BelosStatusTestMaxIters.hpp"
00046 #include "BelosStatusTestResNorm.hpp"
00047 
00048 
00049 namespace Belos {
00050 
00060 template <class ScalarType, class MV, class OP>
00061 class StatusTestSimpleOutput : public StatusTest<ScalarType,MV,OP> {
00062 
00063   typedef Belos::StatusTestCombo<ScalarType,MV,OP>  StatusTestCombo_t;
00064   typedef Belos::StatusTestResNorm<ScalarType,MV,OP>  StatusTestResNorm_t;
00065 
00066  public:
00068 
00069 
00086   StatusTestSimpleOutput(const Teuchos::RCP<OutputManager<ScalarType> > &printer, 
00087       Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > iterTest,
00088       Teuchos::RCP<StatusTest<ScalarType,MV,OP> > resTest,
00089       int mod = 1,
00090       int printStates = Passed)
00091     : printer_(printer), 
00092       iterTest_(iterTest), 
00093       resTest_(resTest), 
00094       state_(Undefined), 
00095       headerPrinted_(false),
00096       stateTest_(printStates), 
00097       modTest_(mod), 
00098       numCalls_(0),
00099       comboType_(0),
00100       numResTests_(0),
00101       blockSize_(1),
00102       currNumRHS_(0),
00103       currLSNum_(0)
00104     {
00105       // Create the status test combination of the iteration and residual test.
00106       test_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, iterTest_, resTest_ ) );
00107 
00108       // First check if the residual status test is a single test
00109       Teuchos::RCP<StatusTestResNorm_t> tmpResTest = Teuchos::rcp_dynamic_cast<StatusTestResNorm_t>(resTest);
00110       // If the residual status test is a single test, put in the vector
00111       if (tmpResTest != Teuchos::null) {
00112   resTestVec_.push_back( tmpResTest );
00113         numResTests_ = 1;
00114       } else {
00115         // Check if the residual test is a combination of several StatusTestResNorm objects.
00116         Teuchos::RCP<StatusTestCombo_t> tmpComboTest = Teuchos::rcp_dynamic_cast<StatusTestCombo_t>(resTest);
00117         TEST_FOR_EXCEPTION(tmpComboTest == Teuchos::null,StatusTestError,"StatusTestSimpleOutput():  resTest must be Belos::StatusTestResNorm or Belos::StatusTestCombo.");
00118         std::vector<Teuchos::RCP<StatusTest<ScalarType,MV,OP> > > tmpVec = tmpComboTest->getStatusTests();
00119         comboType_ = tmpComboTest->getComboType();
00120         numResTests_ = tmpVec.size();
00121         resTestVec_.resize( numResTests_ );
00122         for (int i=0; i<numResTests_; ++i) {
00123     tmpResTest = Teuchos::rcp_dynamic_cast<StatusTestResNorm_t>(tmpVec[i]);
00124           TEST_FOR_EXCEPTION(tmpResTest == Teuchos::null,StatusTestError,"StatusTestSimpleOutput():  resTest must be a vector of Belos::StatusTestResNorm.");
00125           resTestVec_[i] = tmpResTest;
00126         }
00127       }
00128     }
00129 
00131   virtual ~StatusTestSimpleOutput() {};
00133 
00135 
00136 
00153   StatusType checkStatus( Iteration<ScalarType,MV,OP>* solver ) 
00154   {
00155     TEST_FOR_EXCEPTION(iterTest_ == Teuchos::null,StatusTestError,"StatusTestSimpleOutput::checkStatus():  iteration test pointer is null.");
00156     TEST_FOR_EXCEPTION(resTest_ == Teuchos::null,StatusTestError,"StatusTestSimpleOutput::checkStatus():  residual test pointer is null.");
00157     state_ = test_->checkStatus(solver);
00158 
00159     // Update some information for the header, if it has not printed or the linear system has changed.
00160     LinearProblem<ScalarType,MV,OP> currProb = solver->getProblem();
00161     if (!headerPrinted_ || currLSNum_ != currProb.getLSNumber()) {
00162       currLSNum_ = currProb.getLSNumber();
00163       blockSize_ = solver->getBlockSize();
00164       currIdx_ = currProb.getLSIndex();
00165       currNumRHS_ = currIdx_.size();
00166     }
00167 //    if (numCalls_++ % modTest_ == 0) {
00168     if ((iterTest_->getNumIters() % modTest_ == 0) || (state_ == Passed)) {
00169       if ( (state_ & stateTest_) == state_) {
00170         if ( printer_->isVerbosity(StatusTestDetails) ) {
00171           print( printer_->stream(StatusTestDetails) );
00172         }
00173         else if ( printer_->isVerbosity(Debug) ) {
00174           print( printer_->stream(Debug) );
00175         }
00176       }
00177     }
00178 
00179     return state_;
00180   }
00181 
00183   StatusType getStatus() const {
00184     return state_;
00185   }
00187 
00188 
00190 
00191 
00194   void setOutputManager(const Teuchos::RCP<OutputManager<ScalarType> > &printer) { printer_ = printer; }
00195 
00198   void setOutputFrequency(int mod) { modTest_ = mod; }
00199 
00202   void setSolverDesc(const std::string& solverDesc) { solverDesc_ = solverDesc; }
00203 
00206   void setPrecondDesc(const std::string& precondDesc) { precondDesc_ = precondDesc; }
00208 
00209 
00211 
00212 
00217   void reset() { 
00218     state_ = Undefined;
00219     test_->reset();
00220     numCalls_ = 0;
00221     headerPrinted_ = false;
00222   }
00223 
00225   void resetNumCalls() { numCalls_ = 0; }
00226 
00229   void clearStatus() {
00230     state_ = Undefined;
00231     test_->clearStatus();
00232     headerPrinted_ = false;
00233   }
00234 
00236 
00238 
00239   
00241   void print(std::ostream& os, int indent = 0) const {
00242     std::string ind(indent,' ');
00243     std::string starLine(60,'*');
00244     std::string starFront(5,'*');
00245 
00246     os.setf(std::ios::scientific, std::ios::floatfield);
00247     os.precision(6);
00248     
00249     // Print header if this is the first call to this output status test.
00250     if (numCalls_ == 1 || !headerPrinted_) {
00251       os << std::endl << ind << starLine << std::endl;
00252       os << ind << starFront << " Belos Iterative Solver: " << solverDesc_ << std::endl;
00253       if (precondDesc_ != "")
00254         os << ind << starFront << " Preconditioner: " << precondDesc_ << std::endl;
00255       os << ind << starFront << " Maximum Iterations: " << iterTest_->getMaxIters() << std::endl;
00256       os << ind << starFront << " Block Size: " << blockSize_ << std::endl;
00257       if (numResTests_ > 1) {
00258         os << ind << starFront << " Residual Tests (" 
00259            << ((comboType_ == StatusTestCombo_t::OR) ? "OR" : (comboType_ == StatusTestCombo_t::AND) ? "AND" :"SEQ")
00260            << "): " << std::endl;
00261       } else {
00262         os << ind << starFront << " Residual Test: " << std::endl;
00263       } 
00264       for (int i=0; i<numResTests_; ++i) {
00265         os << ind << starFront << "   Test " << i+1 << " : " << resTestVec_[i]->description() << std::endl;
00266       }
00267       os << ind << starLine << std::endl;
00268       headerPrinted_ = true;
00269     }
00270 
00271     // Print out residuals for each residual test.
00272     os.setf(std::ios_base::right, std::ios_base::adjustfield);
00273     std::string ind2(12,' ');
00274     os << ind << "Iter " << std::setw(5) << iterTest_->getNumIters() << " :";
00275     for (int i=0; i<currNumRHS_; ++i) {
00276       if ( i > 0 ) {
00277         // Put in space where 'Iter :' is in the previous lines
00278         os << ind << ind2;
00279       }
00280       for (int j=0; j<numResTests_; ++j) {
00281         if ( resTestVec_[j]->getStatus() != Undefined ) {
00282           os << std::setw(15) << (*resTestVec_[j]->getTestValue())[currIdx_[i]];
00283         } else {
00284           os << std::setw(15) << "---"; 
00285         }
00286       }
00287       os << std::endl;
00288     }
00289   }
00290  
00292 
00293   private:
00294     // Output manager.
00295     Teuchos::RCP<OutputManager<ScalarType> > printer_;
00296 
00297     // Overall status test.
00298     Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test_;
00299 
00300     // Residual test (as passed in).
00301     Teuchos::RCP<StatusTest<ScalarType,MV,OP> > resTest_;
00302 
00303     // Iteration test (as passed in).
00304     Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > iterTest_;
00305 
00307     std::vector<Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > > resTestVec_;
00308 
00309     std::string solverDesc_;
00310     std::string precondDesc_;
00311     std::vector<int> currIdx_;
00312     StatusType state_;
00313     mutable bool headerPrinted_;
00314     int stateTest_, modTest_;
00315     int numCalls_, comboType_;
00316     int numResTests_, blockSize_;
00317     int currNumRHS_, currLSNum_;
00318 };
00319 
00320 } // end of Belos namespace
00321 
00322 #endif /* BELOS_STATUS_TEST_OUTPUT_HPP */

Generated on Wed May 12 21:30:09 2010 for Belos by  doxygen 1.4.7