// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_01.cpp \brief Shows how to use Belos in for a Krylov-Newton method \author Created by G. von Winckel */ #include "ROL_Zakharov.hpp" #include "ROL_BelosKrylov.hpp" #include "ROL_Algorithm.hpp" #include "ROL_LineSearchStep.hpp" #include "ROL_StatusTest.hpp" #include "ROL_Stream.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" #include typedef double RealT; int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; ROL::Ptr outStream; ROL::nullstream bhs; // outputs nothing if (iprint > 0) outStream = ROL::makePtrFromRef(std::cout); else outStream = ROL::makePtrFromRef(bhs); int errorFlag = 0; // *** Example body. try { int dim = 10; ROL::Ptr > step; Teuchos::RCP parlist = Teuchos::rcp( new Teuchos::ParameterList() ); std::string paramfile = "parameters.xml"; Teuchos::updateParametersFromXmlFile(paramfile,parlist.ptr()); // Iteration Vector ROL::Ptr > x_ptr = ROL::makePtr>(dim, 0.0); // Vector of natural numbers ROL::Ptr > k_ptr = ROL::makePtr>(dim, 0.0); for (int i=0; i > k = ROL::makePtr>(k_ptr); ROL::StdVector x(x_ptr); ROL::ZOO::Objective_Zakharov obj(k); // Make a Belos-Krylov solver if specified if(parlist->get("Use Belos",false)) { ROL::Ptr > krylov = ROL::makePtr>(*parlist); step = ROL::makePtr>(*parlist,ROL::nullPtr,ROL::nullPtr,krylov); } else { // Otherwise use ROL's default step = ROL::makePtr>(*parlist); } // Define Status Test RealT gtol = 1e-12; // norm of gradient tolerance RealT stol = 1e-14; // norm of step tolerance int maxit = 100; // maximum number of iterations ROL::Ptr > status = ROL::makePtr>(gtol, stol, maxit); // Define Algorithm ROL::Algorithm algo(step,status,false); // Run Algorithm algo.run(x, obj, true, *outStream); // Get True Solution ROL::Ptr > xtrue_ptr = ROL::makePtr>(dim, 0.0); ROL::StdVector xtrue(xtrue_ptr); // Compute Error x.axpy(-1.0, xtrue); RealT abserr = x.norm(); *outStream << std::scientific << "\n Absolute Error: " << abserr << std::endl; if ( abserr > sqrt(ROL::ROL_EPSILON()) ) { errorFlag += 1; } } catch (std::logic_error& err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }