Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraThyraWrappers_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 #include "Thyra_DefaultProductVectorSpace.hpp"
45 #include "Thyra_VectorStdOps.hpp"
46 #include "Thyra_MultiVectorStdOps.hpp"
47 #include "Thyra_SpmdVectorSpaceBase.hpp"
48 #include "Thyra_VectorSpaceTester.hpp"
49 #include "Thyra_TestingTools.hpp"
50 #include "Epetra_Vector.h"
52 
54 
56 
57 
58 namespace {
59 
60 
61 using Teuchos::ptrFromRef;
62 using Teuchos::Comm;
63 using Teuchos::rcp_dynamic_cast;
64 using Teuchos::rcp;
65 
66 
67 void createEpetraVsAndMap(const Thyra::Ordinal localDim_in,
68  const Ptr<RCP<const Thyra::VectorSpaceBase<double> > > &vs,
69  const Ptr<RCP<const Epetra_Map> > &epetra_map,
70  const int emptyProcRootRank = -1
71  )
72 {
73  const RCP<const Epetra_Comm> epetra_comm = getEpetraComm();
74  const int procRank = epetra_comm->MyPID();
75  const Thyra::Ordinal localDim = (procRank == emptyProcRootRank ? 0 : localDim_in);
76  *epetra_map = rcp(new Epetra_Map(-1, as<int>(localDim), 0, *epetra_comm));
77  *vs = Thyra::create_VectorSpace(*epetra_map);
78 }
79 
80 
81 void runVectorSpaceTesterTest(const int emptyProc,
82  Teuchos::FancyOStream &out, bool &success)
83 {
84  using Thyra::VectorSpaceBase;
85  using Thyra::SpmdVectorSpaceBase;
86  using Thyra::MultiVectorBase;
87 
88  RCP<const VectorSpaceBase<double> > vs;
89  RCP<const Epetra_Map> epetra_map;
90  createEpetraVsAndMap(g_localDim, outArg(vs), outArg(epetra_map), emptyProc);
91  const int numProcs = epetra_map->Comm().NumProc();
92 
93  if (emptyProc >= numProcs) {
94  out << "emptyProc = " << emptyProc << " >= numProcs = " << numProcs
95  << ": Skipping this test case!\n";
96  return;
97  }
98 
99  const Ordinal dimMultiplier = (emptyProc < 0 ? numProcs : numProcs-1);
100 
101  TEST_EQUALITY(vs->dim(), g_localDim * dimMultiplier);
102 
103  const RCP<const SpmdVectorSpaceBase<double> > spmd_vs =
104  rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(vs, true);
105 
106  TEST_EQUALITY(spmd_vs->localSubDim(), as<int>(epetra_map->NumMyElements()));
107 
108  Thyra::VectorSpaceTester<double> vectorSpaceTester;
109  const double tol = 100.0 * Teuchos::ScalarTraits<double>::eps();
110  vectorSpaceTester.warning_tol((0.1)*tol);
111  vectorSpaceTester.error_tol(tol);
112  vectorSpaceTester.show_all_tests(g_show_all_tests);
113  vectorSpaceTester.dump_all(g_dumpAll);
114  TEST_ASSERT(vectorSpaceTester.check(*vs, &out));
115 }
116 
117 
118 void runCreateVectorUnitTest(const int emptyProc,
119  Teuchos::FancyOStream &out, bool &success)
120 {
121  using Thyra::VectorBase;
122  using Thyra::VectorSpaceBase;
123  using Thyra::MultiVectorBase;
124 
125  RCP<const VectorSpaceBase<double> > vs;
126  RCP<const Epetra_Map> epetra_map;
127  createEpetraVsAndMap(g_localDim, outArg(vs), outArg(epetra_map), emptyProc);
128  const int numProcs = epetra_map->Comm().NumProc();
129 
130  if (emptyProc >= numProcs) {
131  out << "emptyProc = " << emptyProc << " >= numProcs = " << numProcs
132  << ": Skipping this test case!\n";
133  return;
134  }
135 
136  const RCP<Epetra_Vector> epetra_vec = rcp(new Epetra_Vector(*epetra_map));
137  const RCP<VectorBase<double> > thyra_vec = Thyra::create_Vector(epetra_vec, vs);
138  const RCP<Epetra_Vector> epetra_vec2 =
139  Thyra::get_Epetra_Vector(*epetra_map, thyra_vec);
140  TEST_EQUALITY(epetra_vec, epetra_vec2);
141 
142  const RCP<const Epetra_Vector> const_epetra_vec2 =
143  Thyra::get_Epetra_Vector(*epetra_map, thyra_vec.getConst());
144  TEST_EQUALITY(epetra_vec.getConst(), const_epetra_vec2);
145 
146 }
147 
148 
149 //
150 // Unit Tests
151 //
152 
153 
154 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, comm )
155 {
156  typedef Teuchos::GlobalMPISession GMPIS;
157  const RCP<const Epetra_Comm> epetra_comm = getEpetraComm();
158  TEST_EQUALITY(epetra_comm->NumProc(), GMPIS::getNProc());
159  TEST_EQUALITY(epetra_comm->MyPID(), GMPIS::getRank());
160  const RCP<const Comm<Ordinal> > comm = Thyra::create_Comm(epetra_comm);
161  TEST_EQUALITY(comm->getSize(), GMPIS::getNProc());
162  TEST_EQUALITY(comm->getRank(), GMPIS::getRank());
163  const RCP<const Epetra_Comm> epetra_comm2 = Thyra::get_Epetra_Comm(*comm);
164  TEST_EQUALITY(epetra_comm2->NumProc(), GMPIS::getNProc());
165  TEST_EQUALITY(epetra_comm2->MyPID(), GMPIS::getRank());
166 }
167 
168 
169 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester )
170 {
171  runVectorSpaceTesterTest(-1, out, success);
172 }
173 
174 
175 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester_empty_p0 )
176 {
177  runVectorSpaceTesterTest(0, out, success);
178 }
179 
180 
181 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester_empty_p1 )
182 {
183  runVectorSpaceTesterTest(1, out, success);
184 }
185 
186 
187 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester_empty_pLast )
188 {
189  runVectorSpaceTesterTest(Teuchos::GlobalMPISession::getNProc()-1, out, success);
190 }
191 
192 
193 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, createAndViewVector_empty_p0 )
194 {
195  runCreateVectorUnitTest(0, out, success);
196 }
197 
198 
199 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, createAndViewVector_empty_p1 )
200 {
201  runCreateVectorUnitTest(1, out, success);
202 }
203 
204 
205 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, createAndViewVector_empty_pLast )
206 {
207  runCreateVectorUnitTest(Teuchos::GlobalMPISession::getNProc()-1, out, success);
208 }
209 
210 
211 // Test Thyra::create_MultiVector() (const and nonconst) and all views with empty processes
212 
213 
214 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, get_Epetra_MultiVector_singleBlockProductVector )
215 {
216 using Thyra::VectorSpaceBase;
217 using Thyra::MultiVectorBase;
218 
219  RCP<const VectorSpaceBase<double> > vs;
220  RCP<const Epetra_Map> epetra_map;
221  createEpetraVsAndMap(g_localDim, outArg(vs), outArg(epetra_map));
222 
223  const RCP<const VectorSpaceBase<double> > pvs = Thyra::productVectorSpace(vs, 1);
224 
225  const RCP<MultiVectorBase<double> > pmv = Thyra::createMembers(pvs, 1);
226 
227  const double alpha = 3.5;
228  Thyra::assign<double>( pmv.ptr(), alpha );
229 
230  const RCP<Epetra_MultiVector> epetra_mv =
231  Thyra::get_Epetra_MultiVector(*epetra_map, pmv);
232 
233  const RCP<MultiVectorBase<double> > mv2 =
234  Thyra::create_MultiVector(epetra_mv, pvs);
235 
236  Thyra::testRelNormDiffErr<double>(
237  "*pmv->col(0)", *pmv->col(0),
238  "*mv2->col(0)", *mv2->col(0),
239  "max-error", 0.0,
240  "max-warning", 0.0,
241  &out
242  );
243 
244 }
245 
246 
247 } // namespace
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
Get a non-const Epetra_MultiVector view from a non-const MultiVectorBase object if possible...
#define TEST_ASSERT(v1)
static magnitudeType eps()
RCP< const VectorSpaceBase< double > > create_VectorSpace(const RCP< const Epetra_Map > &epetra_map)
Create an VectorSpaceBase object given an Epetra_Map object.
TEUCHOS_UNIT_TEST(EpetraOperatorWrapper, basic)
RCP< MultiVectorBase< double > > create_MultiVector(const RCP< Epetra_MultiVector > &epetra_mv, const RCP< const VectorSpaceBase< double > > &range=Teuchos::null, const RCP< const VectorSpaceBase< double > > &domain=Teuchos::null)
Create a non-const MultiVectorBase object from a non-const Epetra_MultiVector object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< VectorBase< double > > create_Vector(const RCP< Epetra_Vector > &epetra_v, const RCP< const VectorSpaceBase< double > > &space=Teuchos::null)
Create a non-const VectorBase object from a non-const Epetra_Vector object.
RCP< Epetra_Vector > get_Epetra_Vector(const Epetra_Map &map, const RCP< VectorBase< double > > &v)
Get a non-const Epetra_Vector view from a non-const VectorBase object if possible.
RCP< const Epetra_Comm > get_Epetra_Comm(const Teuchos::Comm< Ordinal > &comm)
Get (or create) and Epetra_Comm given a Teuchos::Comm object.
#define TEST_EQUALITY(v1, v2)
Teuchos_Ordinal Ordinal
RCP< const Teuchos::Comm< Ordinal > > create_Comm(const RCP< const Epetra_Comm > &epetraComm)
Given an Epetra_Comm object, return an equivalent Teuchos::Comm object.