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