Debian Codegraph @

packages/belos/epetra/test/MultiMatrixSolve/test_bl_gmres_multidiag.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 test generates diagonal matrices for pseudo block GMRES to solve.
11 //
12 // NOTE: No preconditioner is used in this case.
13 //
14
15 #include <BelosConfigDefs.hpp>
16 #include "BelosLinearMultiShiftProblem.hpp"
17 #include <BelosEpetraAdapter.hpp>
18 #include <BelosEpetraOperator.h>
19 #include <BelosPseudoBlockGmresSolMgr.hpp>
20
21 #include <Epetra_Comm.h>
22 #include <Epetra_Map.h>
23 #include <Epetra_MultiVector.h>
24 #include <Epetra_Vector.h>
25 #include <Epetra_CrsMatrix.h>
26
27 #include <Teuchos_Time.hpp>
28 #include "Teuchos_StandardCatchMacros.hpp"
29
30 #ifdef EPETRA_MPI
31 #include <Epetra_MpiComm.h>
32 #include <mpi.h>
33 #else
34 #include <Epetra_SerialComm.h>
35 #endif
36
37 using std::vector;
38 using namespace Belos;
39
40 //************************************************************************************************
41
42 class Vector_Operator
43 {
44 public:
45
46 Vector_Operator(int m_in, int n_in) : m(m_in), n(n_in) {};
47
48 virtual ~Vector_Operator() {};
49
50 virtual void operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y) = 0;
51
52 int size (int dim) const { return (dim == 1) ? m : n; };
53
54 protected:
55
56 int m, n; // an (m x n) operator
57
58 private:
59
60 // Not allowing copy construction.
61 Vector_Operator( const Vector_Operator& ): m(0), n(0) {};
62 Vector_Operator* operator=( const Vector_Operator& ) { return NULL; };
63
64 };
65
66 //************************************************************************************************
67
68 class Diagonal_Operator : public Vector_Operator
69 {
70 public:
71
72 Diagonal_Operator(int n_in, double v_in) : Vector_Operator(n_in, n_in), v(v_in) { };
73
74 ~Diagonal_Operator() { };
75
76 void operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y)
77 {
78 y.Scale( v, x );
79 };
80
81 private:
82
83 double v;
84 };
85
86 //************************************************************************************************
87
88 class Diagonal_Operator_2 : public Vector_Operator
89 {
90 public:
91
92 Diagonal_Operator_2(int n_in, double v_in) : Vector_Operator(n_in, n_in), v(v_in) { };
93
94 ~Diagonal_Operator_2() { };
95
96 void operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y)
97 {
98 int myCols = y.MyLength();
99 for (int j=0; j < x.NumVectors(); ++j) {
100 for (int i=0; i < myCols; ++i) (*y(j))[i] = (i+1)*v*(*x(j))[i]; // NOTE: square operator!
101 }
102 };
103
104 private:
105
106 double v;
107 };
108
109 //************************************************************************************************
110
111 class Composed_Operator : public Vector_Operator
112 {
113 public:
114
115 Composed_Operator(int n,
116 const Teuchos::RCP<Vector_Operator>& pA_in,
117 const Teuchos::RCP<Vector_Operator>& pB_in);
118
119 virtual ~Composed_Operator() {};
120
121 virtual void operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y);
122
123 private:
124
125 Teuchos::RCP<Vector_Operator> pA;
126 Teuchos::RCP<Vector_Operator> pB;
127 };
128
129 Composed_Operator::Composed_Operator(int n_in,
130 const Teuchos::RCP<Vector_Operator>& pA_in,
131 const Teuchos::RCP<Vector_Operator>& pB_in)
132 : Vector_Operator(n_in, n_in), pA(pA_in), pB(pB_in)
133 {
134 }
135
136 void Composed_Operator::operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y)
137 {
138 Epetra_MultiVector ytemp(y.Map(), y.NumVectors(), false);
139 (*pB)( x, ytemp );
140 (*pA)( ytemp, y );
141 }
142
143 //************************************************************************************************
144
145 class Trilinos_Interface : public Epetra_Operator
146 {
147 public:
148
149 Trilinos_Interface(const Teuchos::RCP<Vector_Operator> pA_in,
150 const Teuchos::RCP<const Epetra_Comm> pComm_in,
151 const Teuchos::RCP<const Epetra_Map> pMap_in)
152 : pA (pA_in),
153 pComm (pComm_in),
154 pMap (pMap_in),
155 use_transpose (false)
156 {}
157
158 int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
159
160 int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
161 {
162 return(Apply(X,Y)); // No inverse
163 };
164
165 virtual ~Trilinos_Interface() {};
166
167 const char * Label() const {return("Trilinos_Interface, an Epetra_Operator implementation");};
168
169 bool UseTranspose() const {return(use_transpose);}; // always set to false
170
171 int SetUseTranspose(bool UseTranspose_in) { use_transpose = false; return(-1); };
172
173 bool HasNormInf() const {return(false);}; // cannot return inf-norm
174
175 double NormInf() const {return(0.0);};
176
177 virtual const Epetra_Comm & Comm() const {return *pComm; }
178
179 virtual const Epetra_Map & OperatorDomainMap() const {return *pMap; }
180
181 virtual const Epetra_Map & OperatorRangeMap() const {return *pMap; }
182
183 private:
184
185 Teuchos::RCP<Vector_Operator> pA;
186 Teuchos::RCP<const Epetra_Comm> pComm;
187 Teuchos::RCP<const Epetra_Map> pMap;
188
189 bool use_transpose;
190 };
191
192 int Trilinos_Interface::Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
193 {
194 (*pA)(X,Y);
195
196 return(0);
197 }
198
199 //************************************************************************************************
200
201 class Iterative_Inverse_Operator : public Vector_Operator
202 {
203
204 typedef Epetra_MultiVector MV;
205 typedef Epetra_Operator OP;
206
207 public:
208
209 Iterative_Inverse_Operator(int n_in, int blocksize,
210 const Teuchos::RCP<Vector_Operator>& pA_in,
211 std::string opString="Iterative Solver", bool print_in=true);
212
213 virtual ~Iterative_Inverse_Operator() {}
214
215 virtual void operator () (const Epetra_MultiVector &b, Epetra_MultiVector &x);
216
217 private:
218
219 Teuchos::RCP<Vector_Operator> pA; // operator which will be inverted
220 // supplies a matrix std::vector multiply
221 const bool print;
222
223 Teuchos::Time timer;
224 Teuchos::RCP<Epetra_Comm> pComm;
225 Teuchos::RCP<Epetra_Map> pMap;
226
227 Teuchos::RCP<OP> pPE;
228 Teuchos::RCP<Teuchos::ParameterList> pList;
229 Teuchos::RCP<LinearMultiShiftProblem<double,MV,OP> > pProb;
230 Teuchos::RCP<PseudoBlockGmresSolMgr<double,MV,OP> > pBelos;
231 };
232
233 Iterative_Inverse_Operator::Iterative_Inverse_Operator(int n_in, int blocksize,
234 const Teuchos::RCP<Vector_Operator>& pA_in,
235 std::string opString, bool print_in)
236 : Vector_Operator(n_in, n_in), // square operator
237 pA(pA_in),
238 print(print_in),
239 timer(opString)
240 {
241
242 int n_global;
243
244 #ifdef EPETRA_MPI
245 MPI_Allreduce(&n, &n_global, 1, MPI_UNSIGNED, MPI_SUM, MPI_COMM_WORLD);
246 pComm = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_WORLD) );
247 #else
248 pComm = Teuchos::rcp( new Epetra_SerialComm() );
249 n_global = n;
250 #endif
251 pMap = Teuchos::rcp( new Epetra_Map(n_global, n, 0, *pComm) );
252
253 pPE = Teuchos::rcp( new Trilinos_Interface(pA, pComm, pMap ) );
254
255 pProb = Teuchos::rcp( new LinearMultiShiftProblem<double,MV,OP>() );
256 pProb->setOperator( pPE );
257 pProb->setShift( true );
258
259 int restart = 10;
260 int max_iter = 100;
261 double tol = 1.0e-10;
262 int verbosity = Belos::Errors + Belos::Warnings;
263 if (print)
264 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
265
266 pList = Teuchos::rcp( new Teuchos::ParameterList );
267 pList->set( "Num Blocks", n );
268 pList->set( "Block Size", blocksize );
269 pList->set( "Maximum Iterations", max_iter );
270 pList->set( "Maximum Restarts", restart );
271 pList->set( "Convergence Tolerance", tol );
272 pList->set( "Verbosity", verbosity );
273
274 pBelos = Teuchos::rcp( new PseudoBlockGmresSolMgr<double,MV,OP>(pProb, pList) );
275 }
276
277 void Iterative_Inverse_Operator::operator () (const Epetra_MultiVector &b, Epetra_MultiVector &x)
278 {
279 // Initialize the solution to zero
280 x.PutScalar( 0.0 );
281
282 // Reset the solver, problem, and status test for next solve (HKT)
283 pProb->setProblem( Teuchos::rcp(&x, false), Teuchos::rcp(&b, false) );
284
285 timer.start();
286 Belos::ReturnType ret = pBelos->solve();
287 timer.stop();
288
289 int pid = pComm->MyPID();
290
291 if (pid == 0 && print) {
292 if (ret == Belos::Converged)
293 {
294 std::cout << std::endl << "pid[" << pid << "] Pseudo Block GMRES converged" << std::endl;
295 std::cout << "Solution time: " << timer.totalElapsedTime() << std::endl;
296
297 }
298 else
299 std::cout << std::endl << "pid[" << pid << "] Pseudo Block GMRES did not converge" << std::endl;
300 }
301 }
302
303 //************************************************************************************************
304 //************************************************************************************************
305
306 int main(int argc, char *argv[])
307 {
308 int pid = -1;
309
310 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
311
312 #ifdef EPETRA_MPI
313 Epetra_MpiComm Comm(MPI_COMM_WORLD);
314 #else
315 Epetra_SerialComm Comm;
316 #endif
317
318 bool verbose = false;
319 bool success = true;
320 try {
321
322 pid = Comm.MyPID();
323
324 int n(10);
325 int numRHS=3;
326
327 Epetra_Map Map = Epetra_Map(n, 0, Comm);
328 Epetra_MultiVector X(Map, numRHS, false), Y(Map, numRHS, false);
329 for (int i=0; i<numRHS; ++i)
330 {
331 Epetra_MultiVector Xi(Epetra_DataAccess::View, X, i, 1);
332 Xi.PutScalar( 1.0 + i );
333 }
334
335 // Inner computes inv(D2)*y
336 Teuchos::RCP<Diagonal_Operator_2> D2 = Teuchos::rcp(new Diagonal_Operator_2(n, 1.0));
337 Iterative_Inverse_Operator A2(n, 1, D2, "Belos (inv(D2))", true);
338
339 // should return x=(1, 1/2, 1/3, ..., 1/10)
340 A2(X,Y);
341
342 if (pid==0) {
343 std::cout << "Vector Y should have all entries [1, 1/2, 1/3, ..., 1/10]" << std::endl;
344 }
345 Y.Print(std::cout);
346
347 // Inner computes inv(D)*x
348 Teuchos::RCP<Diagonal_Operator> D = Teuchos::rcp(new Diagonal_Operator(n, 4.0));
349 Teuchos::RCP<Iterative_Inverse_Operator> Inner =
350 Teuchos::rcp(new Iterative_Inverse_Operator(n, 1, D, "Belos (inv(D))", false));
351
352 // Composed_Operator computed inv(D)*B*x
353 Teuchos::RCP<Diagonal_Operator> B = Teuchos::rcp(new Diagonal_Operator(n, 4.0));
354 Teuchos::RCP<Composed_Operator> C = Teuchos::rcp(new Composed_Operator(n, Inner, B));
355
356 // Outer computes inv(C) = inv(inv(D)*B)*x = inv(B)*D*x = x
357 Teuchos::RCP<Iterative_Inverse_Operator> Outer =
358 Teuchos::rcp(new Iterative_Inverse_Operator(n, 1, C, "Belos (inv(C)=inv(inv(D)*B))", true));
359
360 // should return x=1/4
361 (*Inner)(X,Y);
362
363 if (pid==0) {
364 std::cout << std::endl << "Vector Y should have all entries [1/4, 1/4, 1/4, ..., 1/4]" << std::endl;
365 }
366 Y.Print(std::cout);
367
368 // should return x=1
369 (*Outer)(X,Y);
370
371 if (pid==0) {
372 std::cout << "Vector Y should have all entries [1, 1, 1, ..., 1]" << std::endl;
373 }
374 Y.Print(std::cout);
375
376 // Compute the norm of Y - 1.0
377 std::vector<double> norm_Y(Y.NumVectors());
378 X.PutScalar(1.0);
379 Y.Update(-1.0, X, 1.0);
380 Y.Norm2(&norm_Y[0]);
381
382 if (pid==0)
383 std::cout << "Two-norm of std::vector (Y-1.0) : "<< norm_Y[0] << std::endl;
384
385 success = true;
386 for (int i=0; i<numRHS; ++i)
387 success &= (norm_Y[0] < 1e-10 && !Teuchos::ScalarTraits<double>::isnaninf( norm_Y[0] ) );
388
389 if (success) {
390 if (pid==0)
391 std::cout << "End Result: TEST PASSED" << std::endl;
392 } else {
393 if (pid==0)
394 std::cout << "End Result: TEST FAILED" << std::endl;
395 }
396 }
397 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
398
399 return success ? EXIT_SUCCESS : EXIT_FAILURE;
400 }