| 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 "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_Algorithm.hpp" |
| 27 |
#include "ROL_TrustRegionStep.hpp" |
| 28 |
#include "ROL_StatusTest.hpp" |
| 29 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 30 |
#include "ROL_Bounds.hpp" |
| 31 |
|
| 32 |
#include "../TOOLS/meshmanager.hpp" |
| 33 |
#include "../TOOLS/pdeconstraint.hpp" |
| 34 |
#include "../TOOLS/pdeobjective.hpp" |
| 35 |
#include "../TOOLS/pdevector.hpp" |
| 36 |
|
| 37 |
#include "pde_nonlinear_elliptic.hpp" |
| 38 |
#include "obj_nonlinear_elliptic.hpp" |
| 39 |
|
| 40 |
typedef double RealT; |
| 41 |
|
| 42 |
int main(int argc, char *argv[]) { |
| 43 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 44 |
int iprint = argc - 1; |
| 45 |
ROL::Ptr<std::ostream> outStream; |
| 46 |
ROL::nullstream bhs; // outputs nothing |
| 47 |
|
| 48 |
/*** Initialize communicator. ***/ |
| 49 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 50 |
ROL::Ptr<const Teuchos::Comm<int> > comm |
| 51 |
= Tpetra::getDefaultComm(); |
| 52 |
const int myRank = comm->getRank(); |
| 53 |
if ((iprint > 0) && (myRank == 0)) { |
| 54 |
outStream = ROL::makePtrFromRef(std::cout); |
| 55 |
} |
| 56 |
else { |
| 57 |
outStream = ROL::makePtrFromRef(bhs); |
| 58 |
} |
| 59 |
int errorFlag = 0; |
| 60 |
|
| 61 |
// *** Example body. |
| 62 |
try { |
| 63 |
|
| 64 |
/*** Read in XML input ***/ |
| 65 |
std::string filename = "input.xml"; |
| 66 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 67 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 68 |
|
| 69 |
RealT controlPenalty = parlist->sublist("Problem").get("Control penalty parameter",static_cast<RealT>(1.e-4)); |
| 70 |
|
| 71 |
/*** Initialize main data structure. ***/ |
| 72 |
ROL::Ptr<MeshManager<RealT> > meshMgr |
| 73 |
= ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 74 |
// Initialize PDE describe Poisson's equation |
| 75 |
ROL::Ptr<PDE_Nonlinear_Elliptic<RealT> > pde |
| 76 |
= ROL::makePtr<PDE_Nonlinear_Elliptic<RealT>>(*parlist); |
| 77 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con |
| 78 |
= ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 79 |
ROL::Ptr<PDE_Constraint<RealT> > pdecon |
| 80 |
= ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con); |
| 81 |
ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler(); |
| 82 |
|
| 83 |
// Create state vector and set to zeroes |
| 84 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = assembler->createStateVector(); |
| 85 |
u_ptr->randomize(); |
| 86 |
ROL::Ptr<ROL::Vector<RealT> > up |
| 87 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler); |
| 88 |
// Create state vector and set to zeroes |
| 89 |
ROL::Ptr<Tpetra::MultiVector<> > p_ptr = assembler->createStateVector(); |
| 90 |
p_ptr->randomize(); |
| 91 |
ROL::Ptr<ROL::Vector<RealT> > pp |
| 92 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler); |
| 93 |
// Create control vector and set to ones |
| 94 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = assembler->createControlVector(); |
| 95 |
z_ptr->randomize(); |
| 96 |
ROL::Ptr<ROL::Vector<RealT> > zp |
| 97 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler); |
| 98 |
// Create residual vector and set to zeros |
| 99 |
ROL::Ptr<Tpetra::MultiVector<> > r_ptr = assembler->createResidualVector(); |
| 100 |
r_ptr->putScalar(0.0); |
| 101 |
ROL::Ptr<ROL::Vector<RealT> > rp |
| 102 |
= ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler); |
| 103 |
// Create state direction vector and set to random |
| 104 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = assembler->createStateVector(); |
| 105 |
du_ptr->randomize(); |
| 106 |
ROL::Ptr<ROL::Vector<RealT> > dup |
| 107 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler); |
| 108 |
// Create control direction vector and set to random |
| 109 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = assembler->createControlVector(); |
| 110 |
dz_ptr->randomize(); |
| 111 |
ROL::Ptr<ROL::Vector<RealT> > dzp |
| 112 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler); |
| 113 |
// Create ROL SimOpt vectors |
| 114 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 115 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 116 |
|
| 117 |
// Initialize bound constraints. |
| 118 |
ROL::Ptr<Tpetra::MultiVector<> > lo_ptr = assembler->createControlVector(); |
| 119 |
ROL::Ptr<Tpetra::MultiVector<> > hi_ptr = assembler->createControlVector(); |
| 120 |
lo_ptr->putScalar(0.0); hi_ptr->putScalar(1.0); |
| 121 |
ROL::Ptr<ROL::Vector<RealT> > lop |
| 122 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(lo_ptr,pde,assembler); |
| 123 |
ROL::Ptr<ROL::Vector<RealT> > hip |
| 124 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(hi_ptr,pde,assembler); |
| 125 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bnd |
| 126 |
= ROL::makePtr<ROL::Bounds<RealT>>(lop,hip); |
| 127 |
|
| 128 |
// Initialize quadratic objective function |
| 129 |
std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr); |
| 130 |
qoi_vec[0] = ROL::makePtr<QoI_StateTracking_Nonlinear_Elliptic<RealT>>(pde->getFE()); |
| 131 |
qoi_vec[1] = ROL::makePtr<QoI_ControlPenalty_Nonlinear_Elliptic<RealT>>(pde->getFE()); |
| 132 |
std::vector<RealT> weights = {static_cast<RealT>(1), controlPenalty}; |
| 133 |
ROL::Ptr<PDE_Objective<RealT> > obj |
| 134 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,weights,assembler); |
| 135 |
ROL::Ptr<ROL::Objective<RealT> > robj |
| 136 |
= ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj,con,up,zp,pp,true,false); |
| 137 |
|
| 138 |
// Run derivative checks |
| 139 |
obj->checkGradient(x,d,true,*outStream); |
| 140 |
obj->checkHessVec(x,d,true,*outStream); |
| 141 |
|
| 142 |
con->checkApplyJacobian(x,d,*rp,true,*outStream); |
| 143 |
con->checkApplyJacobian_1(*up,*zp,*dup,*rp,true,*outStream); |
| 144 |
con->checkApplyJacobian_2(*up,*zp,*dzp,*rp,true,*outStream); |
| 145 |
con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream); |
| 146 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 147 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 148 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 149 |
|
| 150 |
ROL::Ptr<ROL::Step<RealT>> |
| 151 |
step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist); |
| 152 |
ROL::Ptr<ROL::StatusTest<RealT>> |
| 153 |
status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist); |
| 154 |
ROL::Algorithm<RealT> algo(step,status,false); |
| 155 |
algo.run(*zp,*robj,*bnd,true,*outStream); |
| 156 |
|
| 157 |
// Output. |
| 158 |
assembler->printMeshData(*outStream); |
| 159 |
RealT tol(1.e-8); |
| 160 |
con->solve(*rp,*up,*zp,tol); |
| 161 |
pdecon->outputTpetraVector(u_ptr,"state.txt"); |
| 162 |
pdecon->outputTpetraVector(z_ptr,"control.txt"); |
| 163 |
|
| 164 |
Teuchos::Array<RealT> res(1,0); |
| 165 |
con->value(*rp,*up,*zp,tol); |
| 166 |
r_ptr->norm2(res.view(0,1)); |
| 167 |
*outStream << "Residual Norm: " << res[0] << std::endl; |
| 168 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 169 |
} |
| 170 |
catch (std::logic_error& err) { |
| 171 |
*outStream << err.what() << "\n"; |
| 172 |
errorFlag = -1000; |
| 173 |
}; // end try |
| 174 |
|
| 175 |
if (errorFlag != 0) |
| 176 |
std::cout << "End Result: TEST FAILED\n"; |
| 177 |
else |
| 178 |
std::cout << "End Result: TEST PASSED\n"; |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |