Debian Codegraph @

packages/anasazi/thyra/example/MVOPTester/MVOPTesterEx.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
26 #include "AnasaziConfigDefs.hpp"
27 #include "AnasaziMVOPTester.hpp"
28 #include "AnasaziEpetraAdapter.hpp"
29 #include "AnasaziBasicOutputManager.hpp"
30
31 #ifdef HAVE_EPETRA_THYRA
32 #include "AnasaziThyraAdapter.hpp"
33 #include "Thyra_EpetraThyraWrappers.hpp"
34 #include "Thyra_EpetraLinearOp.hpp"
35 #endif
36
37 int main(int argc, char *argv[])
38 {
39 int i;
40 bool ierr, gerr;
41 gerr = true;
42
43 #ifdef HAVE_MPI
44 // Initialize MPI and setup an Epetra communicator
45 MPI_Init(&argc,&argv);
46 Teuchos::RCP<Epetra_MpiComm> Comm = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_WORLD) );
47 #else
48 // If we aren't using MPI, then setup a serial communicator.
49 Teuchos::RCP<Epetra_SerialComm> Comm = Teuchos::rcp( new Epetra_SerialComm() );
50 #endif
51
52 // number of global elements
53 const int dim = 100;
54 const int blockSize = 5;
55
56 bool verbose = false;
57 if (argc>1) {
58 if (argv[1][0]=='-' && argv[1][1]=='v') {
59 verbose = true;
60 }
61 }
62
63 // Create an output manager to handle the I/O from the solver
64 Teuchos::RCP<Anasazi::OutputManager<double> > MyOM = Teuchos::rcp( new Anasazi::BasicOutputManager<double>() );
65 if (verbose) {
66 MyOM->setVerbosity( Anasazi::Warnings );
67 }
68
69 #ifndef HAVE_EPETRA_THYRA
70 MyOM->stream(Anasazi::Warnings)
71 << "Please configure Anasazi with:" << std::endl
72 << "--enable-epetra-thyra" << std::endl
73 << "--enable-anasazi-thyra" << std::endl;
74 #ifdef HAVE_MPI
75 MPI_Finalize();
76 #endif
77 return -1;
78 #endif
79
80 // Construct a Map that puts approximately the same number of
81 // equations on each processor.
82 Teuchos::RCP<Epetra_Map> Map = Teuchos::rcp( new Epetra_Map(dim, 0, *Comm) );
83
84 // Get update list and number of local equations from newly created Map.
85 int NumMyElements = Map->NumMyElements();
86 std::vector<int> MyGlobalElements(NumMyElements);
87 Map->MyGlobalElements(&MyGlobalElements[0]);
88
89 // Create an integer vector NumNz that is used to build the Petra Matrix.
90 // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation
91 // on this processor
92 std::vector<int> NumNz(NumMyElements);
93
94 // We are building a tridiagonal matrix where each row has (-1 2 -1)
95 // So we need 2 off-diagonal terms (except for the first and last equation)
96 for (i=0; i<NumMyElements; i++) {
97 if (MyGlobalElements[i]==0 || MyGlobalElements[i] == dim-1) {
98 NumNz[i] = 2;
99 }
100 else {
101 NumNz[i] = 3;
102 }
103 }
104
105 // Create an Epetra_Matrix
106 Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *Map, &NumNz[0]) );
107
108 // Add rows one-at-a-time
109 // Need some vectors to help
110 // Off diagonal Values will always be -1
111 std::vector<double> Values(2);
112 Values[0] = -1.0; Values[1] = -1.0;
113 std::vector<int> Indices(2);
114 double two = 2.0;
115 int NumEntries;
116 for (i=0; i<NumMyElements; i++) {
117 if (MyGlobalElements[i]==0) {
118 Indices[0] = 1;
119 NumEntries = 1;
120 }
121 else if (MyGlobalElements[i] == dim-1) {
122 Indices[0] = dim-2;
123 NumEntries = 1;
124 }
125 else {
126 Indices[0] = MyGlobalElements[i]-1;
127 Indices[1] = MyGlobalElements[i]+1;
128 NumEntries = 2;
129 }
130 ierr = A->InsertGlobalValues(MyGlobalElements[i],NumEntries,&Values[0],&Indices[0]);
131 assert(ierr==0);
132 // Put in the diagonal entry
133 ierr = A->InsertGlobalValues(MyGlobalElements[i],1,&two,&MyGlobalElements[i]);
134 assert(ierr==0);
135 }
136
137 // Finish building the epetra matrix A
138 ierr = A->FillComplete();
139 assert(ierr==0);
140
141 #ifdef HAVE_EPETRA_THYRA
142 typedef Thyra::MultiVectorBase<double> TMVB;
143 typedef Thyra::LinearOpBase<double> TLOB;
144
145 // first, create a Thyra::VectorSpaceBase from an Epetra_Map using the Epetra-Thyra wrappers
146 Teuchos::RCP<const Thyra::VectorSpaceBase<double> > space = Thyra::create_VectorSpace(Map);
147
148 // then, create a Thyra::MultiVectorBase from the Thyra::VectorSpaceBase using Thyra creational functions
149 Teuchos::RCP<Thyra::MultiVectorBase<double> > thyra_ivec = Thyra::createMembers(space,blockSize);
150
151 // then, create a Thyra::LinearOpBase from the Epetra_CrsMatrix using the Epetra-Thyra wrappers
152 Teuchos::RCP<const Thyra::LinearOpBase<double> > thyra_op = Thyra::epetraLinearOp(A);
153
154 // test the Thyra multivector adapter
155 ierr = Anasazi::TestMultiVecTraits<double,TMVB>(MyOM,thyra_ivec);
156 gerr |= ierr;
157 if (ierr) {
158 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter PASSED TestMultiVecTraits()" << std::endl;
159 }
160 else {
161 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter FAILED TestMultiVecTraits() ***" << std::endl << std::endl;
162 }
163
164 // test the Thyra operator adapter
165 ierr = Anasazi::TestOperatorTraits<double,TMVB,TLOB>(MyOM,thyra_ivec,thyra_op);
166 gerr |= ierr;
167 if (ierr) {
168 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter PASSED TestOperatorTraits()" << std::endl;
169 }
170 else {
171 MyOM->stream(Anasazi::Warnings) << "*** ThyraAdapter FAILED TestOperatorTraits() ***" << std::endl << std::endl;
172 }
173 #endif
174
175 #ifdef HAVE_MPI
176 MPI_Finalize();
177 #endif
178
179 if (gerr == false) {
180 MyOM->print(Anasazi::Warnings,"End Result: TEST FAILED\n");
181 return -1;
182 }
183 //
184 // Default return value
185 //
186 MyOM->print(Anasazi::Warnings,"End Result: TEST PASSED\n");
187 return 0;
188
189 }