2
// ***********************************************************************
4
// Belos: Block Linear Solvers Package
5
// Copyright (2004) Sandia Corporation
7
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8
// license for use of this work by or on behalf of the U.S. Government.
10
// This library is free software; you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as
12
// published by the Free Software Foundation; either version 2.1 of the
13
// License, or (at your option) any later version.
15
// This library is distributed in the hope that it will be useful, but
16
// WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
// Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public
21
// License along with this library; if not, write to the Free Software
22
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
26
// ***********************************************************************
29
// This driver reads a problem from a Harwell-Boeing (HB) file.
30
// The right-hand-side from the HB file is used instead of random vectors.
31
// The initial guesses are all set to zero.
33
// NOTE: No preconditioner is used in this case.
35
#include "BelosConfigDefs.hpp"
36
#include "BelosLinearProblem.hpp"
37
#include "BelosEpetraAdapter.hpp"
38
#include "BelosTFQMRSolMgr.hpp"
39
#include "createEpetraProblem.hpp"
40
#include "Trilinos_Util.h"
41
#include "Epetra_CrsMatrix.h"
42
#include "Epetra_Map.h"
43
#include "Teuchos_CommandLineProcessor.hpp"
44
#include "Teuchos_ParameterList.hpp"
46
int main(int argc, char *argv[]) {
50
MPI_Init(&argc,&argv);
51
Belos::MPIFinalize mpiFinalize; // Will call finalize with *any* return
58
// Get test parameters from command-line processor
60
bool verbose = false, proc_verbose = false;
61
int frequency = -1; // how often residuals are printed by solver
62
int numrhs = 1; // total number of right-hand sides to solve for
63
int blocksize = 1; // blocksize used by solver
64
int maxiters = -1; // maximum number of iterations for solver to use
65
std::string filename("orsirr1.hb");
66
double tol = 1.0e-5; // relative residual tolerance
68
Teuchos::CommandLineProcessor cmdp(false,true);
69
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
70
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
71
cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver.");
72
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
73
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
74
cmdp.setOption("block-size",&blocksize,"Block size to be used by TFQMR solver.");
75
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
76
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
80
frequency = -1; // reset frequency if test is not verbose
85
RCP<Epetra_CrsMatrix> A;
86
RCP<Epetra_MultiVector> B, X;
87
int return_val =Belos::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
88
if(return_val != 0) return return_val;
89
proc_verbose = ( verbose && (MyPID==0) );
94
typedef Epetra_Operator OP;
95
typedef Epetra_MultiVector MV;
96
typedef Belos::OperatorTraits<ST,MV,OP> OPT;
97
typedef Belos::MultiVecTraits<ST,MV> MVT;
99
// *****Construct initial guess and random right-hand-sides *****
102
MVT::MvInit( *X, 1.0 );
104
X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
106
B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
108
OPT::Apply( *A, *X, *B );
109
MVT::MvInit( *X, 0.0 );
111
// ********Other information used by block solver***********
112
// *****************(can be user specified)******************
114
const int NumGlobalElements = B->GlobalLength();
116
maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
118
ParameterList belosList;
119
belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
120
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
121
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
123
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
124
Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
126
belosList.set( "Output Frequency", frequency );
129
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
131
// Construct an unpreconditioned linear problem instance.
133
Belos::LinearProblem<double,MV,OP> problem( A, X, B );
134
bool set = problem.setProblem();
137
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
141
// Create an iterative solver manager.
143
RCP< Belos::SolverManager<double,MV,OP> > solver
144
= rcp( new Belos::TFQMRSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
146
// **********Print out information about problem*******************
149
std::cout << std::endl << std::endl;
150
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
151
std::cout << "Number of right-hand sides: " << numrhs << std::endl;
152
std::cout << "Block size used by solver: " << blocksize << std::endl;
153
std::cout << "Max number of TFQMR iterations: " << maxiters << std::endl;
154
std::cout << "Relative residual tolerance: " << tol << std::endl;
155
std::cout << std::endl;
160
Belos::ReturnType ret = solver->solve();
162
// Compute actual residuals.
165
std::vector<double> actual_resids( numrhs );
166
std::vector<double> rhs_norm( numrhs );
167
Epetra_MultiVector resid(A->Map(), numrhs);
168
OPT::Apply( *A, *X, resid );
169
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
170
MVT::MvNorm( resid, actual_resids );
171
MVT::MvNorm( *B, rhs_norm );
173
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
174
for ( int i=0; i<numrhs; i++) {
175
double actRes = actual_resids[i]/rhs_norm[i];
176
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
177
if (actRes > tol) badRes = true;
181
if (ret!=Belos::Converged || badRes) {
183
std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
187
// Default return value
190
std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
193
} // end test_bl_cg_hb.cpp