51 #include "Teuchos_oblackholestream.hpp" 52 #include "Teuchos_GlobalMPISession.hpp" 53 #include "Teuchos_XMLParameterListHelpers.hpp" 54 #include "Teuchos_LAPACK.hpp" 77 return std::sqrt(this->
dot(r,r));
80 Real
dot(
const std::vector<Real> &x,
const std::vector<Real> &y) {
82 Real c = (((int)x.size()==this->
nx_) ? 4.0 : 2.0);
83 for (
unsigned i=0; i<x.size(); i++) {
85 ip += this->dx_/6.0*(c*x[i] + x[i+1])*y[i];
87 else if ( i == x.size()-1 ) {
88 ip += this->dx_/6.0*(x[i-1] + c*x[i])*y[i];
91 ip += this->dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
97 void update(std::vector<Real> &u,
const std::vector<Real> &s,
const Real alpha=1.0) {
98 for (
unsigned i=0; i<u.size(); i++) {
103 void scale(std::vector<Real> &u,
const Real alpha=0.0) {
104 for (
unsigned i=0; i<u.size(); i++) {
110 const std::vector<Real> &z) {
112 r.resize(this->nx_,0.0);
113 for (
int i=0; i<this->
nx_; i++) {
116 r[i] = this->nu_/this->dx_*(2.0*u[i]-u[i+1]);
118 else if (i==this->nx_-1) {
119 r[i] = this->nu_/this->dx_*(2.0*u[i]-u[i-1]);
122 r[i] = this->nu_/this->dx_*(2.0*u[i]-u[i-1]-u[i+1]);
126 r[i] += u[i+1]*(u[i]+u[i+1])/6.0;
129 r[i] -= u[i-1]*(u[i-1]+u[i])/6.0;
132 r[i] -= this->dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]);
134 r[i] -= this->dx_*this->
f_;
137 r[0] -= this->u0_*u[ 0]/6.0 + this->u0_*this->u0_/6.0 + this->nu_*this->u0_/this->
dx_;
138 r[this->nx_-1] += this->u1_*u[this->nx_-1]/6.0 + this->u1_*this->u1_/6.0 - this->nu_*this->u1_/this->
dx_;
142 const std::vector<Real> &u) {
145 d.resize(this->nx_,this->nu_*2.0/this->dx_);
147 dl.resize(this->nx_-1,-this->nu_/this->dx_);
149 du.resize(this->nx_-1,-this->nu_/this->dx_);
151 for (
int i=0; i<this->
nx_; i++) {
153 dl[i] += (-2.0*u[i]-u[i+1])/6.0;
158 du[i-1] += (u[i-1]+2.0*u[i])/6.0;
162 d[0] -= this->u0_/6.0;
163 d[this->nx_-1] += this->u1_/6.0;
166 void linear_solve(std::vector<Real> &u, std::vector<Real> &dl, std::vector<Real> &d, std::vector<Real> &du,
167 const std::vector<Real> &r,
const bool transpose =
false) {
168 u.assign(r.begin(),r.end());
170 Teuchos::LAPACK<int,Real> lp;
171 std::vector<Real> du2(this->nx_-2,0.0);
172 std::vector<int> ipiv(this->nx_,0);
176 lp.GTTRF(this->nx_,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info);
181 lp.GTTRS(trans,this->nx_,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info);
187 : nx_(nx), nu_(nu), u0_(u0), u1_(u1), f_(f) {
188 dx_ = 1.0/((Real)nx+1.0);
193 Teuchos::RCP<std::vector<Real> > cp =
194 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(c)).getVector());
195 Teuchos::RCP<const std::vector<Real> > up =
197 Teuchos::RCP<const std::vector<Real> > zp =
203 Teuchos::RCP<std::vector<Real> > up =
204 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(u)).getVector());
205 up->assign(up->size(),z.
norm()/up->size());
206 Teuchos::RCP<const std::vector<Real> > zp =
209 std::vector<Real> r(up->size(),0.0);
216 std::vector<Real> d(this->nx_,0.0);
217 std::vector<Real> dl(this->nx_-1,0.0);
218 std::vector<Real> du(this->nx_-1,0.0);
220 Real alpha = 1.0, tmp = 0.0;
221 std::vector<Real> s(this->nx_,0.0);
222 std::vector<Real> utmp(this->nx_,0.0);
223 for (
int i=0; i<maxit; i++) {
232 utmp.assign(up->begin(),up->end());
233 this->
update(utmp,s,-alpha);
236 while ( rnorm > (1.0-1.e-4*alpha)*tmp && alpha > std::sqrt(
ROL::ROL_EPSILON) ) {
238 utmp.assign(up->begin(),up->end());
239 this->
update(utmp,s,-alpha);
244 up->assign(utmp.begin(),utmp.end());
245 if ( rnorm < rtol ) {
255 Teuchos::RCP<std::vector<Real> > jvp =
256 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(jv)).getVector());
257 Teuchos::RCP<const std::vector<Real> > vp =
259 Teuchos::RCP<const std::vector<Real> > up =
261 Teuchos::RCP<const std::vector<Real> > zp =
264 for (
int i = 0; i < this->
nx_; i++) {
265 (*jvp)[i] = this->nu_/this->dx_*2.0*(*vp)[i];
267 (*jvp)[i] += -this->nu_/this->dx_*(*vp)[i-1]
268 -(*up)[i-1]/6.0*(*vp)[i]
269 -((*up)[i]+2.0*(*up)[i-1])/6.0*(*vp)[i-1];
271 if ( i < this->nx_-1 ) {
272 (*jvp)[i] += -this->nu_/this->dx_*(*vp)[i+1]
273 +(*up)[i+1]/6.0*(*vp)[i]
274 +((*up)[i]+2.0*(*up)[i+1])/6.0*(*vp)[i+1];
277 (*jvp)[0] -= this->u0_/6.0*(*vp)[0];
278 (*jvp)[this->nx_-1] += this->u1_/6.0*(*vp)[this->nx_-1];
283 Teuchos::RCP<std::vector<Real> > jvp =
284 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(jv)).getVector());
285 Teuchos::RCP<const std::vector<Real> > vp =
287 Teuchos::RCP<const std::vector<Real> > up =
289 Teuchos::RCP<const std::vector<Real> > zp =
291 for (
int i=0; i<this->
nx_; i++) {
293 (*jvp)[i] = -this->dx_/6.0*((*vp)[i]+4.0*(*vp)[i+1]+(*vp)[i+2]);
299 Teuchos::RCP<std::vector<Real> > ijvp =
300 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(ijv)).getVector());
301 Teuchos::RCP<const std::vector<Real> > vp =
303 Teuchos::RCP<const std::vector<Real> > up =
305 Teuchos::RCP<const std::vector<Real> > zp =
308 std::vector<Real> d(this->nx_,0.0);
309 std::vector<Real> dl(this->nx_-1,0.0);
310 std::vector<Real> du(this->nx_-1,0.0);
318 Teuchos::RCP<std::vector<Real> > jvp =
319 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(ajv)).getVector());
320 Teuchos::RCP<const std::vector<Real> > vp =
322 Teuchos::RCP<const std::vector<Real> > up =
324 Teuchos::RCP<const std::vector<Real> > zp =
327 for (
int i = 0; i < this->
nx_; i++) {
328 (*jvp)[i] = this->nu_/this->dx_*2.0*(*vp)[i];
330 (*jvp)[i] += -this->nu_/this->dx_*(*vp)[i-1]
331 -(*up)[i-1]/6.0*(*vp)[i]
332 +((*up)[i-1]+2.0*(*up)[i])/6.0*(*vp)[i-1];
334 if ( i < this->nx_-1 ) {
335 (*jvp)[i] += -this->nu_/this->dx_*(*vp)[i+1]
336 +(*up)[i+1]/6.0*(*vp)[i]
337 -((*up)[i+1]+2.0*(*up)[i])/6.0*(*vp)[i+1];
340 (*jvp)[0] -= this->u0_/6.0*(*vp)[0];
341 (*jvp)[this->nx_-1] += this->u1_/6.0*(*vp)[this->nx_-1];
346 Teuchos::RCP<std::vector<Real> > jvp =
347 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(jv)).getVector());
348 Teuchos::RCP<const std::vector<Real> > vp =
350 Teuchos::RCP<const std::vector<Real> > up =
352 Teuchos::RCP<const std::vector<Real> > zp =
354 for (
int i=0; i<this->nx_+2; i++) {
356 (*jvp)[i] = -this->dx_/6.0*(*vp)[i];
359 (*jvp)[i] = -this->dx_/6.0*(4.0*(*vp)[i-1]+(*vp)[i]);
361 else if ( i == this->nx_ ) {
362 (*jvp)[i] = -this->dx_/6.0*(4.0*(*vp)[i-1]+(*vp)[i-2]);
364 else if ( i == this->nx_+1 ) {
365 (*jvp)[i] = -this->dx_/6.0*(*vp)[i-2];
368 (*jvp)[i] = -this->dx_/6.0*((*vp)[i-2]+4.0*(*vp)[i-1]+(*vp)[i]);
375 Teuchos::RCP<std::vector<Real> > iajvp =
376 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(iajv)).getVector());
377 Teuchos::RCP<const std::vector<Real> > vp =
379 Teuchos::RCP<const std::vector<Real> > up =
382 std::vector<Real> d(this->nx_,0.0);
383 std::vector<Real> du(this->nx_-1,0.0);
384 std::vector<Real> dl(this->nx_-1,0.0);
392 Teuchos::RCP<std::vector<Real> > ahwvp =
393 Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<
ROL::StdVector<Real> >(ahwv)).getVector());
394 Teuchos::RCP<const std::vector<Real> > wp =
396 Teuchos::RCP<const std::vector<Real> > vp =
398 Teuchos::RCP<const std::vector<Real> > up =
400 Teuchos::RCP<const std::vector<Real> > zp =
402 for (
int i=0; i<this->
nx_; i++) {
406 (*ahwvp)[i] += ((*wp)[i]*(*vp)[i+1] - (*wp)[i+1]*(2.0*(*vp)[i]+(*vp)[i+1]))/6.0;
409 (*ahwvp)[i] += ((*wp)[i-1]*((*vp)[i-1]+2.0*(*vp)[i]) - (*wp)[i]*(*vp)[i-1])/6.0;
446 case 1: val = ((x<0.5) ? 1.0 : 0.0);
break;
447 case 2: val = 1.0;
break;
448 case 3: val = std::abs(std::sin(8.0*M_PI*x));
break;
449 case 4: val = std::exp(-0.5*(x-0.5)*(x-0.5));
break;
454 Real
dot(
const std::vector<Real> &x,
const std::vector<Real> &y) {
456 Real c = (((int)x.size()==this->
nx_) ? 4.0 : 2.0);
457 for (
unsigned i=0; i<x.size(); i++) {
459 ip += this->dx_/6.0*(c*x[i] + x[i+1])*y[i];
461 else if ( i == x.size()-1 ) {
462 ip += this->dx_/6.0*(x[i-1] + c*x[i])*y[i];
465 ip += this->dx_/6.0*(x[i-1] + 4.0*x[i] + x[i+1])*y[i];
471 void apply_mass(std::vector<Real> &Mu,
const std::vector<Real> &u ) {
472 Mu.resize(u.size(),0.0);
473 Real c = (((int)u.size()==this->
nx_) ? 4.0 : 2.0);
474 for (
unsigned i=0; i<u.size(); i++) {
476 Mu[i] = this->dx_/6.0*(c*u[i] + u[i+1]);
478 else if ( i == u.size()-1 ) {
479 Mu[i] = this->dx_/6.0*(u[i-1] + c*u[i]);
482 Mu[i] = this->dx_/6.0*(u[i-1] + 4.0*u[i] + u[i+1]);
493 dx_ = 1.0/((Real)nx+1.0);
497 Teuchos::RCP<const std::vector<Real> > up =
499 Teuchos::RCP<const std::vector<Real> > zp =
502 Real res1 = 0.0, res2 = 0.0, res3 = 0.0;
503 Real valu = 0.0, valz = this->
dot(*zp,*zp);
504 for (
int i=0; i<this->
nx_; i++) {
506 res1 = (*up)[i]-evaluate_target((Real)(i+1)*this->dx_);
507 res2 = (*up)[i+1]-evaluate_target((Real)(i+2)*this->dx_);
508 valu += this->dx_/6.0*(4.0*res1 + res2)*res1;
510 else if ( i == this->nx_-1 ) {
511 res1 = (*up)[i-1]-evaluate_target((Real)i*this->dx_);
512 res2 = (*up)[i]-evaluate_target((Real)(i+1)*this->dx_);
513 valu += this->dx_/6.0*(res1 + 4.0*res2)*res2;
516 res1 = (*up)[i-1]-evaluate_target((Real)i*this->dx_);
517 res2 = (*up)[i]-evaluate_target((Real)(i+1)*this->dx_);
518 res3 = (*up)[i+1]-evaluate_target((Real)(i+2)*this->dx_);
519 valu += this->dx_/6.0*(res1 + 4.0*res2 + res3)*res2;
522 return 0.5*(valu + this->alpha_*valz);
527 Teuchos::RCP<std::vector<Real> > gup = Teuchos::rcp_const_cast<std::vector<Real> >(
530 Teuchos::RCP<const std::vector<Real> > up =
532 Teuchos::RCP<const std::vector<Real> > zp =
535 std::vector<Real> diff(this->nx_,0.0);
536 for (
int i=0; i<this->
nx_; i++) {
537 diff[i] = ((*up)[i]-this->evaluate_target((Real)(i+1)*this->dx_));
539 this->apply_mass(*gup,diff);
544 Teuchos::RCP<std::vector<Real> > gzp = Teuchos::rcp_const_cast<std::vector<Real> >(
547 Teuchos::RCP<const std::vector<Real> > up =
549 Teuchos::RCP<const std::vector<Real> > zp =
552 for (
int i=0; i<this->nx_+2; i++) {
554 (*gzp)[i] = this->alpha_*this->dx_/6.0*(2.0*(*zp)[i]+(*zp)[i+1]);
556 else if (i==this->nx_+1) {
557 (*gzp)[i] = this->alpha_*this->dx_/6.0*(2.0*(*zp)[i]+(*zp)[i-1]);
560 (*gzp)[i] = this->alpha_*this->dx_/6.0*((*zp)[i-1]+4.0*(*zp)[i]+(*zp)[i+1]);
567 Teuchos::RCP<std::vector<Real> > hvup = Teuchos::rcp_const_cast<std::vector<Real> >(
570 Teuchos::RCP<const std::vector<Real> > vup =
573 this->apply_mass(*hvup,*vup);
588 Teuchos::RCP<std::vector<Real> > hvzp = Teuchos::rcp_const_cast<std::vector<Real> >(
591 Teuchos::RCP<const std::vector<Real> > vzp =
594 for (
int i=0; i<this->nx_+2; i++) {
596 (*hvzp)[i] = this->alpha_*this->dx_/6.0*(2.0*(*vzp)[i]+(*vzp)[i+1]);
598 else if (i==this->nx_+1) {
599 (*hvzp)[i] = this->alpha_*this->dx_/6.0*(2.0*(*vzp)[i]+(*vzp)[i-1]);
602 (*hvzp)[i] = this->alpha_*this->dx_/6.0*((*vzp)[i-1]+4.0*(*vzp)[i]+(*vzp)[i+1]);
Provides the interface to evaluate simulation-based objective functions.
void compute_pde_jacobian(std::vector< Real > &dl, std::vector< Real > &d, std::vector< Real > &du, const std::vector< Real > &u)
void applyAdjointHessian_21(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
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)
Real evaluate_target(Real x)
void applyAdjointHessian_22(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
void applyInverseJacobian_1(ROL::Vector< Real > &ijv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the inverse partial constraint Jacobian at , , to the vector .
Real dot(const std::vector< Real > &x, const std::vector< Real > &y)
Contains definitions of custom data types in ROL.
EqualityConstraint_BurgersControl(int nx=128, Real nu=1.e-2, Real u0=1.0, Real u1=0.0, Real f=0.0)
Real compute_norm(const std::vector< Real > &r)
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
void compute_residual(std::vector< Real > &r, const std::vector< Real > &u, const std::vector< Real > &z)
void applyAdjointHessian_11(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
Defines the equality constraint operator interface for simulation-based optimization.
void hessVec_22(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Real value(const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute value.
void gradient_1(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
void applyInverseAdjointJacobian_1(ROL::Vector< Real > &iajv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the inverse of the adjoint of the partial constraint Jacobian at , , to the vector ...
void applyAdjointJacobian_1(ROL::Vector< Real > &ajv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Jacobian at , , to the vector . This is the primary inter...
void applyAdjointJacobian_2(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Jacobian at , , to vector . This is the primary interface...
Provides the std::vector implementation of the ROL::Vector interface.
void applyJacobian_1(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the partial constraint Jacobian at , , to the vector .
Real dot(const std::vector< Real > &x, const std::vector< Real > &y)
Objective_BurgersControl(Real alpha=1.e-4, int nx=128)
void hessVec_21(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void hessVec_12(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void gradient_2(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
void solve(ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Given , solve for .
void scale(std::vector< Real > &u, const Real alpha=0.0)
void applyJacobian_2(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the partial constraint Jacobian at , , to the vector .
void hessVec_11(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
void value(ROL::Vector< Real > &c, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Evaluate the constraint operator at .
void apply_mass(std::vector< Real > &Mu, const std::vector< Real > &u)
virtual Real norm() const =0
Returns where .
void update(std::vector< Real > &u, const std::vector< Real > &s, const Real alpha=1.0)
void applyAdjointHessian_12(ROL::Vector< Real > &ahwv, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the adjoint of the partial constraint Hessian at , , to vector in direction ...
static const double ROL_EPSILON
Platform-dependent machine epsilon.