00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00106 test_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, iterTest_, resTest_ ) );
00107
00108
00109 Teuchos::RCP<StatusTestResNorm_t> tmpResTest = Teuchos::rcp_dynamic_cast<StatusTestResNorm_t>(resTest);
00110
00111 if (tmpResTest != Teuchos::null) {
00112 resTestVec_.push_back( tmpResTest );
00113 numResTests_ = 1;
00114 } else {
00115
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
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
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
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
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
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
00295 Teuchos::RCP<OutputManager<ScalarType> > printer_;
00296
00297
00298 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test_;
00299
00300
00301 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > resTest_;
00302
00303
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 }
00321
00322 #endif