Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DefaultEvaluationLoggerModelEvaluator.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
43 #define THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
44 
45 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
46 #include "Thyra_LinearOpWithSolveFactoryBase.hpp"
47 #include "Teuchos_Time.hpp"
48 
49 namespace Thyra {
50 
51 
59 template<class Scalar>
61  : virtual public ModelEvaluatorDelegatorBase<Scalar>
62 {
63 public:
64 
67 
70 
73  const RCP<ModelEvaluator<Scalar> > &thyraModel
74  ,const RCP<std::ostream> &tableOut
75  );
76 
89  void initialize(
90  const RCP<ModelEvaluator<Scalar> > &thyraModel
91  ,const RCP<std::ostream> &tableOut
92  );
93 
95 
98 
100  std::string description() const;
101 
103 
104 private:
105 
108 
110  void evalModelImpl(
113  ) const;
114 
116 
117 private:
118 
119  RCP<std::ostream> tableOut_;
120  Teuchos::Time timer_;
121 
122  mutable bool headerPrinted_;
123  mutable bool supports_f_;
124  mutable bool supports_W_;
125 
126  static const int flt_width_;
127  static const int flt_sciPrec_;
128  static const int flt_prec_;
129  static const char flt_line_[];
130  static const int int_width_;
131  static const char int_line_[];
132 
133  void printHeader( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
134  void printLine( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
135 
136 };
137 
138 // /////////////////////////////////
139 // Implementations
140 
141 // Constructors/initializers/accessors/utilities
142 
143 template<class Scalar>
145 template<class Scalar>
147 template<class Scalar>
149 template<class Scalar>
150 const char DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_line_[] = "-------------------------";
151 template<class Scalar>
153 template<class Scalar>
155 
156 template<class Scalar>
158  :timer_(""),headerPrinted_(false)
159 {}
160 
161 template<class Scalar>
163  const RCP<ModelEvaluator<Scalar> > &thyraModel
164  ,const RCP<std::ostream> &tableOut
165  )
166  :timer_(""),headerPrinted_(false), supports_f_(false), supports_W_(false)
167 {
168  initialize(thyraModel,tableOut);
169 }
170 
171 template<class Scalar>
173  const RCP<ModelEvaluator<Scalar> > &thyraModel
174  ,const RCP<std::ostream> &tableOut
175  )
176 {
177  TEUCHOS_TEST_FOR_EXCEPT( tableOut.get()==NULL );
179  tableOut_ = tableOut;
180  timer_.start(true);
181  headerPrinted_ = false;
182 }
183 
184 
185 // Public functions overridden from Teuchos::Describable
186 
187 
188 template<class Scalar>
190 {
192  thyraModel = this->getUnderlyingModel();
193  std::ostringstream oss;
194  oss << "Thyra::DefaultEvaluationLoggerModelEvaluator{";
195  oss << "thyraModel=";
196  if(thyraModel.get())
197  oss << "\'"<<thyraModel->description()<<"\'";
198  else
199  oss << "NULL";
200  oss << "}";
201  return oss.str();
202 }
203 
204 
205 // Private functions overridden from ModelEvaulatorDefaultBase
206 
207 
208 template<class Scalar>
212  ) const
213 {
214 
215  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
216  "Thyra::DefaultEvaluationLoggerModelEvaluator",inArgs,outArgs
217  );
218 
219  thyraModel->evalModel(inArgs,outArgs);
220 
221  if(!headerPrinted_) {
222  printHeader(outArgs);
223  headerPrinted_ = true;
224  }
225  printLine(outArgs);
226 
227  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
228 
229 }
230 
231 
232 // private
233 
234 
235 template<class Scalar>
236 void DefaultEvaluationLoggerModelEvaluator<Scalar>::printHeader(
237  const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
238  ) const
239 {
240 
241  using std::setw;
242  using std::setprecision;
243  using std::right;
244  using std::left;
245  typedef ModelEvaluatorBase MEB;
246 
247  supports_f_ = outArgs.supports(MEB::OUT_ARG_f);
248  supports_W_ = outArgs.supports(MEB::OUT_ARG_W);
249 
250  const int Ng = outArgs.Ng();
251 
252  *tableOut_
253  << "\n***"
254  << "\n*** Table of function evaluations vs. CPU time"
255  << "\n***\n";
256 
257  *tableOut_
258  << "\nModel Evaluator Description:\n" << Teuchos::describe(*this,Teuchos::VERB_LOW);
259 
260  *tableOut_ << "\n";
261  *tableOut_ << " " << left << setw(flt_width_) << "time(s)";
262  for( int j = 0; j < Ng; ++j ) {
263  std::ostringstream oss;
264  oss << "||g("<<j<<")||";
265  *tableOut_ << " " << left << setw(flt_width_) << oss.str();
266  }
267  if(supports_f_)
268  *tableOut_ << " " << left << setw(flt_width_) << "||f||";
269  if(supports_W_)
270  *tableOut_ << " " << left << setw(int_width_) << "Calc W";
271  *tableOut_ << "\n";
272 
273  *tableOut_ << " " << left << setw(flt_width_) << flt_line_; // time(s)
274  for( int j = 0; j < Ng; ++j )
275  *tableOut_ << " " << left << setw(flt_width_) << flt_line_; // ||g(j)||
276  if(supports_f_)
277  *tableOut_ << " " << left << setw(flt_width_) << flt_line_; // ||f||
278  if(supports_W_)
279  *tableOut_ << " " << left << setw(int_width_) << int_line_; // Calc W
280  *tableOut_ << "\n";
281 
282 }
283 
284 template<class Scalar>
285 void DefaultEvaluationLoggerModelEvaluator<Scalar>::printLine(
286  const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
287  ) const
288 {
289 
290  using std::right;
291  using std::left;
292  using std::setprecision;
293  using std::setw;
294 
295  const int Ng = outArgs.Ng();
296 
297  RCP<const VectorBase<Scalar> > f, g_j;
298 
299  *tableOut_ << " " << setprecision(flt_prec_) << right << setw(flt_width_) << timer_.totalElapsedTime(true);
300  for( int j = 0; j < Ng; ++j ) {
301  if((g_j=outArgs.get_g(j)).get())
302  *tableOut_ << " " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*g_j);
303  else
304  *tableOut_ << " " << right << setw(flt_width_) << "-";
305  }
306  if(supports_f_) {
307  if((f=outArgs.get_f()).get())
308  *tableOut_ << " " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*f);
309  else
310  *tableOut_ << " " << right << setw(flt_width_) << "-";
311  }
312  if(supports_W_) {
313  if(outArgs.get_W().get())
314  *tableOut_ << " " << right << setw(int_width_) << "1";
315  else
316  *tableOut_ << " " << right << setw(int_width_) << "-";
317  }
318  *tableOut_ << "\n";
319 
320 }
321 
322 } // namespace Thyra
323 
324 #endif // THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
Pure abstract base interface for evaluating a stateless &quot;model&quot; that can be mapped into a number of d...
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
This is a base class that delegetes almost all function to a wrapped model evaluator object...
void initialize(const RCP< ModelEvaluator< Scalar > > &model)
Initialize given a non-const model evaluator.
T * get() const
void initialize(const RCP< ModelEvaluator< Scalar > > &thyraModel, const RCP< std::ostream > &tableOut)
Initalize.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This class wraps any ModelEvaluator object and logs the evaluation of various functions.
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...