Thyra Version of the Day
Loading...
Searching...
No Matches
Thyra_DefaultEvaluationLoggerModelEvaluator.hpp
1// @HEADER
2// *****************************************************************************
3// Thyra: Interfaces and Support for Abstract Numerical Algorithms
4//
5// Copyright 2004 NTESS and the Thyra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
11#define THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
12
13#include "Thyra_ModelEvaluatorDelegatorBase.hpp"
14#include "Thyra_LinearOpWithSolveFactoryBase.hpp"
15#include "Teuchos_Time.hpp"
16
17namespace Thyra {
18
19
27template<class Scalar>
29 : virtual public ModelEvaluatorDelegatorBase<Scalar>
30{
31public:
32
35
38
41 const RCP<ModelEvaluator<Scalar> > &thyraModel
42 ,const RCP<std::ostream> &tableOut
43 );
44
57 void initialize(
58 const RCP<ModelEvaluator<Scalar> > &thyraModel
59 ,const RCP<std::ostream> &tableOut
60 );
61
63
66
68 std::string description() const;
69
71
72private:
73
76
78 void evalModelImpl(
81 ) const;
82
84
85private:
86
87 RCP<std::ostream> tableOut_;
88 Teuchos::Time timer_;
89
90 mutable bool headerPrinted_;
91 mutable bool supports_f_;
92 mutable bool supports_W_;
93
94 static const int flt_width_;
95 static const int flt_sciPrec_;
96 static const int flt_prec_;
97 static const char flt_line_[];
98 static const int int_width_;
99 static const char int_line_[];
100
101 void printHeader( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
102 void printLine( const ModelEvaluatorBase::OutArgs<Scalar> &outArgs ) const;
103
104};
105
106// /////////////////////////////////
107// Implementations
108
109// Constructors/initializers/accessors/utilities
110
111template<class Scalar>
113template<class Scalar>
115template<class Scalar>
117template<class Scalar>
118const char DefaultEvaluationLoggerModelEvaluator<Scalar>::flt_line_[] = "-------------------------";
119template<class Scalar>
121template<class Scalar>
123
124template<class Scalar>
128
129template<class Scalar>
131 const RCP<ModelEvaluator<Scalar> > &thyraModel
132 ,const RCP<std::ostream> &tableOut
133 )
134 :timer_(""),headerPrinted_(false), supports_f_(false), supports_W_(false)
135{
136 initialize(thyraModel,tableOut);
137}
138
139template<class Scalar>
141 const RCP<ModelEvaluator<Scalar> > &thyraModel
142 ,const RCP<std::ostream> &tableOut
143 )
144{
145 TEUCHOS_TEST_FOR_EXCEPT( tableOut.get()==NULL );
147 tableOut_ = tableOut;
148 timer_.start(true);
149 headerPrinted_ = false;
150}
151
152
153// Public functions overridden from Teuchos::Describable
154
155
156template<class Scalar>
158{
160 thyraModel = this->getUnderlyingModel();
161 std::ostringstream oss;
162 oss << "Thyra::DefaultEvaluationLoggerModelEvaluator{";
163 oss << "thyraModel=";
164 if(thyraModel.get())
165 oss << "\'"<<thyraModel->description()<<"\'";
166 else
167 oss << "NULL";
168 oss << "}";
169 return oss.str();
170}
171
172
173// Private functions overridden from ModelEvaulatorDefaultBase
174
175
176template<class Scalar>
180 ) const
181{
182
183 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
184 "Thyra::DefaultEvaluationLoggerModelEvaluator",inArgs,outArgs
185 );
186
187 thyraModel->evalModel(inArgs,outArgs);
188
189 if(!headerPrinted_) {
190 printHeader(outArgs);
191 headerPrinted_ = true;
192 }
193 printLine(outArgs);
194
195 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
196
197}
198
199
200// private
201
202
203template<class Scalar>
204void DefaultEvaluationLoggerModelEvaluator<Scalar>::printHeader(
205 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
206 ) const
207{
208
209 using std::setw;
210 using std::setprecision;
211 using std::right;
212 using std::left;
213 typedef ModelEvaluatorBase MEB;
214
215 supports_f_ = outArgs.supports(MEB::OUT_ARG_f);
216 supports_W_ = outArgs.supports(MEB::OUT_ARG_W);
217
218 const int Ng = outArgs.Ng();
219
220 *tableOut_
221 << "\n***"
222 << "\n*** Table of function evaluations vs. CPU time"
223 << "\n***\n";
224
225 *tableOut_
226 << "\nModel Evaluator Description:\n" << Teuchos::describe(*this,Teuchos::VERB_LOW);
227
228 *tableOut_ << "\n";
229 *tableOut_ << " " << left << setw(flt_width_) << "time(s)";
230 for( int j = 0; j < Ng; ++j ) {
231 std::ostringstream oss;
232 oss << "||g("<<j<<")||";
233 *tableOut_ << " " << left << setw(flt_width_) << oss.str();
234 }
235 if(supports_f_)
236 *tableOut_ << " " << left << setw(flt_width_) << "||f||";
237 if(supports_W_)
238 *tableOut_ << " " << left << setw(int_width_) << "Calc W";
239 *tableOut_ << "\n";
240
241 *tableOut_ << " " << left << setw(flt_width_) << flt_line_; // time(s)
242 for( int j = 0; j < Ng; ++j )
243 *tableOut_ << " " << left << setw(flt_width_) << flt_line_; // ||g(j)||
244 if(supports_f_)
245 *tableOut_ << " " << left << setw(flt_width_) << flt_line_; // ||f||
246 if(supports_W_)
247 *tableOut_ << " " << left << setw(int_width_) << int_line_; // Calc W
248 *tableOut_ << "\n";
249
250}
251
252template<class Scalar>
253void DefaultEvaluationLoggerModelEvaluator<Scalar>::printLine(
254 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
255 ) const
256{
257
258 using std::right;
259 using std::left;
260 using std::setprecision;
261 using std::setw;
262
263 const int Ng = outArgs.Ng();
264
265 RCP<const VectorBase<Scalar> > f, g_j;
266
267 *tableOut_ << " " << setprecision(flt_prec_) << right << setw(flt_width_) << timer_.totalElapsedTime(true);
268 for( int j = 0; j < Ng; ++j ) {
269 if((g_j=outArgs.get_g(j)).get())
270 *tableOut_ << " " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*g_j);
271 else
272 *tableOut_ << " " << right << setw(flt_width_) << "-";
273 }
274 if(supports_f_) {
275 if((f=outArgs.get_f()).get())
276 *tableOut_ << " " << setprecision(flt_sciPrec_) << right << setw(flt_width_) << norm(*f);
277 else
278 *tableOut_ << " " << right << setw(flt_width_) << "-";
279 }
280 if(supports_W_) {
281 if(outArgs.get_W().get())
282 *tableOut_ << " " << right << setw(int_width_) << "1";
283 else
284 *tableOut_ << " " << right << setw(int_width_) << "-";
285 }
286 *tableOut_ << "\n";
287
288}
289
290} // namespace Thyra
291
292#endif // THYRA_DEFAULT_EVALUATION_LOGGER_MODEL_EVALUATOR_HPP
T * get() const
This class wraps any ModelEvaluator object and logs the evaluation of various functions.
void initialize(const RCP< ModelEvaluator< Scalar > > &thyraModel, const RCP< std::ostream > &tableOut)
Initalize.
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object.
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.
Pure abstract base interface for evaluating a stateless "model" that can be mapped into a number of d...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)