10 #ifndef Tempus_IntegratorPseudoTransientForwardSensitivity_impl_hpp
11 #define Tempus_IntegratorPseudoTransientForwardSensitivity_impl_hpp
13 #include "Thyra_DefaultMultiVectorProductVector.hpp"
14 #include "Thyra_VectorStdOps.hpp"
15 #include "Thyra_MultiVectorStdOps.hpp"
21 template <
class Scalar>
28 const bool reuse_solver,
const bool force_W_update)
30 sens_model_(sens_model),
31 state_integrator_(fwd_integrator),
32 sens_integrator_(sens_integrator),
33 reuse_solver_(reuse_solver),
34 force_W_update_(force_W_update),
39 template <
class Scalar>
42 : reuse_solver_(false),
43 force_W_update_(false),
50 template <
class Scalar>
58 bool state_status = state_integrator_->advanceTime();
61 sens_model_->setForwardSolutionState(state_integrator_->getCurrentState());
65 state_integrator_->getStepper()->getSolver() != Teuchos::null) {
66 sens_model_->setSolver(state_integrator_->getStepper()->getSolver(),
72 bool sens_status = sens_integrator_->advanceTime();
74 buildSolutionHistory();
76 return state_status && sens_status;
79 template <
class Scalar>
81 const Scalar timeFinal)
83 TEMPUS_FUNC_TIME_MONITOR_DIFF(
84 "Tempus::IntegratorPseudoTransientForwardSensitivity::advanceTime()",
91 bool state_status =
true;
93 TEMPUS_FUNC_TIME_MONITOR_DIFF(
94 "Tempus::IntegratorPseudoTransientForwardSensitivity::advanceTime::"
97 state_status = state_integrator_->advanceTime(timeFinal);
101 sens_model_->setForwardSolutionState(state_integrator_->getCurrentState());
105 state_integrator_->getStepper()->getSolver() != Teuchos::null) {
106 sens_model_->setSolver(state_integrator_->getStepper()->getSolver(),
111 bool sens_status =
true;
113 TEMPUS_FUNC_TIME_MONITOR_DIFF(
114 "Tempus::IntegratorPseudoTransientForwardSensitivity::advanceTime::"
117 sens_status = sens_integrator_->advanceTime(timeFinal);
120 buildSolutionHistory();
122 return state_status && sens_status;
125 template <
class Scalar>
128 return solutionHistory_->getCurrentTime();
131 template <
class Scalar>
134 return solutionHistory_->getCurrentIndex();
137 template <
class Scalar>
140 Status state_status = state_integrator_->getStatus();
141 Status sens_status = sens_integrator_->getStatus();
147 template <
class Scalar>
151 state_integrator_->setStatus(st);
152 sens_integrator_->setStatus(st);
155 template <
class Scalar>
159 return state_integrator_->getStepper();
162 template <
class Scalar>
166 return state_integrator_->getStepper();
169 template <
class Scalar>
173 return sens_integrator_->getStepper();
176 template <
class Scalar>
180 return solutionHistory_;
183 template <
class Scalar>
188 return state_integrator_->getSolutionHistory();
191 template <
class Scalar>
196 return sens_integrator_->getSolutionHistory();
199 template <
class Scalar>
202 Scalar>::getNonConstSolutionHistory()
204 return solutionHistory_;
207 template <
class Scalar>
211 return state_integrator_->getTimeStepControl();
214 template <
class Scalar>
217 Scalar>::getNonConstTimeStepControl()
222 template <
class Scalar>
225 Scalar>::getStateNonConstTimeStepControl()
230 template <
class Scalar>
233 Scalar>::getSensNonConstTimeStepControl()
238 template <
class Scalar>
242 return state_integrator_->getObserver();
245 template <
class Scalar>
249 state_integrator_->setObserver(obs);
250 sens_integrator_->setObserver(obs);
253 template <
class Scalar>
264 using Teuchos::rcp_dynamic_cast;
266 using Thyra::createMember;
273 RCP<const VectorSpaceBase<Scalar>> space = sens_model_->get_x_space();
274 RCP<DMVPV> X = rcp_dynamic_cast<DMVPV>(createMember(space));
275 RCP<DMVPV> Xdot = rcp_dynamic_cast<DMVPV>(createMember(space));
276 RCP<DMVPV> Xdotdot = rcp_dynamic_cast<DMVPV>(createMember(space));
280 if (DxDp0 == Teuchos::null)
281 assign(X->getNonconstMultiVector().ptr(), zero);
283 assign(X->getNonconstMultiVector().ptr(), *DxDp0);
286 if (DxdotDp0 == Teuchos::null)
287 assign(Xdot->getNonconstMultiVector().ptr(), zero);
289 assign(Xdot->getNonconstMultiVector().ptr(), *DxdotDp0);
292 if (DxdotDp0 == Teuchos::null)
293 assign(Xdotdot->getNonconstMultiVector().ptr(), zero);
295 assign(Xdotdot->getNonconstMultiVector().ptr(), *DxdotdotDp0);
297 state_integrator_->initializeSolutionHistory(t0, x0, xdot0, xdotdot0);
298 sens_integrator_->initializeSolutionHistory(t0, X, Xdot, Xdotdot);
301 template <
class Scalar>
305 return state_integrator_->getX();
308 template <
class Scalar>
313 using Teuchos::rcp_dynamic_cast;
316 RCP<const DMVPV> X = rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getX());
320 template <
class Scalar>
324 return state_integrator_->getXDot();
327 template <
class Scalar>
332 using Teuchos::rcp_dynamic_cast;
335 RCP<const DMVPV> Xdot =
336 rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getXDot());
340 template <
class Scalar>
344 return state_integrator_->getXDotDot();
347 template <
class Scalar>
352 using Teuchos::rcp_dynamic_cast;
355 RCP<const DMVPV> Xdotdot =
356 rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getXDotDot());
360 template <
class Scalar>
368 MEB::InArgs<Scalar> inargs = sens_model_->getNominalValues();
369 MEB::OutArgs<Scalar> outargs = sens_model_->createOutArgs();
370 inargs.set_t(sens_integrator_->getTime());
371 inargs.set_x(sens_integrator_->getX());
372 if (inargs.supports(MEB::IN_ARG_x_dot))
373 inargs.set_x_dot(sens_integrator_->getXDot());
374 if (inargs.supports(MEB::IN_ARG_x_dot_dot))
375 inargs.set_x_dot_dot(sens_integrator_->getXDotDot());
378 Thyra::createMember(sens_model_->get_g_space(1));
381 sens_model_->evalModel(inargs, outargs);
385 template <
class Scalar>
394 MEB::InArgs<Scalar> inargs = sens_model_->getNominalValues();
395 MEB::OutArgs<Scalar> outargs = sens_model_->createOutArgs();
396 inargs.set_t(sens_integrator_->getTime());
397 inargs.set_x(sens_integrator_->getX());
398 if (inargs.supports(MEB::IN_ARG_x_dot))
399 inargs.set_x_dot(sens_integrator_->getXDot());
400 if (inargs.supports(MEB::IN_ARG_x_dot_dot))
401 inargs.set_x_dot_dot(sens_integrator_->getXDotDot());
404 Thyra::createMember(sens_model_->get_g_space(0));
408 sens_model_->evalModel(inargs, outargs);
409 return dgdp->getMultiVector();
412 template <
class Scalar>
416 std::string name =
"Tempus::IntegratorPseudoTransientForwardSensitivity";
420 template <
class Scalar>
424 auto l_out = Teuchos::fancyOStream(out.
getOStream());
426 l_out->setOutputToRootOnly(0);
428 *l_out << description() <<
"::describe" << std::endl;
429 state_integrator_->describe(*l_out, verbLevel);
430 sens_integrator_->describe(*l_out, verbLevel);
433 template <
class Scalar>
440 template <
class Scalar>
446 using Teuchos::rcp_dynamic_cast;
448 using Thyra::createMembers;
450 using Thyra::multiVectorProductVector;
459 RCP<ParameterList> shPL;
462 solutionHistory_ = createSolutionHistoryPL<Scalar>(shPL);
464 const int num_param = rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getX())
468 RCP<const VectorSpaceBase<Scalar>> x_space = model_->get_x_space();
469 RCP<const Thyra::DefaultMultiVectorProductVectorSpace<Scalar>> prod_space =
470 Thyra::multiVectorProductVectorSpace(x_space, num_param + 1);
474 RCP<const SolutionHistory<Scalar>> state_solution_history =
475 state_integrator_->getSolutionHistory();
476 int num_states = state_solution_history->getNumStates();
477 for (
int i = 0; i < num_states; ++i) {
478 RCP<const SolutionState<Scalar>> state = (*state_solution_history)[i];
481 RCP<MultiVectorBase<Scalar>> x_mv = createMembers(x_space, num_param + 1);
482 assign(x_mv->col(0).ptr(), *(state->getX()));
483 assign(x_mv->subView(rng).ptr(), zero);
484 RCP<VectorBase<Scalar>> x = multiVectorProductVector(prod_space, x_mv);
487 RCP<VectorBase<Scalar>> x_dot;
488 if (state->getXDot() != Teuchos::null) {
489 RCP<MultiVectorBase<Scalar>> x_dot_mv =
490 createMembers(x_space, num_param + 1);
491 assign(x_dot_mv->col(0).ptr(), *(state->getXDot()));
492 assign(x_dot_mv->subView(rng).ptr(), zero);
493 x_dot = multiVectorProductVector(prod_space, x_dot_mv);
497 RCP<VectorBase<Scalar>> x_dot_dot;
498 if (state->getXDotDot() != Teuchos::null) {
499 RCP<MultiVectorBase<Scalar>> x_dot_dot_mv =
500 createMembers(x_space, num_param + 1);
501 assign(x_dot_dot_mv->col(0).ptr(), *(state->getXDotDot()));
502 assign(x_dot_dot_mv->subView(rng).ptr(), zero);
503 x_dot_dot = multiVectorProductVector(prod_space, x_dot_dot_mv);
506 RCP<SolutionState<Scalar>> prod_state = state->clone();
508 prod_state->setXDot(x_dot);
509 prod_state->setXDotDot(x_dot_dot);
510 solutionHistory_->addState(prod_state);
513 RCP<const VectorBase<Scalar>> frozen_x =
514 state_solution_history->getCurrentState()->getX();
515 RCP<const VectorBase<Scalar>> frozen_x_dot =
516 state_solution_history->getCurrentState()->getXDot();
517 RCP<const VectorBase<Scalar>> frozen_x_dot_dot =
518 state_solution_history->getCurrentState()->getXDotDot();
519 RCP<const SolutionHistory<Scalar>> sens_solution_history =
520 sens_integrator_->getSolutionHistory();
521 num_states = sens_solution_history->getNumStates();
522 for (
int i = 0; i < num_states; ++i) {
523 RCP<const SolutionState<Scalar>> state = (*sens_solution_history)[i];
526 RCP<MultiVectorBase<Scalar>> x_mv = createMembers(x_space, num_param + 1);
527 RCP<const MultiVectorBase<Scalar>> dxdp =
528 rcp_dynamic_cast<
const DMVPV>(state->getX())->getMultiVector();
529 assign(x_mv->col(0).ptr(), *(frozen_x));
530 assign(x_mv->subView(rng).ptr(), *dxdp);
531 RCP<VectorBase<Scalar>> x = multiVectorProductVector(prod_space, x_mv);
534 RCP<VectorBase<Scalar>> x_dot;
535 if (state->getXDot() != Teuchos::null) {
536 RCP<MultiVectorBase<Scalar>> x_dot_mv =
537 createMembers(x_space, num_param + 1);
538 RCP<const MultiVectorBase<Scalar>> dxdotdp =
539 rcp_dynamic_cast<
const DMVPV>(state->getXDot())->getMultiVector();
540 assign(x_dot_mv->col(0).ptr(), *(frozen_x_dot));
541 assign(x_dot_mv->subView(rng).ptr(), *dxdotdp);
542 x_dot = multiVectorProductVector(prod_space, x_dot_mv);
546 RCP<VectorBase<Scalar>> x_dot_dot;
547 if (state->getXDotDot() != Teuchos::null) {
548 RCP<MultiVectorBase<Scalar>> x_dot_dot_mv =
549 createMembers(x_space, num_param + 1);
550 RCP<const MultiVectorBase<Scalar>> dxdotdotdp =
551 rcp_dynamic_cast<
const DMVPV>(state->getXDotDot())->getMultiVector();
552 assign(x_dot_dot_mv->col(0).ptr(), *(frozen_x_dot_dot));
553 assign(x_dot_dot_mv->subView(rng).ptr(), *dxdotdotdp);
554 x_dot_dot = multiVectorProductVector(prod_space, x_dot_dot_mv);
557 RCP<SolutionState<Scalar>> prod_state = state->clone();
559 prod_state->setXDot(x_dot);
560 prod_state->setXDotDot(x_dot_dot);
561 solutionHistory_->addState(prod_state,
false);
566 template <
class Scalar>
574 auto fwd_integrator = createIntegratorBasic<Scalar>(pList, model);
582 fwd_integrator->getValidParameters();
587 pl->
sublist(
"Sensitivities").
set(
"Reuse State Linear Solver",
false);
588 pl->
sublist(
"Sensitivities").
set(
"Force W Update",
false);
589 pl->
sublist(
"Sensitivities").
set(
"Cache Matrices",
false);
594 pList->
sublist(
"Sensitivities").
get(
"Reuse State Linear Solver",
false);
595 bool force_W_update =
596 pList->sublist(
"Sensitivities").get(
"Force W Update",
false);
597 bool cache_matrices =
598 pList->sublist(
"Sensitivities").get(
"Cache Matrices",
false);
602 if (pList != Teuchos::null) {
603 *pl = pList->
sublist(
"Sensitivities");
605 pl->
remove(
"Reuse State Linear Solver");
606 pl->
remove(
"Force W Update");
607 pl->
remove(
"Cache Matrices");
609 model, sens_residual_model, sens_solve_model, cache_matrices, pl);
610 sens_integrator = createIntegratorBasic<Scalar>(pList, sens_model);
616 model, sens_model, fwd_integrator, sens_integrator, reuse_solver,
623 template <
class Scalar>
634 #endif // Tempus_IntegratorPseudoTransientForwardSensitivity_impl_hpp
virtual Teuchos::RCP< Stepper< Scalar > > getStepper() const override
Get the Stepper.
Teuchos::RCP< Tempus::IntegratorPseudoTransientForwardSensitivity< Scalar > > createIntegratorPseudoTransientForwardSensitivity(Teuchos::RCP< Teuchos::ParameterList > pList, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar >> &model, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar >> &sens_residual_model, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar >> &sens_solve_model)
Nonmember constructor.
virtual void setStatus(const Status st) override
Set Status.
Teuchos::RCP< const SolutionHistory< Scalar > > getSensSolutionHistory() const
virtual Status getStatus() const override
Get Status.
T & get(const std::string &name, T def_value)
Teuchos::RCP< SensitivityModelEvaluatorBase< Scalar > > wrapStaggeredFSAModelEvaluator(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &sens_residual_model, const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &sens_solve_model, const bool is_pseudotransient, const Teuchos::RCP< const Teuchos::ParameterList > &pList=Teuchos::null)
virtual Teuchos::RCP< const SolutionHistory< Scalar > > getSolutionHistory() const override
Get the SolutionHistory.
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDxDp() const
virtual int getIndex() const override
Get current index.
virtual void initializeSolutionHistory(Scalar t0, Teuchos::RCP< const Thyra::VectorBase< Scalar >> x0, Teuchos::RCP< const Thyra::VectorBase< Scalar >> xdot0=Teuchos::null, Teuchos::RCP< const Thyra::VectorBase< Scalar >> xdotdot0=Teuchos::null, Teuchos::RCP< const Thyra::MultiVectorBase< Scalar >> DxDp0=Teuchos::null, Teuchos::RCP< const Thyra::MultiVectorBase< Scalar >> DxdotDp0=Teuchos::null, Teuchos::RCP< const Thyra::MultiVectorBase< Scalar >> DxdotdotDp0=Teuchos::null)
Set the initial state from Thyra::VectorBase(s)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual Teuchos::RCP< TimeStepControl< Scalar > > getNonConstTimeStepControl() override
static Teuchos::RCP< const Teuchos::ParameterList > getValidParameters()
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getG() const
Return response function g.
Teuchos::RCP< IntegratorBasic< Scalar > > state_integrator_
virtual bool advanceTime()
Advance the solution to timeMax, and return true if successful.
bool remove(std::string const &name, bool throwIfNotExists=true)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void setObserver(Teuchos::RCP< IntegratorObserver< Scalar >> obs=Teuchos::null)
Set the Observer.
SensitivityStepMode getStepMode() const
What mode the current time integration step is in.
Status
Status for the Integrator, the Stepper and the SolutionState.
Teuchos::RCP< const SolutionHistory< Scalar > > getStateSolutionHistory() const
IntegratorObserver class for time integrators.
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDgDp() const
Return forward sensitivity stored in Jacobian format.
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getXDotDot() const
Get current the second time derivative of the solution, xdotdot.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
std::string description() const override
RCP< const MultiVectorBase< Scalar > > getMultiVector() const
virtual Teuchos::RCP< IntegratorObserver< Scalar > > getObserver()
Get the Observer.
virtual Scalar getTime() const override
Get current time.
ParameterList & setParameters(const ParameterList &source)
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDXDotDp() const
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
IntegratorPseudoTransientForwardSensitivity()
Destructor.
Time integrator suitable for pseudotransient forward sensitivity analysis.
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
virtual RCP< const VectorSpaceBase< Scalar > > domain() const
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Teuchos::RCP< Stepper< Scalar > > getSensStepper() const
void buildSolutionHistory()
virtual Teuchos::RCP< const TimeStepControl< Scalar > > getTimeStepControl() const override
Get the TimeStepControl.
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDXDotDotDp() const
Teuchos::RCP< IntegratorBasic< Scalar > > sens_integrator_
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getXDot() const
Get current the time derivative of the solution, xdot.
A ModelEvaluator decorator for sensitivity analysis.
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getX() const
Get current the solution, x.
Teuchos::RCP< Stepper< Scalar > > getStateStepper() const