10 #ifndef THYRA_DEFAULT_FINITE_DIFFERENCE_MODEL_EVALUATOR_DEF_HPP
11 #define THYRA_DEFAULT_FINITE_DIFFERENCE_MODEL_EVALUATOR_DEF_HPP
13 #include "Thyra_DefaultFiniteDifferenceModelEvaluator_decl.hpp"
22 template<
class Scalar>
27 template<
class Scalar>
34 direcFiniteDiffCalculator_ = direcFiniteDiffCalculator_in;
41 template<
class Scalar>
45 thyraModel = this->getUnderlyingModel();
46 std::ostringstream oss;
47 oss <<
"Thyra::DefaultFiniteDifferenceModelEvaluator{";
50 oss <<
"\'"<<thyraModel->description()<<
"\'";
61 template<
class Scalar>
67 thyraModel = this->getUnderlyingModel();
68 const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
69 const int l_Np = wrappedOutArgs.Np(), l_Ng = wrappedOutArgs.Ng();
70 MEB::OutArgsSetup<Scalar> outArgs;
71 outArgs.setModelEvalDescription(this->description());
72 outArgs.set_Np_Ng(l_Np,l_Ng);
73 outArgs.setSupports(wrappedOutArgs);
74 if (wrappedOutArgs.supports(MEB::OUT_ARG_f)) {
75 for(
int l = 0; l < l_Np; ++l ) {
76 outArgs.setSupports(MEB::OUT_ARG_DfDp,l,MEB::DERIV_MV_BY_COL);
79 for(
int j = 0; j < l_Ng; ++j ) {
80 for(
int l = 0; l < l_Np; ++l ) {
81 outArgs.setSupports( MEB::OUT_ARG_DgDp , j, l, MEB::DERIV_MV_BY_COL);
89 template<
class Scalar>
90 void DefaultFiniteDifferenceModelEvaluator<Scalar>::evalModelImpl(
91 const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
92 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
96 using Teuchos::rcp_const_cast;
97 using Teuchos::rcp_dynamic_cast;
99 typedef ModelEvaluatorBase MEB;
100 namespace DFDCT = DirectionalFiniteDiffCalculatorTypes;
102 typedef RCP<VectorBase<Scalar> > V_ptr;
104 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
105 "Thyra::DefaultFiniteDifferenceModelEvaluator",inArgs,outArgs
112 const RCP<const VectorSpaceBase<Scalar> >
113 p_space = thyraModel->get_p_space(0),
114 g_space = thyraModel->get_g_space(0);
121 *out <<
"\nComputing the base point ...\n";
123 const int l_Np = outArgs.Np();
124 const int l_Ng = outArgs.Ng();
125 MEB::InArgs<Scalar> wrappedInArgs = inArgs;
126 MEB::OutArgs<Scalar> baseFunc = thyraModel->createOutArgs();
127 if( outArgs.supports(MEB::OUT_ARG_f) && outArgs.get_f().get() )
128 baseFunc.set_f(outArgs.get_f());
129 for(
int j = 0; j < l_Ng; ++j ) {
131 if( (g_j=outArgs.get_g(j)).
get() )
132 baseFunc.set_g(j,g_j);
139 thyraModel->evalModel(wrappedInArgs,baseFunc);
141 bool failed = baseFunc.isFailed();
151 Array<int> compute_DfDp;
152 Array<Array<int> > compute_DgDp(l_Ng);
153 DFDCT::SelectedDerivatives selectedDerivs;
155 for (
int l = 0; l < l_Np; ++l ) {
159 outArgs.supports(MEB::OUT_ARG_DfDp,l).none()==
false
161 outArgs.get_DfDp(l).isEmpty()==false
164 selectedDerivs.supports(MEB::OUT_ARG_DfDp,l);
165 compute_DfDp.push_back(
true);
169 compute_DfDp.push_back(
false);
173 for (
int j = 0; j < l_Ng; ++j ) {
175 outArgs.supports(MEB::OUT_ARG_DgDp,j,l).none()==
false
177 outArgs.get_DgDp(j,l).isEmpty()==false
180 selectedDerivs.supports(MEB::OUT_ARG_DgDp,j,l);
181 compute_DgDp[j].push_back(
true);
185 compute_DgDp[j].push_back(
false);
194 deriv = direcFiniteDiffCalculator_->createOutArgs(
195 *thyraModel, selectedDerivs );
197 for (
int l = 0; l < l_Np; ++l ) {
198 if ( compute_DfDp[l] )
199 deriv.set_DfDp(l,outArgs.get_DfDp(l));
200 for (
int j = 0; j < l_Ng; ++j ) {
201 if ( compute_DgDp[j][l] )
202 deriv.set_DgDp(j,l,outArgs.get_DgDp(j,l));
208 direcFiniteDiffCalculator_->calcDerivatives(
209 *thyraModel,inArgs,baseFunc,deriv
216 *out <<
"\nEvaluation failed, returning NaNs ...\n";
220 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
228 #endif // THYRA_DEFAULT_FINITE_DIFFERENCE_MODEL_EVALUATOR_DEF_HPP
Pure abstract base interface for evaluating a stateless "model" that can be mapped into a number of d...
basic_OSTab< char > OSTab
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
void initialize(const RCP< ModelEvaluator< Scalar > > &model)
Initialize given a non-const model evaluator.
This class wraps any ModelEvaluator object and computes certain derivatives using finite differences...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
std::string description() const
DefaultFiniteDifferenceModelEvaluator()
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
Base subclass for ModelEvaluator that defines some basic types.
void initialize(const RCP< ModelEvaluator< Scalar > > &thyraModel, const RCP< DirectionalFiniteDiffCalculator< Scalar > > &direcFiniteDiffCalculator)
Utility class for computing directional finite differences of a model.