| 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_01.cpp |
| 11 |
\brief Shows how to solve the Navier-Stokes control problem. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "Teuchos_Comm.hpp" |
| 15 |
#include "ROL_Stream.hpp" |
| 16 |
#include "Teuchos_GlobalMPISession.hpp" |
| 17 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 18 |
|
| 19 |
#include "Tpetra_Core.hpp" |
| 20 |
#include "Tpetra_Version.hpp" |
| 21 |
|
| 22 |
#include <iostream> |
| 23 |
#include <algorithm> |
| 24 |
//#include <fenv.h> |
| 25 |
|
| 26 |
#include "ROL_TpetraMultiVector.hpp" |
| 27 |
#include "ROL_Algorithm.hpp" |
| 28 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 29 |
#include "ROL_MonteCarloGenerator.hpp" |
| 30 |
#include "ROL_OptimizationSolver.hpp" |
| 31 |
#include "ROL_TpetraTeuchosBatchManager.hpp" |
| 32 |
|
| 33 |
#include "../TOOLS/pdeconstraint.hpp" |
| 34 |
#include "../TOOLS/pdeobjective.hpp" |
| 35 |
#include "../TOOLS/pdevector.hpp" |
| 36 |
#include "../TOOLS/batchmanager.hpp" |
| 37 |
#include "pde_thermal-fluids_ex03.hpp" |
| 38 |
#include "obj_thermal-fluids_ex03.hpp" |
| 39 |
#include "mesh_thermal-fluids.hpp" |
| 40 |
|
| 41 |
typedef double RealT; |
| 42 |
|
| 43 |
template<class Real> |
| 44 |
void setUpAndSolve(ROL::OptimizationProblem<Real> &opt, |
| 45 |
Teuchos::ParameterList &parlist, |
| 46 |
std::ostream &outStream) { |
| 47 |
parlist.sublist("Step").set("Type","Trust Region"); |
| 48 |
ROL::OptimizationSolver<RealT> solver(opt,parlist); |
| 49 |
Teuchos::Time timer("Optimization Time", true); |
| 50 |
solver.solve(outStream); |
| 51 |
timer.stop(); |
| 52 |
outStream << "Total optimization time = " << timer.totalElapsedTime() << " seconds." << std::endl; |
| 53 |
} |
| 54 |
|
| 55 |
template<class Real> |
| 56 |
void print(ROL::Objective<Real> &obj, |
| 57 |
const ROL::Vector<Real> &z, |
| 58 |
ROL::SampleGenerator<Real> &sampler, |
| 59 |
const int ngsamp, |
| 60 |
const ROL::Ptr<const Teuchos::Comm<int> > &comm, |
| 61 |
const std::string &filename) { |
| 62 |
Real tol(1e-8); |
| 63 |
// Build objective function distribution |
| 64 |
int nsamp = sampler.numMySamples(); |
| 65 |
std::vector<Real> myvalues(nsamp), myzerovec(nsamp, 0); |
| 66 |
std::vector<double> gvalues(ngsamp), gzerovec(ngsamp, 0); |
| 67 |
std::vector<Real> sample = sampler.getMyPoint(0); |
| 68 |
int sdim = sample.size(); |
| 69 |
std::vector<std::vector<Real> > mysamples(sdim, myzerovec); |
| 70 |
std::vector<std::vector<double> > gsamples(sdim, gzerovec); |
| 71 |
for (int i = 0; i < nsamp; ++i) { |
| 72 |
sample = sampler.getMyPoint(i); |
| 73 |
obj.setParameter(sample); |
| 74 |
myvalues[i] = static_cast<double>(obj.value(z,tol)); |
| 75 |
for (int j = 0; j < sdim; ++j) { |
| 76 |
mysamples[j][i] = static_cast<double>(sample[j]); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Send data to root processor |
| 81 |
#ifdef HAVE_MPI |
| 82 |
ROL::Ptr<const Teuchos::MpiComm<int> > mpicomm |
| 83 |
= ROL::dynamicPtrCast<const Teuchos::MpiComm<int> >(comm); |
| 84 |
int nproc = Teuchos::size<int>(*mpicomm); |
| 85 |
std::vector<int> sampleCounts(nproc, 0), sampleDispls(nproc, 0); |
| 86 |
MPI_Gather(&nsamp,1,MPI_INT,&sampleCounts[0],1,MPI_INT,0,*(mpicomm->getRawMpiComm())()); |
| 87 |
for (int i = 1; i < nproc; ++i) { |
| 88 |
sampleDispls[i] = sampleDispls[i-1] + sampleCounts[i-1]; |
| 89 |
} |
| 90 |
MPI_Gatherv(&myvalues[0],nsamp,MPI_DOUBLE,&gvalues[0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())()); |
| 91 |
for (int j = 0; j < sdim; ++j) { |
| 92 |
MPI_Gatherv(&mysamples[j][0],nsamp,MPI_DOUBLE,&gsamples[j][0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())()); |
| 93 |
} |
| 94 |
#else |
| 95 |
gvalues.assign(myvalues.begin(),myvalues.end()); |
| 96 |
for (int j = 0; j < sdim; ++j) { |
| 97 |
gsamples[j].assign(mysamples[j].begin(),mysamples[j].end()); |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
// Print |
| 102 |
int rank = Teuchos::rank<int>(*comm); |
| 103 |
if ( rank==0 ) { |
| 104 |
std::ofstream file; |
| 105 |
file.open(filename); |
| 106 |
file << std::scientific << std::setprecision(15); |
| 107 |
for (int i = 0; i < ngsamp; ++i) { |
| 108 |
for (int j = 0; j < sdim; ++j) { |
| 109 |
file << std::setw(25) << std::left << gsamples[j][i]; |
| 110 |
} |
| 111 |
file << std::setw(25) << std::left << gvalues[i] << std::endl; |
| 112 |
} |
| 113 |
file.close(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
int main(int argc, char *argv[]) { |
| 118 |
//feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| 119 |
|
| 120 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 121 |
int iprint = argc - 1; |
| 122 |
ROL::Ptr<std::ostream> outStream; |
| 123 |
ROL::nullstream bhs; // outputs nothing |
| 124 |
|
| 125 |
/*** Initialize communicator. ***/ |
| 126 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 127 |
ROL::Ptr<const Teuchos::Comm<int> > comm |
| 128 |
= Tpetra::getDefaultComm(); |
| 129 |
ROL::Ptr<const Teuchos::Comm<int> > serial_comm |
| 130 |
= ROL::makePtr<Teuchos::SerialComm<int>>(); |
| 131 |
const int myRank = comm->getRank(); |
| 132 |
if ((iprint > 0) && (myRank == 0)) { |
| 133 |
outStream = ROL::makePtrFromRef(std::cout); |
| 134 |
} |
| 135 |
else { |
| 136 |
outStream = ROL::makePtrFromRef(bhs); |
| 137 |
} |
| 138 |
int errorFlag = 0; |
| 139 |
|
| 140 |
// *** Example body. |
| 141 |
try { |
| 142 |
|
| 143 |
/*** Read in XML input ***/ |
| 144 |
std::string filename = "input_ex06.xml"; |
| 145 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 146 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 147 |
parlist->sublist("SimOpt").sublist("Solve").set("Output Iteration History",myRank==0); |
| 148 |
|
| 149 |
/*** Initialize main data structure. ***/ |
| 150 |
ROL::Ptr<MeshManager<RealT> > meshMgr |
| 151 |
= ROL::makePtr<MeshManager_ThermalFluids<RealT>>(*parlist); |
| 152 |
// Initialize PDE describing Navier-Stokes equations. |
| 153 |
ROL::Ptr<PDE_ThermalFluids_ex03<RealT> > pde |
| 154 |
= ROL::makePtr<PDE_ThermalFluids_ex03<RealT>>(*parlist); |
| 155 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con |
| 156 |
= ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,serial_comm,*parlist,*outStream); |
| 157 |
// Cast the constraint and get the assembler. |
| 158 |
ROL::Ptr<PDE_Constraint<RealT> > pdecon |
| 159 |
= ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con); |
| 160 |
ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler(); |
| 161 |
con->setSolveParameters(*parlist); |
| 162 |
pdecon->outputTpetraData(); |
| 163 |
|
| 164 |
// Create state vector and set to zeroes |
| 165 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr, p_ptr, du_ptr, dy_ptr, yu_ptr, yp_ptr, r_ptr, z_ptr, dz_ptr; |
| 166 |
u_ptr = assembler->createStateVector(); u_ptr->randomize(); |
| 167 |
p_ptr = assembler->createStateVector(); p_ptr->randomize(); |
| 168 |
du_ptr = assembler->createStateVector(); du_ptr->randomize(); |
| 169 |
dy_ptr = assembler->createStateVector(); dy_ptr->randomize(); |
| 170 |
yu_ptr = assembler->createStateVector(); yu_ptr->randomize(); |
| 171 |
yp_ptr = assembler->createStateVector(); yp_ptr->randomize(); |
| 172 |
r_ptr = assembler->createResidualVector(); r_ptr->randomize(); |
| 173 |
z_ptr = assembler->createControlVector(); z_ptr->randomize(); |
| 174 |
dz_ptr = assembler->createControlVector(); dz_ptr->randomize(); |
| 175 |
ROL::Ptr<ROL::Vector<RealT> > up, pp, dup, dyp, yup, ypp, rp, zp, dzp; |
| 176 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler); |
| 177 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler); |
| 178 |
dup = ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler); |
| 179 |
dyp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(dy_ptr,pde,assembler); |
| 180 |
yup = ROL::makePtr<PDE_PrimalSimVector<RealT>>(yu_ptr,pde,assembler); |
| 181 |
ypp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(yp_ptr,pde,assembler); |
| 182 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler); |
| 183 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler); |
| 184 |
dzp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler); |
| 185 |
// Create ROL SimOpt vectors |
| 186 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 187 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 188 |
|
| 189 |
// Initialize objective function. |
| 190 |
std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr); |
| 191 |
qoi_vec[0] = ROL::makePtr<QoI_State_ThermalFluids<RealT>>(*parlist, |
| 192 |
pde->getVelocityFE(), |
| 193 |
pde->getPressureFE(), |
| 194 |
pde->getThermalFE(), |
| 195 |
pde->getFieldHelper()); |
| 196 |
qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_ThermalFluids<RealT>>(pde->getVelocityFE(), |
| 197 |
pde->getPressureFE(), |
| 198 |
pde->getThermalFE(), |
| 199 |
pde->getThermalBdryFE(), |
| 200 |
pde->getBdryCellLocIds(), |
| 201 |
pde->getFieldHelper()); |
| 202 |
ROL::Ptr<StdObjective_ThermalFluids<RealT> > std_obj |
| 203 |
= ROL::makePtr<StdObjective_ThermalFluids<RealT>>(*parlist); |
| 204 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj |
| 205 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler); |
| 206 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > objState |
| 207 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec[0],assembler); |
| 208 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > objCtrl |
| 209 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec[1],assembler); |
| 210 |
ROL::Ptr<ROL::VectorController<RealT> > stateStore |
| 211 |
= ROL::makePtr<ROL::VectorController<RealT>>(); |
| 212 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > robj |
| 213 |
= ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, stateStore, up, zp, pp, true, false); |
| 214 |
|
| 215 |
/*************************************************************************/ |
| 216 |
/***************** BUILD SAMPLER *****************************************/ |
| 217 |
/*************************************************************************/ |
| 218 |
int Nbottom = parlist->sublist("Problem").get("Bottom KL Truncation Order",5); |
| 219 |
int Nleft = parlist->sublist("Problem").get("Left KL Truncation Order",5); |
| 220 |
int Nright = parlist->sublist("Problem").get("Right KL Truncation Order",5); |
| 221 |
int stochDim = Nbottom + Nleft + Nright + 3; |
| 222 |
int nsamp = parlist->sublist("Problem").get("Number of samples",100); |
| 223 |
int nsamp_dist = parlist->sublist("Problem").get("Number of output samples",100); |
| 224 |
// Build vector of distributions |
| 225 |
std::vector<ROL::Ptr<ROL::Distribution<RealT> > > distVec(stochDim); |
| 226 |
Teuchos::ParameterList UList; |
| 227 |
UList.sublist("Distribution").set("Name","Uniform"); |
| 228 |
UList.sublist("Distribution").sublist("Uniform").set("Lower Bound",-1.0); |
| 229 |
UList.sublist("Distribution").sublist("Uniform").set("Upper Bound", 1.0); |
| 230 |
for (int i = 0; i < stochDim; ++i) { |
| 231 |
distVec[i] = ROL::DistributionFactory<RealT>(UList); |
| 232 |
} |
| 233 |
// Sampler |
| 234 |
ROL::Ptr<ROL::BatchManager<RealT> > bman |
| 235 |
= ROL::makePtr<ROL::TpetraTeuchosBatchManager<RealT>>(comm); |
| 236 |
// = ROL::makePtr<PDE_OptVector_BatchManager<RealT>>(comm); |
| 237 |
ROL::Ptr<ROL::SampleGenerator<RealT> > sampler |
| 238 |
= ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp,distVec,bman); |
| 239 |
ROL::Ptr<ROL::SampleGenerator<RealT> > sampler_dist |
| 240 |
= ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp_dist,distVec,bman); |
| 241 |
|
| 242 |
/*************************************************************************/ |
| 243 |
/***************** BUILD STOCHASTIC PROBLEM ******************************/ |
| 244 |
/*************************************************************************/ |
| 245 |
ROL::Ptr<ROL::OptimizationProblem<RealT> > opt; |
| 246 |
std::vector<RealT> ctrl, stat; |
| 247 |
|
| 248 |
Teuchos::Array<RealT> lambdaArray |
| 249 |
= Teuchos::getArrayFromStringParameter<RealT>(parlist->sublist("Problem"),"Convex Combination Parameters"); |
| 250 |
std::vector<RealT> lambda = lambdaArray.toVector(); |
| 251 |
std::sort(lambda.begin(),lambda.end()); |
| 252 |
int N = lambda.size(); |
| 253 |
RealT alpha = parlist->sublist("Problem").get("CVaR Confidence Level",0.9); |
| 254 |
|
| 255 |
/*************************************************************************/ |
| 256 |
/***************** SOLVE RISK NEUTRAL ************************************/ |
| 257 |
/*************************************************************************/ |
| 258 |
zp->zero(); up->zero(); |
| 259 |
RealT tol(1e-8), zero(0), one(1); |
| 260 |
bool lambdaZero = (lambda[0] == zero); |
| 261 |
if ( lambdaZero ) { |
| 262 |
lambda.erase(lambda.begin()); --N; |
| 263 |
// Solve. |
| 264 |
parlist->sublist("SOL").set("Type","Risk Neutral"); |
| 265 |
opt = ROL::makePtr<ROL::OptimizationProblem<RealT>>(robj,zp); |
| 266 |
parlist->sublist("SOL").set("Initial Statistic",one); |
| 267 |
opt->setStochasticObjective(*parlist,sampler); |
| 268 |
setUpAndSolve<RealT>(*opt,*parlist,*outStream); |
| 269 |
// Output. |
| 270 |
ctrl.push_back(objCtrl->value(*up,*zp,tol)); |
| 271 |
stat.push_back(opt->getSolutionStatistic()); |
| 272 |
std::string CtrlRN = "control_RN.txt"; |
| 273 |
pdecon->outputTpetraVector(z_ptr,CtrlRN); |
| 274 |
std::string ObjRN = "obj_samples_RN.txt"; |
| 275 |
print<RealT>(*robj,*zp,*sampler_dist,nsamp_dist,comm,ObjRN); |
| 276 |
} |
| 277 |
|
| 278 |
/*************************************************************************/ |
| 279 |
/***************** SOLVE MEAN PLUS CVAR **********************************/ |
| 280 |
/*************************************************************************/ |
| 281 |
parlist->sublist("SOL").set("Type","Risk Averse"); |
| 282 |
parlist->sublist("SOL").sublist("Risk Measure").set("Name","CVaR"); |
| 283 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Confidence Level",alpha); |
| 284 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Smoothing Parameter",1e-4); |
| 285 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").set("Name","Parabolic"); |
| 286 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Lower Bound",0.0); |
| 287 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Upper Bound",1.0); |
| 288 |
for (int i = 0; i < N; ++i) { |
| 289 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Convex Combination Parameter",one-lambda[i]); |
| 290 |
// Solve. |
| 291 |
opt = ROL::makePtr<ROL::OptimizationProblem<RealT>>(robj,zp); |
| 292 |
parlist->sublist("SOL").set("Initial Statistic",stat[i]); |
| 293 |
opt->setStochasticObjective(*parlist,sampler); |
| 294 |
setUpAndSolve<RealT>(*opt,*parlist,*outStream); |
| 295 |
// Output. |
| 296 |
ctrl.push_back(objCtrl->value(*up,*zp,tol)); |
| 297 |
stat.push_back(opt->getSolutionStatistic()); |
| 298 |
std::stringstream nameCtrl; |
| 299 |
nameCtrl << "control_MCVaR_" << i << ".txt"; |
| 300 |
pdecon->outputTpetraVector(z_ptr,nameCtrl.str().c_str()); |
| 301 |
std::stringstream nameObj; |
| 302 |
nameObj << "obj_samples_MCVaR_" << i << ".txt"; |
| 303 |
print<RealT>(*robj,*zp,*sampler_dist,nsamp_dist,comm,nameObj.str()); |
| 304 |
} |
| 305 |
|
| 306 |
/*************************************************************************/ |
| 307 |
/***************** OUTPUT RESULTS ****************************************/ |
| 308 |
/*************************************************************************/ |
| 309 |
const int rank = Teuchos::rank<int>(*comm); |
| 310 |
if ( rank==0 ) { |
| 311 |
std::stringstream nameCTRL, nameVAR; |
| 312 |
nameCTRL << "ctrl.txt"; |
| 313 |
nameVAR << "stat.txt"; |
| 314 |
std::ofstream fileCTRL, fileVAR; |
| 315 |
fileCTRL.open(nameCTRL.str()); |
| 316 |
fileVAR.open(nameVAR.str()); |
| 317 |
fileCTRL << std::scientific << std::setprecision(15); |
| 318 |
fileVAR << std::scientific << std::setprecision(15); |
| 319 |
int size = stat.size(); |
| 320 |
for (int i = 0; i < size; ++i) { |
| 321 |
fileCTRL << std::setw(25) << std::left << ctrl[i] << std::endl; |
| 322 |
fileVAR << std::setw(25) << std::left << stat[i] << std::endl; |
| 323 |
} |
| 324 |
fileCTRL.close(); |
| 325 |
fileVAR.close(); |
| 326 |
} |
| 327 |
} |
| 328 |
catch (std::logic_error& err) { |
| 329 |
*outStream << err.what() << "\n"; |
| 330 |
errorFlag = -1000; |
| 331 |
}; // end try |
| 332 |
|
| 333 |
if (errorFlag != 0) |
| 334 |
std::cout << "End Result: TEST FAILED\n"; |
| 335 |
else |
| 336 |
std::cout << "End Result: TEST PASSED\n"; |
| 337 |
|
| 338 |
return 0; |
| 339 |
} |