42 #ifndef THYRA_DEFAULT_FINITE_DIFFERENCE_MODEL_EVALUATOR_DEF_HPP
43 #define THYRA_DEFAULT_FINITE_DIFFERENCE_MODEL_EVALUATOR_DEF_HPP
45 #include "Thyra_DefaultFiniteDifferenceModelEvaluator_decl.hpp"
54 template<
class Scalar>
59 template<
class Scalar>
66 direcFiniteDiffCalculator_ = direcFiniteDiffCalculator_in;
73 template<
class Scalar>
77 thyraModel = this->getUnderlyingModel();
78 std::ostringstream oss;
79 oss <<
"Thyra::DefaultFiniteDifferenceModelEvaluator{";
82 oss <<
"\'"<<thyraModel->description()<<
"\'";
93 template<
class Scalar>
99 thyraModel = this->getUnderlyingModel();
100 const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
101 const int l_Np = wrappedOutArgs.Np(), l_Ng = wrappedOutArgs.Ng();
102 MEB::OutArgsSetup<Scalar> outArgs;
103 outArgs.setModelEvalDescription(this->description());
104 outArgs.set_Np_Ng(l_Np,l_Ng);
105 outArgs.setSupports(wrappedOutArgs);
106 if (wrappedOutArgs.supports(MEB::OUT_ARG_f)) {
107 for(
int l = 0; l < l_Np; ++l ) {
108 outArgs.setSupports(MEB::OUT_ARG_DfDp,l,MEB::DERIV_MV_BY_COL);
111 for(
int j = 0; j < l_Ng; ++j ) {
112 for(
int l = 0; l < l_Np; ++l ) {
113 outArgs.setSupports( MEB::OUT_ARG_DgDp , j, l, MEB::DERIV_MV_BY_COL);
121 template<
class Scalar>
122 void DefaultFiniteDifferenceModelEvaluator<Scalar>::evalModelImpl(
123 const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
124 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
128 using Teuchos::rcp_const_cast;
129 using Teuchos::rcp_dynamic_cast;
131 typedef ModelEvaluatorBase MEB;
132 namespace DFDCT = DirectionalFiniteDiffCalculatorTypes;
134 typedef RCP<VectorBase<Scalar> > V_ptr;
136 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
137 "Thyra::DefaultFiniteDifferenceModelEvaluator",inArgs,outArgs
144 const RCP<const VectorSpaceBase<Scalar> >
145 p_space = thyraModel->get_p_space(0),
146 g_space = thyraModel->get_g_space(0);
153 *out <<
"\nComputing the base point ...\n";
155 const int l_Np = outArgs.Np();
156 const int l_Ng = outArgs.Ng();
157 MEB::InArgs<Scalar> wrappedInArgs = inArgs;
158 MEB::OutArgs<Scalar> baseFunc = thyraModel->createOutArgs();
159 if( outArgs.supports(MEB::OUT_ARG_f) && outArgs.get_f().get() )
160 baseFunc.set_f(outArgs.get_f());
161 for(
int j = 0; j < l_Ng; ++j ) {
163 if( (g_j=outArgs.get_g(j)).
get() )
164 baseFunc.set_g(j,g_j);
171 thyraModel->evalModel(wrappedInArgs,baseFunc);
173 bool failed = baseFunc.isFailed();
183 Array<int> compute_DfDp;
184 Array<Array<int> > compute_DgDp(l_Ng);
185 DFDCT::SelectedDerivatives selectedDerivs;
187 for (
int l = 0; l < l_Np; ++l ) {
191 outArgs.supports(MEB::OUT_ARG_DfDp,l).none()==
false
193 outArgs.get_DfDp(l).isEmpty()==false
196 selectedDerivs.supports(MEB::OUT_ARG_DfDp,l);
197 compute_DfDp.push_back(
true);
201 compute_DfDp.push_back(
false);
205 for (
int j = 0; j < l_Ng; ++j ) {
207 outArgs.supports(MEB::OUT_ARG_DgDp,j,l).none()==
false
209 outArgs.get_DgDp(j,l).isEmpty()==false
212 selectedDerivs.supports(MEB::OUT_ARG_DgDp,j,l);
213 compute_DgDp[j].push_back(
true);
217 compute_DgDp[j].push_back(
false);
226 deriv = direcFiniteDiffCalculator_->createOutArgs(
227 *thyraModel, selectedDerivs );
229 for (
int l = 0; l < l_Np; ++l ) {
230 if ( compute_DfDp[l] )
231 deriv.set_DfDp(l,outArgs.get_DfDp(l));
232 for (
int j = 0; j < l_Ng; ++j ) {
233 if ( compute_DgDp[j][l] )
234 deriv.set_DgDp(j,l,outArgs.get_DgDp(j,l));
240 direcFiniteDiffCalculator_->calcDerivatives(
241 *thyraModel,inArgs,baseFunc,deriv
248 *out <<
"\nEvaluation failed, returning NaNs ...\n";
252 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
260 #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.