| 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 "Teuchos_ParameterList.hpp" |
| 11 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 12 |
#include "ROL_Stream.hpp" |
| 13 |
|
| 14 |
#ifdef HAVE_MPI |
| 15 |
#include "Epetra_MpiComm.h" |
| 16 |
#else |
| 17 |
#include "Epetra_SerialComm.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "ROL_EpetraBatchManager.hpp" |
| 21 |
#include "ROL_SROMGenerator.hpp" |
| 22 |
#include "ROL_DistributionFactory.hpp" |
| 23 |
|
| 24 |
typedef double RealT; |
| 25 |
|
| 26 |
int main(int argc, char* argv[]) { |
| 27 |
ROL::Ptr<Epetra_Comm> comm; |
| 28 |
#ifdef HAVE_MPI |
| 29 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); |
| 30 |
comm = ROL::makePtr<Epetra_MpiComm>(MPI_COMM_WORLD); |
| 31 |
#else |
| 32 |
comm = ROL::makePtr<Epetra_SerialComm>(); |
| 33 |
#endif |
| 34 |
|
| 35 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 36 |
int iprint = argc - 1; |
| 37 |
ROL::Ptr<std::ostream> outStream; |
| 38 |
ROL::nullstream bhs; // outputs nothing |
| 39 |
if (iprint > 0 && comm->MyPID() == 0) |
| 40 |
outStream = ROL::makePtrFromRef(std::cout); |
| 41 |
else |
| 42 |
outStream = ROL::makePtrFromRef(bhs); |
| 43 |
|
| 44 |
int errorFlag = 0; |
| 45 |
|
| 46 |
try { |
| 47 |
/**********************************************************************************************/ |
| 48 |
/************************* CONSTRUCT SOL COMPONENTS *******************************************/ |
| 49 |
/**********************************************************************************************/ |
| 50 |
// Set random seed |
| 51 |
srand(123456789); |
| 52 |
// Build samplers |
| 53 |
size_t dimension = 1; |
| 54 |
|
| 55 |
// Initialize distribution |
| 56 |
ROL::Ptr<ROL::Distribution<RealT> > dist; |
| 57 |
std::vector<ROL::Ptr<ROL::Distribution<RealT> > > distVec(dimension); |
| 58 |
Teuchos::ParameterList Dlist; |
| 59 |
Dlist.sublist("SOL").sublist("Distribution").set("Name","Beta"); |
| 60 |
RealT alpha = 1., beta = 4.; |
| 61 |
// Fill moment vector and initial guess |
| 62 |
for (size_t d = 0; d < dimension; d++) { |
| 63 |
// Build distribution for dimension d |
| 64 |
alpha++; beta++; |
| 65 |
Dlist.sublist("SOL").sublist("Distribution").sublist("Beta").set("Shape 1",alpha); |
| 66 |
Dlist.sublist("SOL").sublist("Distribution").sublist("Beta").set("Shape 2",beta); |
| 67 |
dist = ROL::DistributionFactory<RealT>(Dlist); |
| 68 |
distVec[d] = ROL::DistributionFactory<RealT>(Dlist); |
| 69 |
} |
| 70 |
|
| 71 |
// Get ROL parameterlist |
| 72 |
std::string filename = "input_02.xml"; |
| 73 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 74 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 75 |
|
| 76 |
Teuchos::ParameterList &list = parlist->sublist("SOL").sublist("Sample Generator").sublist("SROM"); |
| 77 |
Teuchos::Array<int> moments = Teuchos::getArrayFromStringParameter<int>(list,"Moments"); |
| 78 |
size_t numMoments = static_cast<size_t>(moments.size()); |
| 79 |
|
| 80 |
ROL::Ptr<ROL::BatchManager<RealT> > bman = |
| 81 |
ROL::makePtr<ROL::EpetraBatchManager<RealT>>(comm); |
| 82 |
ROL::Ptr<ROL::SampleGenerator<RealT> > sampler = |
| 83 |
ROL::makePtr<ROL::SROMGenerator<RealT>>(*parlist,bman,distVec,*outStream); |
| 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 |
for (size_t k = 0; k < (size_t)sampler->numMySamples(); k++) { |
| 112 |
for (size_t d = 0; d < dimension; d++) { |
| 113 |
*outStream << std::setw(20) << (sampler->getMyPoint(k))[d] << " "; |
| 114 |
} |
| 115 |
*outStream << std::setw(20) << sampler->getMyWeight(k) << std::endl; |
| 116 |
} |
| 117 |
*outStream << std::endl; |
| 118 |
*/ |
| 119 |
} |
| 120 |
catch (std::logic_error& err) { |
| 121 |
*outStream << err.what() << "\n"; |
| 122 |
errorFlag = -1000; |
| 123 |
}; // end try |
| 124 |
|
| 125 |
if (errorFlag != 0) |
| 126 |
std::cout << "End Result: TEST FAILED\n"; |
| 127 |
else |
| 128 |
std::cout << "End Result: TEST PASSED\n"; |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |