| 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 example_04.cpp |
| 11 |
\brief Shows how to solve the stochastic Navier-Stokes problem. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "Teuchos_Comm.hpp" |
| 15 |
#ifdef HAVE_MPI |
| 16 |
#include "Teuchos_DefaultMpiComm.hpp" |
| 17 |
#endif |
| 18 |
#include "ROL_Stream.hpp" |
| 19 |
#include "Teuchos_GlobalMPISession.hpp" |
| 20 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 21 |
|
| 22 |
#include "Tpetra_Core.hpp" |
| 23 |
#include "Tpetra_Version.hpp" |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
#include <algorithm> |
| 27 |
//#include <fenv.h> |
| 28 |
|
| 29 |
#include "ROL_Bounds.hpp" |
| 30 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 31 |
#include "ROL_MonteCarloGenerator.hpp" |
| 32 |
#include "ROL_StochasticProblem.hpp" |
| 33 |
#include "ROL_Solver.hpp" |
| 34 |
#include "ROL_TpetraTeuchosBatchManager.hpp" |
| 35 |
|
| 36 |
#include "../TOOLS/meshmanager.hpp" |
| 37 |
#include "../TOOLS/pdeconstraint.hpp" |
| 38 |
#include "../TOOLS/pdeobjective.hpp" |
| 39 |
#include "../TOOLS/pdevector.hpp" |
| 40 |
#include "../TOOLS/batchmanager.hpp" |
| 41 |
#include "pde_navier-stokes.hpp" |
| 42 |
#include "obj_navier-stokes.hpp" |
| 43 |
|
| 44 |
typedef double RealT; |
| 45 |
|
| 46 |
template<class Real> |
| 47 |
void setUpAndSolve(const ROL::Ptr<ROL::Problem<Real>> &opt, |
| 48 |
Teuchos::ParameterList &parlist, |
| 49 |
std::ostream &outStream) { |
| 50 |
parlist.sublist("Step").set("Type","Trust Region"); |
| 51 |
ROL::Solver<Real> solver(opt,parlist); |
| 52 |
Teuchos::Time timer("Optimization Time", true); |
| 53 |
solver.solve(outStream); |
| 54 |
timer.stop(); |
| 55 |
outStream << "Total optimization time = " << timer.totalElapsedTime() << " seconds." << std::endl; |
| 56 |
} |
| 57 |
|
| 58 |
template<class Real> |
| 59 |
void print(ROL::Objective<Real> &obj, |
| 60 |
const ROL::Vector<Real> &z, |
| 61 |
ROL::SampleGenerator<Real> &sampler, |
| 62 |
const int ngsamp, |
| 63 |
const ROL::Ptr<const Teuchos::Comm<int>> &comm, |
| 64 |
const std::string &filename) { |
| 65 |
Real tol(1e-8); |
| 66 |
// Build objective function distribution |
| 67 |
int nsamp = sampler.numMySamples(); |
| 68 |
std::vector<Real> myvalues(nsamp), myzerovec(nsamp, 0); |
| 69 |
std::vector<double> gvalues(ngsamp), gzerovec(ngsamp, 0); |
| 70 |
std::vector<Real> sample = sampler.getMyPoint(0); |
| 71 |
int sdim = sample.size(); |
| 72 |
std::vector<std::vector<Real>> mysamples(sdim, myzerovec); |
| 73 |
std::vector<std::vector<double>> gsamples(sdim, gzerovec); |
| 74 |
for (int i = 0; i < nsamp; ++i) { |
| 75 |
sample = sampler.getMyPoint(i); |
| 76 |
obj.setParameter(sample); |
| 77 |
myvalues[i] = static_cast<double>(obj.value(z,tol)); |
| 78 |
for (int j = 0; j < sdim; ++j) { |
| 79 |
mysamples[j][i] = static_cast<double>(sample[j]); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// Send data to root processor |
| 84 |
#ifdef HAVE_MPI |
| 85 |
ROL::Ptr<const Teuchos::MpiComm<int>> mpicomm |
| 86 |
= ROL::dynamicPtrCast<const Teuchos::MpiComm<int>>(comm); |
| 87 |
int nproc = Teuchos::size<int>(*mpicomm); |
| 88 |
std::vector<int> sampleCounts(nproc, 0), sampleDispls(nproc, 0); |
| 89 |
MPI_Gather(&nsamp,1,MPI_INT,&sampleCounts[0],1,MPI_INT,0,*(mpicomm->getRawMpiComm())()); |
| 90 |
for (int i = 1; i < nproc; ++i) { |
| 91 |
sampleDispls[i] = sampleDispls[i-1] + sampleCounts[i-1]; |
| 92 |
} |
| 93 |
MPI_Gatherv(&myvalues[0],nsamp,MPI_DOUBLE,&gvalues[0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())()); |
| 94 |
for (int j = 0; j < sdim; ++j) { |
| 95 |
MPI_Gatherv(&mysamples[j][0],nsamp,MPI_DOUBLE,&gsamples[j][0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())()); |
| 96 |
} |
| 97 |
#else |
| 98 |
gvalues.assign(myvalues.begin(),myvalues.end()); |
| 99 |
for (int j = 0; j < sdim; ++j) { |
| 100 |
gsamples[j].assign(mysamples[j].begin(),mysamples[j].end()); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
// Print |
| 105 |
int rank = Teuchos::rank<int>(*comm); |
| 106 |
if ( rank==0 ) { |
| 107 |
std::ofstream file; |
| 108 |
file.open(filename); |
| 109 |
file << std::scientific << std::setprecision(15); |
| 110 |
for (int i = 0; i < ngsamp; ++i) { |
| 111 |
for (int j = 0; j < sdim; ++j) { |
| 112 |
file << std::setw(25) << std::left << gsamples[j][i]; |
| 113 |
} |
| 114 |
file << std::setw(25) << std::left << gvalues[i] << std::endl; |
| 115 |
} |
| 116 |
file.close(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
int main(int argc, char *argv[]) { |
| 121 |
//feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| 122 |
|
| 123 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 124 |
int iprint = argc - 1; |
| 125 |
ROL::Ptr<std::ostream> outStream; |
| 126 |
ROL::nullstream bhs; // outputs nothing |
| 127 |
|
| 128 |
/*** Initialize communicator. ***/ |
| 129 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 130 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 131 |
= Tpetra::getDefaultComm(); |
| 132 |
ROL::Ptr<const Teuchos::Comm<int>> serial_comm |
| 133 |
= ROL::makePtr<Teuchos::SerialComm<int>>(); |
| 134 |
const int myRank = comm->getRank(); |
| 135 |
if ((iprint > 0) && (myRank == 0)) { |
| 136 |
outStream = ROL::makePtrFromRef(std::cout); |
| 137 |
} |
| 138 |
else { |
| 139 |
outStream = ROL::makePtrFromRef(bhs); |
| 140 |
} |
| 141 |
int errorFlag = 0; |
| 142 |
|
| 143 |
// *** Example body. |
| 144 |
try { |
| 145 |
|
| 146 |
/*** Read in XML input ***/ |
| 147 |
std::string filename = "input_ex04.xml"; |
| 148 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 149 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 150 |
|
| 151 |
// Problem dimensions |
| 152 |
const int stochDim = 2; |
| 153 |
const RealT one(1); |
| 154 |
|
| 155 |
/*************************************************************************/ |
| 156 |
/***************** BUILD GOVERNING PDE ***********************************/ |
| 157 |
/*************************************************************************/ |
| 158 |
/*** Initialize main data structure. ***/ |
| 159 |
ROL::Ptr<MeshManager<RealT>> meshMgr |
| 160 |
= ROL::makePtr<MeshManager_BackwardFacingStepChannel<RealT>>(*parlist); |
| 161 |
// Initialize PDE describing advection-diffusion equation |
| 162 |
ROL::Ptr<PDE_NavierStokes<RealT>> pde |
| 163 |
= ROL::makePtr<PDE_NavierStokes<RealT>>(*parlist); |
| 164 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT>> con |
| 165 |
= ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,serial_comm,*parlist,*outStream); |
| 166 |
// Cast the constraint and get the assembler. |
| 167 |
ROL::Ptr<PDE_Constraint<RealT>> pdecon |
| 168 |
= ROL::dynamicPtrCast<PDE_Constraint<RealT>>(con); |
| 169 |
ROL::Ptr<Assembler<RealT>> assembler = pdecon->getAssembler(); |
| 170 |
assembler->printMeshData(*outStream); |
| 171 |
con->setSolveParameters(*parlist); |
| 172 |
|
| 173 |
/*************************************************************************/ |
| 174 |
/***************** BUILD VECTORS *****************************************/ |
| 175 |
/*************************************************************************/ |
| 176 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr = assembler->createStateVector(); |
| 177 |
ROL::Ptr<Tpetra::MultiVector<>> p_ptr = assembler->createStateVector(); |
| 178 |
ROL::Ptr<Tpetra::MultiVector<>> du_ptr = assembler->createStateVector(); |
| 179 |
u_ptr->randomize(); //u_ptr->putScalar(static_cast<RealT>(1)); |
| 180 |
p_ptr->randomize(); //p_ptr->putScalar(static_cast<RealT>(1)); |
| 181 |
du_ptr->randomize(); //du_ptr->putScalar(static_cast<RealT>(0)); |
| 182 |
ROL::Ptr<ROL::Vector<RealT>> up |
| 183 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 184 |
ROL::Ptr<ROL::Vector<RealT>> pp |
| 185 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 186 |
ROL::Ptr<ROL::Vector<RealT>> dup |
| 187 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler,*parlist); |
| 188 |
// Create residual vectors |
| 189 |
ROL::Ptr<Tpetra::MultiVector<>> r_ptr = assembler->createResidualVector(); |
| 190 |
r_ptr->randomize(); //r_ptr->putScalar(static_cast<RealT>(1)); |
| 191 |
ROL::Ptr<ROL::Vector<RealT>> rp |
| 192 |
= ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 193 |
// Create control vector and set to ones |
| 194 |
ROL::Ptr<Tpetra::MultiVector<>> z_ptr = assembler->createControlVector(); |
| 195 |
ROL::Ptr<Tpetra::MultiVector<>> dz_ptr = assembler->createControlVector(); |
| 196 |
ROL::Ptr<Tpetra::MultiVector<>> yz_ptr = assembler->createControlVector(); |
| 197 |
z_ptr->randomize(); z_ptr->putScalar(static_cast<RealT>(0)); |
| 198 |
dz_ptr->randomize(); //dz_ptr->putScalar(static_cast<RealT>(0)); |
| 199 |
yz_ptr->randomize(); //yz_ptr->putScalar(static_cast<RealT>(0)); |
| 200 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> zpde |
| 201 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 202 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> dzpde |
| 203 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler,*parlist); |
| 204 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> yzpde |
| 205 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(yz_ptr,pde,assembler,*parlist); |
| 206 |
ROL::Ptr<ROL::Vector<RealT>> zp = ROL::makePtr<PDE_OptVector<RealT>>(zpde); |
| 207 |
ROL::Ptr<ROL::Vector<RealT>> dzp = ROL::makePtr<PDE_OptVector<RealT>>(dzpde); |
| 208 |
ROL::Ptr<ROL::Vector<RealT>> yzp = ROL::makePtr<PDE_OptVector<RealT>>(yzpde); |
| 209 |
// Create ROL SimOpt vectors |
| 210 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 211 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 212 |
|
| 213 |
/*************************************************************************/ |
| 214 |
/***************** BUILD COST FUNCTIONAL *********************************/ |
| 215 |
/*************************************************************************/ |
| 216 |
std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(2,ROL::nullPtr); |
| 217 |
qoi_vec[0] = ROL::makePtr<QoI_State_NavierStokes<RealT>>(*parlist, |
| 218 |
pde->getVelocityFE(), |
| 219 |
pde->getPressureFE(), |
| 220 |
pde->getFieldHelper()); |
| 221 |
qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_NavierStokes<RealT>>(pde->getVelocityFE(), |
| 222 |
pde->getPressureFE(), |
| 223 |
pde->getVelocityBdryFE(), |
| 224 |
pde->getBdryCellLocIds(), |
| 225 |
pde->getFieldHelper()); |
| 226 |
ROL::Ptr<StdObjective_NavierStokes<RealT>> std_obj |
| 227 |
= ROL::makePtr<StdObjective_NavierStokes<RealT>>(*parlist); |
| 228 |
ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj |
| 229 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler); |
| 230 |
ROL::Ptr<ROL::Objective_SimOpt<RealT>> objState |
| 231 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec[0],assembler); |
| 232 |
ROL::Ptr<ROL::Objective_SimOpt<RealT>> objCtrl |
| 233 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec[1],assembler); |
| 234 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT>> objRed |
| 235 |
= ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, pp, true, false); |
| 236 |
|
| 237 |
/*************************************************************************/ |
| 238 |
/***************** BUILD BOUND CONSTRAINT ********************************/ |
| 239 |
/*************************************************************************/ |
| 240 |
ROL::Ptr<Tpetra::MultiVector<>> zlo_ptr = assembler->createControlVector(); |
| 241 |
ROL::Ptr<Tpetra::MultiVector<>> zhi_ptr = assembler->createControlVector(); |
| 242 |
zlo_ptr->putScalar(static_cast<RealT>(0)); |
| 243 |
zhi_ptr->putScalar(ROL::ROL_INF<RealT>()); |
| 244 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> zlopde |
| 245 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(zlo_ptr,pde,assembler,*parlist); |
| 246 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> zhipde |
| 247 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(zhi_ptr,pde,assembler,*parlist); |
| 248 |
ROL::Ptr<ROL::Vector<RealT>> zlop = ROL::makePtr<PDE_OptVector<RealT>>(zlopde); |
| 249 |
ROL::Ptr<ROL::Vector<RealT>> zhip = ROL::makePtr<PDE_OptVector<RealT>>(zhipde); |
| 250 |
ROL::Ptr<ROL::BoundConstraint<RealT>> bnd |
| 251 |
= ROL::makePtr<ROL::Bounds<RealT>>(zlop,zhip); |
| 252 |
bool useBounds = parlist->sublist("Problem").get("Use bounds", false); |
| 253 |
if (!useBounds) bnd->deactivate(); |
| 254 |
|
| 255 |
/*************************************************************************/ |
| 256 |
/***************** BUILD SAMPLER *****************************************/ |
| 257 |
/*************************************************************************/ |
| 258 |
int nsamp = parlist->sublist("Problem").get("Number of samples",100); |
| 259 |
int nsamp_dist = parlist->sublist("Problem").get("Number of output samples",100); |
| 260 |
std::vector<RealT> tmp = {-one,one}; |
| 261 |
std::vector<std::vector<RealT>> bounds(stochDim,tmp); |
| 262 |
ROL::Ptr<ROL::BatchManager<RealT>> bman |
| 263 |
= ROL::makePtr<PDE_OptVector_BatchManager<RealT>>(comm); |
| 264 |
ROL::Ptr<ROL::SampleGenerator<RealT>> sampler |
| 265 |
= ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp,bounds,bman); |
| 266 |
ROL::Ptr<ROL::SampleGenerator<RealT>> sampler_dist |
| 267 |
= ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp_dist,bounds,bman); |
| 268 |
|
| 269 |
/*************************************************************************/ |
| 270 |
/***************** BUILD STOCHASTIC PROBLEM ******************************/ |
| 271 |
/*************************************************************************/ |
| 272 |
ROL::Ptr<ROL::StochasticProblem<RealT>> opt; |
| 273 |
std::vector<RealT> ctrl; |
| 274 |
std::vector<RealT> var; |
| 275 |
|
| 276 |
Teuchos::Array<RealT> alphaArray |
| 277 |
= Teuchos::getArrayFromStringParameter<RealT>(parlist->sublist("Problem"),"bPOE Thresholds"); |
| 278 |
std::vector<RealT> alpha = alphaArray.toVector(); |
| 279 |
std::sort(alpha.begin(),alpha.end()); |
| 280 |
int N = alpha.size(); |
| 281 |
|
| 282 |
/*************************************************************************/ |
| 283 |
/***************** SOLVE MEAN PLUS CVAR **********************************/ |
| 284 |
/*************************************************************************/ |
| 285 |
RealT tol(1e-8); |
| 286 |
parlist->sublist("SOL").set("Type","Risk Averse"); |
| 287 |
parlist->sublist("SOL").sublist("Risk Measure").set("Name","bPOE"); |
| 288 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("bPOE").set("Moment Order",2.0); |
| 289 |
for (int i = 0; i < N; ++i) { |
| 290 |
// Solve. |
| 291 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("bPOE").set("Threshold",alpha[i]); |
| 292 |
opt = ROL::makePtr<ROL::StochasticProblem<RealT>>(objRed,zp); |
| 293 |
opt->addBoundConstraint(bnd); |
| 294 |
RealT stat(1); |
| 295 |
if ( i > 0 ) stat = var[i]; |
| 296 |
parlist->sublist("SOL").set("Initial Statistic",stat); |
| 297 |
opt->makeObjectiveStochastic(*parlist,sampler); |
| 298 |
opt->finalize(false,true,*outStream); |
| 299 |
setUpAndSolve<RealT>(opt,*parlist,*outStream); |
| 300 |
// Output. |
| 301 |
ctrl.push_back(objCtrl->value(*up,*zp,tol)); |
| 302 |
try { var.push_back(opt->getSolutionStatistic()); } |
| 303 |
catch (std::exception &e) { var.push_back(0.0); } |
| 304 |
std::stringstream nameCtrl; |
| 305 |
nameCtrl << "control_bPOE_" << i+1 << ".txt"; |
| 306 |
pdecon->outputTpetraVector(z_ptr,nameCtrl.str().c_str()); |
| 307 |
std::stringstream nameObj; |
| 308 |
nameObj << "obj_samples_bPOE_" << i+1 << ".txt"; |
| 309 |
print<RealT>(*objRed,*zp,*sampler_dist,nsamp_dist,comm,nameObj.str()); |
| 310 |
} |
| 311 |
|
| 312 |
/*************************************************************************/ |
| 313 |
/***************** PRINT CONTROL OBJ AND VAR *****************************/ |
| 314 |
/*************************************************************************/ |
| 315 |
const int rank = Teuchos::rank<int>(*comm); |
| 316 |
if ( rank==0 ) { |
| 317 |
std::stringstream nameCTRL, nameVAR; |
| 318 |
nameCTRL << "ctrl.txt"; |
| 319 |
nameVAR << "var.txt"; |
| 320 |
std::ofstream fileCTRL, fileVAR; |
| 321 |
fileCTRL.open(nameCTRL.str()); |
| 322 |
fileVAR.open(nameVAR.str()); |
| 323 |
fileCTRL << std::scientific << std::setprecision(15); |
| 324 |
fileVAR << std::scientific << std::setprecision(15); |
| 325 |
int size = var.size(); |
| 326 |
for (int i = 0; i < size; ++i) { |
| 327 |
fileCTRL << std::setw(25) << std::left << ctrl[i] << std::endl; |
| 328 |
fileVAR << std::setw(25) << std::left << var[i] << std::endl; |
| 329 |
} |
| 330 |
fileCTRL.close(); |
| 331 |
fileVAR.close(); |
| 332 |
} |
| 333 |
|
| 334 |
// Get a summary from the time monitor. |
| 335 |
Teuchos::TimeMonitor::summarize(); |
| 336 |
} |
| 337 |
catch (std::logic_error& err) { |
| 338 |
*outStream << err.what() << "\n"; |
| 339 |
errorFlag = -1000; |
| 340 |
}; // end try |
| 341 |
|
| 342 |
if (errorFlag != 0) |
| 343 |
std::cout << "End Result: TEST FAILED\n"; |
| 344 |
else |
| 345 |
std::cout << "End Result: TEST PASSED\n"; |
| 346 |
|
| 347 |
return 0; |
| 348 |
} |