19 #ifndef ROL_POISSONINVERSION_HPP
20 #define ROL_POISSONINVERSION_HPP
26 #include "Teuchos_LAPACK.hpp"
78 for (
uint i = 0; i <
nz_; i++) {
80 val +=
alpha_/2.0 *
hz_ * (*zp)[i]*(*zp)[i];
87 val +=
alpha_ * std::sqrt(std::pow((*zp)[i]-(*zp)[i+1],2.0)+
eps_);
102 ROL::Ptr<const vector> zp =
getVector(z);
105 for (
uint i = 0; i <
nz_; i++) {
106 (*gp)[i] =
alpha_ *
hz_ * (*zp)[i]/std::sqrt(std::pow((*zp)[i],2.0)+
eps_);
110 ROL::Ptr<const vector> zp =
getVector(z);
114 for (
uint i = 0; i <
nz_; i++) {
116 diff = (*zp)[i]-(*zp)[i+1];
117 (*gp)[i] =
alpha_ * diff/std::sqrt(std::pow(diff,2.0)+
eps_);
119 else if ( i == nz_-1 ) {
120 diff = (*zp)[i-1]-(*zp)[i];
121 (*gp)[i] = -
alpha_ * diff/std::sqrt(std::pow(diff,2.0)+
eps_);
124 diff = (*zp)[i]-(*zp)[i+1];
125 (*gp)[i] =
alpha_ * diff/std::sqrt(std::pow(diff,2.0)+
eps_);
126 diff = (*zp)[i-1]-(*zp)[i];
127 (*gp)[i] -=
alpha_ * diff/std::sqrt(std::pow(diff,2.0)+
eps_);
142 ROL::Ptr<const vector> zp =
getVector(z);
143 ROL::Ptr<const vector> vp =
getVector(v);
146 for (
uint i = 0; i <
nz_; i++) {
147 (*hvp)[i] =
alpha_*
hz_*(*vp)[i]*
eps_/std::pow(std::pow((*zp)[i],2.0)+
eps_,3.0/2.0);
151 ROL::Ptr<const vector> zp =
getVector(z);
152 ROL::Ptr<const vector> vp =
getVector(v);
157 for (
uint i = 0; i <
nz_; i++) {
159 diff1 = (*zp)[i]-(*zp)[i+1];
160 diff1 =
eps_/std::pow(std::pow(diff1,2.0)+
eps_,3.0/2.0);
161 (*hvp)[i] =
alpha_* ((*vp)[i]*diff1 - (*vp)[i+1]*diff1);
163 else if ( i == nz_-1 ) {
164 diff2 = (*zp)[i-1]-(*zp)[i];
165 diff2 =
eps_/std::pow(std::pow(diff2,2.0)+
eps_,3.0/2.0);
166 (*hvp)[i] =
alpha_* (-(*vp)[i-1]*diff2 + (*vp)[i]*diff2);
169 diff1 = (*zp)[i]-(*zp)[i+1];
170 diff1 =
eps_/std::pow(std::pow(diff1,2.0)+
eps_,3.0/2.0);
171 diff2 = (*zp)[i-1]-(*zp)[i];
172 diff2 =
eps_/std::pow(std::pow(diff2,2.0)+
eps_,3.0/2.0);
173 (*hvp)[i] =
alpha_* (-(*vp)[i-1]*diff2 + (*vp)[i]*(diff1 + diff2) - (*vp)[i+1]*diff1);
183 ROL::Ptr<const vector> fp =
getVector(f);
186 for (
uint i = 0; i <
nu_; i++) {
188 (*Mfp)[i] =
hu_/6.0*(4.0*(*fp)[i] + (*fp)[i+1]);
190 else if ( i == nu_-1 ) {
191 (*Mfp)[i] =
hu_/6.0*((*fp)[i-1] + 4.0*(*fp)[i]);
194 (*Mfp)[i] =
hu_/6.0*((*fp)[i-1] + 4.0*(*fp)[i] + (*fp)[i+1]);
202 ROL::Ptr<const vector> zp =
getVector(z);
209 for (
uint i = 0; i <
nu_; i++ ) {
210 d[i] = (std::exp((*zp)[i]) + std::exp((*zp)[i+1]))/
hu_;
212 o[i] *= -std::exp((*zp)[i+1])/
hu_;
217 Teuchos::LAPACK<int,Real> lp;
221 lp.PTTRF(nu_,&d[0],&o[0],&info);
222 lp.PTTRS(nu_,nhrs,&d[0],&o[0],&(*bp)[0],ldb,&info);
235 ROL::Ptr<const vector> zp =
getVector(z);
236 ROL::Ptr<const vector> up =
getVector(u);
237 ROL::Ptr<const vector> dp =
getVector(d);
240 for (
uint i = 0; i <
nu_; i++) {
242 (*Bdp)[i] = 1.0/
hu_*( std::exp((*zp)[i])*(*up)[i]*(*dp)[i]
243 + std::exp((*zp)[i+1])*((*up)[i]-(*up)[i+1])*(*dp)[i+1] );
245 else if ( i == nu_-1 ) {
246 (*Bdp)[i] = 1.0/
hu_*( std::exp((*zp)[i])*((*up)[i]-(*up)[i-1])*(*dp)[i]
247 + std::exp((*zp)[i+1])*(*up)[i]*(*dp)[i+1] );
250 (*Bdp)[i] = 1.0/
hu_*( std::exp((*zp)[i])*((*up)[i]-(*up)[i-1])*(*dp)[i]
251 + std::exp((*zp)[i+1])*((*up)[i]-(*up)[i+1])*(*dp)[i+1] );
260 ROL::Ptr<const vector> zp =
getVector(z);
261 ROL::Ptr<const vector> up =
getVector(u);
262 ROL::Ptr<const vector> dp =
getVector(d);
265 for (
uint i = 0; i <
nz_; i++) {
267 (*Bdp)[i] = std::exp((*zp)[i])/
hu_*(*up)[i]*(*dp)[i];
269 else if ( i == nz_-1 ) {
270 (*Bdp)[i] = std::exp((*zp)[i])/
hu_*(*up)[i-1]*(*dp)[i-1];
273 (*Bdp)[i] = std::exp((*zp)[i])/
hu_*( ((*up)[i]-(*up)[i-1])*((*dp)[i]-(*dp)[i-1]) );
281 ROL::Ptr<const vector> zp =
getVector(z);
282 ROL::Ptr<const vector> vp =
getVector(v);
283 ROL::Ptr<const vector> up =
getVector(u);
284 ROL::Ptr<const vector> dp =
getVector(d);
287 for (
uint i = 0; i <
nz_; i++) {
289 (*Bdp)[i] = (*vp)[i]*std::exp((*zp)[i])/
hu_*(*up)[i]*(*dp)[i];
291 else if ( i == nz_-1 ) {
292 (*Bdp)[i] = (*vp)[i]*std::exp((*zp)[i])/
hu_*(*up)[i-1]*(*dp)[i-1];
295 (*Bdp)[i] = (*vp)[i]*std::exp((*zp)[i])/
hu_*( ((*up)[i]-(*up)[i-1])*((*dp)[i]-(*dp)[i-1]) );
309 ROL::Ptr<vector> bp = ROL::makePtr<vector>(
nu_, 0.0);
310 for (
uint i = 0; i <
nu_; i++ ) {
311 if ( (Real)(i+1)*
hu_ < 0.5 ) {
312 (*bp)[i] = 2.0*k1*
hu_;
314 else if ( std::abs((Real)(i+1)*
hu_ - 0.5) < ROL_EPSILON<Real>() ) {
315 (*bp)[i] = (k1+k2)*
hu_;
317 else if ( (Real)(i+1)*
hu_ > 0.5 ) {
318 (*bp)[i] = 2.0*k2*
hu_;
332 ROL::Ptr<const vector> up =
getVector(u);
333 ROL::Ptr<vector> rp = ROL::makePtr<vector>(
nu_,0.0);
336 for (
uint i = 0; i <
nu_; i++) {
348 SV b( ROL::makePtr<vector>(
nu_,0.0) );
358 SV res( ROL::makePtr<vector>(
nu_,0.0) );
360 SV res1( ROL::makePtr<vector>(
nu_,0.0) );
373 ROL::Ptr<vector> up = ROL::makePtr<vector>(
nu_,0.0);
379 ROL::Ptr<vector> rp = ROL::makePtr<vector>(
nu_,0.0);
382 for (
uint i = 0; i <
nu_; i++) {
386 ROL::Ptr<V> Mres = res.
clone();
388 return 0.5*Mres->dot(res) +
reg_value(z);
396 SV u( ROL::makePtr<vector>(
nu_,0.0) );
400 SV p( ROL::makePtr<std::vector<Real>>(
nu_,0.0) );
407 SV g_reg( ROL::makePtr<vector>(
nz_,0.0) );
419 SV u( ROL::makePtr<vector>(
nu_,0.0) );
423 SV p( ROL::makePtr<vector>(
nu_,0.0) );
427 SV w( ROL::makePtr<vector>(
nu_,0.0) );
431 SV q( ROL::makePtr<vector>(
nu_,0.0) );
438 SV tmp( ROL::makePtr<vector>(
nz_,0.0) );
448 SV hv_reg( ROL::makePtr<vector>(
nz_,0.0) );
466 int dim =
static_cast<int>(vp.size());
470 Teuchos::SerialDenseMatrix<int, Real> H(dim, dim);
472 H = computeDenseHessian<Real>(obj, x);
475 std::vector<vector> eigenvals = computeEigenvalues<Real>(H);
476 std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
479 Real inertia = (eigenvals[0])[0];
480 Real correction = 0.0;
481 if ( inertia <= 0.0 ) {
482 correction = 2.0*std::abs(inertia);
483 if ( inertia == 0.0 ) {
485 while ( eigenvals[0][cnt] == 0.0 ) {
488 correction = 0.5*eigenvals[0][cnt];
489 if ( cnt == dim-1 ) {
493 for (
int i=0; i<
dim; i++) {
494 H(i,i) += correction;
499 Teuchos::SerialDenseMatrix<int, Real> invH = computeInverse<Real>(H);
502 Teuchos::SerialDenseVector<int, Real> hv_teuchos(Teuchos::View, &((*hvp)[0]), dim);
503 const Teuchos::SerialDenseVector<int, Real> v_teuchos(Teuchos::View, &(vp[0]), dim);
504 hv_teuchos.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, invH, v_teuchos, 0.0);
518 return ROL::makePtr<Objective_PoissonInversion<Real>>(n,1.e-6);
525 ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
526 for (
int i = 0; i < n; i++ ) {
529 return ROL::makePtr<StdVector<Real>>(x0p);
536 ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
537 Real h = 1.0/((Real)n+1), pt = 0.0, k1 = 1.0, k2 = 2.0;
538 for(
int i = 0; i < n; i++ ) {
540 if ( pt >= 0.0 && pt < 0.5 ) {
541 (*xp)[i] = std::log(k1);
543 else if ( pt >= 0.5 && pt < 1.0 ) {
544 (*xp)[i] = std::log(k2);
547 return ROL::makePtr<StdVector<Real>>(xp);
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
void apply_transposed_linearized_control_operator(Vector< Real > &Bd, const Vector< Real > &z, const Vector< Real > &d, const Vector< Real > &u)
virtual void scale(const Real alpha)=0
Compute where .
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void solve_state_sensitivity_equation(Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z)
virtual void plus(const Vector &x)=0
Compute , where .
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
void gradient(Vector< Real > &g, const Vector< Real > &z, Real &tol)
Compute gradient.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Contains definitions for helper functions in ROL.
Defines the linear algebra or vector space interface.
getPoissonInversion(void)
void solve_poisson(Vector< Real > &u, const Vector< Real > &z, Vector< Real > &b)
Ptr< Vector< Real > > getInitialGuess(void) const
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)
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)
Real evaluate_target(Real x)
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
void solve_state_equation(Vector< Real > &u, const Vector< Real > &z)
void reg_hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &z)
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Contains definitions of test objective functions.
void apply_mass(Vector< Real > &Mf, const Vector< Real > &f)
void solve_adjoint_equation(Vector< Real > &p, const Vector< Real > &u, const Vector< Real > &z)
ROL::Ptr< vector > getVector(V &x)
void apply_linearized_control_operator(Vector< Real > &Bd, const Vector< Real > &z, const Vector< Real > &d, const Vector< Real > &u)
Ptr< Vector< Real > > getSolution(const int i=0) const
Real value(const Vector< Real > &z, Real &tol)
Compute value.
Objective_PoissonInversion(uint nz=32, Real alpha=1.e-4)
virtual void set(const Vector &x)
Set where .
Ptr< Objective< Real > > getObjective(void) const
Real reg_value(const Vector< Real > &z)
void reg_gradient(Vector< Real > &g, const Vector< Real > &z)
Poisson material inversion.
ROL::Ptr< const vector > getVector(const V &x)
std::vector< Real > vector