Debian Codegraph @

packages/belos/epetra/test/BlockCG/test_pseudo_cg_indefinite.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 // NOTE: No preconditioner is used in this example.
11 //
12 #include "BelosConfigDefs.hpp"
13 #include "BelosLinearProblem.hpp"
14 #include "BelosEpetraAdapter.hpp"
15 #include "BelosPseudoBlockCGSolMgr.hpp"
16
17 #include "Epetra_Map.h"
18 #ifdef EPETRA_MPI
19 #include "Epetra_MpiComm.h"
20 #else
21 #include "Epetra_SerialComm.h"
22 #endif
23 #include "Epetra_CrsMatrix.h"
24
25 #include "Teuchos_CommandLineProcessor.hpp"
26 #include "Teuchos_ParameterList.hpp"
27 #include "Teuchos_StandardCatchMacros.hpp"
28
29 int main(int argc, char *argv[]) {
30 //
31 int MyPID = 0;
32 #ifdef EPETRA_MPI
33 // Initialize MPI
34 MPI_Init(&argc,&argv);
35 Epetra_MpiComm Comm(MPI_COMM_WORLD);
36 MyPID = Comm.MyPID();
37 #else
38 Epetra_SerialComm Comm;
39 #endif
40 //
41 typedef double ST;
42 typedef Teuchos::ScalarTraits<ST> SCT;
43 typedef SCT::magnitudeType MT;
44 typedef Epetra_MultiVector MV;
45 typedef Epetra_Operator OP;
46 typedef Belos::MultiVecTraits<ST,MV> MVT;
47 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
48
49 using Teuchos::ParameterList;
50 using Teuchos::RCP;
51 using Teuchos::rcp;
52
53 bool success = false;
54 bool verbose = false;
55
56 try {
57 bool debug = false, proc_verbose = false;
58 int frequency = -1; // frequency of status test output.
59 int numrhs = 1; // number of right-hand sides to solve for
60 int maxiters = -1; // maximum number of iterations allowed per linear system
61 MT tol = 1.0e-10; // relative residual tolerance
62
63 Teuchos::CommandLineProcessor cmdp(false,true);
64 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
65 cmdp.setOption("debug","nodebug",&debug,"Print debugging information from the solver.");
66 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
67 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
68 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
69 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
70 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
71 return -1;
72 }
73 if (!verbose)
74 frequency = -1; // reset frequency if test is not verbose
75 // **********************************************************************
76 // ******************Set up the problem to be solved*********************
77 // construct diagonal matrix
78 const int NumGlobalElements = 100;
79 const int m = 4; // number of negative eigenvalues
80
81 // Create diagonal matrix with n-m positive and m negative eigenvalues.
82 Epetra_Map epetraMap( NumGlobalElements, 0, Comm );
83
84 Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix( Epetra_DataAccess::Copy, epetraMap, 1 ) );
85 for ( int k=0; k<epetraMap.NumMyElements(); k++ )
86 {
87 int GIDk = epetraMap.GID(k);
88 double val = 2*(GIDk-m) + 1;
89 TEUCHOS_ASSERT_EQUALITY( 0, A->InsertGlobalValues( GIDk, 1, &val, &GIDk ) );
90 }
91 TEUCHOS_ASSERT_EQUALITY( 0, A->FillComplete() );
92 TEUCHOS_ASSERT_EQUALITY( 0, A->OptimizeStorage() );
93
94 // create initial guess and right-hand side
95 Teuchos::RCP<Epetra_MultiVector> vecX = Teuchos::rcp( new Epetra_MultiVector( epetraMap, numrhs ) );
96
97 Teuchos::RCP<Epetra_MultiVector> vecB = Teuchos::rcp( new Epetra_MultiVector( epetraMap, numrhs ) );
98 // **********************************************************************
99 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
100
101
102 Teuchos::RCP<Epetra_MultiVector> X;
103 Teuchos::RCP<Epetra_MultiVector> B;
104 // Check to see if the number of right-hand sides is the same as requested.
105 if (numrhs>1) {
106 X = rcp( new Epetra_MultiVector( epetraMap, numrhs ) );
107 B = rcp( new Epetra_MultiVector( epetraMap, numrhs ) );
108 X->Random();
109 OPT::Apply( *A, *X, *B );
110 X->PutScalar( 0.0 );
111 }
112 else {
113 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
114 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
115 B->PutScalar( 1.0 );
116 }
117 //
118 // ********Other information used by block solver***********
119 // *****************(can be user specified)******************
120 //
121 if (maxiters == -1)
122 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
123 //
124 ParameterList belosList;
125 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
126 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
127 belosList.set( "Assert Positive Definiteness", false ); // Explicitly don't enforce positive definiteness
128
129 int verbosity = Belos::Errors + Belos::Warnings;
130 if (verbose) {
131 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
132 if (frequency > 0)
133 belosList.set( "Output Frequency", frequency );
134 }
135 if (debug) {
136 verbosity += Belos::Debug;
137 }
138 belosList.set( "Verbosity", verbosity );
139 //
140 // Construct an unpreconditioned linear problem instance.
141 //
142 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
143 bool set = problem.setProblem();
144 if (set == false) {
145 if (proc_verbose)
146 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
147 return -1;
148 }
149 //
150 // *******************************************************************
151 // ****************Start the CG iteration*************************
152 // *******************************************************************
153 //
154 // Create an iterative solver manager.
155 RCP<Belos::SolverManager<double,MV,OP> > newSolver
156 = rcp( new Belos::PseudoBlockCGSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
157
158 //
159 // **********Print out information about problem*******************
160 //
161 if (proc_verbose) {
162 std::cout << std::endl << std::endl;
163 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
164 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
165 std::cout << "Relative residual tolerance: " << tol << std::endl;
166 std::cout << std::endl;
167 }
168 //
169 // Perform solve
170 //
171 Belos::ReturnType ret = newSolver->solve();
172 //
173 // Get the number of iterations for this solve.
174 //
175 int numIters = newSolver->getNumIters();
176 if (proc_verbose)
177 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
178 //
179 // Compute actual residuals.
180 //
181 bool badRes = false;
182 std::vector<double> actual_resids( numrhs );
183 std::vector<double> rhs_norm( numrhs );
184 Epetra_MultiVector resid(epetraMap, numrhs);
185 OPT::Apply( *A, *X, resid );
186 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
187 MVT::MvNorm( resid, actual_resids );
188 MVT::MvNorm( *B, rhs_norm );
189 if (proc_verbose) {
190 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
191 for ( int i=0; i<numrhs; i++) {
192 double actRes = actual_resids[i]/rhs_norm[i];
193 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
194 if (actRes > tol) badRes = true;
195 }
196 }
197
198 success = ret==Belos::Converged && !badRes;
199
200 if (success) {
201 if (proc_verbose)
202 std::cout << "End Result: TEST PASSED" << std::endl;
203 } else {
204 if (proc_verbose)
205 std::cout << "End Result: TEST FAILED" << std::endl;
206 }
207 }
208 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
209
210 #ifdef EPETRA_MPI
211 MPI_Finalize();
212 #endif
213
214 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
215 }