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 #pragma once
45 #ifndef TANKS_DYNAMICOBJECTIVE_HPP
46 #define TANKS_DYNAMICOBJECTIVE_HPP
47 
48 #include "ROL_ParameterList.hpp"
49 #include "ROL_DynamicObjective.hpp"
50 
51 #include "Tanks_StateVector.hpp"
52 #include "Tanks_ControlVector.hpp"
53 #include "LowerBandedMatrix.hpp"
54 
55 #include <utility>
56 
57 /** \class Tanks_DynamicObjective based on the new DynamicObjective interface
58 */
59 
60 namespace Tanks {
61 
62 template<typename Real>
63 class DynamicObjective : public ROL::DynamicObjective<Real> {
64 
65   using State     = StateVector<Real>;
66   using Control   = ControlVector<Real>;
67   using Matrix    = LowerBandedMatrix<Real>;
68   using V         = ROL::Vector<Real>;
69   using TS        = ROL::TimeStamp<Real>;
70   using size_type = typename State::size_type;
71 
72 private:
73   size_type rows_;             // Number of tank rows
74   size_type cols_;             // Number of tank columns
75 
76   //---------- Time Discretization ---------------------------------------------
77   Real T_;                     // Total time
78   Real theta_;                 // Implicit/Explicit splitting factor
79   int  Nt_;                    // Number of Time steps
80   Real dt_;                    // Time step size
81 
82   size_type    Ntanks_;        // Total number of tanks
83 
84   Real htarg_;
85 
86   //--------- Subvector addressing ---------------------------------------------
87   size_type  h_, Qout_, Qin_,  z_;
88 
89 public:
90 
DynamicObjective(ROL::ParameterList & pl)91   DynamicObjective( ROL::ParameterList& pl ) :
92   // ----------- Begin Initializer List ----------------//
93   rows_   ( pl.get( "Number of Rows",        3      ) ),
94   cols_   ( pl.get( "Number of Columns",     3      ) ),
95   T_      ( pl.get( "Total Time",            20.0   ) ),
96   Nt_     ( pl.get( "Number of Time Steps",  100    ) ),
97   //----------------------------------------------------//
98   dt_     ( T_/Nt_                                    ),
99   Ntanks_ (rows_*cols_                                ),
100   htarg_  ( pl.get( "Target Fluid Level",    3.0    ) ),
101   h_      (0                                          ),
102   Qout_   (2*Ntanks_                                  ),
103   Qin_    (Ntanks_                                    ),
104   z_      (0                                          )
105   // ------------- End Initializer List ----------------//
106   {}
107 
value(const V & uo,const V & un,const V & z,const TS & timeStamp) const108   Real value( const V& uo, const V& un, const V& z, const TS& timeStamp ) const {
109     auto& uo_state = to_state(uo);
110     auto& un_state = to_state(un);
111     Real result = 0;
112     for( size_type i=0; i<rows_; ++i ) {
113       for( size_type j=0; j<cols_; ++j ) {
114         Real hdiff = un_state.h(i,j)-htarg_;
115         result += hdiff*hdiff;
116       }
117     }
118     if( timeStamp.k > 0 ) {
119       for( size_type i=0; i<rows_; ++i ) {
120         for( size_type j=0; j<cols_; ++j ) {
121           Real hdiff = uo_state.h(i,j)-htarg_;
122           result += hdiff*hdiff;
123         }
124       }
125     }
126     return static_cast<Real>(0.5)*dt_*result;
127   }
128 
129   //----------------------------------------------------------------------------
130   // Gradient Terms
gradient_uo(V & g,const V & uo,const V & un,const V & z,const TS & timeStamp) const131   void gradient_uo( V& g, const V& uo, const V& un,
132                           const V& z, const TS& timeStamp ) const {
133     if( timeStamp.k > 0 ) {
134       auto& g_state  = to_state(g);
135       auto& uo_state = to_state(uo);
136       for( size_type i=0; i<rows_; ++i ) {
137         for( size_type j=0; j<cols_; ++j ) {
138           g_state.h(i,j)    = dt_*(uo_state.h(i,j)-htarg_);
139           g_state.Qin(i,j)  = static_cast<Real>(0);
140           g_state.Qout(i,j) = static_cast<Real>(0);
141         }
142       }
143     }
144     else {
145       g.zero();
146     }
147   }
148 
gradient_un(V & g,const V & uo,const V & un,const V & z,const TS & timeStamp) const149   void gradient_un( V& g, const V& uo, const V& un,
150                           const V& z, const TS& timeStamp ) const {
151     auto& g_state  = to_state(g);
152     auto& un_state = to_state(un);
153     for( size_type i=0; i<rows_; ++i ) {
154       for( size_type j=0; j<cols_; ++j ) {
155         g_state.h(i,j)    = dt_*(un_state.h(i,j)-htarg_);
156         g_state.Qin(i,j)  = static_cast<Real>(0);
157         g_state.Qout(i,j) = static_cast<Real>(0);
158       }
159     }
160   }
161 
gradient_z(V & g,const V & uo,const V & un,const V & z,const TS & timeStamp) const162   void gradient_z( V& g, const V& uo, const V& un,
163                          const V& z, const TS& timeStamp ) const {
164     g.zero();
165   }
166 
167   //----------------------------------------------------------------------------
168   // Hessian-Vector product terms
hessVec_uo_uo(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const169   void hessVec_uo_uo( V& hv, const V& v, const V& uo, const V& un,
170                              const V& z, const TS& timeStamp ) const {
171     if( timeStamp.k > 0 ) {
172       auto& hv_state = to_state(hv);
173       auto& v_state  = to_state(v);
174       for( size_type i=0; i<rows_; ++i ) {
175         for( size_type j=0; j<cols_; ++j ) {
176           hv_state.h(i,j)    = dt_*v_state.h(i,j);
177           hv_state.Qin(i,j)  = static_cast<Real>(0);
178           hv_state.Qout(i,j) = static_cast<Real>(0);
179         }
180       }
181     }
182     else {
183       hv.zero();
184     }
185   }
186 
hessVec_un_un(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const187   void hessVec_un_un( V& hv, const V& v, const V& uo, const V& un,
188                              const V& z, const TS& timeStamp ) const {
189     auto& hv_state = to_state(hv);
190     auto& v_state  = to_state(v);
191     for( size_type i=0; i<rows_; ++i ) {
192       for( size_type j=0; j<cols_; ++j ) {
193         hv_state.h(i,j)    = dt_*v_state.h(i,j);
194         hv_state.Qin(i,j)  = static_cast<Real>(0);
195         hv_state.Qout(i,j) = static_cast<Real>(0);
196       }
197     }
198   }
199 
hessVec_z_z(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const200   void hessVec_z_z( V& hv, const V& v, const V& uo, const V& un,
201                            const V& z, const TS& timeStamp ) const {
202     hv.zero();
203   }
204 
hessVec_uo_un(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const205   void hessVec_uo_un( V& hv, const V& v, const V& uo, const V& un,
206                              const V& z, const TS& timeStamp ) const {
207     hv.zero();
208   }
209 
hessVec_uo_z(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const210   void hessVec_uo_z( V& hv, const V& v, const V& uo, const V& un,
211                             const V& z, const TS& timeStamp ) const {
212     hv.zero();
213   }
214 
hessVec_un_uo(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const215   void hessVec_un_uo( V& hv, const V& v, const V& uo, const V& un,
216                              const V& z, const TS& timeStamp ) const {
217     hv.zero();
218   }
219 
hessVec_un_z(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const220   void hessVec_un_z( V& hv, const V& v, const V& uo, const V& un,
221                             const V& z, const TS& timeStamp ) const {
222     hv.zero();
223   }
224 
hessVec_z_uo(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const225   void hessVec_z_uo( V& hv, const V& v, const V& uo, const V& un,
226                             const V& z, const TS& timeStamp ) const {
227     hv.zero();
228   }
229 
hessVec_z_un(V & hv,const V & v,const V & uo,const V & un,const V & z,const TS & timeStamp) const230   void hessVec_z_un( V& hv, const V& v, const V& uo, const V& un,
231                             const V& z, const TS& timeStamp ) const {
232     hv.zero();
233   }
234 
235 }; // Tanks::DynamicObjective
236 
237 } // namespace Tanks
238 
239 #endif // TANKS_DYNAMICOBJECTIVE_HPP
240