Debian Codegraph @

packages/anasazi/thyra/test/GeneralizedDavidson/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 GeneralizedDavidson solver using the Thyra interface
11 // The Thyra objects will be extracted from Epetra objects using the
12 // Epetra-Thyra interface.
13 // A few steps of BlockPCG is used as a preconditioner.
14 //
15 #include "AnasaziConfigDefs.hpp"
16 #include "AnasaziTypes.hpp"
17
18 #include "AnasaziEpetraAdapter.hpp"
19 #include "Epetra_CrsMatrix.h"
20 #include "Epetra_InvOperator.h"
21 #include "Epetra_Vector.h"
22
23 #include "AnasaziBasicSort.hpp"
24 #include "AnasaziBasicEigenproblem.hpp"
25 #include "AnasaziGeneralizedDavidsonSolMgr.hpp"
26 #include "Teuchos_CommandLineProcessor.hpp"
27 #ifdef HAVE_MPI
28 #include "Epetra_MpiComm.h"
29 #include <mpi.h>
30 #else
31 #include "Epetra_SerialComm.h"
32 #endif
33
34 #include "Teuchos_StandardCatchMacros.hpp"
35
36 #ifdef HAVE_EPETRA_THYRA
37 #include "AnasaziThyraAdapter.hpp"
38 #include "Thyra_EpetraThyraWrappers.hpp"
39 #include "Thyra_EpetraLinearOp.hpp"
40 #endif
41
42 #include "ModeLaplace1DQ1.h"
43 #include "BlockPCGSolver.h"
44
45 using namespace Teuchos;
46
47 int main(int argc, char *argv[])
48 {
49 using std::cout;
50 using std::endl;
51 bool boolret;
52 int MyPID;
53
54 #ifdef HAVE_MPI
55 // Initialize MPI
56 MPI_Init(&argc,&argv);
57 #endif
58
59 bool success = false;
60 bool verbose = false;
61 try {
62 #ifdef HAVE_MPI
63 // Initialize MPI
64 Epetra_MpiComm Comm(MPI_COMM_WORLD);
65 #else
66 Epetra_SerialComm Comm;
67 #endif
68
69 MyPID = Comm.MyPID();
70
71 bool debug = false;
72 std::string which("LM");
73
74 CommandLineProcessor cmdp(false,true);
75 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
76 cmdp.setOption("debug","nodebug",&debug,"Print debugging information.");
77 cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM).");
78 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
79 #ifdef HAVE_MPI
80 MPI_Finalize();
81 #endif
82 return -1;
83 }
84 if (debug) verbose = true;
85
86 typedef double ScalarType;
87 typedef ScalarTraits<ScalarType> SCT;
88 typedef SCT::magnitudeType MagnitudeType;
89 typedef Thyra::MultiVectorBase<ScalarType> MV;
90 typedef Thyra::LinearOpBase<ScalarType> OP;
91 typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT;
92 typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT;
93 const ScalarType ONE = SCT::one();
94
95 if (verbose && MyPID == 0) {
96 cout << Anasazi::Anasazi_Version() << endl << endl;
97 }
98
99 // Problem information
100 int space_dim = 1;
101 std::vector<double> brick_dim( space_dim );
102 brick_dim[0] = 1.0;
103 std::vector<int> elements( space_dim );
104 elements[0] = 100;
105
106 // Create problem
107 RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) );
108 //
109 // Get the stiffness and mass matrices
110 RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false );
111 RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false );
112 //
113 // Create solver for mass matrix
114 // Note that accuracy of Davidson solution does NOT depend on how accurately the BlockPCG is solved
115 const int maxIterCG = 10;
116 const double tolCG = 1e-2;
117
118 RCP<BlockPCGSolver> opStiffness = rcp( new BlockPCGSolver(Comm, M.get(), tolCG, maxIterCG, 0) );
119 opStiffness->setPreconditioner( 0 );
120 RCP<Epetra_Operator> invStiffness = rcp( new Epetra_InvOperator(opStiffness.get()) );
121
122 // Create the initial vectors
123 int blockSize = 3;
124 RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) );
125 ivec->Random();
126
127 // Get a pointer to the Epetra_Map
128 RCP<const Epetra_Map> Map =
129 rcp( &K->OperatorDomainMap(), false );
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 RCP<const Thyra::LinearOpBase<double> > thyra_Prec =
145 Thyra::epetraLinearOp(invStiffness);
146
147 // Create eigenproblem
148 const int nev = 5;
149 RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem =
150 rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>() );
151 problem->setA(thyra_K);
152 problem->setM(thyra_M);
153 problem->setPrec(thyra_Prec);
154 problem->setInitVec(thyra_ivec);
155 //
156 // Set the number of eigenvalues requested
157 problem->setNEV( nev );
158 //
159 // Inform the eigenproblem that you are done passing it information
160 boolret = problem->setProblem();
161 if (boolret != true) {
162 if (verbose && MyPID == 0) {
163 cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl
164 << "End Result: TEST FAILED" << endl;
165 }
166 #ifdef HAVE_MPI
167 MPI_Finalize() ;
168 #endif
169 return -1;
170 }
171
172
173 // Set verbosity level
174 int verbosity = Anasazi::Errors + Anasazi::Warnings;
175 if (verbose) {
176 verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails;
177 }
178 if (debug) {
179 verbosity += Anasazi::Debug;
180 }
181
182
183 // Eigensolver parameters
184 int maxRestarts = 25;
185 int maxDim = 50;
186 MagnitudeType tol = 1e-6;
187 //
188 // Create parameter list to pass into the solver manager
189 ParameterList MyPL;
190 MyPL.set( "Verbosity", verbosity );
191 MyPL.set( "Which", which );
192 MyPL.set( "Maximum Subspace Dimension", maxDim );
193 MyPL.set( "Block Size", blockSize );
194 MyPL.set( "Maximum Restarts", maxRestarts );
195 MyPL.set( "Convergence Tolerance", tol );
196 //
197 // Create the solver manager
198 Anasazi::GeneralizedDavidsonSolMgr<ScalarType,MV,OP> MySolverMgr(problem, MyPL);
199 //
200 // Check that the parameters were all consumed
201 if (MyPL.getEntryPtr("Verbosity")->isUsed() == false ||
202 MyPL.getEntryPtr("Which")->isUsed() == false ||
203 MyPL.getEntryPtr("Maximum Subspace Dimension")->isUsed() == false ||
204 MyPL.getEntryPtr("Block Size")->isUsed() == false ||
205 MyPL.getEntryPtr("Maximum Restarts")->isUsed() == false ||
206 MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false) {
207 if (verbose && MyPID==0) {
208 cout << "Failure! Unused parameters: " << endl;
209 MyPL.unused(cout);
210 }
211 }
212
213
214 // Solve the problem to the specified tolerances or length
215 Anasazi::ReturnType returnCode = MySolverMgr.solve();
216 success = (returnCode == Anasazi::Converged);
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 2-norm of residuals
242 std::vector<MagnitudeType> resnorm(numev);
243 MVT::MvNorm( *Kvecs, resnorm );
244
245 os << "Direct residual norms computed in GeneralizedDavidson_test.exe" << endl
246 << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual" << endl
247 << "----------------------------------------" << endl;
248 for (int i=0; i<numev; i++) {
249 os << std::setw(20) << evals[i].realpart << std::setw(20) << resnorm[i] << endl;
250 success &= (resnorm[i] < tol);
251 }
252 if (verbose && MyPID==0) {
253 cout << endl << os.str() << endl;
254 }
255 }
256
257 if (verbose && MyPID==0) {
258 if (success)
259 cout << "End Result: TEST PASSED" << endl;
260 else
261 cout << "End Result: TEST FAILED" << endl;
262 }
263 }
264 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
265
266 #ifdef HAVE_MPI
267 MPI_Finalize() ;
268 #endif
269
270 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
271 }