Debian Codegraph @

packages/nox/test/epetra/Thyra/ModelEvaluator_LoggerDecorator.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 #ifndef NOX_MODEL_EVALUATOR_LOGGER_DECORATOR_HPP
10 #define NOX_MODEL_EVALUATOR_LOGGER_DECORATOR_HPP
11
12 namespace NOX_TEST {
13
14
15 /** \brief Model Evaluator Decorator unit testing class that logs
16 calls to evalModel() to count the number of calls made to
17 evaluate the residual and if each call is exact or approximate
18 (used in a Jacobian approximation in JFNK perturbation). This
19 is used to unit test the JFNK support in NOX.
20 */
21 template<typename ScalarT>
22 class ModelEvaluatorLoggerDecorator : public ::Thyra::ModelEvaluatorDelegatorBase<ScalarT> {
23
24 public:
25
26 ModelEvaluatorLoggerDecorator(const Teuchos::RCP< ::Thyra::ModelEvaluator<ScalarT> > &model) :
27 ::Thyra::ModelEvaluatorDelegatorBase<ScalarT>(model),
28 num_calls_exact_(0),
29 num_calls_approx_deriv_(0),
30 num_calls_very_approx_deriv_(0)
31 { }
32
33 void evalModelImpl(const ::Thyra::ModelEvaluatorBase::InArgs<ScalarT> &inArgs,
34 const ::Thyra::ModelEvaluatorBase::OutArgs<ScalarT> &outArgs) const
35 {
36 ::Thyra::ModelEvaluatorBase::InArgs<ScalarT> modelInArgs = this->getUnderlyingModel()->createInArgs();
37 modelInArgs.setArgs(inArgs,false);
38
39 ::Thyra::ModelEvaluatorBase::OutArgs<ScalarT> modelOutArgs = this->getUnderlyingModel()->createOutArgs();
40 modelOutArgs.setArgs(outArgs,false);
41
42 ::Thyra::ModelEvaluatorBase::Evaluation< ::Thyra::VectorBase<ScalarT> > eval = outArgs.get_f();
43 if (nonnull(eval)) {
44 if (eval.getType() == ::Thyra::ModelEvaluatorBase::EVAL_TYPE_EXACT) {
45 ++num_calls_exact_;
46 //std::cout << "EXACT!" << std::endl;
47 }
48 else if (eval.getType() == ::Thyra::ModelEvaluatorBase::EVAL_TYPE_APPROX_DERIV) {
49 ++num_calls_approx_deriv_;
50 //std::cout << "APPROX DERIV!" << std::endl;
51 }
52 else if (eval.getType() == ::Thyra::ModelEvaluatorBase::EVAL_TYPE_VERY_APPROX_DERIV) {
53 ++num_calls_very_approx_deriv_;
54 //std::cout << "VERY APPROX DERIV!" << std::endl;
55 }
56 }
57
58 this->getUnderlyingModel()->evalModel(modelInArgs,modelOutArgs);
59 }
60
61 int getNumExactCalls() const
62 { return num_calls_exact_; }
63
64 int getNumApproxDerivCalls() const
65 { return num_calls_approx_deriv_; }
66
67 int getNumVeryApproxDerivCalls() const
68 { return num_calls_very_approx_deriv_; }
69
70 private:
71
72 void reset()
73 {
74 num_calls_exact_ = 0;
75 num_calls_approx_deriv_ = 0;
76 num_calls_very_approx_deriv_ = 0;
77 }
78
79 mutable int num_calls_exact_;
80 mutable int num_calls_approx_deriv_;
81 mutable int num_calls_very_approx_deriv_;
82
83 };
84
85 }
86
87 #endif