// @HEADER // ***************************************************************************** // Anasazi: Block Eigensolvers Package // // Copyright 2004 NTESS and the Anasazi contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // // This test is for LOBPCG solving a standard (Ax=xl) complex Hermitian // eigenvalue problem, using the LOBPCGSolMgr solver manager. // // The matrix used is from MatrixMarket: // Name: MHD1280B: Alfven Spectra in Magnetohydrodynamics // Source: Source: A. Booten, M.N. Kooper, H.A. van der Vorst, S. Poedts and J.P. Goedbloed University of Utrecht, the Netherlands // Discipline: Plasma physics // URL: http://math.nist.gov/MatrixMarket/data/NEP/mhd/mhd1280b.html // Size: 1280 x 1280 // NNZ: 22778 entries #include "AnasaziConfigDefs.hpp" #include "AnasaziTypes.hpp" #include "AnasaziBasicEigenproblem.hpp" #include "AnasaziBasicOutputManager.hpp" #include "AnasaziFactory.hpp" #include "Teuchos_CommandLineProcessor.hpp" #ifdef HAVE_MPI #include #endif // I/O for Harwell-Boeing files #ifdef HAVE_ANASAZI_TRIUTILS #include "Trilinos_Util_iohb.h" #endif // templated multivector and sparse matrix classes #include "MyMultiVec.hpp" #include "MyBetterOperator.hpp" using namespace Teuchos; int main(int argc, char *argv[]) { using std::cout; using std::endl; int info = 0; bool boolret; int MyPID; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); MPI_Comm_rank( MPI_COMM_WORLD, &MyPID ); #else MyPID = 0; #endif bool testFailed; bool verbose = false; bool debug = false; std::string filename("mhd1280b.cua"); std::string which("LM"); CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM)."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } #ifndef HAVE_ANASAZI_TRIUTILS cout << "This test requires Triutils. Please configure with --enable-triutils." << endl; #ifdef HAVE_MPI MPI_Finalize() ; #endif if (verbose && MyPID == 0) { cout << "End Result: TEST FAILED" << endl; } return -1; #endif typedef std::complex ScalarType; typedef ScalarTraits SCT; typedef SCT::magnitudeType MagnitudeType; typedef Anasazi::MultiVec MV; typedef Anasazi::Operator OP; typedef Anasazi::MultiVecTraits MVT; typedef Anasazi::OperatorTraits OPT; const ScalarType ONE = SCT::one(); if (verbose && MyPID == 0) { cout << Anasazi::Anasazi_Version() << endl << endl; } // Problem information int dim,dim2,nnz; double *dvals; int *colptr,*rowind; nnz = -1; info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz, &colptr,&rowind,&dvals); if (info == 0 || nnz < 0) { if (verbose && MyPID == 0) { cout << "Error reading '" << filename << "'" << endl << "End Result: TEST FAILED" << endl; } #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } // Convert interleaved doubles to complex values std::vector cvals(nnz); for (int ii=0; ii > K = rcp( new MyBetterOperator(dim,colptr,nnz,rowind,&cvals[0]) ); // Create initial vectors int blockSize = 5; RCP > ivec = rcp( new MyMultiVec(dim,blockSize) ); ivec->MvRandom(); // Create eigenproblem const int nev = 4; RCP > problem = rcp( new Anasazi::BasicEigenproblem(K,ivec) ); // // Inform the eigenproblem that the operator K is symmetric problem->setHermitian(true); // // Set the number of eigenvalues requested problem->setNEV( nev ); // // Inform the eigenproblem that you are done passing it information boolret = problem->setProblem(); if (boolret != true) { if (verbose && MyPID == 0) { cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl << "End Result: TEST FAILED" << endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Get a formatted output stream RCP > MyOM = rcp( new Anasazi::BasicOutputManager() ); RCP fos = MyOM->getFancyOStream(); // Set verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (verbose) { verbosity += Anasazi::IterationDetails + Anasazi::FinalSummary + Anasazi::TimingDetails; } if (debug) { verbosity += Anasazi::Debug; } // Eigensolver parameters int maxIters = 450; MagnitudeType tol = 1.0e-6; // // Create parameter list to pass into the solver manager ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Output Stream", fos ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Iterations", maxIters ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Use Locking", true ); MyPL.set( "Locking Tolerance", tol/10 ); MyPL.set( "Full Ortho", true ); // // Create the solver manager auto MySolverMan = Anasazi::Factory::create("LOBPCG", problem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMan->solve(); testFailed = false; if (returnCode != Anasazi::Converged) { testFailed = true; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution sol = problem->getSolution(); RCP evecs = sol.Evecs; int numev = sol.numVecs; if (numev > 0) { std::ostringstream os; os.setf(std::ios::scientific, std::ios::floatfield); os.precision(6); // Compute the direct residual std::vector normV( numev ); SerialDenseMatrix T(numev,numev); for (int i=0; i Kvecs = MVT::Clone( *evecs, numev ); OPT::Apply( *K, *evecs, *Kvecs ); MVT::MvTimesMatAddMv( -ONE, *evecs, T, ONE, *Kvecs ); MVT::MvNorm( *Kvecs, normV ); os << "Direct residual norms computed in LOBPCGComplex_test.exe" << endl << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl << "----------------------------------------" << endl; for (int i=0; i tol ) { testFailed = true; } } if (verbose && MyPID==0) { cout << endl << os.str() << endl; } } #ifdef HAVE_MPI MPI_Finalize() ; #endif // Clean up. std::free( dvals ); std::free( colptr ); std::free( rowind ); if (testFailed) { if (verbose && MyPID==0) { cout << "End Result: TEST FAILED" << endl; } return -1; } // // Default return value // if (verbose && MyPID==0) { cout << "End Result: TEST PASSED" << endl; } return 0; }