ROL
burgers-control/example_03.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_03.hpp"
50 
51 typedef double RealT;
52 
53 int main(int argc, char *argv[]) {
54 
55  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
56 
57  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
58  int iprint = argc - 1;
59  ROL::Ptr<std::ostream> outStream;
60  ROL::nullstream bhs; // outputs nothing
61  if (iprint > 0)
62  outStream = ROL::makePtrFromRef(std::cout);
63  else
64  outStream = ROL::makePtrFromRef(bhs);
65 
66  int errorFlag = 0;
67 
68  // *** Example body.
69 
70  try {
71  // Initialize full objective function.
72  int nx = 80; // Set spatial discretization.
73  int nt = 80; // Set temporal discretization.
74  RealT T = 1.0; // Set end time.
75  RealT alpha = 5e-2; // Set penalty parameter.
76  RealT nu = 1e-2; // Set viscosity parameter.
77  Objective_BurgersControl<RealT> obj(alpha,nx,nt,T);
78  // Initialize equality constraints
79  Constraint_BurgersControl<RealT> con(nx, nt, T, nu);
80  // Initialize iteration vectors.
81  ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>((nx+2)*(nt+1), 1.0);
82  ROL::Ptr<std::vector<RealT> > gz_ptr = ROL::makePtr<std::vector<RealT>>((nx+2)*(nt+1), 1.0);
83  ROL::Ptr<std::vector<RealT> > yz_ptr = ROL::makePtr<std::vector<RealT>>((nx+2)*(nt+1), 1.0);
84  for (int i=0; i<(nx+2)*(nt+1); i++) {
85  (*z_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
86  (*yz_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
87  }
88  ROL::StdVector<RealT> z(z_ptr);
89  ROL::StdVector<RealT> gz(gz_ptr);
90  ROL::StdVector<RealT> yz(yz_ptr);
91  ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtrFromRef(z);
92  ROL::Ptr<ROL::Vector<RealT> > gzp = ROL::makePtrFromRef(gz);
93  ROL::Ptr<ROL::Vector<RealT> > yzp = ROL::makePtrFromRef(yz);
94 
95  ROL::Ptr<std::vector<RealT> > u_ptr = ROL::makePtr<std::vector<RealT>>(nx*nt, 1.0);
96  ROL::Ptr<std::vector<RealT> > gu_ptr = ROL::makePtr<std::vector<RealT>>(nx*nt, 1.0);
97  ROL::Ptr<std::vector<RealT> > yu_ptr = ROL::makePtr<std::vector<RealT>>(nx*nt, 1.0);
98  for (int i=0; i<nx*nt; i++) {
99  (*u_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
100  (*yu_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
101  }
102  ROL::StdVector<RealT> u(u_ptr);
103  ROL::StdVector<RealT> gu(gu_ptr);
104  ROL::StdVector<RealT> yu(yu_ptr);
105  ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtrFromRef(u);
106  ROL::Ptr<ROL::Vector<RealT> > gup = ROL::makePtrFromRef(gu);
107  ROL::Ptr<ROL::Vector<RealT> > yup = ROL::makePtrFromRef(yu);
108 
109  ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(nx*nt, 1.0);
110  ROL::Ptr<std::vector<RealT> > l_ptr = ROL::makePtr<std::vector<RealT>>(nx*nt, 1.0);
111  ROL::StdVector<RealT> c(c_ptr);
112  ROL::StdVector<RealT> l(l_ptr);
113 
114  ROL::Vector_SimOpt<RealT> x(up,zp);
115  ROL::Vector_SimOpt<RealT> g(gup,gzp);
116  ROL::Vector_SimOpt<RealT> y(yup,yzp);
117  // Check derivatives.
118  obj.checkGradient(x,x,y,true,*outStream);
119  obj.checkHessVec(x,x,y,true,*outStream);
120  con.checkApplyJacobian(x,y,c,true,*outStream);
121  //con.checkApplyAdjointJacobian(x,yu,c,x,true,*outStream);
122  con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream);
123  // Check consistency of Jacobians and adjoint Jacobians.
124  con.checkAdjointConsistencyJacobian_1(c,yu,u,z,true,*outStream);
125  con.checkAdjointConsistencyJacobian_2(c,yz,u,z,true,*outStream);
126  // Check consistency of solves.
127  con.checkSolve(u,z,c,true,*outStream);
128  con.checkInverseJacobian_1(c,yu,u,z,true,*outStream);
129  con.checkInverseAdjointJacobian_1(yu,c,u,z,true,*outStream);
130 
131  // Initialize reduced objective function.
132  ROL::Ptr<std::vector<RealT> > p_ptr = ROL::makePtr<std::vector<RealT>>(nx*nt, 1.0);
133  ROL::StdVector<RealT> p(p_ptr);
134  ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtrFromRef(p);
135  ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj = ROL::makePtrFromRef(obj);
136  ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon = ROL::makePtrFromRef(con);
137  ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,zp,pp);
138  // Check derivatives.
139  robj.checkGradient(z,z,yz,true,*outStream);
140  robj.checkHessVec(z,z,yz,true,*outStream);
141  // Get input parameter list.
142  std::string filename = "input.xml";
143  auto parlist = ROL::getParametersFromXmlFile( filename );
144  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-10);
145  parlist->sublist("Status Test").set("Constraint Tolerance",1.e-10);
146  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
147  parlist->sublist("Status Test").set("Iteration Limit",100);
148  // Build Algorithm pointer.
149  ROL::Ptr<ROL::Algorithm<RealT>> algo;
150  ROL::Ptr<ROL::Step<RealT>> step;
151  ROL::Ptr<ROL::StatusTest<RealT>> status;
152 
153  // Solve using trust regions.
154  step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
155  status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
156  algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
157  z.zero();
158  std::clock_t timer_tr = std::clock();
159  algo->run(z,robj,true,*outStream);
160  *outStream << "Trust-Region Newton required " << (std::clock()-timer_tr)/(RealT)CLOCKS_PER_SEC
161  << " seconds.\n";
162  ROL::Ptr<ROL::Vector<RealT> > zTR = z.clone();
163  zTR->set(z);
164 
165  // Solve using a composite step method.
166  step = ROL::makePtr<ROL::CompositeStep<RealT>>(*parlist);
167  status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist);
168  algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
169  x.zero();
170  ROL::Elementwise::Fill<RealT> setFunc(0.25);
171  x.applyUnary(setFunc);
172  std::clock_t timer_cs = std::clock();
173  algo->run(x,g,l,c,obj,con,true,*outStream);
174  *outStream << "Composite Step required " << (std::clock()-timer_cs)/(RealT)CLOCKS_PER_SEC
175  << " seconds.\n";
176 
177  // Compute error between solutions
178  ROL::Ptr<ROL::Vector<RealT> > err = z.clone();
179  err->set(*zTR); err->axpy(-1.,z);
180  errorFlag += (err->norm() > 1.e-4) ? 1 : 0;
181  if (errorFlag) {
182  *outStream << "\n\nControl error = " << err->norm() << "\n";
183  }
184 
185 // std::ofstream control;
186 // control.open("control.txt");
187 // for (int t = 0; t < nt+1; t++) {
188 // for (int n = 0; n < nx+2; n++) {
189 // control << (RealT)t/(RealT)nt << " "
190 // << (RealT)n/((RealT)(nx+1)) << " "
191 // << (*z_ptr)[t*(nx+2)+n] << "\n";
192 // }
193 // }
194 // control.close();
195 //
196 // std::ofstream state;
197 // state.open("state.txt");
198 // for (int t = 0; t < nt; t++) {
199 // for (int n = 0; n < nx; n++) {
200 // state << (RealT)(t+1)/(RealT)nt << " "
201 // << (RealT)(n+1)/((RealT)(nx+1)) << " "
202 // << (*u_ptr)[t*nx+n] << "\n";
203 // }
204 // }
205 // state.close();
206  }
207  catch (std::logic_error& err) {
208  *outStream << err.what() << "\n";
209  errorFlag = -1000;
210  }; // end try
211 
212  if (errorFlag != 0)
213  std::cout << "End Result: TEST FAILED\n";
214  else
215  std::cout << "End Result: TEST PASSED\n";
216 
217  return 0;
218 
219 }
220 
virtual Real checkSolve(const Vector< Real > &u, const Vector< Real > &z, const Vector< Real > &c, const bool printToStream=true, std::ostream &outStream=std::cout)
Defines the linear algebra or vector space interface for simulation-based optimization.
virtual Real checkAdjointConsistencyJacobian_2(const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
Check the consistency of the Jacobian and its adjoint. This is the primary interface.
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.
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
virtual Real checkInverseJacobian_1(const Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
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.
virtual Real checkAdjointConsistencyJacobian_1(const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
Check the consistency of the Jacobian and its adjoint. This is the primary interface.
virtual Real checkInverseAdjointJacobian_1(const Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)