| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "Ifpack_ConfigDefs.h" |
| 11 |
|
| 12 |
#include "Epetra_ConfigDefs.h" |
| 13 |
#ifdef HAVE_MPI |
| 14 |
#include "mpi.h" |
| 15 |
#include "Epetra_MpiComm.h" |
| 16 |
#else |
| 17 |
#include "Epetra_SerialComm.h" |
| 18 |
#endif |
| 19 |
#include "Epetra_Comm.h" |
| 20 |
#include "Epetra_Map.h" |
| 21 |
#include "Epetra_Time.h" |
| 22 |
#include "Epetra_BlockMap.h" |
| 23 |
#include "Epetra_MultiVector.h" |
| 24 |
#include "Epetra_Vector.h" |
| 25 |
#include "Epetra_Export.h" |
| 26 |
#include "AztecOO.h" |
| 27 |
#include "Galeri_Maps.h" |
| 28 |
#include "Galeri_CrsMatrices.h" |
| 29 |
#include "Ifpack_CrsIct.h" |
| 30 |
#include "Ifpack.h" |
| 31 |
#include "Teuchos_RefCountPtr.hpp" |
| 32 |
#ifdef JJH_PRINTMATRIX |
| 33 |
#include "/data/home/jhu/Trilinos/development-branch/Trilinos/packages/epetraext/src/inout/EpetraExt_RowMatrixOut.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef HAVE_IFPACK_ML |
| 37 |
#include "ml_MultiLevelPreconditioner.h" |
| 38 |
#endif |
| 39 |
|
| 40 |
int main(int argc, char *argv[]) { |
| 41 |
|
| 42 |
#ifdef HAVE_MPI |
| 43 |
MPI_Init(&argc,&argv); |
| 44 |
Epetra_MpiComm Comm (MPI_COMM_WORLD); |
| 45 |
#else |
| 46 |
Epetra_SerialComm Comm; |
| 47 |
#endif |
| 48 |
int nx, ny, mx, my, overlap; |
| 49 |
if (argc > 1) overlap = (int) strtol(argv[1],NULL,10); |
| 50 |
else overlap = 0; |
| 51 |
if (argc > 2) nx = (int) strtol(argv[2],NULL,10); |
| 52 |
else nx = 30; |
| 53 |
if (argc > 3) ny = (int) strtol(argv[3],NULL,10); |
| 54 |
else ny = nx * Comm.NumProc(); |
| 55 |
if (argc > 4) mx = (int) strtol(argv[4],NULL,10); |
| 56 |
else mx = 1; |
| 57 |
if (argc > 5) my = (int) strtol(argv[5],NULL,10); |
| 58 |
else my = Comm.NumProc(); |
| 59 |
|
| 60 |
printf("\n--------\noverlap=%d\nnx=%d,ny=%d\nmx=%d,my=%d\n--------\n", |
| 61 |
overlap,nx,ny,mx,my); |
| 62 |
|
| 63 |
int MyPID = Comm.MyPID(); |
| 64 |
bool verbose = false; |
| 65 |
if (MyPID==0) verbose = true; |
| 66 |
|
| 67 |
// The problem is defined on a 2D grid, global size is nx * nx. |
| 68 |
Teuchos::ParameterList GaleriList; |
| 69 |
GaleriList.set("nx", nx); |
| 70 |
GaleriList.set("ny", ny); |
| 71 |
GaleriList.set("mx", mx); |
| 72 |
GaleriList.set("my", my); |
| 73 |
/* |
| 74 |
GaleriList.set("nx", nx); |
| 75 |
GaleriList.set("ny", nx * Comm.NumProc()); |
| 76 |
GaleriList.set("mx", 1); |
| 77 |
GaleriList.set("my", Comm.NumProc()); |
| 78 |
*/ |
| 79 |
Teuchos::RefCountPtr<Epetra_Map> Map = Teuchos::rcp( Galeri::CreateMap("Cartesian2D", Comm, GaleriList) ); |
| 80 |
Teuchos::RefCountPtr<Epetra_CrsMatrix> A = Teuchos::rcp( Galeri::CreateCrsMatrix("Laplace2D", &*Map, GaleriList) ); |
| 81 |
Teuchos::RefCountPtr<Epetra_MultiVector> LHS = Teuchos::rcp( new Epetra_MultiVector(*Map, 1) ); |
| 82 |
Teuchos::RefCountPtr<Epetra_MultiVector> RHS = Teuchos::rcp( new Epetra_MultiVector(*Map, 1) ); |
| 83 |
//LHS->PutScalar(0.0); RHS->Random(); |
| 84 |
LHS->PutScalar(0.0); RHS->PutScalar(1.0); |
| 85 |
|
| 86 |
if (!MyPID) cout << "#global rows in A = " << A->NumGlobalRows() << endl; |
| 87 |
|
| 88 |
#ifdef JJH_PRINT_LOCAL_MATRIX |
| 89 |
A->Print(cout); |
| 90 |
#endif |
| 91 |
|
| 92 |
#ifdef JJH_PRINTMATRIX |
| 93 |
char filename[80]; |
| 94 |
sprintf(filename,"Acheck_%dproc",Comm.NumProc()); |
| 95 |
EpetraExt::RowMatrixToMatlabFile(filename,*A); |
| 96 |
#endif |
| 97 |
|
| 98 |
// ============================ // |
| 99 |
// Construct ML preconditioner // |
| 100 |
// ---------------------------- // |
| 101 |
|
| 102 |
int Niters = 500; |
| 103 |
AztecOO solver; |
| 104 |
Ifpack Factory; |
| 105 |
Teuchos::RefCountPtr<Ifpack_Preconditioner> Prec = |
| 106 |
Teuchos::rcp( Factory.Create("ML", &*A, overlap) ); |
| 107 |
|
| 108 |
Teuchos::ParameterList List; |
| 109 |
ML_Epetra::SetDefaults("SA",List); |
| 110 |
List.set("ML output",0); |
| 111 |
List.set("max levels",1); |
| 112 |
List.set("XML input file","ml_parameters.xml"); |
| 113 |
List.set("read XML",true); |
| 114 |
IFPACK_CHK_ERR(Prec->SetParameters(List)); |
| 115 |
IFPACK_CHK_ERR(Prec->Initialize()); |
| 116 |
IFPACK_CHK_ERR(Prec->Compute()); |
| 117 |
|
| 118 |
// Here we create an AztecOO object |
| 119 |
LHS->PutScalar(0.0); |
| 120 |
|
| 121 |
solver.SetUserMatrix(&*A); |
| 122 |
solver.SetLHS(&*LHS); |
| 123 |
solver.SetRHS(&*RHS); |
| 124 |
solver.SetAztecOption(AZ_solver,AZ_cg); |
| 125 |
solver.SetAztecOption(AZ_conv,AZ_noscaled); |
| 126 |
solver.SetPrecOperator(&*Prec); |
| 127 |
solver.SetAztecOption(AZ_output, 1); |
| 128 |
solver.Iterate(Niters, 1.0e-8); |
| 129 |
|
| 130 |
#ifdef HAVE_MPI |
| 131 |
MPI_Finalize(); |
| 132 |
#endif |
| 133 |
|
| 134 |
return(EXIT_SUCCESS); |
| 135 |
} |