// @HEADER // ***************************************************************************** // Anasazi: Block Eigensolvers Package // // Copyright 2004 NTESS and the Anasazi contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // // This test is for the GeneralizedDavidson solver using the Thyra interface // The Thyra objects will be extracted from Epetra objects using the // Epetra-Thyra interface. // A few steps of BlockPCG is used as a preconditioner. // #include "AnasaziConfigDefs.hpp" #include "AnasaziTypes.hpp" #include "AnasaziEpetraAdapter.hpp" #include "Epetra_CrsMatrix.h" #include "Epetra_InvOperator.h" #include "Epetra_Vector.h" #include "AnasaziBasicSort.hpp" #include "AnasaziBasicEigenproblem.hpp" #include "AnasaziGeneralizedDavidsonSolMgr.hpp" #include "Teuchos_CommandLineProcessor.hpp" #ifdef HAVE_MPI #include "Epetra_MpiComm.h" #include #else #include "Epetra_SerialComm.h" #endif #include "Teuchos_StandardCatchMacros.hpp" #ifdef HAVE_EPETRA_THYRA #include "AnasaziThyraAdapter.hpp" #include "Thyra_EpetraThyraWrappers.hpp" #include "Thyra_EpetraLinearOp.hpp" #endif #include "ModeLaplace1DQ1.h" #include "BlockPCGSolver.h" using namespace Teuchos; int main(int argc, char *argv[]) { using std::cout; using std::endl; bool boolret; int MyPID; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); #endif bool success = false; bool verbose = false; try { #ifdef HAVE_MPI // Initialize MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif MyPID = Comm.MyPID(); 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; } if (debug) verbose = true; typedef double ScalarType; typedef ScalarTraits SCT; typedef SCT::magnitudeType MagnitudeType; typedef Thyra::MultiVectorBase MV; typedef Thyra::LinearOpBase 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 space_dim = 1; std::vector brick_dim( space_dim ); brick_dim[0] = 1.0; std::vector elements( space_dim ); elements[0] = 100; // Create problem RCP testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); // // Get the stiffness and mass matrices RCP K = rcp( const_cast(testCase->getStiffness()), false ); RCP M = rcp( const_cast(testCase->getMass()), false ); // // Create solver for mass matrix // Note that accuracy of Davidson solution does NOT depend on how accurately the BlockPCG is solved const int maxIterCG = 10; const double tolCG = 1e-2; RCP opStiffness = rcp( new BlockPCGSolver(Comm, M.get(), tolCG, maxIterCG, 0) ); opStiffness->setPreconditioner( 0 ); RCP invStiffness = rcp( new Epetra_InvOperator(opStiffness.get()) ); // Create the initial vectors int blockSize = 3; RCP ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) ); ivec->Random(); // Get a pointer to the Epetra_Map RCP Map = rcp( &K->OperatorDomainMap(), false ); // create a Thyra::VectorSpaceBase RCP > epetra_vs = Thyra::create_VectorSpace(Map); // create a MultiVectorBase (from the Epetra_MultiVector) RCP > thyra_ivec = Thyra::create_MultiVector(ivec, epetra_vs); // Create Thyra LinearOpBase objects from the Epetra_Operator objects RCP > thyra_K = Thyra::epetraLinearOp(K); RCP > thyra_M = Thyra::epetraLinearOp(M); RCP > thyra_Prec = Thyra::epetraLinearOp(invStiffness); // Create eigenproblem const int nev = 5; RCP > problem = rcp( new Anasazi::BasicEigenproblem() ); problem->setA(thyra_K); problem->setM(thyra_M); problem->setPrec(thyra_Prec); problem->setInitVec(thyra_ivec); // // 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::FinalSummary + Anasazi::TimingDetails; } if (debug) { verbosity += Anasazi::Debug; } // Eigensolver parameters int maxRestarts = 25; int maxDim = 50; MagnitudeType tol = 1e-6; // // Create parameter list to pass into the solver manager ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Maximum Subspace Dimension", maxDim ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Restarts", maxRestarts ); MyPL.set( "Convergence Tolerance", tol ); // // Create the solver manager Anasazi::GeneralizedDavidsonSolMgr MySolverMgr(problem, MyPL); // // Check that the parameters were all consumed if (MyPL.getEntryPtr("Verbosity")->isUsed() == false || MyPL.getEntryPtr("Which")->isUsed() == false || MyPL.getEntryPtr("Maximum Subspace Dimension")->isUsed() == false || MyPL.getEntryPtr("Block Size")->isUsed() == false || MyPL.getEntryPtr("Maximum Restarts")->isUsed() == false || MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false) { if (verbose && MyPID==0) { cout << "Failure! Unused parameters: " << endl; MyPL.unused(cout); } } // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr.solve(); success = (returnCode == Anasazi::Converged); // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution sol = problem->getSolution(); std::vector > evals = sol.Evals; 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 Mvecs = MVT::Clone( *evecs, numev ), Kvecs = MVT::Clone( *evecs, numev ); OPT::Apply( *thyra_K, *evecs, *Kvecs ); OPT::Apply( *thyra_M, *evecs, *Mvecs ); MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs ); // compute 2-norm of residuals std::vector resnorm(numev); MVT::MvNorm( *Kvecs, resnorm ); os << "Direct residual norms computed in GeneralizedDavidson_test.exe" << endl << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual" << endl << "----------------------------------------" << endl; for (int i=0; i