ROL
ROL_CantileverBeam.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
17 #ifndef ROL_CANTILEVERBEAM_HPP
18 #define ROL_CANTILEVERBEAM_HPP
19 
20 #include "ROL_StdVector.hpp"
21 #include "ROL_TestProblem.hpp"
22 #include "ROL_Bounds.hpp"
23 #include "ROL_StdObjective.hpp"
24 #include "ROL_StdConstraint.hpp"
25 
26 namespace ROL {
27 namespace ZOO {
28 
29 template<class Real>
30 class Objective_CantileverBeam : public StdObjective<Real> {
31 private:
32  int nseg_;
33  Real len_;
34  std::vector<Real> l_;
35 
36 public:
37  Objective_CantileverBeam(const int nseg = 5)
38  : nseg_(nseg), len_(500) {
39  l_.clear(); l_.resize(nseg, len_/static_cast<Real>(nseg_));
40  }
41 
42  Real value( const std::vector<Real> &x, Real &tol ) {
43  Real val(0);
44  for (int i = 0; i < nseg_; ++i) {
45  val += l_[i]*x[i]*x[i+nseg_];
46  }
47  return val;
48  }
49 
50  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
51  for (int i = 0; i < nseg_; ++i) {
52  g[i] = l_[i]*x[i+nseg_];
53  g[i+nseg_] = l_[i]*x[i];
54  }
55  }
56 
57  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
58  for (int i = 0; i < nseg_; ++i) {
59  hv[i] = l_[i]*v[i+nseg_];
60  hv[i+nseg_] = l_[i]*v[i];
61  }
62  }
63 }; // class Objective_CantileverBeam
64 
65 
66 template<class Real>
68 private:
69  int nseg_;
71  std::vector<Real> l_, suml_, M_, dyp_;
72 
73 public:
74  Constraint_CantileverBeam(const int nseg = 5)
75  : nseg_(nseg), len_(500), P_(50e3), E_(200e5), Sigma_max_(14e3), tip_max_(2.5), suml_(0) {
76  const Real half(0.5);
77  l_.clear();
78  l_.resize(nseg_, len_/static_cast<Real>(nseg_));
79  suml_.resize(nseg_);
80  M_.resize(nseg_);
81  dyp_.resize(nseg_);
82  for (int i = 0; i < nseg_; ++i) {
83  suml_[i] = static_cast<Real>(i+1)*l_[i];
84  M_[i] = P_*(len_ - suml_[i] + l_[i]);
85  dyp_[i] = (len_ - suml_[i] + half*l_[i])*static_cast<Real>(nseg_-i-1);
86  }
87  }
88 
89  void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
90  const Real one(1), two(2), three(3), twelve(12), twenty(20);
91  std::vector<Real> y1(nseg_), yp(nseg_);
92  Real Inertia(0), sigma(0), sumy1(0), sumypl(0);
93  for (int i = 0; i < nseg_; ++i) {
94  // stress constraint
95  Inertia = x[i]*std::pow(x[i+nseg_],3)/twelve;
96  sigma = M_[i]*(x[i+nseg_]/two)/Inertia;
97  c[i] = sigma / Sigma_max_ - one;
98  // lateral slope constraint
99  c[i+nseg_] = x[i+nseg_] - twenty*x[i];
100  // tip deflection constraint
101  y1[i] = P_*std::pow(l_[i],2)/(two*E_*Inertia) * (len_ - suml_[i] + two/three*l_[i]);
102  yp[i] = P_*l_[i]/(E_*Inertia) * (len_ - suml_[i] + l_[i]/two);
103  sumy1 += y1[i];
104  }
105  // tip deflection constraint
106  for (int i = 1; i < nseg_; ++i) {
107  yp[i] += yp[i-1];
108  sumypl += yp[i-1]*l_[i];
109  }
110  Real yN = sumy1 + sumypl;
111  c[2*nseg_] = yN / tip_max_ - one;
112  }
113 
114  void applyJacobian( std::vector<Real> &jv, const std::vector<Real> &v,
115  const std::vector<Real> &x, Real &tol ) {
116  const Real two(2), three(3), six(6), twelve(12), twenty(20);
117  // const Real one(1); // Unused
118  Real Inertia(0), dyN(0), sumW(0), sumH(0);
119  for (int i = 0; i < nseg_; ++i) {
120  // stress constraint
121  jv[i] = -six/Sigma_max_*M_[i]*v[i]/std::pow(x[i]*x[i+nseg_],2)
122  -twelve/Sigma_max_*M_[i]*v[i+nseg_]/(x[i]*std::pow(x[i+nseg_],3));
123  // lateral slope constraint
124  jv[i+nseg_] = v[i+nseg_] - twenty*v[i];
125  // tip deflection constraint
126  Inertia = x[i]*std::pow(x[i+nseg_],3)/twelve;
127  dyN = -P_/E_ * std::pow(l_[i],2)/Inertia*((len_ - suml_[i] + two/three*l_[i])/two + dyp_[i]);
128  sumW += v[i]*(dyN/x[i])/tip_max_;
129  sumH += three*v[i+nseg_]*(dyN/x[i+nseg_])/tip_max_;
130  }
131  // tip deflection constraint
132  jv[2*nseg_] = sumW+sumH;
133  }
134 
135  void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v,
136  const std::vector<Real> &x, Real &tol ) {
137  const Real two(2), three(3), six(6), twelve(12), twenty(20);
138 // const Real one(1); // Unused
139  Real Inertia(0), dyN(0);
140  for (int i = 0; i < nseg_; ++i) {
141  // stress constraint
142  ajv[i] = -six/Sigma_max_*M_[i]*v[i]/std::pow(x[i]*x[i+nseg_],2);
143  ajv[i+nseg_] = -twelve/Sigma_max_*M_[i]*v[i]/(x[i]*std::pow(x[i+nseg_],3));
144  // lateral slope constraint
145  ajv[i] += -twenty*v[i+nseg_];
146  ajv[i+nseg_] += v[i+nseg_];
147  // tip deflection constraint
148  Inertia = x[i]*std::pow(x[i+nseg_],3)/twelve;
149  dyN = -P_/E_ * std::pow(l_[i],2)/Inertia*((len_ - suml_[i] + two/three*l_[i])/two + dyp_[i]);
150  ajv[i] += v[2*nseg_]*(dyN/x[i])/tip_max_;
151  ajv[i+nseg_] += three*v[2*nseg_]*(dyN/x[i+nseg_])/tip_max_;
152  }
153  }
154 
155 // void applyAdjointHessian( std::vector<Real> &ahuv, const std::vector<Real> &u,
156 // const std::vector<Real> &v, const std::vector<Real> &x,
157 // Real &tol ) {
158 // }
159 
160 }; // class Constraint_CantileverBeam
161 
162 
163 
164 template<class Real>
165 class getCantileverBeam : public TestProblem<Real> {
166 private:
167  int nseg_;
168 
169 public:
170  getCantileverBeam(int nseg = 5) : nseg_(nseg) {}
171 
172  Ptr<Objective<Real> > getObjective( void ) const {
173  return makePtr<Objective_CantileverBeam<Real>>(nseg_);
174  }
175 
176  Ptr<Constraint<Real> > getInequalityConstraint( void ) const {
177  return makePtr<Constraint_CantileverBeam<Real>>(nseg_);
178  }
179 
180  Ptr<BoundConstraint<Real>> getBoundConstraint( void ) const {
181  // Lower bound is zero
182  Ptr<std::vector<Real>> lp = makePtr<std::vector<Real>>(2*nseg_);
183  for (int i = 0; i < nseg_; ++i) {
184  (*lp)[i] = static_cast<Real>(1);
185  (*lp)[i+nseg_] = static_cast<Real>(5);
186  }
187 
188  // No upper bound
189  Ptr<std::vector<Real>> up = makePtr<std::vector<Real>>(2*nseg_,ROL_INF<Real>());
190 
191  Ptr<Vector<Real>> l = makePtr<StdVector<Real>>(lp);
192  Ptr<Vector<Real>> u = makePtr<StdVector<Real>>(up);
193 
194  return makePtr<Bounds<Real>>(l,u);
195  }
196 
197  Ptr<Vector<Real>> getInitialGuess( void ) const {
198  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(2*nseg_,0);
199  for (int i = 0; i < nseg_; ++i) {
200  (*x0p)[i] = static_cast<Real>(5);
201  (*x0p)[i+nseg_] = static_cast<Real>(40);
202  }
203 
204  return makePtr<StdVector<Real>>(x0p);
205  }
206 
207  Ptr<Vector<Real>> getSolution( const int i = 0 ) const {
208  //Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(2);
209  //(*xp)[0] = 3.0;
210  //(*xp)[1] = std::sqrt(3.0);
211 
212  Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(2*nseg_);
213  for (int i = 0; i < nseg_; ++i) {
214  (*xp)[i] = 3.0;
215  (*xp)[i+nseg_] = std::sqrt(3.0);
216  }
217 
218  return makePtr<StdVector<Real>>(xp);
219  }
220 
221  Ptr<Vector<Real>> getInequalityMultiplier( void ) const {
222  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(2*nseg_+1,0.0);
223  return makePtr<StdVector<Real>>(lp);
224  }
225 
226  Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
227  // No lower bound
228  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(2*nseg_+1,ROL_NINF<Real>());
229 
230  // Upper bound is zero
231  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(2*nseg_+1,0);
232 
233  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
234  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
235 
236  return makePtr<Bounds<Real>>(l,u);
237  }
238 };
239 
240 
241 } // namespace ZOO
242 } // namespace ROL
243 
244 #endif // ROL_CANTILEVERBEAM_HPP
245 
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const
Real value(const std::vector< Real > &x, Real &tol)
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Defines the equality constraint operator interface for StdVectors.
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Ptr< Objective< Real > > getObjective(void) const
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
Contains definitions of test objective functions.
Ptr< Vector< Real > > getSolution(const int i=0) const
Ptr< Vector< Real > > getInitialGuess(void) const
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Ptr< Vector< Real > > getInequalityMultiplier(void) const
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)