22 #include "Teuchos_GlobalMPISession.hpp"
37 for(
int k=0; k<
m_; ++k ) {
39 if( k<
n_ )
y_[k] += x[k]*x[k];
40 if( k>0 )
y_[k] -= 2*x[k-1];
48 RealT& tol )
override {
49 for(
int k=0; k<
m_; ++k ) c[k] = std::cos(
y_[k]);
55 RealT& tol )
override {
56 for(
int k=0; k<
m_; ++k ) {
58 if( k<
n_ ) jv[k] -= 2*v[k]*x[k]*
sin_[k];
59 if( k>0 ) jv[k] += 2*v[k-1]*sin_[k];
66 RealT& tol )
override {
67 for(
int i=0; i<
n_; ++i ) {
68 ajl[i] = 2*(l[i+1]*
sin_[i+1] - l[i]*x[i]*
sin_[i]);
76 RealT& tol )
override {
77 ahlv[0] = -4*
cos_[0]*l[0]*v[0]*x[0]*x[0] - 4*
cos_[1]*l[1]*v[0] + 4*
cos_[1]*l[1]*v[1]*x[1] - 2*l[0]*
sin_[0]*v[0];
78 for(
int i=0; i<
n_-1; ++i) {
79 ahlv[i] = 4*
cos_[i]*l[i]*v[i-1]*x[i] - 4*
cos_[i]*l[i]*v[i]*x[i]*x[i] - 4*
cos_[i+1]*l[i+1]*v[i] + 4*
cos_[i+1]*l[i+1]*v[i+1]*x[i+1] - 2*l[i]*
sin_[i]*v[i];
81 ahlv[n_-1] = 4*
cos_[n_-1]*l[n_-1]*v[n_-2]*x[n_-1] - 4*
cos_[n_-1]*l[n_-1]*v[n_-1]*x[n_-1]*x[n_-1] - 4*
cos_[
n_]*l[
n_]*v[n_-1] - 2*l[n_-1]*
sin_[n_-1]*v[n_-1];
98 for(
int i=0; i<
m_; ++i)
99 d_[i] = -x[i] + 2*x[i+1] - x[i+2];
104 RealT& tol )
override {
105 for(
int i=0; i<
m_; ++i)
112 RealT& tol )
override {
113 for(
int i=0; i<
m_; ++i)
114 jv[i] = 2*
d_[i]*(-v[i]+2*v[i+1]-v[i+2]);
120 RealT& tol )
override {
121 for(
int i=0; i<
n_; ++i) {
123 for(
int j=0; j<
m_; ++j ) {
124 if( j == i-1 ) ajl[i] += 4*
d_[j]*l[j];
125 else if( std::abs(i-j-1) == 1) ajl[i] -= 2*
d_[j]*l[j];
134 RealT& tol )
override {
135 for(
int i=0; i<
m_; ++i)
136 dv_[i] = -v[i] + 2*v[i+1] - v[i+2];
137 for(
int i=0; i<
n_; ++i) {
139 for(
int j=0; j<
m_; ++j ) {
140 if( j == i-1 ) ahlv[i] += 4*
dv_[j]*l[j];
141 else if( std::abs(i-j-1) == 1) ahlv[i] -= 2*
dv_[j]*l[j];
155 int main(
int argc,
char *argv[]) {
157 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
160 int iprint = argc - 1;
161 ROL::Ptr<std::ostream> outStream;
164 outStream = ROL::makePtrFromRef(std::cout);
166 outStream = ROL::makePtrFromRef(bhs);
170 oldFormatState.copyfmt(std::cout);
181 int ci_dim = x_dim + 1;
182 int co_dim = x_dim - 1;
184 auto inner_con = ROL::makePtr<InnerConstraint>(x_dim);
185 auto outer_con = ROL::makePtr<OuterConstraint>(ci_dim);
187 auto x = ROL::makePtr<ROL::StdVector<RealT>>(x_dim);
188 auto v = ROL::makePtr<ROL::StdVector<RealT>>(x_dim);
189 auto y = ROL::makePtr<ROL::StdVector<RealT>>(ci_dim);
190 auto w = ROL::makePtr<ROL::StdVector<RealT>>(ci_dim);
191 auto ci = ROL::makePtr<ROL::StdVector<RealT>>(ci_dim);
192 auto co = ROL::makePtr<ROL::StdVector<RealT>>(co_dim);
194 auto cr_con = ROL::makePtr<ROL::ChainRuleConstraint<RealT>>(outer_con,inner_con,*x,*ci);
203 *outStream <<
"\n\nInner Constraint Check:\n\n";
205 inner_con->checkApplyJacobian(*x,*v,*ci,
true,*outStream,7,4);
206 inner_con->checkAdjointConsistencyJacobian(*ci,*v,*x,
true,*outStream);
207 inner_con->checkApplyAdjointHessian(*x,*ci,*v,*x,
true,*outStream,7,4);
209 *outStream <<
"\n\nOuter Constraint Check:\n\n";
210 outer_con->checkApplyJacobian(*y,*w,*co,
true,*outStream,7,4);
211 outer_con->checkAdjointConsistencyJacobian(*co,*w,*y,
true,*outStream);
212 outer_con->checkApplyAdjointHessian(*y,*co,*w,*y,
true,*outStream,7,4);
214 *outStream <<
"\n\nChain Rule Constraint Check:\n\n";
215 cr_con->checkApplyJacobian(*x,*v,*co,
true,*outStream,7,4);
216 cr_con->checkAdjointConsistencyJacobian(*co,*v,*x,
true,*outStream);
217 cr_con->checkApplyAdjointHessian(*x,*co,*v,*x,
true,*outStream,7,4);
220 catch (std::logic_error& err) {
221 *outStream << err.what() <<
"\n";
226 std::cout <<
"End Result: TEST FAILED\n";
228 std::cout <<
"End Result: TEST PASSED\n";
void applyAdjointJacobian(VectorT &ajl, const VectorT &l, const VectorT &x, RealT &tol) override
void applyJacobian(VectorT &jv, const VectorT &v, const VectorT &x, RealT &tol) override
void applyAdjointHessian(VectorT &ahlv, const VectorT &l, const VectorT &v, const VectorT &x, RealT &tol) override
void update(const VectorT &x, ROL::UpdateType type, int iter) override
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
Defines the equality constraint operator interface for StdVectors.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void value(VectorT &c, const VectorT &x, RealT &tol) override
basic_nullstream< char, std::char_traits< char >> nullstream
std::vector< RealT > VectorT
void value(VectorT &c, const VectorT &x, RealT &tol) override
void update(const VectorT &x, ROL::UpdateType type, int iter) override
void applyJacobian(VectorT &jv, const VectorT &v, const VectorT &x, RealT &tol) override
void applyAdjointHessian(VectorT &ahlv, const VectorT &l, const VectorT &v, const VectorT &x, RealT &tol) override
void applyAdjointJacobian(VectorT &ajl, const VectorT &l, const VectorT &x, RealT &tol) override
int main(int argc, char *argv[])