// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_TEUCHOS_OBJECTIVE_H #define ROL_TEUCHOS_OBJECTIVE_H #include "ROL_Objective.hpp" #include "ROL_TeuchosVector.hpp" /** @ingroup func_group \class TeuchosObjective \brief Specializes the Objective interface for objective functions that operate on TeuchosVector */ namespace ROL { template class TeuchosObjective : public Objective { template using RCP = Teuchos::RCP; using SerialDenseVector = Teuchos::SerialDenseVector; RCP getVector( const Vector& x ) { return dynamic_cast&>(x).getVector(); } RCP getVector( Vector& x ) { return dynamic_cast&>(x).getVector(); } public: virtual ~TeuchosObjective() {} virtual void update( const SerialDenseVector &x, bool flag = true, int iter = -1 ) {} using Objective::update; void update( const Vector &x, bool flag = true, int iter = -1 ) { auto xp = getVector(x); update(*xp,flag,true); } virtual Real value( const SerialDenseVector &x, Real &tol ) = 0; using Objective::value; Real value( const Vector &x, Real &tol ) { auto xp = getVector(x); return value(*xp,tol); } virtual void gradient( SerialDenseVector &g, const SerialDenseVector &x, Real &tol ) { ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> ERROR (TeuchosObjective): gradient not implemented!"); } using Objective::gradient; void gradient( Vector &g, const Vector &x, Real &tol ) { auto gp = getVector(g); auto xp = getVector(x); try { gradient(*gp,*xp,tol); } catch (std::exception &e) { Objective::gradient(g,x,tol); } } virtual Real dirDeriv( const SerialDenseVector &x, const SerialDenseVector &d, Real &tol ) { ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> ERROR (TeuchosObjective): dirDeriv not implemented!"); } using Objective::dirDeriv; Real dirDeriv( const Vector &x, const Vector &d, Real &tol ) { auto xp = getVector(x); auto dp = getVector(d); try { return dirDeriv(*xp,*dp,tol); } catch (std::exception &e) { return Objective::dirDeriv(x,d,tol); } } virtual void hessVec( SerialDenseVector &hv, const SerialDenseVector &v, const SerialDenseVector &x, Real &tol ) { ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> ERROR (TeuchosObjective): hessVec not implemented!"); } using Objective::hessVec; void hessVec( Vector &hv, const Vector &v, const Vector &x, Real &tol ) { auto hvp = getVector(hv); auto vp = getVector(v); auto xp = getVector(x); try { hessVec(*hvp,*vp,*xp,tol); } catch (std::exception &e) { Objective::hessVec(hv,v,x,tol); } } virtual void invHessVec( SerialDenseVector &hv, const SerialDenseVector &v, const SerialDenseVector &x, Real &tol ) { ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> ERROR (TeuchosObjective): invHessVec not implemented!"); } using Objective::invHessVec; void invHessVec( Vector &hv, const Vector &v, const Vector &x, Real &tol ) { auto hvp = getVector(hv); auto vp = getVector(v); auto xp = getVector(x); invHessVec(*hvp,*vp,*xp,tol); } virtual void precond( SerialDenseVector &Pv, const SerialDenseVector &v, const SerialDenseVector &x, Real &tol ) { Pv.assign(v); } using Objective::precond; void precond( Vector &Pv, const Vector &v, const Vector &x, Real &tol ) { auto Pvp = getVector(Pv); auto vp = getVector(v); auto xp = getVector(x); precond(*Pvp,*vp,*xp,tol); } }; // class TeuchosObjective } // namespace ROL #endif // ROL_TEUCHOS_OBJECTIVE_H