// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_SACADO_EQUALITYCONSTRAINT #define ROL_SACADO_EQUALITYCONSTRAINT #include "Sacado.hpp" #include "ROL_StdVector.hpp" #include "ROL_Constraint.hpp" namespace ROL { //! \brief ROL interface wrapper for Sacado Constraint template class Constr> class Sacado_Constraint : public Constraint { protected: Constr constr_; int dim_; template void applyJacobianAD( Vector &jv, const Vector &v, const Vector &x, Real &tol ); template void applyAdjointJacobianAD( Vector &aju, const Vector &u, const Vector &x, Real &tol); template void applyAdjointHessianAD( Vector &ahuv, const Vector &u, const Vector &v, const Vector &x, Real &tol); public: Sacado_Constraint(int dim) : dim_(dim) {} Sacado_Constraint(const Constr & constr) : constr_(constr) {} virtual void value(Vector &c, const Vector &x, Real &tol) { constr_.value(c,x,tol); } virtual void applyJacobian(Vector &jv, const Vector &v, const Vector &x, Real &tol) { this->applyJacobianAD(jv,v,x,tol); } virtual void applyAdjointJacobian(Vector &aju, const Vector &u, const Vector &x, Real &tol) { this->applyAdjointJacobianAD(aju,u,x,tol); } void applyAdjointHessian(Vector &ahuv, const Vector &u, const Vector &v, const Vector &x, Real &tol){ this->applyAdjointHessianAD(ahuv,u,v,x,tol); } }; template class Constr> template void Sacado_Constraint::applyJacobianAD(Vector &jv, const Vector &v, const Vector &x, Real &tol) { // Data type which supports automatic differentiation typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; ROL::Ptr xp = dynamic_cast(x).getVector(); int n = xp->size(); // Get a pointer to the direction vector ROL::Ptr vp = dynamic_cast(v).getVector(); ROL::Ptr jvp = dynamic_cast(jv).getVector(); // Create a vector of independent variables ROL::Ptr x_fad_ptr = ROL::makePtr(); x_fad_ptr->reserve(n); // Initialize constructor for each element for(int i=0; ipush_back(FadType(n,i,(*xp)[i])); } // Create a vector of independent variables ROL::Ptr c_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(dim_); for(int j=0; jpush_back(0); } StdVector x_fad(x_fad_ptr); StdVector c_fad(c_fad_ptr); // Evaluate constraint constr_.value(c_fad,x_fad,tol); for(int i=0; i class Constr> template void Sacado_Constraint::applyAdjointJacobianAD(Vector &aju, const Vector &u, const Vector &x, Real &tol) { // Data type which supports automatic differentiation typedef Sacado::Fad::DFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; // Get a pointer to the optimization vector ROL::Ptr xp = dynamic_cast(x).getVector(); // Get a pointer to the direction vector ROL::Ptr up = dynamic_cast(u).getVector(); ROL::Ptr ajup = dynamic_cast(aju).getVector(); int n = xp->size(); // Create a vector of independent variables ROL::Ptr x_fad_ptr = ROL::makePtr(); x_fad_ptr->reserve(n); // Initialize constructor for each element for(int i=0; ipush_back(FadType(n,i,(*xp)[i])); } ROL::Ptr c_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(dim_); for(int j=0; jpush_back(0); } StdVector x_fad(x_fad_ptr); StdVector c_fad(c_fad_ptr); // Evaluate constraint constr_.value(c_fad,x_fad,tol); FadType udotc = 0; for(int j=0;j class Constr> template void Sacado_Constraint::applyAdjointHessianAD(Vector &ahuv, const Vector &u, const Vector &v, const Vector &x, Real &tol){ // Data type which supports automatic differentiation typedef Sacado::Fad::SFad FadType; typedef std::vector Fadvector; typedef std::vector vector; typedef StdVector SV; // Get a pointer to the optimization vector ROL::Ptr xp = dynamic_cast(x).getVector(); // Get a pointer to the dual constraint vector ROL::Ptr up = dynamic_cast(u).getVector(); // Get a pointer to the direction vector ROL::Ptr vp = dynamic_cast(v).getVector(); // Get a pointer to the directional adjoint Hessian ROL::Ptr ahuvp = dynamic_cast(ahuv).getVector(); // Number of optimization variables int n = xp->size(); // Create a vector of independent variables ROL::Ptr x_fad_ptr = ROL::makePtr(); x_fad_ptr->reserve(n); // Allocate for directional adjoint Jacobian ROL::Ptr aju_fad_ptr = ROL::makePtr(); aju_fad_ptr->reserve(n); for(int i=0; ipush_back(FadType(1,(*xp)[i])); // Set derivative direction (*x_fad_ptr)[i].fastAccessDx(0) = (*vp)[i]; aju_fad_ptr->push_back(0); } // Allocate for constraint vector ROL::Ptr c_fad_ptr = ROL::makePtr(); c_fad_ptr->reserve(dim_); // Allocate for dual constraint vector ROL::Ptr u_fad_ptr = ROL::makePtr(); u_fad_ptr->reserve(dim_); for(int j=0; jpush_back((*up)[j]); } StdVector x_fad(x_fad_ptr); StdVector u_fad(u_fad_ptr); StdVector aju_fad(aju_fad_ptr); // Evaluate constraint adjoint Jacobian direction this->applyAdjointJacobianAD( aju_fad, u_fad, x_fad, tol); for(int i=0; i