ROL
burgers-control/example_02.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 
50 #include "example_02.hpp"
51 
52 typedef double RealT;
53 
54 int main(int argc, char *argv[]) {
55 
56  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
57 
58  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
59  int iprint = argc - 1;
60  ROL::Ptr<std::ostream> outStream;
61  ROL::nullstream bhs; // outputs nothing
62  if (iprint > 0)
63  outStream = ROL::makePtrFromRef(std::cout);
64  else
65  outStream = ROL::makePtrFromRef(bhs);
66 
67  int errorFlag = 0;
68 
69  // *** Example body.
70 
71  try {
72  // Initialize full objective function.
73  int nx = 256; // Set spatial discretization.
74  RealT alpha = 1.e-3; // Set penalty parameter.
75  RealT nu = 1e-2; // Viscosity parameter.
76  Objective_BurgersControl<RealT> obj(alpha,nx);
77  // Initialize equality constraints
79  ROL::ParameterList list;
80  list.sublist("SimOpt").sublist("Solve").set("Absolute Residual Tolerance",1.e2*ROL::ROL_EPSILON<RealT>());
81  con.setSolveParameters(list);
82  // Initialize iteration vectors.
83  ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
84  ROL::Ptr<std::vector<RealT> > gz_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
85  ROL::Ptr<std::vector<RealT> > yz_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
86  for (int i=0; i<nx+2; i++) {
87  (*z_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
88  (*yz_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
89  }
90  ROL::StdVector<RealT> z(z_ptr);
91  ROL::StdVector<RealT> gz(gz_ptr);
92  ROL::StdVector<RealT> yz(yz_ptr);
93  ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtrFromRef(z);
94  ROL::Ptr<ROL::Vector<RealT> > gzp = ROL::makePtrFromRef(z);
95  ROL::Ptr<ROL::Vector<RealT> > yzp = ROL::makePtrFromRef(yz);
96 
97  ROL::Ptr<std::vector<RealT> > u_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
98  ROL::Ptr<std::vector<RealT> > gu_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
99  ROL::Ptr<std::vector<RealT> > yu_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
100  for (int i=0; i<nx; i++) {
101  (*u_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
102  (*yu_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
103  }
104  ROL::StdVector<RealT> u(u_ptr);
105  ROL::StdVector<RealT> gu(gu_ptr);
106  ROL::StdVector<RealT> yu(yu_ptr);
107  ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtrFromRef(u);
108  ROL::Ptr<ROL::Vector<RealT> > gup = ROL::makePtrFromRef(gu);
109  ROL::Ptr<ROL::Vector<RealT> > yup = ROL::makePtrFromRef(yu);
110 
111  ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
112  ROL::Ptr<std::vector<RealT> > l_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
113  ROL::StdVector<RealT> c(c_ptr);
114  ROL::StdVector<RealT> l(l_ptr);
115 
116  ROL::Vector_SimOpt<RealT> x(up,zp);
117  ROL::Vector_SimOpt<RealT> g(gup,gzp);
118  ROL::Vector_SimOpt<RealT> y(yup,yzp);
119 
120  // Check derivatives.
121  obj.checkGradient(x,x,y,true,*outStream);
122  obj.checkHessVec(x,x,y,true,*outStream);
123  con.checkApplyJacobian(x,y,c,true,*outStream);
124  con.checkApplyAdjointJacobian(x,yu,c,x,true,*outStream);
125  con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream);
126 
127  // Initialize reduced objective function.
128  ROL::Ptr<std::vector<RealT> > p_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
129  ROL::StdVector<RealT> p(p_ptr);
130  ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtrFromRef(p);
131  ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj = ROL::makePtrFromRef(obj);
132  ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon = ROL::makePtrFromRef(con);
133  ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,zp,pp);
134  // Check derivatives.
135  robj.checkGradient(z,z,yz,true,*outStream);
136  robj.checkHessVec(z,z,yz,true,*outStream);
137 
138  // Get parameter list.
139  std::string filename = "input.xml";
140  auto parlist = ROL::getParametersFromXmlFile( filename );
141  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-14);
142  parlist->sublist("Status Test").set("Constraint Tolerance",1.e-14);
143  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
144  parlist->sublist("Status Test").set("Iteration Limit",1000);
145  // Declare ROL algorithm pointer.
146  ROL::Ptr<ROL::Algorithm<RealT> > algo;
147 
148  // Run optimization with Composite Step.
149  algo = ROL::makePtr<ROL::Algorithm<RealT>>("Composite Step",*parlist,false);
150  RealT zerotol = std::sqrt(ROL::ROL_EPSILON<RealT>());
151  z.zero();
152  con.solve(c,u,z,zerotol);
153  c.zero(); l.zero();
154  algo->run(x, g, l, c, obj, con, true, *outStream);
155  ROL::Ptr<ROL::Vector<RealT> > zCS = z.clone();
156  zCS->set(z);
157 
158  // Run Optimization with Trust-Region algorithm.
159  algo = ROL::makePtr<ROL::Algorithm<RealT>>("Trust Region",*parlist,false);
160  z.zero();
161  algo->run(z,robj,true,*outStream);
162 
163  // Check solutions.
164  ROL::Ptr<ROL::Vector<RealT> > err = z.clone();
165  err->set(*zCS); err->axpy(-1.,z);
166  errorFlag += ((err->norm()) > 1.e-8) ? 1 : 0;
167  }
168  catch (std::logic_error err) {
169  *outStream << err.what() << "\n";
170  errorFlag = -1000;
171  }; // end try
172 
173  if (errorFlag != 0)
174  std::cout << "End Result: TEST FAILED\n";
175  else
176  std::cout << "End Result: TEST PASSED\n";
177 
178  return 0;
179 
180 }
181 
Defines the linear algebra or vector space interface for simulation-based optimization.
virtual std::vector< std::vector< Real > > checkApplyAdjointJacobian(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &c, const Vector< Real > &ajv, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS)
Finite-difference check for the application of the adjoint of constraint Jacobian.
void solve(ROL::Vector< Real > &c, ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Given , solve for .
Definition: example_03.hpp:485
virtual void setSolveParameters(ROL::ParameterList &parlist)
Set solve parameters.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
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.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
virtual std::vector< std::vector< Real > > checkApplyAdjointHessian(const Vector< Real > &x, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &hv, const std::vector< Real > &step, const bool printToScreen=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference check for the application of the adjoint of constraint Hessian. ...
virtual std::vector< std::vector< Real > > checkApplyJacobian(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &jv, const std::vector< Real > &steps, const bool printToStream=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference check for the constraint Jacobian application.
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.