| 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 stochastic Stefan-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 |
//#include <fenv.h> |
| 25 |
|
| 26 |
#include "ROL_Algorithm.hpp" |
| 27 |
#include "ROL_Bounds.hpp" |
| 28 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 29 |
#include "ROL_Solver.hpp" |
| 30 |
#include "ROL_BoundConstraint_SimOpt.hpp" |
| 31 |
|
| 32 |
#include "../TOOLS/meshmanager.hpp" |
| 33 |
#include "../TOOLS/pdeconstraint.hpp" |
| 34 |
#include "../TOOLS/pdeobjective.hpp" |
| 35 |
#include "../TOOLS/pdevector.hpp" |
| 36 |
#include "pde_navier-stokes.hpp" |
| 37 |
#include "obj_navier-stokes.hpp" |
| 38 |
|
| 39 |
typedef double RealT; |
| 40 |
|
| 41 |
template<class Real> |
| 42 |
Real random(const Teuchos::Comm<int> &comm, |
| 43 |
const Real a = -1, const Real b = 1) { |
| 44 |
Real val(0), u(0); |
| 45 |
if ( Teuchos::rank<int>(comm)==0 ) { |
| 46 |
u = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX); |
| 47 |
val = (b-a)*u + a; |
| 48 |
} |
| 49 |
Teuchos::broadcast<int,Real>(comm,0,1,&val); |
| 50 |
return val; |
| 51 |
} |
| 52 |
|
| 53 |
int main(int argc, char *argv[]) { |
| 54 |
//feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| 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 |
| 64 |
= Tpetra::getDefaultComm(); |
| 65 |
ROL::Ptr<const Teuchos::Comm<int> > serial_comm |
| 66 |
= ROL::makePtr<Teuchos::SerialComm<int>>(); |
| 67 |
const int myRank = comm->getRank(); |
| 68 |
if ((iprint > 0) && (myRank == 0)) { |
| 69 |
outStream = ROL::makePtrFromRef(std::cout); |
| 70 |
} |
| 71 |
else { |
| 72 |
outStream = ROL::makePtrFromRef(bhs); |
| 73 |
} |
| 74 |
int errorFlag = 0; |
| 75 |
|
| 76 |
// *** Example body. |
| 77 |
try { |
| 78 |
|
| 79 |
/*** Read in XML input ***/ |
| 80 |
std::string filename = "input_ex05.xml"; |
| 81 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 82 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 83 |
|
| 84 |
// Problem dimensions |
| 85 |
//const int stochDim = 2; |
| 86 |
//const RealT one(1); |
| 87 |
|
| 88 |
/*************************************************************************/ |
| 89 |
/***************** BUILD GOVERNING PDE ***********************************/ |
| 90 |
/*************************************************************************/ |
| 91 |
/*** Initialize main data structure. ***/ |
| 92 |
ROL::Ptr<MeshManager<RealT> > meshMgr |
| 93 |
= ROL::makePtr<MeshManager_BackwardFacingStepChannel<RealT>>(*parlist); |
| 94 |
// Initialize PDE describing advection-diffusion equation |
| 95 |
ROL::Ptr<PDE_NavierStokes<RealT> > pde |
| 96 |
= ROL::makePtr<PDE_NavierStokes<RealT>>(*parlist); |
| 97 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con |
| 98 |
= ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,serial_comm,*parlist,*outStream); |
| 99 |
// Cast the constraint and get the assembler. |
| 100 |
ROL::Ptr<PDE_Constraint<RealT> > pdecon |
| 101 |
= ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con); |
| 102 |
ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler(); |
| 103 |
con->setSolveParameters(*parlist); |
| 104 |
|
| 105 |
/*************************************************************************/ |
| 106 |
/***************** BUILD VECTORS *****************************************/ |
| 107 |
/*************************************************************************/ |
| 108 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = assembler->createStateVector(); |
| 109 |
ROL::Ptr<Tpetra::MultiVector<> > p_ptr = assembler->createStateVector(); |
| 110 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = assembler->createStateVector(); |
| 111 |
u_ptr->randomize(); //u_ptr->putScalar(static_cast<RealT>(1)); |
| 112 |
p_ptr->randomize(); //p_ptr->putScalar(static_cast<RealT>(1)); |
| 113 |
du_ptr->randomize(); //du_ptr->putScalar(static_cast<RealT>(0)); |
| 114 |
ROL::Ptr<ROL::Vector<RealT> > up |
| 115 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 116 |
ROL::Ptr<ROL::Vector<RealT> > pp |
| 117 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 118 |
ROL::Ptr<ROL::Vector<RealT> > dup |
| 119 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler,*parlist); |
| 120 |
// Create residual vectors |
| 121 |
ROL::Ptr<Tpetra::MultiVector<> > r_ptr = assembler->createResidualVector(); |
| 122 |
r_ptr->randomize(); //r_ptr->putScalar(static_cast<RealT>(1)); |
| 123 |
ROL::Ptr<ROL::Vector<RealT> > rp |
| 124 |
= ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 125 |
// Create control vector and set to ones |
| 126 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = assembler->createControlVector(); |
| 127 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = assembler->createControlVector(); |
| 128 |
ROL::Ptr<Tpetra::MultiVector<> > yz_ptr = assembler->createControlVector(); |
| 129 |
z_ptr->randomize(); z_ptr->putScalar(static_cast<RealT>(0)); |
| 130 |
dz_ptr->randomize(); //dz_ptr->putScalar(static_cast<RealT>(0)); |
| 131 |
yz_ptr->randomize(); //yz_ptr->putScalar(static_cast<RealT>(0)); |
| 132 |
ROL::Ptr<ROL::TpetraMultiVector<RealT> > zpde |
| 133 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 134 |
ROL::Ptr<ROL::TpetraMultiVector<RealT> > dzpde |
| 135 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler,*parlist); |
| 136 |
ROL::Ptr<ROL::TpetraMultiVector<RealT> > yzpde |
| 137 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(yz_ptr,pde,assembler,*parlist); |
| 138 |
ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtr<PDE_OptVector<RealT>>(zpde); |
| 139 |
ROL::Ptr<ROL::Vector<RealT> > dzp = ROL::makePtr<PDE_OptVector<RealT>>(dzpde); |
| 140 |
ROL::Ptr<ROL::Vector<RealT> > yzp = ROL::makePtr<PDE_OptVector<RealT>>(yzpde); |
| 141 |
// Create ROL SimOpt vectors |
| 142 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 143 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 144 |
|
| 145 |
/*************************************************************************/ |
| 146 |
/***************** BUILD COST FUNCTIONAL *********************************/ |
| 147 |
/*************************************************************************/ |
| 148 |
std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr); |
| 149 |
qoi_vec[0] = ROL::makePtr<QoI_State_NavierStokes<RealT>>(*parlist, |
| 150 |
pde->getVelocityFE(), |
| 151 |
pde->getPressureFE(), |
| 152 |
pde->getFieldHelper()); |
| 153 |
qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_NavierStokes<RealT>>(pde->getVelocityFE(), |
| 154 |
pde->getPressureFE(), |
| 155 |
pde->getVelocityBdryFE(), |
| 156 |
pde->getBdryCellLocIds(), |
| 157 |
pde->getFieldHelper()); |
| 158 |
ROL::Ptr<StdObjective_NavierStokes<RealT> > std_obj |
| 159 |
= ROL::makePtr<StdObjective_NavierStokes<RealT>>(*parlist); |
| 160 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj |
| 161 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler); |
| 162 |
|
| 163 |
/*************************************************************************/ |
| 164 |
/***************** BUILD BOUND CONSTRAINT ********************************/ |
| 165 |
/*************************************************************************/ |
| 166 |
ROL::Ptr<Tpetra::MultiVector<> > zlo_ptr = assembler->createControlVector(); |
| 167 |
ROL::Ptr<Tpetra::MultiVector<> > zhi_ptr = assembler->createControlVector(); |
| 168 |
zlo_ptr->putScalar(static_cast<RealT>(0)); |
| 169 |
zhi_ptr->putScalar(ROL::ROL_INF<RealT>()); |
| 170 |
ROL::Ptr<ROL::TpetraMultiVector<RealT> > zlopde |
| 171 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(zlo_ptr,pde,assembler,*parlist); |
| 172 |
ROL::Ptr<ROL::TpetraMultiVector<RealT> > zhipde |
| 173 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(zhi_ptr,pde,assembler,*parlist); |
| 174 |
ROL::Ptr<ROL::Vector<RealT> > zlop = ROL::makePtr<PDE_OptVector<RealT>>(zlopde); |
| 175 |
ROL::Ptr<ROL::Vector<RealT> > zhip = ROL::makePtr<PDE_OptVector<RealT>>(zhipde); |
| 176 |
ROL::Ptr<ROL::BoundConstraint<RealT> > zbnd |
| 177 |
= ROL::makePtr<ROL::Bounds<RealT>>(zlop,zhip); |
| 178 |
bool useBounds = parlist->sublist("Problem").get("Use bounds", false); |
| 179 |
if (!useBounds) zbnd->deactivate(); |
| 180 |
// State bounds |
| 181 |
ROL::Ptr<Tpetra::MultiVector<> > ulo_ptr = assembler->createStateVector(); |
| 182 |
ROL::Ptr<Tpetra::MultiVector<> > uhi_ptr = assembler->createStateVector(); |
| 183 |
ulo_ptr->putScalar(ROL::ROL_NINF<RealT>()); uhi_ptr->putScalar(ROL::ROL_INF<RealT>()); |
| 184 |
ROL::Ptr<ROL::Vector<RealT> > ulop |
| 185 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(ulo_ptr,pde,assembler); |
| 186 |
ROL::Ptr<ROL::Vector<RealT> > uhip |
| 187 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(uhi_ptr,pde,assembler); |
| 188 |
ROL::Ptr<ROL::BoundConstraint<RealT> > ubnd |
| 189 |
= ROL::makePtr<ROL::Bounds<RealT>>(ulop,uhip); |
| 190 |
ubnd->deactivate(); |
| 191 |
|
| 192 |
// SimOpt bounds |
| 193 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bnd |
| 194 |
= ROL::makePtr<ROL::BoundConstraint_SimOpt<RealT>>(ubnd,zbnd); |
| 195 |
|
| 196 |
|
| 197 |
/*************************************************************************/ |
| 198 |
/***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/ |
| 199 |
/*************************************************************************/ |
| 200 |
bool checkDeriv = parlist->sublist("Problem").get("Check derivatives",false); |
| 201 |
if ( checkDeriv ) { |
| 202 |
up->checkVector(*pp,*dup,true,*outStream); |
| 203 |
zp->checkVector(*yzp,*dzp,true,*outStream); |
| 204 |
obj->checkGradient(x,d,true,*outStream); |
| 205 |
obj->checkHessVec(x,d,true,*outStream); |
| 206 |
con->checkApplyJacobian(x,d,*up,true,*outStream); |
| 207 |
con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream); |
| 208 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 209 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 210 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 211 |
} |
| 212 |
|
| 213 |
/*************************************************************************/ |
| 214 |
/***************** SOLVE OPTIMIZATION PROBLEM ****************************/ |
| 215 |
/*************************************************************************/ |
| 216 |
RealT tol(1.e-8); |
| 217 |
up->setScalar(RealT(1)); |
| 218 |
zp->randomize();//->setScalar(RealT(1)); |
| 219 |
con->solve(*rp,*up,*zp,tol); |
| 220 |
ROL::Ptr<ROL::Problem<RealT>> |
| 221 |
optProb = ROL::makePtr<ROL::Problem<RealT>>(obj, makePtrFromRef(x)); |
| 222 |
optProb->addBoundConstraint(bnd); |
| 223 |
optProb->addConstraint("PDE", con, pp); |
| 224 |
optProb->finalize(false,true,*outStream); |
| 225 |
ROL::Solver<RealT> optSolver(optProb, *parlist); |
| 226 |
optSolver.solve(*outStream); |
| 227 |
std::clock_t timer = std::clock(); |
| 228 |
*outStream << "Optimization time: " |
| 229 |
<< static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC) |
| 230 |
<< " seconds." << std::endl << std::endl; |
| 231 |
|
| 232 |
Teuchos::Array<RealT> res(1,0); |
| 233 |
con->solve(*rp,*up,*zp,tol); |
| 234 |
r_ptr->norm2(res.view(0,1)); |
| 235 |
|
| 236 |
/*************************************************************************/ |
| 237 |
/***************** CHECK RESIDUAL NORM ***********************************/ |
| 238 |
/*************************************************************************/ |
| 239 |
*outStream << "Residual Norm: " << res[0] << std::endl << std::endl; |
| 240 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 241 |
} |
| 242 |
catch (std::logic_error& err) { |
| 243 |
*outStream << err.what() << "\n"; |
| 244 |
errorFlag = -1000; |
| 245 |
}; // end try |
| 246 |
|
| 247 |
if (errorFlag != 0) |
| 248 |
std::cout << "End Result: TEST FAILED\n"; |
| 249 |
else |
| 250 |
std::cout << "End Result: TEST PASSED\n"; |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |