| 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 a semilinear parabolic problem. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "Teuchos_Comm.hpp" |
| 15 |
#include "Teuchos_GlobalMPISession.hpp" |
| 16 |
#include "Teuchos_FancyOStream.hpp" |
| 17 |
|
| 18 |
#include "Tpetra_Core.hpp" |
| 19 |
#include "Tpetra_Version.hpp" |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include <algorithm> |
| 23 |
#include <numeric> |
| 24 |
|
| 25 |
#include <sys/types.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include "ROL_Bounds.hpp" |
| 29 |
#include "ROL_Stream.hpp" |
| 30 |
#include "ROL_ParameterList.hpp" |
| 31 |
#include "ROL_OptimizationSolver.hpp" |
| 32 |
#include "ROL_ReducedDynamicObjective.hpp" |
| 33 |
#include "ROL_RandomVector.hpp" |
| 34 |
#include "ROL_DynamicConstraintCheck.hpp" |
| 35 |
#include "ROL_DynamicObjectiveCheck.hpp" |
| 36 |
#include "ROL_PinTConstraint.hpp" |
| 37 |
#include "ROL_PinTVectorCommunication.hpp" |
| 38 |
#include "ROL_PinTVectorCommunication_Tpetra.hpp" |
| 39 |
#include "ROL_PinTVectorCommunication_StdTpetraComposite.hpp" |
| 40 |
|
| 41 |
#include "../../TOOLS/dynconstraint.hpp" |
| 42 |
#include "../../TOOLS/pdeobjective.hpp" |
| 43 |
#include "../../TOOLS/ltiobjective.hpp" |
| 44 |
#include "../../TOOLS/meshmanager.hpp" |
| 45 |
#include "../../TOOLS/pdevector.hpp" |
| 46 |
#include "dynpde_semilinear.hpp" |
| 47 |
#include "obj_semilinear.hpp" |
| 48 |
|
| 49 |
using RealT = double; |
| 50 |
|
| 51 |
class KKTOperator : public ROL::LinearOperator<RealT> { |
| 52 |
public: |
| 53 |
ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con; |
| 54 |
ROL::Ptr<ROL::Vector<RealT>> state; |
| 55 |
ROL::Ptr<ROL::Vector<RealT>> control; |
| 56 |
int myRank; |
| 57 |
ROL::Ptr<std::ostream> outStream; |
| 58 |
|
| 59 |
void apply( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override |
| 60 |
{ |
| 61 |
pint_con->applyAugmentedKKT(Hv,v,*state,*control,tol); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
class MGRITKKTOperator : public ROL::LinearOperator<RealT> { |
| 66 |
public: |
| 67 |
ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con; |
| 68 |
ROL::Ptr<ROL::Vector<RealT>> state; |
| 69 |
ROL::Ptr<ROL::Vector<RealT>> control; |
| 70 |
int myRank; |
| 71 |
ROL::Ptr<std::ostream> outStream; |
| 72 |
|
| 73 |
void apply( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override |
| 74 |
{ |
| 75 |
assert(false); |
| 76 |
pint_con->applyMultigridAugmentedKKT(Hv,v,*state,*control,tol); |
| 77 |
} |
| 78 |
void applyInverse( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override |
| 79 |
{ |
| 80 |
pint_con->applyMultigridAugmentedKKT(Hv,v,*state,*control,tol); |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
class WathenKKTOperator : public ROL::LinearOperator<RealT> { |
| 85 |
public: |
| 86 |
ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con; |
| 87 |
ROL::Ptr<ROL::Vector<RealT>> state; |
| 88 |
ROL::Ptr<ROL::Vector<RealT>> control; |
| 89 |
int myRank; |
| 90 |
ROL::Ptr<std::ostream> outStream; |
| 91 |
|
| 92 |
void apply( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override |
| 93 |
{ |
| 94 |
assert(false); |
| 95 |
} |
| 96 |
void applyInverse( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override |
| 97 |
{ |
| 98 |
pint_con->applyWathenInverse(Hv,v,*state,*control,tol,false,0); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
inline void pauseToAttach(MPI_Comm mpicomm) |
| 103 |
{ |
| 104 |
Teuchos::RCP<Teuchos::Comm<int> > comm = |
| 105 |
Teuchos::createMpiComm<int>(Teuchos::rcp(new Teuchos::OpaqueWrapper<MPI_Comm>(mpicomm))); |
| 106 |
Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout)); |
| 107 |
out.setShowProcRank(true); |
| 108 |
out.setOutputToRootOnly(-1); |
| 109 |
|
| 110 |
comm->barrier(); |
| 111 |
|
| 112 |
// try to get them to print out all at once |
| 113 |
out << "PID = " << getpid() << std::endl; |
| 114 |
|
| 115 |
if (comm->getRank() == 0) |
| 116 |
getchar(); |
| 117 |
comm->barrier(); |
| 118 |
} |
| 119 |
|
| 120 |
ROL::Ptr<ROL::Vector<RealT>> |
| 121 |
solveKKTSystem(const std::string & prefix, |
| 122 |
bool useWathenPrec,int myRank, |
| 123 |
double absTol,double relTol, |
| 124 |
const Teuchos::RCP<Teuchos::StackedTimer> & timer, |
| 125 |
const ROL::Ptr<std::ostream> & outStream, |
| 126 |
const ROL::Ptr<ROL::PinTConstraint<RealT>> & pint_con, |
| 127 |
const ROL::Ptr<ROL::Vector<RealT>> & kkt_b, |
| 128 |
const ROL::Ptr<ROL::PinTVector<RealT>> & state, |
| 129 |
const ROL::Ptr<ROL::PinTVector<RealT>> & control); |
| 130 |
|
| 131 |
int main(int argc, char *argv[]) |
| 132 |
{ |
| 133 |
|
| 134 |
using Teuchos::RCP; |
| 135 |
|
| 136 |
/*** Initialize communicator. ***/ |
| 137 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 138 |
|
| 139 |
// pauseToAttach(MPI_COMM_WORLD); |
| 140 |
|
| 141 |
ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 142 |
|
| 143 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 144 |
const int myRank = comm->getRank(); |
| 145 |
ROL::Ptr<std::ostream> outStream = ROL::makeStreamPtr( std::cout, (argc > 1) && (myRank==0) ); |
| 146 |
|
| 147 |
int errorFlag = 0; |
| 148 |
|
| 149 |
// *** Example body. |
| 150 |
try { |
| 151 |
|
| 152 |
/*** Read in XML input ***/ |
| 153 |
ROL::Ptr<ROL::ParameterList> parlist = ROL::getParametersFromXmlFile("input_ex01.xml"); |
| 154 |
int nt = parlist->sublist("Time Discretization").get("Number of Time Steps", 100); |
| 155 |
RealT T = parlist->sublist("Time Discretization").get("End Time", 1.0); |
| 156 |
RealT dt = T/static_cast<RealT>(nt); |
| 157 |
|
| 158 |
const Teuchos::ParameterList & geomlist = parlist->sublist("Geometry"); |
| 159 |
RealT dx = geomlist.get<double>("Width") / geomlist.get<int>("NX"); |
| 160 |
RealT dy = geomlist.get<double>("Height") / geomlist.get<int>("NY"); |
| 161 |
|
| 162 |
// Add MGRIT parameter list stuff |
| 163 |
int sweeps = parlist->get("MGRIT Sweeps", 1); |
| 164 |
RealT omega = parlist->get("MGRIT Relaxation",2.0/3.0); |
| 165 |
int coarseSweeps = parlist->get("MGRIT Coarse Sweeps", 1); |
| 166 |
RealT coarseOmega = parlist->get("MGRIT Coarse Relaxation",1.0); |
| 167 |
int numLevels = parlist->get("MGRIT Levels",3); |
| 168 |
double relTol = parlist->get("MGRIT Krylov Relative Tolerance",1e-4); |
| 169 |
double absTol = parlist->get("MGRIT Krylov Absolute Tolerance",1e-4); |
| 170 |
int spaceProc = parlist->get("MGRIT Spatial Procs",1); |
| 171 |
double globalScale = parlist->get("MGRIT Global Scale",1.0); |
| 172 |
bool rebalance = parlist->get("MGRIT Rebalance",false); |
| 173 |
int cgIterations = parlist->get("MGRIT CG Iterations",1); |
| 174 |
double cntrlRegPar = parlist->get("MGRIT Control Regularization Parameter",1.0); |
| 175 |
|
| 176 |
ROL::Ptr<const ROL::PinTCommunicators> communicators = ROL::makePtr<ROL::PinTCommunicators>(MPI_COMM_WORLD,spaceProc); |
| 177 |
ROL::Ptr<const Teuchos::Comm<int>> mpiSpaceComm = ROL::makePtr<Teuchos::MpiComm<int>>(communicators->getSpaceCommunicator()); |
| 178 |
|
| 179 |
// for "serial" in time cases, use the wathen preconditioner |
| 180 |
bool useWathenPrec = communicators->getTimeSize()==spaceProc; |
| 181 |
|
| 182 |
/*************************************************************************/ |
| 183 |
/***************** BUILD GOVERNING PDE ***********************************/ |
| 184 |
/*************************************************************************/ |
| 185 |
/*** Initialize mesh data structure. ***/ |
| 186 |
ROL::Ptr<MeshManager<RealT>> meshMgr |
| 187 |
= ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 188 |
// Initialize PDE describe semilinear equation |
| 189 |
ROL::Ptr<DynamicPDE_Semilinear<RealT>> pde |
| 190 |
= ROL::makePtr<DynamicPDE_Semilinear<RealT>>(*parlist); |
| 191 |
|
| 192 |
/*************************************************************************/ |
| 193 |
/***************** BUILD CONSTRAINT **************************************/ |
| 194 |
/*************************************************************************/ |
| 195 |
ROL::Ptr<DynConstraint<RealT> > dyn_con |
| 196 |
= ROL::makePtr<DynConstraint<RealT>>(pde,meshMgr,mpiSpaceComm,*parlist,*outStream); |
| 197 |
const ROL::Ptr<Assembler<RealT>> assembler = dyn_con->getAssembler(); |
| 198 |
dyn_con->setSolveParameters(*parlist); |
| 199 |
dyn_con->getAssembler()->printMeshData(*outStream); |
| 200 |
|
| 201 |
/*************************************************************************/ |
| 202 |
/***************** BUILD VECTORS *****************************************/ |
| 203 |
/*************************************************************************/ |
| 204 |
ROL::Ptr<Tpetra::MultiVector<>> u0_ptr = assembler->createStateVector(); |
| 205 |
ROL::Ptr<Tpetra::MultiVector<>> zk_ptr = assembler->createControlVector(); |
| 206 |
ROL::Ptr<ROL::Vector<RealT>> u0, zk; |
| 207 |
u0 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u0_ptr,pde,*assembler,*parlist); |
| 208 |
zk = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zk_ptr,pde,*assembler,*parlist); |
| 209 |
|
| 210 |
// ROL::Ptr<const ROL::PinTVectorCommunication<RealT>> vectorComm = ROL::makePtr<ROL::PinTVectorCommunication_Tpetra<RealT>>(); |
| 211 |
ROL::Ptr<const ROL::PinTVectorCommunication<RealT>> vectorComm = ROL::makePtr<ROL::PinTVectorCommunication_StdTpetraComposite<RealT>>(); |
| 212 |
ROL::Ptr<ROL::PinTVector<RealT>> state = ROL::buildStatePinTVector<RealT>( communicators, vectorComm, nt, u0); // for Euler, Crank-Nicolson, stencil = [-1,0] |
| 213 |
ROL::Ptr<ROL::PinTVector<RealT>> control = ROL::buildControlPinTVector<RealT>( communicators, vectorComm, nt, zk); // time discontinous, stencil = [0] |
| 214 |
|
| 215 |
// make sure we are globally consistent |
| 216 |
state->boundaryExchange(); |
| 217 |
control->boundaryExchange(); |
| 218 |
|
| 219 |
ROL::Ptr<ROL::Vector<RealT>> kkt_vector = ROL::makePtr<ROL::PartitionedVector<RealT>>({state->clone(),control->clone(),state->clone()}); |
| 220 |
|
| 221 |
/*************************************************************************/ |
| 222 |
/***************** BUILD PINT CONSTRAINT *********************************/ |
| 223 |
/*************************************************************************/ |
| 224 |
|
| 225 |
/*************************************************************************/ |
| 226 |
/************************* BUILD TIME STAMP ******************************/ |
| 227 |
/*************************************************************************/ |
| 228 |
auto timeStamp = ROL::makePtr<std::vector<ROL::TimeStamp<RealT>>>(control->numOwnedSteps()); |
| 229 |
|
| 230 |
double myFinalTime = dt*timeStamp->size(); |
| 231 |
double timeOffset = 0.0; |
| 232 |
MPI_Exscan(&myFinalTime,&timeOffset,1,MPI_DOUBLE,MPI_SUM,communicators->getTimeCommunicator()); |
| 233 |
|
| 234 |
for( uint k=0; k<timeStamp->size(); ++k ) { |
| 235 |
timeStamp->at(k).t.resize(2); |
| 236 |
timeStamp->at(k).t.at(0) = k*dt+timeOffset; |
| 237 |
timeStamp->at(k).t.at(1) = (k+1)*dt+timeOffset; |
| 238 |
} |
| 239 |
|
| 240 |
if(myRank==0) { |
| 241 |
(*outStream) << "Sweeps = " << sweeps << std::endl; |
| 242 |
(*outStream) << "Omega = " << omega << std::endl; |
| 243 |
(*outStream) << "Levels = " << numLevels << std::endl; |
| 244 |
(*outStream) << "Coarse Sweeps = " << coarseSweeps << std::endl; |
| 245 |
(*outStream) << "Coarse Omega = " << coarseOmega << std::endl; |
| 246 |
(*outStream) << "Global Scale = " << globalScale << std::endl; |
| 247 |
(*outStream) << "Rebalance = " << rebalance << std::endl; |
| 248 |
(*outStream) << "CG Iterations = " << cgIterations; |
| 249 |
if(cgIterations==0) |
| 250 |
*(outStream) << " (apply Wathen smoothing)" << std::endl; |
| 251 |
else |
| 252 |
*(outStream) << " (apply CG fix to Wathen smoothing)" << std::endl; |
| 253 |
(*outStream) << "Cntrl Reg Par = " << cntrlRegPar << std::endl; |
| 254 |
} |
| 255 |
|
| 256 |
// build the parallel in time constraint from the user constraint |
| 257 |
ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con = ROL::makePtr<ROL::PinTConstraint<RealT>>(dyn_con,u0,timeStamp); |
| 258 |
pint_con->applyMultigrid(numLevels,communicators,vectorComm,rebalance); |
| 259 |
pint_con->setSweeps(sweeps); |
| 260 |
pint_con->setRelaxation(omega); |
| 261 |
pint_con->setCoarseSweeps(coarseSweeps); |
| 262 |
pint_con->setCoarseRelaxation(coarseOmega); |
| 263 |
pint_con->setGlobalScale(globalScale); |
| 264 |
pint_con->setCGIterations(cgIterations); |
| 265 |
pint_con->setControlRegParam(cntrlRegPar); |
| 266 |
pint_con->setRecordResidualReductions(true); |
| 267 |
|
| 268 |
/*************************************************************************/ |
| 269 |
/***************** Run KKT Solver ***************************************/ |
| 270 |
/*************************************************************************/ |
| 271 |
|
| 272 |
Teuchos::RCP<Teuchos::StackedTimer> timer = Teuchos::TimeMonitor::getStackedTimer(); |
| 273 |
double tol = 1e-12; |
| 274 |
|
| 275 |
state->setScalar(1.0); |
| 276 |
control->setScalar(1.0); |
| 277 |
|
| 278 |
// setup and solve the lagrange multiplier RHS of the SQP algorithm |
| 279 |
/////////////////////////////////////////////////////////////////////////// |
| 280 |
{ |
| 281 |
auto kkt_b = kkt_vector->clone(); |
| 282 |
kkt_b->setScalar(1.0); |
| 283 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(0)->scale(dt*dx*dy); // u |
| 284 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(1)->scale(dt*dx*dy); // z |
| 285 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(2)->scale(0.0); // lambda |
| 286 |
|
| 287 |
if(myRank==0) (*outStream) << std::endl; |
| 288 |
|
| 289 |
solveKKTSystem("LAGR MULTIPLIER STEP: ", |
| 290 |
useWathenPrec,myRank,absTol,relTol, |
| 291 |
timer,outStream, |
| 292 |
pint_con,kkt_b,state,control); |
| 293 |
|
| 294 |
if(myRank==0) (*outStream) << std::endl; |
| 295 |
} |
| 296 |
|
| 297 |
// compute quasi normal step |
| 298 |
/////////////////////////////////////////////////////////////////////////// |
| 299 |
|
| 300 |
ROL::Ptr<ROL::Vector<RealT>> g_k; // this will be use in tangential step |
| 301 |
{ |
| 302 |
ROL::Ptr<ROL::Vector<RealT>> residual = state->clone(); |
| 303 |
ROL::Ptr<ROL::Vector<RealT>> cau_pt_1; |
| 304 |
ROL::Ptr<ROL::Vector<RealT>> cau_pt_2; |
| 305 |
|
| 306 |
// residual = c(x_k) |
| 307 |
pint_con->value(*residual,*state,*control,tol ); |
| 308 |
|
| 309 |
{ |
| 310 |
ROL::Ptr<ROL::Vector<RealT>> jv_1 = state->clone(); |
| 311 |
ROL::Ptr<ROL::Vector<RealT>> jv_2 = state->clone(); |
| 312 |
ROL::Ptr<ROL::Vector<RealT>> ajv_1 = state->clone(); |
| 313 |
ROL::Ptr<ROL::Vector<RealT>> ajv_2 = control->clone(); |
| 314 |
|
| 315 |
// ajv = c'(x_k)^* c(x_k) |
| 316 |
pint_con->applyAdjointJacobian_1(*ajv_1,*residual,*state,*control,tol); |
| 317 |
pint_con->applyAdjointJacobian_2(*ajv_2,*residual,*state,*control,tol); |
| 318 |
|
| 319 |
// jv = c'(x_k) c'(x_k)^* c(x_k) |
| 320 |
pint_con->applyJacobian_1(*jv_1,*ajv_1,*state,*control,tol); |
| 321 |
pint_con->applyJacobian_2(*jv_2,*ajv_2,*state,*control,tol); |
| 322 |
|
| 323 |
RealT norm_ajv_sqr = std::pow(ajv_1->norm(),2.0) + std::pow(ajv_2->norm(),2.0); |
| 324 |
RealT norm_jv_sqr = std::pow(jv_1->norm(),2.0) + std::pow(jv_2->norm(),2.0); |
| 325 |
|
| 326 |
// now we we will modify the jacobian components |
| 327 |
jv_1->scale(norm_ajv_sqr/norm_jv_sqr); |
| 328 |
jv_2->scale(norm_ajv_sqr/norm_jv_sqr); |
| 329 |
residual->plus(*jv_1); |
| 330 |
residual->plus(*jv_2); |
| 331 |
|
| 332 |
jv_1 = ROL::nullPtr; |
| 333 |
jv_2 = ROL::nullPtr; |
| 334 |
|
| 335 |
// cauchy point |
| 336 |
cau_pt_1 = ajv_1; cau_pt_1->scale(norm_ajv_sqr/norm_jv_sqr); |
| 337 |
cau_pt_2 = ajv_2; cau_pt_2->scale(norm_ajv_sqr/norm_jv_sqr); |
| 338 |
} |
| 339 |
|
| 340 |
// build RHS vector |
| 341 |
auto kkt_b = kkt_vector->clone(); |
| 342 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(0)->set(*cau_pt_1); // u |
| 343 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(1)->set(*cau_pt_2); // z |
| 344 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(2)->set(*residual); // lambda |
| 345 |
|
| 346 |
residual = ROL::nullPtr; |
| 347 |
|
| 348 |
if(myRank==0) (*outStream) << std::endl; |
| 349 |
|
| 350 |
auto delta_nk = solveKKTSystem("QUASI-NORMAL STEP: ", |
| 351 |
useWathenPrec,myRank,absTol,relTol, |
| 352 |
timer,outStream, |
| 353 |
pint_con,kkt_b,state,control); |
| 354 |
|
| 355 |
if(myRank==0) (*outStream) << std::endl; |
| 356 |
|
| 357 |
kkt_b->setScalar(1.0); |
| 358 |
kkt_b->plus(*delta_nk); |
| 359 |
|
| 360 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(0)->scale(dt*dx*dy); // u |
| 361 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(1)->scale(dt*dx*dy); // z |
| 362 |
ROL::dynamicPtrCast<ROL::PartitionedVector<double>>(kkt_b)->get(2)->setScalar(0.0); // lambda |
| 363 |
|
| 364 |
g_k = kkt_b; |
| 365 |
} |
| 366 |
|
| 367 |
// compute tangential step step |
| 368 |
/////////////////////////////////////////////////////////////////////////// |
| 369 |
{ |
| 370 |
if(myRank==0) (*outStream) << std::endl; |
| 371 |
|
| 372 |
solveKKTSystem("TANG STEP: ", |
| 373 |
useWathenPrec,myRank,absTol,relTol, |
| 374 |
timer,outStream, |
| 375 |
pint_con,g_k,state,control); |
| 376 |
|
| 377 |
if(myRank==0) (*outStream) << std::endl; |
| 378 |
} |
| 379 |
|
| 380 |
// setup and solve the quasi normal step |
| 381 |
|
| 382 |
#if 0 |
| 383 |
{ |
| 384 |
// This was useful for debugging, maintaing it so that we can recreate it if required. |
| 385 |
// However, its not consistent right now so I don't want to mislead. |
| 386 |
|
| 387 |
ROL::Ptr<ROL::Vector<RealT>> scratch; |
| 388 |
{ |
| 389 |
/*************************************************************************/ |
| 390 |
/***************** BUILD SERIAL-IN-TIME CONSTRAINT ***********************/ |
| 391 |
/*************************************************************************/ |
| 392 |
auto serial_con = ROL::make_SerialConstraint(dyn_con, *u0, timeStamp); |
| 393 |
|
| 394 |
/*************************************************************************/ |
| 395 |
/******************* BUILD SERIAL-IN-TIME VECTORS ************************/ |
| 396 |
/*************************************************************************/ |
| 397 |
auto U = ROL::PartitionedVector<RealT>::create(*uo, nt); |
| 398 |
auto Z = ROL::PartitionedVector<RealT>::create(*zk, nt); |
| 399 |
auto C = ROL::PartitionedVector<RealT>::create(*ck, nt); |
| 400 |
|
| 401 |
scratch = U->clone(); // this will be used for testing in the PINT vector |
| 402 |
|
| 403 |
auto UZ = ROL::make_Vector_SimOpt(U, Z); |
| 404 |
auto X = UZ->clone(); |
| 405 |
auto V_x = X->clone(); |
| 406 |
auto V_c = C->clone(); |
| 407 |
auto V_l = C->dual().clone(); |
| 408 |
auto ajv = X->dual().clone(); |
| 409 |
auto ajvpx = X->dual().clone(); |
| 410 |
auto jv = C->clone(); |
| 411 |
|
| 412 |
/*************************************************************************/ |
| 413 |
/******************* APPLY AUGMENTED SYSTEM TO 1's ***********************/ |
| 414 |
/*************************************************************************/ |
| 415 |
X->zero(); |
| 416 |
ROL::dynamicPtrCast<ROL::Vector_SimOpt<RealT>>(V_x)->get_1()->setScalar(1.0); |
| 417 |
ROL::dynamicPtrCast<ROL::Vector_SimOpt<RealT>>(V_x)->get_2()->setScalar(2.0); |
| 418 |
V_l->setScalar(3.0); |
| 419 |
ajvpx->set(V_x->dual()); |
| 420 |
serial_con->applyAdjointJacobian(*ajv, *V_l, *X, dummytol); |
| 421 |
ajvpx->plus(*ajv); |
| 422 |
|
| 423 |
auto ajv_u = ROL::dynamicPtrCast<ROL::Vector_SimOpt<RealT>>(ajv)->get_1(); |
| 424 |
auto ajv_z = ROL::dynamicPtrCast<ROL::Vector_SimOpt<RealT>>(ajv)->get_2(); |
| 425 |
|
| 426 |
serial_con->applyJacobian(*jv, *V_x, *X, dummytol); |
| 427 |
|
| 428 |
(*outStream) << "\nSerial components of augmented system:\n"; |
| 429 |
(*outStream) << " Norm of Aug*1 1st component = " << ajvpx->norm() << "\n"; |
| 430 |
(*outStream) << " Norm of Aug*1 2nd component = " << jv->norm() << "\n"; |
| 431 |
|
| 432 |
auto Hv_u = ROL::dynamicPtrCast<ROL::Vector_SimOpt<RealT>>(ajvpx)->get_1(); |
| 433 |
auto Hv_z = ROL::dynamicPtrCast<ROL::Vector_SimOpt<RealT>>(ajvpx)->get_2(); |
| 434 |
auto Hv_l = jv; |
| 435 |
|
| 436 |
(*outStream) << " Norm of Aug*1_u = " << Hv_u->norm() << "\n"; |
| 437 |
(*outStream) << " Norm of Aug*1_z = " << Hv_z->norm() << "\n"; |
| 438 |
(*outStream) << " Norm of Aug*1_l = " << Hv_l->norm() << "\n"; |
| 439 |
|
| 440 |
} |
| 441 |
{ |
| 442 |
/*************************************************************************/ |
| 443 |
/************************* TEST THE PINT SYSTEM *************************/ |
| 444 |
/*************************************************************************/ |
| 445 |
|
| 446 |
/*************************************************************************/ |
| 447 |
/******************* BUILD PARALLEL-IN-TIME VECTORS **********************/ |
| 448 |
/*************************************************************************/ |
| 449 |
auto U = ROL::buildStatePinTVector<RealT>( communicators, vectorComm, nt, uo); // for Euler, Crank-Nicolson, stencil = [-1,0] |
| 450 |
auto Z = ROL::buildControlPinTVector<RealT>(communicators, vectorComm, nt, zk); // time discontinous, stencil = [0] |
| 451 |
auto C = ROL::buildStatePinTVector<RealT>( communicators, vectorComm, nt, ck); // for Euler, Crank-Nicolson, stencil = [-1,0] |
| 452 |
|
| 453 |
auto triple = ROL::makePtr<ROL::PartitionedVector<RealT>>({U->clone(),Z->clone(),C->clone()}); |
| 454 |
auto V = triple->clone(); |
| 455 |
auto Hv = triple->dual().clone(); |
| 456 |
|
| 457 |
/*************************************************************************/ |
| 458 |
/******************* APPLY AUGMENTED SYSTEM TO 1's ***********************/ |
| 459 |
/*************************************************************************/ |
| 460 |
|
| 461 |
U->zero(); |
| 462 |
Z->zero(); |
| 463 |
ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(V)->get(0)->setScalar(1.0); |
| 464 |
ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(V)->get(1)->setScalar(2.0); |
| 465 |
ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(V)->get(2)->setScalar(3.0); |
| 466 |
Hv->setScalar(0.0); |
| 467 |
|
| 468 |
pint_con->applyAugmentedKKT(*Hv,*V,*U,*Z,tol); |
| 469 |
|
| 470 |
auto Hv_u = ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(Hv)->get(0); |
| 471 |
auto Hv_z = ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(Hv)->get(1); |
| 472 |
auto Hv_l = ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(Hv)->get(2); |
| 473 |
|
| 474 |
(*outStream) << "\nParallel compnents of augmented system:\n"; |
| 475 |
(*outStream) << " Norm of Aug*1_u = " << Hv_u->norm() << "\n"; |
| 476 |
(*outStream) << " Norm of Aug*1_z = " << Hv_z->norm() << "\n"; |
| 477 |
(*outStream) << " Norm of Aug*1_l = " << Hv_l->norm() << "\n"; |
| 478 |
|
| 479 |
auto pint_Hv_u = ROL::dynamicPtrCast<ROL::PinTVector<RealT>>(Hv_u); |
| 480 |
auto part_scratch = ROL::dynamicPtrCast<ROL::PartitionedVector<RealT>>(scratch); |
| 481 |
for(int i=0;i<pint_Hv_u->numOwnedSteps();i++) { |
| 482 |
|
| 483 |
part_scratch->get(i)->setScalar(-1.0); |
| 484 |
part_scratch->get(i)->axpy(1.0,*pint_Hv_u->getVectorPtr(2*i)); |
| 485 |
part_scratch->get(i)->axpy(1.0,*pint_Hv_u->getVectorPtr(2*i+1)); |
| 486 |
} |
| 487 |
|
| 488 |
(*outStream) << " Norm of Aug*1_u modified = " << scratch->norm() << "\n"; |
| 489 |
} |
| 490 |
} |
| 491 |
#endif |
| 492 |
} |
| 493 |
catch (std::logic_error& err) { |
| 494 |
*outStream << err.what() << "\n"; |
| 495 |
errorFlag = -1000; |
| 496 |
}; // end try |
| 497 |
|
| 498 |
if(myRank==0) { |
| 499 |
if (errorFlag != 0) |
| 500 |
std::cout << "End Result: TEST FAILED " << myRank << "\n"; |
| 501 |
else |
| 502 |
std::cout << "End Result: TEST PASSED " << myRank << "\n"; |
| 503 |
} |
| 504 |
|
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
ROL::Ptr<ROL::Vector<RealT>> |
| 509 |
solveKKTSystem(const std::string & prefix, |
| 510 |
bool useWathenPrec,int myRank, |
| 511 |
double absTol,double relTol, |
| 512 |
const Teuchos::RCP<Teuchos::StackedTimer> & timer, |
| 513 |
const ROL::Ptr<std::ostream> & outStream, |
| 514 |
const ROL::Ptr<ROL::PinTConstraint<RealT>> & pint_con, |
| 515 |
const ROL::Ptr<ROL::Vector<RealT>> & kkt_b, |
| 516 |
const ROL::Ptr<ROL::PinTVector<RealT>> & state, |
| 517 |
const ROL::Ptr<ROL::PinTVector<RealT>> & control) |
| 518 |
{ |
| 519 |
using Teuchos::RCP; |
| 520 |
|
| 521 |
RealT res0 = kkt_b->norm(); |
| 522 |
res0 = kkt_b->norm(); |
| 523 |
|
| 524 |
auto kkt_x_out = kkt_b->clone(); |
| 525 |
kkt_x_out->zero(); |
| 526 |
|
| 527 |
KKTOperator kktOperator; |
| 528 |
kktOperator.pint_con = pint_con; |
| 529 |
kktOperator.state = state; |
| 530 |
kktOperator.control = control; |
| 531 |
kktOperator.outStream = outStream; |
| 532 |
kktOperator.myRank = myRank; |
| 533 |
|
| 534 |
RCP<ROL::LinearOperator<RealT>> precOperator; |
| 535 |
|
| 536 |
// build the preconditioner |
| 537 |
if(useWathenPrec) { |
| 538 |
RCP<WathenKKTOperator> wathenOperator(new WathenKKTOperator); |
| 539 |
wathenOperator->pint_con = pint_con; |
| 540 |
wathenOperator->state = state; |
| 541 |
wathenOperator->control = control; |
| 542 |
wathenOperator->outStream = outStream; |
| 543 |
wathenOperator->myRank = myRank; |
| 544 |
|
| 545 |
precOperator = wathenOperator; |
| 546 |
} |
| 547 |
else { |
| 548 |
RCP<MGRITKKTOperator> mgOperator(new MGRITKKTOperator); |
| 549 |
mgOperator->pint_con = pint_con; |
| 550 |
mgOperator->state = state; |
| 551 |
mgOperator->control = control; |
| 552 |
mgOperator->outStream = outStream; |
| 553 |
mgOperator->myRank = myRank; |
| 554 |
|
| 555 |
precOperator = mgOperator; |
| 556 |
} |
| 557 |
|
| 558 |
Teuchos::ParameterList parlist; |
| 559 |
ROL::ParameterList &krylovList = parlist.sublist("General").sublist("Krylov"); |
| 560 |
krylovList.set("Absolute Tolerance",absTol); |
| 561 |
krylovList.set("Relative Tolerance",relTol); |
| 562 |
|
| 563 |
if(myRank==0) |
| 564 |
(*outStream) << "Relative tolerance = " << relTol << std::endl; |
| 565 |
|
| 566 |
ROL::GMRES<RealT> krylov(parlist); // TODO: Do Belos |
| 567 |
// ROL::MINRES<RealT> krylov(1e0, 1e-6, 200); // TODO: Do Belos |
| 568 |
|
| 569 |
int flag = 0; |
| 570 |
int iter = 0; |
| 571 |
|
| 572 |
|
| 573 |
if(myRank==0) { |
| 574 |
(*outStream) << "Solving KKT system: "; |
| 575 |
if(useWathenPrec) |
| 576 |
(*outStream) << "using Wathen preconditioner" << std::endl; |
| 577 |
else |
| 578 |
(*outStream) << "using MGRIT preconditioner" << std::endl; |
| 579 |
} |
| 580 |
|
| 581 |
MPI_Barrier(MPI_COMM_WORLD); |
| 582 |
double t0 = MPI_Wtime(); |
| 583 |
|
| 584 |
timer->start("krylov"); |
| 585 |
RealT finalTol = krylov.run(*kkt_x_out,kktOperator,*kkt_b,*precOperator,iter,flag); |
| 586 |
timer->stop("krylov"); |
| 587 |
|
| 588 |
MPI_Barrier(MPI_COMM_WORLD); |
| 589 |
double tf = MPI_Wtime(); |
| 590 |
|
| 591 |
// Don't trust residual computation, so repeat. |
| 592 |
auto myb = kkt_b->clone(); |
| 593 |
myb->set(*kkt_b); |
| 594 |
auto norm_myb = myb->norm(); |
| 595 |
auto tmp = kkt_b->clone(); |
| 596 |
RealT dummytol = 1e-8; |
| 597 |
kktOperator.apply(*tmp, *kkt_x_out, dummytol); |
| 598 |
tmp->axpy(-1, *myb); |
| 599 |
auto norm_myAxmb = tmp->norm(); |
| 600 |
auto norm_mysoln = kkt_x_out->norm(); |
| 601 |
|
| 602 |
if(myRank==0) { |
| 603 |
(*outStream) << prefix << "Krylov Iteration = " << iter << " " << (finalTol / res0) << " " << tf-t0 << std::endl; |
| 604 |
(*outStream) << "||x||=" << norm_mysoln << " ||b||=" << norm_myb << " ||Ax-b||=" << norm_myAxmb << std::endl; |
| 605 |
|
| 606 |
const std::map<int,std::vector<double>> & preSmooth = pint_con->getPreSmoothResidualReductions(); |
| 607 |
const std::map<int,std::vector<double>> & postSmooth = pint_con->getPostSmoothResidualReduction(); |
| 608 |
const std::vector<double> & coarseSmooth = pint_con->getCoarseResidualReduction(); |
| 609 |
|
| 610 |
if(not useWathenPrec) { |
| 611 |
// loop over presmoother residual reductions |
| 612 |
*outStream << "Presmooth Reductions: " << std::endl; |
| 613 |
for(auto iter_l : preSmooth) { |
| 614 |
double average = std::accumulate( iter_l.second.begin(), iter_l.second.end(), 0.0)/coarseSmooth.size(); |
| 615 |
double min = *std::min_element(iter_l.second.begin(),iter_l.second.end()); |
| 616 |
double max = *std::max_element(iter_l.second.begin(),iter_l.second.end()); |
| 617 |
*outStream << " level "; |
| 618 |
*outStream << "min,avg,max = " << min << ", " << average << ", " << max << std::endl; |
| 619 |
} |
| 620 |
|
| 621 |
// loop over postsmoother residual reductions |
| 622 |
*outStream << "Postsmooth Reductions: " << std::endl; |
| 623 |
for(auto iter_l : postSmooth) { |
| 624 |
double average = std::accumulate( iter_l.second.begin(), iter_l.second.end(), 0.0)/coarseSmooth.size(); |
| 625 |
double min = *std::min_element(iter_l.second.begin(),iter_l.second.end()); |
| 626 |
double max = *std::max_element(iter_l.second.begin(),iter_l.second.end()); |
| 627 |
*outStream << " level "; |
| 628 |
*outStream << "min,avg,max = " << min << ", " << average << ", " << max << std::endl; |
| 629 |
} |
| 630 |
|
| 631 |
// loop over coarse residual reductions |
| 632 |
{ |
| 633 |
*outStream << "Coarse Reductions: "; |
| 634 |
double average = std::accumulate( coarseSmooth.begin(), coarseSmooth.end(), 0.0)/coarseSmooth.size(); |
| 635 |
double min = *std::min_element(coarseSmooth.begin(),coarseSmooth.end()); |
| 636 |
double max = *std::max_element(coarseSmooth.begin(),coarseSmooth.end()); |
| 637 |
*outStream << "min,avg,max = " << min << ", " << average << ", " << max << std::endl; |
| 638 |
*outStream << std::endl; |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
pint_con->clearResidualReduction(); |
| 644 |
|
| 645 |
return kkt_x_out; |
| 646 |
} |