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>
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();
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 =
93 RCP<const PMVB> zPVector = rcp_dynamic_cast<
const PMVB>(z_mv);
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_ &&
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>
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>
131 using Teuchos::rcp_dynamic_cast;
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 =
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);
163 return Teuchos::null;
166 template <
typename Scalar>
172 using Teuchos::rcp_dynamic_cast;
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 =
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);
205 return Teuchos::null;
208 template <
typename Scalar>
215 using Teuchos::rcp_dynamic_cast;
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 =
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 =
242 return multiVectorProductVector(explicit_y_dydp_prod_space_, explicit_mv);
247 return Teuchos::null;
250 template <
typename Scalar>
257 using Teuchos::rcp_dynamic_cast;
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 =
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 =
285 return multiVectorProductVector(explicit_y_dydp_prod_space_, explicit_mv);
290 return Teuchos::null;
293 template <
typename Scalar>
298 return forwardModel_;
301 template <
typename Scalar>
307 using Teuchos::rcp_dynamic_cast;
308 using Thyra::createMember;
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>
335 using Teuchos::rcp_dynamic_cast;
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) && (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);
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_));
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_;
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_;
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);
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>
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.
Teuchos::RCP< const DMVPVS > imex_x_dxdp_prod_space_
virtual Teuchos::RCP< const ProductVectorSpaceBase< Scalar > > productSpace() const =0
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
static Teuchos::RCP< const Teuchos::ParameterList > getValidParameters()
virtual Teuchos::RCP< MultiVectorBase< Scalar > > getNonconstMultiVectorBlock(const int k)=0
Evaluation< VectorBase< Scalar > > get_f() const
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 MultiVectorBase< Scalar > > getMultiVectorBlock(const int k) const =0
virtual Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > getForwardModel() const
Get the underlying forward model.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
RCP< const VectorBase< Scalar > > col(Ordinal j) const
RCP< const MultiVectorBase< Scalar > > getMultiVector() const
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
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
void set_p(int l, const RCP< const VectorBase< Scalar > > &p_l)
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > appExplicitModel_
Teuchos::RCP< const DMVPVS > explicit_dydp_prod_space_
Teuchos::RCP< const FSAME > fsaExplicitModel_
RCP< MultiVectorBase< Scalar > > getNonconstMultiVector()
Teuchos::RCP< const FSAME > fsaImplicitModel_
#define TEUCHOS_ASSERT(assertion_test)
bool use_dfdp_as_tangent_
Teuchos::RCP< const WrapperModelEvaluatorPairPartIMEX_Basic< Scalar > > forwardModel_
RCP< LinearOpBase< Scalar > > get_W_op() const
virtual void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
RCP< const VectorBase< Scalar > > get_x() const
Teuchos::RCP< const DMVPVS > explicit_y_dydp_prod_space_
RCP< const VectorBase< Scalar > > get_p(int l) const
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getIMEXVector(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &full) const
Extract IMEX vector from a full solution vector.