| 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 BlockDavidson 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 BlockDavidson 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 "AnasaziBlockDavidsonSolMgr.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 std::cout; |
| 47 |
using std::endl; |
| 48 |
|
| 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 |
#endif |
| 59 |
MyPID = Comm.MyPID(); |
| 60 |
|
| 61 |
bool testFailed = false; |
| 62 |
bool verbose = false; |
| 63 |
bool debug = false; |
| 64 |
std::string filename("mhd1280b.cua"); |
| 65 |
std::string which("LM"); |
| 66 |
|
| 67 |
bool success = true; |
| 68 |
try { |
| 69 |
|
| 70 |
CommandLineProcessor cmdp(false,true); |
| 71 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 72 |
cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); |
| 73 |
cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM)."); |
| 74 |
if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 75 |
#ifdef HAVE_MPI |
| 76 |
MPI_Finalize(); |
| 77 |
#endif |
| 78 |
return -1; |
| 79 |
} |
| 80 |
|
| 81 |
#ifndef HAVE_EPETRA_THYRA |
| 82 |
if (verbose && MyPid == 0) { |
| 83 |
cout << "Please configure Anasazi with:" << endl; |
| 84 |
cout << "--enable-epetra-thyra" << endl; |
| 85 |
cout << "--enable-anasazi-thyra" << endl; |
| 86 |
} |
| 87 |
return 0; |
| 88 |
#endif |
| 89 |
|
| 90 |
typedef double ScalarType; |
| 91 |
typedef ScalarTraits<ScalarType> SCT; |
| 92 |
typedef SCT::magnitudeType MagnitudeType; |
| 93 |
typedef Thyra::MultiVectorBase<double> MV; |
| 94 |
typedef Thyra::LinearOpBase<double> 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 space_dim = 1; |
| 105 |
std::vector<double> brick_dim( space_dim ); |
| 106 |
brick_dim[0] = 1.0; |
| 107 |
std::vector<int> elements( space_dim ); |
| 108 |
elements[0] = 100; |
| 109 |
|
| 110 |
// Create problem |
| 111 |
RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); |
| 112 |
// |
| 113 |
// Get the stiffness and mass matrices |
| 114 |
RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); |
| 115 |
RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); |
| 116 |
// |
| 117 |
// Create the initial vectors |
| 118 |
int blockSize = 5; |
| 119 |
// |
| 120 |
// Get a pointer to the Epetra_Map |
| 121 |
RCP<const Epetra_Map> Map = rcp( &K->OperatorDomainMap(), false ); |
| 122 |
// |
| 123 |
// create an epetra multivector |
| 124 |
RCP<Epetra_MultiVector> ivec = |
| 125 |
rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) ); |
| 126 |
ivec->Random(); |
| 127 |
|
| 128 |
// create a Thyra::VectorSpaceBase |
| 129 |
RCP<const Thyra::VectorSpaceBase<double> > epetra_vs = |
| 130 |
Thyra::create_VectorSpace(Map); |
| 131 |
|
| 132 |
// create a MultiVectorBase (from the Epetra_MultiVector) |
| 133 |
RCP<Thyra::MultiVectorBase<double> > thyra_ivec = |
| 134 |
Thyra::create_MultiVector(ivec, epetra_vs); |
| 135 |
|
| 136 |
// Create Thyra LinearOpBase objects from the Epetra_Operator objects |
| 137 |
RCP<const Thyra::LinearOpBase<double> > thyra_K = |
| 138 |
Thyra::epetraLinearOp(K); |
| 139 |
RCP<const Thyra::LinearOpBase<double> > thyra_M = |
| 140 |
Thyra::epetraLinearOp(M); |
| 141 |
|
| 142 |
// Create eigenproblem |
| 143 |
const int nev = 5; |
| 144 |
RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem = |
| 145 |
rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(thyra_K,thyra_M,thyra_ivec) ); |
| 146 |
// |
| 147 |
// Inform the eigenproblem that the operator K is symmetric |
| 148 |
problem->setHermitian(true); |
| 149 |
// |
| 150 |
// Set the number of eigenvalues requested |
| 151 |
problem->setNEV( nev ); |
| 152 |
// |
| 153 |
// Inform the eigenproblem that you are done passing it information |
| 154 |
boolret = problem->setProblem(); |
| 155 |
if (boolret != true) { |
| 156 |
if (verbose && MyPID == 0) { |
| 157 |
cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl |
| 158 |
<< "End Result: TEST FAILED" << endl; |
| 159 |
} |
| 160 |
#ifdef HAVE_MPI |
| 161 |
MPI_Finalize() ; |
| 162 |
#endif |
| 163 |
return -1; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
// Set verbosity level |
| 168 |
int verbosity = Anasazi::Errors + Anasazi::Warnings; |
| 169 |
if (verbose) { |
| 170 |
verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; |
| 171 |
} |
| 172 |
if (debug) { |
| 173 |
verbosity += Anasazi::Debug; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
// Eigensolver parameters |
| 178 |
int numBlocks = 8; |
| 179 |
int maxRestarts = 100; |
| 180 |
MagnitudeType tol = 1.0e-6; |
| 181 |
// |
| 182 |
// Create parameter list to pass into the solver manager |
| 183 |
ParameterList MyPL; |
| 184 |
MyPL.set( "Verbosity", verbosity ); |
| 185 |
MyPL.set( "Which", which ); |
| 186 |
MyPL.set( "Block Size", blockSize ); |
| 187 |
MyPL.set( "Num Blocks", numBlocks ); |
| 188 |
MyPL.set( "Maximum Restarts", maxRestarts ); |
| 189 |
MyPL.set( "Convergence Tolerance", tol ); |
| 190 |
MyPL.set( "Use Locking", true ); |
| 191 |
MyPL.set( "Locking Tolerance", tol/10 ); |
| 192 |
// |
| 193 |
// Create the solver manager |
| 194 |
Anasazi::BlockDavidsonSolMgr<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("Num Blocks")->isUsed() == false || |
| 201 |
MyPL.getEntryPtr("Maximum Restarts")->isUsed() == false || |
| 202 |
MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false || |
| 203 |
MyPL.getEntryPtr("Use Locking")->isUsed() == false || |
| 204 |
MyPL.getEntryPtr("Locking Tolerance")->isUsed() == false) { |
| 205 |
if (verbose && MyPID==0) { |
| 206 |
cout << "Failure! Unused parameters: " << endl; |
| 207 |
MyPL.unused(cout); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
// Solve the problem to the specified tolerances or length |
| 213 |
Anasazi::ReturnType returnCode = MySolverMan.solve(); |
| 214 |
if (returnCode != Anasazi::Converged) { |
| 215 |
testFailed = true; |
| 216 |
} |
| 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 M-norm of residuals |
| 242 |
OPT::Apply( *thyra_M, *Kvecs, *Mvecs ); |
| 243 |
MVT::MvDot( *Mvecs, *Kvecs, normV ); |
| 244 |
|
| 245 |
os << "Direct residual norms computed in BlockDavidsonThyra_test.exe" << endl |
| 246 |
<< std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl |
| 247 |
<< "----------------------------------------" << endl; |
| 248 |
for (int i=0; i<numev; i++) { |
| 249 |
if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) { |
| 250 |
normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) / evals[i].realpart ); |
| 251 |
} |
| 252 |
else { |
| 253 |
normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) ); |
| 254 |
} |
| 255 |
os << std::setw(20) << evals[i].realpart << std::setw(20) << normV[i] << endl; |
| 256 |
if ( normV[i] > tol ) { |
| 257 |
testFailed = true; |
| 258 |
} |
| 259 |
} |
| 260 |
if (verbose && MyPID==0) { |
| 261 |
cout << endl << os.str() << endl; |
| 262 |
} |
| 263 |
|
| 264 |
} |
| 265 |
} |
| 266 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success); |
| 267 |
|
| 268 |
#ifdef HAVE_MPI |
| 269 |
MPI_Finalize() ; |
| 270 |
#endif |
| 271 |
|
| 272 |
if (testFailed || success==false) { |
| 273 |
if (verbose && MyPID==0) { |
| 274 |
cout << "End Result: TEST FAILED" << endl; |
| 275 |
} |
| 276 |
return -1; |
| 277 |
} |
| 278 |
// |
| 279 |
// Default return value |
| 280 |
// |
| 281 |
if (verbose && MyPID==0) { |
| 282 |
cout << "End Result: TEST PASSED" << endl; |
| 283 |
} |
| 284 |
return 0; |
| 285 |
|
| 286 |
} |