Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_WrapperModelEvaluatorSecondOrder_impl.hpp
Go to the documentation of this file.
1 //@HEADER
2 // *****************************************************************************
3 // Tempus: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_WrapperModelEvaluatorSecondOrder_impl_hpp
11 #define Tempus_WrapperModelEvaluatorSecondOrder_impl_hpp
12 
13 namespace Tempus {
14 
15 template <typename Scalar>
18 {
19 #ifdef VERBOSE_DEBUG_OUTPUT
20  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
21 #endif
22  typedef Thyra::ModelEvaluatorBase MEB;
23 
24  MEB::InArgsSetup<Scalar> inArgs;
25  inArgs.setModelEvalDescription(this->description());
26  inArgs.set_Np(appModel_->Np());
27  inArgs.setSupports(MEB::IN_ARG_x);
28 
29  return std::move(inArgs);
30 }
31 
32 template <typename Scalar>
35 {
36 #ifdef VERBOSE_DEBUG_OUTPUT
37  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
38 #endif
39  typedef Thyra::ModelEvaluatorBase MEB;
40 
41  MEB::OutArgsSetup<Scalar> outArgs;
42  outArgs.setModelEvalDescription(this->description());
43  outArgs.set_Np_Ng(appModel_->Np(), 0);
44  outArgs.setSupports(MEB::OUT_ARG_f);
45  outArgs.setSupports(MEB::OUT_ARG_W_op);
46 
47  return std::move(outArgs);
48 }
49 
50 template <typename Scalar>
53  const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs) const
54 {
55 #ifdef VERBOSE_DEBUG_OUTPUT
56  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
57 #endif
58  typedef Thyra::ModelEvaluatorBase MEB;
59  using Teuchos::RCP;
60 
61  // Setup initial condition
62  // Create and populate inArgs
63  MEB::InArgs<Scalar> appInArgs = appModel_->createInArgs();
64  MEB::OutArgs<Scalar> appOutArgs = appModel_->createOutArgs();
65 
66  switch (schemeType_) {
67  case NEWMARK_IMPLICIT_AFORM: {
68  // Specific for the Newmark Implicit a-Form stepper. May want to
69  // redesign this for a generic second-order scheme to not have an
70  // if statement here...
71  // IKT, 3/14/17: this is effectively the same as the
72  // Piro::NewmarkDecorator::evalModel function.
73 
74  // The solution variable in NOX is the acceleration, a_{n+1}
75  appInArgs.set_x_dot_dot(inArgs.get_x());
76 
77  // Compute the velocity.
78  // v_n = velocity_{pred} + \gamma dt a_n
79  auto velocity = Thyra::createMember(inArgs.get_x()->space());
80  Thyra::V_StVpStV(velocity.ptr(), Scalar(1.0), *v_pred_, delta_t_ * gamma_,
81  *inArgs.get_x());
82  appInArgs.set_x_dot(velocity);
83 
84  // Compute the displacement.
85  // d_n = displacement_{pred} + \beta dt^2 a_n
86  auto displacement = Thyra::createMember(inArgs.get_x()->space());
87  Thyra::V_StVpStV(displacement.ptr(), Scalar(1.0), *d_pred_,
88  beta_ * delta_t_ * delta_t_, *inArgs.get_x());
89  appInArgs.set_x(displacement);
90 
91  appInArgs.set_W_x_dot_dot_coeff(Scalar(1.0)); // da/da
92  appInArgs.set_alpha(gamma_ * delta_t_); // dv/da
93  appInArgs.set_beta(beta_ * delta_t_ * delta_t_); // dd/da
94 
95  appInArgs.set_t(t_);
96  for (int i = 0; i < appModel_->Np(); ++i) {
97  if (inArgs.get_p(i) != Teuchos::null)
98  appInArgs.set_p(i, inArgs.get_p(i));
99  }
100 
101  // Setup output condition
102  // Create and populate outArgs
103  appOutArgs.set_f(outArgs.get_f());
104  appOutArgs.set_W_op(outArgs.get_W_op());
105 
106  // build residual and jacobian
107  appModel_->evalModel(appInArgs, appOutArgs);
108  break;
109  }
110 
111  case NEWMARK_IMPLICIT_DFORM: {
112  // Setup initial condition
113  // Populate inArgs
114  RCP<Thyra::VectorBase<Scalar> const> d = inArgs.get_x();
115 
116  RCP<Thyra::VectorBase<Scalar>> v =
117  Thyra::createMember(inArgs.get_x()->space());
118 
119  RCP<Thyra::VectorBase<Scalar>> a =
120  Thyra::createMember(inArgs.get_x()->space());
121 
122 #ifdef DEBUG_OUTPUT
123  Teuchos::Range1D range;
124 
125  *out_ << "\n*** d_bef ***\n";
126  RTOpPack::ConstSubVectorView<Scalar> dov;
127  d->acquireDetachedView(range, &dov);
128  auto doa = dov.values();
129  for (auto i = 0; i < doa.size(); ++i) *out_ << doa[i] << " ";
130  *out_ << "\n*** d_bef ***\n";
131 
132  *out_ << "\n*** v_bef ***\n";
133  RTOpPack::ConstSubVectorView<Scalar> vov;
134  v->acquireDetachedView(range, &vov);
135  auto voa = vov.values();
136  for (auto i = 0; i < voa.size(); ++i) *out_ << voa[i] << " ";
137  *out_ << "\n*** v_bef ***\n";
138 
139  *out_ << "\n*** a_bef ***\n";
140  RTOpPack::ConstSubVectorView<Scalar> aov;
141  a->acquireDetachedView(range, &aov);
142  auto aoa = aov.values();
143  for (auto i = 0; i < aoa.size(); ++i) *out_ << aoa[i] << " ";
144  *out_ << "\n*** a_bef ***\n";
145 #endif
146 
147  Scalar const c = 1.0 / beta_ / delta_t_ / delta_t_;
148 
149  // compute acceleration
150  // a_{n+1} = (d_{n+1} - d_pred) / dt / dt / beta
151  Thyra::V_StVpStV(Teuchos::ptrFromRef(*a), c, *d, -c, *d_pred_);
152 
153  // compute velocity
154  // v_{n+1} = v_pred + \gamma dt a_{n+1}
155  Thyra::V_StVpStV(Teuchos::ptrFromRef(*v), 1.0, *v_pred_,
156  delta_t_ * gamma_, *a);
157 
158  appInArgs.set_x(d);
159  appInArgs.set_x_dot(v);
160  appInArgs.set_x_dot_dot(a);
161 
162  appInArgs.set_W_x_dot_dot_coeff(c); // da/dd
163  appInArgs.set_alpha(gamma_ / delta_t_ / beta_); // dv/dd
164  appInArgs.set_beta(1.0); // dd/dd
165 
166  appInArgs.set_t(t_);
167  for (int i = 0; i < appModel_->Np(); ++i) {
168  if (inArgs.get_p(i) != Teuchos::null)
169  appInArgs.set_p(i, inArgs.get_p(i));
170  }
171 
172  // Setup output condition
173  // Create and populate outArgs
174  appOutArgs.set_f(outArgs.get_f());
175  appOutArgs.set_W_op(outArgs.get_W_op());
176 
177  // build residual and jacobian
178  appModel_->evalModel(appInArgs, appOutArgs);
179 
180  // compute acceleration
181  // a_{n+1} = (d_{n+1} - d_pred) / dt / dt / beta
182  Thyra::V_StVpStV(Teuchos::ptrFromRef(*a), c, *d, -c, *d_pred_);
183 
184  // compute velocity
185  // v_{n+1} = v_pred + \gamma dt a_{n+1}
186  Thyra::V_StVpStV(Teuchos::ptrFromRef(*v), 1.0, *v_pred_,
187  delta_t_ * gamma_, *a);
188 
189  appInArgs.set_x(d);
190  appInArgs.set_x_dot(v);
191  appInArgs.set_x_dot_dot(a);
192 
193 #ifdef DEBUG_OUTPUT
194  *out_ << "\n*** d_aft ***\n";
195  RTOpPack::ConstSubVectorView<Scalar> dnv;
196  d->acquireDetachedView(range, &dnv);
197  auto dna = dnv.values();
198  for (auto i = 0; i < dna.size(); ++i) *out_ << dna[i] << " ";
199  *out_ << "\n*** d_aft ***\n";
200 
201  *out_ << "\n*** v_aft ***\n";
202  RTOpPack::ConstSubVectorView<Scalar> vnv;
203  v->acquireDetachedView(range, &vnv);
204  auto vna = vnv.values();
205  for (auto i = 0; i < vna.size(); ++i) *out_ << vna[i] << " ";
206  *out_ << "\n*** v_aft ***\n";
207 
208  *out_ << "\n*** a_aft ***\n";
209  RTOpPack::ConstSubVectorView<Scalar> anv;
210  a->acquireDetachedView(range, &anv);
211  auto ana = anv.values();
212  for (auto i = 0; i < ana.size(); ++i) *out_ << ana[i] << " ";
213  *out_ << "\n*** a_aft ***\n";
214 #endif
215  break;
216  }
217  }
218 }
219 
220 } // namespace Tempus
221 
222 #endif // Tempus_WrapperModelEvaluatorSecondOrder_impl_hpp
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
Evaluation< VectorBase< Scalar > > get_f() const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
RCP< LinearOpBase< Scalar > > get_W_op() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
RCP< const VectorBase< Scalar > > get_x() const
RCP< const VectorBase< Scalar > > get_p(int l) const