34 case 1: val = ((x<0.5) ? 1.0 : 0.0);
break;
35 case 2: val = 1.0;
break;
36 case 3: val = std::abs(std::sin(8.0*M_PI*x));
break;
37 case 4: val = std::exp(-0.5*(x-0.5)*(x-0.5));
break;
43 return std::sqrt(this->
dot(r,r));
46 Real
dot(
const std::vector<Real> &x,
const std::vector<Real> &y) {
48 Real c = (((int)x.size()==this->
nx_) ? 4.0 : 2.0);
49 for (
unsigned i=0; i<x.size(); i++) {
51 ip += this->
dx_/6.0*(c*x[i] + x[i+1])*y[i];
53 else if ( i == x.size()-1 ) {
54 ip += this->
dx_/6.0*(x[i-1] + c*x[i])*y[i];
57 ip += this->
dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
65 void update(std::vector<Real> &u,
const std::vector<Real> &s,
const Real alpha=1.0) {
66 for (
unsigned i=0; i<u.size(); i++) {
71 void scale(std::vector<Real> &u,
const Real alpha=0.0) {
72 for (
unsigned i=0; i<u.size(); i++) {
77 void apply_mass(std::vector<Real> &Mu,
const std::vector<Real> &u ) {
78 Mu.resize(u.size(),0.0);
79 Real c = (((int)u.size()==this->
nx_) ? 4.0 : 2.0);
80 for (
unsigned i=0; i<u.size(); i++) {
82 Mu[i] = this->
dx_/6.0*(c*u[i] + u[i+1]);
84 else if ( i == u.size()-1 ) {
85 Mu[i] = this->
dx_/6.0*(u[i-1] + c*u[i]);
88 Mu[i] = this->
dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]);
94 const std::vector<Real> &z,
const std::vector<Real> ¶m) {
96 r.resize(this->
nx_,0.0);
97 Real nu = std::pow(10.0,param[0]-2.0);
98 Real f = param[1]/100.0;
99 Real u0 = 1.0+param[2]/1000.0;
100 Real u1 = param[3]/1000.0;
101 for (
int i=0; i<this->
nx_; i++) {
104 r[i] = nu/this->
dx_*(2.0*u[i]-u[i+1]);
106 else if (i==this->nx_-1) {
107 r[i] = nu/this->
dx_*(2.0*u[i]-u[i-1]);
110 r[i] = nu/this->
dx_*(2.0*u[i]-u[i-1]-u[i+1]);
114 r[i] += u[i+1]*(u[i]+u[i+1])/6.0;
117 r[i] -= u[i-1]*(u[i-1]+u[i])/6.0;
120 r[i] -= this->
dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
125 r[0] -= u0*u[ 0]/6.0 + u0*u0/6.0 + nu*u0/this->
dx_;
126 r[this->nx_-1] += u1*u[this->nx_-1]/6.0 + u1*u1/6.0 - nu*u1/this->
dx_;
130 const std::vector<Real> &u,
const std::vector<Real> ¶m) {
131 Real nu = std::pow(10.0,param[0]-2.0);
132 Real u0 = 1.0+param[2]/1000.0;
133 Real u1 = param[3]/1000.0;
136 d.resize(this->
nx_,nu*2.0/this->
dx_);
138 dl.resize(this->
nx_-1,-nu/this->
dx_);
140 du.resize(this->
nx_-1,-nu/this->
dx_);
142 for (
int i=0; i<this->
nx_; i++) {
144 dl[i] += (-2.0*u[i]-u[i+1])/6.0;
149 du[i-1] += (u[i-1]+2.0*u[i])/6.0;
154 d[this->nx_-1] += u1/6.0;
157 void add_pde_hessian(std::vector<Real> &r,
const std::vector<Real> &u,
const std::vector<Real> &p,
158 const std::vector<Real> &s, Real alpha = 1.0) {
159 for (
int i=0; i<this->
nx_; i++) {
162 r[i] += alpha*(p[i]*s[i+1] - p[i+1]*(2.0*s[i]+s[i+1]))/6.0;
165 r[i] += alpha*(p[i-1]*(s[i-1]+2.0*s[i]) - p[i]*s[i-1])/6.0;
170 void linear_solve(std::vector<Real> &u, std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
171 const std::vector<Real> &r,
const bool transpose =
false) {
172 u.assign(r.begin(),r.end());
174 Teuchos::LAPACK<int,Real> lp;
175 std::vector<Real> du2(this->
nx_-2,0.0);
176 std::vector<int> ipiv(this->
nx_,0);
180 lp.GTTRF(this->
nx_,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info);
185 lp.GTTRS(trans,this->
nx_,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info);
188 void run_newton(std::vector<Real> &u,
const std::vector<Real> &z,
const std::vector<Real> ¶m) {
190 std::vector<Real> r(u.size(),0.0);
194 Real tol = 1.e2*ROL::ROL_EPSILON<Real>();
197 std::vector<Real> d(this->
nx_,0.0);
198 std::vector<Real> dl(this->
nx_-1,0.0);
199 std::vector<Real> du(this->
nx_-1,0.0);
201 Real alpha = 1.0, tmp = 0.0;
202 std::vector<Real> s(this->
nx_,0.0);
203 std::vector<Real> utmp(this->
nx_,0.0);
204 for (
int i=0; i<maxit; i++) {
213 utmp.assign(u.begin(),u.end());
214 this->
update(utmp,s,-alpha);
217 while ( rnorm > (1.0-1.e-4*alpha)*tmp && alpha > std::sqrt(ROL::ROL_EPSILON<Real>()) ) {
219 utmp.assign(u.begin(),u.end());
220 this->
update(utmp,s,-alpha);
225 u.assign(utmp.begin(),utmp.end());
238 dx_ = 1.0/((Real)nx+1.0);
241 void solve_state(std::vector<Real> &u,
const std::vector<Real> &z,
const std::vector<Real> ¶m) {
243 u.resize(this->
nx_,1.0);
247 void solve_adjoint(std::vector<Real> &p,
const std::vector<Real> &u,
const std::vector<Real> ¶m) {
252 std::vector<Real> d(this->
nx_,0.0);
253 std::vector<Real> du(this->
nx_-1,0.0);
254 std::vector<Real> dl(this->
nx_-1,0.0);
257 std::vector<Real> r(this->
nx_,0.0);
258 std::vector<Real> diff(this->
nx_,0.0);
259 for (
int i=0; i<this->
nx_; i++) {
268 const std::vector<Real> &z,
const std::vector<Real> ¶m) {
273 std::vector<Real> d(this->
nx_,0.0);
274 std::vector<Real> dl(this->
nx_-1,0.0);
275 std::vector<Real> du(this->
nx_-1,0.0);
278 std::vector<Real> r(this->
nx_,0.0);
279 for (
int i=0; i<this->
nx_; i++) {
280 r[i] = this->
dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
287 const std::vector<Real> &p,
const std::vector<Real> &v,
288 const std::vector<Real> &z,
const std::vector<Real> ¶m) {
293 std::vector<Real> d(this->
nx_,0.0);
294 std::vector<Real> dl(this->
nx_-1,0.0);
295 std::vector<Real> du(this->
nx_-1,0.0);
298 std::vector<Real> r(this->
nx_,0.0);
307 ROL::Ptr<const std::vector<Real> > zp =
310 std::vector<Real> param(4,0.0);
314 Real val = this->
alpha_*0.5*this->
dot(*zp,*zp);
315 Real res = 0.0, res1 = 0.0, res2 = 0.0, res3 = 0.0;
316 for (
int i=0; i<this->
nx_; i++) {
320 res = this->
dx_/6.0*(4.0*res1 + res2)*res1;
322 else if ( i == this->nx_-1 ) {
325 res = this->dx_/6.0*(res1 + 4.0*res2)*res2;
331 res = this->dx_/6.0*(res1 + 4.0*res2 + res3)*res2;
339 ROL::Ptr<const std::vector<Real> > zp =
341 ROL::Ptr<std::vector<Real> > gp =
344 std::vector<Real> param(4,0.0);
351 for (
int i=0; i<this->
nx_+2; i++) {
353 (*gp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*zp)[i]+(*zp)[i+1]);
354 (*gp)[i] -= this->
dx_/6.0*(p[i]);
356 else if (i==this->nx_+1) {
357 (*gp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*zp)[i]+(*zp)[i-1]);
358 (*gp)[i] -= this->
dx_/6.0*(p[i-2]);
361 (*gp)[i] = this->
alpha_*this->
dx_/6.0*((*zp)[i-1]+4.0*(*zp)[i]+(*zp)[i+1]);
363 (*gp)[i] -= this->
dx_/6.0*(4.0*p[i-1]+p[i]);
365 else if (i==this->nx_) {
366 (*gp)[i] -= this->
dx_/6.0*(4.0*p[i-1]+p[i-2]);
369 (*gp)[i] -= this->
dx_/6.0*(p[i-2]+4.0*p[i-1]+p[i]);
376 ROL::Ptr<const std::vector<Real> > zp =
378 ROL::Ptr<const std::vector<Real> > vp =
380 ROL::Ptr<std::vector<Real> > hvp =
383 std::vector<Real> param(4,0.0);
396 for (
int i=0; i<this->
nx_+2; i++) {
398 (*hvp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*vp)[i]+(*vp)[i+1]);
399 (*hvp)[i] -= this->
dx_/6.0*(q[i]);
401 else if (i==this->nx_+1) {
402 (*hvp)[i] = this->
alpha_*this->
dx_/6.0*(2.0*(*vp)[i]+(*vp)[i-1]);
403 (*hvp)[i] -= this->
dx_/6.0*(q[i-2]);
406 (*hvp)[i] = this->
alpha_*this->
dx_/6.0*((*vp)[i-1]+4.0*(*vp)[i]+(*vp)[i+1]);
408 (*hvp)[i] -= this->
dx_/6.0*(4.0*q[i-1]+q[i]);
410 else if (i==this->nx_) {
411 (*hvp)[i] -= this->
dx_/6.0*(4.0*q[i-1]+q[i-2]);
414 (*hvp)[i] -= this->
dx_/6.0*(q[i-2]+4.0*q[i-1]+q[i]);
434 ROL::Ptr<const std::vector<Real> > ex =
438 for (
int i = 0; i < this->
dim_; i++ ) {
439 if ( (*ex)[i] >= this->
x_lo_[i] && (*ex)[i] <= this->
x_up_[i] ) { cnt *= 1; }
442 if ( cnt == 0 ) { val =
false; }
446 ROL::Ptr<std::vector<Real> > ex =
448 for (
int i = 0; i < this->
dim_; i++ ) {
449 (*ex)[i] = std::max(this->
x_lo_[i],std::min(this->
x_up_[i],(*ex)[i]));
453 ROL::Ptr<const std::vector<Real> > ex =
455 ROL::Ptr<std::vector<Real> > ev =
457 Real epsn = std::min(eps,this->
min_diff_);
458 for (
int i = 0; i < this->
dim_; i++ ) {
459 if ( ((*ex)[i] <= this->
x_lo_[i]+epsn) ) {
465 ROL::Ptr<const std::vector<Real> > ex =
467 ROL::Ptr<std::vector<Real> > ev =
469 Real epsn = std::min(eps,this->
min_diff_);
470 for (
int i = 0; i < this->
dim_; i++ ) {
471 if ( ((*ex)[i] >= this->
x_up_[i]-epsn) ) {
477 ROL::Ptr<const std::vector<Real> > ex =
479 ROL::Ptr<const std::vector<Real> > eg =
481 ROL::Ptr<std::vector<Real> > ev =
483 Real epsn = std::min(xeps,this->
min_diff_);
484 for (
int i = 0; i < this->
dim_; i++ ) {
485 if ( ((*ex)[i] <= this->
x_lo_[i]+epsn && (*eg)[i] > geps) ){
491 ROL::Ptr<const std::vector<Real> > ex =
493 ROL::Ptr<const std::vector<Real> > eg =
495 ROL::Ptr<std::vector<Real> > ev =
497 Real epsn = std::min(xeps,this->
min_diff_);
498 for (
int i = 0; i < this->
dim_; i++ ) {
499 if ( ((*ex)[i] >= this->
x_up_[i]-epsn && (*eg)[i] < -geps) ) {
505 ROL::Ptr<std::vector<Real> > us = ROL::makePtr<std::vector<Real>>(this->
dim_,0.0);
506 us->assign(this->
x_up_.begin(),this->
x_up_.end());
507 ROL::Ptr<ROL::Vector<Real> > up = ROL::makePtr<ROL::StdVector<Real>>(us);
511 ROL::Ptr<std::vector<Real> > ls = ROL::makePtr<std::vector<Real>>(this->
dim_,0.0);
512 ls->assign(this->
x_lo_.begin(),this->
x_lo_.end());
513 ROL::Ptr<ROL::Vector<Real> > lp = ROL::makePtr<ROL::StdVector<Real>>(ls);
Provides the interface to evaluate objective functions.
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 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 hessVec(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
void pruneUpperActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the upper -active set.
void pruneUpperActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &g, const ROL::Vector< Real > &x, Real xeps=Real(0), Real geps=Real(0))
Set variables to zero if they correspond to the upper -binding set.
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 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 pruneLowerActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &g, const ROL::Vector< Real > &x, Real xeps=Real(0), Real geps=Real(0))
Set variables to zero if they correspond to the -binding set.
void apply_mass(std::vector< Real > &Mu, const std::vector< Real > &u)
virtual void set(const Vector &x)
Set where .
void pruneLowerActive(ROL::Vector< Real > &v, const ROL::Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the lower -active set.
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)