10 #ifndef ROL_CONSTRAINT_DEF_H
11 #define ROL_CONSTRAINT_DEF_H
13 #include "ROL_LinearAlgebra.hpp"
14 #include "ROL_LAPACK.hpp"
25 Real ctol = std::sqrt(ROL_EPSILON<Real>());
28 Real h = std::max(one,x.
norm()/v.
norm())*tol;
32 ROL::Ptr<Vector<Real> > c = jv.
clone();
33 this->
value(*c,x,ctol);
36 ROL::Ptr<Vector<Real> > xnew = x.
clone();
43 this->
value(jv,*xnew,ctol);
56 applyAdjointJacobian(ajv,v,x,v.
dual(),tol);
73 Real h(0), ctol = std::sqrt(ROL_EPSILON<Real>());
75 ROL::Ptr<Vector<Real> > xnew = x.
clone();
76 ROL::Ptr<Vector<Real> > ex = x.
clone();
77 ROL::Ptr<Vector<Real> > eajv = ajv.
clone();
78 ROL::Ptr<Vector<Real> > cnew = dualv.
clone();
79 ROL::Ptr<Vector<Real> > c0 = dualv.
clone();
80 this->
value(*c0,x,ctol);
83 for (
int i = 0; i < ajv.
dimension(); i++ ) {
86 h = std::max(one,x.
norm()/ex->norm())*tol;
90 this->
value(*cnew,*xnew,ctol);
94 ajv.
axpy(cnew->apply(v),*eajv);
131 template <
class Real>
138 Real h = std::max(static_cast<Real>(1),x.
norm()/v.
norm())*tol;
141 ROL::Ptr<Vector<Real> > aju = huv.
clone();
142 applyAdjointJacobian(*aju,u,x,tol);
145 ROL::Ptr<Vector<Real> > xnew = x.
clone();
152 applyAdjointJacobian(huv,u,*xnew,tol);
155 huv.
axpy(static_cast<Real>(-1),*aju);
156 huv.
scale(static_cast<Real>(1)/h);
160 template <
class Real>
169 const Real
zero(0), one(1);
178 tol = std::sqrt(b1.
dot(b1)+b2.
dot(b2))*tol;
184 ROL::Ptr<Vector<Real> > r1 = b1.
clone();
185 ROL::Ptr<Vector<Real> > r2 = b2.
clone();
186 ROL::Ptr<Vector<Real> > z1 = v1.
clone();
187 ROL::Ptr<Vector<Real> > z2 = v2.
clone();
188 ROL::Ptr<Vector<Real> > w1 = b1.
clone();
189 ROL::Ptr<Vector<Real> > w2 = b2.
clone();
190 std::vector<ROL::Ptr<Vector<Real> > > V1;
191 std::vector<ROL::Ptr<Vector<Real> > > V2;
192 ROL::Ptr<Vector<Real> > V2temp = b2.
clone();
193 std::vector<ROL::Ptr<Vector<Real> > > Z1;
194 std::vector<ROL::Ptr<Vector<Real> > > Z2;
195 ROL::Ptr<Vector<Real> > w1temp = b1.
clone();
196 ROL::Ptr<Vector<Real> > Z2temp = v2.
clone();
198 std::vector<Real> res(m+1,
zero);
199 LA::Matrix<Real> H(m+1,m);
200 LA::Vector<Real> cs(m);
201 LA::Vector<Real> sn(m);
202 LA::Vector<Real> s(m+1);
203 LA::Vector<Real> y(m+1);
204 LA::Vector<Real> cnorm(m);
205 ROL::LAPACK<int, Real> lapack;
208 applyAdjointJacobian(*r1, v2, x, zerotol);
209 r1->scale(-one); r1->axpy(-one, v1.
dual()); r1->plus(b1);
211 r2->scale(-one); r2->plus(b2);
212 res[0] = std::sqrt(r1->dot(*r1) + r2->dot(*r2));
215 if (res[0] ==
zero) {
220 V1.push_back(b1.
clone()); (V1[0])->set(*r1); (V1[0])->scale(one/res[0]);
221 V2.push_back(b2.
clone()); (V2[0])->set(*r2); (V2[0])->scale(one/res[0]);
225 for (i=0; i<m; i++) {
228 V2temp->set(*(V2[i]));
229 applyPreconditioner(*Z2temp, *V2temp, x, b1, zerotol);
230 Z2.push_back(v2.
clone()); (Z2[i])->set(*Z2temp);
231 Z1.push_back(v1.
clone()); (Z1[i])->set((V1[i])->dual());
235 applyAdjointJacobian(*w1temp, *Z2temp, x, zerotol);
236 w1->set(*(V1[i])); w1->plus(*w1temp);
239 for (k=0; k<=i; k++) {
240 H(k,i) = w1->dot(*(V1[k])) + w2->dot(*(V2[k]));
241 w1->axpy(-H(k,i), *(V1[k]));
242 w2->axpy(-H(k,i), *(V2[k]));
244 H(i+1,i) = std::sqrt(w1->dot(*w1) + w2->dot(*w2));
246 V1.push_back(b1.
clone()); (V1[i+1])->set(*w1); (V1[i+1])->scale(one/H(i+1,i));
247 V2.push_back(b2.
clone()); (V2[i+1])->set(*w2); (V2[i+1])->scale(one/H(i+1,i));
250 for (k=0; k<=i-1; k++) {
251 temp = cs(k)*H(k,i) + sn(k)*H(k+1,i);
252 H(k+1,i) = -sn(k)*H(k,i) + cs(k)*H(k+1,i);
257 if ( H(i+1,i) ==
zero ) {
261 else if ( std::abs(H(i+1,i)) > std::abs(H(i,i)) ) {
262 temp = H(i,i) / H(i+1,i);
263 sn(i) = one / std::sqrt( one + temp*temp );
264 cs(i) = temp * sn(i);
267 temp = H(i+1,i) / H(i,i);
268 cs(i) = one / std::sqrt( one + temp*temp );
269 sn(i) = temp * cs(i);
274 s(i+1) = -sn(i)*s(i);
276 H(i,i) = cs(i)*H(i,i) + sn(i)*H(i+1,i);
278 resnrm = std::abs(s(i+1));
282 const char uplo =
'U';
283 const char trans =
'N';
284 const char diag =
'N';
285 const char normin =
'N';
289 lapack.LATRS(uplo, trans, diag, normin, i+1, H.values(), m+1, y.values(), &scaling, cnorm.values(), &info);
292 for (k=0; k<=i; k++) {
293 z1->axpy(y(k), *(Z1[k]));
294 z2->axpy(y(k), *(Z2[k]));
301 if (res[i+1] <= tol) {
329 template <
class Real>
333 const bool printToStream,
334 std::ostream & outStream,
337 std::vector<Real> steps(numSteps);
338 for(
int i=0;i<numSteps;++i) {
339 steps[i] = pow(10,-i);
342 return checkApplyJacobian(x,v,jv,steps,printToStream,outStream,order);
348 template <
class Real>
352 const std::vector<Real> &steps,
353 const bool printToStream,
354 std::ostream & outStream,
356 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
357 "Error: finite difference order must be 1,2,3, or 4" );
364 Real tol = std::sqrt(ROL_EPSILON<Real>());
366 int numSteps = steps.size();
368 std::vector<Real> tmp(numVals);
369 std::vector<std::vector<Real> > jvCheck(numSteps, tmp);
373 oldFormatState.copyfmt(outStream);
376 ROL::Ptr<Vector<Real> > c = jv.
clone();
378 this->
value(*c, x, tol);
381 ROL::Ptr<Vector<Real> > Jv = jv.
clone();
383 Real normJv = Jv->norm();
386 ROL::Ptr<Vector<Real> > cdif = jv.
clone();
387 ROL::Ptr<Vector<Real> > cnew = jv.
clone();
388 ROL::Ptr<Vector<Real> > xnew = x.
clone();
390 for (
int i=0; i<numSteps; i++) {
397 cdif->scale(
weights[order-1][0]);
399 for(
int j=0; j<order; ++j) {
401 xnew->axpy(eta*
shifts[order-1][j], v);
403 if(
weights[order-1][j+1] != 0 ) {
405 this->
value(*cnew,*xnew,tol);
406 cdif->axpy(
weights[order-1][j+1],*cnew);
411 cdif->scale(one/eta);
415 jvCheck[i][1] = normJv;
416 jvCheck[i][2] = cdif->norm();
417 cdif->axpy(-one, *Jv);
418 jvCheck[i][3] = cdif->norm();
421 std::stringstream hist;
424 << std::setw(20) <<
"Step size"
425 << std::setw(20) <<
"norm(Jac*vec)"
426 << std::setw(20) <<
"norm(FD approx)"
427 << std::setw(20) <<
"norm(abs error)"
429 << std::setw(20) <<
"---------"
430 << std::setw(20) <<
"-------------"
431 << std::setw(20) <<
"---------------"
432 << std::setw(20) <<
"---------------"
435 hist << std::scientific << std::setprecision(11) << std::right
436 << std::setw(20) << jvCheck[i][0]
437 << std::setw(20) << jvCheck[i][1]
438 << std::setw(20) << jvCheck[i][2]
439 << std::setw(20) << jvCheck[i][3]
441 outStream << hist.str();
447 outStream.copyfmt(oldFormatState);
453 template <
class Real>
458 const bool printToStream,
459 std::ostream & outStream,
460 const int numSteps) {
461 Real tol = std::sqrt(ROL_EPSILON<Real>());
465 std::vector<Real> tmp(numVals);
466 std::vector<std::vector<Real> > ajvCheck(numSteps, tmp);
467 Real eta_factor = 1e-1;
471 ROL::Ptr<Vector<Real> > c0 = c.
clone();
472 ROL::Ptr<Vector<Real> > cnew = c.
clone();
473 ROL::Ptr<Vector<Real> > xnew = x.
clone();
474 ROL::Ptr<Vector<Real> > ajv0 = ajv.
clone();
475 ROL::Ptr<Vector<Real> > ajv1 = ajv.
clone();
476 ROL::Ptr<Vector<Real> > ex = x.
clone();
477 ROL::Ptr<Vector<Real> > eajv = ajv.
clone();
481 oldFormatState.copyfmt(outStream);
485 this->
value(*c0, x, tol);
488 this->applyAdjointJacobian(*ajv0, v, x, tol);
489 Real normAJv = ajv0->norm();
491 for (
int i=0; i<numSteps; i++) {
495 for (
int j = 0; j < ajv.
dimension(); j++ ) {
501 this->
value(*cnew,*xnew,tol);
502 cnew->axpy(-one,*c0);
503 cnew->scale(one/eta);
505 ajv1->axpy(cnew->apply(v),*eajv);
509 ajvCheck[i][0] = eta;
510 ajvCheck[i][1] = normAJv;
511 ajvCheck[i][2] = ajv1->norm();
512 ajv1->axpy(-one, *ajv0);
513 ajvCheck[i][3] = ajv1->norm();
516 std::stringstream hist;
519 << std::setw(20) <<
"Step size"
520 << std::setw(20) <<
"norm(adj(Jac)*vec)"
521 << std::setw(20) <<
"norm(FD approx)"
522 << std::setw(20) <<
"norm(abs error)"
524 << std::setw(20) <<
"---------"
525 << std::setw(20) <<
"------------------"
526 << std::setw(20) <<
"---------------"
527 << std::setw(20) <<
"---------------"
530 hist << std::scientific << std::setprecision(11) << std::right
531 << std::setw(20) << ajvCheck[i][0]
532 << std::setw(20) << ajvCheck[i][1]
533 << std::setw(20) << ajvCheck[i][2]
534 << std::setw(20) << ajvCheck[i][3]
536 outStream << hist.str();
540 eta = eta*eta_factor;
544 outStream.copyfmt(oldFormatState);
549 template <
class Real>
555 const bool printToStream,
556 std::ostream & outStream) {
557 Real tol = ROL_EPSILON<Real>();
559 ROL::Ptr<Vector<Real> > Jv = dualw.
clone();
560 ROL::Ptr<Vector<Real> > Jw = dualv.
clone();
564 applyAdjointJacobian(*Jw,w,x,tol);
567 Real vJw = v.
apply(*Jw);
569 Real wJv = w.
apply(*Jv);
571 Real diff = std::abs(wJv-vJw);
573 if ( printToStream ) {
574 std::stringstream hist;
575 hist << std::scientific << std::setprecision(8);
576 hist <<
"\nTest Consistency of Jacobian and its adjoint: \n |<w,Jv> - <adj(J)w,v>| = "
578 hist <<
" |<w,Jv>| = " << std::abs(wJv) <<
"\n";
579 hist <<
" Relative Error = " << diff / (std::abs(wJv)+ROL_UNDERFLOW<Real>()) <<
"\n";
580 outStream << hist.str();
585 template <
class Real>
590 const bool printToStream,
591 std::ostream & outStream,
594 std::vector<Real> steps(numSteps);
595 for(
int i=0;i<numSteps;++i) {
596 steps[i] = pow(10,-i);
599 return checkApplyAdjointHessian(x,u,v,hv,steps,printToStream,outStream,order);
603 template <
class Real>
608 const std::vector<Real> &steps,
609 const bool printToStream,
610 std::ostream & outStream,
616 Real tol = std::sqrt(ROL_EPSILON<Real>());
618 int numSteps = steps.size();
620 std::vector<Real> tmp(numVals);
621 std::vector<std::vector<Real> > ahuvCheck(numSteps, tmp);
624 ROL::Ptr<Vector<Real> > AJdif = hv.
clone();
625 ROL::Ptr<Vector<Real> > AJu = hv.
clone();
626 ROL::Ptr<Vector<Real> > AHuv = hv.
clone();
627 ROL::Ptr<Vector<Real> > AJnew = hv.
clone();
628 ROL::Ptr<Vector<Real> > xnew = x.
clone();
632 oldFormatState.copyfmt(outStream);
636 this->applyAdjointJacobian(*AJu, u, x, tol);
639 this->applyAdjointHessian(*AHuv, u, v, x, tol);
640 Real normAHuv = AHuv->norm();
642 for (
int i=0; i<numSteps; i++) {
650 AJdif->scale(
weights[order-1][0]);
652 for(
int j=0; j<order; ++j) {
654 xnew->axpy(eta*
shifts[order-1][j],v);
656 if(
weights[order-1][j+1] != 0 ) {
658 this->applyAdjointJacobian(*AJnew, u, *xnew, tol);
659 AJdif->axpy(
weights[order-1][j+1],*AJnew);
663 AJdif->scale(one/eta);
666 ahuvCheck[i][0] = eta;
667 ahuvCheck[i][1] = normAHuv;
668 ahuvCheck[i][2] = AJdif->norm();
669 AJdif->axpy(-one, *AHuv);
670 ahuvCheck[i][3] = AJdif->norm();
673 std::stringstream hist;
676 << std::setw(20) <<
"Step size"
677 << std::setw(20) <<
"norm(adj(H)(u,v))"
678 << std::setw(20) <<
"norm(FD approx)"
679 << std::setw(20) <<
"norm(abs error)"
681 << std::setw(20) <<
"---------"
682 << std::setw(20) <<
"-----------------"
683 << std::setw(20) <<
"---------------"
684 << std::setw(20) <<
"---------------"
687 hist << std::scientific << std::setprecision(11) << std::right
688 << std::setw(20) << ahuvCheck[i][0]
689 << std::setw(20) << ahuvCheck[i][1]
690 << std::setw(20) << ahuvCheck[i][2]
691 << std::setw(20) << ahuvCheck[i][3]
693 outStream << hist.str();
699 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 .