| 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 uses the MVOPTester.hpp functions to test the Belos adapters |
| 11 |
// to Epetra. |
| 12 |
// |
| 13 |
|
| 14 |
#include "Epetra_Map.h" |
| 15 |
#include "Epetra_CrsMatrix.h" |
| 16 |
#ifdef HAVE_MPI |
| 17 |
#include "mpi.h" |
| 18 |
#include "Epetra_MpiComm.h" |
| 19 |
#endif |
| 20 |
#ifndef __cplusplus |
| 21 |
#define __cplusplus |
| 22 |
#endif |
| 23 |
#include "Epetra_Comm.h" |
| 24 |
#include "Epetra_SerialComm.h" |
| 25 |
|
| 26 |
#include "BelosConfigDefs.hpp" |
| 27 |
#include "BelosMVOPTester.hpp" |
| 28 |
#include "BelosEpetraAdapter.hpp" |
| 29 |
#include "BelosOutputManager.hpp" |
| 30 |
|
| 31 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 32 |
|
| 33 |
int main(int argc, char *argv[]) |
| 34 |
{ |
| 35 |
int i; |
| 36 |
bool ierr, gerr; |
| 37 |
gerr = true; |
| 38 |
|
| 39 |
#ifdef HAVE_MPI |
| 40 |
// Initialize MPI and setup an Epetra communicator |
| 41 |
MPI_Init(&argc,&argv); |
| 42 |
Teuchos::RCP<Epetra_MpiComm> Comm = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_WORLD) ); |
| 43 |
#else |
| 44 |
// If we aren't using MPI, then setup a serial communicator. |
| 45 |
Teuchos::RCP<Epetra_SerialComm> Comm = Teuchos::rcp( new Epetra_SerialComm() ); |
| 46 |
#endif |
| 47 |
|
| 48 |
bool verbose = false; |
| 49 |
if (argc>1) { |
| 50 |
if (argv[1][0]=='-' && argv[1][1]=='v') { |
| 51 |
verbose = true; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
bool success = true; |
| 56 |
try { |
| 57 |
// number of global elements |
| 58 |
int dim = 100; |
| 59 |
int blockSize = 5; |
| 60 |
|
| 61 |
// Construct a Map that puts approximately the same number of |
| 62 |
// equations on each processor. |
| 63 |
Teuchos::RCP<Epetra_Map> Map = Teuchos::rcp( new Epetra_Map(dim, 0, *Comm) ); |
| 64 |
|
| 65 |
// Get update list and number of local equations from newly created Map. |
| 66 |
int NumMyElements = Map->NumMyElements(); |
| 67 |
std::vector<int> MyGlobalElements(NumMyElements); |
| 68 |
Map->MyGlobalElements(&MyGlobalElements[0]); |
| 69 |
|
| 70 |
// Create an integer std::vector NumNz that is used to build the Petra Matrix. |
| 71 |
// NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation |
| 72 |
// on this processor |
| 73 |
std::vector<int> NumNz(NumMyElements); |
| 74 |
|
| 75 |
// We are building a tridiagonal matrix where each row has (-1 2 -1) |
| 76 |
// So we need 2 off-diagonal terms (except for the first and last equation) |
| 77 |
for (i=0; i<NumMyElements; i++) { |
| 78 |
if (MyGlobalElements[i]==0 || MyGlobalElements[i] == dim-1) { |
| 79 |
NumNz[i] = 2; |
| 80 |
} |
| 81 |
else { |
| 82 |
NumNz[i] = 3; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Create an Epetra_Matrix |
| 87 |
Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix(Epetra_DataAccess::Copy, *Map, &NumNz[0]) ); |
| 88 |
|
| 89 |
// Add rows one-at-a-time |
| 90 |
// Need some vectors to help |
| 91 |
// Off diagonal Values will always be -1 |
| 92 |
std::vector<double> Values(2); |
| 93 |
Values[0] = -1.0; Values[1] = -1.0; |
| 94 |
std::vector<int> Indices(2); |
| 95 |
double two = 2.0; |
| 96 |
int NumEntries; |
| 97 |
for (i=0; i<NumMyElements; i++) { |
| 98 |
if (MyGlobalElements[i]==0) { |
| 99 |
Indices[0] = 1; |
| 100 |
NumEntries = 1; |
| 101 |
} |
| 102 |
else if (MyGlobalElements[i] == dim-1) { |
| 103 |
Indices[0] = dim-2; |
| 104 |
NumEntries = 1; |
| 105 |
} |
| 106 |
else { |
| 107 |
Indices[0] = MyGlobalElements[i]-1; |
| 108 |
Indices[1] = MyGlobalElements[i]+1; |
| 109 |
NumEntries = 2; |
| 110 |
} |
| 111 |
ierr = A->InsertGlobalValues(MyGlobalElements[i],NumEntries,&Values[0],&Indices[0]); |
| 112 |
assert(ierr==0); |
| 113 |
// Put in the diagonal entry |
| 114 |
ierr = A->InsertGlobalValues(MyGlobalElements[i],1,&two,&MyGlobalElements[i]); |
| 115 |
assert(ierr==0); |
| 116 |
} |
| 117 |
|
| 118 |
// Finish building the epetra matrix A |
| 119 |
ierr = A->FillComplete(); |
| 120 |
assert(ierr==0); |
| 121 |
|
| 122 |
// Issue several useful typedefs; |
| 123 |
typedef Belos::MultiVec<double> EMV; |
| 124 |
//typedef Belos::Operator<double> EOP; // unused |
| 125 |
|
| 126 |
// Create an Epetra_MultiVector for an initial std::vector to start the solver. |
| 127 |
// Note that this needs to have the same number of columns as the blocksize. |
| 128 |
Teuchos::RCP<Belos::EpetraMultiVec> ivec = Teuchos::rcp( new Belos::EpetraMultiVec(*Map, blockSize) ); |
| 129 |
ivec->Random(); |
| 130 |
|
| 131 |
// Create an output manager to handle the I/O from the solver |
| 132 |
Teuchos::RCP<Belos::OutputManager<double> > MyOM = Teuchos::rcp( new Belos::OutputManager<double>() ); |
| 133 |
if (verbose) { |
| 134 |
MyOM->setVerbosity( Belos::Warnings ); |
| 135 |
} |
| 136 |
|
| 137 |
// test the Epetra adapter multivector |
| 138 |
ierr = Belos::TestMultiVecTraits<double,EMV>(MyOM,ivec); |
| 139 |
gerr &= ierr; |
| 140 |
if (ierr) { |
| 141 |
MyOM->print(Belos::Warnings,"*** EpetraAdapter PASSED TestMultiVecTraits()\n"); |
| 142 |
} |
| 143 |
else { |
| 144 |
MyOM->print(Belos::Warnings,"*** EpetraAdapter FAILED TestMultiVecTraits() ***\n\n"); |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef HAVE_MPI |
| 148 |
MPI_Finalize(); |
| 149 |
#endif |
| 150 |
|
| 151 |
if (!gerr) { |
| 152 |
success = false; |
| 153 |
MyOM->print(Belos::Warnings,"End Result: TEST FAILED\n"); |
| 154 |
} else { |
| 155 |
success = true; |
| 156 |
MyOM->print(Belos::Warnings,"End Result: TEST PASSED\n"); |
| 157 |
} |
| 158 |
} |
| 159 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success); |
| 160 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 161 |
} |