// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_SACADO_EQUALITYCONSTRAINT_SIMOPT #define ROL_SACADO_EQUALITYCONSTRAINT_SIMOPT #include "Sacado.hpp" #include "ROL_StdVector.hpp" #include "ROL_Constraint_SimOpt.hpp" using namespace ROL; //! \brief ROL interface wrapper for Sacado SimOpt Constraint template class Constr> class Sacado_Constraint_SimOpt : public Constraint_SimOpt { protected: Constr constr_; template void applyJacobian_1AD(Vector &jv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyJacobian_2AD(Vector &jv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyAdjointJacobian_1AD(Vector &ajv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyAdjointJacobian_2AD(Vector &ajv, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyAdjointHessian_11AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyAdjointHessian_12AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyAdjointHessian_21AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol); template void applyAdjointHessian_22AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol); public: Sacado_Constraint_SimOpt() : constr_(Constr()) {} Sacado_Constraint_SimOpt(Constr constr) : constr_(constr) { } void value(Vector &c, const Vector &u, const Vector &z, Real &tol) { constr_.value(c,u,z,tol); } void applyJacobian_1(Vector &jv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->applyJacobian_1AD(jv,v,u,z,tol); } void applyJacobian_2(Vector &jv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->applyJacobian_2AD(jv,v,u,z,tol); } void applyAdjointJacobian_1(Vector &ajv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->applyAdjointJacobian_1AD(ajv,v,u,z,tol); } void applyAdjointJacobian_2(Vector &ajv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { this->applyAdjointJacobian_2AD(ajv,v,u,z,tol); } void applyAdjointHessian_11(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol){ this->applyAdjointHessian_11AD(ahwv,w,v,u,z,tol); } void applyAdjointHessian_12(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol){ this->applyAdjointHessian_12AD(ahwv,w,v,u,z,tol); } void applyAdjointHessian_21(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol){ this->applyAdjointHessian_21AD(ahwv,w,v,u,z,tol); } void applyAdjointHessian_22(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol){ this->applyAdjointHessian_22AD(ahwv,w,v,u,z,tol); } }; template class Constr> template void Sacado_Constraint_SimOpt::applyJacobian_1AD(Vector &jv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // v in U (n), jv in C (n) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); ROL::Ptr jvp = dynamic_cast(jv).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr c_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(n); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); for(int i=0; ipush_back(0); u_fad_ptr->push_back(FadType(n,i,(*up)[i])); } for(int j=0; jpush_back((*zp)[j]); } StdVector c_fad(c_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); // Evaluate constraint constr_.value(c_fad,u_fad,z_fad,tol); for(int i=0; i class Constr> template void Sacado_Constraint_SimOpt::applyJacobian_2AD(Vector &jv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // v in Z (m), jv in C (n) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr jvp = dynamic_cast(jv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr c_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(n); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); for(int i=0; ipush_back(0); u_fad_ptr->push_back((*up)[i]); } for(int j=0; jpush_back(FadType(n,j,(*zp)[j])); } StdVector c_fad(c_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); // Evaluate constraint constr_.value(c_fad,u_fad,z_fad,tol); for(int i=0; i class Constr> template void Sacado_Constraint_SimOpt::applyAdjointJacobian_1AD(Vector &ajv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // v in C* (n), ajv in U* (n) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr ajvp = dynamic_cast(ajv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr c_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(n); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); for(int i=0; ipush_back(0); u_fad_ptr->push_back(FadType(n,i,(*up)[i])); } for(int j=0; jpush_back((*zp)[j]); } StdVector c_fad(c_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); // Evaluate constraint constr_.value(c_fad,u_fad,z_fad,tol); FadType vdotc = 0; for(int i=0;i class Constr> template void Sacado_Constraint_SimOpt::applyAdjointJacobian_2AD(Vector &ajv, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // v in C* (n), ajv in Z* (m) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr ajvp = dynamic_cast(ajv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr c_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(n); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); for(int i=0; ipush_back(0); u_fad_ptr->push_back((*up)[i]); } for(int j=0; jpush_back(FadType(n,j,(*zp)[j])); } StdVector c_fad(c_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); // Evaluate constraint constr_.value(c_fad,u_fad,z_fad,tol); FadType vdotc = 0; for(int i=0;i class Constr> template void Sacado_Constraint_SimOpt::applyAdjointHessian_11AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // w in C* (n), v in U* (n), ahwv in U* (n) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr wp = dynamic_cast(w).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr v_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr jv_fad_ptr = ROL::makePtr(); v_fad_ptr->reserve(n); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); jv_fad_ptr->reserve(n); for(int i=0; ipush_back((*vp)[i]); u_fad_ptr->push_back(FadType(n,i,(*up)[i])); jv_fad_ptr->push_back(0); } for(int j=0; jpush_back((*zp)[j]); } StdVector v_fad(v_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector jv_fad(jv_fad_ptr); this->applyJacobian_1AD(jv_fad,v_fad,u_fad,z_fad,tol); FadType wjv_fad = 0; for(int i=0; i class Constr> template void Sacado_Constraint_SimOpt::applyAdjointHessian_12AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // w in C* (n), v in U* (n), ahwv in Z* (m) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr wp = dynamic_cast(w).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr v_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr jv_fad_ptr = ROL::makePtr(); v_fad_ptr->reserve(n); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); jv_fad_ptr->reserve(n); for(int i=0; ipush_back((*vp)[i]); u_fad_ptr->push_back((*up)[i]); jv_fad_ptr->push_back(0); } for(int j=0; jpush_back(FadType(m,j,(*zp)[j])); } StdVector v_fad(v_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector jv_fad(jv_fad_ptr); this->applyJacobian_1AD(jv_fad,v_fad,u_fad,z_fad,tol); FadType wjv_fad = 0; for(int i=0; i class Constr> template void Sacado_Constraint_SimOpt::applyAdjointHessian_21AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // w in C* (n), v in Z* (m), ahwv in U* (n) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr wp = dynamic_cast(w).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr v_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr jv_fad_ptr = ROL::makePtr(); v_fad_ptr->reserve(m); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); jv_fad_ptr->reserve(n); for(int i=0; ipush_back(FadType(1,(*up)[i])); jv_fad_ptr->push_back(0); } for(int j=0; jpush_back((*vp)[j]); z_fad_ptr->push_back((*zp)[j]); } StdVector v_fad(v_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector jv_fad(jv_fad_ptr); this->applyJacobian_2AD(jv_fad,v_fad,u_fad,z_fad,tol); FadType wjv_fad = 0; for(int i=0; i class Constr> template void Sacado_Constraint_SimOpt::applyAdjointHessian_22AD(Vector &ahwv, const Vector &w, const Vector &v, const Vector &u, const Vector &z, Real &tol) { // w in C* (n), v in Z* (m), ahwv in Z* (m) typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr wp = dynamic_cast(w).getVector(); ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr zp = dynamic_cast(z).getVector(); int n = up->size(); int m = zp->size(); ROL::Ptr v_fad_ptr = ROL::makePtr(); ROL::Ptr u_fad_ptr = ROL::makePtr(); ROL::Ptr z_fad_ptr = ROL::makePtr(); ROL::Ptr jv_fad_ptr = ROL::makePtr(); v_fad_ptr->reserve(m); u_fad_ptr->reserve(n); z_fad_ptr->reserve(m); jv_fad_ptr->reserve(n); for(int i=0; ipush_back((*up)[i]); jv_fad_ptr->push_back(0); } for(int j=0; jpush_back((*vp)[j]); z_fad_ptr->push_back(FadType(m,j,(*zp)[j])); } StdVector v_fad(v_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector z_fad(z_fad_ptr); StdVector jv_fad(jv_fad_ptr); this->applyJacobian_2AD(jv_fad,v_fad,u_fad,z_fad,tol); FadType wjv_fad = 0; for(int i=0; i