Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Simple2DTpetraModelEvaluator_def.hpp
Go to the documentation of this file.
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 SIMPLE_2D_TPETRA_MODEL_EVALUATOR_HPP
11 #define SIMPLE_2D_TPETRA_MODEL_EVALUATOR_HPP
12 
13 
16 #include "Tpetra_CrsMatrix.hpp"
17 #include "Teuchos_DefaultComm.hpp"
18 
19 
20 // Constructors/Initializers/Accessors
21 
22 
23 template<class Scalar>
25  : d_(0.0)
26 {
27 
28  using Teuchos::RCP;
29  using Teuchos::rcp;
30  using Teuchos::tuple;
31  using Thyra::VectorBase;
32  using Thyra::createMember;
33  typedef Thyra::ModelEvaluatorBase MEB;
35 
36  const int dim = 2;
37 
38  //
39  // A) Create the structure for the problem
40  //
41 
42  MEB::InArgsSetup<Scalar> inArgs;
43  inArgs.setModelEvalDescription(this->description());
44  inArgs.setSupports(MEB::IN_ARG_x);
45  prototypeInArgs_ = inArgs;
46 
47  MEB::OutArgsSetup<Scalar> outArgs;
48  outArgs.setModelEvalDescription(this->description());
49  outArgs.setSupports(MEB::OUT_ARG_f);
50  outArgs.setSupports(MEB::OUT_ARG_W_op);
51  prototypeOutArgs_ = outArgs;
52 
53  //
54  // B) Create the Tpetra objects
55  //
56 
57  const RCP<const Teuchos::Comm<int> > comm =
59 
60  const RCP<const Tpetra::Map<> > map = rcp(new Tpetra::Map<> (dim, 0, comm));
61 
62  typedef Tpetra::Map<>::global_ordinal_type GO;
63 
64  W_op_graph_ = rcp(new Tpetra::CrsGraph<> (map, dim));
65  W_op_graph_->insertGlobalIndices(0, tuple<GO>(0, 1)());
66  W_op_graph_->insertGlobalIndices(1, tuple<GO>(0, 1)());
67  W_op_graph_->fillComplete();
68 
69  p_.resize(dim, ST::zero());
70 
71  x0_ = rcp(new Tpetra::Vector<Scalar>(map));
72  x0_->putScalar(ST::zero());
73 
74  //
75  // C) Create the Thyra wrapped objects
76  //
77 
78  x_space_ = Thyra::createVectorSpace<Scalar>(map);
80 
81  nominalValues_ = inArgs;
83 
84  //
85  // D) Set initial values through interface functions
86  //
87 
88  set_d(10.0);
89  set_p(Teuchos::tuple<Scalar>(2.0, 0.0)());
90  set_x0(Teuchos::tuple<Scalar>(1.0, 1.0)());
91 
92 }
93 
94 
95 template<class Scalar>
97 {
98  d_ = d;
99 }
100 
101 
102 template<class Scalar>
104 {
105 #ifdef TEUCHOS_DEBUG
106  TEUCHOS_ASSERT_EQUALITY(p_.size(), p.size());
107 #endif
108  p_().assign(p);
109 }
110 
111 
112 template<class Scalar>
114 {
115 #ifdef TEUCHOS_DEBUG
116  TEUCHOS_ASSERT_EQUALITY(x_space_->dim(), x0_in.size());
117 #endif
118  x0_->get1dViewNonConst()().assign(x0_in);
119 }
120 
121 
122 // Public functions overridden from ModelEvaulator
123 
124 
125 template<class Scalar>
128 {
129  return x_space_;
130 }
131 
132 
133 template<class Scalar>
136 {
137  return f_space_;
138 }
139 
140 
141 template<class Scalar>
142 Thyra::ModelEvaluatorBase::InArgs<Scalar>
144 {
145  return nominalValues_;
146 }
147 
148 
149 template<class Scalar>
152 {
153  return Thyra::createLinearOp(
154  Teuchos::RCP<Tpetra::Operator<Scalar,int> >(
155  Teuchos::rcp(new Tpetra::CrsMatrix<Scalar,int>(W_op_graph_))
156  )
157  );
158 }
159 
160 
161 template<class Scalar>
162 Thyra::ModelEvaluatorBase::InArgs<Scalar>
164 {
165  return prototypeInArgs_;
166 }
167 
168 
169 // Private functions overridden from ModelEvaulatorDefaultBase
170 
171 
172 template<class Scalar>
173 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
175 {
176  return prototypeOutArgs_;
177 }
178 
179 
180 template<class Scalar>
182  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
183  const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
184  ) const
185 {
186  using Teuchos::RCP;
187  using Teuchos::ArrayRCP;
188  using Teuchos::Array;
189  using Teuchos::tuple;
190  using Teuchos::rcp_dynamic_cast;
192  typedef Tpetra::Map<>::global_ordinal_type GO;
194 
195  const RCP<const Tpetra::Vector<Scalar, int> > x_vec =
196  ConverterT::getConstTpetraVector(inArgs.get_x());
197  const ArrayRCP<const Scalar> x = x_vec->get1dView();
198 
199  const RCP<Tpetra::Vector<Scalar, int> > f_vec =
200  ConverterT::getTpetraVector(outArgs.get_f());
201 
202  const RCP<Tpetra::CrsMatrix<Scalar, int> > W =
203  rcp_dynamic_cast<Tpetra::CrsMatrix<Scalar,int> >(
204  ConverterT::getTpetraOperator(outArgs.get_W_op()),
205  true
206  );
207 
208  if (nonnull(f_vec)) {
209  const ArrayRCP<Scalar> f = f_vec->get1dViewNonConst();
210  f[0] = x[0] + x[1]*x[1] - p_[0];
211  f[1] = d_ * (x[0]*x[0] -x[1] - p_[1]);
212  }
213 
214  if (nonnull(W)) {
215  W->setAllToScalar(ST::zero());
216  W->sumIntoGlobalValues(0, tuple<GO>(0, 1), tuple<Scalar>(1.0, 2.0*x[1]));
217  W->sumIntoGlobalValues(1, tuple<GO>(0, 1), tuple<Scalar>(2.0*d_*x[0], -d_));
218  }
219 
220 }
221 
222 
223 #endif // SIMPLE_2D_TPETRA_MODEL_EVALUATOR_HPP
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs) const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > prototypeOutArgs_
void f()
Thyra::ModelEvaluatorBase::InArgs< Scalar > prototypeInArgs_
Teuchos::RCP< Tpetra::CrsGraph<> > W_op_graph_
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
size_type size() const
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Traits class that enables the extraction of Tpetra operator/vector objects wrapped in Thyra operator/...
void set_x0(const Teuchos::ArrayView< const Scalar > &x0)
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
RCP< LinearOpBase< Scalar > > createLinearOp(const RCP< Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &tpetraOperator, const RCP< const VectorSpaceBase< Scalar > > rangeSpace=Teuchos::null, const RCP< const VectorSpaceBase< Scalar > > domainSpace=Teuchos::null)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< VectorBase< Scalar > > createVector(const RCP< Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &tpetraVector, const RCP< const VectorSpaceBase< Scalar > > space=Teuchos::null)
Teuchos::RCP< Tpetra::Vector< Scalar > > x0_
void set_p(const Teuchos::ArrayView< const Scalar > &p)
void resize(size_type new_size, const value_type &x=value_type())
bool nonnull(const boost::shared_ptr< T > &p)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
Thyra::ModelEvaluatorBase::InArgs< Scalar > nominalValues_
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > f_space_
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > x_space_