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