| 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 Navier-Stokes 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_TpetraMultiVector.hpp" |
| 26 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 27 |
#include "ROL_Solver.hpp" |
| 28 |
|
| 29 |
#include "../TOOLS/meshmanager.hpp" |
| 30 |
#include "../TOOLS/pdeconstraint.hpp" |
| 31 |
#include "../TOOLS/pdeobjective.hpp" |
| 32 |
#include "../TOOLS/pdevector.hpp" |
| 33 |
#include "pde_navier-stokes.hpp" |
| 34 |
#include "obj_navier-stokes.hpp" |
| 35 |
|
| 36 |
typedef double RealT; |
| 37 |
|
| 38 |
int main(int argc, char *argv[]) { |
| 39 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 40 |
int iprint = argc - 1; |
| 41 |
ROL::Ptr<std::ostream> outStream; |
| 42 |
ROL::nullstream bhs; // outputs nothing |
| 43 |
|
| 44 |
/*** Initialize communicator. ***/ |
| 45 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 46 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 47 |
= Tpetra::getDefaultComm(); |
| 48 |
const int myRank = comm->getRank(); |
| 49 |
if ((iprint > 0) && (myRank == 0)) { |
| 50 |
outStream = ROL::makePtrFromRef(std::cout); |
| 51 |
} |
| 52 |
else { |
| 53 |
outStream = ROL::makePtrFromRef(bhs); |
| 54 |
} |
| 55 |
int errorFlag = 0; |
| 56 |
|
| 57 |
// *** Example body. |
| 58 |
try { |
| 59 |
|
| 60 |
/*** Read in XML input ***/ |
| 61 |
std::string filename = "input.xml"; |
| 62 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 63 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 64 |
|
| 65 |
/*** Initialize main data structure. ***/ |
| 66 |
ROL::Ptr<MeshManager<RealT>> |
| 67 |
meshMgr = ROL::makePtr<MeshManager_BackwardFacingStepChannel<RealT>>(*parlist); |
| 68 |
// Initialize PDE describing Navier-Stokes equations. |
| 69 |
ROL::Ptr<PDE_NavierStokes<RealT>> |
| 70 |
pde = ROL::makePtr<PDE_NavierStokes<RealT>>(*parlist); |
| 71 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT>> |
| 72 |
con = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 73 |
// Cast the constraint and get the assembler. |
| 74 |
ROL::Ptr<PDE_Constraint<RealT>> |
| 75 |
pdecon = ROL::dynamicPtrCast<PDE_Constraint<RealT>>(con); |
| 76 |
ROL::Ptr<Assembler<RealT>> assembler = pdecon->getAssembler(); |
| 77 |
con->setSolveParameters(*parlist); |
| 78 |
|
| 79 |
// Create state vector and set to zeroes |
| 80 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr = assembler->createStateVector(); u_ptr->randomize(); |
| 81 |
ROL::Ptr<Tpetra::MultiVector<>> p_ptr = assembler->createStateVector(); p_ptr->randomize(); |
| 82 |
ROL::Ptr<Tpetra::MultiVector<>> z_ptr = assembler->createControlVector(); z_ptr->randomize(); |
| 83 |
ROL::Ptr<Tpetra::MultiVector<>> r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0); |
| 84 |
ROL::Ptr<ROL::Vector<RealT>> up, pp, zp, rp; |
| 85 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist); |
| 86 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist); |
| 87 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist); |
| 88 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist); |
| 89 |
// Create ROL SimOpt vectors |
| 90 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 91 |
|
| 92 |
// Initialize quadratic objective function. |
| 93 |
std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(2,ROL::nullPtr); |
| 94 |
qoi_vec[0] = ROL::makePtr<QoI_State_NavierStokes<RealT>>(*parlist, |
| 95 |
pde->getVelocityFE(), |
| 96 |
pde->getPressureFE(), |
| 97 |
pde->getFieldHelper()); |
| 98 |
qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_NavierStokes<RealT>>(pde->getVelocityFE(), |
| 99 |
pde->getPressureFE(), |
| 100 |
pde->getVelocityBdryFE(), |
| 101 |
pde->getBdryCellLocIds(), |
| 102 |
pde->getFieldHelper()); |
| 103 |
ROL::Ptr<StdObjective_NavierStokes<RealT>> |
| 104 |
std_obj = ROL::makePtr<StdObjective_NavierStokes<RealT>>(*parlist); |
| 105 |
ROL::Ptr<ROL::Objective_SimOpt<RealT>> |
| 106 |
obj = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler); |
| 107 |
ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT>> |
| 108 |
robj = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, pp, true, false); |
| 109 |
|
| 110 |
// Run derivative checks |
| 111 |
bool checkDeriv = parlist->sublist("Problem").get("Check derivatives",false); |
| 112 |
if ( checkDeriv ) { |
| 113 |
ROL::Ptr<ROL::Vector<RealT>> dup = up->clone(); dup->randomize(-1.0,1.0); |
| 114 |
ROL::Ptr<ROL::Vector<RealT>> dzp = zp->clone(); dzp->randomize(-1.0,1.0); |
| 115 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 116 |
obj->checkGradient(x,d,true,*outStream); |
| 117 |
obj->checkHessVec(x,d,true,*outStream); |
| 118 |
con->checkApplyJacobian_1(*up,*zp,*dup,*up,true,*outStream); |
| 119 |
con->checkApplyJacobian_2(*up,*zp,*dzp,*up,true,*outStream); |
| 120 |
con->checkApplyJacobian(x,d,*up,true,*outStream); |
| 121 |
con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream); |
| 122 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 123 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 124 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 125 |
robj->checkGradient(*zp,*dzp,true,*outStream); |
| 126 |
robj->checkHessVec(*zp,*dzp,true,*outStream); |
| 127 |
} |
| 128 |
|
| 129 |
up->zero(); zp->zero(); |
| 130 |
ROL::Ptr<ROL::Problem<RealT>> optProb; |
| 131 |
bool useFullSpace = parlist->sublist("Problem").get("Full space",false); |
| 132 |
if ( useFullSpace ) { |
| 133 |
std::string step = parlist->sublist("Step").get("Type", "Composite Step"); |
| 134 |
ROL::EStep els = ROL::StringToEStep(step); |
| 135 |
switch( els ) { |
| 136 |
case ROL::STEP_COMPOSITESTEP: |
| 137 |
case ROL::STEP_FLETCHER: { |
| 138 |
RealT tol(1.e-8); |
| 139 |
bool initSolve = parlist->sublist("Problem").get("Solve state for full space",true); |
| 140 |
if (initSolve) { |
| 141 |
con->solve(*rp,*up,*zp,tol); |
| 142 |
pdecon->outputTpetraVector(u_ptr,"state_uncontrolled.txt"); |
| 143 |
} |
| 144 |
optProb = ROL::makePtr<ROL::Problem<RealT>>(obj, makePtrFromRef(x)); |
| 145 |
optProb->addConstraint("PDE", con, rp); |
| 146 |
break; |
| 147 |
} |
| 148 |
default: { |
| 149 |
*outStream << "ERROR: Unsupported step." << std::endl; |
| 150 |
errorFlag = 1; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
else { |
| 155 |
parlist->sublist("Step").set("Type","Trust Region"); |
| 156 |
optProb = ROL::makePtr<ROL::Problem<RealT>>(robj, zp); |
| 157 |
} |
| 158 |
optProb->finalize(false,true,*outStream); |
| 159 |
ROL::Solver<RealT> optSolver(optProb, *parlist); |
| 160 |
optSolver.solve(*outStream); |
| 161 |
|
| 162 |
// Output. |
| 163 |
assembler->printMeshData(*outStream); |
| 164 |
RealT tol(1.e-8); |
| 165 |
Teuchos::Array<RealT> res(1,0); |
| 166 |
con->solve(*rp,*up,*zp,tol); |
| 167 |
pdecon->outputTpetraVector(u_ptr,"state.txt"); |
| 168 |
pdecon->outputTpetraVector(z_ptr,"control.txt"); |
| 169 |
con->value(*rp,*up,*zp,tol); |
| 170 |
r_ptr->norm2(res.view(0,1)); |
| 171 |
*outStream << "Residual Norm: " << res[0] << std::endl; |
| 172 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 173 |
//pdecon->outputTpetraData(); |
| 174 |
} |
| 175 |
catch (std::logic_error& err) { |
| 176 |
*outStream << err.what() << "\n"; |
| 177 |
errorFlag = -1000; |
| 178 |
}; // end try |
| 179 |
|
| 180 |
if (errorFlag != 0) |
| 181 |
std::cout << "End Result: TEST FAILED\n"; |
| 182 |
else |
| 183 |
std::cout << "End Result: TEST PASSED\n"; |
| 184 |
|
| 185 |
return 0; |
| 186 |
} |