Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_Simple2DModelEvaluator_def.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_SIMPLE_2D_MODEL_EVALUATOR_DEF_HPP
11 #define THYRA_SIMPLE_2D_MODEL_EVALUATOR_DEF_HPP
12 
13 
14 #include "Thyra_Simple2DModelEvaluator_decl.hpp"
15 #include "Thyra_SimpleDenseLinearOp.hpp"
16 #include "Thyra_DefaultSpmdVectorSpace.hpp"
17 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
18 #include "Thyra_DefaultPreconditioner.hpp"
19 #include "Thyra_DetachedMultiVectorView.hpp"
20 #include "Thyra_DetachedVectorView.hpp"
21 #include "Thyra_MultiVectorStdOps.hpp"
22 #include "Thyra_VectorStdOps.hpp"
23 
24 
25 namespace Thyra {
26 
27 
28 // Nonmember constuctors
29 
30 
31 template<class Scalar>
33 simple2DModelEvaluator()
34 {
36 }
37 
38 
39 // Initializers/Accessors
40 
41 
42 template<class Scalar>
44 {
45  d_ = d;
46 }
47 
48 
49 template<class Scalar>
51 {
52 #ifdef TEUCHOS_DEBUG
53  TEUCHOS_ASSERT_EQUALITY(p_.size(), p.size());
54 #endif
55  p_().assign(p);
56 }
57 
58 
59 template<class Scalar>
61 {
62 #ifdef TEUCHOS_DEBUG
63  TEUCHOS_ASSERT_EQUALITY(x_space_->dim(), x0_in.size());
64 #endif
66  x0.sv().values()().assign(x0_in);
67 }
68 
69 
70 template<class Scalar>
72 {
73  showGetInvalidArg_ = showGetInvalidArg;
74 }
75 
76 
77 // Public functions overridden from ModelEvaulator
78 
79 
80 template<class Scalar>
83 {
84  return x_space_;
85 }
86 
87 
88 template<class Scalar>
91 {
92  return f_space_;
93 }
94 
95 
96 template<class Scalar>
99 {
100  return nominalValues_;
101 }
102 
103 
104 template<class Scalar>
107 {
108  return createNonconstSimpleDenseLinearOp<Scalar>(
109  createMembers<Scalar>(f_space_, x_space_->dim())
110  );
111 }
112 
113 
114 template<class Scalar>
117 {
118  return nonconstUnspecifiedPrec<Scalar>(
119  createNonconstSimpleDenseLinearOp<Scalar>(
120  createMembers<Scalar>(f_space_, x_space_->dim())
121  )
122  );
123 }
124 
125 
126 template<class Scalar>
129 {
130  return W_factory_;
131 }
132 
133 
134 template<class Scalar>
137 {
138  return prototypeInArgs_;
139 }
140 
141 
142 // Private functions overridden from ModelEvaulatorDefaultBase
143 
144 
145 template<class Scalar>
148 {
149  return prototypeOutArgs_;
150 }
151 
152 
153 template<class Scalar>
154 void Simple2DModelEvaluator<Scalar>::evalModelImpl(
157  ) const
158 {
159  using Teuchos::rcp_dynamic_cast;
160  const Scalar one = 1.0, two = 2.0, zero = 0.0;
161 
162  const ConstDetachedVectorView<Scalar> x(inArgs.get_x());
163 
164  const RCP<Thyra::VectorBase<Scalar> > f_out = outArgs.get_f();
165  const RCP<Thyra::LinearOpBase< Scalar > > W_op_out = outArgs.get_W_op();
166  const RCP<Thyra::PreconditionerBase< Scalar > > W_prec_out = outArgs.get_W_prec();
167 
168  if (nonnull(f_out)) {
169  const DetachedVectorView<Scalar> f(f_out);
170  f[0] = x[0] + x[1] * x[1] - p_[0];
171  f[1] = d_ * (x[0] * x[0] - x[1] - p_[1]);
172  }
173 
174  if (nonnull(W_op_out)) {
175  const RCP<SimpleDenseLinearOp<Scalar> > W =
176  rcp_dynamic_cast<SimpleDenseLinearOp<Scalar> >(W_op_out, true);
177  const RCP<MultiVectorBase<Scalar> > W_mv = W->getNonconstMultiVector();
179  W_dmvv(0, 0) = one;
180  W_dmvv(0, 1) = two * x[1];
181  W_dmvv(1, 0) = d_ * two * x[0];
182  W_dmvv(1, 1) = -d_;
183  }
184 
185  if (nonnull(W_prec_out)) {
186  const RCP<SimpleDenseLinearOp<Scalar> > W_prec_op =
187  rcp_dynamic_cast<SimpleDenseLinearOp<Scalar> >(
188  W_prec_out->getNonconstUnspecifiedPrecOp(), true);
189  const RCP<MultiVectorBase<Scalar> > W_prec_mv = W_prec_op->getNonconstMultiVector();
190  Thyra::DetachedMultiVectorView<Scalar> W_prec_dmvv(W_prec_mv);
191  // Diagonal inverse of W (see W above)
192  W_prec_dmvv(0, 0) = one;
193  W_prec_dmvv(0, 1) = zero;
194  W_prec_dmvv(1, 0) = zero;
195  W_prec_dmvv(1, 1) = -one/d_;
196  }
197 
198 }
199 
200 
201 // private
202 
203 
204 template<class Scalar>
205 Simple2DModelEvaluator<Scalar>::Simple2DModelEvaluator()
206  : x_space_(Thyra::defaultSpmdVectorSpace<Scalar>(2)),
207  f_space_(x_space_),
208  W_factory_(Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>()),
209  d_(0.0),
210  p_(x_space_->dim(), Teuchos::ScalarTraits<Scalar>::zero()),
211  showGetInvalidArg_(false)
212 {
213 
214  using Teuchos::RCP;
215  using Thyra::VectorBase;
216  using Thyra::createMember;
217  typedef Thyra::ModelEvaluatorBase MEB;
219 
220  MEB::InArgsSetup<Scalar> inArgs;
221  inArgs.setModelEvalDescription(this->description());
222  inArgs.setSupports(MEB::IN_ARG_x);
223  prototypeInArgs_ = inArgs;
224 
225  MEB::OutArgsSetup<Scalar> outArgs;
226  outArgs.setModelEvalDescription(this->description());
227  outArgs.setSupports(MEB::OUT_ARG_f);
228  outArgs.setSupports(MEB::OUT_ARG_W_op);
229  outArgs.setSupports(MEB::OUT_ARG_W_prec);
230  prototypeOutArgs_ = outArgs;
231 
232  nominalValues_ = inArgs;
233  x0_ = createMember(x_space_);
234  V_S(x0_.ptr(), ST::zero());
235  nominalValues_.set_x(x0_);
236 
237  set_d(10.0);
238  set_p(Teuchos::tuple<Scalar>(2.0, 0.0)());
239  set_x0(Teuchos::tuple<Scalar>(1.0, 1.0)());
240 
241 }
242 
243 
244 } // namespace Thyra
245 
246 
247 //
248 // Explicit instantiation macro
249 //
250 // Must be expanded from within the global namespace!
251 //
252 
253 #define SIMPLE_2D_MODEL_EVALUATOR_INSTANT(SCALAR) \
254  \
255  template class Simple2DModelEvaluator<SCALAR >; \
256  \
257  template Teuchos::RCP<Simple2DModelEvaluator<SCALAR > > \
258  simple2DModelEvaluator(); \
259 
260 
261 #endif // THYRA_SIMPLE_2D_MODEL_EVALUATOR_DEF_HPP
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Teuchos::RCP< Thyra::PreconditionerBase< Scalar > > create_W_prec() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
size_type size() const
Evaluation< VectorBase< Scalar > > get_f() const
Precondition: supports(OUT_ARG_f)==true.
void set_p(const Teuchos::ArrayView< const Scalar > &p)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
Create an explicit mutable (non-const) view of a MultiVectorBase object.
void set_x0(const Teuchos::ArrayView< const Scalar > &x0)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Create an explicit mutable (non-const) view of a VectorBase object.
void setShowGetInvalidArgs(bool showGetInvalidArg)
Ptr< T > ptr() const
virtual std::string description() const
Abstract interface for finite-dimensional dense vectors.
Simple 2d simulation only ModelEvaluator for f(x) = 0.
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Base subclass for ModelEvaluator that defines some basic types.
bool nonnull(const boost::shared_ptr< T > &p)
RCP< PreconditionerBase< Scalar > > get_W_prec() const
Precondition: supports(OUT_ARG_W_op)==true.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
RCP< LinearOpBase< Scalar > > get_W_op() const
Precondition: supports(OUT_ARG_W_op)==true.
RCP< const VectorBase< Scalar > > get_x() const
Precondition: supports(IN_ARG_x)==true.
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...