10 #ifndef Tempus_IntegratorPseudoTransientAdjointSensitivity_impl_hpp
11 #define Tempus_IntegratorPseudoTransientAdjointSensitivity_impl_hpp
13 #include "Thyra_DefaultMultiVectorProductVector.hpp"
14 #include "Thyra_VectorStdOps.hpp"
15 #include "Thyra_MultiVectorStdOps.hpp"
20 template <
class Scalar>
26 adjoint_residual_model,
30 adjoint_residual_model_ = adjoint_residual_model;
31 adjoint_solve_model_ = adjoint_solve_model;
32 state_integrator_ = createIntegratorBasic<Scalar>(inputPL, model_);
33 sens_model_ = createSensitivityModel(model_, adjoint_residual_model_,
34 adjoint_solve_model_, inputPL);
35 sens_integrator_ = createIntegratorBasic<Scalar>(inputPL, sens_model_);
37 do_forward_integration_ =
true;
38 do_adjoint_integration_ =
true;
41 template <
class Scalar>
46 adjoint_residual_model,
48 std::string stepperType)
51 adjoint_residual_model_ = adjoint_residual_model;
52 adjoint_solve_model_ = adjoint_solve_model;
53 state_integrator_ = createIntegratorBasic<Scalar>(model_, stepperType);
54 sens_model_ = createSensitivityModel(model_, adjoint_residual_model_,
55 adjoint_solve_model_, Teuchos::null);
56 sens_integrator_ = createIntegratorBasic<Scalar>(sens_model_, stepperType);
58 do_forward_integration_ =
true;
59 do_adjoint_integration_ =
true;
62 template <
class Scalar>
68 : IntegratorPseudoTransientAdjointSensitivity(inputPL, model, adjoint_model,
73 template <
class Scalar>
78 std::string stepperType)
79 : IntegratorPseudoTransientAdjointSensitivity(model, adjoint_model,
80 adjoint_model, stepperType)
84 template <
class Scalar>
89 : IntegratorPseudoTransientAdjointSensitivity(
94 template <
class Scalar>
98 std::string stepperType)
99 : IntegratorPseudoTransientAdjointSensitivity(
104 template <
class Scalar>
105 IntegratorPseudoTransientAdjointSensitivity<
108 state_integrator_ = createIntegratorBasic<Scalar>();
109 sens_integrator_ = createIntegratorBasic<Scalar>();
113 template <
class Scalar>
116 const Scalar tfinal = state_integrator_->getTimeStepControl()->getFinalTime();
117 return advanceTime(tfinal);
120 template <
class Scalar>
122 const Scalar timeFinal)
124 TEMPUS_FUNC_TIME_MONITOR_DIFF(
125 "Tempus::IntegratorPseudoTransientAdjointSensitivity::advanceTime()",
132 bool state_status =
true;
133 if (do_forward_integration_) {
134 TEMPUS_FUNC_TIME_MONITOR_DIFF(
135 "Tempus::IntegratorPseudoTransientAdjointSensitivity::advanceTime::"
141 state_status = state_integrator_->advanceTime(timeFinal);
144 bool sens_status =
true;
145 if (do_adjoint_integration_) {
146 TEMPUS_FUNC_TIME_MONITOR_DIFF(
147 "Tempus::IntegratorPseudoTransientAdjointSensitivity::advanceTime::"
155 sens_model_->setFinalTime(state_integrator_->getTime());
158 sens_model_->setForwardSolutionHistory(
159 state_integrator_->getSolutionHistory());
163 sens_status = sens_integrator_->advanceTime(timeFinal);
167 MEB::InArgs<Scalar> inargs = sens_model_->getNominalValues();
168 MEB::OutArgs<Scalar> outargs = sens_model_->createOutArgs();
169 inargs.set_t(sens_integrator_->getTime());
170 inargs.set_x(sens_integrator_->getX());
171 if (inargs.supports(MEB::IN_ARG_x_dot))
172 inargs.set_x_dot(sens_integrator_->getXDot());
173 if (inargs.supports(MEB::IN_ARG_x_dot_dot))
174 inargs.set_x_dot_dot(sens_integrator_->getXDotDot());
175 RCP<VectorBase<Scalar>> G = dgdp_;
176 if (G == Teuchos::null) {
177 G = Thyra::createMember(sens_model_->get_g_space(0));
178 dgdp_ = Teuchos::rcp_dynamic_cast<
DMVPV>(G);
180 if (g_ == Teuchos::null)
181 g_ = Thyra::createMember(sens_model_->get_g_space(1));
183 outargs.set_g(1, g_);
184 sens_model_->evalModel(inargs, outargs);
186 buildSolutionHistory();
189 return state_status && sens_status;
192 template <
class Scalar>
195 return solutionHistory_->getCurrentTime();
198 template <
class Scalar>
201 return solutionHistory_->getCurrentIndex();
204 template <
class Scalar>
207 Status state_status = state_integrator_->getStatus();
208 Status sens_status = sens_integrator_->getStatus();
214 template <
class Scalar>
218 state_integrator_->setStatus(st);
219 sens_integrator_->setStatus(st);
222 template <
class Scalar>
226 return state_integrator_->getStepper();
229 template <
class Scalar>
233 return state_integrator_->getStepper();
236 template <
class Scalar>
240 return sens_integrator_->getStepper();
243 template <
class Scalar>
247 return solutionHistory_;
250 template <
class Scalar>
255 return state_integrator_->getSolutionHistory();
258 template <
class Scalar>
263 return sens_integrator_->getSolutionHistory();
266 template <
class Scalar>
269 Scalar>::getNonConstSolutionHistory()
271 return solutionHistory_;
274 template <
class Scalar>
278 return state_integrator_->getTimeStepControl();
281 template <
class Scalar>
284 Scalar>::getNonConstTimeStepControl()
289 template <
class Scalar>
292 Scalar>::getStateNonConstTimeStepControl()
297 template <
class Scalar>
300 Scalar>::getSensNonConstTimeStepControl()
305 template <
class Scalar>
309 return state_integrator_->getObserver();
312 template <
class Scalar>
316 state_integrator_->setObserver(obs);
317 sens_integrator_->setObserver(obs);
320 template <
class Scalar>
331 using Teuchos::rcp_dynamic_cast;
333 using Thyra::createMember;
339 RCP<const VectorSpaceBase<Scalar>> space = sens_model_->get_x_space();
340 RCP<DMVPV> Y = rcp_dynamic_cast<
DMVPV>(createMember(space));
341 RCP<DMVPV> Ydot = rcp_dynamic_cast<
DMVPV>(createMember(space));
342 RCP<DMVPV> Ydotdot = rcp_dynamic_cast<
DMVPV>(createMember(space));
346 if (y0 == Teuchos::null)
347 assign(Y->getNonconstMultiVector().ptr(), zero);
349 assign(Y->getNonconstMultiVector().ptr(), *y0);
352 if (ydot0 == Teuchos::null)
353 assign(Ydot->getNonconstMultiVector().ptr(), zero);
355 assign(Ydot->getNonconstMultiVector().ptr(), *ydot0);
358 if (ydotdot0 == Teuchos::null)
359 assign(Ydotdot->getNonconstMultiVector().ptr(), zero);
361 assign(Ydotdot->getNonconstMultiVector().ptr(), *ydotdot0);
363 state_integrator_->initializeSolutionHistory(t0, x0, xdot0, xdotdot0);
364 sens_integrator_->initializeSolutionHistory(t0, Y, Ydot, Ydotdot);
367 template <
class Scalar>
371 return state_integrator_->getX();
374 template <
class Scalar>
378 return state_integrator_->getXDot();
381 template <
class Scalar>
385 return state_integrator_->getXDotDot();
388 template <
class Scalar>
393 using Teuchos::rcp_dynamic_cast;
394 RCP<const DMVPV> mvpv =
395 rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getX());
399 template <
class Scalar>
404 using Teuchos::rcp_dynamic_cast;
405 RCP<const DMVPV> mvpv =
406 rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getXDot());
410 template <
class Scalar>
415 using Teuchos::rcp_dynamic_cast;
416 RCP<const DMVPV> mvpv =
417 rcp_dynamic_cast<
const DMVPV>(sens_integrator_->getXDotDot());
421 template <
class Scalar>
428 template <
class Scalar>
432 return dgdp_->getMultiVector();
435 template <
class Scalar>
439 std::string name =
"Tempus::IntegratorPseudoTransientAdjointSensitivity";
443 template <
class Scalar>
447 auto l_out = Teuchos::fancyOStream(out.
getOStream());
449 l_out->setOutputToRootOnly(0);
451 *l_out << description() <<
"::describe" << std::endl;
452 state_integrator_->describe(*l_out, verbLevel);
453 sens_integrator_->describe(*l_out, verbLevel);
456 template <
class Scalar>
464 state_integrator_->getStepper()->getModel());
465 auto tmp_state_integrator = createIntegratorBasic<Scalar>(inputPL, model);
466 state_integrator_->copy(tmp_state_integrator);
469 sens_integrator_->getStepper()->getModel());
470 auto tmp_sens_integrator = createIntegratorBasic<Scalar>(inputPL, model);
471 sens_integrator_->copy(tmp_sens_integrator);
474 template <
class Scalar>
481 auto tmp_state_integrator = createIntegratorBasic<Scalar>();
482 auto model = state_integrator_->getStepper()->getModel();
483 tmp_state_integrator->setModel(model);
484 state_integrator_->copy(tmp_state_integrator);
486 auto tmp_sens_integrator = createIntegratorBasic<Scalar>();
487 model = sens_integrator_->getStepper()->getModel();
488 tmp_sens_integrator->setModel(model);
489 sens_integrator_->copy(tmp_sens_integrator);
492 sens_integrator_->getValidParameters());
496 template <
class Scalar>
503 state_integrator_->getValidParameters();
512 template <
class Scalar>
517 state_integrator_->getValidParameters());
521 template <
class Scalar>
528 template <
class Scalar>
539 if (inputPL != Teuchos::null) {
540 *pl = inputPL->
sublist(
"Sensitivities");
542 const Scalar tinit = state_integrator_->getTimeStepControl()->getInitTime();
543 const Scalar tfinal = state_integrator_->getTimeStepControl()->getFinalTime();
545 model, adjoint_residual_model, adjoint_solve_model_, tinit, tfinal,
true,
549 template <
class Scalar>
555 using Teuchos::rcp_dynamic_cast;
557 using Thyra::defaultProductVector;
566 state_integrator_->getSolutionHistory()->getValidParameters());
567 solutionHistory_ = createSolutionHistoryPL<Scalar>(shPL);
569 RCP<const VectorSpaceBase<Scalar>> x_space = model_->get_x_space();
570 RCP<const VectorSpaceBase<Scalar>> adjoint_space = sens_model_->get_x_space();
573 spaces[1] = adjoint_space;
574 RCP<const DPVS> prod_space = Thyra::productVectorSpace(spaces());
577 RCP<const SolutionHistory<Scalar>> state_solution_history =
578 state_integrator_->getSolutionHistory();
579 int num_states = state_solution_history->getNumStates();
580 for (
int i = 0; i < num_states; ++i) {
581 RCP<const SolutionState<Scalar>> state = (*state_solution_history)[i];
584 RCP<DPV> x = defaultProductVector(prod_space);
585 assign(x->getNonconstVectorBlock(0).ptr(), *(state->getX()));
586 assign(x->getNonconstVectorBlock(1).ptr(), zero);
587 RCP<VectorBase<Scalar>> x_b = x;
590 RCP<VectorBase<Scalar>> x_dot_b;
591 if (state->getXDot() != Teuchos::null) {
592 RCP<DPV> x_dot = defaultProductVector(prod_space);
593 assign(x_dot->getNonconstVectorBlock(0).ptr(), *(state->getXDot()));
594 assign(x_dot->getNonconstVectorBlock(1).ptr(), zero);
599 RCP<VectorBase<Scalar>> x_dot_dot_b;
600 if (state->getXDotDot() != Teuchos::null) {
601 RCP<DPV> x_dot_dot = defaultProductVector(prod_space);
602 assign(x_dot_dot->getNonconstVectorBlock(0).ptr(),
603 *(state->getXDotDot()));
604 assign(x_dot_dot->getNonconstVectorBlock(1).ptr(), zero);
605 x_dot_dot_b = x_dot_dot;
608 RCP<SolutionState<Scalar>> prod_state = state->clone();
609 prod_state->setX(x_b);
610 prod_state->setXDot(x_dot_b);
611 prod_state->setXDotDot(x_dot_dot_b);
612 solutionHistory_->addState(prod_state);
615 RCP<const VectorBase<Scalar>> frozen_x =
616 state_solution_history->getCurrentState()->getX();
617 RCP<const VectorBase<Scalar>> frozen_x_dot =
618 state_solution_history->getCurrentState()->getXDot();
619 RCP<const VectorBase<Scalar>> frozen_x_dot_dot =
620 state_solution_history->getCurrentState()->getXDotDot();
621 RCP<const SolutionHistory<Scalar>> sens_solution_history =
622 sens_integrator_->getSolutionHistory();
623 num_states = sens_solution_history->getNumStates();
624 for (
int i = 0; i < num_states; ++i) {
625 RCP<const SolutionState<Scalar>> state = (*sens_solution_history)[i];
628 RCP<DPV> x = defaultProductVector(prod_space);
629 assign(x->getNonconstVectorBlock(0).ptr(), *frozen_x);
630 assign(x->getNonconstVectorBlock(1).ptr(), *(state->getX()));
631 RCP<VectorBase<Scalar>> x_b = x;
634 RCP<VectorBase<Scalar>> x_dot_b;
635 if (state->getXDot() != Teuchos::null) {
636 RCP<DPV> x_dot = defaultProductVector(prod_space);
637 assign(x_dot->getNonconstVectorBlock(0).ptr(), *frozen_x_dot);
638 assign(x_dot->getNonconstVectorBlock(1).ptr(), *(state->getXDot()));
643 RCP<VectorBase<Scalar>> x_dot_dot_b;
644 if (state->getXDotDot() != Teuchos::null) {
645 RCP<DPV> x_dot_dot = defaultProductVector(prod_space);
646 assign(x_dot_dot->getNonconstVectorBlock(0).ptr(), *frozen_x_dot_dot);
647 assign(x_dot_dot->getNonconstVectorBlock(1).ptr(),
648 *(state->getXDotDot()));
649 x_dot_dot_b = x_dot_dot;
652 RCP<SolutionState<Scalar>> prod_state = state->clone();
653 prod_state->setX(x_b);
654 prod_state->setXDot(x_dot_b);
655 prod_state->setXDotDot(x_dot_dot_b);
656 solutionHistory_->addState(prod_state,
false);
661 template <
class Scalar>
674 template <
class Scalar>
678 std::string stepperType)
682 model, stepperType));
687 template <
class Scalar>
696 pList, model, adjoint_model));
701 template <
class Scalar>
706 std::string stepperType)
710 model, adjoint_model, stepperType));
715 template <
class Scalar>
725 pList, model, adjoint_residual_model, adjoint_solve_model));
730 template <
class Scalar>
736 std::string stepperType)
740 model, adjoint_residual_model, adjoint_solve_model, stepperType));
745 template <
class Scalar>
755 #endif // Tempus_IntegratorPseudoTransientAdjointSensitivity_impl_hpp
Teuchos::RCP< Stepper< Scalar > > getStateStepper() const
static Teuchos::RCP< const Teuchos::ParameterList > getValidParameters()
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl) override
virtual Scalar getTime() const override
Get current time.
Teuchos::RCP< const SolutionHistory< Scalar > > getStateSolutionHistory() const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual Teuchos::RCP< IntegratorObserver< Scalar > > getObserver()
Get the Observer.
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getY() const
Get the current adjoint solution, y.
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getXDot() const
Get the current time derivative of the solution, xdot.
Teuchos::RCP< Tempus::IntegratorPseudoTransientAdjointSensitivity< Scalar > > integratorPseudoTransientAdjointSensitivity(Teuchos::RCP< Teuchos::ParameterList > pList, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &model)
Nonmember constructor.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList() override
virtual void setObserver(Teuchos::RCP< IntegratorObserver< Scalar > > obs=Teuchos::null)
Set the Observer.
std::string description() const override
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDgDp() const
Return adjoint sensitivity stored in gradient format.
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getYDot() const
Get the current time derivative of the adjoint solution, ydot.
Time integrator suitable for pseudotransient adjoint sensitivity analysis.
SensitivityStepMode getStepMode() const
What mode the current time integration step is in.
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getG() const
Return response function g.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual Teuchos::RCP< TimeStepControl< Scalar > > getNonConstTimeStepControl() override
Status
Status for the Integrator, the Stepper and the SolutionState.
IntegratorObserver class for time integrators.
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getXDotDot() const
Get the current second time derivative of the solution, xdotdot.
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 > > y0=Teuchos::null, Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > ydot0=Teuchos::null, Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > ydotdot0=Teuchos::null)
Set the initial state from Thyra::VectorBase(s)
RCP< const MultiVectorBase< Scalar > > getMultiVector() const
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getYDotDot() const
Get the current second time derivative of the adjoint solution, ydotdot.
IntegratorPseudoTransientAdjointSensitivity()
Destructor.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList() override
ParameterList & setParameters(const ParameterList &source)
RCP< ImplicitAdjointModelEvaluator< Scalar > > implicitAdjointModelEvaluator(const RCP< const ModelEvaluator< Scalar > > &model)
Teuchos::RCP< Stepper< Scalar > > getSensStepper() const
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
virtual void setStatus(const Status st) override
Set Status.
virtual int getIndex() const override
Get current index.
virtual Teuchos::RCP< Stepper< Scalar > > getStepper() const override
Get the Stepper.
virtual Teuchos::RCP< const SolutionHistory< Scalar > > getSolutionHistory() const override
Get the SolutionHistory.
virtual Teuchos::RCP< const TimeStepControl< Scalar > > getTimeStepControl() const override
Get the TimeStepControl.
ModelEvaluator for forming adjoint sensitivity equations.
Teuchos::RCP< const SolutionHistory< Scalar > > getSensSolutionHistory() const
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
virtual bool advanceTime()
Advance the solution to timeMax, and return true if successful.
void buildSolutionHistory()
Teuchos::RCP< AdjointSensitivityModelEvaluator< Scalar > > createSensitivityModel(const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &model, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &adjoint_residual_model, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &adjoint_solve_model, const Teuchos::RCP< Teuchos::ParameterList > &inputPL)
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getX() const
Get the current solution, x.
virtual Status getStatus() const override
Get Status.