ROL
ROL_DynamicConstraintCheck.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 
45 #pragma once
46 #ifndef ROL_DYNAMICCONSTRAINTCHECK_HPP
47 #define ROL_DYNAMICCONSTRAINTCHECK_HPP
48 
50 #include "ROL_ValidateFunction.hpp"
51 #include <string>
52 
53 
54 namespace ROL {
55 
56 
57 template<typename Real>
59 
60  static void check( DynamicConstraint<Real>& con,
61  ValidateFunction<Real>& validator,
62  const Vector<Real>& uo,
63  const Vector<Real>& un,
64  const Vector<Real>& z,
65  const std::vector<std::string>& methods ) {
66 
67  auto con_check = make_check( con );
68  check( con_check, validator, uo, un, z, methods );
69  }
70 
71  static void check( DynamicConstraint<Real>& con,
72  ValidateFunction<Real>& validator,
73  const Vector<Real>& uo,
74  const Vector<Real>& un,
75  const Vector<Real>& z,
76  TimeStamp<Real>& timeStamp,
77  const std::vector<std::string>& methods ) {
78 
79  auto con_check = make_check( con, timeStamp );
80  check( con_check, validator, uo, un, z, methods );
81  }
82 
83  static void check( DynamicConstraint_CheckInterface<Real>& con_check,
84  ValidateFunction<Real>& validator,
85  const Vector<Real>& uo,
86  const Vector<Real>& un,
87  const Vector<Real>& z,
88  const std::vector<std::string>& methods ) {
89 
90  auto c = uo.clone();
91  auto vu = uo.clone();
92  auto vz = z.clone();
93  auto l = uo.dual().clone();
94 
95  c->randomize();
96  vu->randomize();
97  vz->randomize();
98  l->randomize();
99 
100  auto update_uo = con_check.update_uo( un, z );
101  auto update_un = con_check.update_un( uo, z );
102  auto update_z = con_check.update_z( un, uo );
103 
104  auto value_uo = con_check.value_uo( un, z );
105  auto value_un = con_check.value_un( uo, z );
106  auto value_z = con_check.value_z( uo, un );
107 
108  //-------------------------------------------------------------------------
109  // Check Jacobian components
110  if( std::find(methods.begin(),methods.end(),"applyJacobian_uo") != methods.end() ) {
111  auto J = con_check.jacobian_uo( un, z );
112  validator.derivative_check( value_uo, J, update_uo, *c, *vu, uo, "norm(J_uo*vec)" );
113  } //else os << "\napplyJacobian_uo not implemented.\n";
114 
115  if( std::find(methods.begin(),methods.end(),"applyJacobian_un") != methods.end() ) {
116  auto J = con_check.jacobian_un( uo, z );
117  validator.derivative_check( value_un, J, update_un, *c, *vu, un, "norm(J_un*vec)" );
118  } //else os << "\napplyJacobian_un not implemented.\n";
119 
120  if( std::find(methods.begin(),methods.end(),"applyJacobian_z") != methods.end() ) {
121  auto J = con_check.jacobian_z( uo, un );
122  validator.derivative_check( value_z, J, update_z, *c, *vz, z, "norm(J_z*vec)" );
123  } //else os << "\napplyJacobian_z not implemented.\n";
124 
125 
126  //-------------------------------------------------------------------------
127  // Check Adjoint Jacobian component consistencies
128  if( std::find(methods.begin(),methods.end(),"applyAdjointJacobian_uo") != methods.end() ) {
129  auto J = con_check.jacobian_uo( un, z );
130  auto aJ = con_check.adjointJacobian_uo( un, z );
131  validator.adjoint_consistency_check( J, aJ, update_uo, *c, *vu, uo,
132  "Jacobian with respect to uo", "J_uo");
133  } //else os << "\napplyAdjointJacobian_uo not implemented.\n";
134 
135  if( std::find(methods.begin(),methods.end(),"applyAdjointJacobian_un") != methods.end() ) {
136  auto J = con_check.jacobian_un( uo, z );
137  auto aJ = con_check.adjointJacobian_un( uo, z );
138  validator.adjoint_consistency_check( J, aJ, update_un, *c, *vu, un,
139  "Jacobian with respect to un", "J_un");
140  } //else os << "\napplyAdjointJacobian_un not implemented.\n";
141 
142  if( std::find(methods.begin(),methods.end(),"applyAdjointJacobian_z") != methods.end() ) {
143  auto J = con_check.jacobian_z( uo, un );
144  auto aJ = con_check.adjointJacobian_z( uo, un );
145  validator.adjoint_consistency_check( J, aJ, update_z, *vz, *c, z,
146  "Jacobian with respect to z", "J_z");
147  } //else os << "\napplyAdjointJacobian_z not implemented.\n";
148 
149 
150  //-------------------------------------------------------------------------
151  // Check inverses
152  if( std::find(methods.begin(),methods.end(),"solve") != methods.end() ) {
153  auto S = con_check.solve_un( uo, z );
154  validator.solve_check( S, value_un, update_un, *c, un, "Dynamic Constraint");
155  } //else os << "\nsolve not implemented.\n";
156 
157 
158  if( std::find(methods.begin(),methods.end(),"applyInverseJacobian_un") != methods.end() ) {
159  auto J = con_check.jacobian_un( uo, z );
160  auto iJ = con_check.inverseJacobian_un( uo, z );
161  validator.inverse_check( J, iJ, update_un, *vu, un,
162  "Jacobian with respect to un", "J_un");
163  } //else os << "\napplyInverseJacobian_un not implemented.\n";
164 
165 
166  if( std::find(methods.begin(),methods.end(),"applyInverseAdjointJacobian_un") != methods.end() ) {
167  auto aJ = con_check.adjointJacobian_un( uo, z );
168  auto iaJ = con_check.inverseAdjointJacobian_un( uo, z );
169  validator.inverse_check( aJ, iaJ, update_un, *vu, un,
170  "adjoint Jacobian with respect to un", "aJ_un");
171  } //else os << "\napplyInverseAdjointJacobian_un not implemented.\n";
172 
173 
174  //-------------------------------------------------------------------------
175  // Check Adjoint Hessian components
176  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_uo_uo") != methods.end() ) {
177  auto aJ = con_check.adjointJacobian_uo_uo( un, z );
178  auto aJl = fix_direction( aJ, *l );
179  auto aH = con_check.adjointHessian_uo_uo( un, z, *l );
180  validator.derivative_check( aJl, aH, update_uo, *c, *vu, uo, "H_uo_uo");
181  } //else os << "\napplyAdjointHessian_uo_uo not implemented.\n";
182 
183  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_uo_un") != methods.end() ) {
184  auto aJ = con_check.adjointJacobian_un_uo( un, z );
185  auto aJl = fix_direction( aJ, *l );
186  auto aH = con_check.adjointHessian_uo_un( un, z, *l );
187  validator.derivative_check( aJl, aH, update_uo, *c, *vu, uo, "H_uo_un");
188  } //else os << "\napplyAdjointHessian_uo_un not implemented.\n";
189 
190  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_uo_z") != methods.end() ) {
191  auto aJ = con_check.adjointJacobian_z_uo( un, z );
192  auto aJl = fix_direction( aJ, *l );
193  auto aH = con_check.adjointHessian_uo_z( un, z, *l );
194  validator.derivative_check( aJl, aH, update_uo, *vz, *vu, uo, "H_uo_z");
195  } //else os << "\napplyAdjointHessian_uo_z not implemented.\n";
196 
197 
198 
199  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_un_uo") != methods.end() ) {
200  auto aJ = con_check.adjointJacobian_uo_un( uo, z );
201  auto aJl = fix_direction( aJ, *l );
202  auto aH = con_check.adjointHessian_un_uo( uo, z, *l );
203  validator.derivative_check( aJl, aH, update_un, *c, *vu, un, "H_un_uo");
204  } //else os << "\napplyAdjointHessian_un_uo not implemented.\n";
205 
206  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_un_un") != methods.end() ) {
207  auto aJ = con_check.adjointJacobian_un_un( uo, z );
208  auto aJl = fix_direction( aJ, *l );
209  auto aH = con_check.adjointHessian_un_un( uo, z, *l );
210  validator.derivative_check( aJl, aH, update_un, *c, *vu, un, "H_un_un");
211  } //else os << "\napplyAdjointHessian_un_un not implemented.\n";
212 
213  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_un_z") != methods.end() ) {
214  auto aJ = con_check.adjointJacobian_z_un( uo, z );
215  auto aJl = fix_direction( aJ, *l );
216  auto aH = con_check.adjointHessian_un_z( un, z, *l );
217  validator.derivative_check( aJl, aH, update_un, *vz, *vu, un, "H_un_z");
218  } //else os << "\napplyAdjointHessian_uo_uo not implemented.\n";
219 
220 
221 
222  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_z_uo") != methods.end() ) {
223  auto aJ = con_check.adjointJacobian_uo_z( uo, un );
224  auto aJl = fix_direction( aJ, *l );
225  auto aH = con_check.adjointHessian_z_uo( uo, un, *l );
226  validator.derivative_check( aJl, aH, update_z, *c, *vz, z, "H_z_uo");
227  } //else os << "\napplyAdjointHessian_z_uo not implemented.\n";
228 
229  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_z_un") != methods.end() ) {
230  auto aJ = con_check.adjointJacobian_un_z( uo, un );
231  auto aJl = fix_direction( aJ, *l );
232  auto aH = con_check.adjointHessian_z_un( uo, un, *l );
233  validator.derivative_check( aJl, aH, update_z, *c, *vz, z, "H_z_un");
234  } //else os << "\napplyAdjointHessian_z_un not implemented.\n";
235 
236  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_z_z") != methods.end() ) {
237  auto aJ = con_check.adjointJacobian_z_z( uo, un );
238  auto aJl = fix_direction( aJ, *l );
239  auto aH = con_check.adjointHessian_z_z( uo, un, *l );
240  validator.derivative_check( aJl, aH, update_z, *vz, *vz, z, "H_z_z");
241  } //else os << "\napplyAdjointHessian_z_z not implemented.\n";
242 
243  } // check()
244 
245  static void check( DynamicConstraint<Real>& con,
246  ValidateFunction<Real>& validator,
247  const Vector<Real>& uo,
248  const Vector<Real>& un,
249  const Vector<Real>& z) {
250  std::vector<std::string> methods = {"applyJacobian_uo",
251  "applyJacobian_un",
252  "applyJacobian_z",
253  "applyAdjointJacobian_uo",
254  "applyAdjointJacobian_un",
255  "applyAdjointJacobian_z",
256  "solve",
257  "applyInverseJacobian_un",
258  "applyInverseAdjointJacobian_un",
259  "applyAdjointHessian_uo_uo",
260  "applyAdjointHessian_uo_un",
261  "applyAdjointHessian_uo_z",
262  "applyAdjointHessian_un_uo",
263  "applyAdjointHessian_un_un",
264  "applyAdjointHessian_un_z",
265  "applyAdjointHessian_z_uo",
266  "applyAdjointHessian_z_un",
267  "applyAdjointHessian_z_z"};
268  check(con, validator, uo, un, z, methods);
269  }
270 
271  static void check( DynamicConstraint<Real>& con,
272  ValidateFunction<Real>& validator,
273  const Vector<Real>& uo,
274  const Vector<Real>& un,
275  const Vector<Real>& z,
276  TimeStamp<Real> &ts) {
277  std::vector<std::string> methods = {"applyJacobian_uo",
278  "applyJacobian_un",
279  "applyJacobian_z",
280  "applyAdjointJacobian_uo",
281  "applyAdjointJacobian_un",
282  "applyAdjointJacobian_z",
283  "solve",
284  "applyInverseJacobian_un",
285  "applyInverseAdjointJacobian_un",
286  "applyAdjointHessian_uo_uo",
287  "applyAdjointHessian_uo_un",
288  "applyAdjointHessian_uo_z",
289  "applyAdjointHessian_un_uo",
290  "applyAdjointHessian_un_un",
291  "applyAdjointHessian_un_z",
292  "applyAdjointHessian_z_uo",
293  "applyAdjointHessian_z_un",
294  "applyAdjointHessian_z_z"};
295  check(con, validator, uo, un, z, ts, methods);
296  }
297 
298 
299 };
300 
301 } // namespace ROL
302 
303 #endif // ROL_DYNAMICCONSTRAINTCHECK_HPP
304 
305 
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:226
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Defines the time-dependent constraint operator interface for simulation-based optimization.
static void check(DynamicConstraint_CheckInterface< Real > &con_check, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, const std::vector< std::string > &methods)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
static void check(DynamicConstraint< Real > &con, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z)
static void check(DynamicConstraint< Real > &con, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, const std::vector< std::string > &methods)
static void check(DynamicConstraint< Real > &con, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, TimeStamp< Real > &timeStamp, const std::vector< std::string > &methods)
static void check(DynamicConstraint< Real > &con, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, TimeStamp< Real > &ts)
DynamicConstraint_CheckInterface< Real > make_check(DynamicConstraint< Real > &con)