// @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 "BelosPseudoBlockTFQMRSolMgr.hpp" #include "Teuchos_CommandLineProcessor.hpp" #include "Teuchos_ParameterList.hpp" #include "Teuchos_StandardCatchMacros.hpp" #ifdef HAVE_MPI #include #endif // I/O for Harwell-Boeing files #ifdef HAVE_BELOS_TRIUTILS #include "Trilinos_Util_iohb.h" #endif #include "MyMultiVec.hpp" #include "MyBetterOperator.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(); int info = 0; bool norm_failure = false; Teuchos::GlobalMPISession session(&argc, &argv, NULL); // int MyPID = session.getRank(); using Teuchos::RCP; using Teuchos::rcp; bool success = false; bool verbose = false; try { bool proc_verbose = false; int frequency = -1; // how often residuals are printed by solver int blocksize = 1; int numrhs = 1; std::string filename("mhd1280b.cua"); MT tol = 1.0e-5; // relative residual tolerance CommandLineProcessor cmdp(false, true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); cmdp.setOption("tol",&tol,"Relative residual tolerance used by pseudo-block TFQMR solver."); cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); 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,dim2,nnz; MT *dvals; int *colptr,*rowind; ST *cvals; nnz = -1; info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz, &colptr,&rowind,&dvals); if (info == 0 || nnz < 0) { if (MyPID==0) { std::cout << "Error reading '" << filename << "'" << std::endl; std::cout << "End Result: TEST FAILED" << std::endl; } return EXIT_FAILURE; } // Convert interleaved doubles to std::complex values cvals = new ST[nnz]; for (int ii=0; ii > A = rcp( new MyBetterOperator(dim,colptr,nnz,rowind,cvals) ); // // ********Other information used by block solver*********** // *****************(can be user specified)****************** // int maxits = dim/blocksize; // maximum number of iterations to run // ParameterList belosList; belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested if (verbose) { belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::FinalSummary + 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::MvRandom( *soln ); OPT::Apply( *A, *soln, *rhs ); 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*********************** // ******************************************************************* // Belos::PseudoBlockTFQMRSolMgr solver( problem, 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 pseudo-block 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; } } // Clean up. delete [] dvals; delete [] colptr; delete [] rowind; delete [] cvals; 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_tfqmr_complex_hb.cpp