| 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 use Belos in for a Krylov-Newton method |
| 12 |
\author Created by G. von Winckel |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
#include "ROL_Zakharov.hpp" |
| 17 |
#include "ROL_BelosKrylov.hpp" |
| 18 |
#include "ROL_Algorithm.hpp" |
| 19 |
#include "ROL_LineSearchStep.hpp" |
| 20 |
#include "ROL_StatusTest.hpp" |
| 21 |
|
| 22 |
#include "ROL_Stream.hpp" |
| 23 |
#include "Teuchos_GlobalMPISession.hpp" |
| 24 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 25 |
|
| 26 |
#include <cstdlib> |
| 27 |
|
| 28 |
typedef double RealT; |
| 29 |
|
| 30 |
int main(int argc, char *argv[]) { |
| 31 |
|
| 32 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); |
| 33 |
|
| 34 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 35 |
int iprint = argc - 1; |
| 36 |
ROL::Ptr<std::ostream> outStream; |
| 37 |
ROL::nullstream bhs; // outputs nothing |
| 38 |
if (iprint > 0) |
| 39 |
outStream = ROL::makePtrFromRef(std::cout); |
| 40 |
else |
| 41 |
outStream = ROL::makePtrFromRef(bhs); |
| 42 |
|
| 43 |
int errorFlag = 0; |
| 44 |
|
| 45 |
// *** Example body. |
| 46 |
|
| 47 |
try { |
| 48 |
|
| 49 |
int dim = 10; |
| 50 |
|
| 51 |
ROL::Ptr<ROL::Step<RealT> > step; |
| 52 |
|
| 53 |
|
| 54 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 55 |
std::string paramfile = "parameters.xml"; |
| 56 |
Teuchos::updateParametersFromXmlFile(paramfile,parlist.ptr()); |
| 57 |
|
| 58 |
// Iteration Vector |
| 59 |
ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0); |
| 60 |
|
| 61 |
// Vector of natural numbers |
| 62 |
ROL::Ptr<std::vector<RealT> > k_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0); |
| 63 |
|
| 64 |
for (int i=0; i<dim; i++) { |
| 65 |
(*x_ptr)[i] = 4; |
| 66 |
(*k_ptr)[i] = i+1.0; |
| 67 |
} |
| 68 |
|
| 69 |
ROL::Ptr<ROL::Vector<RealT> > k = ROL::makePtr<ROL::StdVector<RealT>>(k_ptr); |
| 70 |
ROL::StdVector<RealT> x(x_ptr); |
| 71 |
|
| 72 |
ROL::ZOO::Objective_Zakharov<RealT> obj(k); |
| 73 |
|
| 74 |
// Make a Belos-Krylov solver if specified |
| 75 |
if(parlist->get("Use Belos",false)) { |
| 76 |
ROL::Ptr<ROL::Krylov<RealT> > krylov = ROL::makePtr<ROL::BelosKrylov<RealT>>(*parlist); |
| 77 |
step = ROL::makePtr<ROL::LineSearchStep<RealT>>(*parlist,ROL::nullPtr,ROL::nullPtr,krylov); |
| 78 |
} |
| 79 |
else { // Otherwise use ROL's default |
| 80 |
step = ROL::makePtr<ROL::LineSearchStep<RealT>>(*parlist); |
| 81 |
} |
| 82 |
|
| 83 |
// Define Status Test |
| 84 |
RealT gtol = 1e-12; // norm of gradient tolerance |
| 85 |
RealT stol = 1e-14; // norm of step tolerance |
| 86 |
int maxit = 100; // maximum number of iterations |
| 87 |
ROL::Ptr<ROL::StatusTest<RealT> > status = ROL::makePtr<ROL::StatusTest<RealT>>(gtol, stol, maxit); |
| 88 |
|
| 89 |
// Define Algorithm |
| 90 |
ROL::Algorithm<RealT> algo(step,status,false); |
| 91 |
|
| 92 |
// Run Algorithm |
| 93 |
algo.run(x, obj, true, *outStream); |
| 94 |
|
| 95 |
// Get True Solution |
| 96 |
ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0); |
| 97 |
ROL::StdVector<RealT> xtrue(xtrue_ptr); |
| 98 |
|
| 99 |
// Compute Error |
| 100 |
x.axpy(-1.0, xtrue); |
| 101 |
RealT abserr = x.norm(); |
| 102 |
*outStream << std::scientific << "\n Absolute Error: " << abserr << std::endl; |
| 103 |
if ( abserr > sqrt(ROL::ROL_EPSILON<RealT>()) ) { |
| 104 |
errorFlag += 1; |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
catch (std::logic_error& err) { |
| 109 |
*outStream << err.what() << "\n"; |
| 110 |
errorFlag = -1000; |
| 111 |
}; // end try |
| 112 |
|
| 113 |
if (errorFlag != 0) |
| 114 |
std::cout << "End Result: TEST FAILED\n"; |
| 115 |
else |
| 116 |
std::cout << "End Result: TEST PASSED\n"; |
| 117 |
|
| 118 |
return 0; |
| 119 |
|
| 120 |
} |
| 121 |
|