45 #ifndef ROL_CONSTRAINT_DEF_H
46 #define ROL_CONSTRAINT_DEF_H
48 #include "ROL_LinearAlgebra.hpp"
49 #include "ROL_LAPACK.hpp"
60 Real ctol = std::sqrt(ROL_EPSILON<Real>());
63 Real h = std::max(one,x.
norm()/v.
norm())*tol;
67 ROL::Ptr<Vector<Real> > c = jv.
clone();
68 this->
value(*c,x,ctol);
71 ROL::Ptr<Vector<Real> > xnew = x.
clone();
78 this->
value(jv,*xnew,ctol);
91 applyAdjointJacobian(ajv,v,x,v.
dual(),tol);
108 Real h(0), ctol = std::sqrt(ROL_EPSILON<Real>());
110 ROL::Ptr<Vector<Real> > xnew = x.
clone();
111 ROL::Ptr<Vector<Real> > ex = x.
clone();
112 ROL::Ptr<Vector<Real> > eajv = ajv.
clone();
113 ROL::Ptr<Vector<Real> > cnew = dualv.
clone();
114 ROL::Ptr<Vector<Real> > c0 = dualv.
clone();
115 this->
value(*c0,x,ctol);
118 for (
int i = 0; i < ajv.
dimension(); i++ ) {
121 h = std::max(one,x.
norm()/ex->norm())*tol;
125 this->
value(*cnew,*xnew,ctol);
126 cnew->axpy(-one,*c0);
129 ajv.
axpy(cnew->apply(v),*eajv);
166 template <
class Real>
173 Real h = std::max(static_cast<Real>(1),x.
norm()/v.
norm())*tol;
176 ROL::Ptr<Vector<Real> > aju = huv.
clone();
177 applyAdjointJacobian(*aju,u,x,tol);
180 ROL::Ptr<Vector<Real> > xnew = x.
clone();
187 applyAdjointJacobian(huv,u,*xnew,tol);
190 huv.
axpy(static_cast<Real>(-1),*aju);
191 huv.
scale(static_cast<Real>(1)/h);
195 template <
class Real>
204 const Real
zero(0), one(1);
213 tol = std::sqrt(b1.
dot(b1)+b2.
dot(b2))*tol;
219 ROL::Ptr<Vector<Real> > r1 = b1.
clone();
220 ROL::Ptr<Vector<Real> > r2 = b2.
clone();
221 ROL::Ptr<Vector<Real> > z1 = v1.
clone();
222 ROL::Ptr<Vector<Real> > z2 = v2.
clone();
223 ROL::Ptr<Vector<Real> > w1 = b1.
clone();
224 ROL::Ptr<Vector<Real> > w2 = b2.
clone();
225 std::vector<ROL::Ptr<Vector<Real> > > V1;
226 std::vector<ROL::Ptr<Vector<Real> > > V2;
227 ROL::Ptr<Vector<Real> > V2temp = b2.
clone();
228 std::vector<ROL::Ptr<Vector<Real> > > Z1;
229 std::vector<ROL::Ptr<Vector<Real> > > Z2;
230 ROL::Ptr<Vector<Real> > w1temp = b1.
clone();
231 ROL::Ptr<Vector<Real> > Z2temp = v2.
clone();
233 std::vector<Real> res(m+1,
zero);
234 LA::Matrix<Real> H(m+1,m);
235 LA::Vector<Real> cs(m);
236 LA::Vector<Real> sn(m);
237 LA::Vector<Real> s(m+1);
238 LA::Vector<Real> y(m+1);
239 LA::Vector<Real> cnorm(m);
240 ROL::LAPACK<int, Real> lapack;
243 applyAdjointJacobian(*r1, v2, x, zerotol);
244 r1->scale(-one); r1->axpy(-one, v1.
dual()); r1->plus(b1);
246 r2->scale(-one); r2->plus(b2);
247 res[0] = std::sqrt(r1->dot(*r1) + r2->dot(*r2));
250 if (res[0] ==
zero) {
255 V1.push_back(b1.
clone()); (V1[0])->set(*r1); (V1[0])->scale(one/res[0]);
256 V2.push_back(b2.
clone()); (V2[0])->set(*r2); (V2[0])->scale(one/res[0]);
260 for (i=0; i<m; i++) {
263 V2temp->set(*(V2[i]));
264 applyPreconditioner(*Z2temp, *V2temp, x, b1, zerotol);
265 Z2.push_back(v2.
clone()); (Z2[i])->set(*Z2temp);
266 Z1.push_back(v1.
clone()); (Z1[i])->set((V1[i])->dual());
270 applyAdjointJacobian(*w1temp, *Z2temp, x, zerotol);
271 w1->set(*(V1[i])); w1->plus(*w1temp);
274 for (k=0; k<=i; k++) {
275 H(k,i) = w1->dot(*(V1[k])) + w2->dot(*(V2[k]));
276 w1->axpy(-H(k,i), *(V1[k]));
277 w2->axpy(-H(k,i), *(V2[k]));
279 H(i+1,i) = std::sqrt(w1->dot(*w1) + w2->dot(*w2));
281 V1.push_back(b1.
clone()); (V1[i+1])->set(*w1); (V1[i+1])->scale(one/H(i+1,i));
282 V2.push_back(b2.
clone()); (V2[i+1])->set(*w2); (V2[i+1])->scale(one/H(i+1,i));
285 for (k=0; k<=i-1; k++) {
286 temp = cs(k)*H(k,i) + sn(k)*H(k+1,i);
287 H(k+1,i) = -sn(k)*H(k,i) + cs(k)*H(k+1,i);
292 if ( H(i+1,i) ==
zero ) {
296 else if ( std::abs(H(i+1,i)) > std::abs(H(i,i)) ) {
297 temp = H(i,i) / H(i+1,i);
298 sn(i) = one / std::sqrt( one + temp*temp );
299 cs(i) = temp * sn(i);
302 temp = H(i+1,i) / H(i,i);
303 cs(i) = one / std::sqrt( one + temp*temp );
304 sn(i) = temp * cs(i);
309 s(i+1) = -sn(i)*s(i);
311 H(i,i) = cs(i)*H(i,i) + sn(i)*H(i+1,i);
313 resnrm = std::abs(s(i+1));
317 const char uplo =
'U';
318 const char trans =
'N';
319 const char diag =
'N';
320 const char normin =
'N';
324 lapack.LATRS(uplo, trans, diag, normin, i+1, H.values(), m+1, y.values(), &scaling, cnorm.values(), &info);
327 for (k=0; k<=i; k++) {
328 z1->axpy(y(k), *(Z1[k]));
329 z2->axpy(y(k), *(Z2[k]));
336 if (res[i+1] <= tol) {
364 template <
class Real>
368 const bool printToStream,
369 std::ostream & outStream,
372 std::vector<Real> steps(numSteps);
373 for(
int i=0;i<numSteps;++i) {
374 steps[i] = pow(10,-i);
377 return checkApplyJacobian(x,v,jv,steps,printToStream,outStream,order);
383 template <
class Real>
387 const std::vector<Real> &steps,
388 const bool printToStream,
389 std::ostream & outStream,
391 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
392 "Error: finite difference order must be 1,2,3, or 4" );
399 Real tol = std::sqrt(ROL_EPSILON<Real>());
401 int numSteps = steps.size();
403 std::vector<Real> tmp(numVals);
404 std::vector<std::vector<Real> > jvCheck(numSteps, tmp);
408 oldFormatState.copyfmt(outStream);
411 ROL::Ptr<Vector<Real> > c = jv.
clone();
413 this->
value(*c, x, tol);
416 ROL::Ptr<Vector<Real> > Jv = jv.
clone();
418 Real normJv = Jv->norm();
421 ROL::Ptr<Vector<Real> > cdif = jv.
clone();
422 ROL::Ptr<Vector<Real> > cnew = jv.
clone();
423 ROL::Ptr<Vector<Real> > xnew = x.
clone();
425 for (
int i=0; i<numSteps; i++) {
432 cdif->scale(
weights[order-1][0]);
434 for(
int j=0; j<order; ++j) {
436 xnew->axpy(eta*
shifts[order-1][j], v);
438 if(
weights[order-1][j+1] != 0 ) {
440 this->
value(*cnew,*xnew,tol);
441 cdif->axpy(
weights[order-1][j+1],*cnew);
446 cdif->scale(one/eta);
450 jvCheck[i][1] = normJv;
451 jvCheck[i][2] = cdif->norm();
452 cdif->axpy(-one, *Jv);
453 jvCheck[i][3] = cdif->norm();
456 std::stringstream hist;
459 << std::setw(20) <<
"Step size"
460 << std::setw(20) <<
"norm(Jac*vec)"
461 << std::setw(20) <<
"norm(FD approx)"
462 << std::setw(20) <<
"norm(abs error)"
464 << std::setw(20) <<
"---------"
465 << std::setw(20) <<
"-------------"
466 << std::setw(20) <<
"---------------"
467 << std::setw(20) <<
"---------------"
470 hist << std::scientific << std::setprecision(11) << std::right
471 << std::setw(20) << jvCheck[i][0]
472 << std::setw(20) << jvCheck[i][1]
473 << std::setw(20) << jvCheck[i][2]
474 << std::setw(20) << jvCheck[i][3]
476 outStream << hist.str();
482 outStream.copyfmt(oldFormatState);
488 template <
class Real>
493 const bool printToStream,
494 std::ostream & outStream,
495 const int numSteps) {
496 Real tol = std::sqrt(ROL_EPSILON<Real>());
500 std::vector<Real> tmp(numVals);
501 std::vector<std::vector<Real> > ajvCheck(numSteps, tmp);
502 Real eta_factor = 1e-1;
506 ROL::Ptr<Vector<Real> > c0 = c.
clone();
507 ROL::Ptr<Vector<Real> > cnew = c.
clone();
508 ROL::Ptr<Vector<Real> > xnew = x.
clone();
509 ROL::Ptr<Vector<Real> > ajv0 = ajv.
clone();
510 ROL::Ptr<Vector<Real> > ajv1 = ajv.
clone();
511 ROL::Ptr<Vector<Real> > ex = x.
clone();
512 ROL::Ptr<Vector<Real> > eajv = ajv.
clone();
516 oldFormatState.copyfmt(outStream);
520 this->
value(*c0, x, tol);
523 this->applyAdjointJacobian(*ajv0, v, x, tol);
524 Real normAJv = ajv0->norm();
526 for (
int i=0; i<numSteps; i++) {
530 for (
int j = 0; j < ajv.
dimension(); j++ ) {
536 this->
value(*cnew,*xnew,tol);
537 cnew->axpy(-one,*c0);
538 cnew->scale(one/eta);
540 ajv1->axpy(cnew->apply(v),*eajv);
544 ajvCheck[i][0] = eta;
545 ajvCheck[i][1] = normAJv;
546 ajvCheck[i][2] = ajv1->norm();
547 ajv1->axpy(-one, *ajv0);
548 ajvCheck[i][3] = ajv1->norm();
551 std::stringstream hist;
554 << std::setw(20) <<
"Step size"
555 << std::setw(20) <<
"norm(adj(Jac)*vec)"
556 << std::setw(20) <<
"norm(FD approx)"
557 << std::setw(20) <<
"norm(abs error)"
559 << std::setw(20) <<
"---------"
560 << std::setw(20) <<
"------------------"
561 << std::setw(20) <<
"---------------"
562 << std::setw(20) <<
"---------------"
565 hist << std::scientific << std::setprecision(11) << std::right
566 << std::setw(20) << ajvCheck[i][0]
567 << std::setw(20) << ajvCheck[i][1]
568 << std::setw(20) << ajvCheck[i][2]
569 << std::setw(20) << ajvCheck[i][3]
571 outStream << hist.str();
575 eta = eta*eta_factor;
579 outStream.copyfmt(oldFormatState);
584 template <
class Real>
590 const bool printToStream,
591 std::ostream & outStream) {
592 Real tol = ROL_EPSILON<Real>();
594 ROL::Ptr<Vector<Real> > Jv = dualw.
clone();
595 ROL::Ptr<Vector<Real> > Jw = dualv.
clone();
599 applyAdjointJacobian(*Jw,w,x,tol);
602 Real vJw = v.
apply(*Jw);
604 Real wJv = w.
apply(*Jv);
606 Real diff = std::abs(wJv-vJw);
608 if ( printToStream ) {
609 std::stringstream hist;
610 hist << std::scientific << std::setprecision(8);
611 hist <<
"\nTest Consistency of Jacobian and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
613 hist <<
" |<w,Jv>| = " << std::abs(wJv) <<
"\n";
614 hist <<
" Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) <<
"\n";
615 outStream << hist.str();
620 template <
class Real>
625 const bool printToStream,
626 std::ostream & outStream,
629 std::vector<Real> steps(numSteps);
630 for(
int i=0;i<numSteps;++i) {
631 steps[i] = pow(10,-i);
634 return checkApplyAdjointHessian(x,u,v,hv,steps,printToStream,outStream,order);
638 template <
class Real>
643 const std::vector<Real> &steps,
644 const bool printToStream,
645 std::ostream & outStream,
651 Real tol = std::sqrt(ROL_EPSILON<Real>());
653 int numSteps = steps.size();
655 std::vector<Real> tmp(numVals);
656 std::vector<std::vector<Real> > ahuvCheck(numSteps, tmp);
659 ROL::Ptr<Vector<Real> > AJdif = hv.
clone();
660 ROL::Ptr<Vector<Real> > AJu = hv.
clone();
661 ROL::Ptr<Vector<Real> > AHuv = hv.
clone();
662 ROL::Ptr<Vector<Real> > AJnew = hv.
clone();
663 ROL::Ptr<Vector<Real> > xnew = x.
clone();
667 oldFormatState.copyfmt(outStream);
671 this->applyAdjointJacobian(*AJu, u, x, tol);
674 this->applyAdjointHessian(*AHuv, u, v, x, tol);
675 Real normAHuv = AHuv->norm();
677 for (
int i=0; i<numSteps; i++) {
685 AJdif->scale(
weights[order-1][0]);
687 for(
int j=0; j<order; ++j) {
689 xnew->axpy(eta*
shifts[order-1][j],v);
691 if(
weights[order-1][j+1] != 0 ) {
693 this->applyAdjointJacobian(*AJnew, u, *xnew, tol);
694 AJdif->axpy(
weights[order-1][j+1],*AJnew);
698 AJdif->scale(one/eta);
701 ahuvCheck[i][0] = eta;
702 ahuvCheck[i][1] = normAHuv;
703 ahuvCheck[i][2] = AJdif->norm();
704 AJdif->axpy(-one, *AHuv);
705 ahuvCheck[i][3] = AJdif->norm();
708 std::stringstream hist;
711 << std::setw(20) <<
"Step size"
712 << std::setw(20) <<
"norm(adj(H)(u,v))"
713 << std::setw(20) <<
"norm(FD approx)"
714 << std::setw(20) <<
"norm(abs error)"
716 << std::setw(20) <<
"---------"
717 << std::setw(20) <<
"-----------------"
718 << std::setw(20) <<
"---------------"
719 << std::setw(20) <<
"---------------"
722 hist << std::scientific << std::setprecision(11) << std::right
723 << std::setw(20) << ahuvCheck[i][0]
724 << std::setw(20) << ahuvCheck[i][1]
725 << std::setw(20) << ahuvCheck[i][2]
726 << std::setw(20) << ahuvCheck[i][3]
728 outStream << hist.str();
734 outStream.copyfmt(oldFormatState);
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
virtual void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the constraint Jacobian at , , to vector .
virtual void scale(const Real alpha)=0
Compute where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual Real checkAdjointConsistencyJacobian(const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &x, const bool printToStream=true, std::ostream &outStream=std::cout)
virtual int dimension() const
Return dimension of the vector space.
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
virtual void plus(const Vector &x)=0
Compute , where .
const double weights[4][5]
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1) override
virtual std::vector< std::vector< Real > > checkApplyAdjointJacobian(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &c, const Vector< Real > &ajv, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS)
Finite-difference check for the application of the adjoint of constraint Jacobian.
ROL::Objective_SimOpt value
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
virtual Real dot(const Vector &x) const =0
Compute where .
void applyJacobian(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &sol)
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ...
virtual std::vector< std::vector< Real > > checkApplyAdjointHessian(const Vector< Real > &x, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &hv, const std::vector< Real > &step, const bool printToScreen=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference check for the application of the adjoint of constraint Hessian. ...
virtual std::vector< std::vector< Real > > checkApplyJacobian(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &jv, const std::vector< Real > &steps, const bool printToStream=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference check for the constraint Jacobian application.
virtual void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
basic_nullstream< char, char_traits< char >> nullstream
virtual std::vector< Real > solveAugmentedSystem(Vector< Real > &v1, Vector< Real > &v2, const Vector< Real > &b1, const Vector< Real > &b2, const Vector< Real > &x, Real &tol)
Approximately solves the augmented system where , , , , is an identity or Riesz operator...
virtual Real norm() const =0
Returns where .