Debian Codegraph @

packages/belos/epetra/test/hypre/hypre_UnitTest.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack: Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "Ifpack.h"
11 #include "Ifpack_Hypre.h"
12
13 #include "Galeri_Maps.h"
14 #include "Galeri_CrsMatrices.h"
15 #include "Galeri_Utils.h"
16
17 #include "Epetra_MultiVector.h"
18 #include "Epetra_ConfigDefs.h"
19 #include "Epetra_Vector.h"
20 #include "Epetra_RowMatrix.h"
21 #include "Epetra_MultiVector.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Epetra_Map.h"
24 #ifdef HAVE_MPI
25 #include "mpi.h"
26 #include "Epetra_MpiComm.h"
27 #else
28 #include "Epetra_SerialComm.h"
29 #endif
30 #include "Epetra_InvOperator.h"
31
32 #include "BelosEpetraAdapter.hpp"
33 #include "BelosPseudoBlockGmresSolMgr.hpp"
34
35 #include "Teuchos_UnitTestHarness.hpp"
36 #include "Teuchos_RCP.hpp"
37 #include "Teuchos_Array.hpp"
38
39 #include "hypre_Helpers.hpp"
40
41 #include <string>
42 #include <stdio.h>
43 #include <map>
44
45
46 TEUCHOS_UNIT_TEST(Belos_Hypre, Laplace2D){
47 const double tol = 1E-7;
48 using Teuchos::RCP;
49 using Teuchos::rcp;
50 using Teuchos::ParameterList;
51 typedef Belos::LinearProblem<double,Epetra_MultiVector,Epetra_Operator> LinearProblem;
52
53 //
54 // Create Laplace2D
55 //
56 #ifdef HAVE_MPI
57 Epetra_MpiComm Comm(MPI_COMM_WORLD);
58 #else
59 Epetra_SerialComm Comm();
60 #endif
61 Teuchos::ParameterList GaleriList;
62 int nx = 10 * Comm.NumProc();
63 int ny = 10 * Comm.NumProc();
64 GaleriList.set("nx", nx);
65 GaleriList.set("ny", ny);
66 Epetra_Map Map(nx*ny,0,Comm);
67 RCP<Epetra_CrsMatrix> Crs_Matrix = rcp(Galeri::CreateCrsMatrix("Laplace2D", &Map, GaleriList));
68 int NumProc = Crs_Matrix->Comm().NumProc();
69
70 //
71 // Create the hypre preconditioner
72 //
73 RCP<Ifpack_Hypre> preconditioner = rcp(new Ifpack_Hypre(Crs_Matrix.get()));
74 TEST_EQUALITY(preconditioner->Initialize(),0);
75 TEST_EQUALITY(preconditioner->SetParameter(Preconditioner, ParaSails),0); // Use a Euclid Preconditioner (but not really used)
76 TEST_EQUALITY(preconditioner->SetParameter(Preconditioner),0); // Solve the problem
77 TEST_EQUALITY(preconditioner->Compute(),0);
78
79 //
80 // Create the solution vector and rhs
81 //
82 int numVec = 1;
83 RCP<Epetra_MultiVector> X = rcp(new Epetra_MultiVector(Crs_Matrix->OperatorDomainMap(), numVec));
84 RCP<Epetra_MultiVector> KnownX = rcp(new Epetra_MultiVector(Crs_Matrix->OperatorDomainMap(), numVec));
85 KnownX->Random();
86 RCP<Epetra_MultiVector> B = rcp(new Epetra_MultiVector(Crs_Matrix->OperatorRangeMap(), numVec));
87 Crs_Matrix->Apply(*KnownX, *B);
88
89 //
90 // Test the EpetraExt wrapper
91 // amk November 24, 2015: Should we deprecate this?
92 //
93 // RCP<ParameterList> pl = rcp(new ParameterList());
94 // TEST_EQUALITY(X->PutScalar(0.0),0);
95 // HYPRE_IJMatrix hypre_mat = preconditioner->HypreMatrix();
96 // RCP<EpetraExt_HypreIJMatrix> Hyp_Matrix = rcp(new EpetraExt_HypreIJMatrix(hypre_mat));
97 // TEST_EQUALITY(Hyp_Matrix->SetParameter(Preconditioner, ParaSails),0);
98 // TEST_EQUALITY(Hyp_Matrix->SetParameter(Preconditioner),0);
99 // TEST_EQUALITY(EquivalentMatrices(*Hyp_Matrix, *Crs_Matrix, tol), true);
100 // RCP<LinearProblem> problem1 = rcp(new LinearProblem(Crs_Matrix,X,B));
101 // problem1->setLeftPrec(Hyp_Matrix);
102 // TEST_EQUALITY(problem1->setProblem(),true);
103 // Belos::PseudoBlockGmresSolMgr<double,Epetra_MultiVector,Epetra_Operator> solMgr1(problem1,pl);
104 // Belos::ReturnType rv1 = solMgr1.solve(); // TEST_EQUALITY(solMgr2.solve(),Belos::Converged);
105 // TEST_EQUALITY(rv1,Belos::Converged);
106 // TEST_EQUALITY(EquivalentVectors(*X, *KnownX, tol*10*pow(10.0,NumProc)), true);
107
108 //
109 // Test the Ifpack hypre interface
110 //
111 RCP<ParameterList> pl2 = rcp(new ParameterList());
112 RCP<Epetra_Operator> invOp = rcp(new Epetra_InvOperator(preconditioner.get()));
113 TEST_EQUALITY(X->PutScalar(0.0),0);
114 RCP<LinearProblem> problem2 = rcp(new LinearProblem(Crs_Matrix,X,B));
115 problem2->setLeftPrec(invOp);
116 TEST_EQUALITY(problem2->setProblem(),true);
117 Belos::PseudoBlockGmresSolMgr<double,Epetra_MultiVector,Epetra_Operator> solMgr2(problem2,pl2);
118 Belos::ReturnType rv2 = solMgr2.solve(); // TEST_EQUALITY(solMgr2.solve(),Belos::Converged);
119 TEST_EQUALITY(rv2,Belos::Converged);
120 TEST_EQUALITY(EquivalentVectors(*X, *KnownX, tol*10*pow(10.0,NumProc)), true);
121 }