ROL
burgers-control/example_01.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
49 #include "example_01.hpp"
50 
51 typedef double RealT;
52 
53 int main(int argc, char *argv[]) {
54 
55  typedef std::vector<RealT> vector;
56  typedef ROL::Vector<RealT> V;
57  typedef ROL::StdVector<RealT> SV;
58 
59  typedef typename vector::size_type uint;
60 
61 
62 
63  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
64 
65  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
66  int iprint = argc - 1;
67  ROL::Ptr<std::ostream> outStream;
68  ROL::nullstream bhs; // outputs nothing
69  if (iprint > 0)
70  outStream = ROL::makePtrFromRef(std::cout);
71  else
72  outStream = ROL::makePtrFromRef(bhs);
73 
74  int errorFlag = 0;
75 
76  // *** Example body.
77 
78  try {
79  // Initialize objective function.
80  uint nx = 1028; // Set spatial discretization.
81  RealT alpha = 1.e-3; // Set penalty parameter.
82  Objective_BurgersControl<RealT> obj(alpha,nx);
83  // Initialize iteration vectors.
84  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(nx+2, 1.0);
85  ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(nx+2, 0.0);
86  for (uint i=0; i<nx+2; i++) {
87  (*x_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
88  (*y_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
89  }
90 
91  SV x(x_ptr);
92  SV y(y_ptr);
93 
94  // Check deriatives.
95  obj.checkGradient(x,x,y,true,*outStream);
96  obj.checkHessVec(x,x,y,true,*outStream);
97 
98  // Initialize Constraints
99  ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(nx+2,0.0);
100  ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(nx+2,1.0);
101  ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
102  ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
103 
104  ROL::Bounds<RealT> icon(lo,up);
105 
106  // ROL components.
107  ROL::Ptr<ROL::Algorithm<RealT> > algo;
108 
109  // Primal dual active set.
110  std::string filename = "input.xml";
111  auto parlist = ROL::getParametersFromXmlFile( filename );
112 
113  // Krylov parameters.
114  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-8);
115  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-4);
116  parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
117  // PDAS parameters.
118  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
119  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
120  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit", 10);
121  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",(alpha>0.0)?alpha:1.e-4);
122  // Status test parameters.
123  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
124  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
125  parlist->sublist("Status Test").set("Iteration Limit",100);
126  // Define algorithm.
127  algo = ROL::makePtr<ROL::Algorithm<RealT>>("Primal Dual Active Set",*parlist,false);
128  // Run algorithm.
129  x.zero();
130  algo->run(x, obj, icon, true, *outStream);
131  // Output control to file.
132  std::ofstream file_pdas;
133  file_pdas.open("control_PDAS.txt");
134  for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
135  file_pdas << (*x_ptr)[i] << "\n";
136  }
137  file_pdas.close();
138 
139  // Projected Newton.
140  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-4);
141  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-2);
142  parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
143  // Define algorithm.
144  algo = ROL::makePtr<ROL::Algorithm<RealT>>("Trust Region",*parlist,false);
145  // Run Algorithm
146  y.zero();
147  algo->run(y,obj,icon,true,*outStream);
148  // Output control to file.
149  std::ofstream file_tr;
150  file_tr.open("control_TR.txt");
151  for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
152  file_tr << (*y_ptr)[i] << "\n";
153  }
154  file_tr.close();
155  // Output state to file.
156  std::vector<RealT> u(nx,0.0);
157  std::vector<RealT> param(4,0.0);
158  obj.solve_state(u,*x_ptr,param);
159  std::ofstream file;
160  file.open("state.txt");
161  for (unsigned i=0; i<(unsigned)nx; i++) {
162  file << i/((RealT)(nx+1)) << " " << u[i] << "\n";
163  }
164  file.close();
165  // Compute error
166  ROL::Ptr<ROL::Vector<RealT> > diff = x.clone();
167  diff->set(x);
168  diff->axpy(-1.0,y);
169  RealT error = diff->norm();
170  *outStream << "\nError between PDAS solution and TR solution is " << error << "\n";
171  errorFlag = ((error > 1e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
172  }
173  catch (std::logic_error err) {
174  *outStream << err.what() << "\n";
175  errorFlag = -1000;
176  }; // end try
177 
178  if (errorFlag != 0)
179  std::cout << "End Result: TEST FAILED\n";
180  else
181  std::cout << "End Result: TEST PASSED\n";
182 
183  return 0;
184 
185 }
186 
typename PV< Real >::size_type size_type
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:80
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...
Provides the elementwise interface to apply upper and lower bound constraints.
Definition: ROL_Bounds.hpp:59
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
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.