Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DahlquistTestModel_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_TEST_DAHLQUIST_TEST_MODEL_IMPL_HPP
10 #define TEMPUS_TEST_DAHLQUIST_TEST_MODEL_IMPL_HPP
11 
12 #include "Thyra_DefaultSpmdVectorSpace.hpp"
13 #include "Thyra_DetachedVectorView.hpp"
14 #include "Thyra_DetachedMultiVectorView.hpp"
15 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
16 #include "Thyra_DefaultMultiVectorLinearOpWithSolve.hpp"
17 #include "Thyra_DefaultLinearOpSource.hpp"
18 #include "Thyra_VectorStdOps.hpp"
19 #include "Thyra_MultiVectorStdOps.hpp"
20 #include "Thyra_DefaultMultiVectorProductVector.hpp"
21 
22 //#include <iostream>
23 
24 namespace Tempus_Test {
25 
26 template <class Scalar>
28 {
29  constructDahlquistTestModel(-1.0, false);
30 }
31 
32 template <class Scalar>
33 DahlquistTestModel<Scalar>::DahlquistTestModel(Scalar lambda, bool includeXDot)
34 {
35  constructDahlquistTestModel(lambda, includeXDot);
36 }
37 
38 template <class Scalar>
40  bool includeXDot)
41 {
42  lambda_ = lambda;
43  includeXDot_ = includeXDot;
44  isInitialized_ = false;
45  xIC_ = Scalar(1.0);
46  xDotIC_ = Scalar(lambda);
47  dim_ = 1;
48  haveIC_ = true;
49  Np_ = 1;
50  np_ = 1;
51  Ng_ = 1;
52  ng_ = dim_;
53  acceptModelParams_ = false;
54 
55  // Create x_space and f_space
56  x_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
57  f_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
58 
59  using Teuchos::RCP;
60  typedef Thyra::ModelEvaluatorBase MEB;
61  {
62  // Set up prototypical InArgs
63  MEB::InArgsSetup<Scalar> inArgs;
64  inArgs.setModelEvalDescription(this->description());
65  inArgs.setSupports(MEB::IN_ARG_t);
66  inArgs.setSupports(MEB::IN_ARG_x);
67  inArgs.setSupports(MEB::IN_ARG_x_dot);
68 
69  inArgs.setSupports(MEB::IN_ARG_beta);
70  inArgs.setSupports(MEB::IN_ARG_alpha);
71  if (acceptModelParams_) inArgs.set_Np(Np_);
72 
73  inArgs_ = inArgs;
74  }
75 
76  {
77  // Set up prototypical OutArgs
78  MEB::OutArgsSetup<Scalar> outArgs;
79  outArgs.setModelEvalDescription(this->description());
80  outArgs.setSupports(MEB::OUT_ARG_f);
81 
82  outArgs.setSupports(MEB::OUT_ARG_W_op);
83  if (acceptModelParams_) {
84  outArgs.set_Np_Ng(Np_, Ng_);
85  outArgs.setSupports(MEB::OUT_ARG_DfDp, 0, MEB::DERIV_MV_JACOBIAN_FORM);
86  outArgs.setSupports(MEB::OUT_ARG_DgDp, 0, 0, MEB::DERIV_MV_JACOBIAN_FORM);
87  outArgs.setSupports(MEB::OUT_ARG_DgDx, 0, MEB::DERIV_MV_GRADIENT_FORM);
88  }
89  outArgs_ = outArgs;
90  }
91 
92  // Set up nominal values (initial conditions)
93  nominalValues_ = inArgs_;
94  {
95  nominalValues_.set_t(Scalar(0.0));
96  const RCP<Thyra::VectorBase<Scalar> > x_ic = createMember(x_space_);
97  { // scope to delete DetachedVectorView
98  Thyra::DetachedVectorView<Scalar> x_ic_view(*x_ic);
99  x_ic_view[0] = xIC_;
100  }
101  nominalValues_.set_x(x_ic);
102  }
103 
104  if (includeXDot_) {
105  const RCP<Thyra::VectorBase<Scalar> > x_dot_ic = createMember(x_space_);
106  { // scope to delete DetachedVectorView
107  Thyra::DetachedVectorView<Scalar> x_dot_ic_view(*x_dot_ic);
108  x_dot_ic_view[0] = xDotIC_;
109  }
110  nominalValues_.set_x_dot(x_dot_ic);
111  }
112 
113  isInitialized_ = true;
114 }
115 
116 template <class Scalar>
119 {
121  double exact_t = t;
122  inArgs.set_t(exact_t);
123 
124  // Set the exact solution, x.
125  Teuchos::RCP<Thyra::VectorBase<Scalar> > exact_x = createMember(x_space_);
126  { // scope to delete DetachedVectorView
127  Thyra::DetachedVectorView<Scalar> exact_x_view(*exact_x);
128  exact_x_view[0] = exp(lambda_ * exact_t);
129  }
130  inArgs.set_x(exact_x);
131 
132  // Set the exact solution time derivative, xDot.
133  if (includeXDot_) {
135  createMember(x_space_);
136  { // scope to delete DetachedVectorView
137  Thyra::DetachedVectorView<Scalar> exact_x_dot_view(*exact_x_dot);
138  exact_x_dot_view[0] = lambda_ * exp(lambda_ * exact_t);
139  }
140  inArgs.set_x_dot(exact_x_dot);
141  }
142 
143  return inArgs;
144 }
145 
146 template <class Scalar>
149 {
150  return x_space_;
151 }
152 
153 template <class Scalar>
156 {
157  return f_space_;
158 }
159 
160 template <class Scalar>
163 {
164  return nominalValues_;
165 }
166 
167 template <class Scalar>
170 {
171  return inArgs_;
172 }
173 
174 template <class Scalar>
177 {
178  return outArgs_;
179 }
180 
181 template <class Scalar>
184  const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs) const
185 {
186  using Teuchos::RCP;
187  using Teuchos::rcp_dynamic_cast;
189  using Thyra::VectorBase;
190  TEUCHOS_TEST_FOR_EXCEPTION(!isInitialized_, std::logic_error,
191  "Error, setupInOutArgs_ must be called first!\n");
192 
193  const RCP<const VectorBase<Scalar> > x_in = inArgs.get_x().assert_not_null();
195  const RCP<VectorBase<Scalar> > f_out = outArgs.get_f();
196  const RCP<Thyra::LinearOpBase<Scalar> > W_out = outArgs.get_W_op();
197 
198  if (inArgs.get_x_dot().is_null()) {
199  // Evaluate the Explicit ODE f(x,t) [= 0]
200  if (!is_null(f_out)) {
201  Thyra::DetachedVectorView<Scalar> f_out_view(*f_out);
202  f_out_view[0] = lambda_ * x_in_view[0];
203  }
204  else {
206  true, std::logic_error,
207  "Error -- Dahlquist Test Model requires f_out!\n");
208  }
209 
210  if (!is_null(W_out)) {
212  Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(W_out,
213  true);
214  Thyra::DetachedMultiVectorView<Scalar> matrix_view(*matrix);
215  matrix_view(0, 0) = lambda_;
216  }
217  }
218  else {
219  // Evaluate the implicit ODE f(xdot, x, t) [=0]
220  RCP<const VectorBase<Scalar> > x_dot_in;
221  x_dot_in = inArgs.get_x_dot().assert_not_null();
222  Scalar alpha = inArgs.get_alpha();
223  Scalar beta = inArgs.get_beta();
224 
225  if (!is_null(f_out)) {
226  Thyra::DetachedVectorView<Scalar> f_out_view(*f_out);
227  Thyra::ConstDetachedVectorView<Scalar> x_dot_in_view(*x_dot_in);
228  f_out_view[0] = x_dot_in_view[0] - lambda_ * x_in_view[0];
229  }
230 
231  if (!is_null(W_out)) {
233  Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(W_out,
234  true);
235  Thyra::DetachedMultiVectorView<Scalar> matrix_view(*matrix);
236  matrix_view(0, 0) = alpha - beta * lambda_; // d(f0)/d(x0_n)
237  // Note: alpha = d(xdot)/d(x_n) and beta = d(x)/d(x_n)
238  }
239  }
240 }
241 
242 template <class Scalar>
245 {
246  using Teuchos::RCP;
248  this->get_W_factory();
249  RCP<Thyra::LinearOpBase<Scalar> > matrix = this->create_W_op();
250  {
252  Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(matrix,
253  true);
254  {
255  RCP<Thyra::VectorBase<Scalar> > vec = Thyra::createMember(x_space_);
256  {
257  Thyra::DetachedVectorView<Scalar> vec_view(*vec);
258  vec_view[0] = lambda_;
259  }
260  V_V(multivec->col(0).ptr(), *vec);
261  }
262  }
264  Thyra::linearOpWithSolve<Scalar>(*W_factory, matrix);
265  return W;
266 }
267 
268 template <class Scalar>
271 {
272  // const int dim_ = 1;
274  Thyra::createMembers(x_space_, dim_);
275  return (matrix);
276 }
277 
278 template <class Scalar>
281 {
283  Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>();
284  return W_factory;
285 }
286 
287 template <class Scalar>
290 {
291  if (!acceptModelParams_) {
292  return Teuchos::null;
293  }
295  if (l == 0)
296  return p_space_;
297  else if (l == 1 || l == 2)
298  return DxDp_space_;
299  return Teuchos::null;
300 }
301 
302 template <class Scalar>
305 {
306  if (!acceptModelParams_) {
307  return Teuchos::null;
308  }
312  if (l == 0) {
313  p_strings->push_back("Model Coefficient: a");
314  // p_strings->push_back("Model Coefficient: f");
315  // p_strings->push_back("Model Coefficient: L");
316  }
317  else if (l == 1)
318  p_strings->push_back("DxDp");
319  else if (l == 2)
320  p_strings->push_back("Dx_dotDp");
321  return p_strings;
322 }
323 
324 template <class Scalar>
327 {
329  return g_space_;
330 }
331 
332 } // namespace Tempus_Test
333 #endif // TEMPUS_TEST_DAHLQUIST_TEST_MODEL_IMPL_HPP
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs_bar, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs_bar) const
bool is_null(const boost::shared_ptr< T > &p)
RCP< const VectorBase< Scalar > > get_x_dot() const
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Evaluation< VectorBase< Scalar > > get_f() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(index, lower_inclusive, upper_exclusive)
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
void set_x(const RCP< const VectorBase< Scalar > > &x)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getExactSolution(double t) const
Exact solution.
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
void set_x_dot(const RCP< const VectorBase< Scalar > > &x_dot)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int j) const
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int l) const
Teuchos::RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
void constructDahlquistTestModel(Scalar lambda, bool includeXDot)
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_x() const