Thyra Package Browser (Single Doxygen Collection) Version of the Day
Thyra_DefaultEvaluationLoggerModelEvaluator.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
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 #ifndef THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
00030 #define THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
00031 
00032 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
00033 #include "Thyra_LinearOpWithSolveFactoryBase.hpp"
00034 #include "Teuchos_Time.hpp"
00035 
00036 namespace Thyra {
00037 
00038 
00046 template<class Scalar>
00047 class DefaultEvaluationLoggerModelEvaluator
00048   : virtual public ModelEvaluatorDelegatorBase<Scalar>
00049 {
00050 public:
00051 
00054 
00056   DefaultEvaluationLoggerModelEvaluator();
00057 
00059   DefaultEvaluationLoggerModelEvaluator(
00060     const RCP<ModelEvaluator<Scalar> >   &thyraModel
00061     ,const RCP<std::ostream>             &tableOut
00062     );
00063 
00076   void initialize(
00077     const RCP<ModelEvaluator<Scalar> >   &thyraModel
00078     ,const RCP<std::ostream>             &tableOut
00079     );
00080 
00082 
00085 
00087   std::string description() const;
00088 
00090 
00091 private:
00092 
00095 
00097   void evalModelImpl(
00098     const ModelEvaluatorBase::InArgs<Scalar>    &inArgs
00099     ,const ModelEvaluatorBase::OutArgs<Scalar>  &outArgs
00100     ) const;
00101 
00103 
00104 private:
00105 
00106   RCP<std::ostream> tableOut_;
00107   Teuchos::Time timer_;
00108   
00109   mutable bool headerPrinted_;
00110   mutable bool supports_f_;
00111   mutable bool supports_W_;
00112   
00113   static const int flt_width_;
00114   static const int flt_sciPrec_;
00115   static const int flt_prec_;
00116   static const char flt_line_[];
00117   static const int int_width_;
00118   static const char int_line_[];
00119   
00120   void printHeader( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
00121   void printLine( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
00122   
00123 };
00124 
00125 // /////////////////////////////////
00126 // Implementations
00127 
00128 // Constructors/initializers/accessors/utilities
00129 
00130 template<class Scalar>
00131 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_width_ = 25; 
00132 template<class Scalar>
00133 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_sciPrec_  = 16;
00134 template<class Scalar>
00135 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_prec_  = 16;
00136 template<class Scalar>
00137 const char DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_line_[]  = "-------------------------";
00138 template<class Scalar>
00139 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::int_width_ = 10; 
00140 template<class Scalar>
00141 const char DefaultEvaluationLoggerModelEvaluator<Scalar>::int_line_[]  = "----------";
00142 
00143 template<class Scalar>
00144 DefaultEvaluationLoggerModelEvaluator<Scalar>::DefaultEvaluationLoggerModelEvaluator()
00145   :timer_(""),headerPrinted_(false)
00146 {}
00147 
00148 template<class Scalar>
00149 DefaultEvaluationLoggerModelEvaluator<Scalar>::DefaultEvaluationLoggerModelEvaluator(
00150   const RCP<ModelEvaluator<Scalar> >   &thyraModel
00151   ,const RCP<std::ostream>             &tableOut
00152   )
00153   :timer_(""),headerPrinted_(false)
00154 {
00155   initialize(thyraModel,tableOut);
00156 }
00157 
00158 template<class Scalar>
00159 void DefaultEvaluationLoggerModelEvaluator<Scalar>::initialize(
00160   const RCP<ModelEvaluator<Scalar> >   &thyraModel
00161   ,const RCP<std::ostream>             &tableOut
00162   )
00163 {
00164   TEST_FOR_EXCEPT( tableOut.get()==NULL );
00165   this->ModelEvaluatorDelegatorBase<Scalar>::initialize(thyraModel);
00166   tableOut_ = tableOut;
00167   timer_.start(true);
00168   headerPrinted_ = false;
00169 }
00170 
00171 
00172 // Public functions overridden from Teuchos::Describable
00173 
00174 
00175 template<class Scalar>
00176 std::string DefaultEvaluationLoggerModelEvaluator<Scalar>::description() const
00177 {
00178   const RCP<const ModelEvaluator<Scalar> >
00179     thyraModel = this->getUnderlyingModel();
00180   std::ostringstream oss;
00181   oss << "Thyra::DefaultEvaluationLoggerModelEvaluator{";
00182   oss << "thyraModel=";
00183   if(thyraModel.get())
00184     oss << "\'"<<thyraModel->description()<<"\'";
00185   else
00186     oss << "NULL";
00187   oss << "}";
00188   return oss.str();
00189 }
00190 
00191 
00192 // Private functions overridden from ModelEvaulatorDefaultBase
00193 
00194 
00195 template<class Scalar>
00196 void DefaultEvaluationLoggerModelEvaluator<Scalar>::evalModelImpl(
00197   const ModelEvaluatorBase::InArgs<Scalar>     &inArgs
00198   ,const ModelEvaluatorBase::OutArgs<Scalar>   &outArgs
00199   ) const
00200 {
00201 
00202   THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
00203     "Thyra::DefaultEvaluationLoggerModelEvaluator",inArgs,outArgs
00204     );
00205 
00206   thyraModel->evalModel(inArgs,outArgs);
00207 
00208   if(!headerPrinted_) {
00209     printHeader(outArgs);
00210     headerPrinted_ = true;
00211   }
00212   printLine(outArgs);
00213 
00214   THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
00215   
00216 }
00217 
00218 
00219 // private
00220 
00221 
00222 template<class Scalar>
00223 void DefaultEvaluationLoggerModelEvaluator<Scalar>::printHeader(
00224   const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
00225   ) const
00226 {
00227 
00228   using std::setw;
00229   using std::setprecision;
00230   using std::right;
00231   using std::left;
00232   typedef ModelEvaluatorBase MEB;
00233 
00234   supports_f_ = outArgs.supports(MEB::OUT_ARG_f);
00235   supports_W_ = outArgs.supports(MEB::OUT_ARG_W);
00236 
00237   const int Ng = outArgs.Ng();
00238 
00239   *tableOut_
00240     << "\n***"
00241     << "\n*** Table of function evaluations vs. CPU time"
00242     << "\n***\n";
00243 
00244   *tableOut_
00245     << "\nModel Evaluator Description:\n" << Teuchos::describe(*this,Teuchos::VERB_LOW);
00246   
00247   *tableOut_ << "\n";
00248   *tableOut_ << "  " << left << setw(flt_width_) << "time(s)";
00249   for( int j = 0; j < Ng; ++j ) {
00250     std::ostringstream oss;
00251     oss << "||g("<<j<<")||";
00252     *tableOut_ << "  " << left << setw(flt_width_) << oss.str();
00253   }
00254   if(supports_f_)
00255     *tableOut_ << "  " << left << setw(flt_width_) << "||f||";
00256   if(supports_W_)
00257     *tableOut_ << "  " << left << setw(int_width_) << "Calc W";
00258   *tableOut_ << "\n";
00259   
00260   *tableOut_ << "  " << left << setw(flt_width_) << flt_line_;   // time(s)
00261   for( int j = 0; j < Ng; ++j )
00262     *tableOut_ << "  " << left << setw(flt_width_) << flt_line_; // ||g(j)||
00263   if(supports_f_)
00264     *tableOut_ << "  " << left << setw(flt_width_) << flt_line_; // ||f||
00265   if(supports_W_)
00266     *tableOut_ << "  " << left << setw(int_width_) << int_line_; // Calc W
00267   *tableOut_ << "\n";
00268 
00269 }
00270 
00271 template<class Scalar>
00272 void DefaultEvaluationLoggerModelEvaluator<Scalar>::printLine(
00273   const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
00274   ) const
00275 {
00276 
00277   using std::right;
00278   using std::left;
00279   using std::setprecision;
00280   using std::setw;
00281 
00282   const int Ng = outArgs.Ng();
00283 
00284   RCP<const VectorBase<Scalar> > f, g_j;
00285   
00286   *tableOut_ << "  " << setprecision(flt_prec_) << right << setw(flt_width_) << timer_.totalElapsedTime(true);
00287   for( int j = 0; j < Ng; ++j ) {
00288     if((g_j=outArgs.get_g(j)).get())
00289       *tableOut_ << "  " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*g_j);
00290     else
00291       *tableOut_ << "  " << right << setw(flt_width_) << "-";
00292   }
00293   if(supports_f_) {
00294     if((f=outArgs.get_f()).get())
00295       *tableOut_ << "  " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*f);
00296     else
00297       *tableOut_ << "  " << right << setw(flt_width_) << "-";
00298   }
00299   if(supports_W_) {
00300     if(outArgs.get_W().get())
00301       *tableOut_ << "  " << right << setw(int_width_) << "1";
00302     else
00303       *tableOut_ << "  " << right << setw(int_width_) << "-";
00304   }
00305   *tableOut_ << "\n";
00306 
00307 }
00308 
00309 } // namespace Thyra
00310 
00311 #endif // THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines