| 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 BlockKrylovSchur 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 |
#include "Epetra_Vector.h" |
| 22 |
|
| 23 |
#include "AnasaziBasicSort.hpp" |
| 24 |
#include "AnasaziBasicEigenproblem.hpp" |
| 25 |
#include "AnasaziBlockKrylovSchurSolMgr.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 |
#ifdef HAVE_EPETRA_THYRA |
| 35 |
#include "AnasaziThyraAdapter.hpp" |
| 36 |
#include "Thyra_EpetraThyraWrappers.hpp" |
| 37 |
#include "Thyra_EpetraLinearOp.hpp" |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "ModeLaplace1DQ1.h" |
| 41 |
#include "BlockPCGSolver.h" |
| 42 |
|
| 43 |
using namespace Teuchos; |
| 44 |
|
| 45 |
int main(int argc, char *argv[]) |
| 46 |
{ |
| 47 |
using std::cout; |
| 48 |
using std::endl; |
| 49 |
bool boolret; |
| 50 |
int MyPID; |
| 51 |
|
| 52 |
#ifdef HAVE_MPI |
| 53 |
// Initialize MPI |
| 54 |
MPI_Init(&argc,&argv); |
| 55 |
Epetra_MpiComm Comm(MPI_COMM_WORLD); |
| 56 |
#else |
| 57 |
Epetra_SerialComm Comm; |
| 58 |
|
| 59 |
#endif |
| 60 |
MyPID = Comm.MyPID(); |
| 61 |
|
| 62 |
bool testFailed = false; |
| 63 |
bool verbose = false; |
| 64 |
bool debug = false; |
| 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("sort",&which,"Targetted eigenvalues (SM or LM)."); |
| 71 |
if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 72 |
#ifdef HAVE_MPI |
| 73 |
MPI_Finalize(); |
| 74 |
#endif |
| 75 |
return -1; |
| 76 |
} |
| 77 |
if (debug) verbose = true; |
| 78 |
|
| 79 |
typedef double ScalarType; |
| 80 |
typedef ScalarTraits<ScalarType> SCT; |
| 81 |
typedef SCT::magnitudeType MagnitudeType; |
| 82 |
typedef Thyra::MultiVectorBase<ScalarType> MV; |
| 83 |
typedef Thyra::LinearOpBase<ScalarType> OP; |
| 84 |
typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT; |
| 85 |
typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT; |
| 86 |
const ScalarType ONE = SCT::one(); |
| 87 |
|
| 88 |
if (verbose && MyPID == 0) { |
| 89 |
cout << Anasazi::Anasazi_Version() << endl << endl; |
| 90 |
} |
| 91 |
|
| 92 |
// Problem information |
| 93 |
int space_dim = 1; |
| 94 |
std::vector<double> brick_dim( space_dim ); |
| 95 |
brick_dim[0] = 1.0; |
| 96 |
std::vector<int> elements( space_dim ); |
| 97 |
elements[0] = 100; |
| 98 |
|
| 99 |
// Create problem |
| 100 |
RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); |
| 101 |
// |
| 102 |
// Get the stiffness and mass matrices |
| 103 |
RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); |
| 104 |
RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); |
| 105 |
// |
| 106 |
// Create solver for mass matrix |
| 107 |
const int maxIterCG = 100; |
| 108 |
const double tolCG = 1e-7; |
| 109 |
|
| 110 |
RCP<BlockPCGSolver> opStiffness = rcp( new BlockPCGSolver(Comm, M.get(), tolCG, maxIterCG, 0) ); |
| 111 |
opStiffness->setPreconditioner( 0 ); |
| 112 |
RCP<Anasazi::EpetraGenOp> InverseOp = rcp( new Anasazi::EpetraGenOp( opStiffness, K ) ); |
| 113 |
|
| 114 |
// Create the initial vectors |
| 115 |
int blockSize = 3; |
| 116 |
RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) ); |
| 117 |
ivec->Random(); |
| 118 |
|
| 119 |
// Get a pointer to the Epetra_Map |
| 120 |
RCP<const Epetra_Map> Map = |
| 121 |
rcp( &K->OperatorDomainMap(), false ); |
| 122 |
|
| 123 |
// create a Thyra::VectorSpaceBase |
| 124 |
RCP<const Thyra::VectorSpaceBase<double> > epetra_vs = |
| 125 |
Thyra::create_VectorSpace(Map); |
| 126 |
|
| 127 |
// create a MultiVectorBase (from the Epetra_MultiVector) |
| 128 |
RCP<Thyra::MultiVectorBase<double> > thyra_ivec = |
| 129 |
Thyra::create_MultiVector(ivec, epetra_vs); |
| 130 |
|
| 131 |
// Create Thyra LinearOpBase objects from the Epetra_Operator objects |
| 132 |
RCP<const Thyra::LinearOpBase<double> > thyra_K = |
| 133 |
Thyra::epetraLinearOp(K); |
| 134 |
RCP<const Thyra::LinearOpBase<double> > thyra_M = |
| 135 |
Thyra::epetraLinearOp(M); |
| 136 |
RCP<const Thyra::LinearOpBase<double> > thyra_IOp = |
| 137 |
Thyra::epetraLinearOp(InverseOp); |
| 138 |
|
| 139 |
// Create eigenproblem |
| 140 |
const int nev = 5; |
| 141 |
RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem = |
| 142 |
rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(thyra_IOp, thyra_M, thyra_ivec) ); |
| 143 |
// |
| 144 |
// Inform the eigenproblem that the operator thyra_IOp is Hermitian under an M inner-product |
| 145 |
problem->setHermitian(true); |
| 146 |
// |
| 147 |
// Set the number of eigenvalues requested |
| 148 |
problem->setNEV( nev ); |
| 149 |
// |
| 150 |
// Inform the eigenproblem that you are done passing it information |
| 151 |
boolret = problem->setProblem(); |
| 152 |
if (boolret != true) { |
| 153 |
if (verbose && MyPID == 0) { |
| 154 |
cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl |
| 155 |
<< "End Result: TEST FAILED" << endl; |
| 156 |
} |
| 157 |
#ifdef HAVE_MPI |
| 158 |
MPI_Finalize() ; |
| 159 |
#endif |
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
// Set verbosity level |
| 165 |
int verbosity = Anasazi::Errors + Anasazi::Warnings; |
| 166 |
if (verbose) { |
| 167 |
verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; |
| 168 |
} |
| 169 |
if (debug) { |
| 170 |
verbosity += Anasazi::Debug; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
// Eigensolver parameters |
| 175 |
int numBlocks = 10; |
| 176 |
int maxRestarts = 50; |
| 177 |
int stepSize = numBlocks*maxRestarts; |
| 178 |
MagnitudeType tol = tolCG * 10.0; |
| 179 |
// Create a sort manager to pass into the block Krylov-Schur solver manager |
| 180 |
// --> Make sure the reference-counted pointer is of type Anasazi::SortManager<> |
| 181 |
// --> The block Krylov-Schur solver manager uses Anasazi::BasicSort<> by default, |
| 182 |
// so you can also pass in the parameter "Which", instead of a sort manager. |
| 183 |
RCP<Anasazi::SortManager<MagnitudeType> > MySort = |
| 184 |
rcp( new Anasazi::BasicSort<MagnitudeType>( which ) ); |
| 185 |
// |
| 186 |
// Create parameter list to pass into the solver manager |
| 187 |
ParameterList MyPL; |
| 188 |
MyPL.set( "Verbosity", verbosity ); |
| 189 |
MyPL.set( "Sort Manager", MySort ); |
| 190 |
//MyPL.set( "Which", which ); |
| 191 |
MyPL.set( "Block Size", blockSize ); |
| 192 |
MyPL.set( "Num Blocks", numBlocks ); |
| 193 |
MyPL.set( "Maximum Restarts", maxRestarts ); |
| 194 |
MyPL.set( "Step Size", stepSize ); |
| 195 |
MyPL.set( "Convergence Tolerance", tol ); |
| 196 |
// |
| 197 |
// Create the solver manager |
| 198 |
Anasazi::BlockKrylovSchurSolMgr<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("Block Size")->isUsed() == false || |
| 203 |
MyPL.getEntryPtr("Num Blocks")->isUsed() == false || |
| 204 |
MyPL.getEntryPtr("Maximum Restarts")->isUsed() == false || |
| 205 |
MyPL.getEntryPtr("Step Size")->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 |
if (returnCode != Anasazi::Converged) { |
| 217 |
testFailed = true; |
| 218 |
} |
| 219 |
|
| 220 |
// Get the eigenvalues and eigenvectors from the eigenproblem |
| 221 |
Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution(); |
| 222 |
std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals; |
| 223 |
RCP<MV> evecs = sol.Evecs; |
| 224 |
int numev = sol.numVecs; |
| 225 |
|
| 226 |
if (numev > 0) { |
| 227 |
|
| 228 |
std::ostringstream os; |
| 229 |
os.setf(std::ios::scientific, std::ios::floatfield); |
| 230 |
os.precision(6); |
| 231 |
|
| 232 |
// Compute the direct residual |
| 233 |
std::vector<ScalarType> normV( numev ); |
| 234 |
SerialDenseMatrix<int,ScalarType> T(numev,numev); |
| 235 |
for (int i=0; i<numev; i++) { |
| 236 |
T(i,i) = evals[i].realpart; |
| 237 |
} |
| 238 |
RCP<MV> Mvecs = MVT::Clone( *evecs, numev ), |
| 239 |
Kvecs = MVT::Clone( *evecs, numev ); |
| 240 |
OPT::Apply( *thyra_K, *evecs, *Kvecs ); |
| 241 |
OPT::Apply( *thyra_M, *evecs, *Mvecs ); |
| 242 |
MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs ); |
| 243 |
// compute 2-norm of residuals |
| 244 |
std::vector<MagnitudeType> resnorm(numev); |
| 245 |
MVT::MvNorm( *Kvecs, resnorm ); |
| 246 |
|
| 247 |
os << "Direct residual norms computed in BlockKrylovSchur_test.exe" << endl |
| 248 |
<< std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual" << endl |
| 249 |
<< "----------------------------------------" << endl; |
| 250 |
for (int i=0; i<numev; i++) { |
| 251 |
if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) { |
| 252 |
resnorm[i] = SCT::magnitude( SCT::squareroot( resnorm[i] ) / evals[i].realpart ); |
| 253 |
} |
| 254 |
else { |
| 255 |
resnorm[i] = SCT::magnitude( SCT::squareroot( resnorm[i] ) ); |
| 256 |
} |
| 257 |
os << std::setw(20) << evals[i].realpart << std::setw(20) << resnorm[i] << endl; |
| 258 |
if ( resnorm[i] > tol ) { |
| 259 |
testFailed = true; |
| 260 |
} |
| 261 |
} |
| 262 |
if (verbose && MyPID==0) { |
| 263 |
cout << endl << os.str() << endl; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#ifdef HAVE_MPI |
| 268 |
MPI_Finalize() ; |
| 269 |
#endif |
| 270 |
|
| 271 |
if (testFailed) { |
| 272 |
if (verbose && MyPID==0) { |
| 273 |
cout << "End Result: TEST FAILED" << endl; |
| 274 |
} |
| 275 |
return -1; |
| 276 |
} |
| 277 |
// |
| 278 |
// Default return value |
| 279 |
// |
| 280 |
if (verbose && MyPID==0) { |
| 281 |
cout << "End Result: TEST PASSED" << endl; |
| 282 |
} |
| 283 |
return 0; |
| 284 |
|
| 285 |
|
| 286 |
} |