| 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_03.cpp |
| 11 |
\brief Solve the stochastic stuctural topology optimization problem |
| 12 |
using Mean-Plus-CVaR for a variety of confidence levels and |
| 13 |
convex-combination parameters. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "Teuchos_Comm.hpp" |
| 17 |
#include "Teuchos_Time.hpp" |
| 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 |
|
| 28 |
#include "ROL_Solver.hpp" |
| 29 |
#include "ROL_StochasticProblem.hpp" |
| 30 |
#include "ROL_Bounds.hpp" |
| 31 |
#include "ROL_LinearCombinationObjective.hpp" |
| 32 |
#include "ROL_TpetraTeuchosBatchManager.hpp" |
| 33 |
#include "ROL_SingletonTeuchosBatchManager.hpp" |
| 34 |
|
| 35 |
#include "../../TOOLS/pdeobjective.hpp" |
| 36 |
#include "../../TOOLS/pdevector.hpp" |
| 37 |
#include "../../TOOLS/meshreader.hpp" |
| 38 |
|
| 39 |
#include "src/mesh_topo-opt.hpp" |
| 40 |
#include "src/pde_elasticity.hpp" |
| 41 |
#include "src/sampler.hpp" |
| 42 |
|
| 43 |
#include "../src/pde_filter.hpp" |
| 44 |
#include "../src/filtered_compliance_robj.hpp" |
| 45 |
#include "../src/volume_con.hpp" |
| 46 |
#include "../src/volume_obj.hpp" |
| 47 |
#include "../src/obj_volume.hpp" |
| 48 |
#include "../src/obj_phasefield.hpp" |
| 49 |
#include "../src/printCDF.hpp" |
| 50 |
|
| 51 |
typedef double RealT; |
| 52 |
|
| 53 |
int main(int argc, char *argv[]) { |
| 54 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 55 |
int iprint = argc - 1; |
| 56 |
ROL::Ptr<std::ostream> outStream; |
| 57 |
ROL::nullstream bhs; // outputs nothing |
| 58 |
|
| 59 |
/*** Initialize communicator. ***/ |
| 60 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 61 |
ROL::Ptr<const Teuchos::Comm<int> > comm |
| 62 |
= Tpetra::getDefaultComm(); |
| 63 |
ROL::Ptr<const Teuchos::Comm<int> > serial_comm |
| 64 |
= ROL::makePtr<Teuchos::SerialComm<int>>(); |
| 65 |
const int myRank = comm->getRank(); |
| 66 |
if ((iprint > 0) && (myRank == 0)) { |
| 67 |
outStream = ROL::makePtrFromRef(std::cout); |
| 68 |
} |
| 69 |
else { |
| 70 |
outStream = ROL::makePtrFromRef(bhs); |
| 71 |
} |
| 72 |
int errorFlag = 0; |
| 73 |
|
| 74 |
// *** Example body. |
| 75 |
try { |
| 76 |
RealT tol(1e-8); |
| 77 |
|
| 78 |
/*** Read in XML input ***/ |
| 79 |
std::string filename = "input_ex03.xml"; |
| 80 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 81 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 82 |
|
| 83 |
// Retrieve parameters. |
| 84 |
const std::string example = parlist->sublist("Problem").get("Example", "Default"); |
| 85 |
const RealT volFraction = parlist->sublist("Problem").get("Volume Fraction", 0.4); |
| 86 |
const RealT initDens = parlist->sublist("Problem").get("Initial Density",volFraction); |
| 87 |
const RealT cmpFactor = parlist->sublist("Problem").get("Compliance Factor", 1.1); |
| 88 |
int probDim = parlist->sublist("Problem").get("Problem Dimension", 2); |
| 89 |
const std::string minType = parlist->sublist("Problem").get("Minimization Type", "Volume"); |
| 90 |
const bool usePhaseField = parlist->sublist("Problem").get("Use Phase Field", false); |
| 91 |
bool useFilter = parlist->sublist("Problem").get("Use Filter", true); |
| 92 |
const bool normalizeObj = parlist->sublist("Problem").get("Normalize Compliance", true); |
| 93 |
const bool volEq = parlist->sublist("Problem").get("Use Volume Equality Constraint",true); |
| 94 |
std::string hessAppr = parlist->sublist("Problem").get("Hessian Approximation","None"); |
| 95 |
hessAppr = (minType == "Compliance" ? hessAppr : "None"); |
| 96 |
if (example == "2D Wheel" || |
| 97 |
example == "2D Truss" || |
| 98 |
example == "2D Cantilever with 1 Load" || |
| 99 |
example == "2D Cantilever with 3 Loads" || |
| 100 |
example == "2D Beams" || |
| 101 |
example == "2D Carrier Plate") |
| 102 |
probDim = 2; |
| 103 |
else if (example == "3D Cantilever" || |
| 104 |
example == "3D L Beam") |
| 105 |
probDim = 3; |
| 106 |
if (usePhaseField) useFilter = false; |
| 107 |
|
| 108 |
/*** Initialize main data structure. ***/ |
| 109 |
TEUCHOS_TEST_FOR_EXCEPTION(probDim<2 || probDim>3, std::invalid_argument, |
| 110 |
">>> PDE-OPT/topo-opt/elasticity/example_01.cpp: Problem dim is not 2 or 3!"); |
| 111 |
ROL::Ptr<MeshManager<RealT>> meshMgr; |
| 112 |
if (probDim == 2) meshMgr = ROL::makePtr<MeshManager_TopoOpt<RealT>>(*parlist); |
| 113 |
else meshMgr = ROL::makePtr<MeshReader<RealT>>(*parlist); |
| 114 |
// Initialize PDE describing elasticity equations. |
| 115 |
ROL::Ptr<PDE_Elasticity<RealT>> |
| 116 |
pde = ROL::makePtr<PDE_Elasticity<RealT>>(*parlist); |
| 117 |
|
| 118 |
// Initialize the filter PDE. |
| 119 |
ROL::Ptr<PDE<RealT>> pdeFilter = pde; |
| 120 |
|
| 121 |
// Initialize reduced compliance objective function. |
| 122 |
ROL::Ptr<ROL::Objective<RealT>> robj_com; |
| 123 |
ROL::Ptr<Assembler<RealT>> assembler, assemblerFilter; |
| 124 |
if (useFilter && !usePhaseField) { |
| 125 |
pdeFilter = ROL::makePtr<PDE_Filter<RealT>>(*parlist); |
| 126 |
pde->setDensityFields(pdeFilter->getFields()); |
| 127 |
robj_com = ROL::makePtr<TopOptFilteredComplianceObjective<RealT>>( |
| 128 |
pde,pdeFilter,meshMgr,comm,*parlist,*outStream); |
| 129 |
assembler = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->getAssembler(); |
| 130 |
assemblerFilter = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->getFilterAssembler(); |
| 131 |
} |
| 132 |
else { |
| 133 |
robj_com = ROL::makePtr<TopOptComplianceObjective<RealT>>( |
| 134 |
pde,meshMgr,comm,*parlist,*outStream); |
| 135 |
assembler = ROL::dynamicPtrCast<TopOptComplianceObjective<RealT>>(robj_com)->getAssembler(); |
| 136 |
assemblerFilter = assembler; |
| 137 |
} |
| 138 |
|
| 139 |
// Create vectors. |
| 140 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr, p_ptr, r_ptr, z_ptr; |
| 141 |
u_ptr = assembler->createStateVector(); u_ptr->putScalar(0.0); |
| 142 |
p_ptr = assembler->createStateVector(); p_ptr->putScalar(0.0); |
| 143 |
r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0); |
| 144 |
z_ptr = assemblerFilter->createControlVector(); z_ptr->putScalar(1.0); |
| 145 |
ROL::Ptr<ROL::Vector<RealT>> up, pp, rp, zp; |
| 146 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 147 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 148 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 149 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pdeFilter,assemblerFilter,*parlist); |
| 150 |
|
| 151 |
// Build volume objective function. |
| 152 |
ROL::Ptr<QoI<RealT>> qoi_vol; |
| 153 |
if (minType == "Compliance" && volEq) { |
| 154 |
if (useFilter && !usePhaseField) |
| 155 |
qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(ROL::dynamicPtrCast<PDE_Filter<RealT>>(pdeFilter)->getDensityFE(), |
| 156 |
volFraction); |
| 157 |
else |
| 158 |
qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(pde->getDensityFE(), |
| 159 |
volFraction); |
| 160 |
} |
| 161 |
else { |
| 162 |
if (useFilter && !usePhaseField) |
| 163 |
qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(ROL::dynamicPtrCast<PDE_Filter<RealT>>(pdeFilter)->getDensityFE()); |
| 164 |
else |
| 165 |
qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(pde->getDensityFE()); |
| 166 |
} |
| 167 |
ROL::Ptr<TopOptVolumeObjective<RealT>> |
| 168 |
obj_vol = ROL::makePtr<TopOptVolumeObjective<RealT>>(qoi_vol,assemblerFilter,zp); |
| 169 |
ROL::Ptr<TopOptVolumeConstraint<RealT>> |
| 170 |
con_vol = ROL::makePtr<TopOptVolumeConstraint<RealT>>(qoi_vol,assemblerFilter,zp); |
| 171 |
|
| 172 |
// Compute volume |
| 173 |
zp->setScalar(1.0); |
| 174 |
RealT vol = obj_vol->value(*zp,tol); |
| 175 |
|
| 176 |
// Normalize compliance objective function |
| 177 |
RealT cs(1); |
| 178 |
zp->setScalar(initDens); |
| 179 |
if (normalizeObj) { |
| 180 |
if (useFilter && !usePhaseField) |
| 181 |
cs = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->normalize(*zp,tol); |
| 182 |
else |
| 183 |
cs = ROL::dynamicPtrCast<TopOptComplianceObjective<RealT>>(robj_com)->normalize(*zp,tol); |
| 184 |
} |
| 185 |
|
| 186 |
// Output problem details |
| 187 |
*outStream << std::endl; |
| 188 |
*outStream << "Problem Data" << std::endl; |
| 189 |
*outStream << " Example: " << example << std::endl; |
| 190 |
*outStream << " Dimension: " << probDim << std::endl; |
| 191 |
*outStream << " Minimize Type: " << minType << std::endl; |
| 192 |
*outStream << " Use Phase Field: " << usePhaseField << std::endl; |
| 193 |
if (!usePhaseField) { |
| 194 |
*outStream << " SIMP Power: " |
| 195 |
<< parlist->sublist("Problem").get("SIMP Power",3.0) << std::endl; |
| 196 |
*outStream << " Use Filter: " << useFilter << std::endl; |
| 197 |
} |
| 198 |
if (minType == "Volume") { |
| 199 |
*outStream << " Compliance Factor: " << cmpFactor << std::endl; |
| 200 |
} |
| 201 |
else if (minType == "Compliance") { |
| 202 |
*outStream << " Volume Fraction: " << volFraction << std::endl; |
| 203 |
*outStream << " Initial Density: " << initDens << std::endl; |
| 204 |
*outStream << " Use Equality: " << volEq << std::endl; |
| 205 |
*outStream << " Hessian Approx: " << hessAppr << std::endl; |
| 206 |
} |
| 207 |
*outStream << " Domain Volume: " << vol << std::endl; |
| 208 |
*outStream << " Compliance Scale: " << cs << std::endl; |
| 209 |
*outStream << std::endl; |
| 210 |
|
| 211 |
// Create objective, constraint, multiplier and bounds |
| 212 |
ROL::Ptr<ROL::Objective<RealT>> obj; |
| 213 |
ROL::Ptr<ROL::Constraint<RealT>> icon; |
| 214 |
ROL::Ptr<ROL::Vector<RealT>> iup, ilp; |
| 215 |
ROL::Ptr<ROL::Vector<RealT>> imul; |
| 216 |
ROL::Ptr<ROL::BoundConstraint<RealT>> ibnd; |
| 217 |
if (!usePhaseField) { |
| 218 |
if (minType == "Volume") { |
| 219 |
obj = obj_vol; |
| 220 |
icon = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>(robj_com); |
| 221 |
// Set upper bound to compliance for solid beam. |
| 222 |
zp->setScalar(1.0); |
| 223 |
RealT comp = robj_com->value(*zp,tol); |
| 224 |
ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 225 |
iup = ROL::makePtr<ROL::SingletonVector<RealT>>(cmpFactor*comp); |
| 226 |
imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 227 |
ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup); |
| 228 |
} |
| 229 |
else if (minType == "Compliance") { |
| 230 |
obj = robj_com; |
| 231 |
icon = con_vol; |
| 232 |
imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 233 |
if (volEq) { |
| 234 |
ilp = ROL::nullPtr; |
| 235 |
iup = ROL::nullPtr; |
| 236 |
ibnd = ROL::nullPtr; |
| 237 |
} |
| 238 |
else { |
| 239 |
// Set upper bound to fraction of total volume. |
| 240 |
ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 241 |
iup = ROL::makePtr<ROL::SingletonVector<RealT>>(volFraction*vol); |
| 242 |
ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup); |
| 243 |
} |
| 244 |
} |
| 245 |
else if (minType == "Total") { |
| 246 |
std::vector<ROL::Ptr<ROL::Objective<RealT>>> obj_vec(2,ROL::nullPtr); |
| 247 |
obj_vec[0] = robj_com; |
| 248 |
obj_vec[1] = obj_vol; |
| 249 |
std::vector<RealT> weights(2,0.0); |
| 250 |
weights[0] = static_cast<RealT>(1); |
| 251 |
weights[1] = parlist->sublist("Problem").get("Volume Objective Scale",0.04096); |
| 252 |
obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec); |
| 253 |
icon = ROL::nullPtr; |
| 254 |
ilp = ROL::nullPtr; |
| 255 |
iup = ROL::nullPtr; |
| 256 |
imul = ROL::nullPtr; |
| 257 |
ibnd = ROL::nullPtr; |
| 258 |
} |
| 259 |
else { |
| 260 |
throw ROL::Exception::NotImplemented("Unknown minimization type!"); |
| 261 |
} |
| 262 |
} |
| 263 |
else { |
| 264 |
// Build Modica-Mortola Energy objective function. |
| 265 |
RealT penParam = parlist->sublist("Problem").get("Phase Field Penalty Parameter",1e-1); |
| 266 |
ROL::Ptr<QoI<RealT>> |
| 267 |
qoi_pfe = ROL::makePtr<QoI_ModicaMortolaEnergy_PhaseField<RealT>>(pde->getDensityFE(), |
| 268 |
penParam); |
| 269 |
ROL::Ptr<IntegralOptObjective<RealT>> |
| 270 |
obj_pfe = ROL::makePtr<IntegralOptObjective<RealT>>(qoi_pfe,assembler); |
| 271 |
// Get weights for linear combination objective. |
| 272 |
std::vector<RealT> weights(1,0.0); |
| 273 |
weights[0] = parlist->sublist("Problem").get("Phase Field Energy Objective Scale",0.00064); |
| 274 |
std::vector<ROL::Ptr<ROL::Objective<RealT>>> obj_vec(1,ROL::nullPtr); |
| 275 |
obj_vec[0] = obj_pfe; |
| 276 |
if (minType == "Volume") { |
| 277 |
weights.push_back(parlist->sublist("Problem").get("Volume Objective Scale",0.04096)); |
| 278 |
obj_vec.push_back(obj_vol); |
| 279 |
obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec); |
| 280 |
icon = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>(robj_com); |
| 281 |
// Set upper bound to compliance for solid beam. |
| 282 |
RealT comp = robj_com->value(*zp,tol); |
| 283 |
ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 284 |
iup = ROL::makePtr<ROL::SingletonVector<RealT>>(cmpFactor*comp); |
| 285 |
imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 286 |
ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup); |
| 287 |
} |
| 288 |
else if (minType == "Compliance") { |
| 289 |
weights.push_back(static_cast<RealT>(1)); |
| 290 |
obj_vec.push_back(robj_com); |
| 291 |
obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec); |
| 292 |
icon = con_vol; |
| 293 |
imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 294 |
if (volEq) { |
| 295 |
ilp = ROL::nullPtr; |
| 296 |
iup = ROL::nullPtr; |
| 297 |
ibnd = ROL::nullPtr; |
| 298 |
} |
| 299 |
else { |
| 300 |
// Set upper bound to fraction of total volume. |
| 301 |
ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 302 |
iup = ROL::makePtr<ROL::SingletonVector<RealT>>(volFraction*vol); |
| 303 |
ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup); |
| 304 |
} |
| 305 |
} |
| 306 |
else if (minType == "Total") { |
| 307 |
weights.push_back(static_cast<RealT>(1)); |
| 308 |
obj_vec.push_back(robj_com); |
| 309 |
weights.push_back(parlist->sublist("Problem").get("Volume Objective Scale",0.04096)); |
| 310 |
obj_vec.push_back(obj_vol); |
| 311 |
obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec); |
| 312 |
icon = ROL::nullPtr; |
| 313 |
ilp = ROL::nullPtr; |
| 314 |
iup = ROL::nullPtr; |
| 315 |
imul = ROL::nullPtr; |
| 316 |
ibnd = ROL::nullPtr; |
| 317 |
} |
| 318 |
else { |
| 319 |
throw ROL::Exception::NotImplemented("Unknown minimization type!"); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Initialize bound constraints. |
| 324 |
RealT lval = (usePhaseField ? -1.0 : 0.0), uval = 1.0; |
| 325 |
ROL::Ptr<ROL::Vector<RealT>> lop = zp->clone(); lop->setScalar(lval); |
| 326 |
ROL::Ptr<ROL::Vector<RealT>> hip = zp->clone(); hip->setScalar(uval); |
| 327 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 328 |
bnd = ROL::makePtr<ROL::Bounds<RealT>>(lop,hip); |
| 329 |
if (usePhaseField) bnd->deactivate(); |
| 330 |
|
| 331 |
// Build sampler. |
| 332 |
BuildSampler<RealT> buildSampler(parlist->sublist("Problem"),probDim,example); |
| 333 |
//int stochDim = buildSampler.getDimension(); |
| 334 |
int nsamp = parlist->sublist("Problem").get("Number of samples", 1); |
| 335 |
int nsamp_dist = parlist->sublist("Problem").get("Number of output samples",1000); |
| 336 |
ROL::Ptr<ROL::BatchManager<RealT>> |
| 337 |
bman = ROL::makePtr<ROL::TpetraTeuchosBatchManager<RealT>>(comm); |
| 338 |
ROL::Ptr<ROL::SampleGenerator<RealT>> |
| 339 |
sampler = buildSampler.get(nsamp,bman); |
| 340 |
ROL::Ptr<ROL::SampleGenerator<RealT>> |
| 341 |
sampler_dist = buildSampler.get(nsamp_dist,bman); |
| 342 |
|
| 343 |
// Set up and solve. |
| 344 |
int cnt = 0; |
| 345 |
std::vector<RealT> volume, quantile; |
| 346 |
bool derivCheck = parlist->sublist("Problem").get("Check derivatives",false); |
| 347 |
parlist->sublist("SOL").set("Type","Risk Averse"); |
| 348 |
parlist->sublist("SOL").sublist("Risk Measure").set("Name","CVaR"); |
| 349 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Smoothing Parameter",1e-4); |
| 350 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").set("Name","Parabolic"); |
| 351 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Lower Bound",0.0); |
| 352 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Upper Bound",1.0); |
| 353 |
Teuchos::Array<RealT> lambda |
| 354 |
= Teuchos::getArrayFromStringParameter<RealT>(parlist->sublist("Problem"), "Convex Combination Parameters"); |
| 355 |
Teuchos::Array<RealT> beta |
| 356 |
= Teuchos::getArrayFromStringParameter<RealT>(parlist->sublist("Problem"), "Confidence Levels"); |
| 357 |
ROL::Ptr<ROL::StochasticProblem<RealT>> prob; |
| 358 |
for (int i = 0; i < lambda.size(); ++i) { |
| 359 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Convex Combination Parameter",lambda[i]); |
| 360 |
for (int j = 0; j < beta.size(); ++j) { |
| 361 |
if (cnt!=0) parlist->sublist("SOL").set("Initial Statistic",quantile[cnt]); |
| 362 |
*outStream << std::endl << " lambda = " << lambda[i] |
| 363 |
<< " beta = " << beta[j] << std::endl << std::endl; |
| 364 |
parlist->sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Confidence Level",beta[j]); |
| 365 |
// Set up optimization problem |
| 366 |
prob = ROL::makePtr<ROL::StochasticProblem<RealT>>(obj,zp); |
| 367 |
if (bnd->isActivated()) prob->addBoundConstraint(bnd); |
| 368 |
if ( minType == "Compliance" ) { |
| 369 |
if ( volEq ) prob->addLinearConstraint("Volume",icon,imul); |
| 370 |
else prob->addLinearConstraint("Volume",icon,imul,ibnd); |
| 371 |
} |
| 372 |
else if ( minType == "Volume" ) |
| 373 |
prob->addConstraint("Compliance",icon,imul,ibnd); |
| 374 |
if ( minType == "Volume" ) { |
| 375 |
ROL::Ptr<ROL::BatchManager<RealT>> |
| 376 |
cbman = ROL::makePtr<ROL::SingletonTeuchosBatchManager<RealT,int>>(comm); |
| 377 |
parlist->sublist("SOL").sublist("Compliance") = parlist->sublist("SOL"); |
| 378 |
prob->makeConstraintStochastic("Compliance",*parlist,sampler,cbman); |
| 379 |
} |
| 380 |
else { |
| 381 |
parlist->sublist("SOL").sublist("Objective") = parlist->sublist("SOL"); |
| 382 |
prob->makeObjectiveStochastic(*parlist,sampler); |
| 383 |
} |
| 384 |
prob->setProjectionAlgorithm(*parlist); |
| 385 |
prob->finalize(false,true,*outStream); |
| 386 |
|
| 387 |
// Check derivatives. |
| 388 |
if (derivCheck) prob->check(true,*outStream); |
| 389 |
|
| 390 |
// Solve optimization problem |
| 391 |
Teuchos::Time algoTimer("Algorithm Time", true); |
| 392 |
ROL::Solver<RealT> solver(prob,*parlist); |
| 393 |
solver.solve(*outStream); |
| 394 |
algoTimer.stop(); |
| 395 |
*outStream << "Total optimization time = " << algoTimer.totalElapsedTime() << " seconds.\n"; |
| 396 |
|
| 397 |
// Output. |
| 398 |
volume.push_back(obj_vol->value(*zp,tol)); |
| 399 |
quantile.push_back(prob->getSolutionStatistic((minType=="Volume") ? 1 : 0)); |
| 400 |
std::stringstream nameDens; |
| 401 |
nameDens << "density_CVaR_lambda" << lambda[i] << "_beta" << beta[j] << ".txt"; |
| 402 |
assembler->outputTpetraVector(z_ptr,nameDens.str().c_str()); |
| 403 |
std::stringstream nameObj; |
| 404 |
nameObj << "obj_samples_CVaR_lambda" << lambda[i] << "_beta" << beta[j] << ".txt"; |
| 405 |
if (minType != "Volume") |
| 406 |
TopOptPrintCDF<RealT>(*obj,*zp,*sampler_dist,nsamp_dist,comm,nameObj.str()); |
| 407 |
else |
| 408 |
TopOptPrintCDF<RealT>(*robj_com,*zp,*sampler_dist,nsamp_dist,comm,nameObj.str()); |
| 409 |
cnt++; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
// Output. |
| 414 |
assembler->printMeshData(*outStream); |
| 415 |
const int rank = Teuchos::rank<int>(*comm); |
| 416 |
if ( rank==0 ) { |
| 417 |
std::stringstream nameVOL, nameVAR; |
| 418 |
nameVOL << "vol.txt"; |
| 419 |
nameVAR << "var.txt"; |
| 420 |
std::ofstream fileVOL, fileVAR; |
| 421 |
fileVOL.open(nameVOL.str()); |
| 422 |
fileVAR.open(nameVAR.str()); |
| 423 |
fileVOL << std::scientific << std::setprecision(15); |
| 424 |
fileVAR << std::scientific << std::setprecision(15); |
| 425 |
int size = quantile.size(); |
| 426 |
for (int i = 0; i < size; ++i) { |
| 427 |
fileVOL << std::setw(25) << std::left << volume[i] << std::endl; |
| 428 |
fileVAR << std::setw(25) << std::left << quantile[i] << std::endl; |
| 429 |
} |
| 430 |
fileVOL.close(); |
| 431 |
fileVAR.close(); |
| 432 |
} |
| 433 |
|
| 434 |
// Get a summary from the time monitor. |
| 435 |
Teuchos::TimeMonitor::summarize(); |
| 436 |
} |
| 437 |
catch (std::logic_error& err) { |
| 438 |
*outStream << err.what() << "\n"; |
| 439 |
errorFlag = -1000; |
| 440 |
}; // end try |
| 441 |
|
| 442 |
if (errorFlag != 0) |
| 443 |
std::cout << "End Result: TEST FAILED\n"; |
| 444 |
else |
| 445 |
std::cout << "End Result: TEST PASSED\n"; |
| 446 |
|
| 447 |
return 0; |
| 448 |
} |