Debian Codegraph @

packages/belos/epetra/test/TFQMR/test_pseudo_tfqmr_hb.cpp

raw
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 Harwell-Boeing (HB) file.
11 // The right-hand-side from the HB file is used instead of random vectors.
12 // The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosPseudoBlockTFQMRSolMgr.hpp"
20 #include "BelosEpetraUtils.h"
21 #include "Trilinos_Util.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Epetra_Map.h"
24 #include "Teuchos_CommandLineProcessor.hpp"
25 #include "Teuchos_ParameterList.hpp"
26 #include "Teuchos_StandardCatchMacros.hpp"
27
28 int main(int argc, char *argv[]) {
29 //
30 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
31 //
32 using Teuchos::ParameterList;
33 using Teuchos::RCP;
34 using Teuchos::rcp;
35
36 bool success = false;
37 bool verbose = false;
38 try {
39 //
40 // Get test parameters from command-line processor
41 //
42 bool proc_verbose = false;
43 int frequency = -1; // how often residuals are printed by solver
44 int numrhs = 1; // total number of right-hand sides to solve for
45 int maxiters = -1; // maximum number of iterations for solver to use
46 std::string filename("orsirr1.hb");
47 double tol = 1.0e-5; // relative residual tolerance
48
49 Teuchos::CommandLineProcessor cmdp(false,true);
50 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
51 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
52 cmdp.setOption("tol",&tol,"Relative residual tolerance used by pseudo-block TFQMR solver.");
53 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
54 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
55 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
56 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
57 return -1;
58 }
59 if (!verbose)
60 frequency = -1; // reset frequency if test is not verbose
61 //
62 // Get the problem
63 //
64 int MyPID;
65 RCP<Epetra_CrsMatrix> A;
66 RCP<Epetra_MultiVector> B, X;
67 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
68 if(return_val != 0) return return_val;
69 proc_verbose = ( verbose && (MyPID==0) );
70 //
71 // Solve using Belos
72 //
73 typedef double ST;
74 typedef Epetra_Operator OP;
75 typedef Epetra_MultiVector MV;
76 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
77 typedef Belos::MultiVecTraits<ST,MV> MVT;
78 //
79 // *****Construct initial guess and random right-hand-sides *****
80 //
81 if (numrhs == 1) {
82 MVT::MvInit( *X, 1.0 );
83 } else {
84 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
85 MVT::MvRandom( *X );
86 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
87 }
88 OPT::Apply( *A, *X, *B );
89 MVT::MvInit( *X, 0.0 );
90 //
91 // ********Other information used by block solver***********
92 // *****************(can be user specified)******************
93 //
94 const int NumGlobalElements = B->GlobalLength();
95 if (maxiters == -1)
96 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
97 //
98 ParameterList belosList;
99 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
100 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
101 if (verbose) {
102 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
103 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
104 if (frequency > 0)
105 belosList.set( "Output Frequency", frequency );
106 }
107 else
108 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
109 //
110 // Construct an unpreconditioned linear problem instance.
111 //
112 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
113 bool set = problem.setProblem();
114 if (set == false) {
115 if (proc_verbose)
116 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
117 return -1;
118 }
119 //
120 // Create an iterative solver manager.
121 //
122 RCP< Belos::SolverManager<double,MV,OP> > solver
123 = rcp( new Belos::PseudoBlockTFQMRSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
124 //
125 // **********Print out information about problem*******************
126 //
127 if (proc_verbose) {
128 std::cout << std::endl << std::endl;
129 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
130 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
131 std::cout << "Max number of TFQMR iterations: " << maxiters << std::endl;
132 std::cout << "Relative residual tolerance: " << tol << std::endl;
133 std::cout << std::endl;
134 }
135 //
136 // Perform solve
137 //
138 Belos::ReturnType ret = solver->solve();
139 //
140 // Compute actual residuals.
141 //
142 bool badRes = false;
143 std::vector<double> actual_resids( numrhs );
144 std::vector<double> rhs_norm( numrhs );
145 Epetra_MultiVector resid(A->Map(), numrhs);
146 OPT::Apply( *A, *X, resid );
147 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
148 MVT::MvNorm( resid, actual_resids );
149 MVT::MvNorm( *B, rhs_norm );
150 if (proc_verbose) {
151 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
152 for ( int i=0; i<numrhs; i++) {
153 double actRes = actual_resids[i]/rhs_norm[i];
154 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
155 if (actRes > tol) badRes = true;
156 }
157 }
158
159 success = ret==Belos::Converged && !badRes;
160
161 if (success) {
162 if (proc_verbose)
163 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
164 } else {
165 if (proc_verbose)
166 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
167 }
168 }
169 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
170
171 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
172 } // end test_bl_cg_hb.cpp