ROL
ROL_DynamicFunction.hpp
Go to the documentation of this file.
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 
45 #pragma once
46 #ifndef ROL_DYNAMICFUNCTION_HPP
47 #define ROL_DYNAMICFUNCTION_HPP
48 
49 #include <initializer_list>
50 
51 #include "ROL_TimeStamp.hpp"
52 
54 #include "ROL_VectorWorkspace.hpp"
55 
66 namespace ROL {
67 template<typename Real>
69 public:
70 
71  using V = Vector<Real>;
74 
75  DynamicFunction( std::initializer_list<std::string> zero_deriv_terms={} ) :
76  zero_deriv_terms_(zero_deriv_terms) {
77 
78  if( is_zero_derivative("uo") ) add_terms("uo_uo","uo_un","uo_z");
79  if( is_zero_derivative("un") ) add_terms("un_uo","un_un","un_z");
80  if( is_zero_derivative("z") ) add_terms("z_uo", "z_un", "z_z" );
81  if( is_zero_derivative("uo_un") ) add_terms("un_uo");
82  if( is_zero_derivative("uo_z") ) add_terms("z_uo");
83  if( is_zero_derivative("un_z") ) add_terms("z_un");
84  if( is_zero_derivative("un_uo") ) add_terms("uo_un");
85  if( is_zero_derivative("z_uo") ) add_terms("uo_z");
86  if( is_zero_derivative("z_un") ) add_terms("un_z");
87 
88  }
89 
90  virtual ~DynamicFunction() {}
91 
92  // Update old state
93  virtual void update_uo( const V& x, const TS& ts ) {}
94 
95  // Update new state
96  virtual void update_un( const V& x, const TS& ts ) {}
97 
98  // Update control
99  virtual void update_z( const V& x, const TS& ts ) {}
100 
101  bool is_zero_derivative( const std::string& key ) {
102  return std::find( std::begin(zero_deriv_terms_),
103  std::end(zero_deriv_terms_),
104  key ) != std::end(zero_deriv_terms_);
105  }
106 
107 
108 protected:
109 
111 
112  PV& partition( V& x ) const;
113  const PV& partition( const V& x ) const;
114 
115  V& getNew( V& x ) const;
116  const V& getNew( const V& x ) const;
117 
118  V& getOld( V& x ) const;
119  const V& getOld( const V& x ) const;
120 
121 private:
122 
124  std::vector<std::string> zero_deriv_terms_;
125 
126  template<typename First>
127  void add_terms( const First& first ) {
128  if( !is_zero_derivative(first) )
129  zero_deriv_terms_.push_back(first);
130  }
131 
132  template<typename First, typename...Rest>
133  void add_terms( const First& first, const Rest&... rest ) {
134  if( !is_zero_derivative(first) )
135  zero_deriv_terms_.push_back(first);
136  add_terms(rest...);
137  }
138 
139 };
140 
141 
142 } // namespace ROL
143 
144 
145 
147 
148 #endif // ROL_DYNAMICFUNCTION_HPP
149 
DynamicFunction(std::initializer_list< std::string > zero_deriv_terms={})
VectorWorkspace< Real > & getVectorWorkspace() const
PartitionedVector< Real > PV
void add_terms(const First &first, const Rest &...rest)
Defines the linear algebra of vector space on a generic partitioned vector.
Provides update interface, casting and vector management to DynamicConstraint and DynamicObjective...
bool is_zero_derivative(const std::string &key)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Vector< Real > V
virtual void update_z(const V &x, const TS &ts)
virtual void update_uo(const V &x, const TS &ts)
std::vector< std::string > zero_deriv_terms_
VectorWorkspace< Real > workspace_
void add_terms(const First &first)
virtual void update_un(const V &x, const TS &ts)