Debian Codegraph @

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