| 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 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_Algorithm.hpp" |
| 26 |
//#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 27 |
#include "ROL_KrylovFactory.hpp" |
| 28 |
//#include "ROL_BelosKrylov.hpp" |
| 29 |
|
| 30 |
#include "../../TOOLS/meshmanager.hpp" |
| 31 |
#include "../../TOOLS/linearpdeconstraint.hpp" |
| 32 |
//#include "../../TOOLS/pdeobjective.hpp" |
| 33 |
#include "../../TOOLS/pdevector.hpp" |
| 34 |
#include "pde_fractional_poisson.hpp" |
| 35 |
//#include "obj_fractional_poisson.hpp" |
| 36 |
#include "fractional_operator.hpp" |
| 37 |
#include "fractional_vector.hpp" |
| 38 |
|
| 39 |
typedef double RealT; |
| 40 |
|
| 41 |
template<class Real> |
| 42 |
void checkDerivatives(const ROL::Ptr<ROL::Constraint_SimOpt<Real> > & con, |
| 43 |
std::ostream & outStream = std::cout) { |
| 44 |
// Cast the constraint and get the assembler. |
| 45 |
ROL::Ptr<Linear_PDE_Constraint<Real> > pdecon |
| 46 |
= ROL::dynamicPtrCast<Linear_PDE_Constraint<Real> >(con); |
| 47 |
ROL::Ptr<Assembler<Real> > assembler = pdecon->getAssembler(); |
| 48 |
ROL::Ptr<PDE<Real> > pde = pdecon->getPDE(); |
| 49 |
// Create state vector and set to zeroes |
| 50 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = assembler->createStateVector(); |
| 51 |
u_ptr->randomize(); |
| 52 |
ROL::Ptr<ROL::Vector<Real> > up |
| 53 |
= ROL::makePtr<PDE_PrimalSimVector<Real>>(u_ptr,pde,assembler); |
| 54 |
// Create control vector and set to ones |
| 55 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = assembler->createControlVector(); |
| 56 |
z_ptr->randomize(); |
| 57 |
ROL::Ptr<ROL::Vector<Real> > zp |
| 58 |
= ROL::makePtr<PDE_PrimalOptVector<Real>>(z_ptr,pde,assembler); |
| 59 |
// Create residual vector and set to zeros |
| 60 |
ROL::Ptr<Tpetra::MultiVector<> > r_ptr = assembler->createResidualVector(); |
| 61 |
r_ptr->putScalar(0.0); |
| 62 |
ROL::Ptr<ROL::Vector<Real> > rp |
| 63 |
= ROL::makePtr<PDE_DualSimVector<Real>>(r_ptr,pde,assembler); |
| 64 |
// Create multiplier vector and set to zeros |
| 65 |
ROL::Ptr<Tpetra::MultiVector<> > p_ptr = assembler->createStateVector(); |
| 66 |
p_ptr->putScalar(0.0); |
| 67 |
ROL::Ptr<ROL::Vector<Real> > pp |
| 68 |
= ROL::makePtr<PDE_PrimalSimVector<Real>>(p_ptr,pde,assembler); |
| 69 |
// Create state direction vector and set to random |
| 70 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = assembler->createStateVector(); |
| 71 |
du_ptr->randomize(); |
| 72 |
ROL::Ptr<ROL::Vector<Real> > dup |
| 73 |
= ROL::makePtr<PDE_PrimalSimVector<Real>>(du_ptr,pde,assembler); |
| 74 |
// Create control direction vector and set to random |
| 75 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = assembler->createControlVector(); |
| 76 |
dz_ptr->randomize(); |
| 77 |
ROL::Ptr<ROL::Vector<Real> > dzp |
| 78 |
= ROL::makePtr<PDE_PrimalOptVector<Real>>(dz_ptr,pde,assembler); |
| 79 |
// Create ROL SimOpt vectors |
| 80 |
ROL::Vector_SimOpt<Real> x(up,zp); |
| 81 |
ROL::Vector_SimOpt<Real> d(dup,dzp); |
| 82 |
// Check derivatives. |
| 83 |
con->checkApplyJacobian(x,d,*up,true,outStream); |
| 84 |
con->checkApplyAdjointHessian(x,*dup,d,x,true,outStream); |
| 85 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,outStream); |
| 86 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,outStream); |
| 87 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,outStream); |
| 88 |
} |
| 89 |
|
| 90 |
int main(int argc, char *argv[]) { |
| 91 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 92 |
int iprint = argc - 1; |
| 93 |
ROL::Ptr<std::ostream> outStream; |
| 94 |
ROL::nullstream bhs; // outputs nothing |
| 95 |
|
| 96 |
/*** Initialize communicator. ***/ |
| 97 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 98 |
ROL::Ptr<const Teuchos::Comm<int> > comm |
| 99 |
= Tpetra::getDefaultComm(); |
| 100 |
const int myRank = comm->getRank(); |
| 101 |
if ((iprint > 0) && (myRank == 0)) { |
| 102 |
outStream = ROL::makePtrFromRef(std::cout); |
| 103 |
} |
| 104 |
else { |
| 105 |
outStream = ROL::makePtrFromRef(bhs); |
| 106 |
} |
| 107 |
int errorFlag = 0; |
| 108 |
|
| 109 |
// *** Example body. |
| 110 |
try { |
| 111 |
|
| 112 |
/*** Read in XML input ***/ |
| 113 |
std::string filename = "input.xml"; |
| 114 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 115 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 116 |
|
| 117 |
// Set parameters for cylidner mesh |
| 118 |
RealT s = parlist->sublist("Problem").get("Fractional Power",0.5); |
| 119 |
RealT gamma = (s == 0.5) ? static_cast<RealT>(1) |
| 120 |
: static_cast<RealT>(3)/(static_cast<RealT>(2)*s) + static_cast<RealT>(1e-3); |
| 121 |
int NX = parlist->sublist("Geometry").get("NX",20); |
| 122 |
int NY = parlist->sublist("Geometry").get("NY",20); |
| 123 |
RealT NT = static_cast<RealT>(NX*NY); |
| 124 |
RealT width = static_cast<RealT>(1) + std::log10(NT)/static_cast<RealT>(3); |
| 125 |
int NI = static_cast<int>(width * std::sqrt(NT)); |
| 126 |
parlist->sublist("Geometry").sublist("Cylinder").set("Grading Parameter", gamma); |
| 127 |
parlist->sublist("Geometry").sublist("Cylinder").set("NI", NI); |
| 128 |
parlist->sublist("Geometry").sublist("Cylinder").set("Height", width); |
| 129 |
|
| 130 |
*outStream << std::endl; |
| 131 |
*outStream << "Fractional Power: " << s << std::endl; |
| 132 |
*outStream << "Mesh Grading Parameter: " << gamma << std::endl; |
| 133 |
*outStream << "Cylinder Height: " << width << std::endl; |
| 134 |
*outStream << "Number of Intervals: " << NI << std::endl; |
| 135 |
*outStream << std::endl; |
| 136 |
|
| 137 |
/*** Initialize main data structure. ***/ |
| 138 |
// Initialize PDE describe Poisson's equation |
| 139 |
ROL::Ptr<MeshManager<RealT> > meshMgr_local |
| 140 |
= ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 141 |
ROL::Ptr<PDE_Fractional_Poisson_Local<RealT> > pde_local |
| 142 |
= ROL::makePtr<PDE_Fractional_Poisson_Local<RealT>>(*parlist); |
| 143 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con_local |
| 144 |
= ROL::makePtr<Linear_PDE_Constraint<RealT>>(pde_local,meshMgr_local,comm,*parlist,*outStream); |
| 145 |
// Initialize PDE describe Poisson's equation |
| 146 |
ROL::Ptr<MeshManager<RealT> > meshMgr_cylinder |
| 147 |
= ROL::makePtr<MeshManager_Fractional_Cylinder<RealT>>(*parlist); |
| 148 |
ROL::Ptr<PDE_Fractional_Poisson_Cylinder<RealT> > pde_cylinder |
| 149 |
= ROL::makePtr<PDE_Fractional_Poisson_Cylinder<RealT>>(*parlist); |
| 150 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con_cylinder |
| 151 |
= ROL::makePtr<Linear_PDE_Constraint<RealT>>(pde_cylinder,meshMgr_cylinder,comm,*parlist,*outStream); |
| 152 |
|
| 153 |
// Check derivatives. |
| 154 |
checkDerivatives<RealT>(con_cylinder, *outStream); |
| 155 |
checkDerivatives<RealT>(con_local, *outStream); |
| 156 |
|
| 157 |
// Build fractional problem. |
| 158 |
ROL::Ptr<ROL::LinearOperator<RealT> > A |
| 159 |
= ROL::makePtr<FractionalOperator<RealT>>(pde_local,meshMgr_local,pde_cylinder,meshMgr_cylinder,comm,*parlist,*outStream); |
| 160 |
ROL::Ptr<ROL::LinearOperator<RealT> > M |
| 161 |
= ROL::makePtr<FractionalPreconditioner<RealT>>(pde_local,meshMgr_local,pde_cylinder,meshMgr_cylinder,comm,*parlist,*outStream); |
| 162 |
FractionalVector<RealT> F(pde_local,meshMgr_local,pde_cylinder,meshMgr_cylinder,comm,*parlist,*outStream); |
| 163 |
const ROL::Ptr<const ROL::Vector<RealT> > Fvec = F.get(); |
| 164 |
|
| 165 |
// Test preconditioner |
| 166 |
RealT tol(1e-8); |
| 167 |
ROL::Ptr<ROL::Vector<RealT> > PFvec = Fvec->clone(); |
| 168 |
ROL::Ptr<ROL::Vector<RealT> > PPFvec = Fvec->clone(); |
| 169 |
M->applyInverse(*PFvec,*Fvec,tol); |
| 170 |
M->apply(*PPFvec,*PFvec,tol); |
| 171 |
PPFvec->axpy(static_cast<RealT>(-1),*Fvec); |
| 172 |
*outStream << "Preconditioner error: " << PPFvec->norm() << std::endl; |
| 173 |
|
| 174 |
// Build CG object. |
| 175 |
//ROL::Ptr<ROL::Krylov<RealT> > solver = ROL::makePtr<ROL::BelosKrylov<RealT>>(*parlist); |
| 176 |
ROL::Ptr<ROL::Krylov<RealT> > solver = ROL::KrylovFactory<RealT>(*parlist); |
| 177 |
|
| 178 |
// Run CG |
| 179 |
int iter(0), flag(0); |
| 180 |
ROL::Ptr<ROL::Vector<RealT> > Xvec = Fvec->clone(); |
| 181 |
solver->run(*Xvec,*A,*Fvec,*M,iter,flag); |
| 182 |
*outStream << "GMRES Iterations: " << iter << " GMRES Flag: " << flag << std::endl; |
| 183 |
|
| 184 |
ROL::Ptr<ROL::Vector<RealT> > Rvec = Fvec->clone(); |
| 185 |
A->apply(*Rvec,*Xvec,tol); |
| 186 |
Rvec->axpy(static_cast<RealT>(-1),*Fvec); |
| 187 |
*outStream << "GMRES Residual: " << Rvec->norm() << std::endl; |
| 188 |
|
| 189 |
// Print solution |
| 190 |
ROL::Ptr<Linear_PDE_Constraint<RealT> > pdecon |
| 191 |
= ROL::dynamicPtrCast<Linear_PDE_Constraint<RealT> >(con_local); |
| 192 |
pdecon->printMeshData(*outStream); |
| 193 |
ROL::Ptr<const Tpetra::MultiVector<> > Xptr |
| 194 |
= ROL::dynamicPtrCast<const ROL::TpetraMultiVector<RealT> >(Xvec)->getVector(); |
| 195 |
Teuchos::Array<size_t> col(1,0); |
| 196 |
pdecon->outputTpetraVector(Xptr->subView(col()),"state.txt"); |
| 197 |
|
| 198 |
// Get a summary from the time monitor. |
| 199 |
Teuchos::TimeMonitor::summarize(); |
| 200 |
} |
| 201 |
catch (std::logic_error& err) { |
| 202 |
*outStream << err.what() << "\n"; |
| 203 |
errorFlag = -1000; |
| 204 |
}; // end try |
| 205 |
|
| 206 |
if (errorFlag != 0) |
| 207 |
std::cout << "End Result: TEST FAILED\n"; |
| 208 |
else |
| 209 |
std::cout << "End Result: TEST PASSED\n"; |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |