// @HEADER // ***************************************************************************** // Belos: Block Linear Solvers Package // // Copyright 2004-2016 NTESS and the Belos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // // This driver reads a problem from a Harwell-Boeing (HB) file. // The right-hand-side from the HB file is used instead of random vectors. // The initial guesses are all set to zero. // // NOTE: No preconditioner is used in this case. // #include "BelosConfigDefs.hpp" #include "BelosLinearProblem.hpp" #include "BelosTFQMRSolMgr.hpp" #include "BelosPseudoBlockTFQMRSolMgr.hpp" #include "Teuchos_CommandLineProcessor.hpp" #include "Teuchos_ParameterList.hpp" #include "Teuchos_StandardCatchMacros.hpp" #ifdef HAVE_MPI #include #endif #include "MyMultiVec.hpp" #include "MyOperator.hpp" using namespace Teuchos; int main(int argc, char *argv[]) { // typedef std::complex ST; typedef ScalarTraits SCT; typedef SCT::magnitudeType MT; typedef Belos::MultiVec MV; typedef Belos::Operator OP; typedef Belos::MultiVecTraits MVT; typedef Belos::OperatorTraits OPT; ST one = SCT::one(); ST zero = SCT::zero(); Teuchos::GlobalMPISession session(&argc, &argv, NULL); int MyPID = session.getRank(); // using Teuchos::RCP; using Teuchos::rcp; bool success = false; bool verbose = false; try { bool norm_failure = false; bool proc_verbose = false; bool pseudo = false; // use pseudo block TFQMR to solve this linear system. int frequency = -1; // how often residuals are printed by solver int blocksize = 1; int numrhs = 1; int maxrestarts = 15; int length = 50; MT tol = 1.0e-5; // relative residual tolerance CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block TFQMR to solve the linear systems."); cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver."); cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); cmdp.setOption("num-restarts",&maxrestarts,"Maximum number of restarts allowed for the TFQMR solver."); cmdp.setOption("blocksize",&blocksize,"Block size used by TFQMR."); cmdp.setOption("subspace-length",&length,"Maximum dimension of block-subspace used by TFQMR solver."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { return EXIT_FAILURE; } proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */ if (proc_verbose) { std::cout << Belos::Belos_Version() << std::endl << std::endl; } if (!verbose) frequency = -1; // reset frequency if test is not verbose #ifndef HAVE_BELOS_TRIUTILS std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl; if (MyPID==0) { std::cout << "End Result: TEST FAILED" << std::endl; } return EXIT_FAILURE; #endif // Get the data from the HB file int dim=100; // Build the problem matrix std::vector diag( dim, (ST)4.0 ); RCP< MyOperator > A = rcp( new MyOperator( diag ) ); // // ********Other information used by block solver*********** // *****************(can be user specified)****************** // int maxits = dim/blocksize; // maximum number of iterations to run // ParameterList belosList; belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested if (verbose) { belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::StatusTestDetails ); if (frequency > 0) belosList.set( "Output Frequency", frequency ); } else belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); // // Construct the right-hand side and solution multivectors. // NOTE: The right-hand side will be constructed such that the solution is // a vectors of one. // RCP > soln = rcp( new MyMultiVec(dim,numrhs) ); RCP > rhs = rcp( new MyMultiVec(dim,numrhs) ); MVT::MvInit( *rhs, 1.0 ); MVT::MvInit( *soln, zero ); // // Construct an unpreconditioned linear problem instance. // RCP > problem = rcp( new Belos::LinearProblem( A, soln, rhs ) ); bool set = problem->setProblem(); if (set == false) { if (proc_verbose) std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; return EXIT_FAILURE; } // // ******************************************************************* // *************Start the TFQMR iteration*********************** // ******************************************************************* // Teuchos::RCP< Belos::SolverManager > solver; if (pseudo) solver = Teuchos::rcp( new Belos::PseudoBlockTFQMRSolMgr( problem, Teuchos::rcp(&belosList,false) ) ); else solver = Teuchos::rcp( new Belos::TFQMRSolMgr( problem, Teuchos::rcp(&belosList,false) ) ); // // **********Print out information about problem******************* // if (proc_verbose) { std::cout << std::endl << std::endl; std::cout << "Dimension of matrix: " << dim << std::endl; std::cout << "Number of right-hand sides: " << numrhs << std::endl; std::cout << "Block size used by solver: " << blocksize << std::endl; std::cout << "Max number of TFQMR iterations: " << maxits << std::endl; std::cout << "Relative residual tolerance: " << tol << std::endl; std::cout << std::endl; } // // Perform solve // Belos::ReturnType ret = solver->solve(); // // Compute actual residuals. // RCP > temp = rcp( new MyMultiVec(dim,numrhs) ); OPT::Apply( *A, *soln, *temp ); MVT::MvAddMv( one, *rhs, -one, *temp, *temp ); std::vector norm_num(numrhs), norm_denom(numrhs); MVT::MvNorm( *temp, norm_num ); MVT::MvNorm( *rhs, norm_denom ); for (int i=0; i tol ) { norm_failure = true; } } success = ret==Belos::Converged && !norm_failure; if (success) { if (proc_verbose) std::cout << "End Result: TEST PASSED" << std::endl; } else { if (proc_verbose) std::cout << "End Result: TEST FAILED" << std::endl; } } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); } // end test_bl_gmres_complex_hb.cpp