Debian Codegraph @

packages/anasazi/thyra/test/MVOPTester/cxx_main.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 // This test uses the MVOPTester.hpp functions to test the Anasazi adapters
11 // to Epetra and Thyra.
12 //
13
14 #include "Epetra_Map.h"
15 #include "Epetra_CrsMatrix.h"
16 #ifdef HAVE_MPI
17 #include "mpi.h"
18 #include "Epetra_MpiComm.h"
19 #endif
20 #ifndef __cplusplus
21 #define __cplusplus
22 #endif
23 #include "Epetra_Comm.h"
24 #include "Epetra_SerialComm.h"
25 #include "Teuchos_StandardCatchMacros.hpp"
26
27 #include "AnasaziConfigDefs.hpp"
28 #include "AnasaziMVOPTester.hpp"
29 #include "AnasaziEpetraAdapter.hpp"
30 #include "AnasaziBasicOutputManager.hpp"
31
32 #ifdef HAVE_EPETRA_THYRA
33 #include "AnasaziThyraAdapter.hpp"
34 #include "AnasaziThyraDebugAdapter.hpp"
35 #include "Thyra_EpetraThyraWrappers.hpp"
36 #include "Thyra_EpetraLinearOp.hpp"
37 #endif
38
39 int runTest(
40 int argc,
41 char* argv[],
42 #ifdef HAVE_MPI
43 Teuchos::RCP<Epetra_MpiComm>& Comm
44 #else
45 Teuchos::RCP<Epetra_SerialComm>& Comm
46 #endif
47 ){
48 using std::cout;
49 using std::endl;
50 using Teuchos::rcp_implicit_cast;
51
52 int i;
53 bool ierr, gerr;
54 gerr = true;
55
56 // number of global elements
57 int dim = 100;
58 int blockSize = 5;
59
60 bool verbose = false;
61 if (argc>1) {
62 if (argv[1][0]=='-' && argv[1][1]=='v') {
63 verbose = true;
64 }
65 }
66
67 // Create an output manager to handle the I/O from the solver
68 Teuchos::RCP<Anasazi::OutputManager<double> > MyOM = Teuchos::rcp( new Anasazi::BasicOutputManager<double>() );
69 if (verbose) {
70 MyOM->setVerbosity( Anasazi::Warnings );
71 }
72
73 #ifndef HAVE_EPETRA_THYRA
74 MyOM->stream(Anasazi::Warnings)
75 << "Please configure Anasazi with:" << endl
76 << "--enable-epetra-thyra" << endl
77 << "--enable-anasazi-thyra" << endl;
78 return -1;
79 #endif
80
81 // Construct a Map that puts approximately the same number of
82 // equations on each processor.
83 Teuchos::RCP<Epetra_Map> Map = Teuchos::rcp( new Epetra_Map(dim, 0, *Comm) );
84
85 // Get update list and number of local equations from newly created Map.
86 int NumMyElements = Map->NumMyElements();
87 std::vector<int> MyGlobalElements(NumMyElements);
88 Map->MyGlobalElements(&MyGlobalElements[0]);
89
90 // Create an integer vector NumNz that is used to build the Petra Matrix.
91 // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation
92 // on this processor
93 std::vector<int> NumNz(NumMyElements);
94
95 // We are building a tridiagonal matrix where each row has (-1 2 -1)
96 // So we need 2 off-diagonal terms (except for the first and last equation)
97 for (i=0; i<NumMyElements; i++) {
98 if (MyGlobalElements[i]==0 || MyGlobalElements[i] == dim-1) {
99 NumNz[i] = 2;
100 }
101 else {
102 NumNz[i] = 3;
103 }
104 }
105
106 // Create an Epetra_Matrix
107 Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *Map, &NumNz[0]) );
108
109 // Add rows one-at-a-time
110 // Need some vectors to help
111 // Off diagonal Values will always be -1
112 std::vector<double> Values(2);
113 Values[0] = -1.0; Values[1] = -1.0;
114 std::vector<int> Indices(2);
115 double two = 2.0;
116 int NumEntries;
117 for (i=0; i<NumMyElements; i++) {
118 if (MyGlobalElements[i]==0) {
119 Indices[0] = 1;
120 NumEntries = 1;
121 }
122 else if (MyGlobalElements[i] == dim-1) {
123 Indices[0] = dim-2;
124 NumEntries = 1;
125 }
126 else {
127 Indices[0] = MyGlobalElements[i]-1;
128 Indices[1] = MyGlobalElements[i]+1;
129 NumEntries = 2;
130 }
131 ierr = A->InsertGlobalValues(MyGlobalElements[i],NumEntries,&Values[0],&Indices[0]);
132 assert(ierr==0);
133 // Put in the diagonal entry
134 ierr = A->InsertGlobalValues(MyGlobalElements[i],1,&two,&MyGlobalElements[i]);
135 assert(ierr==0);
136 }
137
138 // Finish building the epetra matrix A
139 ierr = A->FillComplete();
140 assert(ierr==0);
141
142 // Create an Anasazi::EpetraSymOp from this Epetra_CrsMatrix
143 Teuchos::RCP<Anasazi::EpetraSymOp> op = Teuchos::rcp(new Anasazi::EpetraSymOp(A));
144
145 // Create an Epetra_MultiVector for an initial vector to start the solver.
146 // Note that this needs to have the same number of columns as the blocksize.
147 Teuchos::RCP<Anasazi::EpetraMultiVec> ivec = Teuchos::rcp( new Anasazi::EpetraMultiVec(*Map, blockSize) );
148 ivec->Random();
149
150 #ifdef HAVE_EPETRA_THYRA
151 typedef Thyra::MultiVectorBase<double> TMVB;
152 typedef Thyra::LinearOpBase<double> TLOB;
153 typedef Anasazi::MultiVec<double> AMV;
154 typedef Anasazi::Operator<double> AOP;
155 // create thyra objects from the epetra objects
156
157 // first, a Thyra::VectorSpaceBase
158 Teuchos::RCP<const Thyra::VectorSpaceBase<double> > epetra_vs =
159 Thyra::create_VectorSpace(Map);
160
161 // then, a MultiVectorBase (from the Epetra_MultiVector)
162 Teuchos::RCP<Thyra::MultiVectorBase<double> > thyra_ivec =
163 Thyra::create_MultiVector(rcp_implicit_cast<Epetra_MultiVector>(ivec),epetra_vs);
164
165 // then, a LinearOpBase (from the Epetra_CrsMatrix)
166 Teuchos::RCP<const Thyra::LinearOpBase<double> > thyra_op =
167 Thyra::epetraLinearOp(A);
168
169 // test the Thyra adapter multivector
170 ierr = Anasazi::TestMultiVecTraits<double,TMVB>(MyOM,thyra_ivec);
171 gerr |= ierr;
172 if (ierr) {
173 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter PASSED TestMultiVecTraits()" << endl;
174 }
175 else {
176 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter FAILED TestMultiVecTraits() ***" << endl << endl;
177 }
178
179 // test the Thyra adapter operator
180 ierr = Anasazi::TestOperatorTraits<double,TMVB,TLOB>(MyOM,thyra_ivec,thyra_op);
181 gerr |= ierr;
182 if (ierr) {
183 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter PASSED TestOperatorTraits()" << endl;
184 }
185 else {
186 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter FAILED TestOperatorTraits() ***" << endl << endl;
187 }
188
189 // test the Thyra debug adapter, ThyraMultiVec
190 Teuchos::RCP<AMV> thyraMultiVec = Teuchos::rcp( new Anasazi::ThyraMultiVec<double>( thyra_ivec ) );
191 ierr = Anasazi::TestMultiVecTraits<double,AMV>(MyOM,thyraMultiVec);
192 gerr |= ierr;
193 if (ierr) {
194 MyOM->stream(Anasazi::Warnings) << "*** ThyraDebugAdapter PASSED TestMultiVecTraits()" << endl;
195 }
196 else {
197 MyOM->stream(Anasazi::Warnings) << "*** ThyraDebugAdapter FAILED TestMultiVecTraits() ***" << endl << endl;
198 }
199
200 // test the Thyra debug adapter, ThyraOp
201 Teuchos::RCP<AOP> thyraOp = Teuchos::rcp( new Anasazi::ThyraOp<double>( thyra_op ) );
202 ierr = Anasazi::TestOperatorTraits<double,AMV,AOP>(MyOM,thyraMultiVec,thyraOp);
203 gerr |= ierr;
204 if (ierr) {
205 MyOM->stream(Anasazi::Warnings) << "*** ThyraDebugAdapter PASSED TestOperatorTraits()" << endl;
206 }
207 else {
208 MyOM->stream(Anasazi::Warnings) << "*** ThyraDebugAdapter FAILED TestOperatorTraits() ***" << endl << endl;
209 }
210
211 Teuchos::TimeMonitor::summarize(MyOM->stream(Anasazi::Warnings));
212 #endif
213
214 if (gerr == false) {
215 MyOM->print(Anasazi::Warnings,"End Result: TEST FAILED\n");
216 return -1;
217 }
218 //
219 // Default return value
220 //
221 MyOM->print(Anasazi::Warnings,"End Result: TEST PASSED\n");
222 return 0;
223 }
224
225
226 int main(int argc, char *argv[])
227 {
228
229 #ifdef HAVE_MPI
230 // Initialize MPI and setup an Epetra communicator
231 MPI_Init(&argc,&argv);
232 Teuchos::RCP<Epetra_MpiComm> Comm = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_WORLD) );
233 #else
234 // If we aren't using MPI, then setup a serial communicator.
235 Teuchos::RCP<Epetra_SerialComm> Comm = Teuchos::rcp( new Epetra_SerialComm() );
236 #endif
237
238 bool success = false;
239 bool verbose = false;
240 try {
241 int toReturn = runTest(argc, argv, Comm);
242 success = toReturn==0;
243 }
244 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
245
246 Comm = Teuchos::null;
247 #ifdef HAVE_MPI
248 MPI_Finalize();
249 #endif
250
251 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
252 }