| 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 test_01.cpp |
| 11 |
\brief Test Tpetra_MultiVector interface. |
| 12 |
*/ |
| 13 |
|
| 14 |
|
| 15 |
#include "ROL_Algorithm.hpp" |
| 16 |
#include "ROL_TrustRegionStep.hpp" |
| 17 |
#include "ROL_StatusTest.hpp" |
| 18 |
#include "ROL_TpetraMultiVector.hpp" |
| 19 |
#include "ROL_Types.hpp" |
| 20 |
#include "ROL_Zakharov.hpp" |
| 21 |
|
| 22 |
#include "ROL_Stream.hpp" |
| 23 |
#include "Teuchos_GlobalMPISession.hpp" |
| 24 |
|
| 25 |
#include "Tpetra_Core.hpp" |
| 26 |
|
| 27 |
typedef double RealT; |
| 28 |
typedef double ElementT; |
| 29 |
|
| 30 |
typedef Tpetra::Map<>::local_ordinal_type LO; |
| 31 |
typedef Tpetra::Map<>::global_ordinal_type GO; |
| 32 |
typedef Tpetra::Map<>::node_type Node; |
| 33 |
typedef Tpetra::Map<LO, GO, Node> Map; |
| 34 |
typedef Tpetra::MultiVector<RealT, LO, GO, Node> MV; |
| 35 |
|
| 36 |
int main(int argc, char *argv[]) { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef ROL::Ptr<MV> MVP; |
| 41 |
|
| 42 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); |
| 43 |
|
| 44 |
ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 45 |
|
| 46 |
|
| 47 |
int iprint = argc - 1; |
| 48 |
ROL::nullstream bhs; // outputs nothing |
| 49 |
std::ostream& outStream = (iprint > 0) ? std::cout : bhs; |
| 50 |
|
| 51 |
int errorFlag = 0; |
| 52 |
|
| 53 |
RealT errtol = ROL::ROL_THRESHOLD<RealT>(); |
| 54 |
|
| 55 |
try { |
| 56 |
// Dimension of the optimization vector |
| 57 |
|
| 58 |
int dim = 10; |
| 59 |
|
| 60 |
ROL::Ptr<Map> map = ROL::makePtr<Map>(dim,0,comm); |
| 61 |
|
| 62 |
// Create Tpetra::MultiVectors (single vectors) |
| 63 |
MVP x_ptr = ROL::makePtr<MV>(map,1,true); |
| 64 |
MVP y_ptr = ROL::makePtr<MV>(map,1,true); |
| 65 |
|
| 66 |
// Random elements |
| 67 |
x_ptr->randomize(); |
| 68 |
|
| 69 |
// Set all values to 2 |
| 70 |
y_ptr->putScalar(-2.0); |
| 71 |
|
| 72 |
/*---[ Begin Test of ROL::TpetraMultiVector methods ] ---*/ |
| 73 |
|
| 74 |
// Create ROL vectors |
| 75 |
ROL::TpetraMultiVector<RealT> x(x_ptr); // Testing default parameters here |
| 76 |
ROL::TpetraMultiVector<RealT,LO,GO,Node> y(y_ptr); |
| 77 |
|
| 78 |
// norm of x |
| 79 |
RealT xnorm = x.norm(); |
| 80 |
outStream << "\nNorm of ROL::TpetraMultiVector x: " << xnorm << "\n"; |
| 81 |
|
| 82 |
// norm of y |
| 83 |
RealT ynorm = y.norm(); |
| 84 |
outStream << "\nNorm of ROL::TpetraMultiVector y: " << ynorm << "\n"; |
| 85 |
|
| 86 |
// scale x |
| 87 |
x.scale(0.5); |
| 88 |
RealT xnorm2 = x.norm(); |
| 89 |
outStream << "\nNorm of half of x: " << xnorm2 << "\n"; |
| 90 |
if ( std::abs(xnorm/xnorm2 - 2.0) > errtol ) { |
| 91 |
outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 92 |
errorFlag++; |
| 93 |
} |
| 94 |
|
| 95 |
// clone z from x, deep copy x into z, norm of z |
| 96 |
ROL::Ptr<ROL::Vector<RealT> > z = x.clone(); |
| 97 |
z->set(x); |
| 98 |
RealT znorm = z->norm(); |
| 99 |
outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n"; |
| 100 |
if ( std::abs(xnorm2 - znorm) > errtol ) { |
| 101 |
outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 102 |
errorFlag++; |
| 103 |
} |
| 104 |
|
| 105 |
// compute norm of x - x - 0 |
| 106 |
z->set(x); |
| 107 |
x.scale(-1.0); |
| 108 |
z->plus(x); |
| 109 |
y.zero(); |
| 110 |
z->axpy(-1.0, y); |
| 111 |
znorm = z->norm(); |
| 112 |
outStream << "\nNorm of (x - x) - 0: " << znorm << "\n"; |
| 113 |
if ( std::abs(znorm) > errtol ) { |
| 114 |
outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 115 |
errorFlag++; |
| 116 |
} |
| 117 |
|
| 118 |
// set x to first basis vector |
| 119 |
z = x.basis(0); |
| 120 |
znorm = z->norm(); |
| 121 |
outStream << "\nNorm of ROL::Vector z (first basis vector): " << znorm << "\n"; |
| 122 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 123 |
outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 124 |
errorFlag++; |
| 125 |
} |
| 126 |
|
| 127 |
// set x to middle basis vector |
| 128 |
z = x.basis(dim/2); |
| 129 |
znorm = z->norm(); |
| 130 |
outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n"; |
| 131 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 132 |
outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 133 |
errorFlag++; |
| 134 |
} |
| 135 |
|
| 136 |
// set x to last basis vector |
| 137 |
z = x.basis(dim-1); |
| 138 |
znorm = z->norm(); |
| 139 |
outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n"; |
| 140 |
if ( std::abs(znorm-1.0) > errtol ) { |
| 141 |
outStream << "---> POSSIBLE ERROR ABOVE!\n"; |
| 142 |
errorFlag++; |
| 143 |
} |
| 144 |
/*---[ End Test of ROL::TpetraMultiVector methods ] ---*/ |
| 145 |
|
| 146 |
/*---[ Begin Test of optimization using ROL::TpetraMultiVector ]---*/ |
| 147 |
// Set to initial guess |
| 148 |
x_ptr->putScalar(4.0); |
| 149 |
|
| 150 |
// For constructing Zakharov objective |
| 151 |
MVP k_ptr = ROL::makePtr<MV>(map,1,true); |
| 152 |
|
| 153 |
// For gradient and Hessian checks |
| 154 |
MVP xtest_ptr = ROL::makePtr<MV>(map,1,true); |
| 155 |
MVP d_ptr = ROL::makePtr<MV>(map,1,true); |
| 156 |
MVP v_ptr = ROL::makePtr<MV>(map,1,true); |
| 157 |
MVP hv_ptr = ROL::makePtr<MV>(map,1,true); |
| 158 |
MVP ihhv_ptr = ROL::makePtr<MV>(map,1,true); |
| 159 |
|
| 160 |
int numElem = map->getLocalNumElements(); |
| 161 |
|
| 162 |
for( LO lclRow = 0; lclRow < static_cast<LO> (numElem); ++lclRow) { |
| 163 |
const GO gblRow = map->getGlobalElement(lclRow); |
| 164 |
k_ptr->replaceGlobalValue(gblRow,0,gblRow+1.0); |
| 165 |
} |
| 166 |
|
| 167 |
xtest_ptr->randomize(); |
| 168 |
d_ptr->randomize(); |
| 169 |
v_ptr->randomize(); |
| 170 |
|
| 171 |
ROL::Ptr<ROL::Vector<RealT> > k = ROL::makePtr<ROL::TpetraMultiVector<RealT,LO,GO,Node>>(k_ptr); |
| 172 |
|
| 173 |
// Check gradient and Hessian |
| 174 |
ROL::TpetraMultiVector<RealT,LO,GO,Node> xtest(xtest_ptr); |
| 175 |
ROL::TpetraMultiVector<RealT,LO,GO,Node> d(d_ptr); |
| 176 |
ROL::TpetraMultiVector<RealT,LO,GO,Node> v(v_ptr); |
| 177 |
ROL::TpetraMultiVector<RealT,LO,GO,Node> hv(hv_ptr); |
| 178 |
ROL::TpetraMultiVector<RealT,LO,GO,Node> ihhv(ihhv_ptr); |
| 179 |
|
| 180 |
// Create Objective function |
| 181 |
ROL::ZOO::Objective_Zakharov<RealT> obj(k); |
| 182 |
|
| 183 |
|
| 184 |
outStream << "\nChecking Gradient and Hessian: " << znorm << "\n"; |
| 185 |
|
| 186 |
obj.checkGradient(xtest, d, true, outStream); outStream << "\n"; |
| 187 |
obj.checkHessVec(xtest, v, true, outStream); outStream << "\n"; |
| 188 |
obj.checkHessSym(xtest, d, v, true, outStream); outStream << "\n"; |
| 189 |
|
| 190 |
// Check inverse Hessian |
| 191 |
RealT tol=0; |
| 192 |
obj.hessVec(hv,v,xtest,tol); |
| 193 |
obj.invHessVec(ihhv,hv,xtest,tol); |
| 194 |
ihhv.axpy(-1,v); |
| 195 |
outStream << "Checking inverse Hessian" << std::endl; |
| 196 |
outStream << "||H^{-1}Hv-v|| = " << ihhv.norm() << std::endl; |
| 197 |
|
| 198 |
// Set optimization parameters |
| 199 |
Teuchos::ParameterList parlist; |
| 200 |
|
| 201 |
// Trust Region Parameters |
| 202 |
parlist.sublist("Step").sublist("Trust Region").set("Subproblem Solver", "Truncated CG"); |
| 203 |
parlist.sublist("Step").sublist("Trust Region").set("Initial Radius", 0.5); |
| 204 |
parlist.sublist("Step").sublist("Trust Region").set("Maximum Radius", 2.0); |
| 205 |
|
| 206 |
// Krylov Parameters |
| 207 |
parlist.sublist("General").sublist("Krylov").set("Absolute Tolerance", 1.e-4); |
| 208 |
parlist.sublist("General").sublist("Krylov").set("Relative Tolerance", 1.e-2); |
| 209 |
parlist.sublist("General").sublist("Krylov").set("Iteration Limit", 10); |
| 210 |
|
| 211 |
// Define Status Test |
| 212 |
parlist.sublist("Status Test").set("Gradient Tolerance", 1e-12); |
| 213 |
parlist.sublist("Status Test").set("Step Tolerance", 1e-14); |
| 214 |
parlist.sublist("Status Test").set("Iteration Limit", 100); |
| 215 |
|
| 216 |
// Define Algorithm |
| 217 |
ROL::Ptr<ROL::Step<RealT>> |
| 218 |
step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(parlist); |
| 219 |
ROL::Ptr<ROL::StatusTest<RealT>> |
| 220 |
status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist); |
| 221 |
ROL::Algorithm<RealT> algo(step,status,false); |
| 222 |
|
| 223 |
// Run Algorithm |
| 224 |
algo.run(x, obj, true, outStream); |
| 225 |
|
| 226 |
// Compute Error |
| 227 |
RealT abserr = x.norm(); |
| 228 |
outStream << std::scientific << "\n Absolute Error: " << abserr << std::endl; |
| 229 |
if ( abserr > sqrt(ROL::ROL_EPSILON<RealT>()) ) { |
| 230 |
errorFlag += 1; |
| 231 |
} |
| 232 |
/*---[ End Test of optimization using ROL::TpetraMultiVector ]---*/ |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
catch (std::logic_error& err) { |
| 237 |
outStream << err.what() << "\n"; |
| 238 |
errorFlag = -1000; |
| 239 |
}; // end try |
| 240 |
|
| 241 |
if (errorFlag != 0) |
| 242 |
std::cout << "End Result: TEST FAILED\n"; |
| 243 |
else |
| 244 |
std::cout << "End Result: TEST PASSED\n"; |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |