| 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 C Array interface. |
| 12 |
|
| 13 |
*/ |
| 14 |
|
| 15 |
#include "ROL_CArrayVector.hpp" |
| 16 |
#include "ROL_StdVector.hpp" |
| 17 |
#include "ROL_Types.hpp" |
| 18 |
#include "Teuchos_oblackholestream.hpp" |
| 19 |
#include "Teuchos_GlobalMPISession.hpp" |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
|
| 23 |
typedef double RealT; |
| 24 |
typedef double ElementT; |
| 25 |
|
| 26 |
int main(int argc, char *argv[]) { |
| 27 |
|
| 28 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 29 |
|
| 30 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 31 |
int iprint = argc - 1; |
| 32 |
ROL::Ptr<std::ostream> outStream; |
| 33 |
Teuchos::oblackholestream bhs; // outputs nothing |
| 34 |
if (iprint > 0) |
| 35 |
outStream = ROL::makePtrFromRef(std::cout); |
| 36 |
else |
| 37 |
outStream = ROL::makePtrFromRef(bhs); |
| 38 |
|
| 39 |
int errorFlag = 0; |
| 40 |
|
| 41 |
RealT errtol = ROL::ROL_THRESHOLD<RealT>(); |
| 42 |
|
| 43 |
// *** Test body. |
| 44 |
|
| 45 |
try { |
| 46 |
|
| 47 |
int dim = 100; |
| 48 |
|
| 49 |
// Instantiate from raw pointer, Teuchos::ArrayRCP, and int (length) constructor |
| 50 |
ElementT* x_rawp = new ElementT[dim]; |
| 51 |
|
| 52 |
Teuchos::ArrayRCP<ElementT> y_arcp(dim,0); |
| 53 |
|
| 54 |
ROL::CArrayVector<RealT, ElementT> x(x_rawp,dim); |
| 55 |
ROL::CArrayVector<RealT, ElementT> y(y_arcp); |
| 56 |
ROL::CArrayVector<RealT, ElementT> z(dim); |
| 57 |
|
| 58 |
RealT left = -1e0, right = 1e0; |
| 59 |
|
| 60 |
// set x,y,z |
| 61 |
for (int i=0; i<dim; i++) { |
| 62 |
x_rawp[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 63 |
y_arcp[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 64 |
z.getVector()[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 65 |
} |
| 66 |
|
| 67 |
// Standard tests. |
| 68 |
std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream); |
| 69 |
ROL::StdVector<RealT, ElementT> checkvec( ROL::makePtrFromRef(consistency) ); |
| 70 |
if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON<RealT>())) { |
| 71 |
errorFlag++; |
| 72 |
} |
| 73 |
|
| 74 |
// Basis tests. |
| 75 |
// set x to first basis vector |
| 76 |
ROL::Ptr<ROL::Vector<RealT> > zp = x.clone(); |
| 77 |
zp = x.basis(0); |
| 78 |
RealT znorm = zp->norm(); |
| 79 |
*outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n"; |
| 80 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 81 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 82 |
errorFlag++; |
| 83 |
}; |
| 84 |
// set x to middle basis vector |
| 85 |
zp = x.basis(dim/2); |
| 86 |
znorm = zp->norm(); |
| 87 |
*outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n"; |
| 88 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 89 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 90 |
errorFlag++; |
| 91 |
}; |
| 92 |
// set x to last basis vector |
| 93 |
zp = x.basis(dim-1); |
| 94 |
znorm = zp->norm(); |
| 95 |
*outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n"; |
| 96 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 97 |
*outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 98 |
errorFlag++; |
| 99 |
}; |
| 100 |
|
| 101 |
// Repeat the checkVector tests with a zero vector. |
| 102 |
x.scale(0.0); |
| 103 |
consistency = x.checkVector(x, x, true, *outStream); |
| 104 |
if (checkvec.norm() > 0.0) { |
| 105 |
errorFlag++; |
| 106 |
} |
| 107 |
|
| 108 |
delete[] x_rawp; |
| 109 |
} |
| 110 |
catch (std::logic_error& err) { |
| 111 |
*outStream << err.what() << "\n"; |
| 112 |
errorFlag = -1000; |
| 113 |
}; // end try |
| 114 |
|
| 115 |
if (errorFlag != 0) |
| 116 |
std::cout << "End Result: TEST FAILED\n"; |
| 117 |
else |
| 118 |
std::cout << "End Result: TEST PASSED\n"; |
| 119 |
|
| 120 |
return 0; |
| 121 |
|
| 122 |
} |
| 123 |
|