Debian Codegraph @

packages/belos/epetra/test/FixedPoint/test_fp_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 "BelosFixedPointSolMgr.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 typedef double ST;
36 typedef Epetra_Operator OP;
37 typedef Epetra_MultiVector MV;
38 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
39 typedef Belos::MultiVecTraits<ST,MV> MVT;
40
41 bool success = false;
42 bool verbose = false;
43 try {
44 //
45 // Get test parameters from command-line processor
46 //
47 bool proc_verbose = false;
48 int frequency = -1; // how often residuals are printed by solver
49 int numrhs = 1; // total number of right-hand sides to solve for
50 int maxiters = -1; // maximum number of iterations for solver to use
51 std::string filename("bcsstk14.hb");
52 double tol = 1.0e-5; // relative residual tolerance
53 bool precond = false; // use diagonal preconditioner
54 bool leftPrecond = false; // if preconditioner is used, left or right?
55
56
57 Teuchos::CommandLineProcessor cmdp(false,true);
58 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
59 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
60 cmdp.setOption("tol",&tol,"Relative residual tolerance used byfixed point solver.");
61 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
62 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
63 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem).");
64 cmdp.setOption("use-precond","no-precond",&precond,"Use a diagonal preconditioner.");
65 cmdp.setOption("left","right",&leftPrecond,"Use a left/right preconditioner.");
66 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
67 return -1;
68 }
69 if (!verbose)
70 frequency = -1; // reset frequency if test is not verbose
71 //
72 // Get the problem
73 //
74 int MyPID;
75 RCP<Epetra_CrsMatrix> A;
76 RCP<Epetra_MultiVector> B, X;
77 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
78 if(return_val != 0) return return_val;
79 proc_verbose = ( verbose && (MyPID==0) );
80
81 // Scale down the rhs
82 std::vector<double> norm( MVT::GetNumberVecs(*B));
83 MVT::MvNorm(*B,norm);
84 for(int i=0; i< MVT::GetNumberVecs(*B); i++)
85 norm[i] = 1.0 / norm[i];
86 MVT::MvScale(*B,norm);
87
88 // Drastically bump the diagonal and then rescale so FP can converge
89 Epetra_Vector diag(A->RowMap());
90 A->ExtractDiagonalCopy(diag);
91 for(int i=0; i<diag.MyLength(); i++)
92 diag[i]=diag[i]*1e4;
93 A->ReplaceDiagonalValues(diag);
94
95 for(int i=0; i<diag.MyLength(); i++)
96 diag[i] = 1.0/sqrt(diag[i]);
97 A->LeftScale(diag);
98 A->RightScale(diag);
99
100 //
101 // Solve using Belos
102 //
103
104 //
105 // *****Construct initial guess and random right-hand-sides *****
106 //
107 if (numrhs != 1) {
108 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
109 MVT::MvRandom( *X );
110 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
111 OPT::Apply( *A, *X, *B );
112 MVT::MvInit( *X, 0.0 );
113 }
114 //
115 // ********Other information used by block solver***********
116 // *****************(can be user specified)******************
117 //
118 const int NumGlobalElements = B->GlobalLength();
119 if (maxiters == -1)
120 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
121 //
122 ParameterList belosList;
123 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
124 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
125 if (verbose) {
126 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
127 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
128 if (frequency > 0)
129 belosList.set( "Output Frequency", frequency );
130 }
131 else
132 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
133 //
134 // Construct an linear problem instance.
135 //
136 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
137 // diagonal preconditioner
138 if (precond) {
139 Epetra_Vector diagonal(A->RowMap());
140 A->ExtractDiagonalCopy(diagonal);
141
142 int NumMyElements = diagonal.Map().NumMyElements();
143 Teuchos::ArrayView<const int> MyGlobalElements = Teuchos::ArrayView< const int >(diagonal.Map().MyGlobalElements(), NumMyElements);
144 RCP<Epetra_CrsMatrix> invDiagMatrix = Teuchos::rcp(new Epetra_CrsMatrix(Copy,A->RowMap(), 1, true));
145
146 for (Teuchos_Ordinal i=0; i<NumMyElements; ++i) {
147 diagonal[i] = 1.0 / diagonal[i];
148 invDiagMatrix->InsertGlobalValues(MyGlobalElements[i],
149 1,
150 &diagonal[i],
151 &MyGlobalElements[i] );
152 }
153 invDiagMatrix->FillComplete();
154
155 if (leftPrecond)
156 problem.setLeftPrec(invDiagMatrix);
157 else
158 problem.setRightPrec(invDiagMatrix);
159 }
160 bool set = problem.setProblem();
161 if (set == false) {
162 if (proc_verbose)
163 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
164 return -1;
165 }
166 //
167 // Create an iterative solver manager.
168 //
169 RCP< Belos::SolverManager<double,MV,OP> > newSolver
170 = rcp( new Belos::FixedPointSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
171 //
172 // **********Print out information about problem*******************
173 //
174 if (proc_verbose) {
175 std::cout << std::endl << std::endl;
176 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
177 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
178 std::cout << "Max number of iterations: " << maxiters << std::endl;
179 std::cout << "Relative residual tolerance: " << tol << std::endl;
180 std::cout << std::endl;
181 }
182 //
183 // Perform solve
184 //
185 Belos::ReturnType ret = newSolver->solve();
186 //
187 // Compute actual residuals.
188 //
189 bool badRes = false;
190 std::vector<double> actual_resids( numrhs );
191 std::vector<double> rhs_norm( numrhs );
192 Epetra_MultiVector resid(A->Map(), numrhs);
193 OPT::Apply( *A, *X, resid );
194 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
195 MVT::MvNorm( resid, actual_resids );
196 MVT::MvNorm( *B, rhs_norm );
197 if (proc_verbose) {
198 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
199 for ( int i=0; i<numrhs; i++) {
200 double actRes = actual_resids[i]/rhs_norm[i];
201 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
202 if (actRes > tol) badRes = true;
203 }
204 }
205
206 success = ret==Belos::Converged && !badRes;
207
208 if (success) {
209 if (proc_verbose)
210 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
211 } else {
212 if (proc_verbose)
213 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
214 }
215 }
216 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
217
218 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
219 }