| 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 Poisson 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 |
|
| 25 |
#include "ROL_Algorithm.hpp" |
| 26 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 27 |
|
| 28 |
#include "../../TOOLS/meshmanager.hpp" |
| 29 |
#include "../../TOOLS/linearpdeconstraint.hpp" |
| 30 |
#include "../../TOOLS/pdeobjective.hpp" |
| 31 |
#include "../../TOOLS/pdevector.hpp" |
| 32 |
#include "pde_fractional_poisson.hpp" |
| 33 |
#include "obj_fractional_poisson.hpp" |
| 34 |
#include "fractional_constraint.hpp" |
| 35 |
#include "fractional_objective.hpp" |
| 36 |
|
| 37 |
typedef double RealT; |
| 38 |
|
| 39 |
int main(int argc, char *argv[]) { |
| 40 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 41 |
int iprint = argc - 1; |
| 42 |
ROL::Ptr<std::ostream> outStream; |
| 43 |
ROL::nullstream bhs; // outputs nothing |
| 44 |
|
| 45 |
/*** Initialize communicator. ***/ |
| 46 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 47 |
ROL::Ptr<const Teuchos::Comm<int> > comm |
| 48 |
= Tpetra::getDefaultComm(); |
| 49 |
const int myRank = comm->getRank(); |
| 50 |
if ((iprint > 0) && (myRank == 0)) { |
| 51 |
outStream = ROL::makePtrFromRef(std::cout); |
| 52 |
} |
| 53 |
else { |
| 54 |
outStream = ROL::makePtrFromRef(bhs); |
| 55 |
} |
| 56 |
int errorFlag = 0; |
| 57 |
|
| 58 |
// *** Example body. |
| 59 |
try { |
| 60 |
|
| 61 |
/*** Read in XML input ***/ |
| 62 |
std::string filename = "input.xml"; |
| 63 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 64 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 65 |
|
| 66 |
// Set parameters for cylidner mesh |
| 67 |
RealT s = parlist->sublist("Problem").get("Fractional Power",0.5); |
| 68 |
RealT gamma = (s == 0.5) ? static_cast<RealT>(1) |
| 69 |
: static_cast<RealT>(3)/(static_cast<RealT>(2)*s) + static_cast<RealT>(1e-3); |
| 70 |
int NX = parlist->sublist("Geometry").get("NX",20); |
| 71 |
int NY = parlist->sublist("Geometry").get("NY",20); |
| 72 |
RealT NT = static_cast<RealT>(NX*NY); |
| 73 |
RealT width = static_cast<RealT>(1) + std::log10(NT)/static_cast<RealT>(3); |
| 74 |
int NI = static_cast<int>(width * std::sqrt(NT)); |
| 75 |
parlist->sublist("Geometry").sublist("Cylinder").set("Grading Parameter", gamma); |
| 76 |
parlist->sublist("Geometry").sublist("Cylinder").set("NI", NI); |
| 77 |
parlist->sublist("Geometry").sublist("Cylinder").set("Height", width); |
| 78 |
|
| 79 |
*outStream << std::endl; |
| 80 |
*outStream << "Fractional Power: " << s << std::endl; |
| 81 |
*outStream << "Mesh Grading Parameter: " << gamma << std::endl; |
| 82 |
*outStream << "Cylinder Height: " << width << std::endl; |
| 83 |
*outStream << "Number of Intervals: " << NI << std::endl; |
| 84 |
*outStream << std::endl; |
| 85 |
|
| 86 |
// Initialize 2D Poisson's equation |
| 87 |
ROL::Ptr<MeshManager<RealT> > meshMgr_local |
| 88 |
= ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 89 |
ROL::Ptr<PDE_Fractional_Poisson_Local<RealT> > pde_local |
| 90 |
= ROL::makePtr<PDE_Fractional_Poisson_Local<RealT>>(*parlist); |
| 91 |
// Initialize 1D singular Poisson's equation |
| 92 |
ROL::Ptr<MeshManager<RealT> > meshMgr_cylinder |
| 93 |
= ROL::makePtr<MeshManager_Fractional_Cylinder<RealT>>(*parlist); |
| 94 |
ROL::Ptr<PDE_Fractional_Poisson_Cylinder<RealT> > pde_cylinder |
| 95 |
= ROL::makePtr<PDE_Fractional_Poisson_Cylinder<RealT>>(*parlist); |
| 96 |
// Build fractional constraint |
| 97 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con |
| 98 |
= ROL::makePtr<FractionalConstraint<RealT>>(pde_local, meshMgr_local, comm, |
| 99 |
pde_cylinder, meshMgr_cylinder, comm, |
| 100 |
*parlist, *outStream); |
| 101 |
ROL::Ptr<FractionalConstraint<RealT> > fracCon |
| 102 |
= ROL::dynamicPtrCast<FractionalConstraint<RealT> >(con); |
| 103 |
ROL::Ptr<Assembler<RealT> > assembler = fracCon->getLocalAssembler(); |
| 104 |
|
| 105 |
// Build objective fuction |
| 106 |
std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr); |
| 107 |
qoi_vec[0] = ROL::makePtr<QoI_L2Tracking_Fractional_Poisson<RealT>>(pde_local->getFE()); |
| 108 |
qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_Fractional_Poisson<RealT>>(pde_local->getFE()); |
| 109 |
RealT stateCost = parlist->sublist("Problem").get("State Cost",1e0); |
| 110 |
RealT controlCost = parlist->sublist("Problem").get("Control Cost",1e0); |
| 111 |
std::vector<RealT> wts = {stateCost, controlCost}; |
| 112 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > pdeobj |
| 113 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,wts,assembler); |
| 114 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj |
| 115 |
= ROL::makePtr<FractionalObjective<RealT>>(pdeobj); |
| 116 |
|
| 117 |
// Build vectors |
| 118 |
ROL::Ptr<Tpetra::MultiVector<> > stateVec = assembler->createStateVector(); |
| 119 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = ROL::makePtr<Tpetra::MultiVector<>>(stateVec->getMap(),NI+1); |
| 120 |
ROL::Ptr<Tpetra::MultiVector<> > p_ptr = ROL::makePtr<Tpetra::MultiVector<>>(stateVec->getMap(),NI+1); |
| 121 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = ROL::makePtr<Tpetra::MultiVector<>>(stateVec->getMap(),NI+1); |
| 122 |
u_ptr->randomize(); //u_ptr->putScalar(static_cast<RealT>(1)); |
| 123 |
p_ptr->randomize(); //p_ptr->putScalar(static_cast<RealT>(1)); |
| 124 |
du_ptr->randomize(); //du_ptr->putScalar(static_cast<RealT>(0)); |
| 125 |
ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(u_ptr); |
| 126 |
ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_ptr); |
| 127 |
ROL::Ptr<ROL::Vector<RealT> > dup = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(du_ptr); |
| 128 |
// Create residual vectors |
| 129 |
ROL::Ptr<Tpetra::MultiVector<> > conVec = assembler->createResidualVector(); |
| 130 |
ROL::Ptr<Tpetra::MultiVector<> > r_ptr = ROL::makePtr<Tpetra::MultiVector<>>(conVec->getMap(),NI+1); |
| 131 |
r_ptr->randomize(); //r_ptr->putScalar(static_cast<RealT>(1)); |
| 132 |
ROL::Ptr<ROL::Vector<RealT> > rp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(r_ptr); |
| 133 |
// Create control vector and set to ones |
| 134 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = assembler->createControlVector(); |
| 135 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = assembler->createControlVector(); |
| 136 |
z_ptr->randomize(); //z_ptr->putScalar(static_cast<RealT>(1)); |
| 137 |
dz_ptr->randomize(); //dz_ptr->putScalar(static_cast<RealT>(0)); |
| 138 |
ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(z_ptr); |
| 139 |
ROL::Ptr<ROL::Vector<RealT> > dzp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(dz_ptr); |
| 140 |
// Create ROL SimOpt vectors |
| 141 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 142 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 143 |
|
| 144 |
// Build reduced objective function |
| 145 |
bool storage = parlist->sublist("Problem").get("Use State and Adjoint Storage",true); |
| 146 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > objReduced |
| 147 |
= ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, pp, storage, false); |
| 148 |
|
| 149 |
// Check derivatives. |
| 150 |
bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false); |
| 151 |
if ( checkDeriv ) { |
| 152 |
*outStream << "\n\nCheck Gradient_1 of Full Objective Function\n"; |
| 153 |
obj->checkGradient_1(*up,*zp,*dup,true,*outStream); |
| 154 |
*outStream << "\n\nCheck Gradient_2 of Full Objective Function\n"; |
| 155 |
obj->checkGradient_2(*up,*zp,*dzp,true,*outStream); |
| 156 |
*outStream << "\n\nCheck Gradient of Full Objective Function\n"; |
| 157 |
obj->checkGradient(x,d,true,*outStream); |
| 158 |
*outStream << "\n\nCheck Hessian_11 of Full Objective Function\n"; |
| 159 |
obj->checkHessVec_11(*up,*zp,*dup,true,*outStream); |
| 160 |
*outStream << "\n\nCheck Hessian_12 of Full Objective Function\n"; |
| 161 |
obj->checkHessVec_12(*up,*zp,*dzp,true,*outStream); |
| 162 |
*outStream << "\n\nCheck Hessian_21 of Full Objective Function\n"; |
| 163 |
obj->checkHessVec_21(*up,*zp,*dup,true,*outStream); |
| 164 |
*outStream << "\n\nCheck Hessian_22 of Full Objective Function\n"; |
| 165 |
obj->checkHessVec_22(*up,*zp,*dzp,true,*outStream); |
| 166 |
*outStream << "\n\nCheck Hessian of Full Objective Function\n"; |
| 167 |
obj->checkHessVec(x,d,true,*outStream); |
| 168 |
*outStream << "\n\nCheck Full Jacobian of PDE Constraint\n"; |
| 169 |
con->checkApplyJacobian(x,d,*rp,true,*outStream); |
| 170 |
*outStream << "\n\nCheck Jacobian_1 of PDE Constraint\n"; |
| 171 |
con->checkApplyJacobian_1(*up,*zp,*dup,*rp,true,*outStream); |
| 172 |
*outStream << "\n\nCheck Jacobian_2 of PDE Constraint\n"; |
| 173 |
con->checkApplyJacobian_2(*up,*zp,*dzp,*rp,true,*outStream); |
| 174 |
*outStream << "\n\nCheck Full Hessian of PDE Constraint\n"; |
| 175 |
con->checkApplyAdjointHessian(x,*pp,d,x,true,*outStream); |
| 176 |
*outStream << "\n\nCheck Hessian_11 of PDE Constraint\n"; |
| 177 |
con->checkApplyAdjointHessian_11(*up,*zp,*pp,*dup,*dup,true,*outStream); |
| 178 |
*outStream << "\n\nCheck Hessian_21 of PDE Constraint\n"; |
| 179 |
con->checkApplyAdjointHessian_21(*up,*zp,*pp,*dzp,*dup,true,*outStream); |
| 180 |
*outStream << "\n\nCheck Hessian_12 of PDE Constraint\n"; |
| 181 |
con->checkApplyAdjointHessian_12(*up,*zp,*pp,*dup,*dzp,true,*outStream); |
| 182 |
*outStream << "\n\nCheck Hessian_22 of PDE Constraint\n"; |
| 183 |
con->checkApplyAdjointHessian_22(*up,*zp,*pp,*dzp,*dzp,true,*outStream); |
| 184 |
*outStream << "\n"; |
| 185 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 186 |
*outStream << "\n"; |
| 187 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 188 |
*outStream << "\n"; |
| 189 |
*outStream << "\n\nCheck Gradient of Reduced Objective Function\n"; |
| 190 |
objReduced->checkGradient(*zp,*dzp,true,*outStream); |
| 191 |
*outStream << "\n\nCheck Hessian of Reduced Objective Function\n"; |
| 192 |
objReduced->checkHessVec(*zp,*dzp,true,*outStream); |
| 193 |
} |
| 194 |
|
| 195 |
// Run optimization |
| 196 |
ROL::Ptr<ROL::Algorithm<RealT> > algo |
| 197 |
= ROL::makePtr<ROL::Algorithm<RealT>>("Trust Region",*parlist,false); |
| 198 |
zp->zero(); |
| 199 |
algo->run(*zp,*objReduced,true,*outStream); |
| 200 |
|
| 201 |
// Get a summary from the time monitor. |
| 202 |
Teuchos::TimeMonitor::summarize(); |
| 203 |
} |
| 204 |
catch (std::logic_error& err) { |
| 205 |
*outStream << err.what() << "\n"; |
| 206 |
errorFlag = -1000; |
| 207 |
}; // end try |
| 208 |
|
| 209 |
if (errorFlag != 0) |
| 210 |
std::cout << "End Result: TEST FAILED\n"; |
| 211 |
else |
| 212 |
std::cout << "End Result: TEST PASSED\n"; |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |