1 // @HEADER
2 // ************************************************************************
3 //
4 //               Rapid Optimization Library (ROL) Package
5 //                 Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 //              Drew Kouri   (dpkouri@sandia.gov) and
39 //              Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_THYRAME_OBJECTIVE_H
45 #define ROL_THYRAME_OBJECTIVE_H
46 
47 #include "ROL_Objective.hpp"
48 #include "ROL_Vector.hpp"
49 #include "ROL_Types.hpp"
50 #include <iostream>
51 
52 /** \class ROL::ThyraME_Objective
53     \brief Implements the ROL::Objective interface for a Thyra Model Evaluator Objective.
54 */
55 
56 namespace ROL {
57 
58 template <class Real>
59 class ThyraME_Objective : public Objective<Real> {
60 public:
61 
ThyraME_Objective(Thyra::ModelEvaluatorDefaultBase<double> & thyra_model_,int g_index_=0,int p_index_=0)62   ThyraME_Objective(Thyra::ModelEvaluatorDefaultBase<double>& thyra_model_, int g_index_ = 0, int p_index_ = 0) : thyra_model(thyra_model_), g_index(g_index_), p_index(p_index_){};
63 
64   /** \brief Compute value.
65 
66       This function returns the objective function value.
67       @param[in]          rol_x   is the current iterate.
68       @param[in]          tol is a tolerance for inexact objective function computation.
69   */
value(const Vector<Real> & rol_x,Real & tol)70   Real value( const Vector<Real> &rol_x, Real &tol ) {
71     const ThyraVector<Real>  & thyra_p = dynamic_cast<const ThyraVector<Real>&>(rol_x);
72     Teuchos::RCP< Thyra::VectorBase<Real> > g = Thyra::createMember<Real>(thyra_model.get_g_space(g_index));
73 
74     Thyra::ModelEvaluatorBase::InArgs<Real> inArgs = thyra_model.createInArgs();
75     Thyra::ModelEvaluatorBase::OutArgs<Real> outArgs = thyra_model.createOutArgs();
76 
77     inArgs.set_p(p_index, thyra_p.getVector());
78     outArgs.set_g(g_index, g);
79 
80     thyra_model.evalModel(inArgs, outArgs);
81 
82     return ::Thyra::get_ele(*g,0);
83   };
84 
85   /** \brief Compute gradient.
86 
87       This function returns the objective function gradient.
88       @param[out]         rol_g   is the gradient.
89       @param[in]          rol_x   is the current iterate.
90       @param[in]          tol is a tolerance for inexact objective function computation.
91   */
gradient(Vector<Real> & rol_g,const Vector<Real> & rol_x,Real & tol)92   void gradient( Vector<Real> &rol_g, const Vector<Real> &rol_x, Real &tol ) {
93     const ThyraVector<Real>  & thyra_p = dynamic_cast<const ThyraVector<Real>&>(rol_x);
94     ThyraVector<Real>  & thyra_dgdp = dynamic_cast<ThyraVector<Real>&>(rol_g);
95 
96     Teuchos::RCP<Thyra::MultiVectorBase<Real> > dgdp = thyra_dgdp.getVector();
97 
98     Thyra::ModelEvaluatorBase::InArgs<Real> inArgs = thyra_model.createInArgs();
99     Thyra::ModelEvaluatorBase::OutArgs<Real> outArgs = thyra_model.createOutArgs();
100 
101     const Thyra::ModelEvaluatorBase::DerivativeSupport dgdp_support =
102           outArgs.supports(Thyra::ModelEvaluatorBase::OUT_ARG_DgDp, g_index, p_index);
103 
104     Thyra::ModelEvaluatorBase::EDerivativeMultiVectorOrientation dgdp_orient;
105     if (dgdp_support.supports(Thyra::ModelEvaluatorBase::DERIV_MV_GRADIENT_FORM))
106       dgdp_orient = Thyra::ModelEvaluatorBase::DERIV_MV_GRADIENT_FORM;
107     else if(dgdp_support.supports(Thyra::ModelEvaluatorBase::DERIV_MV_JACOBIAN_FORM))
108       dgdp_orient = Thyra::ModelEvaluatorBase::DERIV_MV_JACOBIAN_FORM;
109     else {
110       ROL_TEST_FOR_EXCEPTION(true, std::logic_error,
111        "ROL::ThyraME_Objective: DgDp does support neither DERIV_MV_JACOBIAN_FORM nor DERIV_MV_GRADIENT_FORM forms");
112     }
113 
114     inArgs.set_p(p_index, thyra_p.getVector());
115     outArgs.set_DgDp(g_index,p_index, Thyra::ModelEvaluatorBase::DerivativeMultiVector<Real>(dgdp, dgdp_orient));
116 
117     thyra_model.evalModel(inArgs, outArgs);
118 
119   };
120 
121 private:
122   Thyra::ModelEvaluatorDefaultBase<Real>& thyra_model;
123   int g_index, p_index;
124 
125 }; // class Objective
126 
127 } // namespace ROL
128 #endif
129