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 
72  static void check( DynamicConstraint<Real>& con,
73  ValidateFunction<Real>& validator,
74  const Vector<Real>& uo,
75  const Vector<Real>& un,
76  const Vector<Real>& z,
77  TimeStamp<Real>& timeStamp,
78  const std::vector<std::string>& methods ) {
79 
80  auto con_check = make_check( con, timeStamp );
81  check( con_check, validator, uo, un, z, methods );
82  }
83 
84  static void check( DynamicConstraint_CheckInterface<Real>& con_check,
85  ValidateFunction<Real>& validator,
86  const Vector<Real>& uo,
87  const Vector<Real>& un,
88  const Vector<Real>& z,
89  const std::vector<std::string>& methods ) {
90 
91  auto c = uo.clone();
92  auto vu = uo.clone();
93  auto vz = z.clone();
94  auto l = uo.dual().clone();
95 
96  c->randomize();
97  vu->randomize();
98  vz->randomize();
99  l->randomize();
100 
101  auto update_uo = con_check.update_uo( un, z );
102  auto update_un = con_check.update_un( uo, z );
103  auto update_z = con_check.update_z( un, uo );
104 
105  auto value_uo = con_check.value_uo( un, z );
106  auto value_un = con_check.value_un( uo, z );
107  auto value_z = con_check.value_z( uo, un );
108 
109  //-------------------------------------------------------------------------
110  // Check Jacobian components
111  if( std::find(methods.begin(),methods.end(),"applyJacobian_uo") != methods.end() ) {
112  auto J = con_check.jacobian_uo( un, z );
113  validator.derivative_check( value_uo, J, update_uo, *c, *vu, uo, "norm(J_uo*vec)" );
114  } //else os << "\napplyJacobian_uo not implemented.\n";
115 
116  if( std::find(methods.begin(),methods.end(),"applyJacobian_un") != methods.end() ) {
117  auto J = con_check.jacobian_un( uo, z );
118  validator.derivative_check( value_un, J, update_un, *c, *vu, un, "norm(J_un*vec)" );
119  } //else os << "\napplyJacobian_un not implemented.\n";
120 
121  if( std::find(methods.begin(),methods.end(),"applyJacobian_z") != methods.end() ) {
122  auto J = con_check.jacobian_z( uo, un );
123  validator.derivative_check( value_z, J, update_z, *c, *vz, z, "norm(J_z*vec)" );
124  } //else os << "\napplyJacobian_z not implemented.\n";
125 
126 
127  //-------------------------------------------------------------------------
128  // Check Adjoint Jacobian component consistencies
129  if( std::find(methods.begin(),methods.end(),"applyAdjointJacobian_uo") != methods.end() ) {
130  auto J = con_check.jacobian_uo( un, z );
131  auto aJ = con_check.adjointJacobian_uo( un, z );
132  validator.adjoint_consistency_check( J, aJ, update_uo, *c, *vu, uo,
133  "Jacobian with respect to uo", "J_uo");
134  } //else os << "\napplyAdjointJacobian_uo not implemented.\n";
135 
136  if( std::find(methods.begin(),methods.end(),"applyAdjointJacobian_un") != methods.end() ) {
137  auto J = con_check.jacobian_un( uo, z );
138  auto aJ = con_check.adjointJacobian_un( uo, z );
139  validator.adjoint_consistency_check( J, aJ, update_un, *c, *vu, un,
140  "Jacobian with respect to un", "J_un");
141  } //else os << "\napplyAdjointJacobian_un not implemented.\n";
142 
143  if( std::find(methods.begin(),methods.end(),"applyAdjointJacobian_z") != methods.end() ) {
144  auto J = con_check.jacobian_z( uo, un );
145  auto aJ = con_check.adjointJacobian_z( uo, un );
146  validator.adjoint_consistency_check( J, aJ, update_z, *vz, *c, z,
147  "Jacobian with respect to z", "J_z");
148  } //else os << "\napplyAdjointJacobian_z not implemented.\n";
149 
150 
151  //-------------------------------------------------------------------------
152  // Check inverses
153  if( std::find(methods.begin(),methods.end(),"solve") != methods.end() ) {
154  auto S = con_check.solve_un( uo, z );
155  validator.solve_check( S, value_un, update_un, *c, un, "Dynamic Constraint");
156  } //else os << "\nsolve not implemented.\n";
157 
158 
159  if( std::find(methods.begin(),methods.end(),"applyInverseJacobian_un") != methods.end() ) {
160  auto J = con_check.jacobian_un( uo, z );
161  auto iJ = con_check.inverseJacobian_un( uo, z );
162  validator.inverse_check( J, iJ, update_un, *vu, un,
163  "Jacobian with respect to un", "J_un");
164  } //else os << "\napplyInverseJacobian_un not implemented.\n";
165 
166 
167  if( std::find(methods.begin(),methods.end(),"applyInverseAdjointJacobian_un") != methods.end() ) {
168  auto aJ = con_check.adjointJacobian_un( uo, z );
169  auto iaJ = con_check.inverseAdjointJacobian_un( uo, z );
170  validator.inverse_check( aJ, iaJ, update_un, *vu, un,
171  "adjoint Jacobian with respect to un", "aJ_un");
172  } //else os << "\napplyInverseAdjointJacobian_un not implemented.\n";
173 
174 
175  //-------------------------------------------------------------------------
176  // Check Adjoint Hessian components
177  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_uo_uo") != methods.end() ) {
178  auto aJ = con_check.adjointJacobian_uo_uo( un, z );
179  auto aJl = fix_direction( aJ, *l );
180  auto aH = con_check.adjointHessian_uo_uo( un, z, *l );
181  validator.derivative_check( aJl, aH, update_uo, *c, *vu, uo, "H_uo_uo");
182  } //else os << "\napplyAdjointHessian_uo_uo not implemented.\n";
183 
184  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_uo_un") != methods.end() ) {
185  auto aJ = con_check.adjointJacobian_un_uo( un, z );
186  auto aJl = fix_direction( aJ, *l );
187  auto aH = con_check.adjointHessian_uo_un( un, z, *l );
188  validator.derivative_check( aJl, aH, update_uo, *c, *vu, uo, "H_uo_un");
189  } //else os << "\napplyAdjointHessian_uo_un not implemented.\n";
190 
191  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_uo_z") != methods.end() ) {
192  auto aJ = con_check.adjointJacobian_z_uo( un, z );
193  auto aJl = fix_direction( aJ, *l );
194  auto aH = con_check.adjointHessian_uo_z( un, z, *l );
195  validator.derivative_check( aJl, aH, update_uo, *vz, *vu, uo, "H_uo_z");
196  } //else os << "\napplyAdjointHessian_uo_z not implemented.\n";
197 
198 
199 
200  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_un_uo") != methods.end() ) {
201  auto aJ = con_check.adjointJacobian_uo_un( uo, z );
202  auto aJl = fix_direction( aJ, *l );
203  auto aH = con_check.adjointHessian_un_uo( uo, z, *l );
204  validator.derivative_check( aJl, aH, update_un, *c, *vu, un, "H_un_uo");
205  } //else os << "\napplyAdjointHessian_un_uo not implemented.\n";
206 
207  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_un_un") != methods.end() ) {
208  auto aJ = con_check.adjointJacobian_un_un( uo, z );
209  auto aJl = fix_direction( aJ, *l );
210  auto aH = con_check.adjointHessian_un_un( uo, z, *l );
211  validator.derivative_check( aJl, aH, update_un, *c, *vu, un, "H_un_un");
212  } //else os << "\napplyAdjointHessian_un_un not implemented.\n";
213 
214  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_un_z") != methods.end() ) {
215  auto aJ = con_check.adjointJacobian_z_un( uo, z );
216  auto aJl = fix_direction( aJ, *l );
217  auto aH = con_check.adjointHessian_un_z( un, z, *l );
218  validator.derivative_check( aJl, aH, update_un, *vz, *vu, un, "H_un_z");
219  } //else os << "\napplyAdjointHessian_uo_uo not implemented.\n";
220 
221 
222 
223  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_z_uo") != methods.end() ) {
224  auto aJ = con_check.adjointJacobian_uo_z( uo, un );
225  auto aJl = fix_direction( aJ, *l );
226  auto aH = con_check.adjointHessian_z_uo( uo, un, *l );
227  validator.derivative_check( aJl, aH, update_z, *c, *vz, z, "H_z_uo");
228  } //else os << "\napplyAdjointHessian_z_uo not implemented.\n";
229 
230  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_z_un") != methods.end() ) {
231  auto aJ = con_check.adjointJacobian_un_z( uo, un );
232  auto aJl = fix_direction( aJ, *l );
233  auto aH = con_check.adjointHessian_z_un( uo, un, *l );
234  validator.derivative_check( aJl, aH, update_z, *c, *vz, z, "H_z_un");
235  } //else os << "\napplyAdjointHessian_z_un not implemented.\n";
236 
237  if( std::find(methods.begin(),methods.end(),"applyAdjointHessian_z_z") != methods.end() ) {
238  auto aJ = con_check.adjointJacobian_z_z( uo, un );
239  auto aJl = fix_direction( aJ, *l );
240  auto aH = con_check.adjointHessian_z_z( uo, un, *l );
241  validator.derivative_check( aJl, aH, update_z, *vz, *vz, z, "H_z_z");
242  } //else os << "\napplyAdjointHessian_z_z not implemented.\n";
243 
244  } // check()
245 
246  static void check( DynamicConstraint<Real>& con,
247  ValidateFunction<Real>& validator,
248  const Vector<Real>& uo,
249  const Vector<Real>& un,
250  const Vector<Real>& z ) {
251  std::vector<std::string> methods = {"applyJacobian_uo",
252  "applyJacobian_un",
253  "applyJacobian_z",
254  "applyAdjointJacobian_uo",
255  "applyAdjointJacobian_un",
256  "applyAdjointJacobian_z",
257  "solve",
258  "applyInverseJacobian_un",
259  "applyInverseAdjointJacobian_un",
260  "applyAdjointHessian_uo_uo",
261  "applyAdjointHessian_uo_un",
262  "applyAdjointHessian_uo_z",
263  "applyAdjointHessian_un_uo",
264  "applyAdjointHessian_un_un",
265  "applyAdjointHessian_un_z",
266  "applyAdjointHessian_z_uo",
267  "applyAdjointHessian_z_un",
268  "applyAdjointHessian_z_z"};
269  check(con, validator, uo, un, z, methods);
270  }
271 
272 
273 };
274 
275 } // namespace ROL
276 
277 #endif // ROL_DYNAMICCONSTRAINTCHECK_HPP
278 
279 
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)
DynamicConstraint_CheckInterface< Real > make_check(DynamicConstraint< Real > &con)