Debian Codegraph @

packages/anasazi/test/LOBPCG/cxx_main_complex_lap.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 where the operator (A) is the 1D finite-differenced Laplacian
12 // operator. In this test, the eigenproblem is declared non-Hermitian even though
13 // the operator is Hermitian. This illustrated the bug found in bug 2840.
14
15 #include "AnasaziConfigDefs.hpp"
16 #include "AnasaziTypes.hpp"
17
18 #include "AnasaziBasicEigenproblem.hpp"
19 #include "AnasaziFactory.hpp"
20 #include "Teuchos_CommandLineProcessor.hpp"
21
22 #ifdef HAVE_MPI
23 #include <mpi.h>
24 #endif
25
26 // templated multivector and sparse matrix classes
27 #include "MyMultiVec.hpp"
28 #include "MyOperator.hpp"
29
30 using namespace Teuchos;
31
32 int main(int argc, char *argv[])
33 {
34 using std::cout;
35 using std::endl;
36
37 int MyPID = 0;
38 bool boolret;
39
40 #ifdef HAVE_MPI
41 // Initialize MPI
42 MPI_Init(&argc,&argv);
43 MPI_Comm_rank(MPI_COMM_WORLD, &MyPID);
44 #endif
45
46 bool testFailed;
47 bool verbose = false;
48 bool debug = false;
49 std::string which("LM");
50
51 CommandLineProcessor cmdp(false,true);
52 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
53 cmdp.setOption("debug","nodebug",&debug,"Print debugging information.");
54 cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM).");
55 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
56 #ifdef HAVE_MPI
57 MPI_Finalize();
58 #endif
59 return -1;
60 }
61
62 typedef std::complex<double> ST;
63 typedef ScalarTraits<ST> SCT;
64 typedef SCT::magnitudeType MT;
65 typedef Anasazi::MultiVec<ST> MV;
66 typedef Anasazi::Operator<ST> OP;
67 typedef Anasazi::MultiVecTraits<ST,MV> MVT;
68 typedef Anasazi::OperatorTraits<ST,MV,OP> OPT;
69 ST ONE = SCT::one();
70
71 if (verbose && MyPID == 0) {
72 cout << Anasazi::Anasazi_Version() << endl << endl;
73 }
74
75 // -- Set finite difference grid
76 int dim = 10;
77
78 // Build the problem matrix
79 RCP< const MyOperator<ST> > K
80 = rcp( new MyOperator<ST>(dim) );
81
82 // Create initial vectors
83 const int blockSize = 2;
84 RCP<MyMultiVec<ST> > ivec = rcp( new MyMultiVec<ST>(dim,blockSize) );
85 ivec->MvRandom();
86
87 // Create eigenproblem
88 const int nev = 1;
89 RCP<Anasazi::BasicEigenproblem<ST,MV,OP> > problem =
90 rcp( new Anasazi::BasicEigenproblem<ST,MV,OP>(K,ivec) );
91 //
92 // Inform the eigenproblem that the operator K is non-Hermitian (even when it truly is Hermitian)
93 problem->setHermitian(true);
94 //
95 // Set the number of eigenvalues requested
96 problem->setNEV( nev );
97 //
98 // Inform the eigenproblem that you are done passing it information
99 boolret = problem->setProblem();
100 if (boolret != true) {
101 if (verbose && MyPID == 0) {
102 cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl
103 << "End Result: TEST FAILED" << endl;
104 }
105 #ifdef HAVE_MPI
106 MPI_Finalize() ;
107 #endif
108 return -1;
109 }
110
111 // Set verbosity level
112 int verbosity = Anasazi::Errors + Anasazi::Warnings;
113 if (verbose) {
114 verbosity += Anasazi::IterationDetails + Anasazi::FinalSummary + Anasazi::TimingDetails;
115 }
116 if (debug) {
117 verbosity += Anasazi::Debug;
118 }
119
120 // Eigensolver parameters
121 int maxIters = 200;
122 MT tol = 1.0e-6;
123 //
124 // Create parameter list to pass into the solver manager
125 ParameterList MyPL;
126 MyPL.set( "Verbosity", verbosity );
127 MyPL.set( "Which", which );
128 MyPL.set( "Block Size", blockSize );
129 MyPL.set( "Maximum Iterations", maxIters );
130 MyPL.set( "Convergence Tolerance", tol );
131 MyPL.set( "Full Ortho", true );
132 //
133 // Create the solver manager
134 auto MySolverMgr = Anasazi::Factory::create("LOBPCG", problem, MyPL);
135
136 // Solve the problem to the specified tolerances or length
137 Anasazi::ReturnType returnCode = MySolverMgr->solve();
138 testFailed = false;
139 if (returnCode != Anasazi::Converged) {
140 testFailed = true;
141 }
142
143 // Get the eigenvalues and eigenvectors from the eigenproblem
144 Anasazi::Eigensolution<ST,MV> sol = problem->getSolution();
145 RCP<MV> evecs = sol.Evecs;
146 int numev = sol.numVecs;
147
148 if (numev > 0) {
149
150 std::ostringstream os;
151 os.setf(std::ios::scientific, std::ios::floatfield);
152 os.precision(6);
153
154 // Compute the direct residual
155 std::vector<MT> normV( numev );
156 SerialDenseMatrix<int,ST> T(numev,numev);
157 for (int i=0; i<numev; i++) {
158 T(i,i) = sol.Evals[i].realpart;
159 }
160 RCP<MV> Kvecs = MVT::Clone( *evecs, numev );
161
162 OPT::Apply( *K, *evecs, *Kvecs );
163
164 MVT::MvTimesMatAddMv( -ONE, *evecs, T, ONE, *Kvecs );
165 MVT::MvNorm( *Kvecs, normV );
166
167 os << "Direct residual norms computed in LOBPCGComplex_test.exe" << endl
168 << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual " << endl
169 << "----------------------------------------" << endl;
170 for (int i=0; i<numev; i++) {
171 if ( SCT::magnitude(sol.Evals[i].realpart) != SCT::zero() ) {
172 normV[i] = SCT::magnitude(normV[i]/sol.Evals[i].realpart);
173 }
174 os << std::setw(20) << sol.Evals[i].realpart << std::setw(20) << normV[i] << endl;
175 if ( normV[i] > tol ) {
176 testFailed = true;
177 }
178 }
179 if (verbose && MyPID==0) {
180 cout << endl << os.str() << endl;
181 }
182
183 }
184
185 #ifdef HAVE_MPI
186 MPI_Finalize() ;
187 #endif
188
189 if (testFailed) {
190 if (verbose && MyPID==0) {
191 cout << "End Result: TEST FAILED" << endl;
192 }
193 return -1;
194 }
195 //
196 // Default return value
197 //
198 if (verbose && MyPID==0) {
199 cout << "End Result: TEST PASSED" << endl;
200 }
201 return 0;
202
203 }