ROL
sacado/example_03.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #include "ROL_StdVector.hpp"
45 #include "FiniteElement.hpp"
46 #include "LinearAlgebra.hpp"
47 
48 using namespace ROL;
49 
50 
51 // Define some terms which make up the constraint
52 class DiffU {
53 public:
54  template<class ScalarT>
55  ScalarT operator() (const ScalarT &u, const ScalarT &u_x, const ScalarT &z, const ScalarT &x) {
56  return u_x;
57  }
58 };
59 
60 class ExpU {
61 public:
62  template<class ScalarT>
63  ScalarT operator() (const ScalarT &u, const ScalarT &u_x, const ScalarT &z, const ScalarT &x) {
64  return exp(u);
65  }
66 };
67 
68 class MinusZ {
69 public:
70  template<class ScalarT>
71  ScalarT operator() (const ScalarT &u, const ScalarT &u_x, const ScalarT &z, const ScalarT &x) {
72  return -z;
73  }
74 };
75 
76 
77 
79 template<class Real>
81  private:
82  int n_;
83  VectorFunction<Real,DiffU> diff_; // -u'' term
84  VectorFunction<Real,ExpU> exp_; // exp(u) term
85  VectorFunction<Real,MinusZ> minusz_; // -z term
86  public:
87  BoundaryValueProblem( Real xl, Real xr, Teuchos::RCP<NodalBasis<Real> > basisp ) :
88  n_(basisp->ni_),
89  diff_(xl,xr,true,basisp),
90  exp_(xl,xr,false,basisp),
91  minusz_(xl,xr,false,basisp) {
92  }
93 
94  template<class ScalarT>
95  void value(Vector<ScalarT> &c, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) {
96 
97  Teuchos::RCP<std::vector<ScalarT> > temp_rcp = Teuchos::rcp(new std::vector<ScalarT>(n_,0));
98  StdVector<ScalarT> temp(temp_rcp);
99 
100  c.zero(); // This seems to be important. Getting weird results without this zeroing
101 
102  diff_.evaluate(u,z,c);
103  exp_.evaluate(u,z,temp);
104  c.plus(temp);
105  minusz_.evaluate(u,z,temp);
106  c.plus(temp); // c = -u" + exp(u) - z
107  }
108 };
109 
110 
111 
112 
114 template<class Real,template<class> class BoundaryValueProblem>
115 class BVP_Constraint : public Sacado_EqualityConstraint_SimOpt<Real,BoundaryValueProblem> {
116  private:
117  Teuchos::LAPACK<int,Real> lapack_;
118 
119  public:
120 
122  Sacado_EqualityConstraint_SimOpt<Real,BoundaryValueProblem> (bvp){}
123 
124  /*void applyInverseJacobian_1(Vector< Real > &ijv, const Vector< Real > &v,
125  const Vector< Real > &u, const Vector< Real > &z, Real &tol) {
126 
127  Teuchos::RCP<const std::vector<Real> > vp =
128  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
129 
130  int n = vp->size();
131 
132  // Canonical vector
133  Teuchos::RCP<std::vector<Real> > ep = Teuchos::rcp(new std::vector<Real>(n,0.0) );
134  StdVector<Real> e(ep);
135 
136  // Jacobian applied to canonical vector
137  Teuchos::RCP<std::vector<Real> > jep = Teuchos::rcp(new std::vector<Real>(n,0.0) );
138  StdVector<Real> je(jep);
139 
140  Teuchos::RCP<std::vector<Real> > jacp = Teuchos::rcp(new std::vector<Real>(n*n,0.0));
141  StdVector<Real> jac(jacp);
142 
143  // Form the column-stacked Jacobian
144  for(int i=0; i<n; ++i) {
145  (*ep)[i] = 1.0;
146 
147  Sacado_EqualityConstraint_SimOpt<Real,BoundaryValueProblem>::applyJacobian_1(je,e,u,z,tol);
148  std::copy(jep->begin(),jep->end(),jacp->begin()+n*i);
149  std::fill(ep->begin(),ep->end(),0.0);
150  }
151 
152  lusolve(lapack_,jac,v,ijv);
153  }*/
154 
155  /*void applyInverseAdjointJacobian_1(Vector< Real > &iajv, const Vector< Real > &v,
156  const Vector< Real > &u, const Vector< Real > &z, Real &tol) {
157 
158  Teuchos::RCP<const std::vector<Real> > vp =
159  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
160 
161  int n = vp->size();
162 
163  // Canonical vector
164  Teuchos::RCP<std::vector<Real> > ep = Teuchos::rcp(new std::vector<Real>(n,0.0) );
165  StdVector<Real> e(ep);
166 
167  // Adjoint Jacobian applied to canonical vector
168  Teuchos::RCP<std::vector<Real> > ajep = Teuchos::rcp(new std::vector<Real>(n,0.0) );
169  StdVector<Real> aje(ajep);
170 
171  Teuchos::RCP<std::vector<Real> > ajacp = Teuchos::rcp(new std::vector<Real>(n*n,0.0));
172  StdVector<Real> ajac(ajacp);
173 
174  // Form the column-stacked Jacobian
175  for(int i=0; i<n; ++i) {
176  (*ep)[i] = 1.0;
177 
178  Sacado_EqualityConstraint_SimOpt<Real,BoundaryValueProblem>::applyAdjointJacobian_1(aje,e,u,z,tol);
179  std::copy(ajep->begin(),ajep->end(),ajacp->begin()+n*i);
180  std::fill(ep->begin(),ep->end(),0.0);
181  }
182 
183  lusolve(lapack_,ajac,v,iajv);
184  }*/
185 
186 
187  /*void solve(Vector<Real> &u, const Vector<Real> &z, Real &tol) {
188  Teuchos::RCP<std::vector<Real> > up =
189  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(u)).getVector());
190 
191  int n = up->size();
192 
193  Teuchos::RCP<std::vector<Real> > cp = Teuchos::rcp(new std::vector<Real>(n,0.0) );
194  StdVector<Real> c(cp);
195 
196  // \f$ -\delta u \f$
197  Teuchos::RCP<std::vector<Real> > mdup = Teuchos::rcp(new std::vector<Real>(n,0.0) );
198  StdVector<Real> mdu(mdup);
199 
200  Real res = 1.0;
201  Real restol = 1e-7;
202  int MAXIT = 20;
203  int iter = 0;
204 
205  // Newton's method
206  while(iter<MAXIT && res>restol) {
207  this->value(c,u,z,tol);
208  this->applyInverseJacobian_1(mdu,c,u,z,tol);
209  mdu.scale(-1.0);
210  u.plus(mdu);
211  res = c.norm();
212  }
213 
214  // Should throw exception if restol not met
215 
216  }*/
217 
218 };
219 
220 
221 
222 
224 template<class Real>
226  private:
227  Real gamma_;
228  Teuchos::RCP<std::vector<Real> > u_targ_rcp_;
229  Teuchos::RCP<NodalBasis<Real> > basisp_;
230  int ni_;
231  int nq_;
232  public:
233  QuadraticTracking( Real gamma, const Vector<Real> &u_targ, Teuchos::RCP<NodalBasis<Real> > basisp ) :
234  gamma_(gamma),
235  u_targ_rcp_((Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(u_targ))).getVector() ),
236  basisp_(basisp), ni_(basisp_->ni_), nq_(basisp_->nq_) {}
237 
238  void update_gamma(Real gamma) { gamma_ = gamma; }
239  void update_target(const Vector<Real> &u_targ) {
240  u_targ_rcp_ = Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(u_targ)).getVector()); }
241 
243  template<class ScalarT>
244  ScalarT value(const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) {
245 
246  Teuchos::RCP<const std::vector<ScalarT> > up =
247  (Teuchos::dyn_cast<StdVector<ScalarT> >(const_cast<Vector<ScalarT> &>(u))).getVector();
248 
249  Teuchos::RCP<const std::vector<ScalarT> > zp =
250  (Teuchos::dyn_cast<StdVector<ScalarT> >(const_cast<Vector<ScalarT> &>(z))).getVector();
251 
252  ScalarT err_norm = 0;
253  ScalarT reg_norm = 0;
254 
255  // Numerically approximate the tracking and regularization norms
256  for(int j=0; j<nq_; ++j) {
257  ScalarT err_ptwise = 0;
258  ScalarT reg_ptwise = 0;
259  for(int i=0; i<ni_; ++i) {
260  err_ptwise += ((*up)[i]-(*u_targ_rcp_)[i])*(*basisp_->Lp_)[j+nq_*i];
261  reg_ptwise += (*zp)[i]*(*basisp_->Lp_)[j+nq_*i];
262  }
263  err_norm += 0.5*(*basisp_->wqp_)[j]*err_ptwise*err_ptwise;
264  reg_norm += 0.5*(*basisp_->wqp_)[j]*reg_ptwise*reg_ptwise;
265  }
266 
267  ScalarT J = err_norm + gamma_*reg_norm;
268 
269  return J;
270  }
271 };
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
QuadraticTracking(Real gamma, const Vector< Real > &u_targ, Teuchos::RCP< NodalBasis< Real > > basisp)
Teuchos::RCP< NodalBasis< Real > > basisp_
virtual void plus(const Vector &x)=0
Compute , where .
VectorFunction< Real, MinusZ > minusz_
Teuchos::LAPACK< int, Real > lapack_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
BVP_Constraint(const BoundaryValueProblem< Real > &bvp)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Inherit and add method for applying the inverse partial constraint Jacobian and its adjoint...
BoundaryValueProblem(Real xl, Real xr, Teuchos::RCP< NodalBasis< Real > > basisp)
Provides the std::vector implementation of the ROL::Vector interface.
void update_gamma(Real gamma)
VectorFunction< Real, ExpU > exp_
Objective to minimize .
Compute the constraint vector -u'' + exp(u) - z with natural boundary conditions. ...
VectorFunction< Real, DiffU > diff_
void value(Vector< ScalarT > &c, const Vector< ScalarT > &u, const Vector< ScalarT > &z, Real &tol)
Teuchos::RCP< std::vector< Real > > u_targ_rcp_
ScalarT value(const Vector< ScalarT > &u, const Vector< ScalarT > &z, Real &tol)
Compute .
void update_target(const Vector< Real > &u_targ)