Debian Codegraph @

packages/belos/tpetra/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 "BelosTpetraAdapter.hpp"
15 #include "BelosPseudoBlockCGSolMgr.hpp"
16
17 #include <Teuchos_GlobalMPISession.hpp>
18
19 #include <Tpetra_Core.hpp>
20 #include <Tpetra_CrsMatrix.hpp>
21
22 template<typename ScalarType>
23 int run(int argc, char *argv[]) {
24 using Teuchos::CommandLineProcessor;
25 using Teuchos::GlobalMPISession;
26 using Teuchos::ParameterList;
27 using Teuchos::RCP;
28 using Teuchos::rcp;
29 using Teuchos::rcp_implicit_cast;
30 using Teuchos::tuple;
31
32 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
33 using LO = typename Tpetra::Vector<>::local_ordinal_type;
34 using GO = typename Tpetra::Vector<>::global_ordinal_type;
35 using NT = typename Tpetra::Vector<>::node_type;
36
37 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
38 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
39 using MAP = typename Tpetra::Map<LO,GO,NT>;
40
41 using MVT = typename Belos::MultiVecTraits<ST,MV>;
42 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
43 using MAT = Tpetra::CrsMatrix<ST,LO,GO,NT>;
44 using SCT = typename Teuchos::ScalarTraits<ST>;
45 using MT = typename SCT::magnitudeType;
46
47 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
48 RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm();
49
50 bool success = false;
51 bool verbose = false;
52
53 try
54 {
55 // Get test parameters from command-line processor
56 //
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 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) != 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 GO numGlobalElements = 100;
79 const int m = 4; // number of negative eigenvalues
80 // Create diagonal matrix with n-m positive and m negative eigenvalues.
81 RCP<const MAP> map = rcp(new MAP(numGlobalElements, 0, comm));
82
83 RCP<MAT> A = rcp( new MAT(map, 1) );
84
85 for ( size_t k = 0; k < map->getLocalNumElements(); k++ )
86 {
87 GO gid = map->getGlobalElement(k);
88 const ST val = static_cast<ST> (2 * (gid-m) + 1);
89 A->insertGlobalValues(gid, tuple<GO> (gid), tuple<ST> (val));
90 }
91
92 A->fillComplete();
93 TEUCHOS_ASSERT(A->isStorageOptimized());
94
95 // create initial guess and right-hand side
96 RCP<MV> vecX = rcp( new MV( map, numrhs ) );
97
98 RCP<MV> vecB = rcp( new MV( map, numrhs ) );
99 // **********************************************************************
100
101 proc_verbose = ( verbose && (comm->getRank()==0) ); /* Only print on the zero processor */
102
103 RCP<MV> X;
104 RCP<MV> B;
105 // Check to see if the number of right-hand sides is the same as requested.
106 if (numrhs>1) {
107 X = rcp( new MV( map, numrhs ) );
108 B = rcp( new MV( map, numrhs ) );
109 X->randomize();
110 OPT::Apply( *A, *X, *B );
111 X->putScalar( 0.0 );
112 }
113 else {
114 X = rcp_implicit_cast<MV>(vecX);
115 B = rcp_implicit_cast<MV>(vecB);
116 B->putScalar( 1.0 );
117 }
118 //
119 // ********Other information used by block solver***********
120 // *****************(can be user specified)******************
121 //
122 if (maxiters == -1)
123 maxiters = numGlobalElements - 1; // maximum number of iterations to run
124 //
125 ParameterList belosList;
126 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
127 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
128 belosList.set( "Assert Positive Definiteness", false ); // Explicitly don't enforce positive definiteness
129
130 int verbosity = Belos::Errors + Belos::Warnings;
131 if (verbose) {
132 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
133 if (frequency > 0)
134 belosList.set( "Output Frequency", frequency );
135 }
136 if (debug) {
137 verbosity += Belos::Debug;
138 }
139 belosList.set( "Verbosity", verbosity );
140 //
141 // Construct an unpreconditioned linear problem instance.
142 //
143 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
144 bool set = problem.setProblem();
145 if (set == false) {
146 if (proc_verbose)
147 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
148 return -1;
149 }
150 //
151 // *******************************************************************
152 // ****************Start the CG iteration*************************
153 // *******************************************************************
154 //
155 // Create an iterative solver manager.
156 RCP<Belos::SolverManager<ST,MV,OP> > newSolver
157 = rcp( new Belos::PseudoBlockCGSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
158
159 //
160 // **********Print out information about problem*******************
161 //
162 if (proc_verbose) {
163 std::cout << std::endl << std::endl;
164 std::cout << "Dimension of matrix: " << numGlobalElements << std::endl;
165 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
166 std::cout << "Relative residual tolerance: " << tol << std::endl;
167 std::cout << std::endl;
168 }
169 //
170 // Perform solve
171 //
172 Belos::ReturnType ret = newSolver->solve();
173 //
174 // Get the number of iterations for this solve.
175 //
176 int numIters = newSolver->getNumIters();
177 if (proc_verbose)
178 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
179 //
180 // Compute actual residuals.
181 //
182
183 bool badRes = false;
184
185 std::vector<ST> actual_resids( numrhs );
186 std::vector<ST> rhs_norm( numrhs );
187 MV resid(map, numrhs);
188 OPT::Apply( *A, *X, resid );
189 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
190 MVT::MvNorm( resid, actual_resids );
191 MVT::MvNorm( *B, rhs_norm );
192 if (proc_verbose) {
193 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
194 for ( int i=0; i<numrhs; i++) {
195 ST actRes = actual_resids[i]/rhs_norm[i];
196 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
197 if (actRes > tol) badRes = true;
198 }
199 }
200
201 success = ret==Belos::Converged && !badRes;
202
203 if (success) {
204 if (proc_verbose)
205 std::cout << "End Result: TEST PASSED" << std::endl;
206 } else {
207 if (proc_verbose)
208 std::cout << "End Result: TEST FAILED" << std::endl;
209 }
210 }
211 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
212
213 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
214 }
215
216 int main(int argc, char *argv[]) {
217 return run<double>(argc,argv);
218 } // end test_pseudo_cg_indefinite.cpp