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