| 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 mother problem of PDE-constrained optimization: |
| 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 "ROL_Algorithm.hpp" |
| 23 |
#include "ROL_ConstraintStatusTest.hpp" |
| 24 |
#include "ROL_Bounds.hpp" |
| 25 |
#include "ROL_TrustRegionStep.hpp" |
| 26 |
#include "ROL_MoreauYosidaPenaltyStep.hpp" |
| 27 |
#include "ROL_Reduced_Objective_SimOpt.hpp" |
| 28 |
#include "ROL_BoundConstraint_SimOpt.hpp" |
| 29 |
#include "ROL_TpetraMultiVector.hpp" |
| 30 |
|
| 31 |
#include <iostream> |
| 32 |
#include <algorithm> |
| 33 |
|
| 34 |
#include "data.hpp" |
| 35 |
#include "objective.hpp" |
| 36 |
#include "constraint.hpp" |
| 37 |
|
| 38 |
typedef double RealT; |
| 39 |
|
| 40 |
int main(int argc, char *argv[]) { |
| 41 |
|
| 42 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 43 |
int iprint = argc - 1; |
| 44 |
ROL::Ptr<std::ostream> outStream; |
| 45 |
ROL::nullstream bhs; // outputs nothing |
| 46 |
|
| 47 |
/*** Initialize communicator. ***/ |
| 48 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 49 |
ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 50 |
const int myRank = comm->getRank(); |
| 51 |
if ((iprint > 0) && (myRank == 0)) { |
| 52 |
outStream = ROL::makePtrFromRef(std::cout); |
| 53 |
} |
| 54 |
else { |
| 55 |
outStream = ROL::makePtrFromRef(bhs); |
| 56 |
} |
| 57 |
|
| 58 |
int errorFlag = 0; |
| 59 |
|
| 60 |
// *** Example body. |
| 61 |
try { |
| 62 |
|
| 63 |
/*** Read in XML input ***/ |
| 64 |
std::string filename = "input.xml"; |
| 65 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 66 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 67 |
|
| 68 |
RealT z_lo_bound = parlist->sublist("Problem").get("Control lower bound", ROL::ROL_NINF<RealT>()); |
| 69 |
RealT z_up_bound = parlist->sublist("Problem").get("Control upper bound", ROL::ROL_INF<RealT>()); |
| 70 |
RealT u_lo_bound = parlist->sublist("Problem").get("State lower bound", ROL::ROL_NINF<RealT>()); |
| 71 |
RealT u_up_bound = parlist->sublist("Problem").get("State upper bound", ROL::ROL_INF<RealT>()); |
| 72 |
|
| 73 |
/*** Initialize main data structure. ***/ |
| 74 |
ROL::Ptr<PoissonData<RealT> > data = ROL::makePtr<PoissonData<RealT>>(comm, parlist, outStream); |
| 75 |
|
| 76 |
/*** Build vectors and dress them up as ROL vectors. ***/ |
| 77 |
ROL::Ptr<const Tpetra::Map<> > vecmap_u = data->getMatA()->getDomainMap(); |
| 78 |
ROL::Ptr<const Tpetra::Map<> > vecmap_z = data->getMatB()->getDomainMap(); |
| 79 |
ROL::Ptr<const Tpetra::Map<> > vecmap_c = data->getMatA()->getRangeMap(); |
| 80 |
ROL::Ptr<Tpetra::MultiVector<> > u_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_u, 1, true); |
| 81 |
ROL::Ptr<Tpetra::MultiVector<> > p_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_u, 1, true); |
| 82 |
ROL::Ptr<Tpetra::MultiVector<> > u_lo_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_u, 1, true); |
| 83 |
ROL::Ptr<Tpetra::MultiVector<> > u_up_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_u, 1, true); |
| 84 |
ROL::Ptr<Tpetra::MultiVector<> > z_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_z, 1, true); |
| 85 |
ROL::Ptr<Tpetra::MultiVector<> > z_lo_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_z, 1, true); |
| 86 |
ROL::Ptr<Tpetra::MultiVector<> > z_up_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_z, 1, true); |
| 87 |
ROL::Ptr<Tpetra::MultiVector<> > c_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_c, 1, true); |
| 88 |
ROL::Ptr<Tpetra::MultiVector<> > du_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_u, 1, true); |
| 89 |
ROL::Ptr<Tpetra::MultiVector<> > dz_ptr = ROL::makePtr<Tpetra::MultiVector<>>(vecmap_z, 1, true); |
| 90 |
// Set all values to 1 in u, z and c. |
| 91 |
u_ptr->putScalar(1.0); |
| 92 |
p_ptr->putScalar(1.0); |
| 93 |
u_lo_ptr->putScalar(u_lo_bound); |
| 94 |
u_up_ptr->putScalar(u_up_bound); |
| 95 |
z_ptr->putScalar(1.0); |
| 96 |
z_lo_ptr->putScalar(z_lo_bound); |
| 97 |
z_up_ptr->putScalar(z_up_bound); |
| 98 |
c_ptr->putScalar(1.0); |
| 99 |
// Randomize d vectors. |
| 100 |
du_ptr->randomize(); |
| 101 |
dz_ptr->randomize(); |
| 102 |
// Create ROL::TpetraMultiVectors. |
| 103 |
ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(u_ptr); |
| 104 |
ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_ptr); |
| 105 |
ROL::Ptr<ROL::Vector<RealT> > u_lo_p = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(u_lo_ptr); |
| 106 |
ROL::Ptr<ROL::Vector<RealT> > u_up_p = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(u_up_ptr); |
| 107 |
ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(z_ptr); |
| 108 |
ROL::Ptr<ROL::Vector<RealT> > z_lo_p = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(z_lo_ptr); |
| 109 |
ROL::Ptr<ROL::Vector<RealT> > z_up_p = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(z_up_ptr); |
| 110 |
ROL::Ptr<ROL::Vector<RealT> > cp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(c_ptr); |
| 111 |
ROL::Ptr<ROL::Vector<RealT> > dup = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(du_ptr); |
| 112 |
ROL::Ptr<ROL::Vector<RealT> > dzp = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(dz_ptr); |
| 113 |
// Create ROL SimOpt vectors. |
| 114 |
ROL::Vector_SimOpt<RealT> x(up,zp); |
| 115 |
ROL::Vector_SimOpt<RealT> d(dup,dzp); |
| 116 |
|
| 117 |
/*** Build objective function, equality constraint, reduced objective function and bound constraint. ***/ |
| 118 |
ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj = |
| 119 |
ROL::makePtr<Objective_PDEOPT_Poisson<RealT>>(data, parlist); |
| 120 |
ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con = |
| 121 |
ROL::makePtr<EqualityConstraint_PDEOPT_Poisson<RealT>>(data, parlist); |
| 122 |
ROL::Ptr<ROL::Objective<RealT> > objReduced = |
| 123 |
ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, pp); |
| 124 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bcon_control = |
| 125 |
ROL::makePtr<ROL::Bounds<RealT>>(z_lo_p, z_up_p); |
| 126 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bcon_state = |
| 127 |
ROL::makePtr<ROL::Bounds<RealT>>(u_lo_p, u_up_p); |
| 128 |
// Initialize SimOpt bound constraint. |
| 129 |
ROL::Ptr<ROL::BoundConstraint<RealT> > bcon_simopt = |
| 130 |
ROL::makePtr<ROL::BoundConstraint_SimOpt<RealT>>(bcon_state,bcon_control); |
| 131 |
|
| 132 |
/*** Check functional interface. ***/ |
| 133 |
obj->checkGradient(x,d,true,*outStream); |
| 134 |
obj->checkHessVec(x,d,true,*outStream); |
| 135 |
con->checkApplyJacobian(x,d,*up,true,*outStream); |
| 136 |
con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream); |
| 137 |
con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); |
| 138 |
con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 139 |
con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); |
| 140 |
objReduced->checkGradient(*zp,*dzp,true,*outStream); |
| 141 |
objReduced->checkHessVec(*zp,*dzp,true,*outStream); |
| 142 |
|
| 143 |
/*** Solve optimization problem. ***/ |
| 144 |
|
| 145 |
ROL::Ptr<ROL::Step<RealT>> step_tr = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist); |
| 146 |
ROL::Ptr<ROL::StatusTest<RealT>> status_tr = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist); |
| 147 |
ROL::Algorithm<RealT> algo_tr(step_tr,status_tr,false); |
| 148 |
zp->zero(); // set zero initial guess |
| 149 |
algo_tr.run(*zp, *objReduced, *bcon_control, true, *outStream); |
| 150 |
RealT tol = 1e-8; |
| 151 |
con->solve(*cp, *up, *zp, tol); |
| 152 |
|
| 153 |
data->outputTpetraVector(u_ptr, "state.txt"); |
| 154 |
data->outputTpetraVector(z_ptr, "control.txt"); |
| 155 |
|
| 156 |
ROL::Ptr<ROL::Step<RealT>> step_my = ROL::makePtr<ROL::MoreauYosidaPenaltyStep<RealT>>(*parlist); |
| 157 |
ROL::Ptr<ROL::StatusTest<RealT>> status_my = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist); |
| 158 |
ROL::Algorithm<RealT> algo_my(step_my,status_my,false); |
| 159 |
ROL::MoreauYosidaPenalty<RealT> obj_my(obj, bcon_simopt, x, *parlist); |
| 160 |
x.zero(); // set zero initial guess |
| 161 |
std::vector<std::string> algo_output; |
| 162 |
algo_output = algo_my.run(x, *cp, obj_my, *con, *bcon_simopt, true, *outStream); |
| 163 |
for (unsigned i=0; i<algo_output.size(); ++i) { |
| 164 |
*outStream << algo_output[i]; |
| 165 |
} |
| 166 |
|
| 167 |
//ROL::Algorithm<RealT> algo_cs("Composite Step",*parlist,false); |
| 168 |
//x.zero(); // set zero initial guess |
| 169 |
//algo_cs.run(x, *cp, *obj, *con, true, *outStream); |
| 170 |
|
| 171 |
data->outputTpetraVector(u_ptr, "stateFS.txt"); |
| 172 |
data->outputTpetraVector(z_ptr, "controlFS.txt"); |
| 173 |
|
| 174 |
*outStream << std::endl << "|| u_approx - u_analytic ||_L2 = " |
| 175 |
<< data->computeStateError(u_ptr) << std::endl; |
| 176 |
|
| 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 |
} |