| 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 "Teuchos_GlobalMPISession.hpp" |
| 16 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 17 |
|
| 18 |
#include "Tpetra_Core.hpp" |
| 19 |
#include "Tpetra_Version.hpp" |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
#include "ROL_Stream.hpp" |
| 25 |
#include "ROL_OptimizationSolver.hpp" |
| 26 |
#include "ROL_TpetraMultiVector.hpp" |
| 27 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 28 |
#include "ROL_Bounds.hpp" |
| 29 |
#include "ROL_MonteCarloGenerator.hpp" |
| 30 |
#include "ROL_TpetraTeuchosBatchManager.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 |
ROL::Ptr<const Teuchos::Comm<int> > serial_comm |
| 53 |
= ROL::makePtr<Teuchos::SerialComm<int>>(); |
| 54 |
const int myRank = comm->getRank(); |
| 55 |
if ((iprint > 0) && (myRank == 0)) { |
| 56 |
outStream = ROL::makePtrFromRef(std::cout); |
| 57 |
} |
| 58 |
else { |
| 59 |
outStream = ROL::makePtrFromRef(bhs); |
| 60 |
} |
| 61 |
int errorFlag = 0; |
| 62 |
|
| 63 |
// *** Example body. |
| 64 |
try { |
| 65 |
|
| 66 |
/*** Read in XML input ***/ |
| 67 |
std::string filename = "input.xml"; |
| 68 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 69 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 70 |
|
| 71 |
RealT controlPenalty = parlist->sublist("Problem").get("Control penalty parameter",static_cast<RealT>(1.e-4)); |
| 72 |
|
| 73 |
/*** Initialize main data structure. ***/ |
| 74 |
ROL::Ptr<MeshManager<RealT> > meshMgr |
| 75 |
= ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 76 |
// Initialize PDE describe Poisson's equation |
| 77 |
ROL::Ptr<PDE_Nonlinear_Elliptic<RealT> > pde |
| 78 |
= ROL::makePtr<PDE_Nonlinear_Elliptic<RealT>>(*parlist); |
| 79 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con |
| 80 |
= ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,serial_comm,*parlist,*outStream); |
| 81 |
ROL::Ptr<PDE_Constraint<RealT> > pdecon |
| 82 |
= ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con); |
| 83 |
ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler(); |
| 84 |
con->setSolveParameters(*parlist); |
| 85 |
|
| 86 |
// Create state vector and set to zeroes |
| 87 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = assembler->createStateVector(); |
| 88 |
u_ptr->randomize(); |
| 89 |
ROL::Ptr<ROL::Vector<RealT> > up |
| 90 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler); |
| 91 |
// Create state vector and set to zeroes |
| 92 |
ROL::Ptr<Tpetra::MultiVector<> > p_ptr = assembler->createStateVector(); |
| 93 |
p_ptr->randomize(); |
| 94 |
ROL::Ptr<ROL::Vector<RealT> > pp |
| 95 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler); |
| 96 |
// Create control vector and set to ones |
| 97 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = assembler->createControlVector(); |
| 98 |
z_ptr->randomize(); |
| 99 |
ROL::Ptr<ROL::Vector<RealT> > zp |
| 100 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler); |
| 101 |
// Create residual vector and set to zeros |
| 102 |
ROL::Ptr<Tpetra::MultiVector<> > r_ptr = assembler->createResidualVector(); |
| 103 |
r_ptr->putScalar(0.0); |
| 104 |
ROL::Ptr<ROL::Vector<RealT> > rp |
| 105 |
= ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler); |
| 106 |
// Create state direction vector and set to random |
| 107 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = assembler->createStateVector(); |
| 108 |
du_ptr->randomize(); |
| 109 |
ROL::Ptr<ROL::Vector<RealT> > dup |
| 110 |
= ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler); |
| 111 |
// Create control direction vector and set to random |
| 112 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = assembler->createControlVector(); |
| 113 |
dz_ptr->randomize(); |
| 114 |
ROL::Ptr<ROL::Vector<RealT> > dzp |
| 115 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler); |
| 116 |
// Create ROL SimOpt vectors |
| 117 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 118 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 119 |
|
| 120 |
// Initialize bound constraints. |
| 121 |
ROL::Ptr<Tpetra::MultiVector<> > lo_ptr = assembler->createControlVector(); |
| 122 |
ROL::Ptr<Tpetra::MultiVector<> > hi_ptr = assembler->createControlVector(); |
| 123 |
lo_ptr->putScalar(0.0); hi_ptr->putScalar(1.0); |
| 124 |
ROL::Ptr<ROL::Vector<RealT> > lop |
| 125 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(lo_ptr,pde,assembler); |
| 126 |
ROL::Ptr<ROL::Vector<RealT> > hip |
| 127 |
= ROL::makePtr<PDE_PrimalOptVector<RealT>>(hi_ptr,pde,assembler); |
| 128 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bnd |
| 129 |
= ROL::makePtr<ROL::Bounds<RealT>>(lop,hip); |
| 130 |
|
| 131 |
// Initialize quadratic objective function |
| 132 |
std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr); |
| 133 |
qoi_vec[0] = ROL::makePtr<QoI_StateTracking_Nonlinear_Elliptic<RealT>>(pde->getFE()); |
| 134 |
qoi_vec[1] = ROL::makePtr<QoI_ControlPenalty_Nonlinear_Elliptic<RealT>>(pde->getFE()); |
| 135 |
std::vector<RealT> weights = {static_cast<RealT>(1), controlPenalty}; |
| 136 |
ROL::Ptr<PDE_Objective<RealT> > obj |
| 137 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,weights,assembler); |
| 138 |
ROL::Ptr<ROL::Objective<RealT> > robj |
| 139 |
= ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj,con,up,zp,pp,true,false); |
| 140 |
|
| 141 |
/*************************************************************************/ |
| 142 |
/***************** BUILD SAMPLER *****************************************/ |
| 143 |
/*************************************************************************/ |
| 144 |
int stochDim = 20; |
| 145 |
int nsamp = parlist->sublist("Problem").get("Number of samples",100); |
| 146 |
std::vector<RealT> tmp = {static_cast<RealT>(-1),static_cast<RealT>(1)}; |
| 147 |
std::vector<std::vector<RealT> > bounds(stochDim,tmp); |
| 148 |
ROL::Ptr<ROL::BatchManager<RealT> > bman |
| 149 |
= ROL::makePtr<ROL::TpetraTeuchosBatchManager<RealT>>(comm); |
| 150 |
ROL::Ptr<ROL::SampleGenerator<RealT> > sampler |
| 151 |
= ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp,bounds,bman); |
| 152 |
|
| 153 |
/*************************************************************************/ |
| 154 |
/***************** BUILD STOCHASTIC PROBLEM ******************************/ |
| 155 |
/*************************************************************************/ |
| 156 |
zp->zero(); |
| 157 |
ROL::OptimizationProblem<RealT> opt(robj,zp,bnd); |
| 158 |
parlist->sublist("SOL").set("Initial Statistic", static_cast<RealT>(1)); |
| 159 |
opt.setStochasticObjective(*parlist,sampler); |
| 160 |
|
| 161 |
parlist->sublist("Step").set("Type","Trust Region"); |
| 162 |
ROL::OptimizationSolver<RealT> solver(opt,*parlist); |
| 163 |
solver.solve(*outStream); |
| 164 |
|
| 165 |
// Output. |
| 166 |
assembler->printMeshData(*outStream); |
| 167 |
RealT tol(1.e-8); |
| 168 |
con->solve(*rp,*up,*zp,tol); |
| 169 |
pdecon->outputTpetraVector(u_ptr,"state.txt"); |
| 170 |
pdecon->outputTpetraVector(z_ptr,"control.txt"); |
| 171 |
|
| 172 |
Teuchos::Array<RealT> res(1,0); |
| 173 |
con->value(*rp,*up,*zp,tol); |
| 174 |
r_ptr->norm2(res.view(0,1)); |
| 175 |
*outStream << "Residual Norm: " << res[0] << std::endl; |
| 176 |
errorFlag += (res[0] > 1.e-6 ? 1 : 0); |
| 177 |
} |
| 178 |
catch (std::logic_error& err) { |
| 179 |
*outStream << err.what() << "\n"; |
| 180 |
errorFlag = -1000; |
| 181 |
}; // end try |
| 182 |
|
| 183 |
if (errorFlag != 0) |
| 184 |
std::cout << "End Result: TEST FAILED\n"; |
| 185 |
else |
| 186 |
std::cout << "End Result: TEST PASSED\n"; |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |