Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_IntegratorForwardSensitivity_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_IntegratorForwardSensitivity_impl_hpp
10 #define Tempus_IntegratorForwardSensitivity_impl_hpp
11 
12 #include "Thyra_DefaultMultiVectorProductVector.hpp"
13 #include "Thyra_VectorStdOps.hpp"
14 #include "Thyra_MultiVectorStdOps.hpp"
15 
16 #include "Tempus_CombinedForwardSensitivityModelEvaluator.hpp"
18 
19 
20 namespace Tempus {
21 
22 template<class Scalar>
27 {
28  model_ = model;
29  integrator_ = integratorBasic<Scalar>();
30  this->setParameterList(inputPL);
31  createSensitivityModelAndStepper(model);
32  if (use_combined_method_)
33  integrator_ = integratorBasic<Scalar>(tempus_pl_, sens_model_);
34  else {
35  integrator_ = integratorBasic<Scalar>();
36  integrator_->setTempusParameterList(tempus_pl_);
37  integrator_->setStepperWStepper(sens_stepper_);
38  integrator_->initialize();
39  }
40 }
41 
42 template<class Scalar>
46  std::string stepperType)
47 {
48  model_ = model;
49  integrator_ = integratorBasic<Scalar>();
50  this->setParameterList(Teuchos::null);
51  createSensitivityModelAndStepper(model);
52  if (use_combined_method_)
53  integrator_ = integratorBasic<Scalar>(sens_model_, stepperType);
54  else {
55  integrator_ = integratorBasic<Scalar>();
56  integrator_->setParameterList(tempus_pl_);
57  integrator_->setStepperWStepper(sens_stepper_);
58  integrator_->initialize();
59  }
60 
61 }
62 
63 template<class Scalar>
66 {
67  integrator_ = integratorBasic<Scalar>();
68  this->setParameterList(Teuchos::null);
69 }
70 
71 template<class Scalar>
75 {
76  createSensitivityModelAndStepper(model);
77  if (use_combined_method_)
78  integrator_->setStepper(sens_model_);
79  else
80  integrator_->setStepperWStepper(sens_stepper_);
81 }
82 
83 template<class Scalar>
88  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xdotdot0,
91  Teuchos::RCP<const Thyra::MultiVectorBase<Scalar> > DxdotdotDp0)
92 {
93  using Teuchos::RCP;
94  using Teuchos::rcp_dynamic_cast;
96  using Thyra::assign;
97  using Thyra::createMember;
99 
100  //
101  // Create and initialize product X, Xdot, Xdotdot
102 
103  RCP< const VectorSpaceBase<Scalar> > space;
104  if (use_combined_method_)
105  space = sens_model_->get_x_space();
106  else
107  space = sens_stepper_->get_x_space();
108  RCP<DMVPV> X = rcp_dynamic_cast<DMVPV>(createMember(space));
109  RCP<DMVPV> Xdot = rcp_dynamic_cast<DMVPV>(createMember(space));
110  RCP<DMVPV> Xdotdot = rcp_dynamic_cast<DMVPV>(createMember(space));
111 
112  const int num_param = X->getNonconstMultiVector()->domain()->dim()-1;
113  const Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
114  const Teuchos::Range1D rng(1,num_param);
115 
116  // x
117  assign(X->getNonconstMultiVector()->col(0).ptr(), *x0);
118  if (DxDp0 == Teuchos::null)
119  assign(X->getNonconstMultiVector()->subView(rng).ptr(), zero);
120  else
121  assign(X->getNonconstMultiVector()->subView(rng).ptr(), *DxDp0);
122 
123  // xdot
124  if (xdot0 == Teuchos::null)
125  assign(Xdot->getNonconstMultiVector()->col(0).ptr(), zero);
126  else
127  assign(Xdot->getNonconstMultiVector()->col(0).ptr(), *xdot0);
128  if (DxdotDp0 == Teuchos::null)
129  assign(Xdot->getNonconstMultiVector()->subView(rng).ptr(), zero);
130  else
131  assign(Xdot->getNonconstMultiVector()->subView(rng).ptr(), *DxdotDp0);
132 
133  // xdotdot
134  if (xdotdot0 == Teuchos::null)
135  assign(Xdotdot->getNonconstMultiVector()->col(0).ptr(), zero);
136  else
137  assign(Xdotdot->getNonconstMultiVector()->col(0).ptr(), *xdotdot0);
138  if (DxdotDp0 == Teuchos::null)
139  assign(Xdotdot->getNonconstMultiVector()->subView(rng).ptr(), zero);
140  else
141  assign(Xdotdot->getNonconstMultiVector()->subView(rng).ptr(), *DxdotdotDp0);
142 
143  integrator_->initializeSolutionHistory(t0, X, Xdot, Xdotdot);
144 }
145 
146 template<class Scalar>
149 getX() const
150 {
151  using Teuchos::RCP;
152  using Teuchos::rcp_dynamic_cast;
154 
155  RCP<const DMVPV> X = rcp_dynamic_cast<const DMVPV>(integrator_->getX());
156  return X->getMultiVector()->col(0);
157 }
158 
159 template<class Scalar>
162 getDxDp() const
163 {
164  using Teuchos::RCP;
165  using Teuchos::rcp_dynamic_cast;
167 
168  RCP<const DMVPV> X = rcp_dynamic_cast<const DMVPV>(integrator_->getX());
169  const int num_param = X->getMultiVector()->domain()->dim()-1;
170  const Teuchos::Range1D rng(1,num_param);
171  return X->getMultiVector()->subView(rng);
172 }
173 
174 template<class Scalar>
177 getXDot() const
178 {
179  using Teuchos::RCP;
180  using Teuchos::rcp_dynamic_cast;
182 
183  RCP<const DMVPV> Xdot = rcp_dynamic_cast<const DMVPV>(integrator_->getXDot());
184  return Xdot->getMultiVector()->col(0);
185 }
186 
187 template<class Scalar>
190 getDXDotDp() const
191 {
192  using Teuchos::RCP;
193  using Teuchos::rcp_dynamic_cast;
195 
196  RCP<const DMVPV> Xdot = rcp_dynamic_cast<const DMVPV>(integrator_->getXDot());
197  const int num_param = Xdot->getMultiVector()->domain()->dim()-1;
198  const Teuchos::Range1D rng(1,num_param);
199  return Xdot->getMultiVector()->subView(rng);
200 }
201 
202 template<class Scalar>
205 getXDotDot() const
206 {
207  using Teuchos::RCP;
208  using Teuchos::rcp_dynamic_cast;
210 
211  RCP<const DMVPV> Xdotdot =
212  rcp_dynamic_cast<const DMVPV>(integrator_->getXDotDot());
213  return Xdotdot->getMultiVector()->col(0);
214 }
215 
216 template<class Scalar>
220 {
221  using Teuchos::RCP;
222  using Teuchos::rcp_dynamic_cast;
224 
225  RCP<const DMVPV> Xdotdot =
226  rcp_dynamic_cast<const DMVPV>(integrator_->getXDotDot());
227  const int num_param = Xdotdot->getMultiVector()->domain()->dim()-1;
228  const Teuchos::Range1D rng(1,num_param);
229  return Xdotdot->getMultiVector()->subView(rng);
230 }
231 
232 template<class Scalar>
233 std::string
235 description() const
236 {
237  std::string name = "Tempus::IntegratorForwardSensitivity";
238  return(name);
239 }
240 
241 template<class Scalar>
242 void
245  Teuchos::FancyOStream &in_out,
246  const Teuchos::EVerbosityLevel verbLevel) const
247 {
248  auto out = Teuchos::fancyOStream( in_out.getOStream() );
249  out->setOutputToRootOnly(0);
250  *out << description() << "::describe" << std::endl;
251  integrator_->describe(in_out, verbLevel);
252 }
253 
254 template<class Scalar>
255 void
258 {
259  tempus_pl_ = Teuchos::parameterList();
260  if (inputPL != Teuchos::null)
261  *tempus_pl_ = *inputPL;
262  tempus_pl_->setParametersNotAlreadySet(*this->getValidParameters());
263  sens_pl_ = Teuchos::sublist(tempus_pl_, "Sensitivities", false);
264  std::string integratorName =
265  tempus_pl_->get<std::string>("Integrator Name", "Default Integrator");
266  std::string stepperName =
267  tempus_pl_->sublist(integratorName).get<std::string>("Stepper Name");
268  stepper_pl_ = Teuchos::sublist(tempus_pl_, stepperName, true);
269  use_combined_method_ =
270  sens_pl_->get<std::string>("Sensitivity Method") == "Combined";
271 }
272 
273 template<class Scalar>
277 {
278  Teuchos::RCP<Teuchos::ParameterList> temp_param_list = tempus_pl_;
279  tempus_pl_ = Teuchos::null;
280  sens_pl_ = Teuchos::null;
281  stepper_pl_ = Teuchos::null;
282  integrator_->unsetParameterList();
283  return temp_param_list;
284 }
285 
286 template<class Scalar>
290 {
294  integrator_->getValidParameters();
297  pl->setParameters(*integrator_pl);
298  Teuchos::ParameterList& spl = pl->sublist("Sensitivities");
299  spl.setParameters(*sensitivity_pl);
300  spl.set("Sensitivity Method", "Combined");
301  spl.set("Reuse State Linear Solver", false);
302 
303  return pl;
304 }
305 
306 template <class Scalar>
307 void
310  const Teuchos::RCP<Thyra::ModelEvaluator<Scalar> >& /* model */)
311 {
312  using Teuchos::rcp;
313 
314  Teuchos::RCP<Teuchos::ParameterList> spl = Teuchos::parameterList();
315  *spl = *sens_pl_;
316  spl->remove("Sensitivity Method");
317 
318  if (use_combined_method_) {
319  spl->remove("Reuse State Linear Solver");
320  sens_model_ =
321  wrapCombinedFSAModelEvaluator(model_, spl);
322  }
323  else {
324  sens_stepper_ =
326  model_, stepper_pl_, spl));
327  }
328 }
329 
331 template<class Scalar>
336 {
339  return(integrator);
340 }
341 
343 template<class Scalar>
347  std::string stepperType)
348 {
350  Teuchos::rcp(new IntegratorForwardSensitivity<Scalar>(model, stepperType));
351  return(integrator);
352 }
353 
355 template<class Scalar>
358 {
361  return(integrator);
362 }
363 
364 } // namespace Tempus
365 #endif // Tempus_IntegratorForwardSensitivity_impl_hpp
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getXDotDot() const
Get current the second time derivative of the solution, xdotdot.
virtual void initializeSolutionHistory(Teuchos::RCP< SolutionState< Scalar > > state=Teuchos::null)
Set the initial state which has the initial conditions.
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDXDotDp() const
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDXDotDotDp() const
A stepper implementing staggered forward sensitivity analysis.
virtual void setStepper(Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > model)
Set the Stepper.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList() override
void createSensitivityModelAndStepper(const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &model)
Teuchos::RCP< IntegratorForwardSensitivity< Scalar > > integratorForwardSensitivity(Teuchos::RCP< Teuchos::ParameterList > pList, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &model)
Nonmember constructor.
bool remove(std::string const &name, bool throwIfNotExists=true)
virtual Teuchos::RCP< const Thyra::MultiVectorBase< Scalar > > getDxDp() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl) override
RCP< const MultiVectorBase< Scalar > > getMultiVector() const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
ParameterList & setParameters(const ParameterList &source)
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getX() const
Get current the solution, x.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
Teuchos::RCP< SensitivityModelEvaluatorBase< Scalar > > wrapCombinedFSAModelEvaluator(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, const Teuchos::RCP< const Teuchos::ParameterList > &pList=Teuchos::null)
RCP< MultiVectorBase< Scalar > > getNonconstMultiVector()
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
virtual Teuchos::RCP< const Thyra::VectorBase< Scalar > > getXDot() const
Get current the time derivative of the solution, xdot.
Time integrator implementing forward sensitivity analysis.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override