ROL
ODEConstraint_TimeSimOpt.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 #ifndef __ODEConstraint_TimeSimOpt_hpp__
45 #define __ODEConstraint_TimeSimOpt_hpp__
46 
47 #include "Teuchos_oblackholestream.hpp"
48 #include "Teuchos_GlobalMPISession.hpp"
49 
51 #include "ROL_Vector.hpp"
53 #include "ROL_StdVector.hpp"
54 #include "ROL_RandomVector.hpp"
55 
56 typedef double RealT;
57 
58 template <typename Real>
60 private:
62 
63  const std::vector<Real> & getVector( const ROL::Vector<Real> & x ) {
64  return *dynamic_cast<const VectorType&>(x).getVector();
65  }
66 
67  std::vector<Real> & getVector( ROL::Vector<Real> & x ) {
68  return *dynamic_cast<VectorType&>(x).getVector();
69  }
70 
73 
74 public:
75 
76  ODE_Constraint(double dt,double omega) : timestep_(dt), omega_(omega) {}
77 
78  virtual void value(ROL::Vector<Real> &c,
79  const ROL::Vector<Real> &u_old,
80  const ROL::Vector<Real> &u_new,
81  const ROL::Vector<Real> &z,
82  Real &tol) override
83  {
84  auto & c_data = getVector(c);
85  auto & uo_data = getVector(u_old);
86  auto & un_data = getVector(u_new);
87  auto & z_data = getVector(z);
88 
89  // solving (u,v are states, z is control)
90  //
91  // u' = v + z, v'=-omega^2 * u, w' = sqrt(w)
92  // u(0) = 0
93  // v(0) = omega
94  // w(0) = 1.0
95  //
96  // u(t) = sin(omega*t)
97  // v(t) = omega*cos(omega*t)
98  // w(t) = 0.25*(-2.0*c1*t+c1**2+t**2), (init cond implies c1 = 2)
99  //
100  // using backward euler
101 
102  c_data[0] = un_data[0]-uo_data[0] - timestep_*(un_data[1] + z_data[0]);
103  c_data[1] = un_data[1]-uo_data[1] + timestep_*omega_*omega_*un_data[0];
104  c_data[2] = un_data[2]-uo_data[2] - timestep_*std::sqrt(un_data[2]); // throw in some nonlinear term
105  }
106 
107  virtual void solve(ROL::Vector<Real> &c,
108  const ROL::Vector<Real> &u_old, ROL::Vector<Real> &u_new,
109  const ROL::Vector<Real> &z,
110  Real &tol) override
111  {
112  auto & uo_data = getVector(u_old);
113  auto & un_data = getVector(u_new);
114  auto & z_data = getVector(z);
115 
117  Real b = timestep_;
118  Real gamma = (a*b+1.0);
119 
120  Real rhs_0 = uo_data[0]+timestep_*z_data[0];
121  Real rhs_1 = uo_data[1];
122 
123  un_data[0] = (1.0 * rhs_0 + b * rhs_1)/gamma;
124  un_data[1] = ( -a * rhs_0 + 1.0 * rhs_1)/gamma;
125 
126  // Newton's method
127  for(int i=0;i<5;i++) {
128  // safety check
129  TEUCHOS_ASSERT(un_data[2]>=0.0);
130 
131  double residual = un_data[2]-uo_data[2] - timestep_*std::sqrt(un_data[2]);
132  un_data[2] = un_data[2] - residual / (1.0-timestep_*(0.5/std::sqrt(un_data[2])));
133  }
134 
135  value(c,u_old,u_new,z,tol);
136  }
137 
139  const ROL::Vector<Real> &v_old,
140  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
141  const ROL::Vector<Real> &z,
142  Real &tol) override {
143  auto & jv_data = getVector(jv);
144  auto & vo_data = getVector(v_old);
145 
146  jv_data[0] = -vo_data[0];
147  jv_data[1] = -vo_data[1];
148  jv_data[2] = -vo_data[2];
149  }
150 
152  const ROL::Vector<Real> &v_new,
153  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
154  const ROL::Vector<Real> &z,
155  Real &tol) override {
156  auto & jv_data = getVector(jv);
157  auto & vn_data = getVector(v_new);
158  auto & un_data = getVector(u_new);
159 
160  jv_data[0] = vn_data[0] - timestep_*vn_data[1];
161  jv_data[1] = vn_data[1] + timestep_*omega_*omega_*vn_data[0];
162  // [ 1, -dt ]
163  // [ dt*w*w, 1 ]
164 
165  TEUCHOS_ASSERT(un_data[2]>=0.0);
166  jv_data[2] = (1.0 - timestep_*0.5/std::sqrt(un_data[2]))*vn_data[2];
167  }
168 
170  const ROL::Vector<Real> &v_new,
171  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
172  const ROL::Vector<Real> &z,
173  Real &tol) override {
174  auto & ijv_data = getVector(ijv);
175  auto & v_data = getVector(v_new);
176  auto & un_data = getVector(u_new);
177 
179  Real b = timestep_;
180  Real gamma = 1.0/(a*b+1.0);
181 
182  ijv_data[0] = gamma*(1.0 * v_data[0] + b * v_data[1]);
183  ijv_data[1] = gamma*( -a * v_data[0] + 1.0 * v_data[1]);
184 
185  TEUCHOS_ASSERT(un_data[2]>=0.0);
186  ijv_data[2] = v_data[2]/(1.0 - timestep_*0.5/std::sqrt(un_data[2]));
187  }
188 
190  const ROL::Vector<Real> &v_new,
191  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
192  const ROL::Vector<Real> &z,
193  Real &tol) override {
194  auto & jv_data = getVector(jv);
195  auto & vn_data = getVector(v_new);
196 
197  jv_data[0] = -timestep_*vn_data[0];
198  jv_data[1] = 0.0;
199  jv_data[2] = 0.0;
200  }
201 
203  const ROL::Vector<Real> &dualv,
204  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
205  const ROL::Vector<Real> &z,
206  Real &tol) override {
207  auto & ajv_data = getVector(ajv_old);
208  auto & v_data = getVector(dualv);
209 
210  ajv_data[0] = -v_data[0];
211  ajv_data[1] = -v_data[1];
212  ajv_data[2] = -v_data[2];
213  }
214 
216  const ROL::Vector<Real> &dualv,
217  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
218  const ROL::Vector<Real> &z,
219  Real &tol) override {
220 
221  auto & ajv_data = getVector(ajv_new);
222  auto & v_data = getVector(dualv);
223  auto & un_data = getVector(u_new);
224 
225  ajv_data[0] = v_data[0] + timestep_*omega_*omega_*v_data[1];
226  ajv_data[1] = v_data[1] - timestep_*v_data[0];
227 
228  TEUCHOS_ASSERT(un_data[2]>=0.0);
229  ajv_data[2] = (1.0 - timestep_*0.5/std::sqrt(un_data[2]))*v_data[2];
230  }
231 
233  const ROL::Vector<Real> &dualv,
234  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
235  const ROL::Vector<Real> &z,
236  Real &tol) override {
237  auto & ajv_data = getVector(ajv);
238  auto & v_data = getVector(dualv);
239 
240  ajv_data[0] = -timestep_*v_data[0];
241  }
242 
244  const ROL::Vector<Real> &v_new,
245  const ROL::Vector<Real> &u_old, const ROL::Vector<Real> &u_new,
246  const ROL::Vector<Real> &z,
247  Real &tol) override {
248  auto & iajv_data = getVector(iajv);
249  auto & v_data = getVector(v_new);
250  auto & un_data = getVector(u_new);
251 
253  Real b = timestep_;
254  Real gamma = 1.0/(a*b+1.0);
255 
256  iajv_data[0] = gamma*(1.0 * v_data[0] - a * v_data[1]);
257  iajv_data[1] = gamma*( b * v_data[0] + 1.0 * v_data[1]);
258 
259  TEUCHOS_ASSERT(un_data[2]>=0.0);
260  iajv_data[2] = v_data[2]/(1.0 - timestep_*0.5/std::sqrt(un_data[2]));
261  }
262 
264  const ROL::Vector<Real> &w,
265  const ROL::Vector<Real> &v_old,
266  const ROL::Vector<Real> &u_old,
267  const ROL::Vector<Real> &u_new,
268  const ROL::Vector<Real> &z,
269  Real &tol) override
270  {
271  auto & ahwv_data = getVector(ahwv_old);
272 
273  ahwv_data[0] = 0.0;
274  ahwv_data[1] = 0.0;
275  ahwv_data[2] = 0.0;
276  }
277 
279  const ROL::Vector<Real> &w,
280  const ROL::Vector<Real> &v_new,
281  const ROL::Vector<Real> &u_old,
282  const ROL::Vector<Real> &u_new,
283  const ROL::Vector<Real> &z,
284  Real &tol) override
285  {
286  auto & ahwv_data = getVector(ahwv_new);
287  auto & w_data = getVector(w);
288  auto & v_data = getVector(v_new);
289  auto & un_data = getVector(u_new);
290 
291  ahwv_data[0] = 0.0;
292  ahwv_data[1] = 0.0;
293  ahwv_data[2] = 0.25*timestep_*w_data[2]*v_data[2]/std::pow(un_data[2],1.5);
294  }
295 
296 };
297 
298 #endif
std::vector< Real > & getVector(ROL::Vector< Real > &x)
ODE_Constraint(double dt, double omega)
double RealT
virtual void applyJacobian_1_old(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v_old, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:78
Defines the time dependent constraint operator interface for simulation-based optimization.
virtual void applyAdjointJacobian_1_new(ROL::Vector< Real > &ajv_new, const ROL::Vector< Real > &dualv, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyAdjointHessian_11_old(ROL::Vector< Real > &ahwv_old, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v_old, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyAdjointJacobian_2_time(ROL::Vector< Real > &ajv, const ROL::Vector< Real > &dualv, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyAdjointHessian_11_new(ROL::Vector< Real > &ahwv_new, const ROL::Vector< Real > &w, const ROL::Vector< Real > &v_new, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyJacobian_2(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v_new, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyInverseJacobian_1_new(ROL::Vector< Real > &ijv, const ROL::Vector< Real > &v_new, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyJacobian_1_new(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v_new, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
ROL::StdVector< Real > VectorType
const std::vector< Real > & getVector(const ROL::Vector< Real > &x)
virtual void applyAdjointJacobian_1_old(ROL::Vector< Real > &ajv_old, const ROL::Vector< Real > &dualv, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void applyInverseAdjointJacobian_1_new(ROL::Vector< Real > &iajv, const ROL::Vector< Real > &v_new, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void solve(ROL::Vector< Real > &c, const ROL::Vector< Real > &u_old, ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
virtual void value(ROL::Vector< Real > &c, const ROL::Vector< Real > &u_old, const ROL::Vector< Real > &u_new, const ROL::Vector< Real > &z, Real &tol) override
Evaluate the constraint operator at .