10 #ifndef Tempus_AdjointAuxSensitivityModelEvaluator_impl_hpp
11 #define Tempus_AdjointAuxSensitivityModelEvaluator_impl_hpp
17 #include "Thyra_DefaultBlockedLinearOp.hpp"
19 #include "Thyra_VectorStdOps.hpp"
20 #include "Thyra_MultiVectorStdOps.hpp"
24 template <
typename Scalar>
29 const Scalar& t_init,
const Scalar& t_final,
32 adjoint_model_(adjoint_model),
35 mass_matrix_is_computed_(false),
36 t_interp_(Teuchos::ScalarTraits<Scalar>::rmax())
46 if (pList != Teuchos::null) *pl = *pList;
50 p_index_ = pl->
get<
int>(
"Sensitivity Parameter Index", 0);
51 g_index_ = pl->
get<
int>(
"Response Function Index", 0);
57 "AdjointAuxSensitivityModelEvaluator currently does not support "
58 <<
"non-constant mass matrix df/dx_dot!");
66 Array<RCP<const VectorSpaceBase<Scalar> > > x_spaces(2);
67 Array<RCP<const VectorSpaceBase<Scalar> > > f_spaces(2);
76 MEB::InArgs<Scalar> me_inArgs =
model_->createInArgs();
79 MEB::InArgsSetup<Scalar> inArgs;
80 inArgs.setModelEvalDescription(this->
description());
81 inArgs.setSupports(MEB::IN_ARG_x);
82 inArgs.setSupports(MEB::IN_ARG_t);
83 if (me_inArgs.supports(MEB::IN_ARG_x_dot))
84 inArgs.setSupports(MEB::IN_ARG_x_dot);
85 inArgs.setSupports(MEB::IN_ARG_alpha);
86 inArgs.setSupports(MEB::IN_ARG_beta);
89 inArgs.set_Np(me_inArgs.Np());
92 MEB::OutArgs<Scalar> me_outArgs =
model_->createOutArgs();
93 MEB::OutArgs<Scalar> adj_me_outArgs =
adjoint_model_->createOutArgs();
94 MEB::OutArgsSetup<Scalar> outArgs;
95 outArgs.setModelEvalDescription(this->
description());
96 outArgs.set_Np_Ng(me_inArgs.Np(), 0);
97 outArgs.setSupports(MEB::OUT_ARG_f);
98 outArgs.setSupports(MEB::OUT_ARG_W_op);
105 if (me_inArgs.supports(MEB::IN_ARG_x_dot)) {
111 template <
typename Scalar>
113 const Scalar t_final)
118 template <
typename Scalar>
124 forward_state_ = Teuchos::null;
127 template <
typename Scalar>
132 return model_->get_p_space(p);
135 template <
typename Scalar>
140 return model_->get_p_names(p);
143 template <
typename Scalar>
147 return x_prod_space_;
150 template <
typename Scalar>
154 return f_prod_space_;
157 template <
typename Scalar>
164 RCP<LinearOpBase<Scalar> > adjoint_op = adjoint_model_->create_W_op();
165 RCP<LinearOpBase<Scalar> > mv_adjoint_op =
166 Thyra::nonconstMultiVectorLinearOp(adjoint_op, num_adjoint_);
167 RCP<const Thyra::VectorSpaceBase<Scalar> > g_space = response_space_;
168 RCP<LinearOpBase<Scalar> > g_op = Thyra::scaledIdentity(g_space, Scalar(1.0));
169 RCP<LinearOpBase<Scalar> > null_op;
170 return nonconstBlock2x2(mv_adjoint_op, null_op, null_op, g_op);
173 template <
typename Scalar>
178 using Teuchos::rcp_dynamic_cast;
181 RCP<const LOWSFB> alowsfb = adjoint_model_->get_W_factory();
182 if (alowsfb == Teuchos::null)
183 return Teuchos::null;
185 RCP<const LOWSFB> mv_alowsfb = Thyra::multiVectorLinearOpWithSolveFactory(
186 alowsfb, residual_space_, adjoint_space_);
188 RCP<const Thyra::VectorSpaceBase<Scalar> > g_space = response_space_;
189 RCP<const LOWSFB> g_lowsfb =
190 Thyra::scaledIdentitySolveFactory(g_space, Scalar(1.0));
193 lowsfbs[0] = mv_alowsfb;
194 lowsfbs[1] = g_lowsfb;
195 return Thyra::blockedTriangularLinearOpWithSolveFactory(lowsfbs);
198 template <
typename Scalar>
202 return prototypeInArgs_;
205 template <
typename Scalar>
211 using Teuchos::rcp_dynamic_cast;
213 MEB::InArgs<Scalar> me_nominal = model_->getNominalValues();
214 MEB::InArgs<Scalar> nominal = this->createInArgs();
219 RCP<Thyra::VectorBase<Scalar> > x = Thyra::createMember(*x_prod_space_);
220 Thyra::assign(x.ptr(), zero);
223 if (me_nominal.supports(MEB::IN_ARG_x_dot)) {
224 RCP<Thyra::VectorBase<Scalar> > x_dot = Thyra::createMember(*x_prod_space_);
225 Thyra::assign(x_dot.ptr(), zero);
226 nominal.set_x_dot(x_dot);
229 const int np = model_->Np();
230 for (
int i = 0; i < np; ++i) nominal.set_p(i, me_nominal.get_p(i));
235 template <
typename Scalar>
239 return prototypeOutArgs_;
242 template <
typename Scalar>
249 using Teuchos::rcp_dynamic_cast;
259 const Scalar t = inArgs.
get_t();
260 const Scalar forward_t = t_final_ + t_init_ - t;
261 if (forward_state_ == Teuchos::null || t_interp_ != t) {
262 if (forward_state_ == Teuchos::null)
263 forward_state_ = sh_->interpolateState(forward_t);
265 sh_->interpolateState(forward_t, forward_state_.get());
270 MEB::InArgs<Scalar> me_inArgs = model_->getNominalValues();
271 me_inArgs.set_x(forward_state_->getX());
272 if (me_inArgs.supports(MEB::IN_ARG_x_dot) &&
274 me_inArgs.set_x_dot(forward_state_->getXDot());
275 if (me_inArgs.supports(MEB::IN_ARG_t)) me_inArgs.set_t(forward_t);
276 const int np = me_inArgs.Np();
277 for (
int i = 0; i < np; ++i) me_inArgs.set_p(i, inArgs.
get_p(i));
280 RCP<Thyra::LinearOpBase<Scalar> > op = outArgs.
get_W_op();
281 if (op != Teuchos::null) {
282 if (me_inArgs.supports(MEB::IN_ARG_alpha))
284 if (me_inArgs.supports(MEB::IN_ARG_beta))
285 me_inArgs.set_beta(inArgs.
get_beta());
288 RCP<Thyra::DefaultBlockedLinearOp<Scalar> > block_op =
290 RCP<Thyra::MultiVectorLinearOp<Scalar> > mv_adjoint_op =
292 block_op->getNonconstBlock(0, 0),
true);
293 RCP<Thyra::LinearOpBase<Scalar> > adjoint_op =
295 MEB::OutArgs<Scalar> adj_me_outArgs = adjoint_model_->createOutArgs();
296 adj_me_outArgs.set_W_op(adjoint_op);
297 adjoint_model_->evalModel(me_inArgs, adj_me_outArgs);
300 RCP<Thyra::ScaledIdentityLinearOpWithSolve<Scalar> > si_op =
302 block_op->getNonconstBlock(1, 1),
true);
311 RCP<Thyra::VectorBase<Scalar> > f = outArgs.
get_f();
312 if (f != Teuchos::null) {
313 RCP<const Thyra::VectorBase<Scalar> > x = inArgs.
get_x().assert_not_null();
314 RCP<const DPV> prod_x = rcp_dynamic_cast<
const DPV>(x,
true);
315 RCP<const Thyra::VectorBase<Scalar> > adjoint_x = prod_x->
getVectorBlock(0);
316 RCP<const Thyra::MultiVectorBase<Scalar> > adjoint_x_mv =
317 rcp_dynamic_cast<
const DMVPV>(adjoint_x,
true)->getMultiVector();
319 RCP<DPV> prod_f = rcp_dynamic_cast<
DPV>(f,
true);
320 RCP<Thyra::VectorBase<Scalar> > adjoint_f =
322 RCP<Thyra::MultiVectorBase<Scalar> > adjoint_f_mv =
323 rcp_dynamic_cast<DMVPV>(adjoint_f,
true)->getNonconstMultiVector();
325 MEB::OutArgs<Scalar> adj_me_outArgs = adjoint_model_->createOutArgs();
327 if (my_dfdx_ == Teuchos::null) my_dfdx_ = adjoint_model_->create_W_op();
328 adj_me_outArgs.set_W_op(my_dfdx_);
329 if (me_inArgs.supports(MEB::IN_ARG_alpha)) me_inArgs.set_alpha(0.0);
330 if (me_inArgs.supports(MEB::IN_ARG_beta)) me_inArgs.set_beta(1.0);
331 adjoint_model_->evalModel(me_inArgs, adj_me_outArgs);
334 my_dfdx_->apply(
Thyra::NOTRANS, *adjoint_x_mv, adjoint_f_mv.ptr(),
335 Scalar(-1.0), Scalar(0.0));
339 RCP<const DPV> prod_x_dot;
340 if (me_inArgs.supports(MEB::IN_ARG_x_dot)) {
341 RCP<const Thyra::VectorBase<Scalar> > x_dot = inArgs.
get_x_dot();
342 if (x_dot != Teuchos::null) {
343 prod_x_dot = rcp_dynamic_cast<
const DPV>(x_dot,
true);
344 RCP<const Thyra::VectorBase<Scalar> > adjoint_x_dot =
346 RCP<const Thyra::MultiVectorBase<Scalar> > adjoint_x_dot_mv =
347 rcp_dynamic_cast<
const DMVPV>(adjoint_x_dot,
true)
349 if (mass_matrix_is_identity_) {
351 Thyra::V_StVpV(adjoint_f_mv.ptr(), Scalar(-1.0), *adjoint_f_mv,
355 if (my_dfdxdot_ == Teuchos::null)
356 my_dfdxdot_ = adjoint_model_->create_W_op();
357 if (!mass_matrix_is_constant_ || !mass_matrix_is_computed_) {
358 adj_me_outArgs.set_W_op(my_dfdxdot_);
359 me_inArgs.set_alpha(1.0);
360 me_inArgs.set_beta(0.0);
361 adjoint_model_->evalModel(me_inArgs, adj_me_outArgs);
363 mass_matrix_is_computed_ =
true;
366 adjoint_f_mv.ptr(), Scalar(1.0), Scalar(-1.0));
373 RCP<Thyra::VectorBase<Scalar> > adjoint_g =
374 prod_f->getNonconstVectorBlock(1);
375 RCP<Thyra::MultiVectorBase<Scalar> > adjoint_g_mv =
376 rcp_dynamic_cast<DMVPV>(adjoint_g,
true)->getNonconstMultiVector();
378 MEB::OutArgs<Scalar> me_outArgs2 = model_->createOutArgs();
379 MEB::DerivativeSupport dfdp_support =
380 me_outArgs2.supports(MEB::OUT_ARG_DfDp, p_index_);
382 if (dfdp_support.supports(MEB::DERIV_LINEAR_OP)) {
383 if (my_dfdp_op_ == Teuchos::null)
384 my_dfdp_op_ = model_->create_DfDp_op(p_index_);
385 me_outArgs2.set_DfDp(p_index_, MEB::Derivative<Scalar>(my_dfdp_op_));
388 else if (dfdp_support.supports(MEB::DERIV_MV_JACOBIAN_FORM)) {
389 if (my_dfdp_mv_ == Teuchos::null)
390 my_dfdp_mv_ = createMembers(model_->get_f_space(),
391 model_->get_p_space(p_index_)->dim());
392 me_outArgs2.set_DfDp(
394 MEB::Derivative<Scalar>(my_dfdp_mv_, MEB::DERIV_MV_JACOBIAN_FORM));
395 my_dfdp_op_ = my_dfdp_mv_;
398 else if (dfdp_support.supports(MEB::DERIV_MV_GRADIENT_FORM)) {
399 if (my_dfdp_mv_ == Teuchos::null)
400 my_dfdp_mv_ = createMembers(model_->get_p_space(p_index_),
401 model_->get_f_space()->dim());
402 me_outArgs2.set_DfDp(
404 MEB::Derivative<Scalar>(my_dfdp_mv_, MEB::DERIV_MV_GRADIENT_FORM));
405 my_dfdp_op_ = my_dfdp_mv_;
410 "Invalid df/dp support");
411 model_->evalModel(me_inArgs, me_outArgs2);
412 my_dfdp_op_->apply(trans, *adjoint_x_mv, adjoint_g_mv.ptr(), Scalar(1.0),
415 if (prod_x_dot != Teuchos::null) {
416 RCP<const Thyra::VectorBase<Scalar> > z_dot =
417 prod_x_dot->getVectorBlock(1);
418 RCP<const Thyra::MultiVectorBase<Scalar> > z_dot_mv =
419 rcp_dynamic_cast<
const DMVPV>(z_dot,
true)->getMultiVector();
420 Thyra::V_VmV(adjoint_g_mv.ptr(), *z_dot_mv, *adjoint_g_mv);
425 template <
class Scalar>
430 pl->
set<
int>(
"Sensitivity Parameter Index", 0);
431 pl->
set<
int>(
"Response Function Index", 0);
432 pl->
set<
bool>(
"Mass Matrix Is Constant",
true);
433 pl->
set<
bool>(
"Mass Matrix Is Identity",
false);
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > adjoint_model_
Implicit concrete LinearOpBase subclass that takes a flattended out multi-vector and performs a multi...
void setScale(const Scalar &s)
RCP< const VectorBase< Scalar > > get_x_dot() const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
static Teuchos::RCP< const Teuchos::ParameterList > getValidParameters()
T & get(const std::string &name, T def_value)
RCP< const VectorBase< Scalar > > getVectorBlock(const int k) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
bool mass_matrix_is_constant_
RCP< LinearOpBase< Scalar > > getNonconstLinearOp()
Evaluation< VectorBase< Scalar > > get_f() const
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
static magnitudeType rmax()
bool mass_matrix_is_identity_
Teuchos::RCP< const DPVS > f_prod_space_
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int p) const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
virtual std::string description() const
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > prototypeInArgs_
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void setForwardSolutionHistory(const Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > &sh)
Set solution history from forward evaluation.
RCP< VectorBase< Scalar > > getNonconstVectorBlock(const int k)
Implicit concrete LinearOpBase subclass that takes a flattended out multi-vector and performs a multi...
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int p) const
Teuchos::RCP< const DPVS > x_prod_space_
Teuchos::RCP< const DMVPVS > residual_space_
AdjointAuxSensitivityModelEvaluator(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &adjoint_model, const Scalar &t_init, const Scalar &t_final, const Teuchos::RCP< const Teuchos::ParameterList > &pList=Teuchos::null)
Constructor.
Teuchos::RCP< const DMVPVS > response_space_
Thyra::ModelEvaluatorBase::OutArgs< Scalar > prototypeOutArgs_
Teuchos::RCP< const DMVPVS > adjoint_space_
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > model_
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_x() const
void setFinalTime(const Scalar t_final)
Set the final time from the forward evaluation.
RCP< const VectorBase< Scalar > > get_p(int l) const