Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_AuxiliaryIntegralModelEvaluator_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_AuxiliaryIntegralModelEvaluator_impl_hpp
11 #define Tempus_AuxiliaryIntegralModelEvaluator_impl_hpp
12 
15 #include "Thyra_VectorStdOps.hpp"
16 
17 namespace Tempus {
18 
19 template <typename Scalar>
21  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > &model,
22  const int g_index)
23  : model_(model),
24  g_index_(g_index),
25  t_interp_(Teuchos::ScalarTraits<Scalar>::rmax())
26 {
27  typedef Thyra::ModelEvaluatorBase MEB;
28 
29  space_ = model_->get_g_space(g_index);
30 
31  MEB::InArgs<Scalar> me_inArgs = model_->createInArgs();
32  MEB::InArgsSetup<Scalar> inArgs;
33  inArgs.setModelEvalDescription(this->description());
34  inArgs.setSupports(MEB::IN_ARG_x);
35  inArgs.setSupports(MEB::IN_ARG_t);
36  if (me_inArgs.supports(MEB::IN_ARG_x_dot))
37  inArgs.setSupports(MEB::IN_ARG_x_dot);
38  inArgs.setSupports(MEB::IN_ARG_alpha);
39  inArgs.setSupports(MEB::IN_ARG_beta);
40  inArgs.set_Np(me_inArgs.Np());
41  prototypeInArgs_ = inArgs;
42 
43  MEB::OutArgs<Scalar> me_outArgs = model_->createOutArgs();
44  MEB::OutArgsSetup<Scalar> outArgs;
45  outArgs.setModelEvalDescription(this->description());
46  outArgs.set_Np_Ng(me_inArgs.Np(), 0);
47  outArgs.setSupports(MEB::OUT_ARG_f);
48  outArgs.setSupports(MEB::OUT_ARG_W_op);
49  prototypeOutArgs_ = outArgs;
50 }
51 
52 template <typename Scalar>
55 {
56  sh_ = sh;
58  forward_state_ = Teuchos::null;
59 }
60 
61 template <typename Scalar>
64 {
65  TEUCHOS_ASSERT(p < model_->Np());
66  return model_->get_p_space(p);
67 }
68 
69 template <typename Scalar>
72 {
73  TEUCHOS_ASSERT(p < model_->Np());
74  return model_->get_p_names(p);
75 }
76 
77 template <typename Scalar>
80 {
81  return space_;
82 }
83 
84 template <typename Scalar>
87 {
88  return space_;
89 }
90 
91 template <typename Scalar>
94 {
95  return Thyra::scaledIdentity(space_, Scalar(1.0));
96 }
97 
98 template <typename Scalar>
101 {
102  return Thyra::scaledIdentitySolveFactory(space_, Scalar(1.0));
103 }
104 
105 template <typename Scalar>
108 {
109  return prototypeInArgs_;
110 }
111 
112 template <typename Scalar>
115 {
116  typedef Thyra::ModelEvaluatorBase MEB;
117  using Teuchos::RCP;
118  using Teuchos::rcp_dynamic_cast;
119 
120  MEB::InArgs<Scalar> me_nominal = model_->getNominalValues();
121  MEB::InArgs<Scalar> nominal = this->createInArgs();
122 
123  const Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
124 
125  // Set initial x, x_dot
126  RCP<Thyra::VectorBase<Scalar> > x = Thyra::createMember(*space_);
127  Thyra::assign(x.ptr(), zero);
128  nominal.set_x(x);
129 
130  if (me_nominal.supports(MEB::IN_ARG_x_dot)) {
131  RCP<Thyra::VectorBase<Scalar> > x_dot = Thyra::createMember(*space_);
132  Thyra::assign(x_dot.ptr(), zero);
133  nominal.set_x_dot(x_dot);
134  }
135 
136  const int np = model_->Np();
137  for (int i = 0; i < np; ++i) nominal.set_p(i, me_nominal.get_p(i));
138 
139  return nominal;
140 }
141 
142 template <typename Scalar>
145 {
146  return prototypeOutArgs_;
147 }
148 
149 template <typename Scalar>
152  const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs) const
153 {
154  typedef Thyra::ModelEvaluatorBase MEB;
155  using Teuchos::RCP;
156  using Teuchos::rcp_dynamic_cast;
157 
158  // Interpolate forward solution at supplied time, reusing previous
159  // interpolation if possible
160  TEUCHOS_ASSERT(sh_ != Teuchos::null);
161  const Scalar t = inArgs.get_t();
162  if (forward_state_ == Teuchos::null || t_interp_ != t) {
163  if (forward_state_ == Teuchos::null)
164  forward_state_ = sh_->interpolateState(t);
165  else
166  sh_->interpolateState(t, forward_state_.get());
167  t_interp_ = t;
168  }
169 
170  // Residual
171  RCP<Thyra::VectorBase<Scalar> > f = outArgs.get_f();
172  if (f != Teuchos::null) {
173  MEB::InArgs<Scalar> me_inArgs = model_->getNominalValues();
174  me_inArgs.set_x(forward_state_->getX());
175  if (me_inArgs.supports(MEB::IN_ARG_x_dot))
176  me_inArgs.set_x_dot(forward_state_->getXDot());
177  if (me_inArgs.supports(MEB::IN_ARG_t)) me_inArgs.set_t(t);
178  const int np = me_inArgs.Np();
179  for (int i = 0; i < np; ++i) me_inArgs.set_p(i, inArgs.get_p(i));
180 
181  MEB::OutArgs<Scalar> me_outArgs = model_->createOutArgs();
182  me_outArgs.set_g(g_index_, f);
183 
184  model_->evalModel(me_inArgs, me_outArgs);
185 
186  // For explicit form, f = g and we are done
187  // For implicit form, f = dz/dt - g
188  if (inArgs.supports(MEB::IN_ARG_x_dot)) {
189  RCP<const Thyra::VectorBase<Scalar> > x_dot = inArgs.get_x_dot();
190  if (x_dot != Teuchos::null) Thyra::V_VmV(f.ptr(), *x_dot, *f);
191  }
192  }
193 
194  // W = alpha*df/z_dot + beta*df/dz = alpha * I
195  RCP<Thyra::LinearOpBase<Scalar> > op = outArgs.get_W_op();
196  if (op != Teuchos::null) {
197  const Scalar alpha = inArgs.get_alpha();
198  RCP<Thyra::ScaledIdentityLinearOpWithSolve<Scalar> > si_op =
199  rcp_dynamic_cast<Thyra::ScaledIdentityLinearOpWithSolve<Scalar> >(op);
200  si_op->setScale(alpha);
201  }
202 }
203 
204 } // namespace Tempus
205 
206 #endif
Implicit concrete LinearOpBase subclass that takes a flattended out multi-vector and performs a multi...
AuxiliaryIntegralModelEvaluator(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, const int g_index)
Constructor.
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
RCP< const VectorBase< Scalar > > get_x_dot() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > prototypeInArgs_
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
Evaluation< VectorBase< Scalar > > get_f() const
static magnitudeType rmax()
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
void setForwardSolutionHistory(const Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > &sh)
Set solution history from forward evaluation.
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > space_
virtual std::string description() const
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int p) const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Thyra::ModelEvaluatorBase::OutArgs< Scalar > prototypeOutArgs_
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int p) const
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > model_
bool supports(EInArgsMembers arg) const
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_p(int l) const