10 #ifndef Tempus_ModelEvaluatorPairPartIMEX_StaggeredFSA_impl_hpp
11 #define Tempus_ModelEvaluatorPairPartIMEX_StaggeredFSA_impl_hpp
13 #include "Thyra_VectorStdOps.hpp"
14 #include "Thyra_MultiVectorStdOps.hpp"
18 template <
typename Scalar>
22 Scalar> >& forwardModel,
23 const bool is_pseudotransient,
25 : forwardModel_(forwardModel),
26 use_dfdp_as_tangent_(false),
31 using Thyra::multiVectorProductVectorSpace;
33 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
34 if (pList != Teuchos::null) *pl = *pList;
38 pl->remove(
"Sensitivity Y Tangent Index");
48 const int sens_param_index = pl->get<
int>(
"Sensitivity Parameter Index");
49 const int num_sens_param =
51 RCP<const Thyra::VectorSpaceBase<Scalar> > explicit_y_space =
53 RCP<const Thyra::VectorSpaceBase<Scalar> > implicit_x_space =
56 multiVectorProductVectorSpace(explicit_y_space, num_sens_param);
58 multiVectorProductVectorSpace(implicit_x_space, num_sens_param);
64 template <
typename Scalar>
68 using Teuchos::rcp_dynamic_cast;
70 this->useImplicitModel_ =
true;
71 this->wrapperImplicitInArgs_ = this->createInArgs();
72 this->wrapperImplicitOutArgs_ = this->createOutArgs();
73 this->useImplicitModel_ =
false;
75 RCP<const Thyra::VectorBase<Scalar> > z =
76 this->explicitModel_->getNominalValues().get_x();
80 !(getIMEXVector(z)->space()->isCompatible(
81 *(this->implicitModel_->get_x_space()))),
83 "Error - WrapperModelEvaluatorPairIMEX_StaggeredFSA::initialize()\n"
84 <<
" Explicit and Implicit vector x spaces are incompatible!\n"
85 <<
" Explicit vector x space = "
86 << *(getIMEXVector(z)->space())
87 <<
"\n Implicit vector x space = "
88 << *(this->implicitModel_->get_x_space()) <<
"\n");
91 const RCP<const DMVPV> z_dmvpv = rcp_dynamic_cast<
const DMVPV>(z,
true);
92 const RCP<const Thyra::MultiVectorBase<Scalar> > z_mv =
94 RCP<const PMVB> zPVector = rcp_dynamic_cast<
const PMVB>(z_mv);
96 zPVector == Teuchos::null, std::logic_error,
97 "Error - WrapperModelEvaluatorPairPartIMEX_StaggeredFSA::initialize()\n"
98 " was given a VectorBase that could not be cast to a\n"
99 " ProductVectorBase!\n");
101 int numBlocks = zPVector->productSpace()->numBlocks();
103 TEUCHOS_TEST_FOR_EXCEPTION(
104 !(0 <= this->numExplicitOnlyBlocks_ &&
105 this->numExplicitOnlyBlocks_ < numBlocks),
107 "Error - WrapperModelEvaluatorPairPartIMEX_StaggeredFSA::initialize()\n"
108 <<
"Invalid number of explicit-only blocks = "
109 << this->numExplicitOnlyBlocks_
110 <<
"\nShould be in the interval [0, numBlocks) = [0, "
111 << numBlocks <<
")\n");
114 template <
typename Scalar>
118 if (this->useImplicitModel_) {
119 if (i == this->parameterIndex_)
120 return explicit_dydp_prod_space_;
122 return appImplicitModel_->get_p_space(i);
125 return appExplicitModel_->get_p_space(i);
128 template <
typename Scalar>
134 using Teuchos::rcp_dynamic_cast;
136 using Thyra::multiVectorProductVector;
144 if (full == Teuchos::null)
return Teuchos::null;
146 if (this->numExplicitOnlyBlocks_ == 0)
return full;
148 const RCP<DMVPV> full_dmvpv = rcp_dynamic_cast<
DMVPV>(full,
true);
149 const RCP<MultiVectorBase<Scalar> > full_mv =
151 const RCP<PMVB> blk_full_mv = rcp_dynamic_cast<
PMVB>(full_mv,
true);
154 const int numBlocks = blk_full_mv->
productSpace()->numBlocks();
155 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
156 if (numBlocks == numExplicitBlocks + 1) {
157 const RCP<MultiVectorBase<Scalar> > imex_mv =
158 blk_full_mv->getNonconstMultiVectorBlock(numExplicitBlocks);
159 return multiVectorProductVector(imex_dxdp_prod_space_, imex_mv);
164 return Teuchos::null;
167 template <
typename Scalar>
173 using Teuchos::rcp_dynamic_cast;
175 using Thyra::multiVectorProductVector;
183 if (full == Teuchos::null)
return Teuchos::null;
185 if (this->numExplicitOnlyBlocks_ == 0)
return full;
187 const RCP<const DMVPV> full_dmvpv = rcp_dynamic_cast<
const DMVPV>(full,
true);
188 const RCP<const MultiVectorBase<Scalar> > full_mv =
190 const RCP<const PMVB> blk_full_mv =
191 rcp_dynamic_cast<
const PMVB>(full_mv,
true);
194 const int numBlocks = blk_full_mv->
productSpace()->numBlocks();
195 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
196 if (numBlocks == numExplicitBlocks + 1) {
197 const RCP<const MultiVectorBase<Scalar> > imex_mv =
198 blk_full_mv->getMultiVectorBlock(numExplicitBlocks);
199 return multiVectorProductVector(imex_dxdp_prod_space_, imex_mv);
204 return Teuchos::null;
207 template <
typename Scalar>
213 using Teuchos::rcp_dynamic_cast;
215 using Thyra::multiVectorProductVector;
216 using Thyra::multiVectorProductVectorSpace;
224 if (full == Teuchos::null)
return Teuchos::null;
226 if (this->numExplicitOnlyBlocks_ == 0)
return full;
228 const RCP<DMVPV> full_dmvpv = rcp_dynamic_cast<
DMVPV>(full,
true);
229 const RCP<MultiVectorBase<Scalar> > full_mv =
231 const RCP<PMVB> blk_full_mv = rcp_dynamic_cast<
PMVB>(full_mv,
true);
234 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
235 if (numExplicitBlocks == 1) {
236 const RCP<MultiVectorBase<Scalar> > explicit_mv =
238 return multiVectorProductVector(explicit_dydp_prod_space_, explicit_mv);
243 return Teuchos::null;
246 template <
typename Scalar>
252 using Teuchos::rcp_dynamic_cast;
254 using Thyra::multiVectorProductVector;
255 using Thyra::multiVectorProductVectorSpace;
263 if (full == Teuchos::null)
return Teuchos::null;
265 if (this->numExplicitOnlyBlocks_ == 0)
return full;
267 const RCP<const DMVPV> full_dmvpv = rcp_dynamic_cast<
const DMVPV>(full,
true);
268 const RCP<const MultiVectorBase<Scalar> > full_mv =
270 const RCP<const PMVB> blk_full_mv =
271 rcp_dynamic_cast<
const PMVB>(full_mv,
true);
274 const int numExplicitBlocks = this->numExplicitOnlyBlocks_;
275 if (numExplicitBlocks == 1) {
276 const RCP<const MultiVectorBase<Scalar> > explicit_mv =
278 return multiVectorProductVector(explicit_dydp_prod_space_, explicit_mv);
283 return Teuchos::null;
286 template <
typename Scalar>
290 return forwardModel_;
293 template <
typename Scalar>
300 fsaExplicitModel_->setForwardSolutionHistory(sh);
304 template <
typename Scalar>
312 fsaExplicitModel_->setForwardSolutionState(s);
313 fsaImplicitModel_->setForwardSolutionState(implicit_x_state_);
316 template <
typename Scalar>
319 const bool force_W_update)
323 bool tf = forwardModel_->getUseImplicitModel();
325 nc_forwardModel = Teuchos::rcp_const_cast<
327 nc_forwardModel->setUseImplicitModel(
true);
328 fsaImplicitModel_->setSolver(solver, force_W_update);
329 nc_forwardModel->setUseImplicitModel(tf);
332 template <
typename Scalar>
337 using Teuchos::rcp_dynamic_cast;
338 using Thyra::createMember;
343 if (this->useImplicitModel_ ==
true) {
344 if (inArgs.
get_p(this->parameterIndex_) != Teuchos::null) {
345 RCP<DMVPV> dydp = rcp_dynamic_cast<
DMVPV>(
346 createMember(*explicit_dydp_prod_space_),
true);
347 Thyra::assign(dydp->getNonconstMultiVector().ptr(), Scalar(0.0));
348 inArgs.
set_p(this->parameterIndex_, dydp);
354 template <
typename Scalar>
362 using Teuchos::rcp_dynamic_cast;
367 if (sh_ != Teuchos::null) {
368 forward_t = inArgs.
get_t();
369 if (t_interp_ != forward_t) {
370 if (nc_forward_state_ == Teuchos::null)
371 nc_forward_state_ = sh_->interpolateState(forward_t);
373 sh_->interpolateState(forward_t, nc_forward_state_.get());
374 forward_state_ = nc_forward_state_;
375 t_interp_ = forward_t;
377 fsaImplicitModel_->setForwardSolutionState(implicit_x_state_);
382 forward_t = forward_state_->getTime();
385 const int p_index = this->parameterIndex_;
390 RCP<const Thyra::VectorBase<Scalar> > x = inArgs.
get_x();
391 RCP<Thyra::VectorBase<Scalar> > x_dot =
392 Thyra::createMember(fsaImplicitModel_->get_x_space());
393 this->timeDer_->compute(x, x_dot);
395 MEB::InArgs<Scalar> fsaImplicitInArgs(this->wrapperImplicitInArgs_);
396 MEB::OutArgs<Scalar> fsaImplicitOutArgs(this->wrapperImplicitOutArgs_);
397 if (fsaImplicitInArgs.supports(MEB::IN_ARG_t))
398 fsaImplicitInArgs.set_t(inArgs.
get_t());
399 fsaImplicitInArgs.set_x(x);
400 fsaImplicitInArgs.set_x_dot(x_dot);
401 for (
int i = 0; i < fsaImplicitModel_->Np(); ++i) {
403 if ((inArgs.
get_p(i) != Teuchos::null) && (i != p_index))
404 fsaImplicitInArgs.set_p(i, inArgs.
get_p(i));
412 RCP<const Thyra::VectorBase<Scalar> > y = explicit_y_state_->getX();
413 RCP<const Thyra::MultiVectorBase<Scalar> > dydp;
414 if (fsaImplicitInArgs.get_p(p_index) != Teuchos::null) {
415 RCP<const Thyra::VectorBase<Scalar> > p = fsaImplicitInArgs.get_p(p_index);
416 dydp = rcp_dynamic_cast<
const DMVPV>(p,
true)->getMultiVector();
417 fsaImplicitInArgs.set_p(p_index, y);
419 if (use_dfdp_as_tangent_) {
420 RCP<const Thyra::VectorBase<Scalar> > dydp_vec =
421 Thyra::multiVectorProductVector(explicit_dydp_prod_space_, dydp);
422 fsaImplicitInArgs.set_p(y_tangent_index_, dydp_vec);
425 fsaImplicitOutArgs.set_f(outArgs.
get_f());
426 fsaImplicitOutArgs.set_W_op(outArgs.
get_W_op());
428 fsaImplicitModel_->evalModel(fsaImplicitInArgs, fsaImplicitOutArgs);
432 if (!use_dfdp_as_tangent_ && outArgs.
get_f() != Teuchos::null) {
433 MEB::InArgs<Scalar> appImplicitInArgs =
434 appImplicitModel_->getNominalValues();
436 RCP<const Thyra::VectorBase<Scalar> > app_x = implicit_x_state_->getX();
437 RCP<const Thyra::VectorBase<Scalar> > app_x_dot =
438 implicit_x_state_->getXDot();
439 appImplicitInArgs.set_x(app_x);
440 appImplicitInArgs.set_x_dot(app_x_dot);
441 for (
int i = 0; i < appImplicitModel_->Np(); ++i) {
442 if (i != p_index) appImplicitInArgs.set_p(i, inArgs.
get_p(i));
444 appImplicitInArgs.set_p(p_index, y);
445 if (appImplicitInArgs.supports(MEB::IN_ARG_t))
446 appImplicitInArgs.set_t(forward_t);
447 MEB::OutArgs<Scalar> appImplicitOutArgs =
448 appImplicitModel_->createOutArgs();
449 MEB::DerivativeSupport dfdp_support =
450 appImplicitOutArgs.supports(MEB::OUT_ARG_DfDp, p_index);
452 if (dfdp_support.supports(MEB::DERIV_LINEAR_OP)) {
453 if (my_dfdp_op_ == Teuchos::null)
454 my_dfdp_op_ = appImplicitModel_->create_DfDp_op(p_index);
455 appImplicitOutArgs.set_DfDp(p_index,
456 MEB::Derivative<Scalar>(my_dfdp_op_));
459 else if (dfdp_support.supports(MEB::DERIV_MV_JACOBIAN_FORM)) {
460 if (my_dfdp_mv_ == Teuchos::null)
461 my_dfdp_mv_ = Thyra::createMembers(
462 appImplicitModel_->get_f_space(),
463 appImplicitModel_->get_p_space(p_index)->dim());
464 appImplicitOutArgs.set_DfDp(
466 MEB::Derivative<Scalar>(my_dfdp_mv_, MEB::DERIV_MV_JACOBIAN_FORM));
467 my_dfdp_op_ = my_dfdp_mv_;
470 else if (dfdp_support.supports(MEB::DERIV_MV_GRADIENT_FORM)) {
471 if (my_dfdp_mv_ == Teuchos::null)
473 Thyra::createMembers(appImplicitModel_->get_p_space(p_index),
474 appImplicitModel_->get_f_space()->dim());
475 appImplicitOutArgs.set_DfDp(
477 MEB::Derivative<Scalar>(my_dfdp_mv_, MEB::DERIV_MV_GRADIENT_FORM));
478 my_dfdp_op_ = my_dfdp_mv_;
483 "Invalid df/dp support");
485 appImplicitModel_->evalModel(appImplicitInArgs, appImplicitOutArgs);
488 RCP<Thyra::MultiVectorBase<Scalar> > dfdp =
489 rcp_dynamic_cast<
DMVPV>(outArgs.
get_f(),
true)
490 ->getNonconstMultiVector();
491 my_dfdp_op_->apply(trans, *dydp, dfdp.ptr(), Scalar(1.0), Scalar(1.0));
495 template <
typename Scalar>
503 pl->
set<
int>(
"Sensitivity Y Tangent Index", 3);
507 template <
typename Scalar>
512 forward_state_->getMetaData(),
513 forwardModel_->getExplicitOnlyVector(forward_state_->getX()),
514 forwardModel_->getExplicitOnlyVector(forward_state_->getXDot()),
515 forwardModel_->getExplicitOnlyVector(forward_state_->getXDotDot()),
516 forward_state_->getStepperState(), Teuchos::null));
518 forward_state_->getMetaData(),
519 forwardModel_->getIMEXVector(forward_state_->getX()),
520 forwardModel_->getIMEXVector(forward_state_->getXDot()),
521 forwardModel_->getIMEXVector(forward_state_->getXDotDot()),
522 forward_state_->getStepperState(), Teuchos::null));
527 #endif // Tempus_ModelEvaluatorPairPartIMEX_StaggeredFSA_impl_hpp
virtual Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
virtual Teuchos::RCP< const ProductVectorSpaceBase< Scalar > > productSpace() const =0
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > appImplicitModel_
virtual void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
virtual Teuchos::RCP< MultiVectorBase< Scalar > > getNonconstMultiVectorBlock(const int k)=0
Evaluation< VectorBase< Scalar > > get_f() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual Teuchos::RCP< const MultiVectorBase< Scalar > > getMultiVectorBlock(const int k) const =0
static Teuchos::RCP< const Teuchos::ParameterList > getValidParameters()
static magnitudeType rmax()
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.
Teuchos::RCP< const DMVPVS > explicit_dydp_prod_space_
virtual Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int i) const
Get the p space.
virtual void setSolver(const Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > &solver, const bool force_W_update)
Set the solver of the underlying model if you want to reuse it.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
ModelEvaluator pair for implicit and explicit (IMEX) evaulations.
Teuchos::RCP< FSAME > fsaExplicitModel_
StaggeredForwardSensitivityModelEvaluator< Scalar > FSAME
WrapperModelEvaluatorPairPartIMEX_StaggeredFSA(const Teuchos::RCP< const WrapperModelEvaluatorPairPartIMEX_Basic< Scalar > > &forwardModel, const bool is_pseudotransient, const Teuchos::RCP< const Teuchos::ParameterList > &pList=Teuchos::null)
Constructor.
virtual void setForwardSolutionHistory(const Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > &sh)
Set solution history from forward state evaluation (for interpolation)
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.
virtual void setForwardSolutionState(const Teuchos::RCP< const Tempus::SolutionState< Scalar > > &s)
Set solution state from forward state evaluation (for frozen state)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getIMEXVector(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &full) const
Extract IMEX vector from a full solution vector.
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > appExplicitModel_
void buildIMEXStates() const
Build implicit x and end explicit y states from forward_state_.
Teuchos::RCP< const WrapperModelEvaluatorPairPartIMEX_Basic< Scalar > > forwardModel_
virtual void initialize()
Initialize after setting member data.
void set_p(int l, const RCP< const VectorBase< Scalar > > &p_l)
Teuchos::RCP< const DMVPVS > imex_dxdp_prod_space_
RCP< MultiVectorBase< Scalar > > getNonconstMultiVector()
bool use_dfdp_as_tangent_
#define TEUCHOS_ASSERT(assertion_test)
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_x() const
Solution state for integrators and steppers.
RCP< const VectorBase< Scalar > > get_p(int l) const
Teuchos::RCP< FSAME > fsaImplicitModel_