| 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 "Tpetra_Core.hpp" |
| 17 |
#include "Tpetra_Version.hpp" |
| 18 |
|
| 19 |
#include <iostream> |
| 20 |
#include <algorithm> |
| 21 |
|
| 22 |
#include "ROL_Bounds.hpp" |
| 23 |
#include "ROL_Stream.hpp" |
| 24 |
#include "ROL_ParameterList.hpp" |
| 25 |
#include "ROL_Solver.hpp" |
| 26 |
#include "ROL_ReducedDynamicObjective.hpp" |
| 27 |
#include "ROL_DynamicConstraintCheck.hpp" |
| 28 |
#include "ROL_DynamicObjectiveCheck.hpp" |
| 29 |
|
| 30 |
#include "../../TOOLS/lindynconstraint.hpp" |
| 31 |
#include "../../TOOLS/dynconstraint.hpp" |
| 32 |
#include "../../TOOLS/pdeobjective.hpp" |
| 33 |
#include "../../TOOLS/ltiobjective.hpp" |
| 34 |
#include "../../TOOLS/meshmanager.hpp" |
| 35 |
#include "../../TOOLS/pdevector.hpp" |
| 36 |
#include "dynpde_semilinear.hpp" |
| 37 |
#include "obj_semilinear.hpp" |
| 38 |
|
| 39 |
|
| 40 |
int main(int argc, char *argv[]) { |
| 41 |
using RealT = double; |
| 42 |
|
| 43 |
/*** Initialize communicator. ***/ |
| 44 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 45 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 46 |
= Tpetra::getDefaultComm(); |
| 47 |
|
| 48 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 49 |
const int myRank = comm->getRank(); |
| 50 |
ROL::Ptr<std::ostream> outStream = ROL::makeStreamPtr( std::cout, (argc > 1) && (myRank==0) ); |
| 51 |
|
| 52 |
int errorFlag = 0; |
| 53 |
|
| 54 |
// *** Example body. |
| 55 |
try { |
| 56 |
|
| 57 |
/*** Read in XML input ***/ |
| 58 |
ROL::Ptr<ROL::ParameterList> parlist = ROL::getParametersFromXmlFile("input_ex01.xml"); |
| 59 |
int nt = parlist->sublist("Time Discretization").get("Number of Time Steps", 100); |
| 60 |
RealT T = parlist->sublist("Time Discretization").get("End Time", 1.0); |
| 61 |
RealT dt = T/static_cast<RealT>(nt); |
| 62 |
|
| 63 |
parlist->sublist("Reduced Dynamic Objective").set("State Domain Seed",12321*(myRank+1)); |
| 64 |
parlist->sublist("Reduced Dynamic Objective").set("State Range Seed", 32123*(myRank+1)); |
| 65 |
parlist->sublist("Reduced Dynamic Objective").set("Adjoint Domain Seed",23432*(myRank+1)); |
| 66 |
parlist->sublist("Reduced Dynamic Objective").set("Adjoint Range Seed", 43234*(myRank+1)); |
| 67 |
parlist->sublist("Reduced Dynamic Objective").set("State Sensitivity Domain Seed",34543*(myRank+1)); |
| 68 |
parlist->sublist("Reduced Dynamic Objective").set("State Sensitivity Range Seed", 54345*(myRank+1)); |
| 69 |
|
| 70 |
/*************************************************************************/ |
| 71 |
/***************** BUILD GOVERNING PDE ***********************************/ |
| 72 |
/*************************************************************************/ |
| 73 |
/*** Initialize mesh data structure. ***/ |
| 74 |
ROL::Ptr<MeshManager<RealT>> meshMgr |
| 75 |
= ROL::makePtr<MeshManager_Rectangle<RealT>>(*parlist); |
| 76 |
// Initialize PDE describe semilinear equation |
| 77 |
ROL::Ptr<DynamicPDE_Semilinear<RealT>> pde |
| 78 |
= ROL::makePtr<DynamicPDE_Semilinear<RealT>>(*parlist); |
| 79 |
|
| 80 |
/*************************************************************************/ |
| 81 |
/***************** BUILD CONSTRAINT **************************************/ |
| 82 |
/*************************************************************************/ |
| 83 |
ROL::Ptr<ROL::DynamicConstraint<RealT>> dyn_con; |
| 84 |
ROL::Ptr<Assembler<RealT>> assembler; |
| 85 |
int nltype = parlist->sublist("Semilinear").get("Nonlinearity Type",0); |
| 86 |
if (nltype == 0) { |
| 87 |
dyn_con = ROL::makePtr<LinDynConstraint<RealT>>(pde,meshMgr,comm,*parlist,true,*outStream); |
| 88 |
assembler = ROL::staticPtrCast<LinDynConstraint<RealT>>(dyn_con)->getAssembler(); |
| 89 |
} |
| 90 |
else { |
| 91 |
dyn_con = ROL::makePtr<DynConstraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream); |
| 92 |
assembler = ROL::staticPtrCast<DynConstraint<RealT>>(dyn_con)->getAssembler(); |
| 93 |
} |
| 94 |
dyn_con->setSolveParameters(*parlist); |
| 95 |
assembler->printMeshData(*outStream); |
| 96 |
|
| 97 |
/*************************************************************************/ |
| 98 |
/***************** BUILD VECTORS *****************************************/ |
| 99 |
/*************************************************************************/ |
| 100 |
ROL::Ptr<Tpetra::MultiVector<>> u0_ptr = assembler->createStateVector(); |
| 101 |
ROL::Ptr<Tpetra::MultiVector<>> uo_ptr = assembler->createStateVector(); |
| 102 |
ROL::Ptr<Tpetra::MultiVector<>> un_ptr = assembler->createStateVector(); |
| 103 |
ROL::Ptr<Tpetra::MultiVector<>> ck_ptr = assembler->createResidualVector(); |
| 104 |
ROL::Ptr<Tpetra::MultiVector<>> zk_ptr = assembler->createControlVector(); |
| 105 |
ROL::Ptr<ROL::Vector<RealT>> u0, uo, un, ck, zk; |
| 106 |
u0 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u0_ptr,pde,*assembler,*parlist); |
| 107 |
uo = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uo_ptr,pde,*assembler,*parlist); |
| 108 |
un = ROL::makePtr<PDE_PrimalSimVector<RealT>>(un_ptr,pde,*assembler,*parlist); |
| 109 |
ck = ROL::makePtr<PDE_DualSimVector<RealT>>(ck_ptr,pde,*assembler,*parlist); |
| 110 |
zk = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zk_ptr,pde,*assembler,*parlist); |
| 111 |
ROL::Ptr<ROL::PartitionedVector<RealT>> z |
| 112 |
= ROL::PartitionedVector<RealT>::create(*zk, nt); |
| 113 |
|
| 114 |
/*************************************************************************/ |
| 115 |
/***************** BUILD COST FUNCTIONAL *********************************/ |
| 116 |
/*************************************************************************/ |
| 117 |
std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(2,ROL::nullPtr); |
| 118 |
qoi_vec[0] = ROL::makePtr<QoI_State_Cost_Semilinear<RealT>>(pde->getFE()); |
| 119 |
qoi_vec[1] = ROL::makePtr<QoI_Control_Cost_Semilinear<RealT>>(pde->getFE()); |
| 120 |
RealT w1 = parlist->sublist("Problem").get("State Cost",1.0); |
| 121 |
RealT w2 = parlist->sublist("Problem").get("Control Cost",1e-2); |
| 122 |
std::vector<RealT> wts = {w1, w2}; |
| 123 |
ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj_k |
| 124 |
= ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,wts,assembler); |
| 125 |
ROL::Ptr<LTI_Objective<RealT>> dyn_obj |
| 126 |
= ROL::makePtr<LTI_Objective<RealT>>(*parlist,obj_k,false); |
| 127 |
|
| 128 |
/*************************************************************************/ |
| 129 |
/***************** BUILD REDUCED COST FUNCTIONAL *************************/ |
| 130 |
/*************************************************************************/ |
| 131 |
std::vector<ROL::TimeStamp<RealT>> timeStamp(nt); |
| 132 |
for( int k=0; k<nt; ++k ) { |
| 133 |
timeStamp.at(k).t.resize(2); |
| 134 |
timeStamp.at(k).t.at(0) = k*dt; |
| 135 |
timeStamp.at(k).t.at(1) = (k+1)*dt; |
| 136 |
} |
| 137 |
ROL::ParameterList &rpl = parlist->sublist("Reduced Dynamic Objective"); |
| 138 |
ROL::Ptr<ROL::ReducedDynamicObjective<RealT>> obj |
| 139 |
= ROL::makePtr<ROL::ReducedDynamicObjective<RealT>>(dyn_obj, dyn_con, u0, zk, ck, timeStamp, rpl,outStream); |
| 140 |
// print uncontrolled state |
| 141 |
bool print_unc = true; |
| 142 |
if (print_unc) { |
| 143 |
std::clock_t timer_print_unc = std::clock(); |
| 144 |
// Output state and control to file |
| 145 |
uo->set(*u0); un->zero(); z->zero(); |
| 146 |
for (int k = 1; k < nt; ++k) { |
| 147 |
// Print previous state to file |
| 148 |
std::stringstream ufile; |
| 149 |
ufile << "uncontrolled_state." << k-1 << ".txt"; |
| 150 |
assembler->outputTpetraVector(uo_ptr, ufile.str()); |
| 151 |
// Advance time stepper |
| 152 |
dyn_con->solve(*ck, *uo, *un, *z->get(k), timeStamp[k]); |
| 153 |
uo->set(*un); |
| 154 |
} |
| 155 |
// Print previous state to file |
| 156 |
std::stringstream ufile; |
| 157 |
ufile << "uncontrolled_state." << nt-1 << ".txt"; |
| 158 |
assembler->outputTpetraVector(uo_ptr, ufile.str()); |
| 159 |
*outStream << "Output time: " |
| 160 |
<< static_cast<RealT>(std::clock()-timer_print_unc)/static_cast<RealT>(CLOCKS_PER_SEC) |
| 161 |
<< " seconds." << std::endl << std::endl; |
| 162 |
} |
| 163 |
|
| 164 |
/*************************************************************************/ |
| 165 |
/***************** BUILD BOUND CONSTRAINT ********************************/ |
| 166 |
/*************************************************************************/ |
| 167 |
bool deactivate = parlist->sublist("Problem").get("Deactivate Bound Constraints",false); |
| 168 |
RealT lbnd = parlist->sublist("Problem").get("Lower Bound",0.0); |
| 169 |
RealT ubnd = parlist->sublist("Problem").get("Upper Bound",1.0); |
| 170 |
ROL::Ptr<ROL::PartitionedVector<RealT>> zlo = ROL::PartitionedVector<RealT>::create(*zk, nt); |
| 171 |
ROL::Ptr<ROL::PartitionedVector<RealT>> zhi = ROL::PartitionedVector<RealT>::create(*zk, nt); |
| 172 |
zlo->setScalar(lbnd); |
| 173 |
zhi->setScalar(ubnd); |
| 174 |
ROL::Ptr<ROL::BoundConstraint<RealT>> bnd = ROL::makePtr<ROL::Bounds<RealT>>(zlo,zhi); |
| 175 |
|
| 176 |
/*************************************************************************/ |
| 177 |
/***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/ |
| 178 |
/*************************************************************************/ |
| 179 |
bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false); |
| 180 |
if ( checkDeriv ) { |
| 181 |
ROL::Ptr<ROL::PartitionedVector<RealT>> dz = ROL::PartitionedVector<RealT>::create(*zk, nt); |
| 182 |
ROL::Ptr<ROL::PartitionedVector<RealT>> hz = ROL::PartitionedVector<RealT>::create(*zk, nt); |
| 183 |
zk->randomize(); z->randomize(); dz->randomize(); hz->randomize(); |
| 184 |
uo->randomize(); un->randomize(); |
| 185 |
ROL::ValidateFunction<RealT> validate(1,13,20,11,true,*outStream); |
| 186 |
ROL::DynamicObjectiveCheck<RealT>::check(*dyn_obj,validate,*uo,*un,*zk); |
| 187 |
ROL::DynamicConstraintCheck<RealT>::check(*dyn_con,validate,*uo,*un,*zk); |
| 188 |
obj->checkGradient(*z,*dz,true,*outStream); |
| 189 |
obj->checkHessVec(*z,*dz,true,*outStream); |
| 190 |
obj->checkHessSym(*z,*dz,*hz,true,*outStream); |
| 191 |
} |
| 192 |
|
| 193 |
/*************************************************************************/ |
| 194 |
/***************** SOLVE OPTIMIZATION PROBLEM ****************************/ |
| 195 |
/*************************************************************************/ |
| 196 |
ROL::Ptr<ROL::Problem<RealT>> problem = ROL::makePtr<ROL::Problem<RealT>>(obj,z); |
| 197 |
if (!deactivate) problem->addBoundConstraint(bnd); |
| 198 |
problem->finalize(false,true,*outStream); |
| 199 |
ROL::Solver<RealT> solver(problem,*parlist); |
| 200 |
z->zero(); |
| 201 |
std::clock_t timer = std::clock(); |
| 202 |
solver.solve(*outStream); |
| 203 |
*outStream << "Optimization time: " |
| 204 |
<< static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC) |
| 205 |
<< " seconds." << std::endl << std::endl; |
| 206 |
|
| 207 |
/*************************************************************************/ |
| 208 |
/***************** OUTPUT RESULTS ****************************************/ |
| 209 |
/*************************************************************************/ |
| 210 |
std::clock_t timer_print = std::clock(); |
| 211 |
// Output state and control to file |
| 212 |
uo->set(*u0); un->zero(); |
| 213 |
for (int k = 1; k < nt; ++k) { |
| 214 |
// Print previous state to file |
| 215 |
std::stringstream ufile; |
| 216 |
ufile << "state." << k-1 << ".txt"; |
| 217 |
assembler->outputTpetraVector(uo_ptr, ufile.str()); |
| 218 |
// Print current control |
| 219 |
std::stringstream zfile; |
| 220 |
zfile << "control." << k-1 << ".txt"; |
| 221 |
assembler->outputTpetraVector(ROL::staticPtrCast<PDE_PrimalOptVector<RealT>>(z->get(k-1))->getVector(),zfile.str()); |
| 222 |
// Advance time stepper |
| 223 |
dyn_con->solve(*ck, *uo, *un, *z->get(k), timeStamp[k]); |
| 224 |
uo->set(*un); |
| 225 |
} |
| 226 |
// Print previous state to file |
| 227 |
std::stringstream ufile; |
| 228 |
ufile << "state." << nt-1 << ".txt"; |
| 229 |
assembler->outputTpetraVector(uo_ptr, ufile.str()); |
| 230 |
// Print current control |
| 231 |
std::stringstream zfile; |
| 232 |
zfile << "control." << nt-1 << ".txt"; |
| 233 |
assembler->outputTpetraVector(ROL::staticPtrCast<PDE_PrimalOptVector<RealT>>(z->get(nt-1))->getVector(),zfile.str()); |
| 234 |
*outStream << "Output time: " |
| 235 |
<< static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC) |
| 236 |
<< " seconds." << std::endl << std::endl; |
| 237 |
} |
| 238 |
catch (std::logic_error& err) { |
| 239 |
*outStream << err.what() << "\n"; |
| 240 |
errorFlag = -1000; |
| 241 |
}; // end try |
| 242 |
|
| 243 |
if (errorFlag != 0) |
| 244 |
std::cout << "End Result: TEST FAILED\n"; |
| 245 |
else |
| 246 |
std::cout << "End Result: TEST PASSED\n"; |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |