9 #ifndef Tempus_ModelEvaluatorPairPartIMEX_CombinedFSA_impl_hpp
10 #define Tempus_ModelEvaluatorPairPartIMEX_CombinedFSA_impl_hpp
12 #include "Thyra_VectorStdOps.hpp"
13 #include "Thyra_MultiVectorStdOps.hpp"
17 template <
typename Scalar>
21 const Teuchos::RCP<const Teuchos::ParameterList>& pList) :
22 forwardModel_(forwardModel),
23 use_dfdp_as_tangent_(false),
28 using Thyra::multiVectorProductVectorSpace;
30 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
31 if (pList != Teuchos::null)
36 pl->remove(
"Sensitivity Y Tangent Index");
44 const int sens_param_index = pl->get<
int>(
"Sensitivity Parameter Index");
45 const int num_sens_param =
47 RCP<const Thyra::VectorSpaceBase<Scalar> > explicit_y_space =
49 RCP<const Thyra::VectorSpaceBase<Scalar> > implicit_x_space =
52 multiVectorProductVectorSpace(explicit_y_space, num_sens_param+1);
54 multiVectorProductVectorSpace(explicit_y_space, num_sens_param);
56 multiVectorProductVectorSpace(implicit_x_space, num_sens_param+1);
63 template <
typename Scalar>
69 using Teuchos::rcp_dynamic_cast;
71 this->useImplicitModel_ =
true;
72 this->wrapperImplicitInArgs_ = this->createInArgs();
73 this->wrapperImplicitOutArgs_ = this->createOutArgs();
74 this->useImplicitModel_ =
false;
76 RCP<const Thyra::VectorBase<Scalar> > z =
77 this->explicitModel_->getNominalValues().get_x();
80 TEUCHOS_TEST_FOR_EXCEPTION( !(getIMEXVector(z)->space()->isCompatible(
81 *(this->implicitModel_->get_x_space()))),
83 "Error - WrapperModelEvaluatorPairIMEX_CombinedFSA::initialize()\n"
84 " Explicit and Implicit vector x spaces are incompatible!\n"
85 " Explicit vector x space = " << *(getIMEXVector(z)->space()) <<
"\n"
86 " Implicit vector x space = " << *(this->implicitModel_->get_x_space()) <<
90 const RCP<const DMVPV> z_dmvpv = rcp_dynamic_cast<
const DMVPV>(z,
true);
91 const RCP<const Thyra::MultiVectorBase<Scalar> > z_mv =
92 z_dmvpv->getMultiVector();
93 RCP<const PMVB> zPVector = rcp_dynamic_cast<
const PMVB>(z_mv);
94 TEUCHOS_TEST_FOR_EXCEPTION( zPVector == Teuchos::null, std::logic_error,
95 "Error - WrapperModelEvaluatorPairPartIMEX_CombinedFSA::initialize()\n"
96 " was given a VectorBase that could not be cast to a\n"
97 " ProductVectorBase!\n");
99 int numBlocks = zPVector->productSpace()->numBlocks();
101 TEUCHOS_TEST_FOR_EXCEPTION( !(0 <= this->numExplicitOnlyBlocks_ and
102 this->numExplicitOnlyBlocks_ < numBlocks),
104 "Error - WrapperModelEvaluatorPairPartIMEX_CombinedFSA::initialize()\n"
105 "Invalid number of explicit-only blocks = " <<
106 this->numExplicitOnlyBlocks_ <<
"\n"
107 "Should be in the interval [0, numBlocks) = [0, " << numBlocks <<
")\n");
110 template <
typename Scalar>
111 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
115 if (this->useImplicitModel_) {
116 if (i == this->parameterIndex_)
117 return explicit_y_dydp_prod_space_;
119 return appImplicitModel_->get_p_space(i);
122 return appExplicitModel_->get_p_space(i);
125 template <
typename Scalar>
126 Teuchos::RCP<Thyra::VectorBase<Scalar> >
131 using Teuchos::rcp_dynamic_cast;
132 using Thyra::MultiVectorBase;
133 using Thyra::VectorBase;
134 using Thyra::multiVectorProductVector;
141 if(full == Teuchos::null)
142 return Teuchos::null;
144 if (this->numExplicitOnlyBlocks_==0)
147 const RCP<DMVPV> full_dmvpv = rcp_dynamic_cast<
DMVPV>(full,
true);
148 const RCP<MultiVectorBase<Scalar> > full_mv =
149 full_dmvpv->getNonconstMultiVector();
150 const RCP<PMVB> blk_full_mv = rcp_dynamic_cast<
PMVB>(full_mv,
true);
153 const int numBlocks = blk_full_mv->productSpace()->numBlocks();
154 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
155 if (numBlocks == numExplicitBlocks+1) {
156 const RCP<MultiVectorBase<Scalar> > imex_mv =
157 blk_full_mv->getNonconstMultiVectorBlock(numExplicitBlocks);
158 return multiVectorProductVector(imex_x_dxdp_prod_space_, imex_mv);
162 TEUCHOS_ASSERT(
false);
163 return Teuchos::null;
166 template <
typename Scalar>
167 Teuchos::RCP<const Thyra::VectorBase<Scalar> >
169 getIMEXVector(
const Teuchos::RCP<
const Thyra::VectorBase<Scalar> > & full)
const
172 using Teuchos::rcp_dynamic_cast;
173 using Thyra::MultiVectorBase;
174 using Thyra::VectorBase;
175 using Thyra::multiVectorProductVector;
182 if(full == Teuchos::null)
183 return Teuchos::null;
185 if (this->numExplicitOnlyBlocks_==0)
188 const RCP<const DMVPV> full_dmvpv = rcp_dynamic_cast<
const DMVPV>(full,
true);
189 const RCP<const MultiVectorBase<Scalar> > full_mv =
190 full_dmvpv->getMultiVector();
191 const RCP<const PMVB> blk_full_mv =
192 rcp_dynamic_cast<
const PMVB>(full_mv,
true);
195 const int numBlocks = blk_full_mv->productSpace()->numBlocks();
196 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
197 if (numBlocks == numExplicitBlocks+1) {
198 const RCP<const MultiVectorBase<Scalar> > imex_mv =
199 blk_full_mv->getMultiVectorBlock(numExplicitBlocks);
200 return multiVectorProductVector(imex_x_dxdp_prod_space_, imex_mv);
204 TEUCHOS_ASSERT(
false);
205 return Teuchos::null;
208 template <
typename Scalar>
209 Teuchos::RCP<Thyra::VectorBase<Scalar> >
212 const Teuchos::RCP<Thyra::VectorBase<Scalar> > & full)
const
215 using Teuchos::rcp_dynamic_cast;
216 using Thyra::MultiVectorBase;
217 using Thyra::VectorBase;
218 using Thyra::multiVectorProductVectorSpace;
219 using Thyra::multiVectorProductVector;
226 if(full == Teuchos::null)
227 return Teuchos::null;
229 if (this->numExplicitOnlyBlocks_==0)
232 const RCP<DMVPV> full_dmvpv = rcp_dynamic_cast<
DMVPV>(full,
true);
233 const RCP<MultiVectorBase<Scalar> > full_mv =
234 full_dmvpv->getNonconstMultiVector();
235 const RCP<PMVB> blk_full_mv = rcp_dynamic_cast<
PMVB>(full_mv,
true);
238 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
239 if (numExplicitBlocks == 1) {
240 const RCP<MultiVectorBase<Scalar> > explicit_mv =
241 blk_full_mv->getNonconstMultiVectorBlock(0);
242 return multiVectorProductVector(explicit_y_dydp_prod_space_, explicit_mv);
246 TEUCHOS_ASSERT(
false);
247 return Teuchos::null;
250 template <
typename Scalar>
251 Teuchos::RCP<const Thyra::VectorBase<Scalar> >
254 const Teuchos::RCP<
const Thyra::VectorBase<Scalar> > & full)
const
257 using Teuchos::rcp_dynamic_cast;
258 using Thyra::MultiVectorBase;
259 using Thyra::VectorBase;
260 using Thyra::multiVectorProductVectorSpace;
261 using Thyra::multiVectorProductVector;
268 if(full == Teuchos::null)
269 return Teuchos::null;
271 if (this->numExplicitOnlyBlocks_==0)
274 const RCP<const DMVPV> full_dmvpv = rcp_dynamic_cast<
const DMVPV>(full,
true);
275 const RCP<const MultiVectorBase<Scalar> > full_mv =
276 full_dmvpv->getMultiVector();
277 const RCP<const PMVB> blk_full_mv =
278 rcp_dynamic_cast<
const PMVB>(full_mv,
true);
281 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
282 if (numExplicitBlocks == 1) {
283 const RCP<const MultiVectorBase<Scalar> > explicit_mv =
284 blk_full_mv->getMultiVectorBlock(0);
285 return multiVectorProductVector(explicit_y_dydp_prod_space_, explicit_mv);
289 TEUCHOS_ASSERT(
false);
290 return Teuchos::null;
293 template <
typename Scalar>
294 Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >
298 return forwardModel_;
301 template <
typename Scalar>
302 Thyra::ModelEvaluatorBase::InArgs<Scalar>
307 using Teuchos::rcp_dynamic_cast;
308 using Thyra::createMember;
310 Thyra::ModelEvaluatorBase::InArgs<Scalar> inArgs = Base::createInArgs();
313 if (this->useImplicitModel_ ==
true) {
314 RCP< const Thyra::VectorBase<Scalar> > y =
315 inArgs.get_p(this->parameterIndex_);
316 if (y != Teuchos::null) {
318 rcp_dynamic_cast<
DMVPV>(createMember(*explicit_y_dydp_prod_space_));
319 Thyra::assign(y_dydp->getNonconstMultiVector().ptr(), Scalar(0.0));
320 Thyra::assign(y_dydp->getNonconstMultiVector()->col(0).ptr(), *y);
321 inArgs.set_p(this->parameterIndex_, y_dydp);
327 template <
typename Scalar>
331 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> & outArgs)
const
333 typedef Thyra::ModelEvaluatorBase MEB;
335 using Teuchos::rcp_dynamic_cast;
336 using Teuchos::Range1D;
338 const int p_index = this->parameterIndex_;
343 RCP<const Thyra::VectorBase<Scalar> > x = inArgs.get_x();
344 RCP<Thyra::VectorBase<Scalar> > x_dot =
345 Thyra::createMember(fsaImplicitModel_->get_x_space());
346 this->timeDer_->compute(x, x_dot);
348 MEB::InArgs<Scalar> fsaImplicitInArgs (this->wrapperImplicitInArgs_);
349 MEB::OutArgs<Scalar> fsaImplicitOutArgs(this->wrapperImplicitOutArgs_);
350 fsaImplicitInArgs.set_x(x);
351 fsaImplicitInArgs.set_x_dot(x_dot);
352 for (
int i=0; i<fsaImplicitModel_->Np(); ++i) {
354 if ((inArgs.get_p(i) != Teuchos::null) and (i != p_index))
355 fsaImplicitInArgs.set_p(i, inArgs.get_p(i));
362 RCP<const Thyra::VectorBase<Scalar> > y;
363 RCP<const Thyra::MultiVectorBase<Scalar> > dydp;
364 if (fsaImplicitInArgs.get_p(p_index) != Teuchos::null) {
365 RCP<const Thyra::VectorBase<Scalar> > p =
366 fsaImplicitInArgs.get_p(p_index);
367 RCP<const Thyra::MultiVectorBase<Scalar> > p_mv =
368 rcp_dynamic_cast<
const DMVPV>(p,
true)->getMultiVector();
369 const int num_param = p_mv->domain()->dim()-1;
371 dydp = p_mv->subView(Range1D(1,num_param));
372 fsaImplicitInArgs.set_p(p_index, y);
374 if (use_dfdp_as_tangent_) {
375 RCP< const Thyra::VectorBase<Scalar> > dydp_vec =
376 Thyra::multiVectorProductVector(explicit_dydp_prod_space_, dydp);
377 fsaImplicitInArgs.set_p(y_tangent_index_, dydp_vec);
380 fsaImplicitOutArgs.set_f(outArgs.get_f());
381 fsaImplicitOutArgs.set_W_op(outArgs.get_W_op());
383 fsaImplicitModel_->evalModel(fsaImplicitInArgs,fsaImplicitOutArgs);
387 if (!use_dfdp_as_tangent_ && outArgs.get_f() != Teuchos::null) {
388 MEB::InArgs<Scalar> appImplicitInArgs =
389 appImplicitModel_->getNominalValues();
390 RCP< const Thyra::VectorBase<Scalar> > app_x =
391 rcp_dynamic_cast<
const DMVPV>(x,
true)->getMultiVector()->col(0);
392 RCP< const Thyra::VectorBase<Scalar> > app_x_dot =
393 rcp_dynamic_cast<
const DMVPV>(x_dot,
true)->getMultiVector()->col(0);
394 appImplicitInArgs.set_x(app_x);
395 appImplicitInArgs.set_x_dot(app_x_dot);
396 for (
int i=0; i<appImplicitModel_->Np(); ++i) {
398 appImplicitInArgs.set_p(i, inArgs.get_p(i));
400 appImplicitInArgs.set_p(p_index, y);
401 if (appImplicitInArgs.supports(MEB::IN_ARG_t))
402 appImplicitInArgs.set_t(inArgs.get_t());
403 MEB::OutArgs<Scalar> appImplicitOutArgs =
404 appImplicitModel_->createOutArgs();
405 MEB::DerivativeSupport dfdp_support =
406 appImplicitOutArgs.supports(MEB::OUT_ARG_DfDp, p_index);
407 Thyra::EOpTransp trans = Thyra::NOTRANS;
408 if (dfdp_support.supports(MEB::DERIV_LINEAR_OP)) {
409 if (my_dfdp_op_ == Teuchos::null)
410 my_dfdp_op_ = appImplicitModel_->create_DfDp_op(p_index);
411 appImplicitOutArgs.set_DfDp(p_index,
412 MEB::Derivative<Scalar>(my_dfdp_op_));
413 trans = Thyra::NOTRANS;
415 else if (dfdp_support.supports(MEB::DERIV_MV_JACOBIAN_FORM)) {
416 if (my_dfdp_mv_ == Teuchos::null)
417 my_dfdp_mv_ = Thyra::createMembers(
418 appImplicitModel_->get_f_space(),
419 appImplicitModel_->get_p_space(p_index)->dim());
420 appImplicitOutArgs.set_DfDp(
421 p_index, MEB::Derivative<Scalar>(my_dfdp_mv_,
422 MEB::DERIV_MV_JACOBIAN_FORM));
423 my_dfdp_op_ = my_dfdp_mv_;
424 trans = Thyra::NOTRANS;
426 else if (dfdp_support.supports(MEB::DERIV_MV_GRADIENT_FORM)) {
427 if (my_dfdp_mv_ == Teuchos::null)
428 my_dfdp_mv_ = Thyra::createMembers(
429 appImplicitModel_->get_p_space(p_index),
430 appImplicitModel_->get_f_space()->dim());
431 appImplicitOutArgs.set_DfDp(
432 p_index, MEB::Derivative<Scalar>(my_dfdp_mv_,
433 MEB::DERIV_MV_GRADIENT_FORM));
434 my_dfdp_op_ = my_dfdp_mv_;
435 trans = Thyra::TRANS;
438 TEUCHOS_TEST_FOR_EXCEPTION(
439 true, std::logic_error,
"Invalid df/dp support");
441 appImplicitModel_->evalModel(appImplicitInArgs, appImplicitOutArgs);
444 RCP<DMVPV> f_dfdp = rcp_dynamic_cast<
DMVPV>(outArgs.get_f(),
true);
445 const int num_param = f_dfdp->getNonconstMultiVector()->domain()->dim()-1;
446 RCP<Thyra::MultiVectorBase<Scalar> > dfdp =
447 f_dfdp->getNonconstMultiVector()->subView(Range1D(1,num_param));
448 my_dfdp_op_->apply(trans, *dydp, dfdp.ptr(), Scalar(1.0), Scalar(1.0));
452 template <
typename Scalar>
453 Teuchos::RCP<const Teuchos::ParameterList>
457 Teuchos::RCP<const Teuchos::ParameterList> fsa_pl =
459 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList(*fsa_pl);
460 pl->set<
int>(
"Sensitivity Y Tangent Index", 3);
466 #endif // Tempus_ModelEvaluatorPairPartIMEX_CombinedFSA_impl_hpp
WrapperModelEvaluatorPairPartIMEX_CombinedFSA(const Teuchos::RCP< const WrapperModelEvaluatorPairPartIMEX_Basic< Scalar > > &forwardModel, const Teuchos::RCP< const Teuchos::ParameterList > &pList=Teuchos::null)
Constructor.
virtual void initialize()
Initialize after setting member data.
Thyra::DefaultMultiVectorProductVector< Scalar > DMVPV
Teuchos::RCP< const DMVPVS > imex_x_dxdp_prod_space_
virtual Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
static Teuchos::RCP< const Teuchos::ParameterList > getValidParameters()
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getExplicitOnlyVector(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &full) const
Extract explicit-only vector from a full solution vector.
virtual Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > getForwardModel() const
Get the underlying forward model.
Thyra::ProductMultiVectorBase< Scalar > PMVB
ModelEvaluator pair for implicit and explicit (IMEX) evaulations.
virtual Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int i) const
Get the p space.
void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &explicitModel, const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &implicitModel, int numExplicitOnlyBlocks=0, int parameterIndex=-1)
Setup ME when using default constructor – for derived classes.
CombinedForwardSensitivityModelEvaluator< Scalar > FSAME
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > appImplicitModel_
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > appExplicitModel_
Teuchos::RCP< const DMVPVS > explicit_dydp_prod_space_
Teuchos::RCP< const FSAME > fsaExplicitModel_
Teuchos::RCP< const FSAME > fsaImplicitModel_
bool use_dfdp_as_tangent_
Teuchos::RCP< const WrapperModelEvaluatorPairPartIMEX_Basic< Scalar > > forwardModel_
virtual void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
Teuchos::RCP< const DMVPVS > explicit_y_dydp_prod_space_
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getIMEXVector(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &full) const
Extract IMEX vector from a full solution vector.