| 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 stuctural topology optimization problem. |
| 12 |
|
| 13 |
*/ |
| 14 |
|
| 15 |
#include "Teuchos_Comm.hpp" |
| 16 |
#include "Teuchos_Time.hpp" |
| 17 |
#include "ROL_Stream.hpp" |
| 18 |
#include "Teuchos_GlobalMPISession.hpp" |
| 19 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 20 |
|
| 21 |
#include "Tpetra_Core.hpp" |
| 22 |
#include "Tpetra_Version.hpp" |
| 23 |
|
| 24 |
#include <iostream> |
| 25 |
#include <algorithm> |
| 26 |
|
| 27 |
#include "ROL_Solver.hpp" |
| 28 |
#include "ROL_Bounds.hpp" |
| 29 |
#include "ROL_ConstraintFromObjective.hpp" |
| 30 |
#include "ROL_LinearCombinationObjective.hpp" |
| 31 |
|
| 32 |
#include "../../../TOOLS/pdevector.hpp" |
| 33 |
#include "../../../TOOLS/pdeobjective.hpp" |
| 34 |
#include "../../../TOOLS/meshreader.hpp" |
| 35 |
|
| 36 |
#include "src/pde_elasticity.hpp" |
| 37 |
#include "src/pde_filter.hpp" |
| 38 |
#include "src/filtered_compliance_robj.hpp" |
| 39 |
#include "src/volume_con.hpp" |
| 40 |
#include "src/obj_volume.hpp" |
| 41 |
|
| 42 |
typedef double RealT; |
| 43 |
|
| 44 |
int main(int argc, char *argv[]) { |
| 45 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 46 |
int iprint = argc - 1; |
| 47 |
ROL::Ptr<std::ostream> outStream; |
| 48 |
ROL::nullstream bhs; // outputs nothing |
| 49 |
|
| 50 |
/*** Initialize communicator. ***/ |
| 51 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 52 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 53 |
= Tpetra::getDefaultComm(); |
| 54 |
const int myRank = comm->getRank(); |
| 55 |
if ((iprint > 0) && (myRank == 0)) { |
| 56 |
outStream = ROL::makePtrFromRef(std::cout); |
| 57 |
} |
| 58 |
else { |
| 59 |
outStream = ROL::makePtrFromRef(bhs); |
| 60 |
} |
| 61 |
int errorFlag = 0; |
| 62 |
|
| 63 |
// *** Example body. |
| 64 |
try { |
| 65 |
RealT tol(1e-8); |
| 66 |
|
| 67 |
/*** Read in XML input ***/ |
| 68 |
std::string filename = "input_ex01.xml"; |
| 69 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 70 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 71 |
|
| 72 |
// Retrieve parameters. |
| 73 |
int probDim = parlist->sublist("Problem").get("Problem Dimension", 2); |
| 74 |
const RealT volFraction = parlist->sublist("Problem").get("Volume Fraction", 0.4); |
| 75 |
const RealT initDens = volFraction; |
| 76 |
const std::string minType = "Compliance"; |
| 77 |
const std::string hessAppr = "None"; |
| 78 |
if (probDim==3) { |
| 79 |
parlist->sublist("Mesh").set("File Name","meshfiles/truss3d.txt"); |
| 80 |
} |
| 81 |
else { |
| 82 |
probDim = 2; |
| 83 |
parlist->sublist("Problem").set("Problem Dimension",probDim); |
| 84 |
parlist->sublist("Mesh").set("File Name","meshfiles/truss2d.txt"); |
| 85 |
} |
| 86 |
parlist->sublist("Problem").set("Use Inexact Linear Solves",false); |
| 87 |
parlist->sublist("General").set("Inexact Objective Function",false); |
| 88 |
parlist->sublist("General").set("Inexact Gradient",false); |
| 89 |
|
| 90 |
/*** Initialize main data structure. ***/ |
| 91 |
int nProcs = comm->getSize(); |
| 92 |
if (nProcs == 1) nProcs = 0; |
| 93 |
ROL::Ptr<MeshManager<RealT>> |
| 94 |
meshMgr = ROL::makePtr<MeshReader<RealT>>(*parlist,nProcs); |
| 95 |
// Initialize PDE describing elasticity equations. |
| 96 |
ROL::Ptr<PDE_Elasticity<RealT>> |
| 97 |
pde = ROL::makePtr<PDE_Elasticity<RealT>>(*parlist); |
| 98 |
|
| 99 |
// Initialize the filter PDE. |
| 100 |
ROL::Ptr<PDE_Filter<RealT>> |
| 101 |
pdeFilter = ROL::makePtr<PDE_Filter<RealT>>(*parlist); |
| 102 |
pde->setDensityFields(pdeFilter->getFields()); |
| 103 |
|
| 104 |
// Initialize reduced compliance objective function. |
| 105 |
ROL::Ptr<TopOptFilteredComplianceObjective<RealT>> |
| 106 |
robj_com = ROL::makePtr<TopOptFilteredComplianceObjective<RealT>>( |
| 107 |
pde,pdeFilter,meshMgr,comm,*parlist,*outStream); |
| 108 |
ROL::Ptr<Assembler<RealT>> assembler, assemblerFilter; |
| 109 |
assembler = robj_com->getAssembler(); |
| 110 |
assemblerFilter = robj_com->getFilterAssembler(); |
| 111 |
|
| 112 |
// Create vectors. |
| 113 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr, p_ptr, r_ptr, z_ptr; |
| 114 |
u_ptr = assembler->createStateVector(); u_ptr->putScalar(0.0); |
| 115 |
p_ptr = assembler->createStateVector(); p_ptr->putScalar(0.0); |
| 116 |
r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0); |
| 117 |
z_ptr = assemblerFilter->createControlVector(); z_ptr->putScalar(1.0); |
| 118 |
ROL::Ptr<ROL::Vector<RealT>> up, pp, rp, zp, imul; |
| 119 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 120 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 121 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 122 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pdeFilter,assemblerFilter,*parlist); |
| 123 |
imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0); |
| 124 |
|
| 125 |
// Build volume objective function. |
| 126 |
ROL::Ptr<QoI<RealT>> |
| 127 |
qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(pdeFilter->getDensityFE(), |
| 128 |
volFraction); |
| 129 |
ROL::Ptr<TopOptVolumeConstraint<RealT>> |
| 130 |
con_vol = ROL::makePtr<TopOptVolumeConstraint<RealT>>(qoi_vol,assemblerFilter,zp); |
| 131 |
|
| 132 |
// Normalize compliance objective function |
| 133 |
zp->setScalar(initDens); |
| 134 |
RealT cs = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->normalize(*zp,tol); |
| 135 |
|
| 136 |
// Output problem details |
| 137 |
*outStream << std::endl; |
| 138 |
*outStream << "Problem Data" << std::endl; |
| 139 |
*outStream << " Dimension: " << probDim << std::endl; |
| 140 |
*outStream << " SIMP Power: " |
| 141 |
<< parlist->sublist("Problem").get("SIMP Power",3.0) << std::endl; |
| 142 |
*outStream << " Volume Fraction: " << volFraction << std::endl; |
| 143 |
*outStream << " Initial Density: " << initDens << std::endl; |
| 144 |
*outStream << " Compliance Scale: " << cs << std::endl; |
| 145 |
*outStream << std::endl; |
| 146 |
|
| 147 |
// Create objective, constraint, multiplier and bounds |
| 148 |
|
| 149 |
// Initialize bound constraints. |
| 150 |
RealT lval = 0.0, uval = 1.0; |
| 151 |
ROL::Ptr<ROL::Vector<RealT>> lop = zp->clone(); lop->setScalar(lval); |
| 152 |
ROL::Ptr<ROL::Vector<RealT>> hip = zp->clone(); hip->setScalar(uval); |
| 153 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 154 |
bnd = ROL::makePtr<ROL::Bounds<RealT>>(lop,hip); |
| 155 |
|
| 156 |
// Set up optimization problem. |
| 157 |
ROL::Ptr<ROL::Problem<RealT>> |
| 158 |
prob = ROL::makePtr<ROL::Problem<RealT>>(robj_com,zp); |
| 159 |
prob->addBoundConstraint(bnd); |
| 160 |
prob->addLinearConstraint("Volume",con_vol,imul); |
| 161 |
prob->setProjectionAlgorithm(*parlist); |
| 162 |
|
| 163 |
// Check derivatives. |
| 164 |
bool derivCheck = parlist->sublist("Problem").get("Check derivatives",false); |
| 165 |
if (derivCheck) prob->check(true,*outStream); |
| 166 |
|
| 167 |
// Solve optimization problem. |
| 168 |
ROL::Ptr<ROL::Solver<RealT>> solver; |
| 169 |
std::vector<RealT> vals(6); |
| 170 |
|
| 171 |
// SPG |
| 172 |
zp->setScalar(initDens); |
| 173 |
imul->zero(); |
| 174 |
robj_com->reset(); |
| 175 |
con_vol->reset(); |
| 176 |
prob->finalize(false,true,*outStream); |
| 177 |
parlist->sublist("Step").set("Type","Spectral Gradient"); |
| 178 |
Teuchos::Time spgTimer("SPG Time", true); |
| 179 |
solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist); |
| 180 |
solver->solve(*outStream); |
| 181 |
spgTimer.stop(); |
| 182 |
*outStream << "Total optimization time = " << spgTimer.totalElapsedTime() << " seconds.\n"; |
| 183 |
// Output. |
| 184 |
robj_com->summarize(*outStream); |
| 185 |
con_vol->summarize(*outStream); |
| 186 |
vals[0] = solver->getAlgorithmState()->value; |
| 187 |
|
| 188 |
// PQN |
| 189 |
zp->setScalar(initDens); |
| 190 |
imul->zero(); |
| 191 |
robj_com->reset(); |
| 192 |
con_vol->reset(); |
| 193 |
parlist->sublist("Step").set("Type","Line Search"); |
| 194 |
parlist->sublist("Step").sublist("Line Search").sublist("Descent Method").set("Type","Quasi-Newton Method"); |
| 195 |
Teuchos::Time pqnTimer("PQN Time", true); |
| 196 |
solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist); |
| 197 |
solver->solve(*outStream); |
| 198 |
pqnTimer.stop(); |
| 199 |
*outStream << "Total optimization time = " << pqnTimer.totalElapsedTime() << " seconds.\n"; |
| 200 |
// Output. |
| 201 |
robj_com->summarize(*outStream); |
| 202 |
con_vol->summarize(*outStream); |
| 203 |
vals[1] = solver->getAlgorithmState()->value; |
| 204 |
|
| 205 |
// LMTR |
| 206 |
zp->setScalar(initDens); |
| 207 |
imul->zero(); |
| 208 |
robj_com->reset(); |
| 209 |
con_vol->reset(); |
| 210 |
parlist->sublist("Step").set("Type","Trust Region"); |
| 211 |
parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","Lin-More"); |
| 212 |
Teuchos::Time lmtrTimer("LMTR Time", true); |
| 213 |
solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist); |
| 214 |
solver->solve(*outStream); |
| 215 |
lmtrTimer.stop(); |
| 216 |
*outStream << "Total optimization time = " << lmtrTimer.totalElapsedTime() << " seconds.\n"; |
| 217 |
// Output. |
| 218 |
robj_com->summarize(*outStream); |
| 219 |
con_vol->summarize(*outStream); |
| 220 |
vals[2] = solver->getAlgorithmState()->value; |
| 221 |
|
| 222 |
// TRSPG |
| 223 |
zp->setScalar(initDens); |
| 224 |
imul->zero(); |
| 225 |
robj_com->reset(); |
| 226 |
con_vol->reset(); |
| 227 |
parlist->sublist("Step").set("Type","Trust Region"); |
| 228 |
parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","SPG"); |
| 229 |
Teuchos::Time trspgTimer("TRSPG Time", true); |
| 230 |
solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist); |
| 231 |
solver->solve(*outStream); |
| 232 |
trspgTimer.stop(); |
| 233 |
*outStream << "Total optimization time = " << trspgTimer.totalElapsedTime() << " seconds.\n"; |
| 234 |
// Output. |
| 235 |
robj_com->summarize(*outStream); |
| 236 |
con_vol->summarize(*outStream); |
| 237 |
vals[3] = solver->getAlgorithmState()->value; |
| 238 |
|
| 239 |
// AL-LMTR |
| 240 |
zp->setScalar(initDens); |
| 241 |
imul->zero(); |
| 242 |
robj_com->reset(); |
| 243 |
con_vol->reset(); |
| 244 |
prob->edit(); |
| 245 |
prob->finalize(true,true,*outStream); |
| 246 |
parlist->sublist("Step").set("Type","Augmented Lagrangian"); |
| 247 |
parlist->sublist("Step").sublist("Augmented Lagrangian").set("Subproblem Step Type","Trust Region"); |
| 248 |
parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","Lin-More"); |
| 249 |
Teuchos::Time allmtrTimer("AL-LMTR Time", true); |
| 250 |
solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist); |
| 251 |
solver->solve(*outStream); |
| 252 |
allmtrTimer.stop(); |
| 253 |
*outStream << "Total optimization time = " << allmtrTimer.totalElapsedTime() << " seconds.\n"; |
| 254 |
// Output. |
| 255 |
robj_com->summarize(*outStream); |
| 256 |
con_vol->summarize(*outStream); |
| 257 |
vals[4] = solver->getAlgorithmState()->value; |
| 258 |
|
| 259 |
// AL-LMTR |
| 260 |
zp->setScalar(initDens); |
| 261 |
imul->zero(); |
| 262 |
robj_com->reset(); |
| 263 |
con_vol->reset(); |
| 264 |
parlist->sublist("Step").set("Type","Augmented Lagrangian"); |
| 265 |
parlist->sublist("Step").sublist("Augmented Lagrangian").set("Subproblem Step Type","Trust Region"); |
| 266 |
parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model","SPG"); |
| 267 |
Teuchos::Time altrspgTimer("AL-TRSPG Time", true); |
| 268 |
solver = ROL::makePtr<ROL::Solver<RealT>>(prob,*parlist); |
| 269 |
solver->solve(*outStream); |
| 270 |
altrspgTimer.stop(); |
| 271 |
*outStream << "Total optimization time = " << altrspgTimer.totalElapsedTime() << " seconds.\n"; |
| 272 |
// Output. |
| 273 |
robj_com->summarize(*outStream); |
| 274 |
con_vol->summarize(*outStream); |
| 275 |
vals[5] = solver->getAlgorithmState()->value; |
| 276 |
|
| 277 |
RealT minval = std::min(vals[0],std::min(vals[1],std::min(vals[2],vals[3]))); |
| 278 |
*outStream << std::scientific << std::setprecision(16); |
| 279 |
*outStream << "SPG " << (vals[0]-minval)/minval << std::endl; |
| 280 |
*outStream << "PQN " << (vals[1]-minval)/minval << std::endl; |
| 281 |
*outStream << "LMTR " << (vals[2]-minval)/minval << std::endl; |
| 282 |
*outStream << "TRSPG " << (vals[3]-minval)/minval << std::endl; |
| 283 |
*outStream << "AL-LMTR " << (vals[4]-minval)/minval << std::endl; |
| 284 |
*outStream << "AL-TRSPG " << (vals[5]-minval)/minval << std::endl; |
| 285 |
*outStream << std::endl; |
| 286 |
|
| 287 |
robj_com->printToFile(*zp,*outStream); |
| 288 |
|
| 289 |
// Get a summary from the time monitor. |
| 290 |
Teuchos::TimeMonitor::summarize(); |
| 291 |
} |
| 292 |
catch (std::logic_error& err) { |
| 293 |
*outStream << err.what() << "\n"; |
| 294 |
errorFlag = -1000; |
| 295 |
}; // end try |
| 296 |
|
| 297 |
if (errorFlag != 0) |
| 298 |
std::cout << "End Result: TEST FAILED\n"; |
| 299 |
else |
| 300 |
std::cout << "End Result: TEST PASSED\n"; |
| 301 |
|
| 302 |
return 0; |
| 303 |
} |