Thyra_DefaultEvaluationLoggerModelEvaluator.hpp

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 
00044 template<class Scalar>
00045 class DefaultEvaluationLoggerModelEvaluator
00046   : virtual public ModelEvaluatorDelegatorBase<Scalar>
00047 {
00048 public:
00049 
00052 
00054   DefaultEvaluationLoggerModelEvaluator();
00055 
00057   DefaultEvaluationLoggerModelEvaluator(
00058     const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >   &thyraModel
00059     ,const Teuchos::RefCountPtr<std::ostream>             &tableOut
00060     );
00061 
00074   void initialize(
00075     const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >   &thyraModel
00076     ,const Teuchos::RefCountPtr<std::ostream>             &tableOut
00077     );
00078 
00080 
00083 
00085   void evalModel(
00086     const ModelEvaluatorBase::InArgs<Scalar>    &inArgs
00087     ,const ModelEvaluatorBase::OutArgs<Scalar>  &outArgs
00088     ) const;
00089 
00091 
00094 
00096   std::string description() const;
00097 
00099 
00100 private:
00101 
00102   Teuchos::RefCountPtr<std::ostream>  tableOut_;
00103   Teuchos::Time                       timer_;
00104 
00105   mutable bool                        headerPrinted_;
00106   mutable bool                        supports_f_;
00107   mutable bool                        supports_W_;
00108 
00109   static const int    flt_width_;
00110   static const int    flt_sciPrec_;
00111   static const int    flt_prec_;
00112   static const char   flt_line_[];
00113   static const int    int_width_;
00114   static const char   int_line_[];
00115 
00116   void printHeader( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
00117   void printLine( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
00118   
00119 };
00120 
00121 // /////////////////////////////////
00122 // Implementations
00123 
00124 // Constructors/initializers/accessors/utilities
00125 
00126 template<class Scalar>
00127 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_width_ = 25; 
00128 template<class Scalar>
00129 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_sciPrec_  = 16;
00130 template<class Scalar>
00131 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_prec_  = 16;
00132 template<class Scalar>
00133 const char DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_line_[]  = "-------------------------";
00134 template<class Scalar>
00135 const int DefaultEvaluationLoggerModelEvaluator<Scalar>::int_width_ = 10; 
00136 template<class Scalar>
00137 const char DefaultEvaluationLoggerModelEvaluator<Scalar>::int_line_[]  = "----------";
00138 
00139 template<class Scalar>
00140 DefaultEvaluationLoggerModelEvaluator<Scalar>::DefaultEvaluationLoggerModelEvaluator()
00141   :timer_(""),headerPrinted_(false)
00142 {}
00143 
00144 template<class Scalar>
00145 DefaultEvaluationLoggerModelEvaluator<Scalar>::DefaultEvaluationLoggerModelEvaluator(
00146   const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >   &thyraModel
00147   ,const Teuchos::RefCountPtr<std::ostream>             &tableOut
00148   )
00149   :timer_(""),headerPrinted_(false)
00150 {
00151   initialize(thyraModel,tableOut);
00152 }
00153 
00154 template<class Scalar>
00155 void DefaultEvaluationLoggerModelEvaluator<Scalar>::initialize(
00156   const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >   &thyraModel
00157   ,const Teuchos::RefCountPtr<std::ostream>             &tableOut
00158   )
00159 {
00160   TEST_FOR_EXCEPT( tableOut.get()==NULL );
00161   this->ModelEvaluatorDelegatorBase<Scalar>::initialize(thyraModel);
00162   tableOut_ = tableOut;
00163   timer_.start(true);
00164   headerPrinted_ = false;
00165 }
00166 
00167 // Overridden from ModelEvaulator.
00168 
00169 template<class Scalar>
00170 void DefaultEvaluationLoggerModelEvaluator<Scalar>::evalModel(
00171   const ModelEvaluatorBase::InArgs<Scalar>     &inArgs
00172   ,const ModelEvaluatorBase::OutArgs<Scalar>   &outArgs
00173   ) const
00174 {
00175   typedef ModelEvaluatorBase MEB;
00176   using Teuchos::RefCountPtr;
00177   using Teuchos::rcp;
00178   using Teuchos::rcp_const_cast;
00179   using Teuchos::rcp_dynamic_cast;
00180   using Teuchos::OSTab;
00181 
00182   const Teuchos::RefCountPtr<Teuchos::FancyOStream> out       = this->getOStream();
00183   const Teuchos::EVerbosityLevel                    verbLevel = this->getVerbLevel();
00184   Teuchos::OSTab tab(out);
00185   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00186     *out << "\nEntering Thyra::DefaultEvaluationLoggerModelEvaluator<Scalar>::evalModel(...) ...\n";
00187 
00188   const Teuchos::RefCountPtr<const ModelEvaluator<Scalar> >
00189     thyraModel = this->getUnderlyingModel();
00190 
00191   thyraModel->evalModel(inArgs,outArgs);
00192 
00193   if(!headerPrinted_) {
00194     printHeader(outArgs);
00195     headerPrinted_ = true;
00196   }
00197   printLine(outArgs);
00198 
00199   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00200     *out
00201       << "\nLeaving Thyra::DefaultEvaluationLoggerModelEvaluator<Scalar>::evalModel(...) ...\n";
00202   
00203 }
00204 
00205 // Public functions overridden from Teuchos::Describable
00206 
00207 template<class Scalar>
00208 std::string DefaultEvaluationLoggerModelEvaluator<Scalar>::description() const
00209 {
00210   const Teuchos::RefCountPtr<const ModelEvaluator<Scalar> >
00211     thyraModel = this->getUnderlyingModel();
00212   std::ostringstream oss;
00213   oss << "Thyra::DefaultEvaluationLoggerModelEvaluator{";
00214   oss << "thyraModel=";
00215   if(thyraModel.get())
00216     oss << "\'"<<thyraModel->description()<<"\'";
00217   else
00218     oss << "NULL";
00219   oss << "}";
00220   return oss.str();
00221 }
00222 
00223 // private
00224 
00225 template<class Scalar>
00226 void DefaultEvaluationLoggerModelEvaluator<Scalar>::printHeader(
00227   const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
00228   ) const
00229 {
00230 
00231   using std::setw;
00232   using std::setprecision;
00233   using std::right;
00234   using std::left;
00235   typedef ModelEvaluatorBase MEB;
00236 
00237   supports_f_ = outArgs.supports(MEB::OUT_ARG_f);
00238   supports_W_ = outArgs.supports(MEB::OUT_ARG_W);
00239 
00240   const int Ng = outArgs.Ng();
00241 
00242   *tableOut_
00243     << "\n***"
00244     << "\n*** Table of function evaluations vs. CPU time"
00245     << "\n***\n";
00246 
00247   *tableOut_
00248     << "\nModel Evaluator Description:\n" << Teuchos::describe(*this,Teuchos::VERB_LOW);
00249   
00250   *tableOut_ << "\n";
00251   *tableOut_ << "  " << left << setw(flt_width_) << "time(s)";
00252   for( int j = 0; j < Ng; ++j ) {
00253     std::ostringstream oss;
00254     oss << "||g("<<j<<")||";
00255     *tableOut_ << "  " << left << setw(flt_width_) << oss.str();
00256   }
00257   if(supports_f_)
00258     *tableOut_ << "  " << left << setw(flt_width_) << "||f||";
00259   if(supports_W_)
00260     *tableOut_ << "  " << left << setw(int_width_) << "Calc W";
00261   *tableOut_ << "\n";
00262   
00263   *tableOut_ << "  " << left << setw(flt_width_) << flt_line_;   // time(s)
00264   for( int j = 0; j < Ng; ++j )
00265     *tableOut_ << "  " << left << setw(flt_width_) << flt_line_; // ||g(j)||
00266   if(supports_f_)
00267     *tableOut_ << "  " << left << setw(flt_width_) << flt_line_; // ||f||
00268   if(supports_W_)
00269     *tableOut_ << "  " << left << setw(int_width_) << int_line_; // Calc W
00270   *tableOut_ << "\n";
00271 
00272 }
00273 
00274 template<class Scalar>
00275 void DefaultEvaluationLoggerModelEvaluator<Scalar>::printLine(
00276   const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
00277   ) const
00278 {
00279 
00280   const int Ng = outArgs.Ng();
00281 
00282   Teuchos::RefCountPtr<const VectorBase<Scalar> > f, g_j;
00283   
00284   *tableOut_ << "  " << setprecision(flt_prec_) << right << setw(flt_width_) << timer_.totalElapsedTime(true);
00285   for( int j = 0; j < Ng; ++j ) {
00286     if((g_j=outArgs.get_g(j)).get())
00287       *tableOut_ << "  " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*g_j);
00288     else
00289       *tableOut_ << "  " << right << setw(flt_width_) << "-";
00290   }
00291   if(supports_f_) {
00292     if((f=outArgs.get_f()).get())
00293       *tableOut_ << "  " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*f);
00294     else
00295       *tableOut_ << "  " << right << setw(flt_width_) << "-";
00296   }
00297   if(supports_W_) {
00298     if(outArgs.get_W().get())
00299       *tableOut_ << "  " << right << setw(int_width_) << "1";
00300     else
00301       *tableOut_ << "  " << right << setw(int_width_) << "-";
00302   }
00303   *tableOut_ << "\n";
00304 
00305 }
00306 
00307 } // namespace Thyra
00308 
00309 #endif // THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP

Generated on Thu Sep 18 12:32:48 2008 for Thyra Nonlinear Model Evaluator Support by doxygen 1.3.9.1