Debian Codegraph @

packages/rol/adapters/tpetra/test/sol/test_01.hpp

raw
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
10 #include "Teuchos_ParameterList.hpp"
11 #include "Teuchos_XMLParameterListHelpers.hpp"
12 #include "ROL_Stream.hpp"
13 #include "Teuchos_LAPACK.hpp"
14 #include "Teuchos_GlobalMPISession.hpp"
15 #include "Teuchos_Comm.hpp"
16 #include "Teuchos_DefaultComm.hpp"
17 #include "Teuchos_CommHelpers.hpp"
18
19 #include <iostream>
20
21 // ROL_Types contains predefined constants and objects
22 #include "ROL_Types.hpp"
23 // ROL algorithmic information
24 #include "ROL_Algorithm.hpp"
25 #include "ROL_ConstraintStatusTest.hpp"
26 #include "ROL_CompositeStep.hpp"
27 #include "ROL_TrustRegionStep.hpp"
28 // ROL vectors
29 #include "ROL_StdVector.hpp"
30 // ROL objective functions and constraints
31 #include "ROL_Objective_SimOpt.hpp"
32 #include "ROL_Constraint_SimOpt.hpp"
33 #include "ROL_Reduced_Objective_SimOpt.hpp"
34 #include "ROL_OptimizationProblem.hpp"
35 // ROL sample generators
36 #include "ROL_MonteCarloGenerator.hpp"
37 #include "ROL_StdTeuchosBatchManager.hpp"
38
39 #include "ROL_SimulatedConstraint.hpp"
40 #include "ROL_SimulatedObjective.hpp"
41 #include "ROL_SimulatedObjectiveCVaR.hpp"
42
43 template<class Real>
44 class Constraint_BurgersControl : public ROL::Constraint_SimOpt<Real> {
45 private:
46 int nx_;
47 Real dx_;
48
49 Real compute_norm(const std::vector<Real> &r) {
50 return std::sqrt(dot(r,r));
51 }
52
53 Real dot(const std::vector<Real> &x, const std::vector<Real> &y) {
54 Real ip = 0.0;
55 Real c = (((int)x.size()==nx_) ? 4.0 : 2.0);
56 for (unsigned i=0; i<x.size(); i++) {
57 if ( i == 0 ) {
58 ip += dx_/6.0*(c*x[i] + x[i+1])*y[i];
59 }
60 else if ( i == x.size()-1 ) {
61 ip += dx_/6.0*(x[i-1] + c*x[i])*y[i];
62 }
63 else {
64 ip += dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
65 }
66 }
67 return ip;
68 }
69
70 using ROL::Constraint_SimOpt<Real>::update;
71
72 void update(std::vector<Real> &u, const std::vector<Real> &s, const Real alpha=1.0) {
73 for (unsigned i=0; i<u.size(); i++) {
74 u[i] += alpha*s[i];
75 }
76 }
77
78 void scale(std::vector<Real> &u, const Real alpha=0.0) {
79 for (unsigned i=0; i<u.size(); i++) {
80 u[i] *= alpha;
81 }
82 }
83
84 void compute_residual(std::vector<Real> &r, const std::vector<Real> &u,
85 const std::vector<Real> &z) {
86 r.clear(); r.resize(nx_,0.0);
87 const std::vector<Real> param =
88 ROL::Constraint_SimOpt<Real>::getParameter();
89 Real nu = std::pow(10.0,param[0]-2.0);
90 Real f = param[1]/100.0;
91 Real u0 = 1.0+param[2]/1000.0;
92 Real u1 = param[3]/1000.0;
93 for (int i=0; i<nx_; i++) {
94 // Contribution from stiffness term
95 if ( i==0 ) {
96 r[i] = nu/dx_*(2.0*u[i]-u[i+1]);
97 }
98 else if (i==nx_-1) {
99 r[i] = nu/dx_*(2.0*u[i]-u[i-1]);
100 }
101 else {
102 r[i] = nu/dx_*(2.0*u[i]-u[i-1]-u[i+1]);
103 }
104 // Contribution from nonlinear term
105 if (i<nx_-1){
106 r[i] += u[i+1]*(u[i]+u[i+1])/6.0;
107 }
108 if (i>0) {
109 r[i] -= u[i-1]*(u[i-1]+u[i])/6.0;
110 }
111 // Contribution from control
112 r[i] -= dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
113 // Contribution from right-hand side
114 r[i] -= dx_*f;
115 }
116 // Contribution from Dirichlet boundary terms
117 r[ 0] -= u0*u[ 0]/6.0 + u0*u0/6.0 + nu*u0/dx_;
118 r[nx_-1] += u1*u[nx_-1]/6.0 + u1*u1/6.0 - nu*u1/dx_;
119 }
120
121 void compute_pde_jacobian(std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
122 const std::vector<Real> &u) {
123 const std::vector<Real> param =
124 ROL::Constraint_SimOpt<Real>::getParameter();
125 Real nu = std::pow(10.0,param[0]-2.0);
126 Real u0 = 1.0+param[2]/1000.0;
127 Real u1 = param[3]/1000.0;
128 // Get Diagonal and Off-Diagonal Entries of linear PDE Jacobian
129 d.clear(); d.resize(nx_,nu*2.0/dx_);
130 dl.clear(); dl.resize(nx_-1,-nu/dx_);
131 du.clear(); du.resize(nx_-1,-nu/dx_);
132 // Contribution from nonlinearity
133 for (int i=0; i<nx_; i++) {
134 if (i<nx_-1) {
135 dl[i] += (-2.0*u[i]-u[i+1])/6.0;
136 d[i] += u[i+1]/6.0;
137 }
138 if (i>0) {
139 d[i] += -u[i-1]/6.0;
140 du[i-1] += (u[i-1]+2.0*u[i])/6.0;
141 }
142 }
143 // Contribution from Dirichlet boundary conditions
144 d[ 0] -= u0/6.0;
145 d[nx_-1] += u1/6.0;
146 }
147
148 void linear_solve(std::vector<Real> &u, std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
149 const std::vector<Real> &r, const bool transpose = false) {
150 u.assign(r.begin(),r.end());
151 // Perform LDL factorization
152 Teuchos::LAPACK<int,Real> lp;
153 std::vector<Real> du2(nx_-2,0.0);
154 std::vector<int> ipiv(nx_,0);
155 int info;
156 int ldb = nx_;
157 int nhrs = 1;
158 lp.GTTRF(nx_,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info);
159 char trans = 'N';
160 if ( transpose ) {
161 trans = 'T';
162 }
163 lp.GTTRS(trans,nx_,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info);
164 }
165
166 ROL::Ptr<std::vector<Real>> getVector( ROL::Vector<Real>& x ) {
167 return dynamic_cast<ROL::StdVector<Real>&>(x).getVector();
168 }
169
170 ROL::Ptr<const std::vector<Real>> getVector( const ROL::Vector<Real>& x ) {
171 return dynamic_cast<const ROL::StdVector<Real>&>(x).getVector();
172 }
173
174 public:
175
176 Constraint_BurgersControl(int nx = 128) : nx_(nx), dx_(1.0/((Real)nx+1.0)) {}
177
178 void value(ROL::Vector<Real> &c, const ROL::Vector<Real> &u,
179 const ROL::Vector<Real> &z, Real &tol) {
180 auto cp = getVector(c);
181 auto up = getVector(u);
182 auto zp = getVector(z);
183 compute_residual(*cp,*up,*zp);
184 }
185
186 void solve(ROL::Vector<Real> &c, ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
187 auto up = getVector(u);
188 up->assign(up->size(),1.0);
189 ROL::Constraint_SimOpt<Real>::solve(c,u,z,tol);
190 }
191
192 void applyJacobian_1(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
193 const ROL::Vector<Real> &z, Real &tol) {
194 auto jvp = getVector(jv);
195 auto vp = getVector(v);
196 auto up = getVector(u);
197 auto zp = getVector(z);
198 const std::vector<Real> param =
199 ROL::Constraint_SimOpt<Real>::getParameter();
200 Real nu = std::pow(10.0,param[0]-2.0);
201 Real u0 = 1.0+param[2]/1000.0;
202 Real u1 = param[3]/1000.0;
203 // Fill jvp
204 for (int i = 0; i < nx_; i++) {
205 (*jvp)[i] = nu/dx_*2.0*(*vp)[i];
206 if ( i > 0 ) {
207 (*jvp)[i] += -nu/dx_*(*vp)[i-1]
208 -(*up)[i-1]/6.0*(*vp)[i]
209 -((*up)[i]+2.0*(*up)[i-1])/6.0*(*vp)[i-1];
210 }
211 if ( i < nx_-1 ) {
212 (*jvp)[i] += -nu/dx_*(*vp)[i+1]
213 +(*up)[i+1]/6.0*(*vp)[i]
214 +((*up)[i]+2.0*(*up)[i+1])/6.0*(*vp)[i+1];
215 }
216 }
217 (*jvp)[ 0] -= u0/6.0*(*vp)[0];
218 (*jvp)[nx_-1] += u1/6.0*(*vp)[nx_-1];
219 }
220
221 void applyJacobian_2(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
222 const ROL::Vector<Real> &z, Real &tol) {
223 auto jvp = getVector(jv);
224 auto vp = getVector(v);
225 auto up = getVector(u);
226 auto zp = getVector(z);
227 for (int i=0; i<nx_; i++) {
228 // Contribution from control
229 (*jvp)[i] = -dx_/6.0*((*vp)[i]+4.0*(*vp)[i+1]+(*vp)[i+2]);
230 }
231 }
232
233 void applyInverseJacobian_1(ROL::Vector<Real> &ijv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
234 const ROL::Vector<Real> &z, Real &tol) {
235 auto ijvp = getVector(ijv);
236 auto vp = getVector(v);
237 auto up = getVector(u);
238 auto zp = getVector(z);
239 // Get PDE Jacobian
240 std::vector<Real> d(nx_,0.0);
241 std::vector<Real> dl(nx_-1,0.0);
242 std::vector<Real> du(nx_-1,0.0);
243 compute_pde_jacobian(dl,d,du,*up);
244 // Solve solve state sensitivity system at current time step
245 linear_solve(*ijvp,dl,d,du,*vp);
246 }
247
248 void applyAdjointJacobian_1(ROL::Vector<Real> &ajv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
249 const ROL::Vector<Real> &z, Real &tol) {
250 auto ajvp = getVector(ajv);
251 auto vp = getVector(v);
252 auto up = getVector(u);
253 auto zp = getVector(z);
254 const std::vector<Real> param =
255 ROL::Constraint_SimOpt<Real>::getParameter();
256 Real nu = std::pow(10.0,param[0]-2.0);
257 Real u0 = 1.0+param[2]/1000.0;
258 Real u1 = param[3]/1000.0;
259 // Fill jvp
260 for (int i = 0; i < nx_; i++) {
261 (*ajvp)[i] = nu/dx_*2.0*(*vp)[i];
262 if ( i > 0 ) {
263 (*ajvp)[i] += -nu/dx_*(*vp)[i-1]
264 -(*up)[i-1]/6.0*(*vp)[i]
265 +((*up)[i-1]+2.0*(*up)[i])/6.0*(*vp)[i-1];
266 }
267 if ( i < nx_-1 ) {
268 (*ajvp)[i] += -nu/dx_*(*vp)[i+1]
269 +(*up)[i+1]/6.0*(*vp)[i]
270 -((*up)[i+1]+2.0*(*up)[i])/6.0*(*vp)[i+1];
271 }
272 }
273 (*ajvp)[ 0] -= u0/6.0*(*vp)[0];
274 (*ajvp)[nx_-1] += u1/6.0*(*vp)[nx_-1];
275 }
276
277 void applyAdjointJacobian_2(ROL::Vector<Real> &ajv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
278 const ROL::Vector<Real> &z, Real &tol) {
279 auto ajvp = getVector(ajv);
280 auto vp = getVector(v);
281 auto up = getVector(u);
282 auto zp = getVector(z);
283 for (int i=0; i<nx_+2; i++) {
284 if ( i == 0 ) {
285 (*ajvp)[i] = -dx_/6.0*(*vp)[i];
286 }
287 else if ( i == 1 ) {
288 (*ajvp)[i] = -dx_/6.0*(4.0*(*vp)[i-1]+(*vp)[i]);
289 }
290 else if ( i == nx_ ) {
291 (*ajvp)[i] = -dx_/6.0*(4.0*(*vp)[i-1]+(*vp)[i-2]);
292 }
293 else if ( i == nx_+1 ) {
294 (*ajvp)[i] = -dx_/6.0*(*vp)[i-2];
295 }
296 else {
297 (*ajvp)[i] = -dx_/6.0*((*vp)[i-2]+4.0*(*vp)[i-1]+(*vp)[i]);
298 }
299 }
300 }
301
302 void applyInverseAdjointJacobian_1(ROL::Vector<Real> &iajv, const ROL::Vector<Real> &v,
303 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
304 auto iajvp = getVector(iajv);
305 auto vp = getVector(v);
306 auto up = getVector(u);
307 auto zp = getVector(z);
308 // Get PDE Jacobian
309 std::vector<Real> d(nx_,0.0);
310 std::vector<Real> du(nx_-1,0.0);
311 std::vector<Real> dl(nx_-1,0.0);
312 compute_pde_jacobian(dl,d,du,*up);
313 // Solve solve adjoint system at current time step
314 linear_solve(*iajvp,dl,d,du,*vp,true);
315 }
316
317 void applyAdjointHessian_11(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
318 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
319 auto ahwvp = getVector(ahwv);
320 auto wp = getVector(w);
321 auto vp = getVector(v);
322 auto up = getVector(u);
323 auto zp = getVector(z);
324 for (int i=0; i<nx_; i++) {
325 // Contribution from nonlinear term
326 (*ahwvp)[i] = 0.0;
327 if (i<nx_-1){
328 (*ahwvp)[i] += ((*wp)[i]*(*vp)[i+1] - (*wp)[i+1]*(2.0*(*vp)[i]+(*vp)[i+1]))/6.0;
329 }
330 if (i>0) {
331 (*ahwvp)[i] += ((*wp)[i-1]*((*vp)[i-1]+2.0*(*vp)[i]) - (*wp)[i]*(*vp)[i-1])/6.0;
332 }
333 }
334 }
335
336 void applyAdjointHessian_12(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
337 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
338 ahwv.zero();
339 }
340 void applyAdjointHessian_21(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
341 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
342 ahwv.zero();
343 }
344 void applyAdjointHessian_22(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
345 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
346 ahwv.zero();
347 }
348 };
349
350 template<class Real>
351 class Objective_BurgersControl : public ROL::Objective_SimOpt<Real> {
352 private:
353 Real alpha_; // Penalty Parameter
354
355 int nx_; // Number of interior nodes
356 Real dx_; // Mesh spacing (i.e. 1/(nx+1))
357
358 /***************************************************************/
359 /********** BEGIN PRIVATE MEMBER FUNCTION DECLARATION **********/
360 /***************************************************************/
361 Real evaluate_target(Real x) {
362 Real val = 0.0;
363 int example = 2;
364 switch (example) {
365 case 1: val = ((x<0.5) ? 1.0 : 0.0); break;
366 case 2: val = 1.0; break;
367 case 3: val = std::abs(std::sin(8.0*M_PI*x)); break;
368 case 4: val = std::exp(-0.5*(x-0.5)*(x-0.5)); break;
369 }
370 return val;
371 }
372
373 Real dot(const std::vector<Real> &x, const std::vector<Real> &y) {
374 Real ip = 0.0;
375 Real c = (((int)x.size()==nx_) ? 4.0 : 2.0);
376 for (unsigned i=0; i<x.size(); i++) {
377 if ( i == 0 ) {
378 ip += dx_/6.0*(c*x[i] + x[i+1])*y[i];
379 }
380 else if ( i == x.size()-1 ) {
381 ip += dx_/6.0*(x[i-1] + c*x[i])*y[i];
382 }
383 else {
384 ip += dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
385 }
386 }
387 return ip;
388 }
389
390 void apply_mass(std::vector<Real> &Mu, const std::vector<Real> &u ) {
391 Mu.resize(u.size(),0.0);
392 Real c = (((int)u.size()==nx_) ? 4.0 : 2.0);
393 for (unsigned i=0; i<u.size(); i++) {
394 if ( i == 0 ) {
395 Mu[i] = dx_/6.0*(c*u[i] + u[i+1]);
396 }
397 else if ( i == u.size()-1 ) {
398 Mu[i] = dx_/6.0*(u[i-1] + c*u[i]);
399 }
400 else {
401 Mu[i] = dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]);
402 }
403 }
404 }
405
406 ROL::Ptr<std::vector<Real>> getVector( ROL::Vector<Real>& x ) {
407 return dynamic_cast<ROL::StdVector<Real>&>(x).getVector();
408 }
409
410 ROL::Ptr<const std::vector<Real>> getVector( const ROL::Vector<Real>& x ) {
411 return dynamic_cast<const ROL::StdVector<Real>&>(x).getVector();
412 }
413
414 /*************************************************************/
415 /********** END PRIVATE MEMBER FUNCTION DECLARATION **********/
416 /*************************************************************/
417
418 public:
419
420 Objective_BurgersControl(Real alpha = 1.e-4, int nx = 128) : alpha_(alpha), nx_(nx) {
421 dx_ = 1.0/((Real)nx+1.0);
422 }
423
424 using ROL::Objective_SimOpt<Real>::value;
425
426 Real value( const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
427 auto up = getVector(u);
428 auto zp = getVector(z);
429 // COMPUTE RESIDUAL
430 Real res1 = 0.0, res2 = 0.0, res3 = 0.0;
431 Real valu = 0.0, valz = dot(*zp,*zp);
432 for (int i=0; i<nx_; i++) {
433 if ( i == 0 ) {
434 res1 = (*up)[i]-evaluate_target((Real)(i+1)*dx_);
435 res2 = (*up)[i+1]-evaluate_target((Real)(i+2)*dx_);
436 valu += dx_/6.0*(4.0*res1 + res2)*res1;
437 }
438 else if ( i == nx_-1 ) {
439 res1 = (*up)[i-1]-evaluate_target((Real)i*dx_);
440 res2 = (*up)[i]-evaluate_target((Real)(i+1)*dx_);
441 valu += dx_/6.0*(res1 + 4.0*res2)*res2;
442 }
443 else {
444 res1 = (*up)[i-1]-evaluate_target((Real)i*dx_);
445 res2 = (*up)[i]-evaluate_target((Real)(i+1)*dx_);
446 res3 = (*up)[i+1]-evaluate_target((Real)(i+2)*dx_);
447 valu += dx_/6.0*(res1 + 4.0*res2 + res3)*res2;
448 }
449 }
450 return 0.5*(valu + alpha_*valz);
451 }
452
453 void gradient_1( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
454 auto gup = getVector(g);
455 auto up = getVector(u);
456 auto zp = getVector(z);
457 // COMPUTE GRADIENT WRT U
458 std::vector<Real> diff(nx_,0.0);
459 for (int i=0; i<nx_; i++) {
460 diff[i] = ((*up)[i]-evaluate_target((Real)(i+1)*dx_));
461 }
462 apply_mass(*gup,diff);
463 }
464
465 void gradient_2( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
466 auto gzp = getVector(g);
467 auto up = getVector(u);
468 auto zp = getVector(z);
469 // COMPUTE GRADIENT WRT Z
470 for (int i=0; i<nx_+2; i++) {
471 if (i==0) {
472 (*gzp)[i] = alpha_*dx_/6.0*(2.0*(*zp)[i]+(*zp)[i+1]);
473 }
474 else if (i==nx_+1) {
475 (*gzp)[i] = alpha_*dx_/6.0*(2.0*(*zp)[i]+(*zp)[i-1]);
476 }
477 else {
478 (*gzp)[i] = alpha_*dx_/6.0*((*zp)[i-1]+4.0*(*zp)[i]+(*zp)[i+1]);
479 }
480 }
481 }
482
483 void hessVec_11( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
484 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
485 auto hvup = getVector(hv);
486 auto vup = getVector(v);
487 auto up = getVector(u);
488 auto zp = getVector(z);
489 // COMPUTE GRADIENT WRT U
490 apply_mass(*hvup,*vup);
491 }
492
493 void hessVec_12( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
494 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
495 hv.zero();
496 }
497
498 void hessVec_21( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
499 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
500 hv.zero();
501 }
502
503 void hessVec_22( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
504 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
505 auto hvzp = getVector(hv);
506 auto vzp = getVector(v);
507 // COMPUTE GRADIENT WRT Z
508 for (int i=0; i<nx_+2; i++) {
509 if (i==0) {
510 (*hvzp)[i] = alpha_*dx_/6.0*(2.0*(*vzp)[i]+(*vzp)[i+1]);
511 }
512 else if (i==nx_+1) {
513 (*hvzp)[i] = alpha_*dx_/6.0*(2.0*(*vzp)[i]+(*vzp)[i-1]);
514 }
515 else {
516 (*hvzp)[i] = alpha_*dx_/6.0*((*vzp)[i-1]+4.0*(*vzp)[i]+(*vzp)[i+1]);
517 }
518 }
519 }
520 };