Debian Codegraph @

packages/anasazi/thyra/test/IRTR/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 SIRTR/IRTR solvers 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 IRTR 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 "AnasaziRTRSolMgr.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("LR");
67 bool skinny = true;
68
69 bool success = true;
70 try {
71
72 CommandLineProcessor cmdp(false,true);
73 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
74 cmdp.setOption("debug","nodebug",&debug,"Print debugging information.");
75 cmdp.setOption("sort",&which,"Targetted eigenvalues (SR or LR).");
76 cmdp.setOption("skinny","hefty",&skinny,"Use a skinny (low-mem) or hefty (higher-mem) implementation of IRTR.");
77 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
78 #ifdef HAVE_MPI
79 MPI_Finalize();
80 #endif
81 return -1;
82 }
83
84 #ifndef HAVE_EPETRA_THYRA
85 if (verbose && MyPid == 0) {
86 cout << "Please configure Anasazi with:" << endl;
87 cout << "--enable-epetra-thyra" << endl;
88 cout << "--enable-anasazi-thyra" << endl;
89 }
90 return 0;
91 #endif
92
93 typedef double ScalarType;
94 typedef ScalarTraits<ScalarType> SCT;
95 typedef SCT::magnitudeType MagnitudeType;
96 typedef Thyra::MultiVectorBase<double> MV;
97 typedef Thyra::LinearOpBase<double> OP;
98 typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT;
99 typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT;
100 const ScalarType ONE = SCT::one();
101
102 if (verbose && MyPID == 0) {
103 cout << Anasazi::Anasazi_Version() << endl << endl;
104 }
105
106 // Problem information
107 int space_dim = 1;
108 std::vector<double> brick_dim( space_dim );
109 brick_dim[0] = 1.0;
110 std::vector<int> elements( space_dim );
111 elements[0] = 100;
112
113 // Create problem
114 RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) );
115 //
116 // Get the stiffness and mass matrices
117 RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false );
118 RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false );
119 //
120 // Create the initial vectors
121 int blockSize = 5;
122 //
123 // Get a pointer to the Epetra_Map
124 RCP<const Epetra_Map> Map = rcp( &K->OperatorDomainMap(), false );
125 //
126 // create an epetra multivector
127 RCP<Epetra_MultiVector> ivec =
128 rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) );
129 ivec->Random();
130
131 // create a Thyra::VectorSpaceBase
132 RCP<const Thyra::VectorSpaceBase<double> > epetra_vs =
133 Thyra::create_VectorSpace(Map);
134
135 // create a MultiVectorBase (from the Epetra_MultiVector)
136 RCP<Thyra::MultiVectorBase<double> > thyra_ivec =
137 Thyra::create_MultiVector(ivec, epetra_vs);
138
139 // Create Thyra LinearOpBase objects from the Epetra_Operator objects
140 RCP<const Thyra::LinearOpBase<double> > thyra_K =
141 Thyra::epetraLinearOp(K);
142 RCP<const Thyra::LinearOpBase<double> > thyra_M =
143 Thyra::epetraLinearOp(M);
144
145 // Create eigenproblem
146 const int nev = 5;
147 RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem =
148 rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(thyra_K,thyra_M,thyra_ivec) );
149 //
150 // Inform the eigenproblem that the operator K is symmetric
151 problem->setHermitian(true);
152 //
153 // Set the number of eigenvalues requested
154 problem->setNEV( nev );
155 //
156 // Inform the eigenproblem that you are done passing it information
157 boolret = problem->setProblem();
158 if (boolret != true) {
159 if (verbose && MyPID == 0) {
160 cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl
161 << "End Result: TEST FAILED" << endl;
162 }
163 #ifdef HAVE_MPI
164 MPI_Finalize() ;
165 #endif
166 return -1;
167 }
168
169
170 // Set verbosity level
171 int verbosity = Anasazi::Errors + Anasazi::Warnings;
172 if (verbose) {
173 verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails;
174 }
175 if (debug) {
176 verbosity += Anasazi::Debug;
177 }
178
179
180 // Eigensolver parameters
181 int maxIters = 450;
182 MagnitudeType tol = 1.0e-6;
183 //
184 // Create parameter list to pass into the solver manager
185 ParameterList MyPL;
186 MyPL.set( "Verbosity", verbosity );
187 MyPL.set( "Which", which );
188 MyPL.set( "Block Size", blockSize );
189 MyPL.set( "Maximum Iterations", maxIters );
190 MyPL.set( "Convergence Tolerance", tol );
191 MyPL.set( "Skinny Solver", skinny);
192 //
193 // Create the solver manager
194 Anasazi::RTRSolMgr<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("Skinny Solver")->isUsed() == false) {
203 if (verbose && MyPID==0) {
204 cout << "Failure! Unused parameters: " << endl;
205 MyPL.unused(cout);
206 }
207 }
208
209
210 // Solve the problem to the specified tolerances or length
211 Anasazi::ReturnType returnCode = MySolverMan.solve();
212 if (returnCode != Anasazi::Converged) {
213 testFailed = true;
214 }
215
216 // Get the eigenvalues and eigenvectors from the eigenproblem
217 Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution();
218 std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals;
219 RCP<MV> evecs = sol.Evecs;
220 int numev = sol.numVecs;
221
222 if (numev > 0) {
223
224 std::ostringstream os;
225 os.setf(std::ios::scientific, std::ios::floatfield);
226 os.precision(6);
227
228 // Compute the direct residual
229 std::vector<ScalarType> normV( numev );
230 SerialDenseMatrix<int,ScalarType> T(numev,numev);
231 for (int i=0; i<numev; i++) {
232 T(i,i) = evals[i].realpart;
233 }
234 RCP<MV> Mvecs = MVT::Clone( *evecs, numev ),
235 Kvecs = MVT::Clone( *evecs, numev );
236 OPT::Apply( *thyra_K, *evecs, *Kvecs );
237 OPT::Apply( *thyra_M, *evecs, *Mvecs );
238 MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs );
239 // compute M-norm of residuals
240 OPT::Apply( *thyra_M, *Kvecs, *Mvecs );
241 MVT::MvDot( *Mvecs, *Kvecs, normV );
242
243 os << "Direct residual norms computed in LOBPCGThyra_test.exe" << endl
244 << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl
245 << "----------------------------------------" << endl;
246 for (int i=0; i<numev; i++) {
247 if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) {
248 normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) / evals[i].realpart );
249 }
250 else {
251 normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) );
252 }
253 os << std::setw(20) << evals[i].realpart << std::setw(20) << normV[i] << endl;
254 if ( normV[i] > tol ) {
255 testFailed = true;
256 }
257 }
258 if (verbose && MyPID==0) {
259 cout << endl << os.str() << endl;
260 }
261
262 }
263 }
264 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success);
265
266 #ifdef HAVE_MPI
267 MPI_Finalize() ;
268 #endif
269
270 if (testFailed || success==false) {
271 if (verbose && MyPID==0) {
272 cout << "End Result: TEST FAILED" << endl;
273 }
274 return -1;
275 }
276 //
277 // Default return value
278 //
279 if (verbose && MyPID==0) {
280 cout << "End Result: TEST PASSED" << endl;
281 }
282 return 0;
283
284 }