ROL
example_05.hpp
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 
44 #include "Teuchos_ParameterList.hpp"
45 #include "Teuchos_XMLParameterListHelpers.hpp"
46 #include "Teuchos_oblackholestream.hpp"
47 #include "Teuchos_LAPACK.hpp"
48 #include "Teuchos_GlobalMPISession.hpp"
49 #include "Teuchos_Comm.hpp"
50 #include "Teuchos_DefaultComm.hpp"
51 #include "Teuchos_CommHelpers.hpp"
52 
53 // ROL_Types contains predefined constants and objects
54 #include "ROL_Types.hpp"
55 // ROL algorithmic information
56 #include "ROL_Algorithm.hpp"
57 // ROL vectors
58 #include "ROL_StdVector.hpp"
59 #include "ROL_CVaRVector.hpp"
60 // ROL objective functions and constraints
65 // ROL sample generators
68 // ROL CVaR definitions
70 #include "ROL_PlusFunction.hpp"
71 #include "ROL_CVaR.hpp"
72 
73 template<class Real>
75 private:
76  int nx_;
77  Real dx_;
78 
79  Real compute_norm(const std::vector<Real> &r) {
80  return std::sqrt(dot(r,r));
81  }
82 
83  Real dot(const std::vector<Real> &x, const std::vector<Real> &y) {
84  Real ip = 0.0;
85  Real c = (((int)x.size()==nx_) ? 4.0 : 2.0);
86  for (unsigned i=0; i<x.size(); i++) {
87  if ( i == 0 ) {
88  ip += dx_/6.0*(c*x[i] + x[i+1])*y[i];
89  }
90  else if ( i == x.size()-1 ) {
91  ip += dx_/6.0*(x[i-1] + c*x[i])*y[i];
92  }
93  else {
94  ip += dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
95  }
96  }
97  return ip;
98  }
99 
100  void update(std::vector<Real> &u, const std::vector<Real> &s, const Real alpha=1.0) {
101  for (unsigned i=0; i<u.size(); i++) {
102  u[i] += alpha*s[i];
103  }
104  }
105 
106  void scale(std::vector<Real> &u, const Real alpha=0.0) {
107  for (unsigned i=0; i<u.size(); i++) {
108  u[i] *= alpha;
109  }
110  }
111 
112  void compute_residual(std::vector<Real> &r, const std::vector<Real> &u,
113  const std::vector<Real> &z) {
114  r.clear(); r.resize(nx_,0.0);
115  const std::vector<Real> param =
117  Real nu = std::pow(10.0,param[0]-2.0);
118  Real f = param[1]/100.0;
119  Real u0 = 1.0+param[2]/1000.0;
120  Real u1 = param[3]/1000.0;
121  for (int i=0; i<nx_; i++) {
122  // Contribution from stiffness term
123  if ( i==0 ) {
124  r[i] = nu/dx_*(2.0*u[i]-u[i+1]);
125  }
126  else if (i==nx_-1) {
127  r[i] = nu/dx_*(2.0*u[i]-u[i-1]);
128  }
129  else {
130  r[i] = nu/dx_*(2.0*u[i]-u[i-1]-u[i+1]);
131  }
132  // Contribution from nonlinear term
133  if (i<nx_-1){
134  r[i] += u[i+1]*(u[i]+u[i+1])/6.0;
135  }
136  if (i>0) {
137  r[i] -= u[i-1]*(u[i-1]+u[i])/6.0;
138  }
139  // Contribution from control
140  r[i] -= dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
141  // Contribution from right-hand side
142  r[i] -= dx_*f;
143  }
144  // Contribution from Dirichlet boundary terms
145  r[ 0] -= u0*u[ 0]/6.0 + u0*u0/6.0 + nu*u0/dx_;
146  r[nx_-1] += u1*u[nx_-1]/6.0 + u1*u1/6.0 - nu*u1/dx_;
147  }
148 
149  void compute_pde_jacobian(std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
150  const std::vector<Real> &u) {
151  const std::vector<Real> param =
153  Real nu = std::pow(10.0,param[0]-2.0);
154  Real u0 = 1.0+param[2]/1000.0;
155  Real u1 = param[3]/1000.0;
156  // Get Diagonal and Off-Diagonal Entries of linear PDE Jacobian
157  d.clear(); d.resize(nx_,nu*2.0/dx_);
158  dl.clear(); dl.resize(nx_-1,-nu/dx_);
159  du.clear(); du.resize(nx_-1,-nu/dx_);
160  // Contribution from nonlinearity
161  for (int i=0; i<nx_; i++) {
162  if (i<nx_-1) {
163  dl[i] += (-2.0*u[i]-u[i+1])/6.0;
164  d[i] += u[i+1]/6.0;
165  }
166  if (i>0) {
167  d[i] += -u[i-1]/6.0;
168  du[i-1] += (u[i-1]+2.0*u[i])/6.0;
169  }
170  }
171  // Contribution from Dirichlet boundary conditions
172  d[ 0] -= u0/6.0;
173  d[nx_-1] += u1/6.0;
174  }
175 
176  void linear_solve(std::vector<Real> &u, std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
177  const std::vector<Real> &r, const bool transpose = false) {
178  u.assign(r.begin(),r.end());
179  // Perform LDL factorization
180  Teuchos::LAPACK<int,Real> lp;
181  std::vector<Real> du2(nx_-2,0.0);
182  std::vector<int> ipiv(nx_,0);
183  int info;
184  int ldb = nx_;
185  int nhrs = 1;
186  lp.GTTRF(nx_,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info);
187  char trans = 'N';
188  if ( transpose ) {
189  trans = 'T';
190  }
191  lp.GTTRS(trans,nx_,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info);
192  }
193 
194 public:
195 
196  EqualityConstraint_BurgersControl(int nx = 128) : nx_(nx), dx_(1.0/((Real)nx+1.0)) {}
197 
199  const ROL::Vector<Real> &z, Real &tol) {
200  Teuchos::RCP<std::vector<Real> > cp =
201  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(c)).getVector());
202  Teuchos::RCP<const std::vector<Real> > up =
203  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
204  Teuchos::RCP<const std::vector<Real> > zp =
205  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
206  compute_residual(*cp,*up,*zp);
207  }
208 
209  void solve(ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
210  Teuchos::RCP<std::vector<Real> > up =
211  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(u)).getVector());
212  up->assign(up->size(),z.norm()/up->size());
213  Teuchos::RCP<const std::vector<Real> > zp =
214  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
215  // Compute residual and residual norm
216  std::vector<Real> r(up->size(),0.0);
217  compute_residual(r,*up,*zp);
218  Real rnorm = compute_norm(r);
219  // Define tolerances
220  Real rtol = 1.e2*ROL::ROL_EPSILON;
221  Real maxit = 500;
222  // Initialize Jacobian storage
223  std::vector<Real> d(nx_,0.0);
224  std::vector<Real> dl(nx_-1,0.0);
225  std::vector<Real> du(nx_-1,0.0);
226  // Iterate Newton's method
227  Real alpha = 1.0, tmp = 0.0;
228  std::vector<Real> s(nx_,0.0);
229  std::vector<Real> utmp(nx_,0.0);
230  for (int i=0; i<maxit; i++) {
231  //std::cout << i << " " << rnorm << "\n";
232  // Get Jacobian
233  compute_pde_jacobian(dl,d,du,*up);
234  // Solve Newton system
235  linear_solve(s,dl,d,du,r);
236  // Perform line search
237  tmp = rnorm;
238  alpha = 1.0;
239  utmp.assign(up->begin(),up->end());
240  update(utmp,s,-alpha);
241  compute_residual(r,utmp,*zp);
242  rnorm = compute_norm(r);
243  while ( rnorm > (1.0-1.e-4*alpha)*tmp && alpha > std::sqrt(ROL::ROL_EPSILON) ) {
244  alpha /= 2.0;
245  utmp.assign(up->begin(),up->end());
246  update(utmp,s,-alpha);
247  compute_residual(r,utmp,*zp);
248  rnorm = compute_norm(r);
249  }
250  // Update iterate
251  up->assign(utmp.begin(),utmp.end());
252  if ( rnorm < rtol ) {
253  break;
254  }
255  }
256  }
257 
259  const ROL::Vector<Real> &z, Real &tol) {
260  Teuchos::RCP<std::vector<Real> > jvp =
261  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(jv)).getVector());
262  Teuchos::RCP<const std::vector<Real> > vp =
263  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
264  Teuchos::RCP<const std::vector<Real> > up =
265  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
266  Teuchos::RCP<const std::vector<Real> > zp =
267  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
268  const std::vector<Real> param =
270  Real nu = std::pow(10.0,param[0]-2.0);
271  Real u0 = 1.0+param[2]/1000.0;
272  Real u1 = param[3]/1000.0;
273  // Fill jvp
274  for (int i = 0; i < nx_; i++) {
275  (*jvp)[i] = nu/dx_*2.0*(*vp)[i];
276  if ( i > 0 ) {
277  (*jvp)[i] += -nu/dx_*(*vp)[i-1]
278  -(*up)[i-1]/6.0*(*vp)[i]
279  -((*up)[i]+2.0*(*up)[i-1])/6.0*(*vp)[i-1];
280  }
281  if ( i < nx_-1 ) {
282  (*jvp)[i] += -nu/dx_*(*vp)[i+1]
283  +(*up)[i+1]/6.0*(*vp)[i]
284  +((*up)[i]+2.0*(*up)[i+1])/6.0*(*vp)[i+1];
285  }
286  }
287  (*jvp)[ 0] -= u0/6.0*(*vp)[0];
288  (*jvp)[nx_-1] += u1/6.0*(*vp)[nx_-1];
289  }
290 
292  const ROL::Vector<Real> &z, Real &tol) {
293  Teuchos::RCP<std::vector<Real> > jvp =
294  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(jv)).getVector());
295  Teuchos::RCP<const std::vector<Real> > vp =
296  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
297  Teuchos::RCP<const std::vector<Real> > up =
298  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
299  Teuchos::RCP<const std::vector<Real> > zp =
300  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
301  for (int i=0; i<nx_; i++) {
302  // Contribution from control
303  (*jvp)[i] = -dx_/6.0*((*vp)[i]+4.0*(*vp)[i+1]+(*vp)[i+2]);
304  }
305  }
306 
308  const ROL::Vector<Real> &z, Real &tol) {
309  Teuchos::RCP<std::vector<Real> > ijvp =
310  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(ijv)).getVector());
311  Teuchos::RCP<const std::vector<Real> > vp =
312  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
313  Teuchos::RCP<const std::vector<Real> > up =
314  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
315  Teuchos::RCP<const std::vector<Real> > zp =
316  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
317  // Get PDE Jacobian
318  std::vector<Real> d(nx_,0.0);
319  std::vector<Real> dl(nx_-1,0.0);
320  std::vector<Real> du(nx_-1,0.0);
321  compute_pde_jacobian(dl,d,du,*up);
322  // Solve solve state sensitivity system at current time step
323  linear_solve(*ijvp,dl,d,du,*vp);
324  }
325 
327  const ROL::Vector<Real> &z, Real &tol) {
328  Teuchos::RCP<std::vector<Real> > jvp =
329  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(ajv)).getVector());
330  Teuchos::RCP<const std::vector<Real> > vp =
331  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
332  Teuchos::RCP<const std::vector<Real> > up =
333  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
334  Teuchos::RCP<const std::vector<Real> > zp =
335  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
336  const std::vector<Real> param =
338  Real nu = std::pow(10.0,param[0]-2.0);
339  Real u0 = 1.0+param[2]/1000.0;
340  Real u1 = param[3]/1000.0;
341  // Fill jvp
342  for (int i = 0; i < nx_; i++) {
343  (*jvp)[i] = nu/dx_*2.0*(*vp)[i];
344  if ( i > 0 ) {
345  (*jvp)[i] += -nu/dx_*(*vp)[i-1]
346  -(*up)[i-1]/6.0*(*vp)[i]
347  +((*up)[i-1]+2.0*(*up)[i])/6.0*(*vp)[i-1];
348  }
349  if ( i < nx_-1 ) {
350  (*jvp)[i] += -nu/dx_*(*vp)[i+1]
351  +(*up)[i+1]/6.0*(*vp)[i]
352  -((*up)[i+1]+2.0*(*up)[i])/6.0*(*vp)[i+1];
353  }
354  }
355  (*jvp)[ 0] -= u0/6.0*(*vp)[0];
356  (*jvp)[nx_-1] += u1/6.0*(*vp)[nx_-1];
357  }
358 
360  const ROL::Vector<Real> &z, Real &tol) {
361  Teuchos::RCP<std::vector<Real> > jvp =
362  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(jv)).getVector());
363  Teuchos::RCP<const std::vector<Real> > vp =
364  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
365  Teuchos::RCP<const std::vector<Real> > up =
366  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
367  Teuchos::RCP<const std::vector<Real> > zp =
368  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
369  for (int i=0; i<nx_+2; i++) {
370  if ( i == 0 ) {
371  (*jvp)[i] = -dx_/6.0*(*vp)[i];
372  }
373  else if ( i == 1 ) {
374  (*jvp)[i] = -dx_/6.0*(4.0*(*vp)[i-1]+(*vp)[i]);
375  }
376  else if ( i == nx_ ) {
377  (*jvp)[i] = -dx_/6.0*(4.0*(*vp)[i-1]+(*vp)[i-2]);
378  }
379  else if ( i == nx_+1 ) {
380  (*jvp)[i] = -dx_/6.0*(*vp)[i-2];
381  }
382  else {
383  (*jvp)[i] = -dx_/6.0*((*vp)[i-2]+4.0*(*vp)[i-1]+(*vp)[i]);
384  }
385  }
386  }
387 
389  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
390  Teuchos::RCP<std::vector<Real> > iajvp =
391  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(iajv)).getVector());
392  Teuchos::RCP<const std::vector<Real> > vp =
393  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
394  Teuchos::RCP<const std::vector<Real> > up =
395  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
396  // Get PDE Jacobian
397  std::vector<Real> d(nx_,0.0);
398  std::vector<Real> du(nx_-1,0.0);
399  std::vector<Real> dl(nx_-1,0.0);
400  compute_pde_jacobian(dl,d,du,*up);
401  // Solve solve adjoint system at current time step
402  linear_solve(*iajvp,dl,d,du,*vp,true);
403  }
404 
406  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
407  Teuchos::RCP<std::vector<Real> > ahwvp =
408  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<ROL::StdVector<Real> >(ahwv)).getVector());
409  Teuchos::RCP<const std::vector<Real> > wp =
410  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(w))).getVector();
411  Teuchos::RCP<const std::vector<Real> > vp =
412  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
413  Teuchos::RCP<const std::vector<Real> > up =
414  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
415  Teuchos::RCP<const std::vector<Real> > zp =
416  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
417  for (int i=0; i<nx_; i++) {
418  // Contribution from nonlinear term
419  (*ahwvp)[i] = 0.0;
420  if (i<nx_-1){
421  (*ahwvp)[i] += ((*wp)[i]*(*vp)[i+1] - (*wp)[i+1]*(2.0*(*vp)[i]+(*vp)[i+1]))/6.0;
422  }
423  if (i>0) {
424  (*ahwvp)[i] += ((*wp)[i-1]*((*vp)[i-1]+2.0*(*vp)[i]) - (*wp)[i]*(*vp)[i-1])/6.0;
425  }
426  }
427  }
428 
430  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
431  ahwv.zero();
432  }
434  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
435  ahwv.zero();
436  }
438  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
439  ahwv.zero();
440  }
441 };
442 
443 template<class Real>
445 private:
446  Real alpha_; // Penalty Parameter
447 
448  int nx_; // Number of interior nodes
449  Real dx_; // Mesh spacing (i.e. 1/(nx+1))
450 
451 /***************************************************************/
452 /********** BEGIN PRIVATE MEMBER FUNCTION DECLARATION **********/
453 /***************************************************************/
454  Real evaluate_target(Real x) {
455  Real val = 0.0;
456  int example = 2;
457  switch (example) {
458  case 1: val = ((x<0.5) ? 1.0 : 0.0); break;
459  case 2: val = 1.0; break;
460  case 3: val = std::abs(std::sin(8.0*M_PI*x)); break;
461  case 4: val = std::exp(-0.5*(x-0.5)*(x-0.5)); break;
462  }
463  return val;
464  }
465 
466  Real dot(const std::vector<Real> &x, const std::vector<Real> &y) {
467  Real ip = 0.0;
468  Real c = (((int)x.size()==nx_) ? 4.0 : 2.0);
469  for (unsigned i=0; i<x.size(); i++) {
470  if ( i == 0 ) {
471  ip += dx_/6.0*(c*x[i] + x[i+1])*y[i];
472  }
473  else if ( i == x.size()-1 ) {
474  ip += dx_/6.0*(x[i-1] + c*x[i])*y[i];
475  }
476  else {
477  ip += dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
478  }
479  }
480  return ip;
481  }
482 
483  void apply_mass(std::vector<Real> &Mu, const std::vector<Real> &u ) {
484  Mu.resize(u.size(),0.0);
485  Real c = (((int)u.size()==nx_) ? 4.0 : 2.0);
486  for (unsigned i=0; i<u.size(); i++) {
487  if ( i == 0 ) {
488  Mu[i] = dx_/6.0*(c*u[i] + u[i+1]);
489  }
490  else if ( i == u.size()-1 ) {
491  Mu[i] = dx_/6.0*(u[i-1] + c*u[i]);
492  }
493  else {
494  Mu[i] = dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]);
495  }
496  }
497  }
498 /*************************************************************/
499 /********** END PRIVATE MEMBER FUNCTION DECLARATION **********/
500 /*************************************************************/
501 
502 public:
503 
504  Objective_BurgersControl(Real alpha = 1.e-4, int nx = 128) : alpha_(alpha), nx_(nx) {
505  dx_ = 1.0/((Real)nx+1.0);
506  }
507 
508  Real value( const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
509  Teuchos::RCP<const std::vector<Real> > up =
510  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
511  Teuchos::RCP<const std::vector<Real> > zp =
512  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
513  // COMPUTE RESIDUAL
514  Real res1 = 0.0, res2 = 0.0, res3 = 0.0;
515  Real valu = 0.0, valz = dot(*zp,*zp);
516  for (int i=0; i<nx_; i++) {
517  if ( i == 0 ) {
518  res1 = (*up)[i]-evaluate_target((Real)(i+1)*dx_);
519  res2 = (*up)[i+1]-evaluate_target((Real)(i+2)*dx_);
520  valu += dx_/6.0*(4.0*res1 + res2)*res1;
521  }
522  else if ( i == nx_-1 ) {
523  res1 = (*up)[i-1]-evaluate_target((Real)i*dx_);
524  res2 = (*up)[i]-evaluate_target((Real)(i+1)*dx_);
525  valu += dx_/6.0*(res1 + 4.0*res2)*res2;
526  }
527  else {
528  res1 = (*up)[i-1]-evaluate_target((Real)i*dx_);
529  res2 = (*up)[i]-evaluate_target((Real)(i+1)*dx_);
530  res3 = (*up)[i+1]-evaluate_target((Real)(i+2)*dx_);
531  valu += dx_/6.0*(res1 + 4.0*res2 + res3)*res2;
532  }
533  }
534  return 0.5*(valu + alpha_*valz);
535  }
536 
537  void gradient_1( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
538  // Unwrap g
539  Teuchos::RCP<std::vector<Real> > gup = Teuchos::rcp_const_cast<std::vector<Real> >(
540  (Teuchos::dyn_cast<const ROL::StdVector<Real> >(g)).getVector());
541  // Unwrap x
542  Teuchos::RCP<const std::vector<Real> > up =
543  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
544  Teuchos::RCP<const std::vector<Real> > zp =
545  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
546  // COMPUTE GRADIENT WRT U
547  std::vector<Real> diff(nx_,0.0);
548  for (int i=0; i<nx_; i++) {
549  diff[i] = ((*up)[i]-evaluate_target((Real)(i+1)*dx_));
550  }
551  apply_mass(*gup,diff);
552  }
553 
554  void gradient_2( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
555  // Unwrap g
556  Teuchos::RCP<std::vector<Real> > gzp = Teuchos::rcp_const_cast<std::vector<Real> >(
557  (Teuchos::dyn_cast<const ROL::StdVector<Real> >(g)).getVector());
558  // Unwrap x
559  Teuchos::RCP<const std::vector<Real> > up =
560  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(u))).getVector();
561  Teuchos::RCP<const std::vector<Real> > zp =
562  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(z))).getVector();
563  // COMPUTE GRADIENT WRT Z
564  for (int i=0; i<nx_+2; i++) {
565  if (i==0) {
566  (*gzp)[i] = alpha_*dx_/6.0*(2.0*(*zp)[i]+(*zp)[i+1]);
567  }
568  else if (i==nx_+1) {
569  (*gzp)[i] = alpha_*dx_/6.0*(2.0*(*zp)[i]+(*zp)[i-1]);
570  }
571  else {
572  (*gzp)[i] = alpha_*dx_/6.0*((*zp)[i-1]+4.0*(*zp)[i]+(*zp)[i+1]);
573  }
574  }
575  }
576 
578  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
579  Teuchos::RCP<std::vector<Real> > hvup = Teuchos::rcp_const_cast<std::vector<Real> >(
580  (Teuchos::dyn_cast<const ROL::StdVector<Real> >(hv)).getVector());
581  // Unwrap v
582  Teuchos::RCP<const std::vector<Real> > vup =
583  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
584  // COMPUTE GRADIENT WRT U
585  apply_mass(*hvup,*vup);
586  }
587 
589  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
590  hv.zero();
591  }
592 
594  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
595  hv.zero();
596  }
597 
599  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
600  Teuchos::RCP<std::vector<Real> > hvzp = Teuchos::rcp_const_cast<std::vector<Real> >(
601  (Teuchos::dyn_cast<const ROL::StdVector<Real> >(hv)).getVector());
602  // Unwrap v
603  Teuchos::RCP<const std::vector<Real> > vzp =
604  (Teuchos::dyn_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &>(v))).getVector();
605  // COMPUTE GRADIENT WRT Z
606  for (int i=0; i<nx_+2; i++) {
607  if (i==0) {
608  (*hvzp)[i] = alpha_*dx_/6.0*(2.0*(*vzp)[i]+(*vzp)[i+1]);
609  }
610  else if (i==nx_+1) {
611  (*hvzp)[i] = alpha_*dx_/6.0*(2.0*(*vzp)[i]+(*vzp)[i-1]);
612  }
613  else {
614  (*hvzp)[i] = alpha_*dx_/6.0*((*vzp)[i-1]+4.0*(*vzp)[i]+(*vzp)[i+1]);
615  }
616  }
617  }
618 };
void compute_pde_jacobian(std::vector< Real > &dl, std::vector< Real > &d, std::vector< Real > &du, const std::vector< Real > &u)
Definition: example_05.hpp:149
void applyAdjointHessian_21(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
Definition: example_05.hpp:433
void linear_solve(std::vector< Real > &u, std::vector< Real > &dl, std::vector< Real > &d, std::vector< Real > &du, const std::vector< Real > &r, const bool transpose=false)
Definition: example_05.hpp:176
Real evaluate_target(Real x)
Definition: example_05.hpp:454
void applyAdjointHessian_22(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
Definition: example_05.hpp:437
void applyInverseJacobian_1(ROL::Vector< Real > &ijv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the inverse partial constraint Jacobian at , , to the vector .
Definition: example_05.hpp:307
Real dot(const std::vector< Real > &x, const std::vector< Real > &y)
Contains definitions of custom data types in ROL.
Real compute_norm(const std::vector< Real > &r)
Definition: example_05.hpp:79
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void compute_residual(std::vector< Real > &r, const std::vector< Real > &u, const std::vector< Real > &z)
Definition: example_05.hpp:112
void applyAdjointHessian_11(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
Definition: example_05.hpp:405
void hessVec_22(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Definition: example_05.hpp:598
Real value(const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute value.
Definition: example_05.hpp:508
void gradient_1(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
Definition: example_05.hpp:537
void applyInverseAdjointJacobian_1(ROL::Vector< Real > &iajv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the inverse of the adjoint of the partial constraint Jacobian at , , to the vector ...
Definition: example_05.hpp:388
void applyAdjointJacobian_1(ROL::Vector< Real > &ajv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Jacobian at , , to the vector . This is the primary inter...
Definition: example_05.hpp:326
void applyAdjointJacobian_2(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Jacobian at , , to vector . This is the primary interface...
Definition: example_05.hpp:359
Provides the std::vector implementation of the ROL::Vector interface.
void applyJacobian_1(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the partial constraint Jacobian at , , to the vector .
Definition: example_05.hpp:258
Real dot(const std::vector< Real > &x, const std::vector< Real > &y)
Definition: example_05.hpp:466
Objective_BurgersControl(Real alpha=1.e-4, int nx=128)
Definition: example_05.hpp:504
void hessVec_21(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Definition: example_05.hpp:593
void hessVec_12(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Definition: example_05.hpp:588
void gradient_2(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
Definition: example_05.hpp:554
void solve(ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Given , solve for .
Definition: example_05.hpp:209
void scale(std::vector< Real > &u, const Real alpha=0.0)
Definition: example_05.hpp:106
void applyJacobian_2(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the partial constraint Jacobian at , , to the vector .
Definition: example_05.hpp:291
void hessVec_11(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
Definition: example_05.hpp:577
void value(ROL::Vector< Real > &c, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Evaluate the constraint operator at .
Definition: example_05.hpp:198
void apply_mass(std::vector< Real > &Mu, const std::vector< Real > &u)
Definition: example_05.hpp:483
virtual Real norm() const =0
Returns where .
void update(std::vector< Real > &u, const std::vector< Real > &s, const Real alpha=1.0)
Definition: example_05.hpp:100
void applyAdjointHessian_12(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
Definition: example_05.hpp:429
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:118