/usr/include/trilinos/ROL_PoissonInversion.hpp is in libtrilinos-rol-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | // @HEADER
// ************************************************************************
//
// Rapid Optimization Library (ROL) Package
// Copyright (2014) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact lead developers:
// Drew Kouri (dpkouri@sandia.gov) and
// Denis Ridzal (dridzal@sandia.gov)
//
// ************************************************************************
// @HEADER
/** \file
\brief Contains definitions for Poisson material inversion.
\author Created by D. Ridzal and D. Kouri.
*/
#ifndef USE_HESSVEC
#define USE_HESSVEC 1
#endif
#ifndef ROL_POISSONINVERSION_HPP
#define ROL_POISSONINVERSION_HPP
#include "ROL_StdVector.hpp"
#include "ROL_Objective.hpp"
#include "ROL_HelperFunctions.hpp"
#include "Teuchos_LAPACK.hpp"
namespace ROL {
namespace ZOO {
/** \brief Poisson material inversion.
*/
template<class Real>
class Objective_PoissonInversion : public Objective<Real> {
typedef std::vector<Real> vector;
typedef Vector<Real> V;
typedef StdVector<Real> SV;
typedef typename vector::size_type uint;
private:
uint nu_;
uint nz_;
Real hu_;
Real hz_;
Real alpha_;
Real eps_;
int reg_type_;
Teuchos::RCP<const vector> getVector( const V& x ) {
using Teuchos::dyn_cast;
return dyn_cast<const SV>(x).getVector();
}
Teuchos::RCP<vector> getVector( V& x ) {
using Teuchos::dyn_cast;
return dyn_cast<SV>(x).getVector();
}
public:
/* CONSTRUCTOR */
Objective_PoissonInversion(uint nz = 32, Real alpha = 1.e-4)
: nu_(nz-1), nz_(nz), hu_(1./((Real)nu_+1.)), hz_(hu_),
alpha_(alpha), eps_(1.e-4), reg_type_(2) {}
/* REGULARIZATION DEFINITIONS */
Real reg_value(const Vector<Real> &z) {
using Teuchos::RCP;
RCP<const vector> zp = getVector(z);
Real val = 0.0;
for (uint i = 0; i < nz_; i++) {
if ( reg_type_ == 2 ) {
val += alpha_/2.0 * hz_ * (*zp)[i]*(*zp)[i];
}
else if ( reg_type_ == 1 ) {
val += alpha_ * hz_ * std::sqrt((*zp)[i]*(*zp)[i] + eps_);
}
else if ( reg_type_ == 0 ) {
if ( i < nz_-1 ) {
val += alpha_ * std::sqrt(std::pow((*zp)[i]-(*zp)[i+1],2.0)+eps_);
}
}
}
return val;
}
void reg_gradient(Vector<Real> &g, const Vector<Real> &z) {
using Teuchos::RCP;
if ( reg_type_ == 2 ) {
g.set(z);
g.scale(alpha_*hz_);
}
else if ( reg_type_ == 1 ) {
RCP<const vector> zp = getVector(z);
RCP<vector > gp = getVector(g);
for (uint i = 0; i < nz_; i++) {
(*gp)[i] = alpha_ * hz_ * (*zp)[i]/std::sqrt(std::pow((*zp)[i],2.0)+eps_);
}
}
else if ( reg_type_ == 0 ) {
RCP<const vector> zp = getVector(z);
RCP<vector> gp = getVector(g);
Real diff = 0.0;
for (uint i = 0; i < nz_; i++) {
if ( i == 0 ) {
diff = (*zp)[i]-(*zp)[i+1];
(*gp)[i] = alpha_ * diff/std::sqrt(std::pow(diff,2.0)+eps_);
}
else if ( i == nz_-1 ) {
diff = (*zp)[i-1]-(*zp)[i];
(*gp)[i] = -alpha_ * diff/std::sqrt(std::pow(diff,2.0)+eps_);
}
else {
diff = (*zp)[i]-(*zp)[i+1];
(*gp)[i] = alpha_ * diff/std::sqrt(std::pow(diff,2.0)+eps_);
diff = (*zp)[i-1]-(*zp)[i];
(*gp)[i] -= alpha_ * diff/std::sqrt(std::pow(diff,2.0)+eps_);
}
}
}
}
void reg_hessVec(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &z) {
using Teuchos::RCP;
if ( reg_type_ == 2 ) {
hv.set(v);
hv.scale(alpha_*hz_);
}
else if ( reg_type_ == 1 ) {
RCP<const vector> zp = getVector(z);
RCP<const vector> vp = getVector(v);
RCP<vector> hvp = getVector(hv);
for (uint i = 0; i < nz_; i++) {
(*hvp)[i] = alpha_*hz_*(*vp)[i]*eps_/std::pow(std::pow((*zp)[i],2.0)+eps_,3.0/2.0);
}
}
else if ( reg_type_ == 0 ) {
RCP<const vector> zp = getVector(z);
RCP<const vector> vp = getVector(v);
RCP<vector> hvp = getVector(hv);
Real diff1 = 0.0;
Real diff2 = 0.0;
for (uint i = 0; i < nz_; i++) {
if ( i == 0 ) {
diff1 = (*zp)[i]-(*zp)[i+1];
diff1 = eps_/std::pow(std::pow(diff1,2.0)+eps_,3.0/2.0);
(*hvp)[i] = alpha_* ((*vp)[i]*diff1 - (*vp)[i+1]*diff1);
}
else if ( i == nz_-1 ) {
diff2 = (*zp)[i-1]-(*zp)[i];
diff2 = eps_/std::pow(std::pow(diff2,2.0)+eps_,3.0/2.0);
(*hvp)[i] = alpha_* (-(*vp)[i-1]*diff2 + (*vp)[i]*diff2);
}
else {
diff1 = (*zp)[i]-(*zp)[i+1];
diff1 = eps_/std::pow(std::pow(diff1,2.0)+eps_,3.0/2.0);
diff2 = (*zp)[i-1]-(*zp)[i];
diff2 = eps_/std::pow(std::pow(diff2,2.0)+eps_,3.0/2.0);
(*hvp)[i] = alpha_* (-(*vp)[i-1]*diff2 + (*vp)[i]*(diff1 + diff2) - (*vp)[i+1]*diff1);
}
}
}
}
/* FINITE ELEMENT DEFINTIONS */
void apply_mass(Vector<Real> &Mf, const Vector<Real> &f ) {
using Teuchos::RCP;
RCP<const vector> fp = getVector(f);
RCP<vector> Mfp = getVector(Mf);
for (uint i = 0; i < nu_; i++) {
if ( i == 0 ) {
(*Mfp)[i] = hu_/6.0*(4.0*(*fp)[i] + (*fp)[i+1]);
}
else if ( i == nu_-1 ) {
(*Mfp)[i] = hu_/6.0*((*fp)[i-1] + 4.0*(*fp)[i]);
}
else {
(*Mfp)[i] = hu_/6.0*((*fp)[i-1] + 4.0*(*fp)[i] + (*fp)[i+1]);
}
}
}
void solve_poisson(Vector<Real> &u, const Vector<Real> &z, Vector<Real> &b) {
using Teuchos::RCP;
RCP<const vector> zp = getVector(z);
RCP<vector> up = getVector(u);
RCP<vector> bp = getVector(b);
// Get Diagonal and Off-Diagonal Entries of PDE Jacobian
vector d(nu_,1.0);
vector o(nu_-1,1.0);
for ( uint i = 0; i < nu_; i++ ) {
d[i] = (std::exp((*zp)[i]) + std::exp((*zp)[i+1]))/hu_;
if ( i < nu_-1 ) {
o[i] *= -std::exp((*zp)[i+1])/hu_;
}
}
// Solve Tridiagonal System Using LAPACK's SPD Tridiagonal Solver
Teuchos::LAPACK<int,Real> lp;
int info;
int ldb = nu_;
int nhrs = 1;
lp.PTTRF(nu_,&d[0],&o[0],&info);
lp.PTTRS(nu_,nhrs,&d[0],&o[0],&(*bp)[0],ldb,&info);
u.set(b);
}
Real evaluate_target(Real x) {
return x*(1.0-x);
}
void apply_linearized_control_operator( Vector<Real> &Bd, const Vector<Real> &z,
const Vector<Real> &d, const Vector<Real> &u ) {
using Teuchos::RCP;
RCP<const vector> zp = getVector(z);
RCP<const vector> up = getVector(u);
RCP<const vector> dp = getVector(d);
RCP<vector> Bdp = getVector(Bd);
for (uint i = 0; i < nu_; i++) {
if ( i == 0 ) {
(*Bdp)[i] = 1.0/hu_*( std::exp((*zp)[i])*(*up)[i]*(*dp)[i]
+ std::exp((*zp)[i+1])*((*up)[i]-(*up)[i+1])*(*dp)[i+1] );
}
else if ( i == nu_-1 ) {
(*Bdp)[i] = 1.0/hu_*( std::exp((*zp)[i])*((*up)[i]-(*up)[i-1])*(*dp)[i]
+ std::exp((*zp)[i+1])*(*up)[i]*(*dp)[i+1] );
}
else {
(*Bdp)[i] = 1.0/hu_*( std::exp((*zp)[i])*((*up)[i]-(*up)[i-1])*(*dp)[i]
+ std::exp((*zp)[i+1])*((*up)[i]-(*up)[i+1])*(*dp)[i+1] );
}
}
}
void apply_transposed_linearized_control_operator( Vector<Real> &Bd, const Vector<Real> &z,
const Vector<Real> &d, const Vector<Real> &u ) {
using Teuchos::RCP;
RCP<const vector> zp = getVector(z);
RCP<const vector> up = getVector(u);
RCP<const vector> dp = getVector(d);
RCP<vector> Bdp = getVector(Bd);
for (uint i = 0; i < nz_; i++) {
if ( i == 0 ) {
(*Bdp)[i] = std::exp((*zp)[i])/hu_*(*up)[i]*(*dp)[i];
}
else if ( i == nz_-1 ) {
(*Bdp)[i] = std::exp((*zp)[i])/hu_*(*up)[i-1]*(*dp)[i-1];
}
else {
(*Bdp)[i] = std::exp((*zp)[i])/hu_*( ((*up)[i]-(*up)[i-1])*((*dp)[i]-(*dp)[i-1]) );
}
}
}
void apply_transposed_linearized_control_operator_2( Vector<Real> &Bd, const Vector<Real> &z, const Vector<Real> &v,
const Vector<Real> &d, const Vector<Real> &u ) {
using Teuchos::RCP;
RCP<const vector> zp = getVector(z);
RCP<const vector> vp = getVector(v);
RCP<const vector> up = getVector(u);
RCP<const vector> dp = getVector(d);
RCP<vector> Bdp = getVector(Bd);
for (uint i = 0; i < nz_; i++) {
if ( i == 0 ) {
(*Bdp)[i] = (*vp)[i]*std::exp((*zp)[i])/hu_*(*up)[i]*(*dp)[i];
}
else if ( i == nz_-1 ) {
(*Bdp)[i] = (*vp)[i]*std::exp((*zp)[i])/hu_*(*up)[i-1]*(*dp)[i-1];
}
else {
(*Bdp)[i] = (*vp)[i]*std::exp((*zp)[i])/hu_*( ((*up)[i]-(*up)[i-1])*((*dp)[i]-(*dp)[i-1]) );
}
}
}
/* STATE AND ADJOINT EQUATION DEFINTIONS */
void solve_state_equation(Vector<Real> &u, const Vector<Real> &z) {
using Teuchos::RCP;
using Teuchos::rcp;
Real k1 = 1.0;
Real k2 = 2.0;
// Right Hand Side
RCP<vector> bp = rcp( new vector(nu_, 0.0) );
for ( uint i = 0; i < nu_; i++ ) {
if ( (Real)(i+1)*hu_ < 0.5 ) {
(*bp)[i] = 2.0*k1*hu_;
}
else if ( std::abs((Real)(i+1)*hu_ - 0.5) < ROL_EPSILON<Real>() ) {
(*bp)[i] = (k1+k2)*hu_;
}
else if ( (Real)(i+1)*hu_ > 0.5 ) {
(*bp)[i] = 2.0*k2*hu_;
}
}
SV b(bp);
// Solve Equation
solve_poisson(u,z,b);
}
void solve_adjoint_equation(Vector<Real> &p, const Vector<Real> &u, const Vector<Real> &z) {
using Teuchos::RCP;
using Teuchos::rcp;
RCP<const vector> up = getVector(u);
RCP<vector> rp = rcp( new vector(nu_,0.0) );
SV res(rp);
for ( uint i = 0; i < nu_; i++) {
(*rp)[i] = -((*up)[i]-evaluate_target((Real)(i+1)*hu_));
}
StdVector<Real> Mres( Teuchos::rcp( new std::vector<Real>(nu_,0.0) ) );
apply_mass(Mres,res);
solve_poisson(p,z,Mres);
}
void solve_state_sensitivity_equation(Vector<Real> &w, const Vector<Real> &v,
const Vector<Real> &u, const Vector<Real> &z) {
using Teuchos::rcp;
SV b( rcp( new vector(nu_,0.0) ) );
apply_linearized_control_operator(b,z,v,u);
solve_poisson(w,z,b);
}
void solve_adjoint_sensitivity_equation(Vector<Real> &q, const Vector<Real> &w, const Vector<Real> &v,
const Vector<Real> &p, const Vector<Real> &u, const Vector<Real> &z) {
using Teuchos::rcp;
SV res( rcp( new vector(nu_,0.0) ) );
apply_mass(res,w);
SV res1( rcp( new vector(nu_,0.0) ) );
apply_linearized_control_operator(res1,z,v,p);
res.axpy(-1.0,res1);
solve_poisson(q,z,res);
}
/* OBJECTIVE FUNCTION DEFINITIONS */
Real value( const Vector<Real> &z, Real &tol ) {
using Teuchos::RCP;
using Teuchos::rcp;
// SOLVE STATE EQUATION
RCP<vector> up = rcp( new vector(nu_,0.0) );
SV u( up );
solve_state_equation(u,z);
// COMPUTE MISFIT
RCP<vector> rp = rcp( new vector(nu_,0.0) );
SV res( rp );
for ( uint i = 0; i < nu_; i++) {
(*rp)[i] = ((*up)[i]-evaluate_target((Real)(i+1)*hu_));
}
RCP<V> Mres = res.clone();
apply_mass(*Mres,res);
return 0.5*Mres->dot(res) + reg_value(z);
}
void gradient( Vector<Real> &g, const Vector<Real> &z, Real &tol ) {
using Teuchos::rcp;
// SOLVE STATE EQUATION
SV u( rcp( new vector(nu_,0.0) ) );
solve_state_equation(u,z);
// SOLVE ADJOINT EQUATION
SV p( Teuchos::rcp( new std::vector<Real>(nu_,0.0) ) );
solve_adjoint_equation(p,u,z);
// Apply Transpose of Linearized Control Operator
apply_transposed_linearized_control_operator(g,z,p,u);
// Regularization gradient
SV g_reg( rcp( new vector(nz_,0.0) ) );
reg_gradient(g_reg,z);
// Build Gradient
g.plus(g_reg);
}
#if USE_HESSVEC
void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &z, Real &tol ) {
using Teuchos::rcp;
// SOLVE STATE EQUATION
SV u( rcp( new vector(nu_,0.0) ) );
solve_state_equation(u,z);
// SOLVE ADJOINT EQUATION
SV p( rcp( new vector(nu_,0.0) ) );
solve_adjoint_equation(p,u,z);
// SOLVE STATE SENSITIVITY EQUATION
SV w( rcp( new vector(nu_,0.0) ) );
solve_state_sensitivity_equation(w,v,u,z);
// SOLVE ADJOINT SENSITIVITY EQUATION
SV q( rcp( new vector(nu_,0.0) ) );
solve_adjoint_sensitivity_equation(q,w,v,p,u,z);
// Apply Transpose of Linearized Control Operator
apply_transposed_linearized_control_operator(hv,z,q,u);
// Apply Transpose of Linearized Control Operator
SV tmp( rcp( new vector(nz_,0.0) ) );
apply_transposed_linearized_control_operator(tmp,z,w,p);
hv.axpy(-1.0,tmp);
// Apply Transpose of 2nd Derivative of Control Operator
tmp.zero();
apply_transposed_linearized_control_operator_2(tmp,z,v,p,u);
hv.plus(tmp);
// Regularization hessVec
SV hv_reg( rcp( new vector(nz_,0.0) ) );
reg_hessVec(hv_reg,v,z);
// Build hessVec
hv.plus(hv_reg);
}
#endif
void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
using Teuchos::RCP;
using Teuchos::rcp;
// Cast hv and v vectors to std::vector.
RCP<vector> hvp = getVector(hv);
std::vector<Real> vp(*getVector(v));
int dim = static_cast<int>(vp.size());
// Compute dense Hessian.
Teuchos::SerialDenseMatrix<int, Real> H(dim, dim);
Objective_PoissonInversion<Real> & obj = *this;
H = computeDenseHessian<Real>(obj, x);
// Compute eigenvalues, sort real part.
std::vector<vector> eigenvals = computeEigenvalues<Real>(H);
std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
// Perform 'inertia' correction.
Real inertia = (eigenvals[0])[0];
Real correction = 0.0;
if ( inertia <= 0.0 ) {
correction = 2.0*std::abs(inertia);
if ( inertia == 0.0 ) {
int cnt = 0;
while ( eigenvals[0][cnt] == 0.0 ) {
cnt++;
}
correction = 0.5*eigenvals[0][cnt];
if ( cnt == dim-1 ) {
correction = 1.0;
}
}
for (int i=0; i<dim; i++) {
H(i,i) += correction;
}
}
// Compute dense inverse Hessian.
Teuchos::SerialDenseMatrix<int, Real> invH = computeInverse<Real>(H);
// Apply dense inverse Hessian.
Teuchos::SerialDenseVector<int, Real> hv_teuchos(Teuchos::View, &((*hvp)[0]), dim);
const Teuchos::SerialDenseVector<int, Real> v_teuchos(Teuchos::View, &(vp[0]), dim);
hv_teuchos.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, invH, v_teuchos, 0.0);
}
};
template<class Real>
void getPoissonInversion( Teuchos::RCP<Objective<Real> > &obj,
Teuchos::RCP<Vector<Real> > &x0,
Teuchos::RCP<Vector<Real> > &x ) {
// Problem dimension
int n = 128;
// Get Initial Guess
Teuchos::RCP<std::vector<Real> > x0p = Teuchos::rcp(new std::vector<Real>(n,0.0));
for ( int i = 0; i < n; i++ ) {
(*x0p)[i] = 1.5;
}
x0 = Teuchos::rcp(new StdVector<Real>(x0p));
// Get Solution
Teuchos::RCP<std::vector<Real> > xp = Teuchos::rcp(new std::vector<Real>(n,0.0));
Real h = 1.0/((Real)n+1), pt = 0.0, k1 = 1.0, k2 = 2.0;
for( int i = 0; i < n; i++ ) {
pt = (Real)(i+1)*h;
if ( pt >= 0.0 && pt < 0.5 ) {
(*xp)[i] = std::log(k1);
}
else if ( pt >= 0.5 && pt < 1.0 ) {
(*xp)[i] = std::log(k2);
}
}
x = Teuchos::rcp(new StdVector<Real>(xp));
// Instantiate Objective Function
obj = Teuchos::rcp(new Objective_PoissonInversion<Real>(n,1.e-6));
}
} // End ZOO Namespace
} // End ROL Namespace
#endif
|