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 // @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 #include "Thyra_DefaultProductVectorSpace.hpp"
11 #include "Thyra_VectorStdOps.hpp"
12 #include "Thyra_MultiVectorStdOps.hpp"
13 #include "Thyra_SpmdVectorSpaceBase.hpp"
14 #include "Thyra_VectorSpaceTester.hpp"
15 #include "Thyra_TestingTools.hpp"
16 #include "Epetra_Vector.h"
18 
20 
22 
23 
24 namespace {
25 
26 
27 using Teuchos::ptrFromRef;
28 using Teuchos::Comm;
29 using Teuchos::rcp_dynamic_cast;
30 using Teuchos::rcp;
31 
32 
33 void createEpetraVsAndMap(const Thyra::Ordinal localDim_in,
34  const Ptr<RCP<const Thyra::VectorSpaceBase<double> > > &vs,
35  const Ptr<RCP<const Epetra_Map> > &epetra_map,
36  const int emptyProcRootRank = -1
37  )
38 {
39  const RCP<const Epetra_Comm> epetra_comm = getEpetraComm();
40  const int procRank = epetra_comm->MyPID();
41  const Thyra::Ordinal localDim = (procRank == emptyProcRootRank ? 0 : localDim_in);
42  *epetra_map = rcp(new Epetra_Map(-1, as<int>(localDim), 0, *epetra_comm));
43  *vs = Thyra::create_VectorSpace(*epetra_map);
44 }
45 
46 
47 void runVectorSpaceTesterTest(const int emptyProc,
48  Teuchos::FancyOStream &out, bool &success)
49 {
50  using Thyra::VectorSpaceBase;
51  using Thyra::SpmdVectorSpaceBase;
52  using Thyra::MultiVectorBase;
53 
54  RCP<const VectorSpaceBase<double> > vs;
55  RCP<const Epetra_Map> epetra_map;
56  createEpetraVsAndMap(g_localDim, outArg(vs), outArg(epetra_map), emptyProc);
57  const int numProcs = epetra_map->Comm().NumProc();
58 
59  if (emptyProc >= numProcs) {
60  out << "emptyProc = " << emptyProc << " >= numProcs = " << numProcs
61  << ": Skipping this test case!\n";
62  return;
63  }
64 
65  const Ordinal dimMultiplier = (emptyProc < 0 ? numProcs : numProcs-1);
66 
67  TEST_EQUALITY(vs->dim(), g_localDim * dimMultiplier);
68 
69  const RCP<const SpmdVectorSpaceBase<double> > spmd_vs =
70  rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(vs, true);
71 
72  TEST_EQUALITY(spmd_vs->localSubDim(), as<int>(epetra_map->NumMyElements()));
73 
74  Thyra::VectorSpaceTester<double> vectorSpaceTester;
75  const double tol = 100.0 * Teuchos::ScalarTraits<double>::eps();
76  vectorSpaceTester.warning_tol((0.1)*tol);
77  vectorSpaceTester.error_tol(tol);
78  vectorSpaceTester.show_all_tests(g_show_all_tests);
79  vectorSpaceTester.dump_all(g_dumpAll);
80  TEST_ASSERT(vectorSpaceTester.check(*vs, &out));
81 }
82 
83 
84 void runCreateVectorUnitTest(const int emptyProc,
85  Teuchos::FancyOStream &out, bool &success)
86 {
87  using Thyra::VectorBase;
88  using Thyra::VectorSpaceBase;
89  using Thyra::MultiVectorBase;
90 
91  RCP<const VectorSpaceBase<double> > vs;
92  RCP<const Epetra_Map> epetra_map;
93  createEpetraVsAndMap(g_localDim, outArg(vs), outArg(epetra_map), emptyProc);
94  const int numProcs = epetra_map->Comm().NumProc();
95 
96  if (emptyProc >= numProcs) {
97  out << "emptyProc = " << emptyProc << " >= numProcs = " << numProcs
98  << ": Skipping this test case!\n";
99  return;
100  }
101 
102  const RCP<Epetra_Vector> epetra_vec = rcp(new Epetra_Vector(*epetra_map));
103  const RCP<VectorBase<double> > thyra_vec = Thyra::create_Vector(epetra_vec, vs);
104  const RCP<Epetra_Vector> epetra_vec2 =
105  Thyra::get_Epetra_Vector(*epetra_map, thyra_vec);
106  TEST_EQUALITY(epetra_vec, epetra_vec2);
107 
108  const RCP<const Epetra_Vector> const_epetra_vec2 =
109  Thyra::get_Epetra_Vector(*epetra_map, thyra_vec.getConst());
110  TEST_EQUALITY(epetra_vec.getConst(), const_epetra_vec2);
111 
112 }
113 
114 
115 //
116 // Unit Tests
117 //
118 
119 
120 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, comm )
121 {
122  typedef Teuchos::GlobalMPISession GMPIS;
123  const RCP<const Epetra_Comm> epetra_comm = getEpetraComm();
124  TEST_EQUALITY(epetra_comm->NumProc(), GMPIS::getNProc());
125  TEST_EQUALITY(epetra_comm->MyPID(), GMPIS::getRank());
126  const RCP<const Comm<Ordinal> > comm = Thyra::create_Comm(epetra_comm);
127  TEST_EQUALITY(comm->getSize(), GMPIS::getNProc());
128  TEST_EQUALITY(comm->getRank(), GMPIS::getRank());
129  const RCP<const Epetra_Comm> epetra_comm2 = Thyra::get_Epetra_Comm(*comm);
130  TEST_EQUALITY(epetra_comm2->NumProc(), GMPIS::getNProc());
131  TEST_EQUALITY(epetra_comm2->MyPID(), GMPIS::getRank());
132 }
133 
134 
135 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester )
136 {
137  runVectorSpaceTesterTest(-1, out, success);
138 }
139 
140 
141 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester_empty_p0 )
142 {
143  runVectorSpaceTesterTest(0, out, success);
144 }
145 
146 
147 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester_empty_p1 )
148 {
149  runVectorSpaceTesterTest(1, out, success);
150 }
151 
152 
153 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, vectorSpaceTester_empty_pLast )
154 {
155  runVectorSpaceTesterTest(Teuchos::GlobalMPISession::getNProc()-1, out, success);
156 }
157 
158 
159 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, createAndViewVector_empty_p0 )
160 {
161  runCreateVectorUnitTest(0, out, success);
162 }
163 
164 
165 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, createAndViewVector_empty_p1 )
166 {
167  runCreateVectorUnitTest(1, out, success);
168 }
169 
170 
171 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, createAndViewVector_empty_pLast )
172 {
173  runCreateVectorUnitTest(Teuchos::GlobalMPISession::getNProc()-1, out, success);
174 }
175 
176 
177 // Test Thyra::create_MultiVector() (const and nonconst) and all views with empty processes
178 
179 
180 TEUCHOS_UNIT_TEST( EpetraThyraWrappers, get_Epetra_MultiVector_singleBlockProductVector )
181 {
182 using Thyra::VectorSpaceBase;
183 using Thyra::MultiVectorBase;
184 
185  RCP<const VectorSpaceBase<double> > vs;
186  RCP<const Epetra_Map> epetra_map;
187  createEpetraVsAndMap(g_localDim, outArg(vs), outArg(epetra_map));
188 
189  const RCP<const VectorSpaceBase<double> > pvs = Thyra::productVectorSpace(vs, 1);
190 
191  const RCP<MultiVectorBase<double> > pmv = Thyra::createMembers(pvs, 1);
192 
193  const double alpha = 3.5;
194  Thyra::assign<double>( pmv.ptr(), alpha );
195 
196  const RCP<Epetra_MultiVector> epetra_mv =
197  Thyra::get_Epetra_MultiVector(*epetra_map, pmv);
198 
199  const RCP<MultiVectorBase<double> > mv2 =
200  Thyra::create_MultiVector(epetra_mv, pvs);
201 
202  Thyra::testRelNormDiffErr<double>(
203  "*pmv->col(0)", *pmv->col(0),
204  "*mv2->col(0)", *mv2->col(0),
205  "max-error", 0.0,
206  "max-warning", 0.0,
207  &out
208  );
209 
210 }
211 
212 
213 } // 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.