| 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 |
/*! \file example_06.cpp |
| 11 |
\brief Shows how to solve a steady Burgers' optimal control problem using |
| 12 |
full-space methods. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "ROL_Types.hpp" |
| 16 |
#include "ROL_Vector.hpp" |
| 17 |
#include "ROL_BoundConstraint.hpp" |
| 18 |
#include "ROL_Constraint_SimOpt.hpp" |
| 19 |
#include "ROL_TeuchosBatchManager.hpp" |
| 20 |
|
| 21 |
#include "Teuchos_LAPACK.hpp" |
| 22 |
|
| 23 |
template<class Real> |
| 24 |
class L2VectorPrimal; |
| 25 |
|
| 26 |
template<class Real> |
| 27 |
class L2VectorDual; |
| 28 |
|
| 29 |
template<class Real> |
| 30 |
class H1VectorPrimal; |
| 31 |
|
| 32 |
template<class Real> |
| 33 |
class H1VectorDual; |
| 34 |
|
| 35 |
template<class Real> |
| 36 |
class BurgersFEM { |
| 37 |
private: |
| 38 |
int nx_; |
| 39 |
Real dx_; |
| 40 |
Real nu_; |
| 41 |
Real nl_; |
| 42 |
Real u0_; |
| 43 |
Real u1_; |
| 44 |
Real f_; |
| 45 |
Real cH1_; |
| 46 |
Real cL2_; |
| 47 |
|
| 48 |
private: |
| 49 |
void update(std::vector<Real> &u, const std::vector<Real> &s, const Real alpha=1.0) const { |
| 50 |
for (unsigned i=0; i<u.size(); i++) { |
| 51 |
u[i] += alpha*s[i]; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void axpy(std::vector<Real> &out, const Real a, const std::vector<Real> &x, const std::vector<Real> &y) const { |
| 56 |
for (unsigned i=0; i < x.size(); i++) { |
| 57 |
out[i] = a*x[i] + y[i]; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void scale(std::vector<Real> &u, const Real alpha=0.0) const { |
| 62 |
for (unsigned i=0; i<u.size(); i++) { |
| 63 |
u[i] *= alpha; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void linear_solve(std::vector<Real> &u, std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du, |
| 68 |
const std::vector<Real> &r, const bool transpose = false) const { |
| 69 |
if ( r.size() == 1 ) { |
| 70 |
u.resize(1,r[0]/d[0]); |
| 71 |
} |
| 72 |
else if ( r.size() == 2 ) { |
| 73 |
u.resize(2,0.0); |
| 74 |
Real det = d[0]*d[1] - dl[0]*du[0]; |
| 75 |
u[0] = (d[1]*r[0] - du[0]*r[1])/det; |
| 76 |
u[1] = (d[0]*r[1] - dl[0]*r[0])/det; |
| 77 |
} |
| 78 |
else { |
| 79 |
u.assign(r.begin(),r.end()); |
| 80 |
// Perform LDL factorization |
| 81 |
Teuchos::LAPACK<int,Real> lp; |
| 82 |
std::vector<Real> du2(r.size()-2,0.0); |
| 83 |
std::vector<int> ipiv(r.size(),0); |
| 84 |
int info; |
| 85 |
int dim = r.size(); |
| 86 |
int ldb = r.size(); |
| 87 |
int nhrs = 1; |
| 88 |
lp.GTTRF(dim,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info); |
| 89 |
char trans = 'N'; |
| 90 |
if ( transpose ) { |
| 91 |
trans = 'T'; |
| 92 |
} |
| 93 |
lp.GTTRS(trans,dim,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public: |
| 98 |
BurgersFEM(int nx = 128, Real nl = 1.0, Real cH1 = 1.0, Real cL2 = 1.0) |
| 99 |
: nx_(nx), dx_(1.0/((Real)nx+1.0)), nl_(nl), cH1_(cH1), cL2_(cL2) {} |
| 100 |
|
| 101 |
void set_problem_data(const Real nu, const Real f, const Real u0, const Real u1) { |
| 102 |
nu_ = std::pow(10.0,nu-2.0); |
| 103 |
f_ = 0.01*f; |
| 104 |
u0_ = 1.0+0.001*u0; |
| 105 |
u1_ = 0.001*u1; |
| 106 |
} |
| 107 |
|
| 108 |
int num_dof(void) const { |
| 109 |
return nx_; |
| 110 |
} |
| 111 |
|
| 112 |
Real mesh_spacing(void) const { |
| 113 |
return dx_; |
| 114 |
} |
| 115 |
|
| 116 |
/***************************************************************************/ |
| 117 |
/*********************** L2 INNER PRODUCT FUNCTIONS ************************/ |
| 118 |
/***************************************************************************/ |
| 119 |
// Compute L2 inner product |
| 120 |
Real compute_L2_dot(const std::vector<Real> &x, const std::vector<Real> &y) const { |
| 121 |
Real ip = 0.0; |
| 122 |
Real c = (((int)x.size()==nx_) ? 4.0 : 2.0); |
| 123 |
for (unsigned i=0; i<x.size(); i++) { |
| 124 |
if ( i == 0 ) { |
| 125 |
ip += dx_/6.0*(c*x[i] + x[i+1])*y[i]; |
| 126 |
} |
| 127 |
else if ( i == x.size()-1 ) { |
| 128 |
ip += dx_/6.0*(x[i-1] + c*x[i])*y[i]; |
| 129 |
} |
| 130 |
else { |
| 131 |
ip += dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i]; |
| 132 |
} |
| 133 |
} |
| 134 |
return ip; |
| 135 |
} |
| 136 |
|
| 137 |
// compute L2 norm |
| 138 |
Real compute_L2_norm(const std::vector<Real> &r) const { |
| 139 |
return std::sqrt(compute_L2_dot(r,r)); |
| 140 |
} |
| 141 |
|
| 142 |
// Apply L2 Reisz operator |
| 143 |
void apply_mass(std::vector<Real> &Mu, const std::vector<Real> &u ) const { |
| 144 |
Mu.resize(u.size(),0.0); |
| 145 |
Real c = (((int)u.size()==nx_) ? 4.0 : 2.0); |
| 146 |
for (unsigned i=0; i<u.size(); i++) { |
| 147 |
if ( i == 0 ) { |
| 148 |
Mu[i] = dx_/6.0*(c*u[i] + u[i+1]); |
| 149 |
} |
| 150 |
else if ( i == u.size()-1 ) { |
| 151 |
Mu[i] = dx_/6.0*(u[i-1] + c*u[i]); |
| 152 |
} |
| 153 |
else { |
| 154 |
Mu[i] = dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// Apply L2 inverse Reisz operator |
| 160 |
void apply_inverse_mass(std::vector<Real> &Mu, const std::vector<Real> &u) const { |
| 161 |
unsigned nx = u.size(); |
| 162 |
// Build mass matrix |
| 163 |
std::vector<Real> dl(nx-1,dx_/6.0); |
| 164 |
std::vector<Real> d(nx,2.0*dx_/3.0); |
| 165 |
std::vector<Real> du(nx-1,dx_/6.0); |
| 166 |
if ( (int)nx != nx_ ) { |
| 167 |
d[ 0] = dx_/3.0; |
| 168 |
d[nx-1] = dx_/3.0; |
| 169 |
} |
| 170 |
linear_solve(Mu,dl,d,du,u); |
| 171 |
} |
| 172 |
|
| 173 |
void test_inverse_mass(std::ostream &outStream = std::cout) { |
| 174 |
// State Mass Matrix |
| 175 |
std::vector<Real> u(nx_,0.0), Mu(nx_,0.0), iMMu(nx_,0.0), diff(nx_,0.0); |
| 176 |
for (int i = 0; i < nx_; i++) { |
| 177 |
u[i] = 2.0*(Real)rand()/(Real)RAND_MAX - 1.0; |
| 178 |
} |
| 179 |
apply_mass(Mu,u); |
| 180 |
apply_inverse_mass(iMMu,Mu); |
| 181 |
axpy(diff,-1.0,iMMu,u); |
| 182 |
Real error = compute_L2_norm(diff); |
| 183 |
Real normu = compute_L2_norm(u); |
| 184 |
outStream << "\nTest Inverse State Mass Matrix\n"; |
| 185 |
outStream << " ||u - inv(M)Mu|| = " << error << "\n"; |
| 186 |
outStream << " ||u|| = " << normu << "\n"; |
| 187 |
outStream << " Relative Error = " << error/normu << "\n"; |
| 188 |
outStream << "\n"; |
| 189 |
// Control Mass Matrix |
| 190 |
u.resize(nx_+2,0.0); Mu.resize(nx_+2,0.0); iMMu.resize(nx_+2,0.0); diff.resize(nx_+2,0.0); |
| 191 |
for (int i = 0; i < nx_+2; i++) { |
| 192 |
u[i] = 2.0*(Real)rand()/(Real)RAND_MAX - 1.0; |
| 193 |
} |
| 194 |
apply_mass(Mu,u); |
| 195 |
apply_inverse_mass(iMMu,Mu); |
| 196 |
axpy(diff,-1.0,iMMu,u); |
| 197 |
error = compute_L2_norm(diff); |
| 198 |
normu = compute_L2_norm(u); |
| 199 |
outStream << "\nTest Inverse Control Mass Matrix\n"; |
| 200 |
outStream << " ||z - inv(M)Mz|| = " << error << "\n"; |
| 201 |
outStream << " ||z|| = " << normu << "\n"; |
| 202 |
outStream << " Relative Error = " << error/normu << "\n"; |
| 203 |
outStream << "\n"; |
| 204 |
} |
| 205 |
|
| 206 |
/***************************************************************************/ |
| 207 |
/*********************** H1 INNER PRODUCT FUNCTIONS ************************/ |
| 208 |
/***************************************************************************/ |
| 209 |
// Compute H1 inner product |
| 210 |
Real compute_H1_dot(const std::vector<Real> &x, const std::vector<Real> &y) const { |
| 211 |
Real ip = 0.0; |
| 212 |
for (int i=0; i<nx_; i++) { |
| 213 |
if ( i == 0 ) { |
| 214 |
ip += cL2_*dx_/6.0*(4.0*x[i] + x[i+1])*y[i]; // Mass term |
| 215 |
ip += cH1_*(2.0*x[i] - x[i+1])/dx_*y[i]; // Stiffness term |
| 216 |
} |
| 217 |
else if ( i == nx_-1 ) { |
| 218 |
ip += cL2_*dx_/6.0*(x[i-1] + 4.0*x[i])*y[i]; // Mass term |
| 219 |
ip += cH1_*(2.0*x[i] - x[i-1])/dx_*y[i]; // Stiffness term |
| 220 |
} |
| 221 |
else { |
| 222 |
ip += cL2_*dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i]; // Mass term |
| 223 |
ip += cH1_*(2.0*x[i] - x[i-1] - x[i+1])/dx_*y[i]; // Stiffness term |
| 224 |
} |
| 225 |
} |
| 226 |
return ip; |
| 227 |
} |
| 228 |
|
| 229 |
// compute H1 norm |
| 230 |
Real compute_H1_norm(const std::vector<Real> &r) const { |
| 231 |
return std::sqrt(compute_H1_dot(r,r)); |
| 232 |
} |
| 233 |
|
| 234 |
// Apply H2 Reisz operator |
| 235 |
void apply_H1(std::vector<Real> &Mu, const std::vector<Real> &u ) const { |
| 236 |
Mu.resize(nx_,0.0); |
| 237 |
for (int i=0; i<nx_; i++) { |
| 238 |
if ( i == 0 ) { |
| 239 |
Mu[i] = cL2_*dx_/6.0*(4.0*u[i] + u[i+1]) |
| 240 |
+ cH1_*(2.0*u[i] - u[i+1])/dx_; |
| 241 |
} |
| 242 |
else if ( i == nx_-1 ) { |
| 243 |
Mu[i] = cL2_*dx_/6.0*(u[i-1] + 4.0*u[i]) |
| 244 |
+ cH1_*(2.0*u[i] - u[i-1])/dx_; |
| 245 |
} |
| 246 |
else { |
| 247 |
Mu[i] = cL2_*dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]) |
| 248 |
+ cH1_*(2.0*u[i] - u[i-1] - u[i+1])/dx_; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
// Apply H1 inverse Reisz operator |
| 254 |
void apply_inverse_H1(std::vector<Real> &Mu, const std::vector<Real> &u) const { |
| 255 |
// Build mass matrix |
| 256 |
std::vector<Real> dl(nx_-1,cL2_*dx_/6.0 - cH1_/dx_); |
| 257 |
std::vector<Real> d(nx_,2.0*(cL2_*dx_/3.0 + cH1_/dx_)); |
| 258 |
std::vector<Real> du(nx_-1,cL2_*dx_/6.0 - cH1_/dx_); |
| 259 |
linear_solve(Mu,dl,d,du,u); |
| 260 |
} |
| 261 |
|
| 262 |
void test_inverse_H1(std::ostream &outStream = std::cout) { |
| 263 |
std::vector<Real> u(nx_,0.0), Mu(nx_,0.0), iMMu(nx_,0.0), diff(nx_,0.0); |
| 264 |
for (int i = 0; i < nx_; i++) { |
| 265 |
u[i] = 2.0*(Real)rand()/(Real)RAND_MAX - 1.0; |
| 266 |
} |
| 267 |
apply_H1(Mu,u); |
| 268 |
apply_inverse_H1(iMMu,Mu); |
| 269 |
axpy(diff,-1.0,iMMu,u); |
| 270 |
Real error = compute_H1_norm(diff); |
| 271 |
Real normu = compute_H1_norm(u); |
| 272 |
outStream << "\nTest Inverse State H1 Matrix\n"; |
| 273 |
outStream << " ||u - inv(M)Mu|| = " << error << "\n"; |
| 274 |
outStream << " ||u|| = " << normu << "\n"; |
| 275 |
outStream << " Relative Error = " << error/normu << "\n"; |
| 276 |
outStream << "\n"; |
| 277 |
} |
| 278 |
|
| 279 |
/***************************************************************************/ |
| 280 |
/*********************** PDE RESIDUAL AND SOLVE ****************************/ |
| 281 |
/***************************************************************************/ |
| 282 |
// Compute current PDE residual |
| 283 |
void compute_residual(std::vector<Real> &r, const std::vector<Real> &u, |
| 284 |
const std::vector<Real> &z) const { |
| 285 |
r.clear(); |
| 286 |
r.resize(nx_,0.0); |
| 287 |
for (int i=0; i<nx_; i++) { |
| 288 |
// Contribution from stiffness term |
| 289 |
if ( i==0 ) { |
| 290 |
r[i] = nu_/dx_*(2.0*u[i]-u[i+1]); |
| 291 |
} |
| 292 |
else if (i==nx_-1) { |
| 293 |
r[i] = nu_/dx_*(2.0*u[i]-u[i-1]); |
| 294 |
} |
| 295 |
else { |
| 296 |
r[i] = nu_/dx_*(2.0*u[i]-u[i-1]-u[i+1]); |
| 297 |
} |
| 298 |
// Contribution from nonlinear term |
| 299 |
if (i<nx_-1){ |
| 300 |
r[i] += nl_*u[i+1]*(u[i]+u[i+1])/6.0; |
| 301 |
} |
| 302 |
if (i>0) { |
| 303 |
r[i] -= nl_*u[i-1]*(u[i-1]+u[i])/6.0; |
| 304 |
} |
| 305 |
// Contribution from control |
| 306 |
r[i] -= dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]); |
| 307 |
// Contribution from right-hand side |
| 308 |
r[i] -= dx_*f_; |
| 309 |
} |
| 310 |
// Contribution from Dirichlet boundary terms |
| 311 |
r[0] -= nl_*(u0_*u[ 0]/6.0 + u0_*u0_/6.0) + nu_*u0_/dx_; |
| 312 |
r[nx_-1] += nl_*(u1_*u[nx_-1]/6.0 + u1_*u1_/6.0) - nu_*u1_/dx_; |
| 313 |
} |
| 314 |
|
| 315 |
/***************************************************************************/ |
| 316 |
/*********************** PDE JACOBIAN FUNCTIONS ****************************/ |
| 317 |
/***************************************************************************/ |
| 318 |
// Build PDE Jacobian trigiagonal matrix |
| 319 |
void compute_pde_jacobian(std::vector<Real> &dl, // Lower diagonal |
| 320 |
std::vector<Real> &d, // Diagonal |
| 321 |
std::vector<Real> &du, // Upper diagonal |
| 322 |
const std::vector<Real> &u) const { // State variable |
| 323 |
// Get Diagonal and Off-Diagonal Entries of linear PDE Jacobian |
| 324 |
d.clear(); |
| 325 |
d.resize(nx_,nu_*2.0/dx_); |
| 326 |
dl.clear(); |
| 327 |
dl.resize(nx_-1,-nu_/dx_); |
| 328 |
du.clear(); |
| 329 |
du.resize(nx_-1,-nu_/dx_); |
| 330 |
// Contribution from nonlinearity |
| 331 |
for (int i=0; i<nx_; i++) { |
| 332 |
if (i<nx_-1) { |
| 333 |
dl[i] += nl_*(-2.0*u[i]-u[i+1])/6.0; |
| 334 |
d[i] += nl_*u[i+1]/6.0; |
| 335 |
} |
| 336 |
if (i>0) { |
| 337 |
d[i] -= nl_*u[i-1]/6.0; |
| 338 |
du[i-1] += nl_*(u[i-1]+2.0*u[i])/6.0; |
| 339 |
} |
| 340 |
} |
| 341 |
// Contribution from Dirichlet boundary conditions |
| 342 |
d[ 0] -= nl_*u0_/6.0; |
| 343 |
d[nx_-1] += nl_*u1_/6.0; |
| 344 |
} |
| 345 |
|
| 346 |
// Apply PDE Jacobian to a vector |
| 347 |
void apply_pde_jacobian(std::vector<Real> &jv, |
| 348 |
const std::vector<Real> &v, |
| 349 |
const std::vector<Real> &u, |
| 350 |
const std::vector<Real> &z) const { |
| 351 |
// Fill jv |
| 352 |
for (int i = 0; i < nx_; i++) { |
| 353 |
jv[i] = nu_/dx_*2.0*v[i]; |
| 354 |
if ( i > 0 ) { |
| 355 |
jv[i] += -nu_/dx_*v[i-1]-nl_*(u[i-1]/6.0*v[i]+(u[i]+2.0*u[i-1])/6.0*v[i-1]); |
| 356 |
} |
| 357 |
if ( i < nx_-1 ) { |
| 358 |
jv[i] += -nu_/dx_*v[i+1]+nl_*(u[i+1]/6.0*v[i]+(u[i]+2.0*u[i+1])/6.0*v[i+1]); |
| 359 |
} |
| 360 |
} |
| 361 |
jv[ 0] -= nl_*u0_/6.0*v[0]; |
| 362 |
jv[nx_-1] += nl_*u1_/6.0*v[nx_-1]; |
| 363 |
} |
| 364 |
|
| 365 |
// Apply inverse PDE Jacobian to a vector |
| 366 |
void apply_inverse_pde_jacobian(std::vector<Real> &ijv, |
| 367 |
const std::vector<Real> &v, |
| 368 |
const std::vector<Real> &u, |
| 369 |
const std::vector<Real> &z) const { |
| 370 |
// Get PDE Jacobian |
| 371 |
std::vector<Real> d(nx_,0.0); |
| 372 |
std::vector<Real> dl(nx_-1,0.0); |
| 373 |
std::vector<Real> du(nx_-1,0.0); |
| 374 |
compute_pde_jacobian(dl,d,du,u); |
| 375 |
// Solve solve state sensitivity system at current time step |
| 376 |
linear_solve(ijv,dl,d,du,v); |
| 377 |
} |
| 378 |
|
| 379 |
// Apply adjoint PDE Jacobian to a vector |
| 380 |
void apply_adjoint_pde_jacobian(std::vector<Real> &ajv, |
| 381 |
const std::vector<Real> &v, |
| 382 |
const std::vector<Real> &u, |
| 383 |
const std::vector<Real> &z) const { |
| 384 |
// Fill jvp |
| 385 |
for (int i = 0; i < nx_; i++) { |
| 386 |
ajv[i] = nu_/dx_*2.0*v[i]; |
| 387 |
if ( i > 0 ) { |
| 388 |
ajv[i] += -nu_/dx_*v[i-1]-nl_*(u[i-1]/6.0*v[i] |
| 389 |
-(u[i-1]+2.0*u[i])/6.0*v[i-1]); |
| 390 |
} |
| 391 |
if ( i < nx_-1 ) { |
| 392 |
ajv[i] += -nu_/dx_*v[i+1]+nl_*(u[i+1]/6.0*v[i] |
| 393 |
-(u[i+1]+2.0*u[i])/6.0*v[i+1]); |
| 394 |
} |
| 395 |
} |
| 396 |
ajv[ 0] -= nl_*u0_/6.0*v[0]; |
| 397 |
ajv[nx_-1] += nl_*u1_/6.0*v[nx_-1]; |
| 398 |
} |
| 399 |
|
| 400 |
// Apply inverse adjoint PDE Jacobian to a vector |
| 401 |
void apply_inverse_adjoint_pde_jacobian(std::vector<Real> &iajv, |
| 402 |
const std::vector<Real> &v, |
| 403 |
const std::vector<Real> &u, |
| 404 |
const std::vector<Real> &z) const { |
| 405 |
// Get PDE Jacobian |
| 406 |
std::vector<Real> d(nx_,0.0); |
| 407 |
std::vector<Real> du(nx_-1,0.0); |
| 408 |
std::vector<Real> dl(nx_-1,0.0); |
| 409 |
compute_pde_jacobian(dl,d,du,u); |
| 410 |
// Solve solve adjoint system at current time step |
| 411 |
linear_solve(iajv,dl,d,du,v,true); |
| 412 |
} |
| 413 |
|
| 414 |
/***************************************************************************/ |
| 415 |
/*********************** CONTROL JACOBIAN FUNCTIONS ************************/ |
| 416 |
/***************************************************************************/ |
| 417 |
// Apply control Jacobian to a vector |
| 418 |
void apply_control_jacobian(std::vector<Real> &jv, |
| 419 |
const std::vector<Real> &v, |
| 420 |
const std::vector<Real> &u, |
| 421 |
const std::vector<Real> &z) const { |
| 422 |
for (int i=0; i<nx_; i++) { |
| 423 |
// Contribution from control |
| 424 |
jv[i] = -dx_/6.0*(v[i]+4.0*v[i+1]+v[i+2]); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
// Apply adjoint control Jacobian to a vector |
| 429 |
void apply_adjoint_control_jacobian(std::vector<Real> &jv, |
| 430 |
const std::vector<Real> &v, |
| 431 |
const std::vector<Real> &u, |
| 432 |
const std::vector<Real> &z) const { |
| 433 |
for (int i=0; i<nx_+2; i++) { |
| 434 |
if ( i == 0 ) { |
| 435 |
jv[i] = -dx_/6.0*v[i]; |
| 436 |
} |
| 437 |
else if ( i == 1 ) { |
| 438 |
jv[i] = -dx_/6.0*(4.0*v[i-1]+v[i]); |
| 439 |
} |
| 440 |
else if ( i == nx_ ) { |
| 441 |
jv[i] = -dx_/6.0*(4.0*v[i-1]+v[i-2]); |
| 442 |
} |
| 443 |
else if ( i == nx_+1 ) { |
| 444 |
jv[i] = -dx_/6.0*v[i-2]; |
| 445 |
} |
| 446 |
else { |
| 447 |
jv[i] = -dx_/6.0*(v[i-2]+4.0*v[i-1]+v[i]); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
/***************************************************************************/ |
| 453 |
/*********************** AJDOINT HESSIANS **********************************/ |
| 454 |
/***************************************************************************/ |
| 455 |
void apply_adjoint_pde_hessian(std::vector<Real> &ahwv, |
| 456 |
const std::vector<Real> &w, |
| 457 |
const std::vector<Real> &v, |
| 458 |
const std::vector<Real> &u, |
| 459 |
const std::vector<Real> &z) const { |
| 460 |
for (int i=0; i<nx_; i++) { |
| 461 |
// Contribution from nonlinear term |
| 462 |
ahwv[i] = 0.0; |
| 463 |
if (i<nx_-1){ |
| 464 |
ahwv[i] += (w[i]*v[i+1] - w[i+1]*(2.0*v[i]+v[i+1]))/6.0; |
| 465 |
} |
| 466 |
if (i>0) { |
| 467 |
ahwv[i] += (w[i-1]*(v[i-1]+2.0*v[i]) - w[i]*v[i-1])/6.0; |
| 468 |
} |
| 469 |
} |
| 470 |
//ahwv.assign(u.size(),0.0); |
| 471 |
} |
| 472 |
void apply_adjoint_pde_control_hessian(std::vector<Real> &ahwv, |
| 473 |
const std::vector<Real> &w, |
| 474 |
const std::vector<Real> &v, |
| 475 |
const std::vector<Real> &u, |
| 476 |
const std::vector<Real> &z) { |
| 477 |
ahwv.assign(u.size(),0.0); |
| 478 |
} |
| 479 |
void apply_adjoint_control_pde_hessian(std::vector<Real> &ahwv, |
| 480 |
const std::vector<Real> &w, |
| 481 |
const std::vector<Real> &v, |
| 482 |
const std::vector<Real> &u, |
| 483 |
const std::vector<Real> &z) { |
| 484 |
ahwv.assign(z.size(),0.0); |
| 485 |
} |
| 486 |
void apply_adjoint_control_hessian(std::vector<Real> &ahwv, |
| 487 |
const std::vector<Real> &w, |
| 488 |
const std::vector<Real> &v, |
| 489 |
const std::vector<Real> &u, |
| 490 |
const std::vector<Real> &z) { |
| 491 |
ahwv.assign(z.size(),0.0); |
| 492 |
} |
| 493 |
}; |
| 494 |
|
| 495 |
template<class Real> |
| 496 |
class L2VectorPrimal : public ROL::Vector<Real> { |
| 497 |
private: |
| 498 |
ROL::Ptr<std::vector<Real> > vec_; |
| 499 |
ROL::Ptr<BurgersFEM<Real> > fem_; |
| 500 |
|
| 501 |
mutable ROL::Ptr<L2VectorDual<Real> > dual_vec_; |
| 502 |
|
| 503 |
public: |
| 504 |
L2VectorPrimal(const ROL::Ptr<std::vector<Real> > & vec, |
| 505 |
const ROL::Ptr<BurgersFEM<Real> > &fem) |
| 506 |
: vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} |
| 507 |
|
| 508 |
void set( const ROL::Vector<Real> &x ) { |
| 509 |
const L2VectorPrimal &ex = dynamic_cast<const L2VectorPrimal&>(x); |
| 510 |
const std::vector<Real>& xval = *ex.getVector(); |
| 511 |
std::copy(xval.begin(),xval.end(),vec_->begin()); |
| 512 |
} |
| 513 |
|
| 514 |
void plus( const ROL::Vector<Real> &x ) { |
| 515 |
const L2VectorPrimal &ex = dynamic_cast<const L2VectorPrimal&>(x); |
| 516 |
const std::vector<Real>& xval = *ex.getVector(); |
| 517 |
unsigned dimension = vec_->size(); |
| 518 |
for (unsigned i=0; i<dimension; i++) { |
| 519 |
(*vec_)[i] += xval[i]; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
void scale( const Real alpha ) { |
| 524 |
unsigned dimension = vec_->size(); |
| 525 |
for (unsigned i=0; i<dimension; i++) { |
| 526 |
(*vec_)[i] *= alpha; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 531 |
const L2VectorPrimal & ex = dynamic_cast<const L2VectorPrimal&>(x); |
| 532 |
const std::vector<Real>& xval = *ex.getVector(); |
| 533 |
return fem_->compute_L2_dot(xval,*vec_); |
| 534 |
} |
| 535 |
|
| 536 |
Real norm() const { |
| 537 |
Real val = 0; |
| 538 |
val = std::sqrt( dot(*this) ); |
| 539 |
return val; |
| 540 |
} |
| 541 |
|
| 542 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 543 |
return ROL::makePtr<L2VectorPrimal>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 544 |
} |
| 545 |
|
| 546 |
ROL::Ptr<const std::vector<Real> > getVector() const { |
| 547 |
return vec_; |
| 548 |
} |
| 549 |
|
| 550 |
ROL::Ptr<std::vector<Real> > getVector() { |
| 551 |
return vec_; |
| 552 |
} |
| 553 |
|
| 554 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 555 |
ROL::Ptr<L2VectorPrimal> e |
| 556 |
= ROL::makePtr<L2VectorPrimal>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 557 |
(*e->getVector())[i] = 1.0; |
| 558 |
return e; |
| 559 |
} |
| 560 |
|
| 561 |
int dimension() const { |
| 562 |
return vec_->size(); |
| 563 |
} |
| 564 |
|
| 565 |
const ROL::Vector<Real>& dual() const { |
| 566 |
dual_vec_ = ROL::makePtr<L2VectorDual<Real>>( |
| 567 |
ROL::makePtr<std::vector<Real>>(*vec_),fem_); |
| 568 |
|
| 569 |
fem_->apply_mass(*(ROL::constPtrCast<std::vector<Real> >(dual_vec_->getVector())),*vec_); |
| 570 |
return *dual_vec_; |
| 571 |
} |
| 572 |
|
| 573 |
}; |
| 574 |
|
| 575 |
template<class Real> |
| 576 |
class L2VectorDual : public ROL::Vector<Real> { |
| 577 |
private: |
| 578 |
ROL::Ptr<std::vector<Real> > vec_; |
| 579 |
ROL::Ptr<BurgersFEM<Real> > fem_; |
| 580 |
|
| 581 |
mutable ROL::Ptr<L2VectorPrimal<Real> > dual_vec_; |
| 582 |
|
| 583 |
public: |
| 584 |
L2VectorDual(const ROL::Ptr<std::vector<Real> > & vec, |
| 585 |
const ROL::Ptr<BurgersFEM<Real> > &fem) |
| 586 |
: vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} |
| 587 |
|
| 588 |
void set( const ROL::Vector<Real> &x ) { |
| 589 |
const L2VectorDual &ex = dynamic_cast<const L2VectorDual&>(x); |
| 590 |
const std::vector<Real>& xval = *ex.getVector(); |
| 591 |
std::copy(xval.begin(),xval.end(),vec_->begin()); |
| 592 |
} |
| 593 |
|
| 594 |
void plus( const ROL::Vector<Real> &x ) { |
| 595 |
const L2VectorDual &ex = dynamic_cast<const L2VectorDual&>(x); |
| 596 |
const std::vector<Real>& xval = *ex.getVector(); |
| 597 |
unsigned dimension = vec_->size(); |
| 598 |
for (unsigned i=0; i<dimension; i++) { |
| 599 |
(*vec_)[i] += xval[i]; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
void scale( const Real alpha ) { |
| 604 |
unsigned dimension = vec_->size(); |
| 605 |
for (unsigned i=0; i<dimension; i++) { |
| 606 |
(*vec_)[i] *= alpha; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 611 |
const L2VectorDual & ex = dynamic_cast<const L2VectorDual&>(x); |
| 612 |
const std::vector<Real>& xval = *ex.getVector(); |
| 613 |
unsigned dimension = vec_->size(); |
| 614 |
std::vector<Real> Mx(dimension,0.0); |
| 615 |
fem_->apply_inverse_mass(Mx,xval); |
| 616 |
Real val = 0.0; |
| 617 |
for (unsigned i = 0; i < dimension; i++) { |
| 618 |
val += Mx[i]*(*vec_)[i]; |
| 619 |
} |
| 620 |
return val; |
| 621 |
} |
| 622 |
|
| 623 |
Real norm() const { |
| 624 |
Real val = 0; |
| 625 |
val = std::sqrt( dot(*this) ); |
| 626 |
return val; |
| 627 |
} |
| 628 |
|
| 629 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 630 |
return ROL::makePtr<L2VectorDual>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 631 |
} |
| 632 |
|
| 633 |
ROL::Ptr<const std::vector<Real> > getVector() const { |
| 634 |
return vec_; |
| 635 |
} |
| 636 |
|
| 637 |
ROL::Ptr<std::vector<Real> > getVector() { |
| 638 |
return vec_; |
| 639 |
} |
| 640 |
|
| 641 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 642 |
ROL::Ptr<L2VectorDual> e |
| 643 |
= ROL::makePtr<L2VectorDual>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 644 |
(*e->getVector())[i] = 1.0; |
| 645 |
return e; |
| 646 |
} |
| 647 |
|
| 648 |
int dimension() const { |
| 649 |
return vec_->size(); |
| 650 |
} |
| 651 |
|
| 652 |
const ROL::Vector<Real>& dual() const { |
| 653 |
dual_vec_ = ROL::makePtr<L2VectorPrimal<Real>>( |
| 654 |
ROL::makePtr<std::vector<Real>>(*vec_),fem_); |
| 655 |
|
| 656 |
fem_->apply_inverse_mass(*(ROL::constPtrCast<std::vector<Real> >(dual_vec_->getVector())),*vec_); |
| 657 |
return *dual_vec_; |
| 658 |
} |
| 659 |
|
| 660 |
}; |
| 661 |
|
| 662 |
template<class Real> |
| 663 |
class H1VectorPrimal : public ROL::Vector<Real> { |
| 664 |
private: |
| 665 |
ROL::Ptr<std::vector<Real> > vec_; |
| 666 |
ROL::Ptr<BurgersFEM<Real> > fem_; |
| 667 |
|
| 668 |
mutable ROL::Ptr<H1VectorDual<Real> > dual_vec_; |
| 669 |
|
| 670 |
public: |
| 671 |
H1VectorPrimal(const ROL::Ptr<std::vector<Real> > & vec, |
| 672 |
const ROL::Ptr<BurgersFEM<Real> > &fem) |
| 673 |
: vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} |
| 674 |
|
| 675 |
void set( const ROL::Vector<Real> &x ) { |
| 676 |
const H1VectorPrimal &ex = dynamic_cast<const H1VectorPrimal&>(x); |
| 677 |
const std::vector<Real>& xval = *ex.getVector(); |
| 678 |
std::copy(xval.begin(),xval.end(),vec_->begin()); |
| 679 |
} |
| 680 |
|
| 681 |
void plus( const ROL::Vector<Real> &x ) { |
| 682 |
const H1VectorPrimal &ex = dynamic_cast<const H1VectorPrimal&>(x); |
| 683 |
const std::vector<Real>& xval = *ex.getVector(); |
| 684 |
unsigned dimension = vec_->size(); |
| 685 |
for (unsigned i=0; i<dimension; i++) { |
| 686 |
(*vec_)[i] += xval[i]; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
void scale( const Real alpha ) { |
| 691 |
unsigned dimension = vec_->size(); |
| 692 |
for (unsigned i=0; i<dimension; i++) { |
| 693 |
(*vec_)[i] *= alpha; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 698 |
const H1VectorPrimal & ex = dynamic_cast<const H1VectorPrimal&>(x); |
| 699 |
const std::vector<Real>& xval = *ex.getVector(); |
| 700 |
return fem_->compute_H1_dot(xval,*vec_); |
| 701 |
} |
| 702 |
|
| 703 |
Real norm() const { |
| 704 |
Real val = 0; |
| 705 |
val = std::sqrt( dot(*this) ); |
| 706 |
return val; |
| 707 |
} |
| 708 |
|
| 709 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 710 |
return ROL::makePtr<H1VectorPrimal>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 711 |
} |
| 712 |
|
| 713 |
ROL::Ptr<const std::vector<Real> > getVector() const { |
| 714 |
return vec_; |
| 715 |
} |
| 716 |
|
| 717 |
ROL::Ptr<std::vector<Real> > getVector() { |
| 718 |
return vec_; |
| 719 |
} |
| 720 |
|
| 721 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 722 |
ROL::Ptr<H1VectorPrimal> e |
| 723 |
= ROL::makePtr<H1VectorPrimal>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 724 |
(*e->getVector())[i] = 1.0; |
| 725 |
return e; |
| 726 |
} |
| 727 |
|
| 728 |
int dimension() const { |
| 729 |
return vec_->size(); |
| 730 |
} |
| 731 |
|
| 732 |
const ROL::Vector<Real>& dual() const { |
| 733 |
dual_vec_ = ROL::makePtr<H1VectorDual<Real>>( |
| 734 |
ROL::makePtr<std::vector<Real>>(*vec_),fem_); |
| 735 |
|
| 736 |
fem_->apply_H1(*(ROL::constPtrCast<std::vector<Real> >(dual_vec_->getVector())),*vec_); |
| 737 |
return *dual_vec_; |
| 738 |
} |
| 739 |
|
| 740 |
}; |
| 741 |
|
| 742 |
template<class Real> |
| 743 |
class H1VectorDual : public ROL::Vector<Real> { |
| 744 |
private: |
| 745 |
ROL::Ptr<std::vector<Real> > vec_; |
| 746 |
ROL::Ptr<BurgersFEM<Real> > fem_; |
| 747 |
|
| 748 |
mutable ROL::Ptr<H1VectorPrimal<Real> > dual_vec_; |
| 749 |
|
| 750 |
public: |
| 751 |
H1VectorDual(const ROL::Ptr<std::vector<Real> > & vec, |
| 752 |
const ROL::Ptr<BurgersFEM<Real> > &fem) |
| 753 |
: vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} |
| 754 |
|
| 755 |
void set( const ROL::Vector<Real> &x ) { |
| 756 |
const H1VectorDual &ex = dynamic_cast<const H1VectorDual&>(x); |
| 757 |
const std::vector<Real>& xval = *ex.getVector(); |
| 758 |
std::copy(xval.begin(),xval.end(),vec_->begin()); |
| 759 |
} |
| 760 |
|
| 761 |
void plus( const ROL::Vector<Real> &x ) { |
| 762 |
const H1VectorDual &ex = dynamic_cast<const H1VectorDual&>(x); |
| 763 |
const std::vector<Real>& xval = *ex.getVector(); |
| 764 |
unsigned dimension = vec_->size(); |
| 765 |
for (unsigned i=0; i<dimension; i++) { |
| 766 |
(*vec_)[i] += xval[i]; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
void scale( const Real alpha ) { |
| 771 |
unsigned dimension = vec_->size(); |
| 772 |
for (unsigned i=0; i<dimension; i++) { |
| 773 |
(*vec_)[i] *= alpha; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 778 |
const H1VectorDual & ex = dynamic_cast<const H1VectorDual&>(x); |
| 779 |
const std::vector<Real>& xval = *ex.getVector(); |
| 780 |
unsigned dimension = vec_->size(); |
| 781 |
std::vector<Real> Mx(dimension,0.0); |
| 782 |
fem_->apply_inverse_H1(Mx,xval); |
| 783 |
Real val = 0.0; |
| 784 |
for (unsigned i = 0; i < dimension; i++) { |
| 785 |
val += Mx[i]*(*vec_)[i]; |
| 786 |
} |
| 787 |
return val; |
| 788 |
} |
| 789 |
|
| 790 |
Real norm() const { |
| 791 |
Real val = 0; |
| 792 |
val = std::sqrt( dot(*this) ); |
| 793 |
return val; |
| 794 |
} |
| 795 |
|
| 796 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 797 |
return ROL::makePtr<H1VectorDual>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 798 |
} |
| 799 |
|
| 800 |
ROL::Ptr<const std::vector<Real> > getVector() const { |
| 801 |
return vec_; |
| 802 |
} |
| 803 |
|
| 804 |
ROL::Ptr<std::vector<Real> > getVector() { |
| 805 |
return vec_; |
| 806 |
} |
| 807 |
|
| 808 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 809 |
ROL::Ptr<H1VectorDual> e |
| 810 |
= ROL::makePtr<H1VectorDual>( ROL::makePtr<std::vector<Real>>(vec_->size(),0.0),fem_); |
| 811 |
(*e->getVector())[i] = 1.0; |
| 812 |
return e; |
| 813 |
} |
| 814 |
|
| 815 |
int dimension() const { |
| 816 |
return vec_->size(); |
| 817 |
} |
| 818 |
|
| 819 |
const ROL::Vector<Real>& dual() const { |
| 820 |
dual_vec_ = ROL::makePtr<H1VectorPrimal<Real>>( |
| 821 |
ROL::makePtr<std::vector<Real>>(*vec_),fem_); |
| 822 |
|
| 823 |
fem_->apply_inverse_H1(*(ROL::constPtrCast<std::vector<Real> >(dual_vec_->getVector())),*vec_); |
| 824 |
return *dual_vec_; |
| 825 |
} |
| 826 |
|
| 827 |
}; |
| 828 |
|
| 829 |
template<class Real> |
| 830 |
class Constraint_BurgersControl : public ROL::Constraint_SimOpt<Real> { |
| 831 |
private: |
| 832 |
|
| 833 |
typedef H1VectorPrimal<Real> PrimalStateVector; |
| 834 |
typedef H1VectorDual<Real> DualStateVector; |
| 835 |
|
| 836 |
typedef L2VectorPrimal<Real> PrimalControlVector; |
| 837 |
typedef L2VectorDual<Real> DualControlVector; |
| 838 |
|
| 839 |
typedef H1VectorDual<Real> PrimalConstraintVector; |
| 840 |
typedef H1VectorPrimal<Real> DualConstraintVector; |
| 841 |
|
| 842 |
ROL::Ptr<BurgersFEM<Real> > fem_; |
| 843 |
bool useHessian_; |
| 844 |
|
| 845 |
public: |
| 846 |
Constraint_BurgersControl(const ROL::Ptr<BurgersFEM<Real> > &fem, |
| 847 |
const bool useHessian = true) |
| 848 |
: fem_(fem), useHessian_(useHessian) {} |
| 849 |
|
| 850 |
void value(ROL::Vector<Real> &c, const ROL::Vector<Real> &u, |
| 851 |
const ROL::Vector<Real> &z, Real &tol) { |
| 852 |
ROL::Ptr<std::vector<Real> > cp = dynamic_cast<PrimalConstraintVector&>(c).getVector(); |
| 853 |
ROL::Ptr<const std::vector<Real> > up = |
| 854 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 855 |
ROL::Ptr<const std::vector<Real> > zp = |
| 856 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 857 |
|
| 858 |
const std::vector<Real> param |
| 859 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 860 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 861 |
|
| 862 |
fem_->compute_residual(*cp,*up,*zp); |
| 863 |
} |
| 864 |
|
| 865 |
void applyJacobian_1(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u, |
| 866 |
const ROL::Vector<Real> &z, Real &tol) { |
| 867 |
ROL::Ptr<std::vector<Real> > jvp = dynamic_cast<PrimalConstraintVector&>(jv).getVector(); |
| 868 |
ROL::Ptr<const std::vector<Real> > vp = |
| 869 |
dynamic_cast<const PrimalStateVector&>(v).getVector(); |
| 870 |
ROL::Ptr<const std::vector<Real> > up = |
| 871 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 872 |
ROL::Ptr<const std::vector<Real> > zp = |
| 873 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 874 |
|
| 875 |
const std::vector<Real> param |
| 876 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 877 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 878 |
|
| 879 |
fem_->apply_pde_jacobian(*jvp,*vp,*up,*zp); |
| 880 |
} |
| 881 |
|
| 882 |
void applyJacobian_2(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u, |
| 883 |
const ROL::Vector<Real> &z, Real &tol) { |
| 884 |
ROL::Ptr<std::vector<Real> > jvp = dynamic_cast<PrimalConstraintVector&>(jv).getVector(); |
| 885 |
ROL::Ptr<const std::vector<Real> > vp = |
| 886 |
dynamic_cast<const PrimalControlVector&>(v).getVector(); |
| 887 |
ROL::Ptr<const std::vector<Real> > up = |
| 888 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 889 |
ROL::Ptr<const std::vector<Real> > zp = |
| 890 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 891 |
|
| 892 |
const std::vector<Real> param |
| 893 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 894 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 895 |
|
| 896 |
fem_->apply_control_jacobian(*jvp,*vp,*up,*zp); |
| 897 |
} |
| 898 |
|
| 899 |
void applyInverseJacobian_1(ROL::Vector<Real> &ijv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u, |
| 900 |
const ROL::Vector<Real> &z, Real &tol) { |
| 901 |
ROL::Ptr<std::vector<Real> > ijvp = |
| 902 |
dynamic_cast<PrimalStateVector&>(ijv).getVector(); |
| 903 |
ROL::Ptr<const std::vector<Real> > vp = |
| 904 |
dynamic_cast<const PrimalConstraintVector&>(v).getVector(); |
| 905 |
ROL::Ptr<const std::vector<Real> > up = |
| 906 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 907 |
ROL::Ptr<const std::vector<Real> > zp = |
| 908 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 909 |
|
| 910 |
const std::vector<Real> param |
| 911 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 912 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 913 |
|
| 914 |
fem_->apply_inverse_pde_jacobian(*ijvp,*vp,*up,*zp); |
| 915 |
} |
| 916 |
|
| 917 |
void applyAdjointJacobian_1(ROL::Vector<Real> &ajv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u, |
| 918 |
const ROL::Vector<Real> &z, Real &tol) { |
| 919 |
ROL::Ptr<std::vector<Real> > jvp = |
| 920 |
dynamic_cast<DualStateVector&>(ajv).getVector(); |
| 921 |
ROL::Ptr<const std::vector<Real> > vp = |
| 922 |
dynamic_cast<const DualConstraintVector&>(v).getVector(); |
| 923 |
ROL::Ptr<const std::vector<Real> > up = |
| 924 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 925 |
ROL::Ptr<const std::vector<Real> > zp = |
| 926 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 927 |
|
| 928 |
const std::vector<Real> param |
| 929 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 930 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 931 |
|
| 932 |
fem_->apply_adjoint_pde_jacobian(*jvp,*vp,*up,*zp); |
| 933 |
} |
| 934 |
|
| 935 |
void applyAdjointJacobian_2(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u, |
| 936 |
const ROL::Vector<Real> &z, Real &tol) { |
| 937 |
ROL::Ptr<std::vector<Real> > jvp = |
| 938 |
dynamic_cast<DualControlVector&>(jv).getVector(); |
| 939 |
ROL::Ptr<const std::vector<Real> > vp = |
| 940 |
dynamic_cast<const DualConstraintVector&>(v).getVector(); |
| 941 |
ROL::Ptr<const std::vector<Real> > up = |
| 942 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 943 |
ROL::Ptr<const std::vector<Real> > zp = |
| 944 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 945 |
|
| 946 |
const std::vector<Real> param |
| 947 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 948 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 949 |
|
| 950 |
fem_->apply_adjoint_control_jacobian(*jvp,*vp,*up,*zp); |
| 951 |
} |
| 952 |
|
| 953 |
void applyInverseAdjointJacobian_1(ROL::Vector<Real> &iajv, const ROL::Vector<Real> &v, |
| 954 |
const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 955 |
ROL::Ptr<std::vector<Real> > iajvp = |
| 956 |
dynamic_cast<DualConstraintVector&>(iajv).getVector(); |
| 957 |
ROL::Ptr<const std::vector<Real> > vp = |
| 958 |
dynamic_cast<const DualStateVector&>(v).getVector(); |
| 959 |
ROL::Ptr<const std::vector<Real> > up = |
| 960 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 961 |
ROL::Ptr<const std::vector<Real> > zp = |
| 962 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 963 |
|
| 964 |
const std::vector<Real> param |
| 965 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 966 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 967 |
|
| 968 |
fem_->apply_inverse_adjoint_pde_jacobian(*iajvp,*vp,*up,*zp); |
| 969 |
} |
| 970 |
|
| 971 |
void applyAdjointHessian_11(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v, |
| 972 |
const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 973 |
if ( useHessian_ ) { |
| 974 |
ROL::Ptr<std::vector<Real> > ahwvp = |
| 975 |
dynamic_cast<DualStateVector&>(ahwv).getVector(); |
| 976 |
ROL::Ptr<const std::vector<Real> > wp = |
| 977 |
dynamic_cast<const DualConstraintVector&>(w).getVector(); |
| 978 |
ROL::Ptr<const std::vector<Real> > vp = |
| 979 |
dynamic_cast<const PrimalStateVector&>(v).getVector(); |
| 980 |
ROL::Ptr<const std::vector<Real> > up = |
| 981 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 982 |
ROL::Ptr<const std::vector<Real> > zp = |
| 983 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 984 |
|
| 985 |
const std::vector<Real> param |
| 986 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 987 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 988 |
|
| 989 |
fem_->apply_adjoint_pde_hessian(*ahwvp,*wp,*vp,*up,*zp); |
| 990 |
} |
| 991 |
else { |
| 992 |
ahwv.zero(); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
void applyAdjointHessian_12(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v, |
| 997 |
const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 998 |
if ( useHessian_ ) { |
| 999 |
ROL::Ptr<std::vector<Real> > ahwvp = |
| 1000 |
dynamic_cast<DualControlVector&>(ahwv).getVector(); |
| 1001 |
ROL::Ptr<const std::vector<Real> > wp = |
| 1002 |
dynamic_cast<const DualConstraintVector&>(w).getVector(); |
| 1003 |
ROL::Ptr<const std::vector<Real> > vp = |
| 1004 |
dynamic_cast<const PrimalStateVector&>(v).getVector(); |
| 1005 |
ROL::Ptr<const std::vector<Real> > up = |
| 1006 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 1007 |
ROL::Ptr<const std::vector<Real> > zp = |
| 1008 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 1009 |
|
| 1010 |
const std::vector<Real> param |
| 1011 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 1012 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 1013 |
|
| 1014 |
fem_->apply_adjoint_control_pde_hessian(*ahwvp,*wp,*vp,*up,*zp); |
| 1015 |
} |
| 1016 |
else { |
| 1017 |
ahwv.zero(); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
void applyAdjointHessian_21(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v, |
| 1021 |
const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 1022 |
if ( useHessian_ ) { |
| 1023 |
ROL::Ptr<std::vector<Real> > ahwvp = |
| 1024 |
dynamic_cast<DualStateVector&>(ahwv).getVector(); |
| 1025 |
ROL::Ptr<const std::vector<Real> > wp = |
| 1026 |
dynamic_cast<const DualConstraintVector&>(w).getVector(); |
| 1027 |
ROL::Ptr<const std::vector<Real> > vp = |
| 1028 |
dynamic_cast<const PrimalControlVector&>(v).getVector(); |
| 1029 |
ROL::Ptr<const std::vector<Real> > up = |
| 1030 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 1031 |
ROL::Ptr<const std::vector<Real> > zp = |
| 1032 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 1033 |
|
| 1034 |
const std::vector<Real> param |
| 1035 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 1036 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 1037 |
|
| 1038 |
fem_->apply_adjoint_pde_control_hessian(*ahwvp,*wp,*vp,*up,*zp); |
| 1039 |
} |
| 1040 |
else { |
| 1041 |
ahwv.zero(); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
void applyAdjointHessian_22(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v, |
| 1045 |
const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 1046 |
if ( useHessian_ ) { |
| 1047 |
ROL::Ptr<std::vector<Real> > ahwvp = |
| 1048 |
dynamic_cast<DualControlVector&>(ahwv).getVector(); |
| 1049 |
ROL::Ptr<const std::vector<Real> > wp = |
| 1050 |
dynamic_cast<const DualConstraintVector&>(w).getVector(); |
| 1051 |
ROL::Ptr<const std::vector<Real> > vp = |
| 1052 |
dynamic_cast<const PrimalControlVector&>(v).getVector(); |
| 1053 |
ROL::Ptr<const std::vector<Real> > up = |
| 1054 |
dynamic_cast<const PrimalStateVector&>(u).getVector(); |
| 1055 |
ROL::Ptr<const std::vector<Real> > zp = |
| 1056 |
dynamic_cast<const PrimalControlVector&>(z).getVector(); |
| 1057 |
|
| 1058 |
const std::vector<Real> param |
| 1059 |
= ROL::Constraint_SimOpt<Real>::getParameter(); |
| 1060 |
fem_->set_problem_data(param[0],param[1],param[2],param[3]); |
| 1061 |
|
| 1062 |
fem_->apply_adjoint_control_hessian(*ahwvp,*wp,*vp,*up,*zp); |
| 1063 |
} |
| 1064 |
else { |
| 1065 |
ahwv.zero(); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
}; |
| 1069 |
|
| 1070 |
template<class Real, class Ordinal> |
| 1071 |
class L2VectorBatchManager : public ROL::TeuchosBatchManager<Real,Ordinal> { |
| 1072 |
private: |
| 1073 |
void cast_vector(ROL::Ptr<std::vector<Real> > &xvec, |
| 1074 |
ROL::Vector<Real> &x) const { |
| 1075 |
try { |
| 1076 |
xvec = ROL::constPtrCast<std::vector<Real> >( |
| 1077 |
(dynamic_cast<L2VectorPrimal<Real>&>(x)).getVector()); |
| 1078 |
} |
| 1079 |
catch (std::exception &e) { |
| 1080 |
xvec = ROL::constPtrCast<std::vector<Real> >( |
| 1081 |
(dynamic_cast<L2VectorDual<Real>&>(x)).getVector()); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
public: |
| 1085 |
L2VectorBatchManager(const ROL::Ptr<const Teuchos::Comm<int> > &comm) |
| 1086 |
: ROL::TeuchosBatchManager<Real,Ordinal>(comm) {} |
| 1087 |
void sumAll(ROL::Vector<Real> &input, ROL::Vector<Real> &output) { |
| 1088 |
ROL::Ptr<std::vector<Real> > input_ptr; |
| 1089 |
cast_vector(input_ptr,input); |
| 1090 |
int dim_i = input_ptr->size(); |
| 1091 |
ROL::Ptr<std::vector<Real> > output_ptr; |
| 1092 |
cast_vector(output_ptr,output); |
| 1093 |
int dim_o = output_ptr->size(); |
| 1094 |
if ( dim_i != dim_o ) { |
| 1095 |
std::cout << "L2VectorBatchManager: DIMENSION MISMATCH ON RANK " |
| 1096 |
<< ROL::TeuchosBatchManager<Real,Ordinal>::batchID() << "\n"; |
| 1097 |
} |
| 1098 |
else { |
| 1099 |
ROL::TeuchosBatchManager<Real,Ordinal>::sumAll(&(*input_ptr)[0],&(*output_ptr)[0],dim_i); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
}; |