| 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 SimulatedVector functionality with TpetraTeuchosBatchManager |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "ROL_SimulatedVector.hpp" |
| 15 |
#include "ROL_TpetraTeuchosBatchManager.hpp" |
| 16 |
#include "ROL_MonteCarloGenerator.hpp" |
| 17 |
#include "ROL_StdVector.hpp" |
| 18 |
#include "ROL_Types.hpp" |
| 19 |
#include "ROL_Stream.hpp" |
| 20 |
#include "Teuchos_GlobalMPISession.hpp" |
| 21 |
#include "Teuchos_Comm.hpp" |
| 22 |
#include "Tpetra_Core.hpp" |
| 23 |
#include "Tpetra_Version.hpp" |
| 24 |
|
| 25 |
typedef double RealT; |
| 26 |
|
| 27 |
template<class Real> |
| 28 |
void print_vector( const ROL::Vector<Real> &x ) { |
| 29 |
|
| 30 |
typedef ROL::Vector<Real> V; |
| 31 |
typedef ROL::StdVector<Real> SV; |
| 32 |
typedef ROL::SimulatedVector<Real> PV; |
| 33 |
typedef typename PV::size_type size_type; |
| 34 |
|
| 35 |
const PV eb = dynamic_cast<const PV&>(x); |
| 36 |
size_type n = eb.numVectors(); |
| 37 |
|
| 38 |
for(size_type k=0; k<n; ++k) { |
| 39 |
std::cout << "[subvector " << k << "]" << std::endl; |
| 40 |
ROL::Ptr<const V> vec = eb.get(k); |
| 41 |
ROL::Ptr<const std::vector<Real> > vp = |
| 42 |
dynamic_cast<const SV&>(*vec).getVector(); |
| 43 |
for(size_type i=0;i<vp->size();++i) { |
| 44 |
std::cout << (*vp)[i] << std::endl; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
int main(int argc, char *argv[]) { |
| 51 |
|
| 52 |
using namespace Teuchos; |
| 53 |
|
| 54 |
typedef ROL::Vector<RealT> V; |
| 55 |
typedef ROL::StdVector<RealT> SV; |
| 56 |
typedef ROL::SimulatedVector<RealT> PV; |
| 57 |
|
| 58 |
GlobalMPISession mpiSession(&argc, &argv); |
| 59 |
ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 60 |
ROL::Ptr<ROL::TpetraTeuchosBatchManager<RealT> > bman = ROL::makePtr<ROL::TpetraTeuchosBatchManager<RealT>>(comm); |
| 61 |
|
| 62 |
int iprint = argc - 1; |
| 63 |
|
| 64 |
ROL::Ptr<std::ostream> outStream; |
| 65 |
ROL::nullstream bhs; // no output |
| 66 |
|
| 67 |
if( iprint>0 ) |
| 68 |
outStream = ROL::makePtrFromRef(std::cout); |
| 69 |
else |
| 70 |
outStream = ROL::makePtrFromRef(bhs); |
| 71 |
|
| 72 |
int errorFlag = 0; |
| 73 |
|
| 74 |
RealT errtol = ROL::ROL_THRESHOLD<RealT>(); |
| 75 |
|
| 76 |
try { |
| 77 |
|
| 78 |
int stoch_dim = 4; |
| 79 |
int nSamp = 100; |
| 80 |
std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0; |
| 81 |
std::vector<std::vector<RealT> > bounds(stoch_dim,tmp); |
| 82 |
ROL::Ptr<ROL::SampleGenerator<RealT> > sampler |
| 83 |
= ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman); |
| 84 |
|
| 85 |
int batchID = bman->batchID(); |
| 86 |
int nvecloc = sampler->numMySamples(); |
| 87 |
|
| 88 |
*outStream << "Proc " << batchID << ", number of vectors = " << nvecloc << std::endl; |
| 89 |
|
| 90 |
int dim = 5; |
| 91 |
|
| 92 |
int total_dim = 0; |
| 93 |
|
| 94 |
RealT left = -1e0, right = 1e0; |
| 95 |
|
| 96 |
std::vector<ROL::Ptr<V> > x_ptr; |
| 97 |
std::vector<ROL::Ptr<V> > y_ptr; |
| 98 |
std::vector<ROL::Ptr<V> > z_ptr; |
| 99 |
|
| 100 |
for( int k=0; k<nvecloc; ++k ) { |
| 101 |
|
| 102 |
ROL::Ptr<std::vector<RealT> > xk_ptr = ROL::makePtr<std::vector<RealT>>(dim); |
| 103 |
ROL::Ptr<std::vector<RealT> > yk_ptr = ROL::makePtr<std::vector<RealT>>(dim); |
| 104 |
ROL::Ptr<std::vector<RealT> > zk_ptr = ROL::makePtr<std::vector<RealT>>(dim); |
| 105 |
|
| 106 |
ROL::Ptr<V> xk = ROL::makePtr<SV>( xk_ptr ); |
| 107 |
ROL::Ptr<V> yk = ROL::makePtr<SV>( yk_ptr ); |
| 108 |
ROL::Ptr<V> zk = ROL::makePtr<SV>( zk_ptr ); |
| 109 |
|
| 110 |
for( int i=0; i<dim; ++i ) { |
| 111 |
(*xk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 112 |
(*yk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 113 |
(*zk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 114 |
} |
| 115 |
|
| 116 |
x_ptr.push_back(xk); |
| 117 |
y_ptr.push_back(yk); |
| 118 |
z_ptr.push_back(zk); |
| 119 |
|
| 120 |
total_dim += dim; |
| 121 |
} |
| 122 |
|
| 123 |
*outStream << "Proc " << batchID << ", total dimension = " << total_dim << std::endl; |
| 124 |
|
| 125 |
PV x(x_ptr, bman); |
| 126 |
PV y(y_ptr, bman); |
| 127 |
PV z(z_ptr, bman); |
| 128 |
|
| 129 |
// Standard tests. |
| 130 |
std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream); |
| 131 |
ROL::StdVector<RealT> checkvec(ROL::makePtrFromRef(consistency)); |
| 132 |
if (checkvec.norm() > std::sqrt(errtol)) { |
| 133 |
errorFlag++; |
| 134 |
} |
| 135 |
|
| 136 |
// Repeat the checkVector tests with a zero vector. |
| 137 |
x.scale(0.0); |
| 138 |
consistency = x.checkVector(x, x, true, *outStream); |
| 139 |
if (checkvec.norm() > 0.0) { |
| 140 |
errorFlag++; |
| 141 |
} |
| 142 |
|
| 143 |
// Reset x and y vectors to all 1 and 2, resp., and compute dot product. |
| 144 |
/* |
| 145 |
x_ptr.resize(0); |
| 146 |
y_ptr.resize(0); |
| 147 |
for( int k=0; k<nvecloc; ++k ) { |
| 148 |
ROL::Ptr<std::vector<RealT> > xk_ptr = ROL::makePtr<std::vector<RealT>>(dim); |
| 149 |
ROL::Ptr<std::vector<RealT> > yk_ptr = ROL::makePtr<std::vector<RealT>>(dim); |
| 150 |
ROL::Ptr<V> xk = ROL::makePtr<SV>( xk_ptr ); |
| 151 |
ROL::Ptr<V> yk = ROL::makePtr<SV>( yk_ptr ); |
| 152 |
for( int i=0; i<dim; ++i ) { |
| 153 |
(*xk_ptr)[i] = 1.0; |
| 154 |
(*yk_ptr)[i] = 2.0; |
| 155 |
} |
| 156 |
x_ptr.push_back(xk); |
| 157 |
y_ptr.push_back(yk); |
| 158 |
} |
| 159 |
*outStream << "x.dot(y) = " << x.dot(y) << std::endl; |
| 160 |
if (std::abs(x.dot(y) - nSamp*dim*2) > std::sqrt(errtol)) { |
| 161 |
errorFlag++; |
| 162 |
} |
| 163 |
*/ |
| 164 |
|
| 165 |
} |
| 166 |
catch (std::logic_error& err) { |
| 167 |
*outStream << err.what() << "\n"; |
| 168 |
errorFlag = -1000; |
| 169 |
}; // end try |
| 170 |
|
| 171 |
if (errorFlag != 0) |
| 172 |
std::cout << "End Result: TEST FAILED\n"; |
| 173 |
else |
| 174 |
std::cout << "End Result: TEST PASSED\n"; |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |