| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/*! \file test_02.cpp |
| 11 |
\brief Test Epetra_MultiVector interface. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "ROL_EpetraMultiVector.hpp" |
| 15 |
#include "ROL_Types.hpp" |
| 16 |
#include "Epetra_Map.h" |
| 17 |
#include "ROL_Stream.hpp" |
| 18 |
#include "Teuchos_GlobalMPISession.hpp" |
| 19 |
#ifdef HAVE_MPI |
| 20 |
#include "Epetra_MpiComm.h" |
| 21 |
#else |
| 22 |
#include "Epetra_SerialComm.h" |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
|
| 27 |
typedef double RealT; |
| 28 |
typedef double ElementT; |
| 29 |
|
| 30 |
int main(int argc, char *argv[]) { |
| 31 |
|
| 32 |
#ifdef HAVE_MPI |
| 33 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); |
| 34 |
Epetra_MpiComm Comm(MPI_COMM_WORLD); |
| 35 |
#else |
| 36 |
Epetra_SerialComm Comm; |
| 37 |
#endif |
| 38 |
|
| 39 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 40 |
int iprint = argc - 1; |
| 41 |
ROL::Ptr<std::ostream> outStream; |
| 42 |
ROL::nullstream bhs; // outputs nothing |
| 43 |
if (iprint > 0) |
| 44 |
outStream = ROL::makePtrFromRef(std::cout); |
| 45 |
else |
| 46 |
outStream = ROL::makePtrFromRef(bhs); |
| 47 |
|
| 48 |
int errorFlag = 0; |
| 49 |
|
| 50 |
RealT errtol = ROL::ROL_THRESHOLD<RealT>(); |
| 51 |
|
| 52 |
// *** Test body. |
| 53 |
|
| 54 |
try { |
| 55 |
|
| 56 |
int dim = 100; |
| 57 |
Epetra_Map Map(dim, 0, Comm); |
| 58 |
|
| 59 |
ROL::Ptr<Epetra_MultiVector> x_ptr = ROL::makePtr<Epetra_MultiVector>(Map, 1); |
| 60 |
ROL::Ptr<Epetra_MultiVector> y_ptr = ROL::makePtr<Epetra_MultiVector>(Map, 1); |
| 61 |
ROL::EpetraMultiVector<RealT> x(x_ptr); |
| 62 |
ROL::EpetraMultiVector<RealT> y(y_ptr); |
| 63 |
|
| 64 |
// set x,y |
| 65 |
for (int i=0; i<dim; i++) { |
| 66 |
x_ptr->Random(); |
| 67 |
y_ptr->PutScalar(2.0); |
| 68 |
} |
| 69 |
|
| 70 |
// norm of x |
| 71 |
RealT xnorm = x.norm(); |
| 72 |
*outStream << "\nNorm of ROL::EpetraMultiVector x: " << xnorm << "\n"; |
| 73 |
|
| 74 |
// norm of y |
| 75 |
RealT ynorm = y.norm(); |
| 76 |
*outStream << "\nNorm of ROL::EpetraMultiVector y: " << ynorm << "\n"; |
| 77 |
|
| 78 |
// scale x |
| 79 |
x.scale(0.5); |
| 80 |
RealT xnorm2 = x.norm(); |
| 81 |
*outStream << "\nNorm of half of x: " << xnorm2 << "\n"; |
| 82 |
if ( std::abs(xnorm/xnorm2 - 2.0) > errtol ) { |
| 83 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 84 |
errorFlag++; |
| 85 |
}; |
| 86 |
|
| 87 |
// clone z from x, deep copy x into z, norm of z |
| 88 |
ROL::Ptr<ROL::Vector<RealT> > z = x.clone(); |
| 89 |
z->set(x); |
| 90 |
RealT znorm = z->norm(); |
| 91 |
*outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n"; |
| 92 |
if ( std::abs(xnorm2 - znorm) > errtol ) { |
| 93 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 94 |
errorFlag++; |
| 95 |
}; |
| 96 |
|
| 97 |
// compute norm of x - x - 0 |
| 98 |
z->set(x); |
| 99 |
x.scale(-1.0); |
| 100 |
z->plus(x); |
| 101 |
y.zero(); |
| 102 |
z->axpy(-1.0, y); |
| 103 |
znorm = z->norm(); |
| 104 |
*outStream << "\nNorm of (x - x) - 0: " << znorm << "\n"; |
| 105 |
if ( std::abs(znorm) > errtol ) { |
| 106 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 107 |
errorFlag++; |
| 108 |
}; |
| 109 |
|
| 110 |
// set x to first basis vector |
| 111 |
z = x.basis(0); |
| 112 |
znorm = z->norm(); |
| 113 |
*outStream << "\nNorm of ROL::Vector z (first basis vector): " << znorm << "\n"; |
| 114 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 115 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 116 |
errorFlag++; |
| 117 |
}; |
| 118 |
// set x to middle basis vector |
| 119 |
z = x.basis(dim/2); |
| 120 |
znorm = z->norm(); |
| 121 |
*outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n"; |
| 122 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 123 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 124 |
errorFlag++; |
| 125 |
}; |
| 126 |
// set x to last basis vector |
| 127 |
z = x.basis(dim-1); |
| 128 |
znorm = z->norm(); |
| 129 |
*outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n"; |
| 130 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 131 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 132 |
errorFlag++; |
| 133 |
}; |
| 134 |
|
| 135 |
} |
| 136 |
catch (std::logic_error& err) { |
| 137 |
*outStream << err.what() << "\n"; |
| 138 |
errorFlag = -1000; |
| 139 |
}; // end try |
| 140 |
|
| 141 |
if (errorFlag != 0) |
| 142 |
std::cout << "End Result: TEST FAILED\n"; |
| 143 |
else |
| 144 |
std::cout << "End Result: TEST PASSED\n"; |
| 145 |
|
| 146 |
return 0; |
| 147 |
|
| 148 |
} |
| 149 |
|