// @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 where the operator (A) is the 1D finite-differenced Laplacian // operator. In this test, the eigenproblem is declared non-Hermitian even though // the operator is Hermitian. This illustrated the bug found in bug 2840. #include "AnasaziConfigDefs.hpp" #include "AnasaziTypes.hpp" #include "AnasaziBasicEigenproblem.hpp" #include "AnasaziFactory.hpp" #include "Teuchos_CommandLineProcessor.hpp" #ifdef HAVE_MPI #include #endif // templated multivector and sparse matrix classes #include "MyMultiVec.hpp" #include "MyOperator.hpp" using namespace Teuchos; int main(int argc, char *argv[]) { using std::cout; using std::endl; int MyPID = 0; bool boolret; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &MyPID); #endif bool testFailed; bool verbose = false; bool debug = false; 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("sort",&which,"Targetted eigenvalues (SM or LM)."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } typedef std::complex ST; typedef ScalarTraits SCT; typedef SCT::magnitudeType MT; typedef Anasazi::MultiVec MV; typedef Anasazi::Operator OP; typedef Anasazi::MultiVecTraits MVT; typedef Anasazi::OperatorTraits OPT; ST ONE = SCT::one(); if (verbose && MyPID == 0) { cout << Anasazi::Anasazi_Version() << endl << endl; } // -- Set finite difference grid int dim = 10; // Build the problem matrix RCP< const MyOperator > K = rcp( new MyOperator(dim) ); // Create initial vectors const int blockSize = 2; RCP > ivec = rcp( new MyMultiVec(dim,blockSize) ); ivec->MvRandom(); // Create eigenproblem const int nev = 1; RCP > problem = rcp( new Anasazi::BasicEigenproblem(K,ivec) ); // // Inform the eigenproblem that the operator K is non-Hermitian (even when it truly is Hermitian) 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; } // 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 = 200; MT tol = 1.0e-6; // // Create parameter list to pass into the solver manager ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Iterations", maxIters ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Full Ortho", true ); // // Create the solver manager auto MySolverMgr = Anasazi::Factory::create("LOBPCG", problem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr->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 " << 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 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; }