Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_TpetraExplicitAdjointModelEvaluator.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef Thyra_TpetraExplicitAdjointModelEvaluator_hpp
11 #define Thyra_TpetraExplicitAdjointModelEvaluator_hpp
12 
13 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
14 #include "Thyra_TpetraLinearOp.hpp"
15 #include "Tpetra_RowMatrixTransposer.hpp"
16 
17 namespace Thyra {
18 
29  template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal=LocalOrdinal, typename Node=Tpetra::KokkosClassic::DefaultNode::DefaultNodeType>
31  public ModelEvaluatorDelegatorBase<Scalar>{
32 public:
33 
36  const RCP<const ModelEvaluator<Scalar> >& model) :
37  ModelEvaluatorDelegatorBase<Scalar>(model) {}
38 
41  const RCP<ModelEvaluator<Scalar> >& model) :
42  ModelEvaluatorDelegatorBase<Scalar>(model) {}
43 
45  virtual ~TpetraExplicitAdjointModelEvaluator() = default;
46 
48  {
49  // This ME should use the same InArgs as the underlying model. However
50  // we can't just use it's InArgs directly because the description won't
51  // match (which is checked in debug builds). Instead create a new
52  // InArgsSetup initialized by the underlying model's createInArgs() and
53  // set the description appropriately.
55  this->getUnderlyingModel()->createInArgs();
56  inArgs.setModelEvalDescription(this->description());
57  return inArgs;
58  }
59 
65  if (thyra_fwd_op == Teuchos::null)
66  thyra_fwd_op = this->getUnderlyingModel()->create_W_op();
67  RCP<TLO> thyra_tpetra_fwd_op =
68  Teuchos::rcp_dynamic_cast<TLO>(thyra_fwd_op,true);
69  RCP<TO> tpetra_fwd_op = thyra_tpetra_fwd_op->getTpetraOperator();
70  RCP<TCM> tpetra_fwd_mat =
71  Teuchos::rcp_dynamic_cast<TCM>(tpetra_fwd_op,true);
72  TRMT transposer(tpetra_fwd_mat);
73  RCP<TCM> tpetra_trans_mat = transposer.createTranspose();
74  return tpetraLinearOp(thyra_op->range(), thyra_op->domain(),
75  tpetra_trans_mat);
76  }
77 
78 private:
79 
80  mutable RCP< Thyra::LinearOpBase<Scalar> > thyra_fwd_op;
81 
82  ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const
83  {
84  typedef ModelEvaluatorBase MEB;
85  MEB::OutArgs<Scalar> model_outArgs =
86  this->getUnderlyingModel()->createOutArgs();
87  MEB::OutArgsSetup<Scalar> outArgs;
88  outArgs.setModelEvalDescription(this->description());
89  outArgs.setSupports(MEB::OUT_ARG_f); //All models must support f, apparently
90  outArgs.setSupports(MEB::OUT_ARG_W_op);
91  TEUCHOS_ASSERT(model_outArgs.supports(MEB::OUT_ARG_W_op));
92  return outArgs;
93  }
94 
95  void evalModelImpl(
96  const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
97  const ModelEvaluatorBase::OutArgs<Scalar> &outArgs) const
98  {
99  typedef ModelEvaluatorBase MEB;
100  typedef TpetraLinearOp<Scalar,LocalOrdinal,GlobalOrdinal,Node> TLO;
104 
105  MEB::OutArgs<Scalar> model_outArgs =
106  this->getUnderlyingModel()->createOutArgs();
107 
108  if (model_outArgs.supports(MEB::OUT_ARG_W_op) &&
109  outArgs.get_W_op() != Teuchos::null) {
110  // Compute underlying W_op.
111  if (thyra_fwd_op == Teuchos::null)
112  thyra_fwd_op = this->getUnderlyingModel()->create_W_op();
113  model_outArgs->set_W_op(thyra_fwd_op);
114  this->getUnderlyingModel()->evalModel(inArgs, model_outArgs);
115 
116  // Transpose W_op
117  // Unfortunately, because of the horrendous design of the Tpetra
118  // RowMatrixTransposer, this creates a new transposed matrix each time
119  RCP<TLO> thyra_tpetra_fwd_op =
120  Teuchos::rcp_dynamic_cast<TLO>(thyra_fwd_op,true);
121  RCP<TO> tpetra_fwd_op = thyra_tpetra_fwd_op->getTpetraOperator();
122  RCP<TCM> tpetra_fwd_mat =
123  Teuchos::rcp_dynamic_cast<TCM>(tpetra_fwd_op,true);
124  TRMT transposer(tpetra_fwd_mat);
125  RCP<TCM> tpetra_trans_mat = transposer.createTranspose();
126 
127  // Copy transposed matrix into our outArg
128  RCP<LOB> thyra_adj_op = outArgs.get_W_op();
129  RCP<TLO> thyra_tpetra_adj_op =
130  Teuchos::rcp_dynamic_cast<TLO>(thyra_adj_op,true);
131  RCP<TO> tpetra_adj_op = thyra_tpetra_adj_op->getTpetraOperator();
132  RCP<TCM> tpetra_adj_mat =
133  Teuchos::rcp_dynamic_cast<TCM>(tpetra_adj_op,true);
134  *tpetra_adj_mat = *tpetra_trans_mat;
135  }
136  }
137 
138 };
139 
140 template <typename Scalar>
141 RCP<TpetraExplicitAdjointModelEvaluator<Scalar> >
142 tpetraExplicitAdjointModelEvaluator(
143  const RCP<const ModelEvaluator<Scalar> >& model)
144 {
145  return Teuchos::rcp(new TpetraExplicitAdjointModelEvaluator<Scalar>(model));
146 }
147 
148 template <typename Scalar>
149 RCP<TpetraExplicitAdjointModelEvaluator<Scalar> >
150 tpetraExplicitAdjointModelEvaluator(
151  const RCP<ModelEvaluator<Scalar> >& model)
152 {
153  return Teuchos::rcp(new TpetraExplicitAdjointModelEvaluator<Scalar>(model));
154 }
155 
156 } // namespace Thyra
157 
158 #endif
Protected subclass of InArgs that only ModelEvaluator subclasses can access to set up the selection o...
Pure abstract base interface for evaluating a stateless &quot;model&quot; that can be mapped into a number of d...
virtual RCP< const ModelEvaluator< Scalar > > getUnderlyingModel() const
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
void setModelEvalDescription(const std::string &modelEvalDescription)
This is a base class that delegetes almost all function to a wrapped model evaluator object...
ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
virtual ~TpetraExplicitAdjointModelEvaluator()=default
Destructor.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
A model evaluator decorator for computing an explicit adjoint.
virtual std::string description() const
Base subclass for ModelEvaluator that defines some basic types.
#define TEUCHOS_ASSERT(assertion_test)
Concrete Thyra::LinearOpBase subclass for Tpetra::Operator.
TpetraExplicitAdjointModelEvaluator(const RCP< const ModelEvaluator< Scalar > > &model)
Constructor.
TpetraExplicitAdjointModelEvaluator(const RCP< ModelEvaluator< Scalar > > &model)
Constructor.
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...