| 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_03.cpp |
| 11 |
\brief Shows how to solve the Poisson-Boltzmann 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_Solver.hpp" |
| 26 |
#include "ROL_UnaryFunctions.hpp" |
| 27 |
#include "ROL_Bounds.hpp" |
| 28 |
#include "ROL_BoundConstraint_SimOpt.hpp" |
| 29 |
#include "ROL_CompositeConstraint_SimOpt.hpp" |
| 30 |
|
| 31 |
#include "../TOOLS/linearpdeconstraint.hpp" |
| 32 |
#include "../TOOLS/pdeconstraint.hpp" |
| 33 |
#include "../TOOLS/pdeobjective.hpp" |
| 34 |
#include "../TOOLS/pdevector.hpp" |
| 35 |
#include "pde_poisson_boltzmann_ex02.hpp" |
| 36 |
#include "mesh_poisson_boltzmann_ex02.hpp" |
| 37 |
#include "obj_poisson_boltzmann_ex02.hpp" |
| 38 |
|
| 39 |
typedef double RealT; |
| 40 |
|
| 41 |
int main(int argc, char *argv[]) { |
| 42 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 43 |
int iprint = argc - 1; |
| 44 |
ROL::Ptr<std::ostream> outStream; |
| 45 |
ROL::nullstream bhs; // outputs nothing |
| 46 |
|
| 47 |
/*** Initialize communicator. ***/ |
| 48 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 49 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 50 |
= Tpetra::getDefaultComm(); |
| 51 |
const int myRank = comm->getRank(); |
| 52 |
if ((iprint > 0) && (myRank == 0)) { |
| 53 |
outStream = ROL::makePtrFromRef(std::cout); |
| 54 |
} |
| 55 |
else { |
| 56 |
outStream = ROL::makePtrFromRef(bhs); |
| 57 |
} |
| 58 |
int errorFlag = 0; |
| 59 |
|
| 60 |
// *** Example body. |
| 61 |
try { |
| 62 |
|
| 63 |
/*** Read in XML input ***/ |
| 64 |
std::string filename = "input_ex03.xml"; |
| 65 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 66 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 67 |
|
| 68 |
/*** Initialize main data structure. ***/ |
| 69 |
ROL::Ptr<MeshManager<RealT>> |
| 70 |
meshMgr = ROL::makePtr<MeshManager_Example02<RealT>>(*parlist); |
| 71 |
// Initialize PDE describe Poisson's equation |
| 72 |
ROL::Ptr<PDE_Poisson_Boltzmann_ex02<RealT>> |
| 73 |
pde = ROL::makePtr<PDE_Poisson_Boltzmann_ex02<RealT>>(*parlist); |
| 74 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT>> |
| 75 |
con = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 76 |
ROL::Ptr<PDE_Constraint<RealT>> |
| 77 |
pdeCon = ROL::dynamicPtrCast<PDE_Constraint<RealT>>(con); |
| 78 |
ROL::Ptr<PDE_Doping<RealT>> |
| 79 |
pdeDoping = ROL::makePtr<PDE_Doping<RealT>>(*parlist); |
| 80 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT>> |
| 81 |
conDoping = ROL::makePtr<Linear_PDE_Constraint<RealT>>(pdeDoping,meshMgr,comm,*parlist,*outStream,true); |
| 82 |
const ROL::Ptr<Assembler<RealT>> assembler = pdeCon->getAssembler(); |
| 83 |
assembler->printMeshData(*outStream); |
| 84 |
con->setSolveParameters(*parlist); |
| 85 |
|
| 86 |
/*************************************************************************/ |
| 87 |
/***************** BUILD VECTORS *****************************************/ |
| 88 |
/*************************************************************************/ |
| 89 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr = assembler->createStateVector(); |
| 90 |
ROL::Ptr<Tpetra::MultiVector<>> p_ptr = assembler->createStateVector(); |
| 91 |
ROL::Ptr<Tpetra::MultiVector<>> r_ptr = assembler->createResidualVector(); |
| 92 |
ROL::Ptr<Tpetra::MultiVector<>> z_ptr = assembler->createControlVector(); |
| 93 |
ROL::Ptr<ROL::Vector<RealT>> up, pp, rp, zp; |
| 94 |
u_ptr->randomize(); //u_ptr->putScalar(static_cast<RealT>(1)); |
| 95 |
p_ptr->randomize(); //p_ptr->putScalar(static_cast<RealT>(1)); |
| 96 |
r_ptr->randomize(); //r_ptr->putScalar(static_cast<RealT>(1)); |
| 97 |
z_ptr->randomize(); //z_ptr->putScalar(static_cast<RealT>(1)); |
| 98 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 99 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 100 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 101 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 102 |
ROL::Ptr<ROL::Vector<RealT>> xp |
| 103 |
= ROL::makePtr<ROL::Vector_SimOpt<RealT>>(up,zp); |
| 104 |
|
| 105 |
/*************************************************************************/ |
| 106 |
/***************** BUILD REFERENCE DOPING AND POTENTIAL ******************/ |
| 107 |
/*************************************************************************/ |
| 108 |
ROL::Ptr<Tpetra::MultiVector<>> ru_ptr = assembler->createStateVector(); |
| 109 |
ROL::Ptr<Tpetra::MultiVector<>> rz_ptr = assembler->createControlVector(); |
| 110 |
ROL::Ptr<ROL::Vector<RealT>> rup, rzp; |
| 111 |
rup = ROL::makePtr<PDE_PrimalSimVector<RealT>>(ru_ptr,pde,assembler,*parlist); |
| 112 |
rzp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(rz_ptr,pde,assembler,*parlist); |
| 113 |
ROL::Ptr<Doping<RealT>> |
| 114 |
dope = ROL::makePtr<Doping<RealT>>(pde->getFE(), pde->getCellNodes(), |
| 115 |
assembler->getDofManager()->getCellDofs(), |
| 116 |
assembler->getCellIds(),*parlist); |
| 117 |
// Initialize "filtered" of "unfiltered" constraint. |
| 118 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT>> |
| 119 |
pdeWithDoping = ROL::makePtr<ROL::CompositeConstraint_SimOpt<RealT>>( |
| 120 |
con, conDoping, *rp, *rp, *up, *zp, *zp, true, true); |
| 121 |
pdeWithDoping->setSolveParameters(*parlist); |
| 122 |
dope->build(rz_ptr); |
| 123 |
RealT tol(1.e-8); |
| 124 |
pdeWithDoping->solve(*rp,*rup,*rzp,tol); |
| 125 |
pdeCon->outputTpetraVector(ru_ptr,"reference_state.txt"); |
| 126 |
pdeCon->outputTpetraVector(rz_ptr,"reference_control.txt"); |
| 127 |
|
| 128 |
/*************************************************************************/ |
| 129 |
/***************** BUILD COST FUNCTIONAL *********************************/ |
| 130 |
/*************************************************************************/ |
| 131 |
std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(3,ROL::nullPtr); |
| 132 |
// Current flow over drain |
| 133 |
qoi_vec[0] = ROL::makePtr<QoI_State_Cost_1_Poisson_Boltzmann<RealT>>(pde->getFE(), |
| 134 |
pde->getBdryFE(),pde->getBdryCellLocIds(),*parlist); |
| 135 |
ROL::Ptr<IntegralObjective<RealT>> |
| 136 |
stateObj = ROL::makePtr<IntegralObjective<RealT>>(qoi_vec[0],assembler); |
| 137 |
// Deviation from reference doping |
| 138 |
qoi_vec[1] = ROL::makePtr<QoI_Control_Cost_1_Poisson_Boltzmann<RealT>>(pde->getFE(),dope); |
| 139 |
ROL::Ptr<IntegralObjective<RealT>> |
| 140 |
ctrlObj1 = ROL::makePtr<IntegralObjective<RealT>>(qoi_vec[1],assembler); |
| 141 |
// H1-Seminorm of doping |
| 142 |
qoi_vec[2] = ROL::makePtr<QoI_Control_Cost_2_Poisson_Boltzmann<RealT>>(pde->getFE()); |
| 143 |
ROL::Ptr<IntegralObjective<RealT>> |
| 144 |
ctrlObj2 = ROL::makePtr<IntegralObjective<RealT>>(qoi_vec[2],assembler); |
| 145 |
// Build standard vector objective function |
| 146 |
RealT currentWeight = parlist->sublist("Problem").get("Desired Current Scale",1.5); |
| 147 |
RealT J = stateObj->value(*rup,*rzp,tol); // Reference current flow over drain |
| 148 |
J *= currentWeight; // Increase current flow by 50% |
| 149 |
RealT w1 = parlist->sublist("Problem").get("State Cost Parameter",1e-3); |
| 150 |
RealT w2 = parlist->sublist("Problem").get("Control Misfit Parameter",1e-2); |
| 151 |
RealT w3 = parlist->sublist("Problem").get("Control Cost Parameter",1e-8); |
| 152 |
ROL::Ptr<ROL::StdObjective<RealT>> |
| 153 |
std_obj = ROL::makePtr<StdObjective_Poisson_Boltzmann<RealT>>(J,w1,w2,w3); |
| 154 |
// Build full-space objective |
| 155 |
ROL::Ptr<PDE_Objective<RealT>> |
| 156 |
obj = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler); |
| 157 |
|
| 158 |
/*************************************************************************/ |
| 159 |
/***************** BUILD BOUND CONSTRAINT ********************************/ |
| 160 |
/*************************************************************************/ |
| 161 |
ROL::Ptr<Tpetra::MultiVector<>> zlo_ptr = assembler->createControlVector(); |
| 162 |
ROL::Ptr<Tpetra::MultiVector<>> zhi_ptr = assembler->createControlVector(); |
| 163 |
ROL::Ptr<DopingBounds<RealT>> |
| 164 |
dopeBnd = ROL::makePtr<DopingBounds<RealT>>(pde->getFE(),pde->getCellNodes(), |
| 165 |
assembler->getDofManager()->getCellDofs(), |
| 166 |
assembler->getCellIds(),*parlist); |
| 167 |
dopeBnd->build(zlo_ptr,zhi_ptr); |
| 168 |
ROL::Ptr<ROL::Vector<RealT>> zlop, zhip; |
| 169 |
zlop = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zlo_ptr,pde,assembler); |
| 170 |
zhip = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zhi_ptr,pde,assembler); |
| 171 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 172 |
zbnd = ROL::makePtr<ROL::Bounds<RealT>>(zlop,zhip); |
| 173 |
// State bounds |
| 174 |
ROL::Ptr<Tpetra::MultiVector<>> ulo_ptr = assembler->createStateVector(); |
| 175 |
ROL::Ptr<Tpetra::MultiVector<>> uhi_ptr = assembler->createStateVector(); |
| 176 |
ulo_ptr->putScalar(ROL::ROL_NINF<RealT>()); uhi_ptr->putScalar(ROL::ROL_INF<RealT>()); |
| 177 |
ROL::Ptr<ROL::Vector<RealT>> ulop, uhip; |
| 178 |
ulop = ROL::makePtr<PDE_PrimalSimVector<RealT>>(ulo_ptr,pde,assembler); |
| 179 |
uhip = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uhi_ptr,pde,assembler); |
| 180 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 181 |
ubnd = ROL::makePtr<ROL::Bounds<RealT>>(ulop,uhip); |
| 182 |
ubnd->deactivate(); |
| 183 |
// SimOpt bounds |
| 184 |
ROL::Ptr<ROL::BoundConstraint<RealT>> |
| 185 |
bnd = ROL::makePtr<ROL::BoundConstraint_SimOpt<RealT>>(ubnd,zbnd); |
| 186 |
bool deactivate = parlist->sublist("Problem").get("Deactivate Bound Constraints",false); |
| 187 |
if (deactivate) bnd->deactivate(); |
| 188 |
|
| 189 |
/*************************************************************************/ |
| 190 |
/***************** BUILD OPTIMIZATION PROBLEM ****************************/ |
| 191 |
/*************************************************************************/ |
| 192 |
ROL::Ptr<ROL::Problem<RealT>> |
| 193 |
opt = ROL::makePtr<ROL::Problem<RealT>>(obj,xp); |
| 194 |
opt->addBoundConstraint(bnd); |
| 195 |
opt->addConstraint("PDE",pdeWithDoping,pp); |
| 196 |
opt->finalize(false,true,*outStream); |
| 197 |
bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false); |
| 198 |
if ( checkDeriv ) opt->check(true,*outStream); |
| 199 |
|
| 200 |
/*************************************************************************/ |
| 201 |
/***************** SOLVE OPTIMIZATION PROBLEM ****************************/ |
| 202 |
/*************************************************************************/ |
| 203 |
ROL::Solver<RealT> solver(opt,*parlist); |
| 204 |
zp->set(*rzp); |
| 205 |
|
| 206 |
bool initSolve = parlist->sublist("Problem").get("Solve state for full space",true); |
| 207 |
if( initSolve ) pdeWithDoping->solve(*rp,*up,*zp,tol); |
| 208 |
|
| 209 |
std::clock_t timer = std::clock(); |
| 210 |
solver.solve(*outStream); |
| 211 |
*outStream << "Optimization time: " |
| 212 |
<< static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC) |
| 213 |
<< " seconds." << std::endl << std::endl; |
| 214 |
|
| 215 |
/*************************************************************************/ |
| 216 |
/***************** OUTPUT RESULTS ****************************************/ |
| 217 |
/*************************************************************************/ |
| 218 |
std::clock_t timer_print = std::clock(); |
| 219 |
// Output control to file |
| 220 |
pdeCon->outputTpetraVector(z_ptr,"control.txt"); |
| 221 |
// Output expected state and samples to file |
| 222 |
pdeCon->outputTpetraVector(u_ptr,"state.txt"); |
| 223 |
*outStream << "Output time: " |
| 224 |
<< static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC) |
| 225 |
<< " seconds." << std::endl << std::endl; |
| 226 |
} |
| 227 |
catch (std::logic_error& err) { |
| 228 |
*outStream << err.what() << "\n"; |
| 229 |
errorFlag = -1000; |
| 230 |
}; // end try |
| 231 |
|
| 232 |
if (errorFlag != 0) |
| 233 |
std::cout << "End Result: TEST FAILED\n"; |
| 234 |
else |
| 235 |
std::cout << "End Result: TEST PASSED\n"; |
| 236 |
|
| 237 |
return 0; |
| 238 |
} |