// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_OED_DOUBLE_WELL_PENALTY_DEF_HPP #define ROL_OED_DOUBLE_WELL_PENALTY_DEF_HPP namespace ROL { namespace OED { template DoubleWellPenalty::DoubleWellPenalty() : ProfiledClass("OED::DoubleWellPenalty") {} template std::vector& DoubleWellPenalty::getData(Vector &x) const { return *dynamic_cast&>(x).getVector(); } template const std::vector& DoubleWellPenalty::getConstData(const Vector &x) const { return *dynamic_cast&>(x).getVector(); } template void DoubleWellPenalty::sumAll(Real *input, Real *output, int size, const Vector &x) const { dynamic_cast&>(x).getBatchManager()->sumAll(input,output,size); } template Real DoubleWellPenalty::value( const Vector &x, Real &tol ) { startTimer("value"); const Real one(1); const std::vector &xdata = getConstData(x); Real mval(0), gval(0); for (const auto &xi : xdata) mval += xi*xi*(one-xi)*(one-xi); sumAll(&mval,&gval,1,x); stopTimer("value"); return gval; } template void DoubleWellPenalty::gradient( Vector &g, const Vector &x, Real &tol ) { startTimer("gradient"); const Real one(1), two(2), three(3); Real xi(0); std::vector &gdata = getData(g); const std::vector &xdata = getConstData(x); for (int i = 0; i < static_cast(gdata.size()); ++i) { xi = xdata[i]; gdata[i] = two*xi*(two*xi*xi-three*xi+one); } stopTimer("gradient"); } template void DoubleWellPenalty::hessVec( Vector &hv, const Vector &v, const Vector &x, Real &tol ) { startTimer("hessVec"); const Real one(1), two(2), three(3); Real xi(0), vi(0); std::vector &hvdata = getData(hv); const std::vector &xdata = getConstData(x); const std::vector &vdata = getConstData(v); for (int i = 0; i < static_cast(hvdata.size()); ++i) { xi = xdata[i]; vi = vdata[i]; hvdata[i] = (three*(two*xi-one)*(two*xi-one)-one)*vi; } stopTimer("hessVec"); } } // End OED Namespace } // End ROL Namespace #endif