| 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 |
#ifndef ROL_SACADO_EQUALITYCONSTRAINT_SIMOPT |
| 11 |
#define ROL_SACADO_EQUALITYCONSTRAINT_SIMOPT |
| 12 |
|
| 13 |
#include "Sacado.hpp" |
| 14 |
#include "ROL_StdVector.hpp" |
| 15 |
#include "ROL_Constraint_SimOpt.hpp" |
| 16 |
|
| 17 |
using namespace ROL; |
| 18 |
|
| 19 |
//! \brief ROL interface wrapper for Sacado SimOpt Constraint |
| 20 |
template<class Real, template<class> class Constr> |
| 21 |
class Sacado_Constraint_SimOpt : public Constraint_SimOpt<Real> { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected: |
| 26 |
Constr<Real> constr_; |
| 27 |
|
| 28 |
template<class ScalarT> |
| 29 |
void applyJacobian_1AD(Vector<ScalarT> &jv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 30 |
const Vector<ScalarT> &z, Real &tol); |
| 31 |
|
| 32 |
template<class ScalarT> |
| 33 |
void applyJacobian_2AD(Vector<ScalarT> &jv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 34 |
const Vector<ScalarT> &z, Real &tol); |
| 35 |
|
| 36 |
template<class ScalarT> |
| 37 |
void applyAdjointJacobian_1AD(Vector<ScalarT> &ajv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 38 |
const Vector<ScalarT> &z, Real &tol); |
| 39 |
|
| 40 |
template<class ScalarT> |
| 41 |
void applyAdjointJacobian_2AD(Vector<ScalarT> &ajv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 42 |
const Vector<ScalarT> &z, Real &tol); |
| 43 |
|
| 44 |
template<class ScalarT> |
| 45 |
void applyAdjointHessian_11AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 46 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 47 |
const Vector<ScalarT> &z, Real &tol); |
| 48 |
template<class ScalarT> |
| 49 |
void applyAdjointHessian_12AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 50 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 51 |
const Vector<ScalarT> &z, Real &tol); |
| 52 |
template<class ScalarT> |
| 53 |
void applyAdjointHessian_21AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 54 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 55 |
const Vector<ScalarT> &z, Real &tol); |
| 56 |
template<class ScalarT> |
| 57 |
void applyAdjointHessian_22AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 58 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 59 |
const Vector<ScalarT> &z, Real &tol); |
| 60 |
|
| 61 |
public: |
| 62 |
Sacado_Constraint_SimOpt() : constr_(Constr<Real>()) {} |
| 63 |
Sacado_Constraint_SimOpt(Constr<Real> constr) : constr_(constr) { } |
| 64 |
|
| 65 |
void value(Vector<Real> &c, const Vector<Real> &u, const Vector<Real> &z, Real &tol) { |
| 66 |
constr_.value(c,u,z,tol); |
| 67 |
} |
| 68 |
|
| 69 |
void applyJacobian_1(Vector<Real> &jv, const Vector<Real> &v, const Vector<Real> &u, |
| 70 |
const Vector<Real> &z, Real &tol) { |
| 71 |
this->applyJacobian_1AD(jv,v,u,z,tol); |
| 72 |
} |
| 73 |
|
| 74 |
void applyJacobian_2(Vector<Real> &jv, const Vector<Real> &v, const Vector<Real> &u, |
| 75 |
const Vector<Real> &z, Real &tol) { |
| 76 |
this->applyJacobian_2AD(jv,v,u,z,tol); |
| 77 |
} |
| 78 |
|
| 79 |
void applyAdjointJacobian_1(Vector<Real> &ajv, const Vector<Real> &v, const Vector<Real> &u, |
| 80 |
const Vector<Real> &z, Real &tol) { |
| 81 |
this->applyAdjointJacobian_1AD(ajv,v,u,z,tol); |
| 82 |
} |
| 83 |
|
| 84 |
void applyAdjointJacobian_2(Vector<Real> &ajv, const Vector<Real> &v, const Vector<Real> &u, |
| 85 |
const Vector<Real> &z, Real &tol) { |
| 86 |
this->applyAdjointJacobian_2AD(ajv,v,u,z,tol); |
| 87 |
} |
| 88 |
|
| 89 |
void applyAdjointHessian_11(Vector<Real> &ahwv, const Vector<Real> &w, const Vector<Real> &v, |
| 90 |
const Vector<Real> &u, const Vector<Real> &z, Real &tol){ |
| 91 |
this->applyAdjointHessian_11AD(ahwv,w,v,u,z,tol); |
| 92 |
} |
| 93 |
|
| 94 |
void applyAdjointHessian_12(Vector<Real> &ahwv, const Vector<Real> &w, const Vector<Real> &v, |
| 95 |
const Vector<Real> &u, const Vector<Real> &z, Real &tol){ |
| 96 |
this->applyAdjointHessian_12AD(ahwv,w,v,u,z,tol); |
| 97 |
} |
| 98 |
|
| 99 |
void applyAdjointHessian_21(Vector<Real> &ahwv, const Vector<Real> &w, const Vector<Real> &v, |
| 100 |
const Vector<Real> &u, const Vector<Real> &z, Real &tol){ |
| 101 |
this->applyAdjointHessian_21AD(ahwv,w,v,u,z,tol); |
| 102 |
} |
| 103 |
|
| 104 |
void applyAdjointHessian_22(Vector<Real> &ahwv, const Vector<Real> &w, const Vector<Real> &v, |
| 105 |
const Vector<Real> &u, const Vector<Real> &z, Real &tol){ |
| 106 |
this->applyAdjointHessian_22AD(ahwv,w,v,u,z,tol); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
template<class Real, template<class> class Constr> |
| 113 |
template<class ScalarT> |
| 114 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyJacobian_1AD(Vector<ScalarT> &jv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 115 |
const Vector<ScalarT> &z, Real &tol) { |
| 116 |
// v in U (n), jv in C (n) |
| 117 |
|
| 118 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 119 |
typedef std::vector<FadType> Fadvector; |
| 120 |
typedef std::vector<ScalarT> vector; |
| 121 |
typedef StdVector<ScalarT> SV; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 127 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 128 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 129 |
ROL::Ptr<vector> jvp = dynamic_cast<SV&>(jv).getVector(); |
| 130 |
|
| 131 |
int n = up->size(); |
| 132 |
int m = zp->size(); |
| 133 |
|
| 134 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 135 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 136 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 137 |
|
| 138 |
c_fad_ptr->reserve(n); |
| 139 |
u_fad_ptr->reserve(n); |
| 140 |
z_fad_ptr->reserve(m); |
| 141 |
|
| 142 |
for(int i=0; i<n; ++i) { |
| 143 |
c_fad_ptr->push_back(0); |
| 144 |
u_fad_ptr->push_back(FadType(n,i,(*up)[i])); |
| 145 |
} |
| 146 |
|
| 147 |
for(int j=0; j<m; ++j) { |
| 148 |
z_fad_ptr->push_back((*zp)[j]); |
| 149 |
} |
| 150 |
|
| 151 |
StdVector<FadType> c_fad(c_fad_ptr); |
| 152 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 153 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 154 |
|
| 155 |
// Evaluate constraint |
| 156 |
constr_.value(c_fad,u_fad,z_fad,tol); |
| 157 |
|
| 158 |
for(int i=0; i<n; ++i) { |
| 159 |
(*jvp)[i] = 0; |
| 160 |
for(int j=0; j<n; ++j) { |
| 161 |
(*jvp)[i] += (*vp)[j]*(*c_fad_ptr)[i].dx(j); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
template<class Real, template<class> class Constr> |
| 168 |
template<class ScalarT> |
| 169 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyJacobian_2AD(Vector<ScalarT> &jv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 170 |
const Vector<ScalarT> &z, Real &tol) { |
| 171 |
// v in Z (m), jv in C (n) |
| 172 |
|
| 173 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 174 |
typedef std::vector<FadType> Fadvector; |
| 175 |
typedef std::vector<ScalarT> vector; |
| 176 |
typedef StdVector<ScalarT> SV; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
ROL::Ptr<vector> jvp = dynamic_cast<SV&>(jv).getVector(); |
| 182 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 183 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 184 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 185 |
|
| 186 |
int n = up->size(); |
| 187 |
int m = zp->size(); |
| 188 |
|
| 189 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 190 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 191 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 192 |
|
| 193 |
c_fad_ptr->reserve(n); |
| 194 |
u_fad_ptr->reserve(n); |
| 195 |
z_fad_ptr->reserve(m); |
| 196 |
|
| 197 |
for(int i=0; i<n; ++i) { |
| 198 |
c_fad_ptr->push_back(0); |
| 199 |
u_fad_ptr->push_back((*up)[i]); |
| 200 |
} |
| 201 |
|
| 202 |
for(int j=0; j<m; ++j) { |
| 203 |
z_fad_ptr->push_back(FadType(n,j,(*zp)[j])); |
| 204 |
} |
| 205 |
|
| 206 |
StdVector<FadType> c_fad(c_fad_ptr); |
| 207 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 208 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 209 |
|
| 210 |
// Evaluate constraint |
| 211 |
constr_.value(c_fad,u_fad,z_fad,tol); |
| 212 |
|
| 213 |
for(int i=0; i<n; ++i) { |
| 214 |
(*jvp)[i] = 0; |
| 215 |
for(int j=0; j<n; ++j) { |
| 216 |
(*jvp)[i] += (*vp)[j]*(*c_fad_ptr)[i].dx(j); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
template<class Real, template<class> class Constr> |
| 224 |
template<class ScalarT> |
| 225 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyAdjointJacobian_1AD(Vector<ScalarT> &ajv, const Vector<ScalarT> &v, |
| 226 |
const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) { |
| 227 |
|
| 228 |
// v in C* (n), ajv in U* (n) |
| 229 |
|
| 230 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 231 |
typedef std::vector<FadType> Fadvector; |
| 232 |
typedef std::vector<ScalarT> vector; |
| 233 |
typedef StdVector<ScalarT> SV; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
ROL::Ptr<vector> ajvp = dynamic_cast<SV&>(ajv).getVector(); |
| 239 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 240 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 241 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 242 |
|
| 243 |
int n = up->size(); |
| 244 |
int m = zp->size(); |
| 245 |
|
| 246 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 247 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 248 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 249 |
|
| 250 |
c_fad_ptr->reserve(n); |
| 251 |
u_fad_ptr->reserve(n); |
| 252 |
z_fad_ptr->reserve(m); |
| 253 |
|
| 254 |
for(int i=0; i<n; ++i) { |
| 255 |
c_fad_ptr->push_back(0); |
| 256 |
u_fad_ptr->push_back(FadType(n,i,(*up)[i])); |
| 257 |
} |
| 258 |
|
| 259 |
for(int j=0; j<m; ++j) { |
| 260 |
z_fad_ptr->push_back((*zp)[j]); |
| 261 |
} |
| 262 |
|
| 263 |
StdVector<FadType> c_fad(c_fad_ptr); |
| 264 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 265 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 266 |
|
| 267 |
// Evaluate constraint |
| 268 |
constr_.value(c_fad,u_fad,z_fad,tol); |
| 269 |
|
| 270 |
FadType vdotc = 0; |
| 271 |
|
| 272 |
for(int i=0;i<n;++i) { |
| 273 |
vdotc += (*c_fad_ptr)[i]*(*vp)[i]; |
| 274 |
} |
| 275 |
|
| 276 |
for(int i=0;i<n;++i) { |
| 277 |
(*ajvp)[i] = vdotc.dx(i); |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
template<class Real, template<class> class Constr> |
| 285 |
template<class ScalarT> |
| 286 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyAdjointJacobian_2AD(Vector<ScalarT> &ajv, const Vector<ScalarT> &v, |
| 287 |
const Vector<ScalarT> &u, const Vector<ScalarT> &z, |
| 288 |
Real &tol) { |
| 289 |
// v in C* (n), ajv in Z* (m) |
| 290 |
|
| 291 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 292 |
typedef std::vector<FadType> Fadvector; |
| 293 |
typedef std::vector<ScalarT> vector; |
| 294 |
typedef StdVector<ScalarT> SV; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
ROL::Ptr<vector> ajvp = dynamic_cast<SV&>(ajv).getVector(); |
| 300 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 301 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 302 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 303 |
|
| 304 |
int n = up->size(); |
| 305 |
int m = zp->size(); |
| 306 |
|
| 307 |
ROL::Ptr<Fadvector> c_fad_ptr = ROL::makePtr<Fadvector>(); |
| 308 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 309 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 310 |
|
| 311 |
c_fad_ptr->reserve(n); |
| 312 |
u_fad_ptr->reserve(n); |
| 313 |
z_fad_ptr->reserve(m); |
| 314 |
|
| 315 |
for(int i=0; i<n; ++i) { |
| 316 |
c_fad_ptr->push_back(0); |
| 317 |
u_fad_ptr->push_back((*up)[i]); |
| 318 |
} |
| 319 |
|
| 320 |
for(int j=0; j<m; ++j) { |
| 321 |
z_fad_ptr->push_back(FadType(n,j,(*zp)[j])); |
| 322 |
} |
| 323 |
|
| 324 |
StdVector<FadType> c_fad(c_fad_ptr); |
| 325 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 326 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 327 |
|
| 328 |
// Evaluate constraint |
| 329 |
constr_.value(c_fad,u_fad,z_fad,tol); |
| 330 |
|
| 331 |
FadType vdotc = 0; |
| 332 |
|
| 333 |
for(int i=0;i<n;++i) { |
| 334 |
vdotc += (*c_fad_ptr)[i]*(*vp)[i]; |
| 335 |
} |
| 336 |
|
| 337 |
for(int j=0;j<m;++j) { |
| 338 |
(*ajvp)[j] = vdotc.dx(j); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
template<class Real, template<class> class Constr> |
| 345 |
template<class ScalarT> |
| 346 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyAdjointHessian_11AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 347 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 348 |
const Vector<ScalarT> &z, Real &tol) { |
| 349 |
// w in C* (n), v in U* (n), ahwv in U* (n) |
| 350 |
|
| 351 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 352 |
typedef std::vector<FadType> Fadvector; |
| 353 |
typedef std::vector<ScalarT> vector; |
| 354 |
typedef StdVector<ScalarT> SV; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
ROL::Ptr<vector> ahwvp = dynamic_cast<SV&>(ahwv).getVector(); |
| 360 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 361 |
ROL::Ptr<const vector> wp = dynamic_cast<const SV&>(w).getVector(); |
| 362 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 363 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 364 |
|
| 365 |
int n = up->size(); |
| 366 |
int m = zp->size(); |
| 367 |
|
| 368 |
ROL::Ptr<Fadvector> v_fad_ptr = ROL::makePtr<Fadvector>(); |
| 369 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 370 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 371 |
ROL::Ptr<Fadvector> jv_fad_ptr = ROL::makePtr<Fadvector>(); |
| 372 |
|
| 373 |
v_fad_ptr->reserve(n); |
| 374 |
u_fad_ptr->reserve(n); |
| 375 |
z_fad_ptr->reserve(m); |
| 376 |
jv_fad_ptr->reserve(n); |
| 377 |
|
| 378 |
for(int i=0; i<n; ++i) { |
| 379 |
v_fad_ptr->push_back((*vp)[i]); |
| 380 |
u_fad_ptr->push_back(FadType(n,i,(*up)[i])); |
| 381 |
jv_fad_ptr->push_back(0); |
| 382 |
} |
| 383 |
|
| 384 |
for(int j=0; j<m; ++j) { |
| 385 |
z_fad_ptr->push_back((*zp)[j]); |
| 386 |
} |
| 387 |
|
| 388 |
StdVector<FadType> v_fad(v_fad_ptr); |
| 389 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 390 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 391 |
StdVector<FadType> jv_fad(jv_fad_ptr); |
| 392 |
|
| 393 |
this->applyJacobian_1AD(jv_fad,v_fad,u_fad,z_fad,tol); |
| 394 |
|
| 395 |
FadType wjv_fad = 0; |
| 396 |
|
| 397 |
for(int i=0; i<n; ++i) { |
| 398 |
wjv_fad += (*wp)[i]*(*jv_fad_ptr)[i]; |
| 399 |
} |
| 400 |
|
| 401 |
for(int i=0; i<n; ++i) { |
| 402 |
(*ahwvp)[i] = wjv_fad.dx(i); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
template<class Real, template<class> class Constr> |
| 410 |
template<class ScalarT> |
| 411 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyAdjointHessian_12AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 412 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 413 |
const Vector<ScalarT> &z, Real &tol) { |
| 414 |
// w in C* (n), v in U* (n), ahwv in Z* (m) |
| 415 |
|
| 416 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 417 |
typedef std::vector<FadType> Fadvector; |
| 418 |
typedef std::vector<ScalarT> vector; |
| 419 |
typedef StdVector<ScalarT> SV; |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
ROL::Ptr<vector> ahwvp = dynamic_cast<SV&>(ahwv).getVector(); |
| 425 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 426 |
ROL::Ptr<const vector> wp = dynamic_cast<const SV&>(w).getVector(); |
| 427 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 428 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 429 |
|
| 430 |
int n = up->size(); |
| 431 |
int m = zp->size(); |
| 432 |
|
| 433 |
ROL::Ptr<Fadvector> v_fad_ptr = ROL::makePtr<Fadvector>(); |
| 434 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 435 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 436 |
ROL::Ptr<Fadvector> jv_fad_ptr = ROL::makePtr<Fadvector>(); |
| 437 |
|
| 438 |
v_fad_ptr->reserve(n); |
| 439 |
u_fad_ptr->reserve(n); |
| 440 |
z_fad_ptr->reserve(m); |
| 441 |
jv_fad_ptr->reserve(n); |
| 442 |
|
| 443 |
for(int i=0; i<n; ++i) { |
| 444 |
v_fad_ptr->push_back((*vp)[i]); |
| 445 |
u_fad_ptr->push_back((*up)[i]); |
| 446 |
jv_fad_ptr->push_back(0); |
| 447 |
} |
| 448 |
|
| 449 |
for(int j=0; j<m; ++j) { |
| 450 |
z_fad_ptr->push_back(FadType(m,j,(*zp)[j])); |
| 451 |
} |
| 452 |
|
| 453 |
StdVector<FadType> v_fad(v_fad_ptr); |
| 454 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 455 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 456 |
StdVector<FadType> jv_fad(jv_fad_ptr); |
| 457 |
|
| 458 |
this->applyJacobian_1AD(jv_fad,v_fad,u_fad,z_fad,tol); |
| 459 |
|
| 460 |
FadType wjv_fad = 0; |
| 461 |
|
| 462 |
for(int i=0; i<n; ++i) { |
| 463 |
wjv_fad += (*wp)[i]*(*jv_fad_ptr)[i]; |
| 464 |
} |
| 465 |
|
| 466 |
for(int j=0; j<m; ++j) { |
| 467 |
(*ahwvp)[j] = wjv_fad.dx(j); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
template<class Real, template<class> class Constr> |
| 473 |
template<class ScalarT> |
| 474 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyAdjointHessian_21AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 475 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 476 |
const Vector<ScalarT> &z, Real &tol) { |
| 477 |
// w in C* (n), v in Z* (m), ahwv in U* (n) |
| 478 |
|
| 479 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 480 |
typedef std::vector<FadType> Fadvector; |
| 481 |
typedef std::vector<ScalarT> vector; |
| 482 |
typedef StdVector<ScalarT> SV; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
ROL::Ptr<vector> ahwvp = dynamic_cast<SV&>(ahwv).getVector(); |
| 488 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 489 |
ROL::Ptr<const vector> wp = dynamic_cast<const SV&>(w).getVector(); |
| 490 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 491 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 492 |
|
| 493 |
int n = up->size(); |
| 494 |
int m = zp->size(); |
| 495 |
|
| 496 |
ROL::Ptr<Fadvector> v_fad_ptr = ROL::makePtr<Fadvector>(); |
| 497 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 498 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 499 |
ROL::Ptr<Fadvector> jv_fad_ptr = ROL::makePtr<Fadvector>(); |
| 500 |
|
| 501 |
v_fad_ptr->reserve(m); |
| 502 |
u_fad_ptr->reserve(n); |
| 503 |
z_fad_ptr->reserve(m); |
| 504 |
jv_fad_ptr->reserve(n); |
| 505 |
|
| 506 |
for(int i=0; i<n; ++i) { |
| 507 |
u_fad_ptr->push_back(FadType(1,(*up)[i])); |
| 508 |
jv_fad_ptr->push_back(0); |
| 509 |
} |
| 510 |
|
| 511 |
for(int j=0; j<m; ++j) { |
| 512 |
v_fad_ptr->push_back((*vp)[j]); |
| 513 |
z_fad_ptr->push_back((*zp)[j]); |
| 514 |
} |
| 515 |
|
| 516 |
StdVector<FadType> v_fad(v_fad_ptr); |
| 517 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 518 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 519 |
StdVector<FadType> jv_fad(jv_fad_ptr); |
| 520 |
|
| 521 |
this->applyJacobian_2AD(jv_fad,v_fad,u_fad,z_fad,tol); |
| 522 |
|
| 523 |
FadType wjv_fad = 0; |
| 524 |
|
| 525 |
for(int i=0; i<n; ++i) { |
| 526 |
wjv_fad += (*wp)[i]*(*jv_fad_ptr)[i]; |
| 527 |
} |
| 528 |
|
| 529 |
for(int i=0; i<n; ++i) { |
| 530 |
(*ahwvp)[i] = wjv_fad.dx(i); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
template<class Real, template<class> class Constr> |
| 536 |
template<class ScalarT> |
| 537 |
void Sacado_Constraint_SimOpt<Real,Constr>::applyAdjointHessian_22AD(Vector<ScalarT> &ahwv, const Vector<ScalarT> &w, |
| 538 |
const Vector<ScalarT> &v, const Vector<ScalarT> &u, |
| 539 |
const Vector<ScalarT> &z, Real &tol) { |
| 540 |
// w in C* (n), v in Z* (m), ahwv in Z* (m) |
| 541 |
|
| 542 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 543 |
typedef std::vector<FadType> Fadvector; |
| 544 |
typedef std::vector<ScalarT> vector; |
| 545 |
typedef StdVector<ScalarT> SV; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
ROL::Ptr<vector> ahwvp = dynamic_cast<SV&>(ahwv).getVector(); |
| 551 |
ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector(); |
| 552 |
ROL::Ptr<const vector> wp = dynamic_cast<const SV&>(w).getVector(); |
| 553 |
ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector(); |
| 554 |
ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector(); |
| 555 |
|
| 556 |
int n = up->size(); |
| 557 |
int m = zp->size(); |
| 558 |
|
| 559 |
ROL::Ptr<Fadvector> v_fad_ptr = ROL::makePtr<Fadvector>(); |
| 560 |
ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>(); |
| 561 |
ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>(); |
| 562 |
ROL::Ptr<Fadvector> jv_fad_ptr = ROL::makePtr<Fadvector>(); |
| 563 |
|
| 564 |
v_fad_ptr->reserve(m); |
| 565 |
u_fad_ptr->reserve(n); |
| 566 |
z_fad_ptr->reserve(m); |
| 567 |
jv_fad_ptr->reserve(n); |
| 568 |
|
| 569 |
for(int i=0; i<n; ++i) { |
| 570 |
u_fad_ptr->push_back((*up)[i]); |
| 571 |
jv_fad_ptr->push_back(0); |
| 572 |
} |
| 573 |
|
| 574 |
for(int j=0; j<m; ++j) { |
| 575 |
v_fad_ptr->push_back((*vp)[j]); |
| 576 |
z_fad_ptr->push_back(FadType(m,j,(*zp)[j])); |
| 577 |
} |
| 578 |
|
| 579 |
StdVector<FadType> v_fad(v_fad_ptr); |
| 580 |
StdVector<FadType> u_fad(u_fad_ptr); |
| 581 |
StdVector<FadType> z_fad(z_fad_ptr); |
| 582 |
StdVector<FadType> jv_fad(jv_fad_ptr); |
| 583 |
|
| 584 |
this->applyJacobian_2AD(jv_fad,v_fad,u_fad,z_fad,tol); |
| 585 |
|
| 586 |
FadType wjv_fad = 0; |
| 587 |
|
| 588 |
for(int i=0; i<n; ++i) { |
| 589 |
wjv_fad += (*wp)[i]*(*jv_fad_ptr)[i]; |
| 590 |
} |
| 591 |
|
| 592 |
for(int j=0; j<m; ++j) { |
| 593 |
(*ahwvp)[j] = wjv_fad.dx(j); |
| 594 |
|
| 595 |
} |
| 596 |
} |
| 597 |
#endif |