54 #include "Teuchos_GlobalMPISession.hpp"
55 #include "Teuchos_XMLParameterListHelpers.hpp"
56 #include "Teuchos_LAPACK.hpp"
81 case 1: val = ((x<0.5) ? 1.0 : 0.0);
break;
82 case 2: val = 1.0;
break;
83 case 3: val = std::abs(std::sin(8.0*M_PI*x));
break;
84 case 4: val = std::exp(-0.5*(x-0.5)*(x-0.5));
break;
90 return std::sqrt(this->
dot(r,r));
93 Real
dot(
const std::vector<Real> &x,
const std::vector<Real> &y) {
95 Real c = (((int)x.size()==this->
nx_) ? 4.0 : 2.0);
96 for (
unsigned i=0; i<x.size(); i++) {
98 ip += this->
dx_/6.0*(c*x[i] + x[i+1])*y[i];
100 else if ( i == x.size()-1 ) {
101 ip += this->
dx_/6.0*(x[i-1] + c*x[i])*y[i];
104 ip += this->
dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
112 void update(std::vector<Real> &u,
const std::vector<Real> &s,
const Real alpha=1.0) {
113 for (
unsigned i=0; i<u.size(); i++) {
118 void scale(std::vector<Real> &u,
const Real alpha=0.0) {
119 for (
unsigned i=0; i<u.size(); i++) {
124 void apply_mass(std::vector<Real> &Mu,
const std::vector<Real> &u ) {
125 Mu.resize(u.size(),0.0);
126 Real c = (((int)u.size()==this->
nx_) ? 4.0 : 2.0);
127 for (
unsigned i=0; i<u.size(); i++) {
129 Mu[i] = this->
dx_/6.0*(c*u[i] + u[i+1]);
131 else if ( i == u.size()-1 ) {
132 Mu[i] = this->
dx_/6.0*(u[i-1] + c*u[i]);
135 Mu[i] = this->
dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]);
141 const std::vector<Real> &z,
const std::vector<Real> ¶m) {
143 r.resize(this->
nx_,0.0);
144 Real nu = std::pow(10.0,param[0]-2.0);
145 Real f = param[1]/100.0;
146 Real u0 = 1.0+param[2]/1000.0;
147 Real u1 = param[3]/1000.0;
148 for (
int i=0; i<this->
nx_; i++) {
151 r[i] = nu/this->
dx_*(2.0*u[i]-u[i+1]);
153 else if (i==this->nx_-1) {
154 r[i] = nu/this->
dx_*(2.0*u[i]-u[i-1]);
157 r[i] = nu/this->
dx_*(2.0*u[i]-u[i-1]-u[i+1]);
161 r[i] += u[i+1]*(u[i]+u[i+1])/6.0;
164 r[i] -= u[i-1]*(u[i-1]+u[i])/6.0;
167 r[i] -= this->
dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
172 r[0] -= u0*u[ 0]/6.0 + u0*u0/6.0 + nu*u0/this->
dx_;
173 r[this->nx_-1] += u1*u[this->nx_-1]/6.0 + u1*u1/6.0 - nu*u1/this->
dx_;
177 const std::vector<Real> &u,
const std::vector<Real> ¶m) {
178 Real nu = std::pow(10.0,param[0]-2.0);
179 Real u0 = 1.0+param[2]/1000.0;
180 Real u1 = param[3]/1000.0;
183 d.resize(this->
nx_,nu*2.0/this->
dx_);
185 dl.resize(this->
nx_-1,-nu/this->
dx_);
187 du.resize(this->
nx_-1,-nu/this->
dx_);
189 for (
int i=0; i<this->
nx_; i++) {
191 dl[i] += (-2.0*u[i]-u[i+1])/6.0;
196 du[i-1] += (u[i-1]+2.0*u[i])/6.0;
201 d[this->nx_-1] += u1/6.0;
204 void add_pde_hessian(std::vector<Real> &r,
const std::vector<Real> &u,
const std::vector<Real> &p,
205 const std::vector<Real> &s, Real alpha = 1.0) {
206 for (
int i=0; i<this->
nx_; i++) {
209 r[i] += alpha*(p[i]*s[i+1] - p[i+1]*(2.0*s[i]+s[i+1]))/6.0;
212 r[i] += alpha*(p[i-1]*(s[i-1]+2.0*s[i]) - p[i]*s[i-1])/6.0;
217 void linear_solve(std::vector<Real> &u, std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
218 const std::vector<Real> &r,
const bool transpose =
false) {
219 u.assign(r.begin(),r.end());
221 Teuchos::LAPACK<int,Real> lp;
222 std::vector<Real> du2(this->
nx_-2,0.0);
223 std::vector<int> ipiv(this->
nx_,0);
227 lp.GTTRF(this->
nx_,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info);
232 lp.GTTRS(trans,this->
nx_,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info);
235 void run_newton(std::vector<Real> &u,
const std::vector<Real> &z,
const std::vector<Real> ¶m) {
237 std::vector<Real> r(u.size(),0.0);
241 Real tol = 1.e2*ROL::ROL_EPSILON<Real>();
244 std::vector<Real> d(this->
nx_,0.0);
245 std::vector<Real> dl(this->
nx_-1,0.0);
246 std::vector<Real> du(this->
nx_-1,0.0);
248 Real alpha = 1.0, tmp = 0.0;
249 std::vector<Real> s(this->
nx_,0.0);
250 std::vector<Real> utmp(this->
nx_,0.0);
251 for (
int i=0; i<maxit; i++) {
260 utmp.assign(u.begin(),u.end());
261 this->
update(utmp,s,-alpha);
264 while ( rnorm > (1.0-1.e-4*alpha)*tmp && alpha > std::sqrt(ROL::ROL_EPSILON<Real>()) ) {
266 utmp.assign(u.begin(),u.end());
267 this->
update(utmp,s,-alpha);
272 u.assign(utmp.begin(),utmp.end());
285 dx_ = 1.0/((Real)nx+1.0);
288 void solve_state(std::vector<Real> &u,
const std::vector<Real> &z,
const std::vector<Real> ¶m) {
290 u.resize(this->
nx_,1.0);
294 void solve_adjoint(std::vector<Real> &p,
const std::vector<Real> &u,
const std::vector<Real> ¶m) {
299 std::vector<Real> d(this->
nx_,0.0);
300 std::vector<Real> du(this->
nx_-1,0.0);
301 std::vector<Real> dl(this->
nx_-1,0.0);
304 std::vector<Real> r(this->
nx_,0.0);
305 std::vector<Real> diff(this->
nx_,0.0);
306 for (
int i=0; i<this->
nx_; i++) {
315 const std::vector<Real> &z,
const std::vector<Real> ¶m) {
320 std::vector<Real> d(this->
nx_,0.0);
321 std::vector<Real> dl(this->
nx_-1,0.0);
322 std::vector<Real> du(this->
nx_-1,0.0);
325 std::vector<Real> r(this->
nx_,0.0);
326 for (
int i=0; i<this->
nx_; i++) {
327 r[i] = this->
dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
334 const std::vector<Real> &p,
const std::vector<Real> &v,
335 const std::vector<Real> &z,
const std::vector<Real> ¶m) {
340 std::vector<Real> d(this->
nx_,0.0);
341 std::vector<Real> dl(this->
nx_-1,0.0);
342 std::vector<Real> du(this->
nx_-1,0.0);
345 std::vector<Real> r(this->
nx_,0.0);
354 ROL::Ptr<const std::vector<Real> > zp =
357 std::vector<Real> param(4,0.0);
361 Real val = this->
alpha_*0.5*this->
dot(*zp,*zp);
362 Real res = 0.0, res1 = 0.0, res2 = 0.0, res3 = 0.0;
363 for (
int i=0; i<this->
nx_; i++) {
367 res = this->
dx_/6.0*(4.0*res1 + res2)*res1;
369 else if ( i == this->nx_-1 ) {
372 res = this->dx_/6.0*(res1 + 4.0*res2)*res2;
378 res = this->dx_/6.0*(res1 + 4.0*res2 + res3)*res2;
386 ROL::Ptr<const std::vector<Real> > zp =
388 ROL::Ptr<std::vector<Real> > gp =
391 std::vector<Real> param(4,0.0);
398 for (
int i=0; i<this->
nx_+2; i++) {
400 (*gp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*zp)[i]+(*zp)[i+1]);
401 (*gp)[i] -= this->
dx_/6.0*(p[i]);
403 else if (i==this->nx_+1) {
404 (*gp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*zp)[i]+(*zp)[i-1]);
405 (*gp)[i] -= this->
dx_/6.0*(p[i-2]);
408 (*gp)[i] = this->
alpha_*this->
dx_/6.0*((*zp)[i-1]+4.0*(*zp)[i]+(*zp)[i+1]);
410 (*gp)[i] -= this->
dx_/6.0*(4.0*p[i-1]+p[i]);
412 else if (i==this->nx_) {
413 (*gp)[i] -= this->
dx_/6.0*(4.0*p[i-1]+p[i-2]);
416 (*gp)[i] -= this->
dx_/6.0*(p[i-2]+4.0*p[i-1]+p[i]);
423 ROL::Ptr<const std::vector<Real> > zp =
425 ROL::Ptr<const std::vector<Real> > vp =
427 ROL::Ptr<std::vector<Real> > hvp =
430 std::vector<Real> param(4,0.0);
443 for (
int i=0; i<this->
nx_+2; i++) {
445 (*hvp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*vp)[i]+(*vp)[i+1]);
446 (*hvp)[i] -= this->
dx_/6.0*(q[i]);
448 else if (i==this->nx_+1) {
449 (*hvp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*vp)[i]+(*vp)[i-1]);
450 (*hvp)[i] -= this->
dx_/6.0*(q[i-2]);
453 (*hvp)[i] = this->
alpha_*this->
dx_/6.0*((*vp)[i-1]+4.0*(*vp)[i]+(*vp)[i+1]);
455 (*hvp)[i] -= this->
dx_/6.0*(4.0*q[i-1]+q[i]);
457 else if (i==this->nx_) {
458 (*hvp)[i] -= this->
dx_/6.0*(4.0*q[i-1]+q[i-2]);
461 (*hvp)[i] -= this->
dx_/6.0*(q[i-2]+4.0*q[i-1]+q[i]);
481 ROL::Ptr<const std::vector<Real> > ex =
485 for (
int i = 0; i < this->
dim_; i++ ) {
486 if ( (*ex)[i] >= this->
x_lo_[i] && (*ex)[i] <= this->
x_up_[i] ) { cnt *= 1; }
489 if ( cnt == 0 ) { val =
false; }
493 ROL::Ptr<std::vector<Real> > ex =
495 for (
int i = 0; i < this->
dim_; i++ ) {
496 (*ex)[i] = std::max(this->
x_lo_[i],std::min(this->
x_up_[i],(*ex)[i]));
500 ROL::Ptr<const std::vector<Real> > ex =
502 ROL::Ptr<std::vector<Real> > ev =
504 Real epsn = std::min(eps,this->
min_diff_);
505 for (
int i = 0; i < this->
dim_; i++ ) {
506 if ( ((*ex)[i] <= this->
x_lo_[i]+epsn) ) {
512 ROL::Ptr<const std::vector<Real> > ex =
514 ROL::Ptr<std::vector<Real> > ev =
516 Real epsn = std::min(eps,this->
min_diff_);
517 for (
int i = 0; i < this->
dim_; i++ ) {
518 if ( ((*ex)[i] >= this->
x_up_[i]-epsn) ) {
524 ROL::Ptr<const std::vector<Real> > ex =
526 ROL::Ptr<const std::vector<Real> > eg =
528 ROL::Ptr<std::vector<Real> > ev =
530 Real epsn = std::min(eps,this->
min_diff_);
531 for (
int i = 0; i < this->
dim_; i++ ) {
532 if ( ((*ex)[i] <= this->
x_lo_[i]+epsn && (*eg)[i] > 0.0) ){
538 ROL::Ptr<const std::vector<Real> > ex =
540 ROL::Ptr<const std::vector<Real> > eg =
542 ROL::Ptr<std::vector<Real> > ev =
544 Real epsn = std::min(eps,this->
min_diff_);
545 for (
int i = 0; i < this->
dim_; i++ ) {
546 if ( ((*ex)[i] >= this->
x_up_[i]-epsn && (*eg)[i] < 0.0) ) {
552 ROL::Ptr<std::vector<Real> > us = ROL::makePtr<std::vector<Real>>(this->
dim_,0.0);
553 us->assign(this->
x_up_.begin(),this->
x_up_.end());
554 ROL::Ptr<ROL::Vector<Real> > up = ROL::makePtr<ROL::StdVector<Real>>(us);
558 ROL::Ptr<std::vector<Real> > ls = ROL::makePtr<std::vector<Real>>(this->
dim_,0.0);
559 ls->assign(this->
x_lo_.begin(),this->
x_lo_.end());
560 ROL::Ptr<ROL::Vector<Real> > lp = ROL::makePtr<ROL::StdVector<Real>>(ls);
Provides the interface to evaluate objective functions.
void pruneUpperActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -active set.
Real evaluate_target(Real x)
void solve_adjoint_sensitivity(std::vector< Real > &q, const std::vector< Real > &u, const std::vector< Real > &p, const std::vector< Real > &v, const std::vector< Real > &z, const std::vector< Real > ¶m)
void solve_state(std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > ¶m)
void run_newton(std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > ¶m)
void pruneUpperActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &g, const ROL::Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the upper -binding set.
Contains definitions of custom data types in ROL.
void solve_state_sensitivity(std::vector< Real > &v, const std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > ¶m)
void compute_residual(std::vector< Real > &r, const std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > ¶m)
Defines the linear algebra or vector space interface.
void pruneLowerActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &g, const ROL::Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the -binding set.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void hessVec(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
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)
std::vector< Real > x_lo_
Real dot(const std::vector< Real > &x, const std::vector< Real > &y)
Objective_BurgersControl(Real alpha=1.e-4, int nx=128)
void pruneLowerActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &x, Real eps)
Set variables to zero if they correspond to the lower -active set.
void compute_pde_jacobian(std::vector< Real > &dl, std::vector< Real > &d, std::vector< Real > &du, const std::vector< Real > &u, const std::vector< Real > ¶m)
void update(std::vector< Real > &u, const std::vector< Real > &s, const Real alpha=1.0)
std::vector< Real > x_up_
void setVectorToLowerBound(ROL::Vector< Real > &l)
Provides the interface to apply upper and lower bound constraints.
void add_pde_hessian(std::vector< Real > &r, const std::vector< Real > &u, const std::vector< Real > &p, const std::vector< Real > &s, Real alpha=1.0)
void solve_adjoint(std::vector< Real > &p, const std::vector< Real > &u, const std::vector< Real > ¶m)
void project(ROL::Vector< Real > &x)
Project optimization variables onto the bounds.
Real value(const ROL::Vector< Real > &z, Real &tol)
Compute value.
void apply_mass(std::vector< Real > &Mu, const std::vector< Real > &u)
virtual void set(const Vector &x)
Set where .
void gradient(ROL::Vector< Real > &g, const ROL::Vector< Real > &z, Real &tol)
Compute gradient.
bool isFeasible(const ROL::Vector< Real > &x)
Check if the vector, v, is feasible.
BoundConstraint_BurgersControl(int dim)
void setVectorToUpperBound(ROL::Vector< Real > &u)
Real compute_norm(const std::vector< Real > &r)
void scale(std::vector< Real > &u, const Real alpha=0.0)