Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Simple2DTpetraModelEvaluator_UnitTests.cpp
Go to the documentation of this file.
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 
46 
48 
49 
50 namespace {
51 
52 
53 using Teuchos::null;
54 using Teuchos::RCP;
55 typedef Thyra::ModelEvaluatorBase MEB;
56 
57 
58 //
59 // Unit tests
60 //
61 
62 
64 {
65  RCP<Simple2DTpetraModelEvaluator<Scalar> > model = simple2DTpetraModelEvaluator<Scalar>();
66  TEST_ASSERT(model != null);
67  TEST_EQUALITY(model->Np(), 0);
68  TEST_EQUALITY(model->Ng(), 0);
69  TEST_ASSERT(model->get_x_space() != null);
70  TEST_EQUALITY(model->get_x_space()->dim(), 2);
71  TEST_ASSERT(model->get_f_space() != null);
72  TEST_EQUALITY(model->get_f_space()->dim(), 2);
73  TEST_ASSERT(nonnull(model->getNominalValues().get_x()));
74  TEST_ASSERT(model->create_W_op() != null);
75  //TEST_ASSERT(model->get_W_factory() != null);
76  MEB::InArgs<Scalar> inArgs = model->createInArgs();
77  TEST_ASSERT(inArgs.supports(MEB::IN_ARG_x));
78  TEST_EQUALITY(inArgs.Np(), 0);
79 }
80 
81 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_DOUBLE)
83  Simple2DTpetraModelEvaluator, construct )
84 #endif
85 
86 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
88  Simple2DTpetraModelEvaluator, construct )
89 #endif
90 
91 
93 {
94  using Thyra::LinearOpBase;
95  using Thyra::VectorBase;
96  using Teuchos::ArrayRCP;
97  using Teuchos::ArrayView;
98  using Teuchos::as;
99  using Teuchos::rcp_dynamic_cast;
100  using Teuchos::tuple;
102  typedef Tpetra::Map<>::local_ordinal_type LO;
104  typedef typename ST::magnitudeType ScalarMag;
106 
107  RCP<Simple2DTpetraModelEvaluator<Scalar> > model = simple2DTpetraModelEvaluator<Scalar>();
108 
109  const Scalar d = 7.0;
110  model->set_d(d);
111 
112  const Scalar p_0 = 2.0, p_1 = 6.0;
113  model->set_p(tuple<Scalar>(p_0, p_1));
114 
115  const Scalar x_0 = 3.0, x_1 = 4.0;
116  model->set_x0(tuple<Scalar>(x_0, x_1));
117 
118  MEB::InArgs<Scalar> inArgs = model->getNominalValues();
119  MEB::OutArgs<Scalar> outArgs = model->createOutArgs();
120  const RCP<VectorBase<Scalar> > f = createMember(model->get_f_space());
121  const RCP<LinearOpBase<Scalar> > W_op = model->create_W_op();
122  outArgs.set_f(f);
123  outArgs.set_W_op(W_op);
124  model->evalModel(inArgs, outArgs);
125 
126  const ScalarMag tol = 100.0 * SMT::eps();
127 
128  const RCP<const Tpetra::Vector<Scalar> > f_tpetra =
129  ConverterT::getConstTpetraVector(f);
130 
131  const ArrayRCP<const Scalar> f_tpetra_vals = f_tpetra->get1dView();
132  TEST_FLOATING_EQUALITY(f_tpetra_vals[0], as<Scalar>(x_0 + x_1*x_1 - p_0), tol);
133  TEST_FLOATING_EQUALITY(f_tpetra_vals[1], as<Scalar>(d * (x_0*x_0 - x_1 - p_1)), tol);
134 
135  const RCP<const Tpetra::CrsMatrix<Scalar> > W_tpetra =
136  rcp_dynamic_cast<Tpetra::CrsMatrix<Scalar> >(
137  ConverterT::getTpetraOperator(W_op));
138 
139  ArrayView<const LO> row_indices;
140  ArrayView<const Scalar> row_values;
141 
142  W_tpetra->getLocalRowView(0, row_indices, row_values);
143  // FIXME (mfh 22 Oct 2015) This test assumes that local indices
144  // occur in a particular order. Tpetra does not necessarily
145  // guarantee this.
146  TEST_EQUALITY( row_indices[0], static_cast<LO> (0) );
147  TEST_EQUALITY( row_indices[1], static_cast<LO> (1) );
148  TEST_FLOATING_EQUALITY( row_values[0], as<Scalar>(1.0), tol );
149  TEST_FLOATING_EQUALITY( row_values[1], as<Scalar>(2.0*x_1), tol );
150 
151  W_tpetra->getLocalRowView(1, row_indices, row_values);
152  TEST_EQUALITY( row_indices[0], static_cast<LO> (0) );
153  TEST_EQUALITY( row_indices[1], static_cast<LO> (1) );
154  TEST_FLOATING_EQUALITY( row_values[0], as<Scalar>(d*2.0*x_0), tol );
155  TEST_FLOATING_EQUALITY( row_values[1], as<Scalar>(-d), tol );
156 
157 }
158 
159 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_DOUBLE)
162 #endif
163 
164 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
167 #endif
168 
169 
170 
171 } // namespace
#define TEST_ASSERT(v1)
void f()
Traits class that enables the extraction of Tpetra operator/vector objects wrapped in Thyra operator/...
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_FLOAT(TEST_GROUP, TEST_NAME)
TypeTo as(const TypeFrom &t)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
bool nonnull(const boost::shared_ptr< T > &p)
Simple 2d simulation only ModelEvaluator for f(x) = 0 using Tpetra objects.
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TpetraThyraWrappers, convertTpetraToThyraComm, Scalar)
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_DOUBLE(TEST_GROUP, TEST_NAME)
#define TEST_EQUALITY(v1, v2)