| 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 |
#include "ROL_ParameterList.hpp" |
| 11 |
#include "ROL_Stream.hpp" |
| 12 |
#include "Teuchos_GlobalMPISession.hpp" |
| 13 |
#include "Teuchos_Comm.hpp" |
| 14 |
#include "Teuchos_DefaultComm.hpp" |
| 15 |
#include "Teuchos_CommHelpers.hpp" |
| 16 |
|
| 17 |
#include "ROL_TeuchosBatchManager.hpp" |
| 18 |
#include "ROL_SROMGenerator.hpp" |
| 19 |
#include "ROL_DistributionFactory.hpp" |
| 20 |
|
| 21 |
#include <ctime> |
| 22 |
|
| 23 |
typedef double RealT; |
| 24 |
|
| 25 |
int main(int argc, char* argv[]) { |
| 26 |
|
| 27 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 28 |
ROL::Ptr<const Teuchos::Comm<int> > commptr = |
| 29 |
ROL::toPtr(Teuchos::DefaultComm<int>::getComm()); |
| 30 |
|
| 31 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 32 |
int iprint = argc - 1; |
| 33 |
ROL::Ptr<std::ostream> outStream; |
| 34 |
ROL::nullstream bhs; // outputs nothing |
| 35 |
if (iprint > 0 && commptr->getRank() == 0) |
| 36 |
outStream = ROL::makePtrFromRef(std::cout); |
| 37 |
else |
| 38 |
outStream = ROL::makePtrFromRef(bhs); |
| 39 |
|
| 40 |
int errorFlag = 0; |
| 41 |
|
| 42 |
try { |
| 43 |
/**********************************************************************************************/ |
| 44 |
/************************* CONSTRUCT SOL COMPONENTS *******************************************/ |
| 45 |
/**********************************************************************************************/ |
| 46 |
// Set random seed |
| 47 |
srand(123456789); |
| 48 |
// Build samplers |
| 49 |
size_t dimension = 1; |
| 50 |
|
| 51 |
// Initialize distribution |
| 52 |
ROL::Ptr<ROL::Distribution<RealT> > dist; |
| 53 |
std::vector<ROL::Ptr<ROL::Distribution<RealT> > > distVec(dimension); |
| 54 |
ROL::ParameterList Dlist; |
| 55 |
Dlist.sublist("SOL").sublist("Distribution").set("Name","Beta"); |
| 56 |
RealT alpha = 1., beta = 4.; |
| 57 |
// Fill moment vector and initial guess |
| 58 |
for (size_t d = 0; d < dimension; d++) { |
| 59 |
// Build distribution for dimension d |
| 60 |
alpha++; beta++; |
| 61 |
Dlist.sublist("SOL").sublist("Distribution").sublist("Beta").set("Shape 1",alpha); |
| 62 |
Dlist.sublist("SOL").sublist("Distribution").sublist("Beta").set("Shape 2",beta); |
| 63 |
dist = ROL::DistributionFactory<RealT>(Dlist); |
| 64 |
distVec[d] = ROL::DistributionFactory<RealT>(Dlist); |
| 65 |
} |
| 66 |
|
| 67 |
// Get ROL parameterlist |
| 68 |
std::string filename = "input_04.xml"; |
| 69 |
|
| 70 |
auto parlist = ROL::getParametersFromXmlFile( filename ); |
| 71 |
|
| 72 |
ROL::ParameterList &list = parlist->sublist("SOL").sublist("Sample Generator").sublist("SROM"); |
| 73 |
std::vector<int> moments = ROL::getArrayFromStringParameter<int>(list,"Moments"); |
| 74 |
size_t numMoments = static_cast<size_t>(moments.size()); |
| 75 |
|
| 76 |
std::clock_t timer = std::clock(); |
| 77 |
ROL::Ptr<ROL::BatchManager<RealT> > bman = |
| 78 |
ROL::makePtr<ROL::TeuchosBatchManager<RealT,int>>(commptr); |
| 79 |
ROL::Ptr<ROL::SampleGenerator<RealT> > sampler = |
| 80 |
ROL::makePtr<ROL::SROMGenerator<RealT>>(*parlist,bman,distVec,*outStream); |
| 81 |
*outStream << std::endl << "Sample Time: " |
| 82 |
<< (std::clock()-timer)/(RealT)CLOCKS_PER_SEC << " seconds" |
| 83 |
<< std::endl; |
| 84 |
|
| 85 |
RealT val = 0., error = 0., data = 0., sum = 0.; |
| 86 |
*outStream << std::endl; |
| 87 |
*outStream << std::scientific << std::setprecision(11); |
| 88 |
*outStream << std::right << std::setw(20) << "Computed Moment" |
| 89 |
<< std::setw(20) << "True Moment" |
| 90 |
<< std::setw(20) << "Relative Error" |
| 91 |
<< std::endl; |
| 92 |
for (size_t m = 0; m < numMoments; m++) { |
| 93 |
for (size_t d = 0; d < dimension; d++) { |
| 94 |
val = 0.; data = distVec[d]->moment(moments[m]); |
| 95 |
for (size_t k = 0; k < (size_t)sampler->numMySamples(); k++) { |
| 96 |
val += sampler->getMyWeight(k)*std::pow((sampler->getMyPoint(k))[d],moments[m]); |
| 97 |
} |
| 98 |
bman->sumAll(&val,&sum,1); |
| 99 |
error = std::abs(sum-data)/std::abs(data); |
| 100 |
if ( error > 1.e-1 ) { |
| 101 |
errorFlag++; |
| 102 |
} |
| 103 |
*outStream << std::right << std::setw(20) << sum |
| 104 |
<< std::setw(20) << data |
| 105 |
<< std::setw(20) << error |
| 106 |
<< std::endl; |
| 107 |
} |
| 108 |
} |
| 109 |
*outStream << std::endl; |
| 110 |
|
| 111 |
// std::ofstream file; |
| 112 |
// std::stringstream name; |
| 113 |
// name << "samples." << commptr->getRank() << ".txt"; |
| 114 |
// file.open(name.str().c_str()); |
| 115 |
// for (size_t k = 0; k < (size_t)sampler->numMySamples(); k++) { |
| 116 |
// for (size_t d = 0; d < dimension; d++) { |
| 117 |
// file << std::setprecision(std::numeric_limits<RealT>::digits10) |
| 118 |
// << std::scientific |
| 119 |
// << (sampler->getMyPoint(k))[d]; |
| 120 |
// file << " "; |
| 121 |
// } |
| 122 |
// file << std::setprecision(std::numeric_limits<RealT>::digits10) |
| 123 |
// << std::scientific |
| 124 |
// << sampler->getMyWeight(k) << std::endl; |
| 125 |
// } |
| 126 |
// file.close(); |
| 127 |
// commptr->barrier(); |
| 128 |
|
| 129 |
} |
| 130 |
catch (std::logic_error& err) { |
| 131 |
*outStream << err.what() << "\n"; |
| 132 |
errorFlag = -1000; |
| 133 |
}; // end try |
| 134 |
|
| 135 |
if (errorFlag != 0) |
| 136 |
std::cout << "End Result: TEST FAILED\n"; |
| 137 |
else |
| 138 |
std::cout << "End Result: TEST PASSED\n"; |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |