| 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 "../TOOLS/meshmanager.hpp" |
| 26 |
#include "../TOOLS/pdeconstraint.hpp" |
| 27 |
#include "../TOOLS/pdeobjective.hpp" |
| 28 |
#include "../TOOLS/pdevector.hpp" |
| 29 |
#include "pde_poisson_boltzmann.hpp" |
| 30 |
#include "obj_poisson_boltzmann.hpp" |
| 31 |
|
| 32 |
#include "ROL_Solver.hpp" |
| 33 |
|
| 34 |
typedef double RealT; |
| 35 |
|
| 36 |
int main(int argc, char *argv[]) { |
| 37 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 38 |
int iprint = argc - 1; |
| 39 |
ROL::Ptr<std::ostream> outStream; |
| 40 |
ROL::nullstream bhs; // outputs nothing |
| 41 |
|
| 42 |
/*** Initialize communicator. ***/ |
| 43 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 44 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 45 |
= Tpetra::getDefaultComm(); |
| 46 |
const int myRank = comm->getRank(); |
| 47 |
if ((iprint > 0) && (myRank == 0)) { |
| 48 |
outStream = ROL::makePtrFromRef(std::cout); |
| 49 |
} |
| 50 |
else { |
| 51 |
outStream = ROL::makePtrFromRef(bhs); |
| 52 |
} |
| 53 |
int errorFlag = 0; |
| 54 |
|
| 55 |
// *** Example body. |
| 56 |
try { |
| 57 |
|
| 58 |
/*** Read in XML input ***/ |
| 59 |
std::string filename = "input.xml"; |
| 60 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 61 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 62 |
|
| 63 |
/*** Initialize main data structure. ***/ |
| 64 |
ROL::Ptr<MeshManager<RealT>> |
| 65 |
meshMgr = ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 66 |
// Initialize PDE describe Poisson's equation |
| 67 |
ROL::Ptr<PDE_Poisson_Boltzmann<RealT>> |
| 68 |
pde = ROL::makePtr<PDE_Poisson_Boltzmann<RealT>>(*parlist); |
| 69 |
ROL::Ptr<PDE_Constraint<RealT>> |
| 70 |
con = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 71 |
// Initialize quadratic objective function |
| 72 |
std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(2,ROL::nullPtr); |
| 73 |
qoi_vec[0] = ROL::makePtr<QoI_L2Tracking_Poisson_Boltzmann<RealT>>(pde->getFE()); |
| 74 |
qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_Poisson_Boltzmann<RealT>>(pde->getFE()); |
| 75 |
ROL::Ptr<StdObjective_Poisson_Boltzmann<RealT>> |
| 76 |
std_obj = ROL::makePtr<StdObjective_Poisson_Boltzmann<RealT>>(*parlist); |
| 77 |
ROL::Ptr<PDE_Objective<RealT>> |
| 78 |
obj = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,con->getAssembler()); |
| 79 |
|
| 80 |
// Create vectors |
| 81 |
ROL::Ptr<Tpetra::MultiVector<>> u_ptr = con->getAssembler()->createStateVector(); |
| 82 |
ROL::Ptr<Tpetra::MultiVector<>> z_ptr = con->getAssembler()->createControlVector(); |
| 83 |
ROL::Ptr<Tpetra::MultiVector<>> p_ptr = con->getAssembler()->createStateVector(); |
| 84 |
ROL::Ptr<Tpetra::MultiVector<>> r_ptr = con->getAssembler()->createResidualVector(); |
| 85 |
ROL::Ptr<Tpetra::MultiVector<>> du_ptr = con->getAssembler()->createStateVector(); |
| 86 |
ROL::Ptr<Tpetra::MultiVector<>> dz_ptr = con->getAssembler()->createControlVector(); |
| 87 |
u_ptr->randomize(); |
| 88 |
z_ptr->putScalar(1.0); |
| 89 |
p_ptr->putScalar(0.0); |
| 90 |
r_ptr->putScalar(0.0); |
| 91 |
du_ptr->randomize(); |
| 92 |
dz_ptr->putScalar(0.0); |
| 93 |
ROL::Ptr<ROL::Vector<RealT>> up, zp, pp, rp, dup, dzp; |
| 94 |
up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,con->getAssembler()); |
| 95 |
zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,con->getAssembler()); |
| 96 |
pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,con->getAssembler()); |
| 97 |
rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,con->getAssembler()); |
| 98 |
dup = ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,con->getAssembler()); |
| 99 |
dzp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,con->getAssembler()); |
| 100 |
// Create ROL SimOpt vectors |
| 101 |
ROL::Ptr<ROL::Vector_SimOpt<RealT>> x, d; |
| 102 |
x = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(up,zp); |
| 103 |
d = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(dup,dzp); |
| 104 |
|
| 105 |
// Run derivative checks |
| 106 |
obj->checkGradient(*x,*d,true,*outStream); |
| 107 |
obj->checkHessVec(*x,*d,true,*outStream); |
| 108 |
con->checkApplyJacobian(*x,*d,*up,true,*outStream); |
| 109 |
con->checkApplyAdjointHessian(*x,*dup,*d,*x,true,*outStream); |
| 110 |
con->checkAdjointConsistencyJacobian(*dup,*d,*x,true,*outStream); |
| 111 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 112 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 113 |
|
| 114 |
RealT tol(1.e-8); |
| 115 |
con->solve(*rp,*up,*zp,tol); |
| 116 |
ROL::Ptr<ROL::Problem<RealT>> |
| 117 |
problem = ROL::makePtr<ROL::Problem<RealT>>(obj,x); |
| 118 |
problem->addConstraint("PDE",con,pp); |
| 119 |
problem->finalize(false,true,*outStream); |
| 120 |
ROL::Solver<RealT> solver(problem,*parlist); |
| 121 |
solver.solve(*outStream); |
| 122 |
|
| 123 |
// Output. |
| 124 |
con->getAssembler()->printMeshData(*outStream); |
| 125 |
con->solve(*rp,*up,*zp,tol); |
| 126 |
con->outputTpetraVector(u_ptr,"state.txt"); |
| 127 |
con->outputTpetraVector(z_ptr,"control.txt"); |
| 128 |
|
| 129 |
Teuchos::Array<RealT> res(1,0); |
| 130 |
con->value(*rp,*up,*zp,tol); |
| 131 |
r_ptr->norm2(res.view(0,1)); |
| 132 |
*outStream << "Residual Norm: " << res[0] << std::endl; |
| 133 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 134 |
} |
| 135 |
catch (std::logic_error& err) { |
| 136 |
*outStream << err.what() << "\n"; |
| 137 |
errorFlag = -1000; |
| 138 |
}; // end try |
| 139 |
|
| 140 |
if (errorFlag != 0) |
| 141 |
std::cout << "End Result: TEST FAILED\n"; |
| 142 |
else |
| 143 |
std::cout << "End Result: TEST PASSED\n"; |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |