Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_Simple2DModelEvaluator_def.hpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 
45 #ifndef THYRA_SIMPLE_2D_MODEL_EVALUATOR_DEF_HPP
46 #define THYRA_SIMPLE_2D_MODEL_EVALUATOR_DEF_HPP
47 
48 
49 #include "Thyra_Simple2DModelEvaluator_decl.hpp"
50 #include "Thyra_SimpleDenseLinearOp.hpp"
51 #include "Thyra_DefaultSpmdVectorSpace.hpp"
52 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
53 #include "Thyra_DefaultPreconditioner.hpp"
54 #include "Thyra_DetachedMultiVectorView.hpp"
55 #include "Thyra_DetachedVectorView.hpp"
56 #include "Thyra_MultiVectorStdOps.hpp"
57 #include "Thyra_VectorStdOps.hpp"
58 
59 
60 namespace Thyra {
61 
62 
63 // Nonmember constuctors
64 
65 
66 template<class Scalar>
68 simple2DModelEvaluator()
69 {
71 }
72 
73 
74 // Initializers/Accessors
75 
76 
77 template<class Scalar>
79 {
80  d_ = d;
81 }
82 
83 
84 template<class Scalar>
86 {
87 #ifdef TEUCHOS_DEBUG
88  TEUCHOS_ASSERT_EQUALITY(p_.size(), p.size());
89 #endif
90  p_().assign(p);
91 }
92 
93 
94 template<class Scalar>
96 {
97 #ifdef TEUCHOS_DEBUG
98  TEUCHOS_ASSERT_EQUALITY(x_space_->dim(), x0_in.size());
99 #endif
101  x0.sv().values()().assign(x0_in);
102 }
103 
104 
105 template<class Scalar>
107 {
108  showGetInvalidArg_ = showGetInvalidArg;
109 }
110 
111 
112 // Public functions overridden from ModelEvaulator
113 
114 
115 template<class Scalar>
118 {
119  return x_space_;
120 }
121 
122 
123 template<class Scalar>
126 {
127  return f_space_;
128 }
129 
130 
131 template<class Scalar>
134 {
135  return nominalValues_;
136 }
137 
138 
139 template<class Scalar>
142 {
143  return createNonconstSimpleDenseLinearOp<Scalar>(
144  createMembers<Scalar>(f_space_, x_space_->dim())
145  );
146 }
147 
148 
149 template<class Scalar>
152 {
153  return nonconstUnspecifiedPrec<Scalar>(
154  createNonconstSimpleDenseLinearOp<Scalar>(
155  createMembers<Scalar>(f_space_, x_space_->dim())
156  )
157  );
158 }
159 
160 
161 template<class Scalar>
164 {
165  return W_factory_;
166 }
167 
168 
169 template<class Scalar>
172 {
173  return prototypeInArgs_;
174 }
175 
176 
177 // Private functions overridden from ModelEvaulatorDefaultBase
178 
179 
180 template<class Scalar>
183 {
184  return prototypeOutArgs_;
185 }
186 
187 
188 template<class Scalar>
189 void Simple2DModelEvaluator<Scalar>::evalModelImpl(
192  ) const
193 {
194  using Teuchos::rcp_dynamic_cast;
195  const Scalar one = 1.0, two = 2.0, zero = 0.0;
196 
197  const ConstDetachedVectorView<Scalar> x(inArgs.get_x());
198 
199  const RCP<Thyra::VectorBase<Scalar> > f_out = outArgs.get_f();
200  const RCP<Thyra::LinearOpBase< Scalar > > W_op_out = outArgs.get_W_op();
201  const RCP<Thyra::PreconditionerBase< Scalar > > W_prec_out = outArgs.get_W_prec();
202 
203  if (nonnull(f_out)) {
204  const DetachedVectorView<Scalar> f(f_out);
205  f[0] = x[0] + x[1] * x[1] - p_[0];
206  f[1] = d_ * (x[0] * x[0] - x[1] - p_[1]);
207  }
208 
209  if (nonnull(W_op_out)) {
210  const RCP<SimpleDenseLinearOp<Scalar> > W =
211  rcp_dynamic_cast<SimpleDenseLinearOp<Scalar> >(W_op_out, true);
212  const RCP<MultiVectorBase<Scalar> > W_mv = W->getNonconstMultiVector();
214  W_dmvv(0, 0) = one;
215  W_dmvv(0, 1) = two * x[1];
216  W_dmvv(1, 0) = d_ * two * x[0];
217  W_dmvv(1, 1) = -d_;
218  }
219 
220  if (nonnull(W_prec_out)) {
221  const RCP<SimpleDenseLinearOp<Scalar> > W_prec_op =
222  rcp_dynamic_cast<SimpleDenseLinearOp<Scalar> >(
223  W_prec_out->getNonconstUnspecifiedPrecOp(), true);
224  const RCP<MultiVectorBase<Scalar> > W_prec_mv = W_prec_op->getNonconstMultiVector();
225  Thyra::DetachedMultiVectorView<Scalar> W_prec_dmvv(W_prec_mv);
226  // Diagonal inverse of W (see W above)
227  W_prec_dmvv(0, 0) = one;
228  W_prec_dmvv(0, 1) = zero;
229  W_prec_dmvv(1, 0) = zero;
230  W_prec_dmvv(1, 1) = -one/d_;
231  }
232 
233 }
234 
235 
236 // private
237 
238 
239 template<class Scalar>
240 Simple2DModelEvaluator<Scalar>::Simple2DModelEvaluator()
241  : x_space_(Thyra::defaultSpmdVectorSpace<Scalar>(2)),
242  f_space_(x_space_),
243  W_factory_(Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>()),
244  d_(0.0),
245  p_(x_space_->dim(), Teuchos::ScalarTraits<Scalar>::zero()),
246  showGetInvalidArg_(false)
247 {
248 
249  using Teuchos::RCP;
250  using Thyra::VectorBase;
251  using Thyra::createMember;
252  typedef Thyra::ModelEvaluatorBase MEB;
254 
255  MEB::InArgsSetup<Scalar> inArgs;
256  inArgs.setModelEvalDescription(this->description());
257  inArgs.setSupports(MEB::IN_ARG_x);
258  prototypeInArgs_ = inArgs;
259 
260  MEB::OutArgsSetup<Scalar> outArgs;
261  outArgs.setModelEvalDescription(this->description());
262  outArgs.setSupports(MEB::OUT_ARG_f);
263  outArgs.setSupports(MEB::OUT_ARG_W_op);
264  outArgs.setSupports(MEB::OUT_ARG_W_prec);
265  prototypeOutArgs_ = outArgs;
266 
267  nominalValues_ = inArgs;
268  x0_ = createMember(x_space_);
269  V_S(x0_.ptr(), ST::zero());
270  nominalValues_.set_x(x0_);
271 
272  set_d(10.0);
273  set_p(Teuchos::tuple<Scalar>(2.0, 0.0)());
274  set_x0(Teuchos::tuple<Scalar>(1.0, 1.0)());
275 
276 }
277 
278 
279 } // namespace Thyra
280 
281 
282 //
283 // Explicit instantiation macro
284 //
285 // Must be expanded from within the global namespace!
286 //
287 
288 #define SIMPLE_2D_MODEL_EVALUATOR_INSTANT(SCALAR) \
289  \
290  template class Simple2DModelEvaluator<SCALAR >; \
291  \
292  template Teuchos::RCP<Simple2DModelEvaluator<SCALAR > > \
293  simple2DModelEvaluator(); \
294 
295 
296 #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...