| 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 Darcy porosity optimization 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 |
|
| 25 |
#include "ROL_TpetraMultiVector.hpp" |
| 26 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 27 |
#include "ROL_Bounds.hpp" |
| 28 |
#include "ROL_Solver.hpp" |
| 29 |
#include "ROL_SingletonVector.hpp" |
| 30 |
#include "ROL_ConstraintFromObjective.hpp" |
| 31 |
|
| 32 |
#include "../../../../TOOLS/meshreader.hpp" |
| 33 |
#include "../../../../TOOLS/pdeconstraint.hpp" |
| 34 |
#include "../../../../TOOLS/pdeobjective.hpp" |
| 35 |
#include "../../../../TOOLS/pdevector.hpp" |
| 36 |
#include "../../../../TOOLS/integralconstraint.hpp" |
| 37 |
|
| 38 |
#include "pde_darcy.hpp" |
| 39 |
#include "obj_darcy.hpp" |
| 40 |
|
| 41 |
typedef double RealT; |
| 42 |
|
| 43 |
int main(int argc, char *argv[]) { |
| 44 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 45 |
int iprint = argc - 1; |
| 46 |
ROL::Ptr<std::ostream> outStream; |
| 47 |
ROL::nullstream bhs; // outputs nothing |
| 48 |
|
| 49 |
/*** Initialize communicator. ***/ |
| 50 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 51 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 52 |
= Tpetra::getDefaultComm(); |
| 53 |
const int myRank = comm->getRank(); |
| 54 |
if ((iprint > 0) && (myRank == 0)) { |
| 55 |
outStream = ROL::makePtrFromRef(std::cout); |
| 56 |
} |
| 57 |
else { |
| 58 |
outStream = ROL::makePtrFromRef(bhs); |
| 59 |
} |
| 60 |
int errorFlag = 0; |
| 61 |
|
| 62 |
// *** Example body. |
| 63 |
try { |
| 64 |
|
| 65 |
/*** Read in XML input ***/ |
| 66 |
std::string filename = "input.xml"; |
| 67 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 68 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 69 |
|
| 70 |
bool output = parlist->sublist("SimOpt").sublist("Solve").get("Output Iteration History",false); |
| 71 |
output = (iprint > 0) && (myRank==0) && output; |
| 72 |
parlist->sublist("SimOpt").sublist("Solve").set("Output Iteration History",output); |
| 73 |
|
| 74 |
ROL::Ptr<MeshManager<RealT>> meshMgr; |
| 75 |
ROL::Ptr<PDE<RealT>> pde; |
| 76 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT>> con; |
| 77 |
ROL::Ptr<PDE_Constraint<RealT>> pdecon; |
| 78 |
ROL::Ptr<Assembler<RealT>> assembler; |
| 79 |
/*** Initialize main data structure. ***/ |
| 80 |
meshMgr = ROL::makePtr<MeshReader<RealT>>(*parlist); |
| 81 |
// Initialize PDE describing Darcy equations. |
| 82 |
pde = ROL::makePtr<PDE_Darcy<RealT>>(*parlist); |
| 83 |
con = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 84 |
// Cast the constraint and get the assembler. |
| 85 |
pdecon = ROL::dynamicPtrCast<PDE_Constraint<RealT>>(con); |
| 86 |
assembler = pdecon->getAssembler(); |
| 87 |
con->setSolveParameters(*parlist); |
| 88 |
|
| 89 |
// Create state vector and set to zeroes |
| 90 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr, p_ptr, z_ptr, r_ptr; |
| 91 |
ROL::Ptr<std::vector<RealT>> z0_ptr; |
| 92 |
ROL::Ptr<ROL::StdVector<RealT>> z0p; |
| 93 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> z1p; |
| 94 |
ROL::Ptr<ROL::Vector<RealT>> up, pp, zp, rp, xp; |
| 95 |
bool useParamVar = parlist->sublist("Problem").get("Use Optimal Constant Velocity",false); |
| 96 |
int dim = 2; |
| 97 |
u_ptr = assembler->createStateVector(); u_ptr->randomize(); |
| 98 |
p_ptr = assembler->createStateVector(); p_ptr->randomize(); |
| 99 |
z_ptr = assembler->createControlVector(); z_ptr->randomize(); |
| 100 |
r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0); |
| 101 |
if (useParamVar) { |
| 102 |
z0_ptr = ROL::makePtr<std::vector<RealT>>(dim); |
| 103 |
z0p = ROL::makePtr<ROL::StdVector<RealT>>(z0_ptr); |
| 104 |
z1p = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 105 |
} |
| 106 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 107 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 108 |
if (useParamVar) |
| 109 |
zp = ROL::makePtr<PDE_OptVector<RealT>>(z1p,z0p,myRank); |
| 110 |
else |
| 111 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 112 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 113 |
xp = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(up,zp); |
| 114 |
|
| 115 |
// Initialize quadratic objective function. |
| 116 |
ROL::Ptr<QoI<RealT>> qoi; |
| 117 |
ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj; |
| 118 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT>> robj; |
| 119 |
//qoi = ROL::makePtr<QoI_Velocity_Darcy<RealT>>(*parlist, |
| 120 |
// ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getPressureFE(), |
| 121 |
// ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getControlFE(), |
| 122 |
// ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getPressureBdryFE(4), |
| 123 |
// ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getControlBdryFE(4), |
| 124 |
// ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getBdryCellLocIds(4), |
| 125 |
// ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getPermeability()); |
| 126 |
qoi = ROL::makePtr<QoI_VelocityTracking_Darcy<RealT>>(*parlist, |
| 127 |
ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getPressureFE(), |
| 128 |
ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getControlFE(), |
| 129 |
ROL::staticPtrCast<PDE_Darcy<RealT>>(pde)->getPermeability()); |
| 130 |
obj = ROL::makePtr<PDE_Objective<RealT>>(qoi,assembler); |
| 131 |
robj = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj,con,up,zp,pp,true,false); |
| 132 |
|
| 133 |
// Build bound constraint |
| 134 |
ROL::Ptr<ROL::Vector<RealT>> lp, hp; |
| 135 |
ROL::Ptr<ROL::BoundConstraint<RealT>> bnd; |
| 136 |
if (useParamVar) { |
| 137 |
ROL::Ptr<ROL::StdVector<RealT>> l0p, h0p; |
| 138 |
ROL::Ptr<Tpetra::MultiVector<>> l1_ptr, h1_ptr; |
| 139 |
ROL::Ptr<ROL::TpetraMultiVector<RealT>> l1p, h1p; |
| 140 |
l0p = ROL::makePtr<ROL::StdVector<RealT>>(dim,ROL::ROL_NINF<RealT>()); |
| 141 |
h0p = ROL::makePtr<ROL::StdVector<RealT>>(dim,ROL::ROL_INF<RealT>()); |
| 142 |
l1_ptr = assembler->createControlVector(); |
| 143 |
h1_ptr = assembler->createControlVector(); |
| 144 |
l1p = ROL::makePtr<PDE_PrimalOptVector<RealT>>(l1_ptr,pde,assembler,*parlist); |
| 145 |
h1p = ROL::makePtr<PDE_PrimalOptVector<RealT>>(h1_ptr,pde,assembler,*parlist); |
| 146 |
l1p->setScalar(0.0); |
| 147 |
h1p->setScalar(1.0); |
| 148 |
lp = ROL::makePtr<PDE_OptVector<RealT>>(l1p,l0p,myRank); |
| 149 |
hp = ROL::makePtr<PDE_OptVector<RealT>>(h1p,h0p,myRank); |
| 150 |
} |
| 151 |
else { |
| 152 |
lp = zp->clone(); lp->setScalar(0.0); |
| 153 |
hp = zp->clone(); hp->setScalar(1.0); |
| 154 |
} |
| 155 |
bnd = ROL::makePtr<ROL::Bounds<RealT>>(lp, hp); |
| 156 |
// Build optimization problem |
| 157 |
ROL::Ptr<ROL::Problem<RealT>> optProb; |
| 158 |
optProb = ROL::makePtr<ROL::Problem<RealT>>(robj, zp); |
| 159 |
optProb->addBoundConstraint(bnd); |
| 160 |
optProb->finalize(false,true,*outStream); |
| 161 |
|
| 162 |
// Run derivative checks |
| 163 |
bool checkDeriv = parlist->sublist("Problem").get("Check derivatives",false); |
| 164 |
if ( checkDeriv ) { |
| 165 |
ROL::Ptr<ROL::Vector<RealT>> rup = up->clone(); rup->randomize(-1.0,1.0); |
| 166 |
ROL::Ptr<ROL::Vector<RealT>> rzp = zp->clone(); rzp->randomize( 0.0,1.0); |
| 167 |
ROL::Ptr<ROL::Vector<RealT>> rpp = pp->clone(); rpp->randomize(-1.0,1.0); |
| 168 |
ROL::Ptr<ROL::Vector<RealT>> dup = up->clone(); dup->randomize(-1.0,1.0); |
| 169 |
ROL::Ptr<ROL::Vector<RealT>> dzp = zp->clone(); dzp->randomize( 0.0,1.0); |
| 170 |
con->checkApplyJacobian_1(*rup,*rzp,*dup,*rup,true,*outStream); |
| 171 |
con->checkApplyJacobian_2(*rup,*rzp,*dzp,*rup,true,*outStream); |
| 172 |
con->checkInverseJacobian_1(*rup,*rup,*rup,*rzp,true,*outStream); |
| 173 |
con->checkInverseAdjointJacobian_1(*rup,*rup,*rup,*rzp,true,*outStream); |
| 174 |
con->checkApplyAdjointHessian_11(*rup,*rzp,*rpp,*dup,*rup,true,*outStream); |
| 175 |
con->checkApplyAdjointHessian_21(*rup,*rzp,*rpp,*dzp,*rup,true,*outStream); |
| 176 |
con->checkApplyAdjointHessian_12(*rup,*rzp,*rpp,*dup,*rzp,true,*outStream); |
| 177 |
con->checkApplyAdjointHessian_22(*rup,*rzp,*rpp,*dzp,*rzp,true,*outStream); |
| 178 |
obj->checkGradient_1(*rup,*rzp,*dup,true,*outStream); |
| 179 |
obj->checkGradient_2(*rup,*rzp,*dzp,true,*outStream); |
| 180 |
obj->checkHessVec_11(*rup,*rzp,*dup,true,*outStream); |
| 181 |
obj->checkHessVec_12(*rup,*rzp,*dzp,true,*outStream); |
| 182 |
obj->checkHessVec_21(*rup,*rzp,*dup,true,*outStream); |
| 183 |
obj->checkHessVec_22(*rup,*rzp,*dzp,true,*outStream); |
| 184 |
robj->checkGradient(*rzp,*dzp,true,*outStream); |
| 185 |
robj->checkHessVec(*rzp,*dzp,true,*outStream); |
| 186 |
//optProb->check(*outStream); |
| 187 |
} |
| 188 |
|
| 189 |
// Solve optimization problem |
| 190 |
zp->setScalar(0.5); |
| 191 |
up->zero(); pp->zero(); |
| 192 |
bool opt = parlist->sublist("Problem").get("Solve Optimization Problem",true); |
| 193 |
if (opt) { |
| 194 |
std::ifstream infile("control.txt"); |
| 195 |
if (infile.good()) |
| 196 |
assembler->inputTpetraVector(z_ptr,"control.txt"); |
| 197 |
if (useParamVar) { |
| 198 |
std::ifstream infile0; infile0.open("target.txt"); |
| 199 |
if (infile0.good()) { |
| 200 |
for (int i = 0; i < dim; ++i) infile0 >> (*z0_ptr)[i]; |
| 201 |
infile0.close(); |
| 202 |
} |
| 203 |
} |
| 204 |
ROL::Solver<RealT> optSolver(optProb, *parlist); |
| 205 |
optSolver.solve(*outStream); |
| 206 |
pdecon->outputTpetraVector(z_ptr,"control.txt"); |
| 207 |
if (useParamVar) { |
| 208 |
std::ofstream outfile; outfile.open("target.txt"); |
| 209 |
for (const auto x : *z0_ptr) { |
| 210 |
outfile << std::scientific << std::setprecision(16); |
| 211 |
outfile << x << std::endl; |
| 212 |
} |
| 213 |
outfile.close(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// Output |
| 218 |
assembler->printMeshData(*outStream); |
| 219 |
RealT tol(1.e-8); |
| 220 |
Teuchos::Array<RealT> res(1,0); |
| 221 |
con->solve(*rp,*up,*zp,tol); |
| 222 |
pdecon->outputTpetraVector(u_ptr,"state.txt"); |
| 223 |
con->value(*rp,*up,*zp,tol); |
| 224 |
r_ptr->norm2(res.view(0,1)); |
| 225 |
*outStream << "Residual Norm: " << res[0] << std::endl; |
| 226 |
assembler->printCellAveragesPDE(pde,u_ptr,z_ptr); |
| 227 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 228 |
//pdecon->outputTpetraData(); |
| 229 |
|
| 230 |
// Get a summary from the time monitor. |
| 231 |
Teuchos::TimeMonitor::summarize(); |
| 232 |
} |
| 233 |
catch (std::logic_error& err) { |
| 234 |
*outStream << err.what() << "\n"; |
| 235 |
errorFlag = -1000; |
| 236 |
}; // end try |
| 237 |
|
| 238 |
if (errorFlag != 0) |
| 239 |
std::cout << "End Result: TEST FAILED\n"; |
| 240 |
else |
| 241 |
std::cout << "End Result: TEST PASSED\n"; |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |