Debian Codegraph @

packages/anasazi/test/LOBPCG/cxx_main_complex.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 LOBPCG solving a standard (Ax=xl) complex Hermitian
11 // eigenvalue problem, using the LOBPCGSolMgr solver manager.
12 //
13 // The matrix used is from MatrixMarket:
14 // Name: MHD1280B: Alfven Spectra in Magnetohydrodynamics
15 // Source: Source: A. Booten, M.N. Kooper, H.A. van der Vorst, S. Poedts and J.P. Goedbloed University of Utrecht, the Netherlands
16 // Discipline: Plasma physics
17 // URL: http://math.nist.gov/MatrixMarket/data/NEP/mhd/mhd1280b.html
18 // Size: 1280 x 1280
19 // NNZ: 22778 entries
20
21 #include "AnasaziConfigDefs.hpp"
22 #include "AnasaziTypes.hpp"
23
24 #include "AnasaziBasicEigenproblem.hpp"
25 #include "AnasaziBasicOutputManager.hpp"
26 #include "AnasaziFactory.hpp"
27 #include "Teuchos_CommandLineProcessor.hpp"
28
29 #ifdef HAVE_MPI
30 #include <mpi.h>
31 #endif
32
33 // I/O for Harwell-Boeing files
34 #ifdef HAVE_ANASAZI_TRIUTILS
35 #include "Trilinos_Util_iohb.h"
36 #endif
37
38 // templated multivector and sparse matrix classes
39 #include "MyMultiVec.hpp"
40 #include "MyBetterOperator.hpp"
41
42 using namespace Teuchos;
43
44 int main(int argc, char *argv[])
45 {
46 using std::cout;
47 using std::endl;
48
49 int info = 0;
50 bool boolret;
51 int MyPID;
52
53 #ifdef HAVE_MPI
54 // Initialize MPI
55 MPI_Init(&argc,&argv);
56 MPI_Comm_rank( MPI_COMM_WORLD, &MyPID );
57 #else
58 MyPID = 0;
59 #endif
60
61 bool testFailed;
62 bool verbose = false;
63 bool debug = false;
64 std::string filename("mhd1280b.cua");
65 std::string which("LM");
66
67 CommandLineProcessor cmdp(false,true);
68 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
69 cmdp.setOption("debug","nodebug",&debug,"Print debugging information.");
70 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
71 cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM).");
72 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
73 #ifdef HAVE_MPI
74 MPI_Finalize();
75 #endif
76 return -1;
77 }
78
79 #ifndef HAVE_ANASAZI_TRIUTILS
80 cout << "This test requires Triutils. Please configure with --enable-triutils." << endl;
81 #ifdef HAVE_MPI
82 MPI_Finalize() ;
83 #endif
84 if (verbose && MyPID == 0) {
85 cout << "End Result: TEST FAILED" << endl;
86 }
87 return -1;
88 #endif
89
90 typedef std::complex<double> ScalarType;
91 typedef ScalarTraits<ScalarType> SCT;
92 typedef SCT::magnitudeType MagnitudeType;
93 typedef Anasazi::MultiVec<ScalarType> MV;
94 typedef Anasazi::Operator<ScalarType> OP;
95 typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT;
96 typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT;
97 const ScalarType ONE = SCT::one();
98
99 if (verbose && MyPID == 0) {
100 cout << Anasazi::Anasazi_Version() << endl << endl;
101 }
102
103 // Problem information
104 int dim,dim2,nnz;
105 double *dvals;
106 int *colptr,*rowind;
107 nnz = -1;
108 info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
109 &colptr,&rowind,&dvals);
110 if (info == 0 || nnz < 0) {
111 if (verbose && MyPID == 0) {
112 cout << "Error reading '" << filename << "'" << endl
113 << "End Result: TEST FAILED" << endl;
114 }
115 #ifdef HAVE_MPI
116 MPI_Finalize();
117 #endif
118 return -1;
119 }
120 // Convert interleaved doubles to complex values
121 std::vector<ScalarType> cvals(nnz);
122 for (int ii=0; ii<nnz; ii++) {
123 cvals[ii] = ScalarType(dvals[ii*2],dvals[ii*2+1]);
124 }
125 // Build the problem matrix
126 RCP< const MyBetterOperator<ScalarType> > K
127 = rcp( new MyBetterOperator<ScalarType>(dim,colptr,nnz,rowind,&cvals[0]) );
128
129 // Create initial vectors
130 int blockSize = 5;
131 RCP<MyMultiVec<ScalarType> > ivec = rcp( new MyMultiVec<ScalarType>(dim,blockSize) );
132 ivec->MvRandom();
133
134 // Create eigenproblem
135 const int nev = 4;
136 RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem =
137 rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(K,ivec) );
138 //
139 // Inform the eigenproblem that the operator K is symmetric
140 problem->setHermitian(true);
141 //
142 // Set the number of eigenvalues requested
143 problem->setNEV( nev );
144 //
145 // Inform the eigenproblem that you are done passing it information
146 boolret = problem->setProblem();
147 if (boolret != true) {
148 if (verbose && MyPID == 0) {
149 cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl
150 << "End Result: TEST FAILED" << endl;
151 }
152 #ifdef HAVE_MPI
153 MPI_Finalize() ;
154 #endif
155 return -1;
156 }
157
158 // Get a formatted output stream
159 RCP<Anasazi::OutputManager<ScalarType> > MyOM = rcp( new Anasazi::BasicOutputManager<ScalarType>() );
160 RCP<Teuchos::FancyOStream> fos = MyOM->getFancyOStream();
161
162 // Set verbosity level
163 int verbosity = Anasazi::Errors + Anasazi::Warnings;
164 if (verbose) {
165 verbosity += Anasazi::IterationDetails + Anasazi::FinalSummary + Anasazi::TimingDetails;
166 }
167 if (debug) {
168 verbosity += Anasazi::Debug;
169 }
170
171
172 // Eigensolver parameters
173 int maxIters = 450;
174 MagnitudeType tol = 1.0e-6;
175 //
176 // Create parameter list to pass into the solver manager
177 ParameterList MyPL;
178 MyPL.set( "Verbosity", verbosity );
179 MyPL.set( "Output Stream", fos );
180 MyPL.set( "Which", which );
181 MyPL.set( "Block Size", blockSize );
182 MyPL.set( "Maximum Iterations", maxIters );
183 MyPL.set( "Convergence Tolerance", tol );
184 MyPL.set( "Use Locking", true );
185 MyPL.set( "Locking Tolerance", tol/10 );
186 MyPL.set( "Full Ortho", true );
187 //
188 // Create the solver manager
189 auto MySolverMan = Anasazi::Factory::create("LOBPCG", problem, MyPL);
190
191 // Solve the problem to the specified tolerances or length
192 Anasazi::ReturnType returnCode = MySolverMan->solve();
193 testFailed = false;
194 if (returnCode != Anasazi::Converged) {
195 testFailed = true;
196 }
197
198 // Get the eigenvalues and eigenvectors from the eigenproblem
199 Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution();
200 RCP<MV> evecs = sol.Evecs;
201 int numev = sol.numVecs;
202
203 if (numev > 0) {
204
205 std::ostringstream os;
206 os.setf(std::ios::scientific, std::ios::floatfield);
207 os.precision(6);
208
209 // Compute the direct residual
210 std::vector<MagnitudeType> normV( numev );
211 SerialDenseMatrix<int,ScalarType> T(numev,numev);
212 for (int i=0; i<numev; i++) {
213 T(i,i) = sol.Evals[i].realpart;
214 }
215 RCP<MV> Kvecs = MVT::Clone( *evecs, numev );
216
217 OPT::Apply( *K, *evecs, *Kvecs );
218
219 MVT::MvTimesMatAddMv( -ONE, *evecs, T, ONE, *Kvecs );
220 MVT::MvNorm( *Kvecs, normV );
221
222 os << "Direct residual norms computed in LOBPCGComplex_test.exe" << endl
223 << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl
224 << "----------------------------------------" << endl;
225 for (int i=0; i<numev; i++) {
226 if ( SCT::magnitude(sol.Evals[i].realpart) != SCT::zero() ) {
227 normV[i] = SCT::magnitude(normV[i]/sol.Evals[i].realpart);
228 }
229 os << std::setw(20) << sol.Evals[i].realpart << std::setw(20) << normV[i] << endl;
230 if ( normV[i] > tol ) {
231 testFailed = true;
232 }
233 }
234 if (verbose && MyPID==0) {
235 cout << endl << os.str() << endl;
236 }
237
238 }
239
240 #ifdef HAVE_MPI
241 MPI_Finalize() ;
242 #endif
243
244 // Clean up.
245 std::free( dvals );
246 std::free( colptr );
247 std::free( rowind );
248
249 if (testFailed) {
250 if (verbose && MyPID==0) {
251 cout << "End Result: TEST FAILED" << endl;
252 }
253 return -1;
254 }
255 //
256 // Default return value
257 //
258 if (verbose && MyPID==0) {
259 cout << "End Result: TEST PASSED" << endl;
260 }
261 return 0;
262
263 }