Debian Codegraph @

packages/belos/epetra/test/TFQMR/test_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 "BelosTFQMRSolMgr.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 bool explicit_test = false;
44 bool comp_recursive = false;
45 int frequency = -1; // how often residuals are printed by solver
46 int numrhs = 1; // total number of right-hand sides to solve for
47 int maxiters = -1; // maximum number of iterations for solver to use
48 std::string filename("orsirr1.hb");
49 double tol = 1.0e-5; // relative residual tolerance
50
51 Teuchos::CommandLineProcessor cmdp(false,true);
52 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
53
54 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
55 cmdp.setOption("explicit","implicit-only",&explicit_test,"Compute explicit residuals.");
56 cmdp.setOption("recursive","native",&comp_recursive,"Compute recursive residuals.");
57 cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver.");
58 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
59 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
60 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem size).");
61 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
62 return -1;
63 }
64 if (!verbose)
65 frequency = -1; // reset frequency if test is not verbose
66 //
67 // Get the problem
68 //
69 int MyPID;
70 RCP<Epetra_CrsMatrix> A;
71 RCP<Epetra_MultiVector> B, X;
72 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
73 if(return_val != 0) return return_val;
74 proc_verbose = ( verbose && (MyPID==0) );
75 //
76 // Solve using Belos
77 //
78 typedef double ST;
79 typedef Epetra_Operator OP;
80 typedef Epetra_MultiVector MV;
81 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
82 typedef Belos::MultiVecTraits<ST,MV> MVT;
83 //
84 // *****Construct initial guess and random right-hand-sides *****
85 //
86 if (numrhs == 1) {
87 MVT::MvInit( *X, 1.0 );
88 } else {
89 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
90 MVT::MvRandom( *X );
91 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
92 }
93 OPT::Apply( *A, *X, *B );
94 MVT::MvInit( *X, 0.0 );
95 //
96 // ********Other information used by block solver***********
97 // *****************(can be user specified)******************
98 //
99 const int NumGlobalElements = B->GlobalLength();
100 if (maxiters == -1)
101 maxiters = NumGlobalElements; // maximum number of iterations to run
102 //
103 ParameterList belosList;
104 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
105 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
106 if (explicit_test)
107 {
108 belosList.set( "Explicit Residual Test", true ); // Scale by norm of right-hand side vector."
109 belosList.set( "Explicit Residual Scaling", "Norm of RHS" ); // Scale by norm of right-hand side vector."
110 }
111 if (comp_recursive)
112 {
113 belosList.set( "Compute Recursive Residuals", true );
114 }
115 if (verbose) {
116 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
117 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
118 if (frequency > 0)
119 belosList.set( "Output Frequency", frequency );
120 }
121 else
122 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
123 //
124 // Construct an unpreconditioned linear problem instance.
125 //
126 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
127 bool set = problem.setProblem();
128 if (set == false) {
129 if (proc_verbose)
130 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
131 return -1;
132 }
133 //
134 // Create an iterative solver manager.
135 //
136 RCP< Belos::SolverManager<double,MV,OP> > solver
137 = rcp( new Belos::TFQMRSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
138 //
139 // **********Print out information about problem*******************
140 //
141 if (proc_verbose) {
142 std::cout << std::endl << std::endl;
143 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
144 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
145 std::cout << "Max number of TFQMR iterations: " << maxiters << std::endl;
146 std::cout << "Relative residual tolerance: " << tol << std::endl;
147 std::cout << std::endl;
148 }
149 //
150 // Perform solve
151 //
152 Belos::ReturnType ret = solver->solve();
153 //
154 // Compute actual residuals.
155 //
156 bool badRes = false;
157 std::vector<double> actual_resids( numrhs );
158 std::vector<double> rhs_norm( numrhs );
159 Epetra_MultiVector resid(A->Map(), numrhs);
160 OPT::Apply( *A, *X, resid );
161 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
162 MVT::MvNorm( resid, actual_resids );
163 MVT::MvNorm( *B, rhs_norm );
164 if (proc_verbose) {
165 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
166 for ( int i=0; i<numrhs; i++) {
167 double actRes = actual_resids[i]/rhs_norm[i];
168 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
169 if (actRes > tol) badRes = true;
170 }
171 }
172
173 success = ret==Belos::Converged && !badRes;
174
175 if (success) {
176 if (proc_verbose)
177 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
178 } else {
179 if (proc_verbose)
180 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
181 }
182 }
183 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
184
185 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
186 } // end test_bl_cg_hb.cpp