ROL
burgers-control/example_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 
17 #include "ROL_Bounds.hpp"
18 
19 #include "Teuchos_GlobalMPISession.hpp"
20 #include "Teuchos_XMLParameterListHelpers.hpp"
21 #include "Teuchos_LAPACK.hpp"
22 
23 #include <iostream>
24 #include <fstream>
25 #include <algorithm>
26 
27 #include "ROL_Stream.hpp"
28 
29 #include "example_01.hpp"
30 
31 typedef double RealT;
32 
33 int main(int argc, char *argv[]) {
34 
35  typedef std::vector<RealT> vector;
36  typedef ROL::Vector<RealT> V;
37  typedef ROL::StdVector<RealT> SV;
38 
39  typedef typename vector::size_type uint;
40 
41  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
42 
43  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
44  int iprint = argc - 1;
45  ROL::Ptr<std::ostream> outStream;
46  ROL::nullstream bhs; // outputs nothing
47  if (iprint > 0)
48  outStream = ROL::makePtrFromRef(std::cout);
49  else
50  outStream = ROL::makePtrFromRef(bhs);
51 
52  int errorFlag = 0;
53 
54  // *** Example body.
55 
56  try {
57  // Initialize objective function.
58  uint nx = 1028; // Set spatial discretization.
59  RealT alpha = 1.e-3; // Set penalty parameter.
60  Objective_BurgersControl<RealT> obj(alpha,nx);
61  // Initialize iteration vectors.
62  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(nx+2, 1.0);
63  ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(nx+2, 0.0);
64  for (uint i=0; i<nx+2; i++) {
65  (*x_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
66  (*y_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
67  }
68 
69  SV x(x_ptr);
70  SV y(y_ptr);
71 
72  // Check derivatives.
73  obj.checkGradient(x,x,y,true,*outStream);
74  obj.checkHessVec(x,x,y,true,*outStream);
75 
76  // Initialize Constraints
77  ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(nx+2,0.0);
78  ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(nx+2,1.0);
79  ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
80  ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
81 
82  ROL::Bounds<RealT> bcon(lo,up);
83 
84  // Primal dual active set.
85  std::string filename = "input.xml";
86  auto parlist = ROL::getParametersFromXmlFile( filename );
87 
88  // Krylov parameters.
89  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-8);
90  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-4);
91  parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
92  // PDAS parameters.
93  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
94  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
95  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit", 10);
96  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",(alpha>0.0)?alpha:1.e-4);
97  // Status test parameters.
98  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
99  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
100  parlist->sublist("Status Test").set("Iteration Limit",100);
101  // Set initial guess.
102  x.zero();
103  {
104  // Define algorithm.
106  // Run algorithm.
107  algo.run(x, obj, bcon, *outStream);
108  }
109  // Output control to file.
110  std::ofstream file_pdas;
111  file_pdas.open("control_PDAS.txt");
112  for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
113  file_pdas << (*x_ptr)[i] << "\n";
114  }
115  file_pdas.close();
116 
117  // Projected Newton.
118  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-4);
119  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-2);
120  parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
121  // Set initial guess.
122  y.zero();
123  {
124  // Define algorithm.
126  // Run Algorithm
127  algo.run(y,obj,bcon,*outStream);
128  }
129  // Output control to file.
130  std::ofstream file_tr;
131  file_tr.open("control_TR.txt");
132  for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
133  file_tr << (*y_ptr)[i] << "\n";
134  }
135  file_tr.close();
136  // Output state to file.
137  std::vector<RealT> u(nx,0.0);
138  std::vector<RealT> param(4,0.0);
139  obj.solve_state(u,*x_ptr,param);
140  std::ofstream file;
141  file.open("state.txt");
142  for (unsigned i=0; i<(unsigned)nx; i++) {
143  file << i/((RealT)(nx+1)) << " " << u[i] << "\n";
144  }
145  file.close();
146 
147  // Compute error between PDAS and Lin-More solutions.
148  ROL::Ptr<ROL::Vector<RealT> > diff = x.clone();
149  diff->set(x);
150  diff->axpy(-1.0,y);
151  RealT error = diff->norm();
152  *outStream << "\nError between PDAS solution and TR solution is " << error << "\n";
153  errorFlag = ((error > 1e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
154  }
155  catch (std::logic_error& err) {
156  *outStream << err.what() << "\n";
157  errorFlag = -1000;
158  }; // end try
159 
160  if (errorFlag != 0)
161  std::cout << "End Result: TEST FAILED\n";
162  else
163  std::cout << "End Result: TEST PASSED\n";
164 
165  return 0;
166 
167 }
168 
typename PV< Real >::size_type size_type
Provides an interface to run the trust-region algorithm of Lin and More.
void solve_state(std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > &param)
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...
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
Vector< Real > V
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
Provides the elementwise interface to apply upper and lower bound constraints.
Definition: ROL_Bounds.hpp:25
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
Provides an interface to run the projected secant algorithm.
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...