| 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-Boltzmann problem. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "Teuchos_Comm.hpp" |
| 15 |
#include "Teuchos_GlobalMPISession.hpp" |
| 16 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 17 |
|
| 18 |
#include "Tpetra_Core.hpp" |
| 19 |
#include "Tpetra_Version.hpp" |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
#include "ROL_Stream.hpp" |
| 25 |
#include "ROL_Solver.hpp" |
| 26 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 27 |
#include "ROL_BoundConstraint_SimOpt.hpp" |
| 28 |
#include "ROL_Bounds.hpp" |
| 29 |
|
| 30 |
#include "../TOOLS/meshmanager.hpp" |
| 31 |
#include "../TOOLS/pdeconstraint.hpp" |
| 32 |
#include "../TOOLS/pdeobjective.hpp" |
| 33 |
#include "../TOOLS/pdevector.hpp" |
| 34 |
#include "pde_allen_cahn.hpp" |
| 35 |
#include "obj_allen_cahn.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 |
/*** Initialize main data structure. ***/ |
| 67 |
ROL::Ptr<MeshManager<RealT>> |
| 68 |
meshMgr = ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 69 |
// Initialize PDE describe Poisson's equation |
| 70 |
ROL::Ptr<PDE_Allen_Cahn<RealT>> |
| 71 |
pde = ROL::makePtr<PDE_Allen_Cahn<RealT>>(*parlist); |
| 72 |
ROL::Ptr<PDE_Constraint<RealT>> |
| 73 |
con = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 74 |
const ROL::Ptr<Assembler<RealT>> assembler = con->getAssembler(); |
| 75 |
assembler->printMeshData(*outStream); |
| 76 |
con->setSolveParameters(*parlist); |
| 77 |
|
| 78 |
/*************************************************************************/ |
| 79 |
/***************** BUILD VECTORS *****************************************/ |
| 80 |
/*************************************************************************/ |
| 81 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr = assembler->createStateVector(); |
| 82 |
ROL::Ptr<Tpetra::MultiVector<>> p_ptr = assembler->createStateVector(); |
| 83 |
ROL::Ptr<Tpetra::MultiVector<>> r_ptr = assembler->createResidualVector(); |
| 84 |
ROL::Ptr<Tpetra::MultiVector<>> z_ptr = assembler->createControlVector(); |
| 85 |
ROL::Ptr<ROL::Vector<RealT>> up, pp, rp, zp; |
| 86 |
u_ptr->randomize(); //u_ptr->putScalar(static_cast<RealT>(1)); |
| 87 |
p_ptr->randomize(); //p_ptr->putScalar(static_cast<RealT>(1)); |
| 88 |
r_ptr->randomize(); //r_ptr->putScalar(static_cast<RealT>(1)); |
| 89 |
z_ptr->randomize(); //z_ptr->putScalar(static_cast<RealT>(1)); |
| 90 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 91 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 92 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 93 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 94 |
|
| 95 |
/*************************************************************************/ |
| 96 |
/***************** BUILD COST FUNCTIONAL *********************************/ |
| 97 |
/*************************************************************************/ |
| 98 |
// Initialize quadratic objective function |
| 99 |
std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(2,ROL::nullPtr); |
| 100 |
qoi_vec[0] = ROL::makePtr<QoI_State_Cost_Allen_Cahn<RealT>>(pde->getFE()); |
| 101 |
qoi_vec[1] = ROL::makePtr<QoI_Control_Cost_Allen_Cahn<RealT>>(pde->getFE(), |
| 102 |
pde->getBdryFE(), |
| 103 |
pde->getBdryCellLocIds()); |
| 104 |
std::vector<RealT> weights(2); |
| 105 |
weights[0] = static_cast<RealT>(1); |
| 106 |
weights[1] = parlist->sublist("Problem").get("Control Penalty Parameter", 1e-4); |
| 107 |
// Build full-space objective |
| 108 |
ROL::Ptr<PDE_Objective<RealT>> |
| 109 |
obj = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,weights,assembler); |
| 110 |
// Build reduced-space objective |
| 111 |
bool storage = parlist->sublist("Problem").get("Use state storage",true); |
| 112 |
ROL::Ptr<ROL::VectorController<RealT>> |
| 113 |
stateStore = ROL::makePtr<ROL::VectorController<RealT>>(); |
| 114 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT>> |
| 115 |
robj = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj,con, |
| 116 |
stateStore, |
| 117 |
up,zp,pp, |
| 118 |
storage); |
| 119 |
|
| 120 |
/*************************************************************************/ |
| 121 |
/***************** BUILD BOUND CONSTRAINT ********************************/ |
| 122 |
/*************************************************************************/ |
| 123 |
// Control bounds |
| 124 |
ROL::Ptr<Tpetra::MultiVector<>> zlo_ptr = assembler->createControlVector(); |
| 125 |
ROL::Ptr<Tpetra::MultiVector<>> zhi_ptr = assembler->createControlVector(); |
| 126 |
RealT lo = parlist->sublist("Problem").get("Lower Bound",0.0); |
| 127 |
RealT hi = parlist->sublist("Problem").get("Upper Bound",1.0); |
| 128 |
zlo_ptr->putScalar(lo); zhi_ptr->putScalar(hi); |
| 129 |
ROL::Ptr<ROL::Vector<RealT>> zlop, zhip; |
| 130 |
zlop = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zlo_ptr,pde,assembler); |
| 131 |
zhip = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zhi_ptr,pde,assembler); |
| 132 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 133 |
zbnd = ROL::makePtr<ROL::Bounds<RealT>>(zlop,zhip); |
| 134 |
bool deactivate = parlist->sublist("Problem").get("Deactivate Bound Constraints",false); |
| 135 |
if (deactivate) zbnd->deactivate(); |
| 136 |
// State bounds |
| 137 |
ROL::Ptr<Tpetra::MultiVector<>> ulo_ptr = assembler->createStateVector(); |
| 138 |
ROL::Ptr<Tpetra::MultiVector<>> uhi_ptr = assembler->createStateVector(); |
| 139 |
ulo_ptr->putScalar(ROL::ROL_NINF<RealT>()); uhi_ptr->putScalar(ROL::ROL_INF<RealT>()); |
| 140 |
ROL::Ptr<ROL::Vector<RealT>> ulop, uhip; |
| 141 |
ulop = ROL::makePtr<PDE_PrimalSimVector<RealT>>(ulo_ptr,pde,assembler); |
| 142 |
uhip = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uhi_ptr,pde,assembler); |
| 143 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 144 |
ubnd = ROL::makePtr<ROL::Bounds<RealT>>(ulop,uhip); |
| 145 |
ubnd->deactivate(); |
| 146 |
|
| 147 |
// SimOpt bounds |
| 148 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 149 |
bnd = ROL::makePtr<ROL::BoundConstraint_SimOpt<RealT>>(ubnd,zbnd); |
| 150 |
|
| 151 |
// Create ROL SimOpt vectors |
| 152 |
ROL::Ptr<Tpetra::MultiVector<>> du_ptr = assembler->createStateVector(); |
| 153 |
ROL::Ptr<Tpetra::MultiVector<>> dz_ptr = assembler->createControlVector(); |
| 154 |
du_ptr->randomize(); //du_ptr->putScalar(static_cast<RealT>(0)); |
| 155 |
dz_ptr->randomize(); //dz_ptr->putScalar(static_cast<RealT>(1)); |
| 156 |
ROL::Ptr<ROL::Vector<RealT>> dup, dzp; |
| 157 |
dup = ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler,*parlist); |
| 158 |
dzp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler,*parlist); |
| 159 |
|
| 160 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 161 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 162 |
|
| 163 |
/*************************************************************************/ |
| 164 |
/***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/ |
| 165 |
/*************************************************************************/ |
| 166 |
bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false); |
| 167 |
if ( checkDeriv ) { |
| 168 |
*outStream << "\n\nCheck Gradient of Full Objective Function\n"; |
| 169 |
obj->checkGradient(x,d,true,*outStream); |
| 170 |
*outStream << "\n\nCheck Hessian of Full Objective Function\n"; |
| 171 |
obj->checkHessVec(x,d,true,*outStream); |
| 172 |
*outStream << "\n\nCheck Full Jacobian of PDE Constraint\n"; |
| 173 |
con->checkApplyJacobian(x,d,*rp,true,*outStream); |
| 174 |
*outStream << "\n\nCheck Jacobian_1 of PDE Constraint\n"; |
| 175 |
con->checkApplyJacobian_1(*up,*zp,*dup,*rp,true,*outStream); |
| 176 |
*outStream << "\n\nCheck Jacobian_2 of PDE Constraint\n"; |
| 177 |
con->checkApplyJacobian_2(*up,*zp,*dzp,*rp,true,*outStream); |
| 178 |
*outStream << "\n\nCheck Full Hessian of PDE Constraint\n"; |
| 179 |
con->checkApplyAdjointHessian(x,*pp,d,x,true,*outStream); |
| 180 |
*outStream << "\n"; |
| 181 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 182 |
*outStream << "\n"; |
| 183 |
con->checkInverseJacobian_1(*rp,*dup,*up,*zp,true,*outStream); |
| 184 |
*outStream << "\n"; |
| 185 |
*outStream << "\n\nCheck Gradient of Reduced Objective Function\n"; |
| 186 |
robj->checkGradient(*zp,*dzp,true,*outStream); |
| 187 |
*outStream << "\n\nCheck Hessian of Reduced Objective Function\n"; |
| 188 |
robj->checkHessVec(*zp,*dzp,true,*outStream); |
| 189 |
} |
| 190 |
|
| 191 |
bool useFullSpace = parlist->sublist("Problem").get("Full space",false); |
| 192 |
ROL::Ptr<ROL::Problem<RealT>> optProb; |
| 193 |
if ( useFullSpace ) { |
| 194 |
optProb = ROL::makePtr<ROL::Problem<RealT>>(obj, makePtrFromRef(x)); |
| 195 |
optProb->addBoundConstraint(bnd); |
| 196 |
optProb->addConstraint("PDE", con, pp); |
| 197 |
} |
| 198 |
else { |
| 199 |
optProb = ROL::makePtr<ROL::Problem<RealT>>(robj, zp); |
| 200 |
optProb->addBoundConstraint(zbnd); |
| 201 |
} |
| 202 |
optProb->finalize(false,true,*outStream); |
| 203 |
ROL::Solver<RealT> optSolver(optProb, *parlist); |
| 204 |
optSolver.solve(*outStream); |
| 205 |
|
| 206 |
// Output. |
| 207 |
RealT tol(1.e-8); |
| 208 |
con->solve(*rp,*up,*zp,tol); |
| 209 |
con->outputTpetraVector(u_ptr,"state.txt"); |
| 210 |
con->outputTpetraVector(z_ptr,"control.txt"); |
| 211 |
|
| 212 |
Teuchos::Array<RealT> res(1,0); |
| 213 |
con->value(*rp,*up,*zp,tol); |
| 214 |
r_ptr->norm2(res.view(0,1)); |
| 215 |
*outStream << "Residual Norm: " << res[0] << std::endl; |
| 216 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 217 |
} |
| 218 |
catch (std::logic_error& err) { |
| 219 |
*outStream << err.what() << "\n"; |
| 220 |
errorFlag = -1000; |
| 221 |
}; // end try |
| 222 |
|
| 223 |
if (errorFlag != 0) |
| 224 |
std::cout << "End Result: TEST FAILED\n"; |
| 225 |
else |
| 226 |
std::cout << "End Result: TEST PASSED\n"; |
| 227 |
|
| 228 |
return 0; |
| 229 |
} |