| 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 mother problem of PDE-constrained optimization: |
| 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 "ROL_Algorithm.hpp" |
| 23 |
#include "ROL_AugmentedLagrangianStep.hpp" |
| 24 |
//#include "ROL_MoreauYosidaPenaltyStep.hpp" |
| 25 |
#include "ROL_TrustRegionStep.hpp" |
| 26 |
#include "ROL_CompositeStep.hpp" |
| 27 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 28 |
#include "ROL_ScaledTpetraMultiVector.hpp" |
| 29 |
|
| 30 |
#include "ROL_ScaledStdVector.hpp" |
| 31 |
|
| 32 |
#include "ROL_Bounds.hpp" |
| 33 |
#include "ROL_AugmentedLagrangian.hpp" |
| 34 |
#include "ROL_Algorithm.hpp" |
| 35 |
|
| 36 |
#include <iostream> |
| 37 |
#include <algorithm> |
| 38 |
|
| 39 |
#include "data.hpp" |
| 40 |
#include "filter.hpp" |
| 41 |
#include "objective.hpp" |
| 42 |
#include "constraint.hpp" |
| 43 |
#include "volume_constraint.hpp" |
| 44 |
|
| 45 |
//#include <fenv.h> |
| 46 |
|
| 47 |
typedef double RealT; |
| 48 |
|
| 49 |
ROL::Ptr<Tpetra::MultiVector<> > createTpetraVector(const ROL::Ptr<const Tpetra::Map<> > &map) { |
| 50 |
return ROL::makePtr<Tpetra::MultiVector<>>(map, 1, true); |
| 51 |
} |
| 52 |
|
| 53 |
int main(int argc, char *argv[]) { |
| 54 |
//feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT); |
| 55 |
|
| 56 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 57 |
int iprint = argc - 1; |
| 58 |
ROL::Ptr<std::ostream> outStream; |
| 59 |
ROL::nullstream bhs; // outputs nothing |
| 60 |
|
| 61 |
/*** Initialize communicator. ***/ |
| 62 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 63 |
ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 64 |
const int myRank = comm->getRank(); |
| 65 |
if ((iprint > 0) && (myRank == 0)) { |
| 66 |
outStream = ROL::makePtrFromRef(std::cout); |
| 67 |
} |
| 68 |
else { |
| 69 |
outStream = ROL::makePtrFromRef(bhs); |
| 70 |
} |
| 71 |
|
| 72 |
int errorFlag = 0; |
| 73 |
|
| 74 |
// *** Example body. |
| 75 |
try { |
| 76 |
|
| 77 |
/*** Read in XML input ***/ |
| 78 |
std::string filename = "input.xml"; |
| 79 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 80 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 81 |
|
| 82 |
/*** Initialize main data structure. ***/ |
| 83 |
ROL::Ptr<ElasticitySIMPOperators<RealT> > data |
| 84 |
= ROL::makePtr<ElasticitySIMPOperators<RealT>>(comm, parlist, outStream); |
| 85 |
/*** Initialize density filter. ***/ |
| 86 |
ROL::Ptr<DensityFilter<RealT> > filter |
| 87 |
= ROL::makePtr<DensityFilter<RealT>>(comm, parlist, outStream); |
| 88 |
/*** Build vectors and dress them up as ROL vectors. ***/ |
| 89 |
ROL::Ptr<const Tpetra::Map<> > vecmap_u = data->getDomainMapA(); |
| 90 |
ROL::Ptr<const Tpetra::Map<> > vecmap_z = data->getCellMap(); |
| 91 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = createTpetraVector(vecmap_u); |
| 92 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = createTpetraVector(vecmap_z); |
| 93 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = createTpetraVector(vecmap_u); |
| 94 |
ROL::Ptr<Tpetra::MultiVector<> > dw_ptr = createTpetraVector(vecmap_u); |
| 95 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = createTpetraVector(vecmap_z); |
| 96 |
ROL::Ptr<Tpetra::MultiVector<> > dz2_ptr = createTpetraVector(vecmap_z); |
| 97 |
ROL::Ptr<std::vector<RealT> > vc_ptr = ROL::makePtr<std::vector<RealT>>(1, 0); |
| 98 |
ROL::Ptr<std::vector<RealT> > vc_lam_ptr = ROL::makePtr<std::vector<RealT>>(1, 0); |
| 99 |
ROL::Ptr<std::vector<RealT> > vscale_ptr = ROL::makePtr<std::vector<RealT>>(1, 0); |
| 100 |
// Set all values to 1 in u, z. |
| 101 |
u_ptr->putScalar(1.0); |
| 102 |
// Set z to gray solution. |
| 103 |
RealT volFrac = parlist->sublist("ElasticityTopoOpt").get<RealT>("Volume Fraction"); |
| 104 |
z_ptr->putScalar(volFrac); |
| 105 |
// Set scaling vector for constraint |
| 106 |
RealT W = parlist->sublist("Geometry").get<RealT>("Width"); |
| 107 |
RealT H = parlist->sublist("Geometry").get<RealT>("Height"); |
| 108 |
RealT one(1), two(2); |
| 109 |
(*vscale_ptr)[0] = one/std::pow(W*H*(one-volFrac),two); |
| 110 |
// Set Scaling vector for density |
| 111 |
bool useZscale = parlist->sublist("Problem").get<bool>("Use Scaled Density Vectors"); |
| 112 |
RealT densityScaling = parlist->sublist("Problem").get<RealT>("Density Scaling"); |
| 113 |
ROL::Ptr<Tpetra::MultiVector<> > scaleVec = createTpetraVector(vecmap_z); |
| 114 |
scaleVec->putScalar(densityScaling); |
| 115 |
if ( !useZscale ) { |
| 116 |
scaleVec->putScalar(one); |
| 117 |
} |
| 118 |
ROL::Ptr<const Tpetra::Vector<> > zscale_ptr = scaleVec->getVector(0); |
| 119 |
|
| 120 |
//test |
| 121 |
/*data->updateMaterialDensity (z_ptr); |
| 122 |
ROL::Ptr<Tpetra::MultiVector<RealT> > rhs |
| 123 |
= ROL::makePtr<Tpetra::MultiVector<>> (data->getVecF()->getMap(), 1, true); |
| 124 |
data->ApplyMatAToVec(rhs, u_ptr); |
| 125 |
data->outputTpetraVector(rhs, "KU0.txt"); |
| 126 |
data->ApplyInverseJacobian1ToVec(u_ptr, rhs, false); |
| 127 |
data->outputTpetraVector(u_ptr, "KKU0.txt"); |
| 128 |
|
| 129 |
data->ApplyJacobian1ToVec(rhs, u_ptr); |
| 130 |
data->outputTpetraVector(rhs, "KU1.txt"); |
| 131 |
data->ApplyInverseJacobian1ToVec(u_ptr, rhs, false); |
| 132 |
data->outputTpetraVector(u_ptr, "KKU1.txt"); |
| 133 |
*/ |
| 134 |
//u_ptr->putScalar(1.0); |
| 135 |
//z_ptr->putScalar(1.0); |
| 136 |
// Randomize d vectors. |
| 137 |
du_ptr->randomize(); //du_ptr->scale(0); |
| 138 |
dw_ptr->randomize(); |
| 139 |
dz_ptr->randomize(); //dz_ptr->scale(0); |
| 140 |
dz2_ptr->randomize(); |
| 141 |
// Create ROL::TpetraMultiVectors. |
| 142 |
ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(u_ptr); |
| 143 |
ROL::Ptr<ROL::Vector<RealT> > dup = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(du_ptr); |
| 144 |
ROL::Ptr<ROL::Vector<RealT> > dwp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(dw_ptr); |
| 145 |
ROL::Ptr<ROL::Vector<RealT> > zp |
| 146 |
= ROL::makePtr<ROL::PrimalScaledTpetraMultiVector<RealT>>(z_ptr,zscale_ptr); |
| 147 |
ROL::Ptr<ROL::Vector<RealT> > dzp |
| 148 |
= ROL::makePtr<ROL::PrimalScaledTpetraMultiVector<RealT>>(dz_ptr,zscale_ptr); |
| 149 |
ROL::Ptr<ROL::Vector<RealT> > dz2p |
| 150 |
= ROL::makePtr<ROL::PrimalScaledTpetraMultiVector<RealT>>(dz2_ptr,zscale_ptr); |
| 151 |
ROL::Ptr<ROL::Vector<RealT> > vcp |
| 152 |
= ROL::makePtr<ROL::PrimalScaledStdVector<RealT>>(vc_ptr,vscale_ptr); |
| 153 |
ROL::Ptr<ROL::Vector<RealT> > vc_lamp |
| 154 |
= ROL::makePtr<ROL::DualScaledStdVector<RealT>>(vc_lam_ptr,vscale_ptr); |
| 155 |
// Create ROL SimOpt vectors. |
| 156 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 157 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 158 |
ROL::Vector_SimOpt<RealT> d2(dwp,dz2p); |
| 159 |
|
| 160 |
/*** Build objective function, constraint and reduced objective function. ***/ |
| 161 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj |
| 162 |
= ROL::makePtr<Objective_PDEOPT_ElasticitySIMP<RealT>>(data, filter, parlist); |
| 163 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con |
| 164 |
= ROL::makePtr<EqualityConstraint_PDEOPT_ElasticitySIMP<RealT>>(data, filter, parlist); |
| 165 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > objReduced |
| 166 |
= ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, dwp); |
| 167 |
ROL::Ptr<ROL::Constraint<RealT> > volcon |
| 168 |
= ROL::makePtr<EqualityConstraint_PDEOPT_ElasticitySIMP_Volume<RealT>>(data, parlist); |
| 169 |
|
| 170 |
/*** Build bound constraint ***/ |
| 171 |
ROL::Ptr<Tpetra::MultiVector<> > lo_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_z, 1, true); |
| 172 |
ROL::Ptr<Tpetra::MultiVector<> > hi_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_z, 1, true); |
| 173 |
lo_ptr->putScalar(0.0); hi_ptr->putScalar(1.0); |
| 174 |
ROL::Ptr<ROL::Vector<RealT> > lop |
| 175 |
= ROL::makePtr<ROL::PrimalScaledTpetraMultiVector<RealT>>(lo_ptr, zscale_ptr); |
| 176 |
ROL::Ptr<ROL::Vector<RealT> > hip |
| 177 |
= ROL::makePtr<ROL::PrimalScaledTpetraMultiVector<RealT>>(hi_ptr, zscale_ptr); |
| 178 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bnd = ROL::makePtr<ROL::Bounds<RealT>>(lop,hip); |
| 179 |
|
| 180 |
/*** Check functional interface. ***/ |
| 181 |
*outStream << "Checking Objective:" << "\n"; |
| 182 |
obj->checkGradient(x,d,true,*outStream); |
| 183 |
obj->checkHessVec(x,d,true,*outStream); |
| 184 |
obj->checkHessSym(x,d,d2,true,*outStream); |
| 185 |
*outStream << "Checking Constraint:" << "\n"; |
| 186 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 187 |
con->checkAdjointConsistencyJacobian_1(*dwp, *dup, *up, *zp, true, *outStream); |
| 188 |
con->checkAdjointConsistencyJacobian_2(*dwp, *dzp, *up, *zp, true, *outStream); |
| 189 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 190 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 191 |
con->checkApplyJacobian(x,d,*up,true,*outStream); |
| 192 |
con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream); |
| 193 |
*outStream << "Checking Reduced Objective:" << "\n"; |
| 194 |
objReduced->checkGradient(*zp,*dzp,true,*outStream); |
| 195 |
objReduced->checkHessVec(*zp,*dzp,true,*outStream); |
| 196 |
*outStream << "Checking Volume Constraint:" << "\n"; |
| 197 |
volcon->checkAdjointConsistencyJacobian(*vcp,*dzp,*zp,true,*outStream); |
| 198 |
volcon->checkApplyJacobian(*zp,*dzp,*vcp,true,*outStream); |
| 199 |
volcon->checkApplyAdjointHessian(*zp,*vcp,*dzp,*zp,true,*outStream); |
| 200 |
|
| 201 |
/*** Run optimization ***/ |
| 202 |
ROL::Ptr<ROL::Step<RealT>> |
| 203 |
step = ROL::makePtr<ROL::AugmentedLagrangianStep<RealT>>(*parlist); |
| 204 |
ROL::Ptr<ROL::StatusTest<RealT>> |
| 205 |
status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist); |
| 206 |
ROL::Algorithm<RealT> algo(step,status,false); |
| 207 |
ROL::AugmentedLagrangian<RealT> augLag(objReduced,volcon,*vc_lamp,1.0,*zp,*vcp,*parlist); |
| 208 |
algo.run(*zp,*vc_lamp,augLag,*volcon,*bnd,true,*outStream); |
| 209 |
//ROL::Ptr<ROL::Step<RealT>> |
| 210 |
// step = ROL::makePtr<ROL::MoreauYosidaPenaltyStep<RealT>>(*parlist); |
| 211 |
//ROL::Ptr<ROL::StatusTest<RealT>> |
| 212 |
// status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist); |
| 213 |
//ROL::MoreauYosidaPenalty<RealT> MYpen(objReduced,bnd,*zp,*parlist); |
| 214 |
//ROL::Algorithm<RealT> algo(step,status,false); |
| 215 |
//algo.run(*zp,*vc_lamp,MYpen,*volcon,*bnd,true,*outStream); |
| 216 |
|
| 217 |
// new filter, for testing |
| 218 |
/*parlist->sublist("Density Filter").set("Enable", true); |
| 219 |
ROL::Ptr<DensityFilter<RealT> > testfilter |
| 220 |
= ROL::makePtr<DensityFilter<RealT>>(comm, parlist, outStream); |
| 221 |
ROL::Ptr<Tpetra::MultiVector<> > z_filtered_ptr = ROL::makePtr<Tpetra::MultiVector<>>(*z_ptr, Teuchos::Copy); |
| 222 |
testfilter->apply(z_filtered_ptr, z_ptr); |
| 223 |
ROL::Ptr<Tpetra::MultiVector<> > cm_ptr = data->getCellAreas(); |
| 224 |
ROL::Ptr<Tpetra::MultiVector<> > icm_ptr = ROL::makePtr<Tpetra::MultiVector<>>(*cm_ptr, Teuchos::Copy); |
| 225 |
ROL::Ptr<Tpetra::MultiVector<> > zf_scaled_ptr = ROL::makePtr<Tpetra::MultiVector<>>(*z_ptr, Teuchos::Copy); |
| 226 |
icm_ptr->reciprocal(*cm_ptr); |
| 227 |
zf_scaled_ptr->elementWiseMultiply(1.0, *(icm_ptr->getVector(0)), *z_filtered_ptr, 0.0); |
| 228 |
data->outputTpetraVector(zf_scaled_ptr, "density_filtered_scaled.txt"); |
| 229 |
data->outputTpetraVector(z_filtered_ptr, "density_filtered.txt");*/ |
| 230 |
|
| 231 |
data->outputTpetraVector(z_ptr, "density.txt"); |
| 232 |
data->outputTpetraVector(u_ptr, "state.txt"); |
| 233 |
data->outputTpetraVector(zscale_ptr, "weights.txt"); |
| 234 |
|
| 235 |
// Get a summary from the time monitor. |
| 236 |
Teuchos::TimeMonitor::summarize(); |
| 237 |
} |
| 238 |
catch (std::logic_error& err) { |
| 239 |
*outStream << err.what() << "\n"; |
| 240 |
errorFlag = -1000; |
| 241 |
}; // end try |
| 242 |
|
| 243 |
if (errorFlag != 0) |
| 244 |
std::cout << "End Result: TEST FAILED\n"; |
| 245 |
else |
| 246 |
std::cout << "End Result: TEST PASSED\n"; |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |