Debian Codegraph @

packages/belos/epetra/test/TFQMR/test_tfqmr_diag.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 TFQMR to solve.
11 //
12 // NOTE: No preconditioner is used in this case.
13 //
14
15 #include <BelosConfigDefs.hpp>
16 #include <BelosLinearProblem.hpp>
17 #include <BelosEpetraAdapter.hpp>
18 #include <BelosEpetraOperator.h>
19 #include <BelosTFQMRSolMgr.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, int min_gid_in, double v_in)
93 : Vector_Operator(n_in, n_in), min_gid(min_gid_in), v(v_in) {}
94
95 ~Diagonal_Operator_2() { };
96
97 void operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y)
98 {
99 int myCols = y.MyLength();
100 for (int j=0; j < x.NumVectors(); ++j) {
101 for (int i=0; i < myCols; ++i) (*y(j))[i] = (min_gid+i+1)*v*(*x(j))[i]; // NOTE: square operator!
102 }
103 };
104
105 private:
106
107 int min_gid;
108 double v;
109 };
110
111 //************************************************************************************************
112
113 class Composed_Operator : public Vector_Operator
114 {
115 public:
116
117 Composed_Operator(int n,
118 const Teuchos::RCP<Vector_Operator>& pA_in,
119 const Teuchos::RCP<Vector_Operator>& pB_in);
120
121 virtual ~Composed_Operator() {};
122
123 virtual void operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y);
124
125 private:
126
127 Teuchos::RCP<Vector_Operator> pA;
128 Teuchos::RCP<Vector_Operator> pB;
129 };
130
131 Composed_Operator::Composed_Operator(int n_in,
132 const Teuchos::RCP<Vector_Operator>& pA_in,
133 const Teuchos::RCP<Vector_Operator>& pB_in)
134 : Vector_Operator(n_in, n_in), pA(pA_in), pB(pB_in)
135 {
136 }
137
138 void Composed_Operator::operator () (const Epetra_MultiVector &x, Epetra_MultiVector &y)
139 {
140 Epetra_MultiVector ytemp(y.Map(), y.NumVectors(), false);
141 (*pB)( x, ytemp );
142 (*pA)( ytemp, y );
143 }
144
145 //************************************************************************************************
146
147 class Trilinos_Interface : public Epetra_Operator
148 {
149 public:
150
151 Trilinos_Interface(const Teuchos::RCP<Vector_Operator> pA_in,
152 const Teuchos::RCP<const Epetra_Comm> pComm_in,
153 const Teuchos::RCP<const Epetra_Map> pMap_in)
154 : pA (pA_in),
155 pComm (pComm_in),
156 pMap (pMap_in),
157 use_transpose (false)
158 {}
159
160 int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
161
162 int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
163 {
164 return(Apply(X,Y)); // No inverse
165 };
166
167 virtual ~Trilinos_Interface() {};
168
169 const char * Label() const {return("Trilinos_Interface, an Epetra_Operator implementation");};
170
171 bool UseTranspose() const {return(use_transpose);}; // always set to false
172
173 int SetUseTranspose(bool UseTranspose_in) { use_transpose = false; return(-1); };
174
175 bool HasNormInf() const {return(false);}; // cannot return inf-norm
176
177 double NormInf() const {return(0.0);};
178
179 virtual const Epetra_Comm & Comm() const {return *pComm; }
180
181 virtual const Epetra_Map & OperatorDomainMap() const {return *pMap; }
182
183 virtual const Epetra_Map & OperatorRangeMap() const {return *pMap; }
184
185 private:
186
187 Teuchos::RCP<Vector_Operator> pA;
188 Teuchos::RCP<const Epetra_Comm> pComm;
189 Teuchos::RCP<const Epetra_Map> pMap;
190
191 bool use_transpose;
192 };
193
194 int Trilinos_Interface::Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
195 {
196 (*pA)(X,Y);
197
198 return(0);
199 }
200
201 //************************************************************************************************
202
203 class Iterative_Inverse_Operator : public Vector_Operator
204 {
205
206 typedef Epetra_MultiVector MV;
207 typedef Epetra_Operator OP;
208
209 public:
210
211 Iterative_Inverse_Operator(int n_in, int blocksize,
212 const Teuchos::RCP<Vector_Operator>& pA_in,
213 std::string opString="Iterative Solver", bool print_in=false);
214
215 virtual ~Iterative_Inverse_Operator() {}
216
217 virtual void operator () (const Epetra_MultiVector &b, Epetra_MultiVector &x);
218
219 private:
220
221 Teuchos::RCP<Vector_Operator> pA; // operator which will be inverted
222 // supplies a matrix std::vector multiply
223 const bool print;
224
225 Teuchos::Time timer;
226 Teuchos::RCP<Epetra_Comm> pComm;
227 Teuchos::RCP<Epetra_Map> pMap;
228
229 Teuchos::RCP<OP> pPE;
230 Teuchos::RCP<Teuchos::ParameterList> pList;
231 Teuchos::RCP<LinearProblem<double,MV,OP> > pProb;
232 Teuchos::RCP<TFQMRSolMgr<double,MV,OP> > pBelos;
233 };
234
235 Iterative_Inverse_Operator::Iterative_Inverse_Operator(int n_in, int blocksize,
236 const Teuchos::RCP<Vector_Operator>& pA_in,
237 std::string opString, bool print_in)
238 : Vector_Operator(n_in, n_in), // square operator
239 pA(pA_in),
240 print(print_in),
241 timer(opString)
242 {
243
244 int n_global;
245
246 #ifdef EPETRA_MPI
247 MPI_Allreduce(&n, &n_global, 1, MPI_UNSIGNED, MPI_SUM, MPI_COMM_WORLD);
248 pComm = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_WORLD) );
249 #else
250 pComm = Teuchos::rcp( new Epetra_SerialComm() );
251 n_global = n;
252 #endif
253 pMap = Teuchos::rcp( new Epetra_Map(n_global, n, 0, *pComm) );
254
255 pPE = Teuchos::rcp( new Trilinos_Interface(pA, pComm, pMap ) );
256
257 pProb = Teuchos::rcp( new LinearProblem<double,MV,OP>() );
258 pProb->setOperator( pPE );
259
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( "Maximum Iterations", max_iter );
268 pList->set( "Convergence Tolerance", tol );
269 pList->set( "Verbosity", verbosity );
270
271 pBelos = Teuchos::rcp( new TFQMRSolMgr<double,MV,OP>(pProb, pList) );
272 }
273
274 void Iterative_Inverse_Operator::operator () (const Epetra_MultiVector &b, Epetra_MultiVector &x)
275 {
276 int pid = pComm->MyPID();
277
278 // Initialize the solution to zero
279 x.PutScalar( 0.0 );
280
281 // Reset the solver, problem, and status test for next solve (HKT)
282 pProb->setProblem( Teuchos::rcp(&x, false), Teuchos::rcp(&b, false) );
283
284 timer.start();
285 Belos::ReturnType ret = pBelos->solve();
286 timer.stop();
287
288 if (pid == 0 && print) {
289 if (ret == Belos::Converged)
290 {
291 std::cout << std::endl << "pid[" << pid << "] TFQMR converged" << std::endl;
292 std::cout << "Solution time: " << timer.totalElapsedTime() << std::endl;
293
294 }
295 else
296 std::cout << std::endl << "pid[" << pid << "] TFQMR did not converge" << std::endl;
297 }
298 }
299
300 //************************************************************************************************
301 //************************************************************************************************
302
303 int main(int argc, char *argv[])
304 {
305 int pid = -1;
306
307 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
308
309 #ifdef EPETRA_MPI
310 Epetra_MpiComm Comm(MPI_COMM_WORLD);
311 #else
312 Epetra_SerialComm Comm;
313 #endif
314
315 bool verbose = false;
316 bool success = true;
317 try {
318
319 pid = Comm.MyPID();
320
321 int n(10);
322 int numRHS=1;
323
324 Epetra_Map Map = Epetra_Map(n, 0, Comm);
325 Epetra_MultiVector X(Map, numRHS, false), Y(Map, numRHS, false);
326 X.PutScalar( 1.0 );
327
328 // Inner computes inv(D2)*y
329 Teuchos::RCP<Diagonal_Operator_2> D2 = Teuchos::rcp(new Diagonal_Operator_2(n, Map.MinMyGID(), 1.0));
330 Iterative_Inverse_Operator A2(n, 1, D2, "Belos (inv(D2))", true);
331
332 // should return x=(1, 1/2, 1/3, ..., 1/10)
333 A2(X,Y);
334
335 if (pid==0) {
336 std::cout << "Vector Y should have all entries [1, 1/2, 1/3, ..., 1/10]" << std::endl;
337 }
338 Y.Print(std::cout);
339
340 // Inner computes inv(D)*x
341 Teuchos::RCP<Diagonal_Operator> D = Teuchos::rcp(new Diagonal_Operator(n, 4.0));
342 Teuchos::RCP<Iterative_Inverse_Operator> Inner =
343 Teuchos::rcp(new Iterative_Inverse_Operator(n, 1, D, "Belos (inv(D))", false));
344
345 // Composed_Operator computed inv(D)*B*x
346 Teuchos::RCP<Diagonal_Operator> B = Teuchos::rcp(new Diagonal_Operator(n, 4.0));
347 Teuchos::RCP<Composed_Operator> C = Teuchos::rcp(new Composed_Operator(n, Inner, B));
348
349 // Outer computes inv(C) = inv(inv(D)*B)*x = inv(B)*D*x = x
350 Teuchos::RCP<Iterative_Inverse_Operator> Outer =
351 Teuchos::rcp(new Iterative_Inverse_Operator(n, 1, C, "Belos (inv(C)=inv(inv(D)*B))", true));
352
353 // should return x=1/4
354 (*Inner)(X,Y);
355
356 if (pid==0) {
357 std::cout << std::endl << "Vector Y should have all entries [1/4, 1/4, 1/4, ..., 1/4]" << std::endl;
358 }
359 Y.Print(std::cout);
360
361 // should return x=1
362 (*Outer)(X,Y);
363
364 if (pid==0) {
365 std::cout << "Vector Y should have all entries [1, 1, 1, ..., 1]" << std::endl;
366 }
367 Y.Print(std::cout);
368
369 // Compute the norm of Y - 1.0
370 std::vector<double> norm_Y(Y.NumVectors());
371 Y.Update(-1.0, X, 1.0);
372 Y.Norm2(&norm_Y[0]);
373
374 if (pid==0)
375 std::cout << "Two-norm of std::vector (Y-1.0) : "<< norm_Y[0] << std::endl;
376
377 success = (norm_Y[0] < 1e-10 && !Teuchos::ScalarTraits<double>::isnaninf( norm_Y[0] ) );
378
379 if (success) {
380 if (pid==0)
381 std::cout << "End Result: TEST PASSED" << std::endl;
382 } else {
383 if (pid==0)
384 std::cout << "End Result: TEST FAILED" << std::endl;
385 }
386 }
387 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
388
389 return success ? EXIT_SUCCESS : EXIT_FAILURE;
390 }