| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
// |
| 10 |
// This driver reads a problem from a file, which can be in Harwell-Boeing (*.hb), |
| 11 |
// Matrix Market (*.mtx), or triplet format (*.triU, *.triS). The right-hand side |
| 12 |
// from the problem, if it exists, will be used instead of multiple |
| 13 |
// random right-hand-sides. The initial guesses are all set to zero. |
| 14 |
// |
| 15 |
// NOTE: No preconditioner is used in this example. |
| 16 |
// |
| 17 |
#include "BelosConfigDefs.hpp" |
| 18 |
#include "BelosLinearProblem.hpp" |
| 19 |
#include "BelosEpetraAdapter.hpp" |
| 20 |
#include "BelosMinresSolMgr.hpp" |
| 21 |
|
| 22 |
#include "Epetra_Map.h" |
| 23 |
#ifdef EPETRA_MPI |
| 24 |
#include "Epetra_MpiComm.h" |
| 25 |
#else |
| 26 |
#include "Epetra_SerialComm.h" |
| 27 |
#endif |
| 28 |
#include "Epetra_CrsMatrix.h" |
| 29 |
|
| 30 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 31 |
#include "Teuchos_ParameterList.hpp" |
| 32 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 33 |
|
| 34 |
int |
| 35 |
main(int argc, char *argv[]) |
| 36 |
{ |
| 37 |
using Teuchos::CommandLineProcessor; |
| 38 |
using Teuchos::ParameterList; |
| 39 |
using Teuchos::RCP; |
| 40 |
using Teuchos::rcp; |
| 41 |
using std::cout; |
| 42 |
using std::endl; |
| 43 |
|
| 44 |
typedef double ST; |
| 45 |
typedef Teuchos::ScalarTraits<ST> SCT; |
| 46 |
typedef SCT::magnitudeType MT; |
| 47 |
typedef Epetra_MultiVector MV; |
| 48 |
typedef Epetra_Operator OP; |
| 49 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 50 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 51 |
|
| 52 |
#ifdef EPETRA_MPI |
| 53 |
// Initialize MPI |
| 54 |
MPI_Init(&argc,&argv); |
| 55 |
Epetra_MpiComm Comm(MPI_COMM_WORLD); |
| 56 |
const int MyPID = Comm.MyPID(); |
| 57 |
#else // No EPETRA_MPI |
| 58 |
Epetra_SerialComm Comm; |
| 59 |
const int MyPID = 0; |
| 60 |
#endif // EPETRA_MPI |
| 61 |
|
| 62 |
bool verbose = false; |
| 63 |
bool debug = false; |
| 64 |
bool success = true; |
| 65 |
try { |
| 66 |
int frequency = -1; // frequency of status test output. |
| 67 |
int numrhs = 1; // number of right-hand sides to solve for |
| 68 |
int maxiters = -1; // maximum number of iterations allowed per linear system |
| 69 |
MT tol = 1.0e-8; // relative residual tolerance |
| 70 |
|
| 71 |
// Define command-line arguments |
| 72 |
CommandLineProcessor cmdp(false,true); |
| 73 |
cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results."); |
| 74 |
cmdp.setOption ("debug", "nodebug", &debug, |
| 75 |
"Print debugging information from the solver."); |
| 76 |
cmdp.setOption ("frequency", &frequency, |
| 77 |
"Solver's frequency for printing residuals (#iters)."); |
| 78 |
cmdp.setOption ("tol", &tol, |
| 79 |
"Relative residual tolerance used by MINRES solver."); |
| 80 |
cmdp.setOption ("num-rhs", &numrhs, |
| 81 |
"Number of right-hand sides for which to solve (> 0)."); |
| 82 |
cmdp.setOption ("max-iters", &maxiters, |
| 83 |
"Maximum number of iterations per linear system. -1 means " |
| 84 |
"we choose, based on problem size."); |
| 85 |
|
| 86 |
// Parse command-line arguments and fetch values |
| 87 |
if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) |
| 88 |
{ |
| 89 |
std::cout << "Failed to parse command-line arguments!" << std::endl; |
| 90 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 91 |
return -1; |
| 92 |
} |
| 93 |
|
| 94 |
// ********************************************************************** |
| 95 |
// ******************Set up the problem to be solved********************* |
| 96 |
// construct diagonal matrix |
| 97 |
const int NumGlobalElements = 100; |
| 98 |
const int m = 4; // number of negative eigenvalues |
| 99 |
|
| 100 |
// Create diagonal matrix with n-m positive and m negative eigenvalues. |
| 101 |
Epetra_Map epetraMap( NumGlobalElements, 0, Comm ); |
| 102 |
|
| 103 |
Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix( Epetra_DataAccess::Copy, epetraMap, 1 ) ); |
| 104 |
for ( int k=0; k<epetraMap.NumMyElements(); k++ ) |
| 105 |
{ |
| 106 |
int GIDk = epetraMap.GID(k); |
| 107 |
double val = 2*(GIDk-m) + 1; |
| 108 |
TEUCHOS_ASSERT_EQUALITY( 0, A->InsertGlobalValues( GIDk, 1, &val, &GIDk ) ); |
| 109 |
} |
| 110 |
TEUCHOS_ASSERT_EQUALITY( 0, A->FillComplete() ); |
| 111 |
TEUCHOS_ASSERT_EQUALITY( 0, A->OptimizeStorage() ); |
| 112 |
|
| 113 |
// |
| 114 |
// Make some (multi)vectors, for use in testing: an exact solution, |
| 115 |
// its corresponding right-hand side, and an initial guess. |
| 116 |
// |
| 117 |
|
| 118 |
// Make a random exact solution. |
| 119 |
Teuchos::RCP<Epetra_MultiVector> X_exact (new Epetra_MultiVector (epetraMap, numrhs)); |
| 120 |
MVT::MvRandom (*X_exact); |
| 121 |
|
| 122 |
// Compute the right-hand side as B = A*X. |
| 123 |
Teuchos::RCP<Epetra_MultiVector> B = MVT::Clone (*X_exact, numrhs); |
| 124 |
OPT::Apply (*A, *X_exact, *B); |
| 125 |
|
| 126 |
// Choose an initial guess of all zeros. |
| 127 |
Teuchos::RCP<Epetra_MultiVector> X = MVT::Clone (*X_exact, numrhs); |
| 128 |
MVT::MvInit (*X, ST(0.0)); |
| 129 |
|
| 130 |
// ********************************************************************** |
| 131 |
// |
| 132 |
// Set parameters that the user asked us to pick intelligently. |
| 133 |
// |
| 134 |
|
| 135 |
// Maximum number of iterations: set according to problem size. |
| 136 |
// maxiters=numGlobalElements-1 may not always converge, so we |
| 137 |
// set to numGlobalElements+1 for good measure. |
| 138 |
if (maxiters == -1) |
| 139 |
maxiters = NumGlobalElements + 1; |
| 140 |
|
| 141 |
// In a nonverbose test, the frequency should be set to -1, which |
| 142 |
// Belos interprets as "no intermediate status output." Override |
| 143 |
// whatever the user may have specified. |
| 144 |
if (! verbose) |
| 145 |
frequency = -1; |
| 146 |
// Silently fix a bad frequency value. |
| 147 |
else if (frequency < 0 && frequency != -1) |
| 148 |
frequency = -1; |
| 149 |
|
| 150 |
// Validate command-line arguments |
| 151 |
TEUCHOS_TEST_FOR_EXCEPTION( tol < 0, std::invalid_argument, |
| 152 |
"Relative residual tolerance must be nonnegative, but " |
| 153 |
"you supplied tol = " << tol << "." ); |
| 154 |
TEUCHOS_TEST_FOR_EXCEPTION( numrhs < 1, std::invalid_argument, |
| 155 |
"MINRES test requires at least one right-hand side, but " |
| 156 |
"you set the number of right-hand sides to " |
| 157 |
<< numrhs << "." ); |
| 158 |
TEUCHOS_TEST_FOR_EXCEPTION( maxiters < 1, std::invalid_argument, |
| 159 |
"MINRES test requires at least one iteration, but you " |
| 160 |
"set the maximum number of iterations to " |
| 161 |
<< maxiters << "." ); |
| 162 |
|
| 163 |
const bool proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */ |
| 164 |
|
| 165 |
// |
| 166 |
// ********Other information used by block solver*********** |
| 167 |
// *****************(can be user specified)****************** |
| 168 |
// |
| 169 |
|
| 170 |
// |
| 171 |
ParameterList belosList; |
| 172 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 173 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 174 |
|
| 175 |
int verbosity = Belos::Errors + Belos::Warnings; |
| 176 |
if (verbose) { |
| 177 |
verbosity += Belos::TimingDetails + Belos::StatusTestDetails; |
| 178 |
if (frequency > 0) |
| 179 |
belosList.set( "Output Frequency", frequency ); |
| 180 |
} |
| 181 |
if (debug) { |
| 182 |
verbosity += Belos::Debug; |
| 183 |
} |
| 184 |
belosList.set( "Verbosity", (int) verbosity ); |
| 185 |
// |
| 186 |
// Construct an unpreconditioned linear problem instance. |
| 187 |
// |
| 188 |
Belos::LinearProblem< ST, MV, OP > problem (A, X, B); |
| 189 |
{ |
| 190 |
const bool set = problem.setProblem(); |
| 191 |
TEUCHOS_TEST_FOR_EXCEPTION( set == false, std::logic_error, |
| 192 |
"Belos::LinearProblem failed to set up correctly (setP" |
| 193 |
"roblem() returned false)! This probably means we imp" |
| 194 |
"lemented our test incorrectly." ); |
| 195 |
} |
| 196 |
// |
| 197 |
// ******************************************************************* |
| 198 |
// ****************Start the MINRES iteration************************* |
| 199 |
// ******************************************************************* |
| 200 |
// |
| 201 |
// Create an iterative solver manager. |
| 202 |
RCP< Belos::SolverManager<ST,MV,OP> > newSolver |
| 203 |
= rcp( new Belos::MinresSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false))); |
| 204 |
|
| 205 |
// |
| 206 |
// **********Print out information about problem******************* |
| 207 |
// |
| 208 |
if (proc_verbose) { |
| 209 |
cout << endl << endl; |
| 210 |
cout << "Dimension of matrix: " << NumGlobalElements << endl; |
| 211 |
cout << "Number of right-hand sides: " << numrhs << endl; |
| 212 |
cout << "Relative residual tolerance: " << tol << endl; |
| 213 |
cout << endl; |
| 214 |
} |
| 215 |
// |
| 216 |
// Perform solve |
| 217 |
// |
| 218 |
Belos::ReturnType ret = Belos::Unconverged; |
| 219 |
try { |
| 220 |
ret = newSolver->solve(); |
| 221 |
} catch (std::exception& e) { |
| 222 |
if (MyPID == 0) |
| 223 |
cout << "MINRES solver threw an exception: " << e.what() << endl; |
| 224 |
throw e; |
| 225 |
} |
| 226 |
// |
| 227 |
// Compute actual residuals. |
| 228 |
// |
| 229 |
bool badRes = false; |
| 230 |
std::vector<double> actual_resids( numrhs ); |
| 231 |
std::vector<double> rhs_norm( numrhs ); |
| 232 |
Epetra_MultiVector resid(A->Map(), numrhs); |
| 233 |
OPT::Apply( *A, *X, resid ); |
| 234 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 235 |
MVT::MvNorm( resid, actual_resids ); |
| 236 |
MVT::MvNorm( *B, rhs_norm ); |
| 237 |
if (proc_verbose) { |
| 238 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 239 |
for ( int i=0; i<numrhs; i++) { |
| 240 |
double actRes = actual_resids[i]/rhs_norm[i]; |
| 241 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 242 |
if (actRes > tol) badRes = true; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if (ret!=Belos::Converged || badRes) { |
| 247 |
success = false; |
| 248 |
if (proc_verbose) |
| 249 |
std::cout << std::endl << "End Result: TEST FAILED" << std::endl; |
| 250 |
} else { |
| 251 |
success = true; |
| 252 |
if (proc_verbose) |
| 253 |
std::cout << std::endl << "End Result: TEST PASSED" << std::endl; |
| 254 |
} |
| 255 |
} |
| 256 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 257 |
|
| 258 |
#ifdef EPETRA_MPI |
| 259 |
MPI_Finalize(); |
| 260 |
#endif |
| 261 |
|
| 262 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 263 |
} |