Debian Codegraph @

packages/anasazi/thyra/test/LOBPCG/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 is for the LOBPCG solver using the Thyra interface
11 // The Thyra objects will be extracted from Epetra objects using the
12 // Epetra-Thyra interface.
13 // Therefore, this test should yield identical results compared against
14 // the Epetra-only LOBPCG solver test.
15 //
16 #include "AnasaziConfigDefs.hpp"
17 #include "AnasaziTypes.hpp"
18
19 #include "AnasaziEpetraAdapter.hpp"
20 #include "Epetra_CrsMatrix.h"
21
22 #include "AnasaziBasicEigenproblem.hpp"
23 #include "AnasaziLOBPCGSolMgr.hpp"
24 #include "Teuchos_CommandLineProcessor.hpp"
25 #include "Teuchos_StandardCatchMacros.hpp"
26 #ifdef HAVE_MPI
27 #include "Epetra_MpiComm.h"
28 #include <mpi.h>
29 #else
30 #include "Epetra_SerialComm.h"
31 #endif
32
33 #ifdef HAVE_EPETRA_THYRA
34 #include "AnasaziThyraAdapter.hpp"
35 #include "Thyra_EpetraThyraWrappers.hpp"
36 #include "Thyra_EpetraLinearOp.hpp"
37 #endif
38
39 #include "ModeLaplace1DQ1.h"
40
41 using namespace Teuchos;
42
43 int main(int argc, char *argv[])
44 {
45
46 using Teuchos::rcp_implicit_cast;
47 using std::cout;
48 using std::endl;
49
50 bool boolret;
51 int MyPID;
52
53 #ifdef HAVE_MPI
54 // Initialize MPI
55 MPI_Init(&argc,&argv);
56 Epetra_MpiComm Comm(MPI_COMM_WORLD);
57 #else
58 Epetra_SerialComm Comm;
59 #endif
60 MyPID = Comm.MyPID();
61
62 bool testFailed = false;
63 bool verbose = false;
64 bool debug = false;
65 std::string filename("mhd1280b.cua");
66 std::string which("LM");
67
68 bool success = true;
69 try {
70
71 CommandLineProcessor cmdp(false,true);
72 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
73 cmdp.setOption("debug","nodebug",&debug,"Print debugging information.");
74 cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM).");
75 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
76 #ifdef HAVE_MPI
77 MPI_Finalize();
78 #endif
79 return -1;
80 }
81
82 #ifndef HAVE_EPETRA_THYRA
83 if (verbose && MyPid == 0) {
84 cout << "Please configure Anasazi with:" << endl;
85 cout << "--enable-epetra-thyra" << endl;
86 cout << "--enable-anasazi-thyra" << endl;
87 }
88 return 0;
89 #endif
90
91 typedef double ScalarType;
92 typedef ScalarTraits<ScalarType> SCT;
93 typedef SCT::magnitudeType MagnitudeType;
94 typedef Thyra::MultiVectorBase<double> MV;
95 typedef Thyra::LinearOpBase<double> OP;
96 typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT;
97 typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT;
98 const ScalarType ONE = SCT::one();
99
100 if (verbose && MyPID == 0) {
101 cout << Anasazi::Anasazi_Version() << endl << endl;
102 }
103
104 // Problem information
105 int space_dim = 1;
106 std::vector<double> brick_dim( space_dim );
107 brick_dim[0] = 1.0;
108 std::vector<int> elements( space_dim );
109 elements[0] = 100;
110
111 // Create problem
112 RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) );
113 //
114 // Get the stiffness and mass matrices
115 RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false );
116 RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false );
117 //
118 // Create the initial vectors
119 int blockSize = 5;
120 //
121 // Get a pointer to the Epetra_Map
122 RCP<const Epetra_Map> Map = rcp( &K->OperatorDomainMap(), false );
123 //
124 // create an epetra multivector
125 RCP<Epetra_MultiVector> ivec =
126 rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) );
127 ivec->Random();
128
129 // create a Thyra::VectorSpaceBase
130 RCP<const Thyra::VectorSpaceBase<double> > epetra_vs =
131 Thyra::create_VectorSpace(Map);
132
133 // create a MultiVectorBase (from the Epetra_MultiVector)
134 RCP<Thyra::MultiVectorBase<double> > thyra_ivec =
135 Thyra::create_MultiVector(ivec, epetra_vs);
136
137 // Create Thyra LinearOpBase objects from the Epetra_Operator objects
138 RCP<const Thyra::LinearOpBase<double> > thyra_K =
139 Thyra::epetraLinearOp(K);
140 RCP<const Thyra::LinearOpBase<double> > thyra_M =
141 Thyra::epetraLinearOp(M);
142
143 // Create eigenproblem
144 const int nev = 5;
145 RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem =
146 rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(thyra_K,thyra_M,thyra_ivec) );
147 //
148 // Inform the eigenproblem that the operator K is symmetric
149 problem->setHermitian(true);
150 //
151 // Set the number of eigenvalues requested
152 problem->setNEV( nev );
153 //
154 // Inform the eigenproblem that you are done passing it information
155 boolret = problem->setProblem();
156 if (boolret != true) {
157 if (verbose && MyPID == 0) {
158 cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl
159 << "End Result: TEST FAILED" << endl;
160 }
161 #ifdef HAVE_MPI
162 MPI_Finalize() ;
163 #endif
164 return -1;
165 }
166
167
168 // Set verbosity level
169 int verbosity = Anasazi::Errors + Anasazi::Warnings;
170 if (verbose) {
171 verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails;
172 }
173 if (debug) {
174 verbosity += Anasazi::Debug;
175 }
176
177
178 // Eigensolver parameters
179 int maxIters = 450;
180 MagnitudeType tol = 1.0e-6;
181 //
182 // Create parameter list to pass into the solver manager
183 ParameterList MyPL;
184 MyPL.set( "Verbosity", verbosity );
185 MyPL.set( "Which", which );
186 MyPL.set( "Block Size", blockSize );
187 MyPL.set( "Maximum Iterations", maxIters );
188 MyPL.set( "Convergence Tolerance", tol );
189 MyPL.set( "Use Locking", true );
190 MyPL.set( "Locking Tolerance", tol/10 );
191 MyPL.set( "Full Ortho", true );
192 //
193 // Create the solver manager
194 Anasazi::LOBPCGSolMgr<ScalarType,MV,OP> MySolverMan(problem, MyPL);
195 //
196 // Check that the parameters were all consumed
197 if (MyPL.getEntryPtr("Verbosity")->isUsed() == false ||
198 MyPL.getEntryPtr("Which")->isUsed() == false ||
199 MyPL.getEntryPtr("Block Size")->isUsed() == false ||
200 MyPL.getEntryPtr("Maximum Iterations")->isUsed() == false ||
201 MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false ||
202 MyPL.getEntryPtr("Use Locking")->isUsed() == false ||
203 MyPL.getEntryPtr("Locking Tolerance")->isUsed() == false ||
204 MyPL.getEntryPtr("Full Ortho")->isUsed() == false) {
205 if (verbose && MyPID==0) {
206 cout << "Failure! Unused parameters: " << endl;
207 MyPL.unused(cout);
208 }
209 }
210
211
212 // Solve the problem to the specified tolerances or length
213 Anasazi::ReturnType returnCode = MySolverMan.solve();
214 if (returnCode != Anasazi::Converged) {
215 testFailed = true;
216 }
217
218 // Get the eigenvalues and eigenvectors from the eigenproblem
219 Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution();
220 std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals;
221 RCP<MV> evecs = sol.Evecs;
222 int numev = sol.numVecs;
223
224 if (numev > 0) {
225
226 std::ostringstream os;
227 os.setf(std::ios::scientific, std::ios::floatfield);
228 os.precision(6);
229
230 // Compute the direct residual
231 std::vector<ScalarType> normV( numev );
232 SerialDenseMatrix<int,ScalarType> T(numev,numev);
233 for (int i=0; i<numev; i++) {
234 T(i,i) = evals[i].realpart;
235 }
236 RCP<MV> Mvecs = MVT::Clone( *evecs, numev ),
237 Kvecs = MVT::Clone( *evecs, numev );
238 OPT::Apply( *thyra_K, *evecs, *Kvecs );
239 OPT::Apply( *thyra_M, *evecs, *Mvecs );
240 MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs );
241 // compute M-norm of residuals
242 OPT::Apply( *thyra_M, *Kvecs, *Mvecs );
243 MVT::MvDot( *Mvecs, *Kvecs, normV );
244
245 os << "Direct residual norms computed in LOBPCGThyra_test.exe" << endl
246 << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl
247 << "----------------------------------------" << endl;
248 for (int i=0; i<numev; i++) {
249 if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) {
250 normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) / evals[i].realpart );
251 }
252 else {
253 normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) );
254 }
255 os << std::setw(20) << evals[i].realpart << std::setw(20) << normV[i] << endl;
256 if ( normV[i] > tol ) {
257 testFailed = true;
258 }
259 }
260 if (verbose && MyPID==0) {
261 cout << endl << os.str() << endl;
262 }
263
264 }
265 }
266 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success);
267
268 #ifdef HAVE_MPI
269 MPI_Finalize() ;
270 #endif
271
272 if (testFailed || success==false) {
273 if (verbose && MyPID==0) {
274 cout << "End Result: TEST FAILED" << endl;
275 }
276 return -1;
277 }
278 //
279 // Default return value
280 //
281 if (verbose && MyPID==0) {
282 cout << "End Result: TEST PASSED" << endl;
283 }
284 return 0;
285
286 }