29 #ifndef RYTHMOS_FORWARD_RESPONSE_SENSITIVITY_COMPUTER_HPP
30 #define RYTHMOS_FORWARD_RESPONSE_SENSITIVITY_COMPUTER_HPP
33 #include "Rythmos_Types.hpp"
34 #include "Thyra_ModelEvaluator.hpp"
35 #include "Teuchos_VerboseObject.hpp"
36 #include "Teuchos_StandardMemberCompositionMacros.hpp"
48 template<
class Scalar>
50 :
public Teuchos::VerboseObject<ForwardResponseSensitivityComputer<Scalar> >
77 const RCP<
const Thyra::ModelEvaluator<Scalar> > &responseFunc,
78 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
88 const RCP<
const Thyra::ModelEvaluator<Scalar> > &responseFunc,
89 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint
93 const RCP<Thyra::VectorBase<Scalar> >
create_g_hat()
const;
111 const Thyra::VectorBase<Scalar> *x_dot,
112 const Thyra::VectorBase<Scalar> &x,
114 Thyra::VectorBase<Scalar> *g_hat
137 const Thyra::VectorBase<Scalar> *x_dot,
138 const Thyra::MultiVectorBase<Scalar> *S_dot,
139 const Thyra::VectorBase<Scalar> &x,
140 const Thyra::MultiVectorBase<Scalar> &S,
142 Thyra::VectorBase<Scalar> *g_hat,
143 Thyra::MultiVectorBase<Scalar> *D_g_hat_D_p
148 RCP<const Thyra::ModelEvaluator<Scalar> > responseFunc_;
149 Thyra::ModelEvaluatorBase::InArgs<Scalar> basePoint_;
153 RCP<const Thyra::VectorSpaceBase<Scalar> > p_space_;
154 RCP<const Thyra::VectorSpaceBase<Scalar> > g_space_;
156 bool response_func_supports_x_dot_;
157 bool response_func_supports_D_x_dot_;
158 bool response_func_supports_D_p_;
160 mutable Thyra::ModelEvaluatorBase::InArgs<Scalar> responseInArgs_;
161 mutable Thyra::ModelEvaluatorBase::OutArgs<Scalar> responseOutArgs_;
163 mutable RCP<Thyra::LinearOpBase<Scalar> > D_g_D_x_dot_;
164 mutable RCP<Thyra::LinearOpBase<Scalar> > D_g_D_x_;
165 mutable RCP<Thyra::MultiVectorBase<Scalar> > D_g_D_p_;
171 void createCache(
const bool computeSens)
const;
173 void computeResponseAndSensitivityImpl(
174 const Thyra::VectorBase<Scalar> *x_dot,
175 const Thyra::MultiVectorBase<Scalar> *S_dot,
176 const Thyra::VectorBase<Scalar> &x,
177 const Thyra::MultiVectorBase<Scalar> *S,
179 Thyra::VectorBase<Scalar> *g_hat,
180 Thyra::MultiVectorBase<Scalar> *D_g_hat_D_p
191 template<
class Scalar>
193 :dumpSensitivities_(false),
196 response_func_supports_x_dot_(false),
197 response_func_supports_D_x_dot_(false),
198 response_func_supports_D_p_(false)
202 template<
class Scalar>
204 const RCP<
const Thyra::ModelEvaluator<Scalar> > &responseFunc,
205 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
211 typedef Thyra::ModelEvaluatorBase MEB;
215 responseFunc_ = responseFunc;
216 basePoint_ = basePoint;
220 p_space_ = responseFunc_->get_p_space(p_index_);
221 g_space_ = responseFunc_->get_g_space(g_index_);
225 responseInArgs = responseFunc_->createInArgs();
226 response_func_supports_x_dot_ =
227 responseInArgs.supports(MEB::IN_ARG_x_dot);
229 responseOutArgs = responseFunc_->createOutArgs();
230 response_func_supports_D_x_dot_ =
231 !responseOutArgs.supports(MEB::OUT_ARG_DgDx_dot,g_index_).none();
232 response_func_supports_D_p_ =
233 !responseOutArgs.supports(MEB::OUT_ARG_DgDp,g_index_,p_index_).none();
240 template<
class Scalar>
242 const RCP<
const Thyra::ModelEvaluator<Scalar> > &responseFunc,
243 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint
248 responseFunc_ = responseFunc;
249 basePoint_ = basePoint;
253 template<
class Scalar>
254 const RCP<Thyra::VectorBase<Scalar> >
257 return Thyra::createMember(g_space_);
261 template<
class Scalar>
262 const RCP<Thyra::MultiVectorBase<Scalar> >
265 return Thyra::createMembers(g_space_,p_space_->dim());
269 template<
class Scalar>
271 const Thyra::VectorBase<Scalar> *x_dot,
272 const Thyra::VectorBase<Scalar> &x,
274 Thyra::VectorBase<Scalar> *g_hat
277 computeResponseAndSensitivityImpl(x_dot,0,x,0,t,g_hat,0);
281 template<
class Scalar>
283 const Thyra::VectorBase<Scalar> *x_dot,
284 const Thyra::MultiVectorBase<Scalar> *S_dot,
285 const Thyra::VectorBase<Scalar> &x,
286 const Thyra::MultiVectorBase<Scalar> &S,
288 Thyra::VectorBase<Scalar> *g_hat,
289 Thyra::MultiVectorBase<Scalar> *D_g_hat_D_p
292 computeResponseAndSensitivityImpl(x_dot,S_dot,x,&S,t,g_hat,D_g_hat_D_p);
299 template<
class Scalar>
302 D_g_D_x_dot_ = Teuchos::null;
303 D_g_D_x_ = Teuchos::null;
304 D_g_D_p_ = Teuchos::null;
308 template<
class Scalar>
309 void ForwardResponseSensitivityComputer<Scalar>::createCache(
310 const bool computeSens
314 if (response_func_supports_D_x_dot_ && is_null(D_g_D_x_dot_))
315 D_g_D_x_dot_ = responseFunc_->create_DgDx_dot_op(g_index_);
316 D_g_D_x_ = responseFunc_->create_DgDx_op(g_index_);
317 if (response_func_supports_D_p_ && is_null(D_g_D_p_))
318 D_g_D_p_ = createMembers(g_space_,p_space_->dim());
323 template<
class Scalar>
324 void ForwardResponseSensitivityComputer<Scalar>::computeResponseAndSensitivityImpl(
325 const Thyra::VectorBase<Scalar> *x_dot,
326 const Thyra::MultiVectorBase<Scalar> *S_dot,
327 const Thyra::VectorBase<Scalar> &x,
328 const Thyra::MultiVectorBase<Scalar> *S,
330 Thyra::VectorBase<Scalar> *g_hat,
331 Thyra::MultiVectorBase<Scalar> *D_g_hat_D_p
337 using Teuchos::includesVerbLevel;
338 typedef ScalarTraits<Scalar> ST;
341 typedef Thyra::ModelEvaluatorBase MEB;
347 const RCP<Teuchos::FancyOStream> out = this->getOStream();
348 const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
351 out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW);
352 const bool print_norms =
353 out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM);
355 out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_EXTREME);
361 const bool computeSens = ( D_g_hat_D_p != 0 );
362 createCache(computeSens);
373 *out <<
"\nEvaluating response function at time t = " << t <<
" ...\n";
377 MEB::InArgs<Scalar> responseInArgs = responseFunc_->createInArgs();
378 responseInArgs.setArgs(basePoint_);
379 responseInArgs.set_x(rcp(&x,
false));
380 if (response_func_supports_x_dot_)
381 responseInArgs.set_x_dot(rcp(x_dot,
false));
385 MEB::OutArgs<Scalar> responseOutArgs = responseFunc_->createOutArgs();
388 responseOutArgs.set_g(g_index_,rcp(g_hat,
false));
393 if (response_func_supports_D_x_dot_) {
394 responseOutArgs.set_DgDx_dot(
396 MEB::Derivative<Scalar>(D_g_D_x_dot_)
401 responseOutArgs.set_DgDx(
403 MEB::Derivative<Scalar>(D_g_D_x_)
407 if (response_func_supports_D_p_) {
408 responseOutArgs.set_DgDp(
410 MEB::Derivative<Scalar>(D_g_D_p_,MEB::DERIV_MV_BY_COL)
419 RYTHMOS_FUNC_TIME_MONITOR(
"Rythmos:ForwardResponseSensitivityComputer::evalModel: evalResponse");
420 responseFunc_->evalModel( responseInArgs, responseOutArgs );
427 *out <<
"\n||g_hat||inf = " << norm_inf(*g_hat) << std::endl;
428 if (computeSens && !is_null(D_g_D_p_))
429 *out <<
"\n||D_g_D_p_||inf = " << norms_inf(*D_g_D_p_) << std::endl;
432 if ( g_hat && (dumpSensitivities_ || print_x) )
433 *out <<
"\ng_hat = " << *g_hat;
435 if (computeSens && print_x) {
436 if (!is_null(D_g_D_x_dot_))
437 *out <<
"\nD_g_D_x_dot = " << *D_g_D_x_ << std::endl;
438 if (!is_null(D_g_D_x_))
439 *out <<
"\nD_g_D_x = " << *D_g_D_x_ << std::endl;
440 if (!is_null(D_g_D_p_))
441 *out <<
"\nD_g_D_p = " << *D_g_D_p_ << std::endl;
452 RYTHMOS_FUNC_TIME_MONITOR(
"Rythmos:ForwardResponseSensitivityComputer::evalModel: computeSens");
455 *out <<
"\nD_g_hat_D_p = DgDx_dot * S_dot + DgDx * S + DgDp ...\n";
457 assign( ptr(D_g_hat_D_p), ST::zero() );
460 if (response_func_supports_D_x_dot_) {
461 apply( *D_g_D_x_dot_, Thyra::NOTRANS, *S_dot,
462 ptr(D_g_hat_D_p), ST::one(), ST::one() );
466 apply( *D_g_D_x_, Thyra::NOTRANS, *S,
467 ptr(D_g_hat_D_p), ST::one(), ST::one() );
470 if (response_func_supports_D_p_) {
471 Vp_V( ptr(D_g_hat_D_p), *D_g_D_p_ );
474 if (dumpSensitivities_ || print_x)
475 *out <<
"\nD_g_hat_D_p = "
476 << Teuchos::describe(*D_g_hat_D_p,Teuchos::VERB_EXTREME);
486 #endif // RYTHMOS_FORWARD_RESPONSE_SENSITIVITY_COMPUTER_HPP
void setResponseFunction(const RCP< const Thyra::ModelEvaluator< Scalar > > &responseFunc, const Thyra::ModelEvaluatorBase::InArgs< Scalar > &basePoint, const int p_index, const int g_index)
Set the response function for the first time.
void resetResponseFunction(const RCP< const Thyra::ModelEvaluator< Scalar > > &responseFunc, const Thyra::ModelEvaluatorBase::InArgs< Scalar > &basePoint)
Reset the point-specific response function along with its base point.
void computeResponseAndSensitivity(const Thyra::VectorBase< Scalar > *x_dot, const Thyra::MultiVectorBase< Scalar > *S_dot, const Thyra::VectorBase< Scalar > &x, const Thyra::MultiVectorBase< Scalar > &S, const Scalar t, Thyra::VectorBase< Scalar > *g_hat, Thyra::MultiVectorBase< Scalar > *D_g_hat_D_p) const
Compute the reduced sensitivity and perhaps the response itself at a point (xdot,x,t).
const RCP< Thyra::VectorBase< Scalar > > create_g_hat() const
const RCP< Thyra::MultiVectorBase< Scalar > > create_D_g_hat_D_p() const
STANDARD_MEMBER_COMPOSITION_MEMBERS(bool, dumpSensitivities)
Concrete utility class for computing (assembling) forward transient response sensitivities.
void computeResponse(const Thyra::VectorBase< Scalar > *x_dot, const Thyra::VectorBase< Scalar > &x, const Scalar t, Thyra::VectorBase< Scalar > *g_hat) const
Compute the reduced response at a point (xdot,x,t).
ForwardResponseSensitivityComputer()