ROL
step/fletcher/test_01.cpp
Go to the documentation of this file.
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 
18 #include "ROL_Fletcher.hpp"
19 
20 #include "ROL_Stream.hpp"
21 #include "Teuchos_GlobalMPISession.hpp"
22 
23 
24 #include <iostream>
25 
26 typedef double RealT;
27 
28 int main(int argc, char *argv[]) {
29 
30  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
31 
32  std::string filename = "input.xml";
33 
34  auto parlist = ROL::getParametersFromXmlFile( filename );
35 
37  using V = ROL::Vector<RealT>;
38 
39  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
40  int iprint = argc - 1;
41  ROL::Ptr<std::ostream> outStream;
42  ROL::nullstream bhs; // outputs nothing
43  if (iprint > 0)
44  outStream = ROL::makePtrFromRef(std::cout);
45  else
46  outStream = ROL::makePtrFromRef(bhs);
47 
48  // Save the format state of the original std::cout.
49  ROL::nullstream oldFormatState;
50  oldFormatState.copyfmt(std::cout);
51 
52  int errorFlag = 0;
53 
54  // *** Test body.
55 
56  try {
57 
58  // Set up optimization problem
59  ROL::Ptr<V> x;
60  std::vector<ROL::Ptr<V> > sol;
61  ROL::Ptr<Opt> optProb;
63  SEC.get( optProb, x, sol );
64  ROL::Ptr<V> error = x->clone();
65 
66  // Solve optimization problem
67  ROL::OptimizationSolver<RealT> optSolver(*optProb, *parlist);
68  optSolver.solve(*outStream);
69 
70  error->set(*sol[0]);
71  error->axpy(static_cast<RealT>(-1), *x);
72  RealT solnErr = error->norm();
73 
74  *outStream << "Distance from true solution: " << solnErr << "\n";
75 
76  errorFlag += (solnErr < static_cast<RealT>(1e-6)) ? 0 : 1;
77  }
78  catch (std::logic_error& err) {
79  *outStream << err.what() << "\n";
80  errorFlag = -1000;
81  }; // end try
82 
83  if (errorFlag != 0)
84  std::cout << "End Result: TEST FAILED\n";
85  else
86  std::cout << "End Result: TEST PASSED\n";
87 
88  // reset format state of std::cout
89  std::cout.copyfmt(oldFormatState);
90 
91  return 0;
92 }
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Vector< Real > V
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
void get(Ptr< OptimizationProblem< Real >> &problem, Ptr< Vector< Real >> &x0, std::vector< Ptr< Vector< Real >>> &x) const
Contains definitions for the equality constrained NLP from Nocedal/Wright, 2nd edition, page 574, example 18.2; note the typo in reversing the initial guess and the solution.
Provides a simplified interface for solving a wide range of optimization problems.
int main(int argc, char *argv[])
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.