| 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 |
#ifndef BUILDSAMPLERHPP |
| 11 |
#define BUILDSAMPLERHPP |
| 12 |
|
| 13 |
#include "Teuchos_Comm.hpp" |
| 14 |
#include "ROL_Stream.hpp" |
| 15 |
#include "Teuchos_GlobalMPISession.hpp" |
| 16 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 17 |
|
| 18 |
// SOL Inputs |
| 19 |
#include "ROL_MonteCarloGenerator.hpp" |
| 20 |
#include "ROL_SROMGenerator.hpp" |
| 21 |
#include "ROL_DistributionFactory.hpp" |
| 22 |
#include "ROL_TpetraTeuchosBatchManager.hpp" |
| 23 |
|
| 24 |
#include <iostream> |
| 25 |
#include <fstream> |
| 26 |
#include <sstream> |
| 27 |
#include <string> |
| 28 |
|
| 29 |
template<class Real> |
| 30 |
class BuildSampler { |
| 31 |
private: |
| 32 |
ROL::Ptr<ROL::BatchManager<Real> > bman_; |
| 33 |
ROL::Ptr<ROL::SampleGenerator<Real> > sampler_; |
| 34 |
std::vector<ROL::Ptr<ROL::Distribution<Real> > > distVec_; |
| 35 |
|
| 36 |
public: |
| 37 |
virtual ~BuildSampler() {}; |
| 38 |
BuildSampler(const ROL::Ptr<const Teuchos::Comm<int> > &comm, |
| 39 |
Teuchos::ParameterList &Slist, |
| 40 |
Teuchos::ParameterList &Elist) { |
| 41 |
/*** Build stochastic functionality. ***/ |
| 42 |
Real scale = static_cast<Real>(M_PI)/static_cast<Real>(180); |
| 43 |
int sdim = 0; |
| 44 |
distVec_.clear(); |
| 45 |
// Build distribution for volumetric force |
| 46 |
bool vStochMag = Elist.sublist("Elasticity").get<bool>("Stochastic Bodyforce Magnitude"); |
| 47 |
if ( vStochMag ) { |
| 48 |
Teuchos::ParameterList &magList |
| 49 |
= Slist.sublist("Problem").sublist("Volumetric Force").sublist("Magnitude"); |
| 50 |
distVec_.push_back(ROL::DistributionFactory<Real>(magList)); |
| 51 |
Elist.sublist("Elasticity").set("Bodyforce Magnitude",distVec_[sdim]->moment(1)); |
| 52 |
sdim++; |
| 53 |
} |
| 54 |
bool vStochAng = Elist.sublist("Elasticity").get<bool>("Stochastic Bodyforce Angle"); |
| 55 |
if ( vStochAng ) { |
| 56 |
Teuchos::ParameterList &angList |
| 57 |
= Slist.sublist("Problem").sublist("Volumetric Force").sublist("Angle"); |
| 58 |
distVec_.push_back(ROL::DistributionFactory<Real>(angList)); |
| 59 |
Elist.sublist("Elasticity").set("Bodyforce Angle",scale * distVec_[sdim]->moment(1)); |
| 60 |
sdim++; |
| 61 |
} |
| 62 |
// Build distribution for traction force |
| 63 |
bool tStochMag = Elist.sublist("Elasticity").get<bool>("Stochastic Traction Magnitude"); |
| 64 |
if ( tStochMag ) { |
| 65 |
Teuchos::ParameterList &magList |
| 66 |
= Slist.sublist("Problem").sublist("Traction Force").sublist("Magnitude"); |
| 67 |
distVec_.push_back(ROL::DistributionFactory<Real>(magList)); |
| 68 |
Elist.sublist("Elasticity").set("Traction Magnitude",distVec_[sdim]->moment(1)); |
| 69 |
sdim++; |
| 70 |
} |
| 71 |
bool tStochAng = Elist.sublist("Elasticity").get<bool>("Stochastic Traction Angle"); |
| 72 |
if ( tStochAng ) { |
| 73 |
Teuchos::ParameterList &angList |
| 74 |
= Slist.sublist("Problem").sublist("Traction Force").sublist("Angle"); |
| 75 |
distVec_.push_back(ROL::DistributionFactory<Real>(angList)); |
| 76 |
Elist.sublist("Elasticity").set("Traction Angle",scale * distVec_[sdim]->moment(1)); |
| 77 |
sdim++; |
| 78 |
} |
| 79 |
// Build distribution for point force |
| 80 |
bool pStochMag = Elist.sublist("Elasticity").get<bool>("Stochastic Pointload Magnitude"); |
| 81 |
if ( pStochMag ) { |
| 82 |
Teuchos::ParameterList &magList |
| 83 |
= Slist.sublist("Problem").sublist("Point Force").sublist("Magnitude"); |
| 84 |
distVec_.push_back(ROL::DistributionFactory<Real>(magList)); |
| 85 |
Elist.sublist("Elasticity").set("Pointload Magnitude",distVec_[sdim]->moment(1)); |
| 86 |
sdim++; |
| 87 |
} |
| 88 |
bool pStochAng = Elist.sublist("Elasticity").get<bool>("Stochastic Pointload Angle"); |
| 89 |
if ( pStochAng ) { |
| 90 |
Teuchos::ParameterList &angList |
| 91 |
= Slist.sublist("Problem").sublist("Point Force").sublist("Angle"); |
| 92 |
distVec_.push_back(ROL::DistributionFactory<Real>(angList)); |
| 93 |
Elist.sublist("Elasticity").set("Pointload Angle",scale * distVec_[sdim]->moment(1)); |
| 94 |
sdim++; |
| 95 |
} |
| 96 |
// Build sampler |
| 97 |
int nsamp = Slist.sublist("Problem").get("Number of Samples",100); |
| 98 |
if ( !vStochMag && !vStochAng && |
| 99 |
!tStochMag && !tStochAng && |
| 100 |
!pStochMag && !pStochAng ) { |
| 101 |
nsamp = 1; |
| 102 |
} |
| 103 |
bman_ = ROL::makePtr<ROL::TpetraTeuchosBatchManager<Real>>(comm); |
| 104 |
bool useOBS = Slist.sublist("Problem").get("Use Optimization-Based Sampling",false); |
| 105 |
if ( useOBS ) { |
| 106 |
Slist.sublist("SOL").sublist("Sample Generator").sublist("SROM").set("Number of Samples",nsamp); |
| 107 |
sampler_ = ROL::makePtr<ROL::SROMGenerator<Real>>(Slist,bman_,distVec_); |
| 108 |
} |
| 109 |
else { |
| 110 |
sampler_ = ROL::makePtr<ROL::MonteCarloGenerator<Real>>(nsamp,distVec_,bman_,false,false,0); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
ROL::Ptr<ROL::SampleGenerator<Real> >& get(void) { |
| 115 |
return sampler_; |
| 116 |
} |
| 117 |
|
| 118 |
ROL::Ptr<ROL::BatchManager<Real> >& getBatchManager(void) { |
| 119 |
return bman_; |
| 120 |
} |
| 121 |
|
| 122 |
void print(const std::string &filename, const int prec = 12) const { |
| 123 |
int width = prec + 5 + 4; |
| 124 |
std::stringstream name; |
| 125 |
name << filename << "_" << sampler_->batchID() << ".txt"; |
| 126 |
std::ofstream file(name.str().c_str()); |
| 127 |
if (file.is_open()) { |
| 128 |
file << std::scientific << std::setprecision(prec); |
| 129 |
for (int i = 0; i < sampler_->numMySamples(); ++i) { |
| 130 |
std::vector<Real> pt = sampler_->getMyPoint(i); |
| 131 |
Real wt = sampler_->getMyWeight(i); |
| 132 |
for (int j = 0; j < static_cast<int>(pt.size()); ++j) { |
| 133 |
file << std::setw(width) << std::left << pt[j]; |
| 134 |
} |
| 135 |
file << std::setw(width) << std::left << wt << std::endl; |
| 136 |
} |
| 137 |
file.close(); |
| 138 |
} |
| 139 |
else { |
| 140 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 141 |
">>> (BuildSampler::print): Unable to open file!"); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
}; |
| 146 |
|
| 147 |
#endif |