Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraOperatorWrapper_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 
11 #include "Thyra_VectorStdOps.hpp"
13 #include "Thyra_EpetraLinearOp.hpp"
14 #include "Thyra_DefaultBlockedLinearOp.hpp"
15 #include "Thyra_ProductVectorBase.hpp"
16 #include "Thyra_SpmdVectorSpaceBase.hpp"
17 #include "Thyra_DetachedSpmdVectorView.hpp"
18 #include "Thyra_TestingTools.hpp"
19 #include "Thyra_LinearOpTester.hpp"
20 #include "Trilinos_Util_CrsMatrixGallery.h"
26 
27 #ifdef HAVE_MPI
28 # include "Epetra_MpiComm.h"
29 #else
30 # include "Epetra_SerialComm.h"
31 #endif
32 #include "Epetra_Vector.h"
33 #include "Epetra_CrsMatrix.h"
34 
36 
37 
38 namespace Thyra {
39 
40 
41 using Teuchos::null;
42 using Teuchos::RCP;
43 using Teuchos::rcp;
44 using Teuchos::rcp_dynamic_cast;
45 using Teuchos::inOutArg;
46 using Teuchos::as;
47 
48 
50 {
51 
52 #ifdef HAVE_MPI
53  Epetra_MpiComm comm(MPI_COMM_WORLD);
54 #else
55  Epetra_SerialComm comm;
56 #endif
57 
58  out << "\nRunning on " << comm.NumProc() << " processors\n";
59 
60  int nx = 39; // essentially random values
61  int ny = 53;
62 
63  out << "Using Trilinos_Util to create test matrices\n";
64 
65  // create some big blocks to play with
66  Trilinos_Util::CrsMatrixGallery FGallery("recirc_2d",comm,false); // CJ TODO FIXME: change for Epetra64
67  FGallery.Set("nx",nx);
68  FGallery.Set("ny",ny);
69  RCP<Epetra_CrsMatrix> F = rcp(FGallery.GetMatrix(),false);
70 
71  Trilinos_Util::CrsMatrixGallery CGallery("laplace_2d",comm,false); // CJ TODO FIXME: change for Epetra64
72  CGallery.Set("nx",nx);
73  CGallery.Set("ny",ny);
74  RCP<Epetra_CrsMatrix> C = rcp(CGallery.GetMatrix(),false);
75 
76  Trilinos_Util::CrsMatrixGallery BGallery("diag",comm,false); // CJ TODO FIXME: change for Epetra64
77  BGallery.Set("nx",nx*ny);
78  BGallery.Set("a",5.0);
79  RCP<Epetra_CrsMatrix> B = rcp(BGallery.GetMatrix(),false);
80 
81  Trilinos_Util::CrsMatrixGallery BtGallery("diag",comm,false); // CJ TODO FIXME: change for Epetra64
82  BtGallery.Set("nx",nx*ny);
83  BtGallery.Set("a",3.0);
84  RCP<Epetra_CrsMatrix> Bt = rcp(BtGallery.GetMatrix(),false);
85 
86  // load'em up in a thyra operator
87  out << "Building block2x2 Thyra matrix ... wrapping in EpetraOperatorWrapper\n";
89  Thyra::block2x2<double>(
90  Thyra::epetraLinearOp(F),
91  Thyra::epetraLinearOp(Bt),
92  Thyra::epetraLinearOp(B),
93  Thyra::epetraLinearOp(C),
94  "A"
95  );
96 
97  const RCP<Thyra::EpetraOperatorWrapper> epetra_A =
99 
100  // begin the tests!
101  const Epetra_Map & rangeMap = epetra_A->OperatorRangeMap();
102  const Epetra_Map & domainMap = epetra_A->OperatorDomainMap();
103 
104  // check to see that the number of global elements is correct
105  TEST_EQUALITY(rangeMap.NumGlobalElements(), 2*nx*ny);
106  TEST_EQUALITY(domainMap.NumGlobalElements(), 2*nx*ny);
107 
108  // largest global ID should be one less then the # of elements
109  TEST_EQUALITY(rangeMap.NumGlobalElements()-1, rangeMap.MaxAllGID());
110  TEST_EQUALITY(domainMap.NumGlobalElements()-1, domainMap.MaxAllGID());
111 
112  // create a vector to test: copyThyraIntoEpetra
113  {
114  const RCP<VectorBase<double> > tv = Thyra::createMember(A->domain());
115  Thyra::randomize(-100.0, 100.0, tv.ptr());
116  const RCP<const VectorBase<double> > tv_0 =
117  Thyra::productVectorBase<double>(tv)->getVectorBlock(0);
118  const RCP<const VectorBase<double> > tv_1 =
119  Thyra::productVectorBase<double>(tv)->getVectorBlock(1);
120  const Thyra::ConstDetachedSpmdVectorView<double> vv_0(tv_0);
121  const Thyra::ConstDetachedSpmdVectorView<double> vv_1(tv_1);
122 
123  int off_0 = vv_0.globalOffset();
124  int off_1 = vv_1.globalOffset();
125 
126  // create its Epetra counter part
127  Epetra_Vector ev(epetra_A->OperatorDomainMap());
128  epetra_A->copyThyraIntoEpetra(*tv, ev);
129 
130  // compare handle_tv to ev!
131  TEST_EQUALITY(tv->space()->dim(), as<Ordinal>(ev.GlobalLength()));
132  const int numMyElements = domainMap.NumMyElements();
133  double tval = 0.0;
134  for(int i=0; i < numMyElements; i++) {
135  int gid = domainMap.GID(i);
136  if(gid<nx*ny)
137  tval = vv_0[gid-off_0];
138  else
139  tval = vv_1[gid-off_1-nx*ny];
140  TEST_EQUALITY(ev[i], tval);
141  }
142  }
143 
144  // create a vector to test: copyEpetraIntoThyra
145  {
146  // create an Epetra vector
147  Epetra_Vector ev(epetra_A->OperatorDomainMap());
148  ev.Random();
149 
150  // create its thyra counterpart
151  const RCP<VectorBase<double> > tv = Thyra::createMember(A->domain());
152  const RCP<const VectorBase<double> > tv_0 =
153  Thyra::productVectorBase<double>(tv)->getVectorBlock(0);
154  const RCP<const VectorBase<double> > tv_1 =
155  Thyra::productVectorBase<double>(tv)->getVectorBlock(1);
156  const Thyra::ConstDetachedSpmdVectorView<double> vv_0(tv_0);
157  const Thyra::ConstDetachedSpmdVectorView<double> vv_1(tv_1);
158 
159  int off_0 = rcp_dynamic_cast<const Thyra::SpmdVectorSpaceBase<double> >(
160  tv_0->space())->localOffset();
161  int off_1 = rcp_dynamic_cast<const Thyra::SpmdVectorSpaceBase<double> >(
162  tv_1->space())->localOffset();
163 
164  epetra_A->copyEpetraIntoThyra(ev, tv.ptr());
165 
166  // compare tv to ev!
167  TEST_EQUALITY(tv->space()->dim(), as<Ordinal>(ev.GlobalLength()));
168  int numMyElements = domainMap.NumMyElements();
169  double tval = 0.0;
170  for(int i=0;i<numMyElements;i++) {
171  int gid = domainMap.GID(i);
172  if(gid<nx*ny)
173  tval = vv_0[gid-off_0];
174  else
175  tval = vv_1[gid-off_1-nx*ny];
176  TEST_EQUALITY(ev[i], tval);
177  }
178  }
179 
180  // Test using Thyra::LinearOpTester
181  const RCP<const LinearOpBase<double> > thyraEpetraOp = epetraLinearOp(epetra_A);
182  LinearOpTester<double> linearOpTester;
183  linearOpTester.show_all_tests(true);
184  const bool checkResult = linearOpTester.check(*thyraEpetraOp, inOutArg(out));
185  TEST_ASSERT(checkResult);
186 
187 }
188 
189 
190 } // namespace Thyra
#define TEST_ASSERT(v1)
TEUCHOS_UNIT_TEST(EpetraOperatorWrapper, basic)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
Implements the Epetra_Operator interface with a Thyra LinearOperator.
TypeTo as(const TypeFrom &t)
#define TEST_EQUALITY(v1, v2)