ROL
ROL_AtomVector.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 #ifndef ROL_ATOMVECTOR_H
45 #define ROL_ATOMVECTOR_H
46 
47 #include "ROL_BatchStdVector.hpp"
48 
54 namespace ROL {
55 
56 template <class Real>
58 
59 template <class Real>
61 
62 template <class Real>
63 class AtomVector : public BatchStdVector<Real> {
65 private:
66  const int numMySamples_;
67  const int dimension_;
68 
69 public:
70  AtomVector(const ROL::Ptr<std::vector<Real> > &vec,
71  const ROL::Ptr<BatchManager<Real> > &bman,
72  const int numMySamples, const int dimension)
73  : BatchStdVector<Real>(vec,bman),
74  numMySamples_(numMySamples), dimension_(dimension) {}
75 
76  ROL::Ptr<const std::vector<Real> > getAtom(const int i) const {
77  ROL_TEST_FOR_EXCEPTION((i < 0 || i > numMySamples_), std::invalid_argument,
78  ">>> ERROR (ROL::AtomVector): index out of bounds in getAtom!");
79  uint dim = static_cast<uint>(dimension_), I = static_cast<uint>(i);
80  std::vector<Real> pt(dim,0);
81  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
82  for (uint j = 0; j < dim; ++j) {
83  pt[j] = yval[I*dim + j];
84  }
85  return ROL::makePtr<std::vector<Real>>(pt);
86  }
87 
88  void setAtom(const int i, const std::vector<Real> &pt) {
89  ROL_TEST_FOR_EXCEPTION((i < 0 || i > numMySamples_), std::invalid_argument,
90  ">>> ERROR (ROL::AtomVector): index out of bounds in setAtom!");
91  uint dim = static_cast<uint>(dimension_), I = static_cast<uint>(i);
92  std::vector<Real> &yval = *(StdVector<Real>::getVector());
93  for (uint j = 0; j < dim; ++j) {
94  yval[I*dim + j] = pt[j];
95  }
96  }
97 
98  int getNumMyAtoms(void) const {
99  return numMySamples_;
100  }
101 
102  int getDimension(void) const {
103  return dimension_;
104  }
105 };
106 
107 template<class Real>
108 class PrimalAtomVector : public AtomVector<Real> {
110 private:
111  const ROL::Ptr<std::vector<Real> > scale_;
112  mutable ROL::Ptr<DualAtomVector<Real> > dual_vec_;
113  mutable bool isDualInitialized_;
114 
115 public:
116  PrimalAtomVector(const ROL::Ptr<std::vector<Real> > &vec,
117  const ROL::Ptr<BatchManager<Real> > &bman,
118  const int numMySamples, const int dimension,
119  const ROL::Ptr<std::vector<Real> > &scale)
120  : AtomVector<Real>(vec,bman,numMySamples,dimension),
121  scale_(scale), isDualInitialized_(false) {}
122 
123  Real dot(const Vector<Real> &x) const {
124  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
125  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
126  uint ysize = yval.size();
127  ROL_TEST_FOR_EXCEPTION( xval.size() != ysize, std::invalid_argument,
128  "Error: Vectors must have the same dimension." );
129  uint index = 0;
130  uint numMySamples = static_cast<uint>(AtomVector<Real>::getNumMyAtoms());
132  Real val(0), sum_val(0);
133  for (uint i = 0; i < numMySamples; i++) {
134  for (uint j = 0; j < dimension; j++) {
135  index = i*dimension + j;
136  val += xval[index] * (*scale_)[index] * yval[index];
137  }
138  }
139  // Global sum
140  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
141  return sum_val;
142  }
143 
144  ROL::Ptr<Vector<Real> > clone(void) const {
145  uint numMySamples = static_cast<uint>(AtomVector<Real>::getNumMyAtoms());
147  return ROL::makePtr<PrimalAtomVector>(
148  ROL::makePtr<std::vector<Real>>(numMySamples*dimension),
150  numMySamples,dimension,scale_);
151  }
152 
153  const Vector<Real> & dual(void) const {
154  uint numMySamples = static_cast<uint>(AtomVector<Real>::getNumMyAtoms());
156  if ( !isDualInitialized_ ) {
157  dual_vec_ = ROL::makePtr<DualAtomVector<Real>>(
158  ROL::makePtr<std::vector<Real>>(numMySamples*dimension),
160  numMySamples,dimension,scale_);
161  isDualInitialized_ = true;
162  }
163  uint index = 0;
164  for (uint i = 0; i < numMySamples; i++) {
165  for (uint j = 0; j < dimension; j++) {
166  index = i*dimension + j;
167  (*(dual_vec_->getVector()))[index]
168  = (*scale_)[index] * (*(StdVector<Real>::getVector()))[index];
169  }
170  }
171  return *dual_vec_;
172  }
173 };
174 
175 template<class Real>
176 class DualAtomVector : public AtomVector<Real> {
178 private:
179  const ROL::Ptr<std::vector<Real> > scale_;
180  mutable ROL::Ptr<PrimalAtomVector<Real> > primal_vec_;
181  mutable bool isDualInitialized_;
182 
183 public:
184  DualAtomVector(const ROL::Ptr<std::vector<Real> > &vec,
185  const ROL::Ptr<BatchManager<Real> > &bman,
186  const int numMySamples, const int dimension,
187  const ROL::Ptr<std::vector<Real> > &scale)
188  : AtomVector<Real>(vec,bman,numMySamples,dimension),
189  scale_(scale), isDualInitialized_(false) {}
190 
191  Real dot(const Vector<Real> &x) const {
192  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
193  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
194  uint ysize = yval.size();
195  ROL_TEST_FOR_EXCEPTION( xval.size() != ysize, std::invalid_argument,
196  "Error: Vectors must have the same dimension." );
197  uint index = 0;
198  uint numMySamples = static_cast<uint>(AtomVector<Real>::getNumMyAtoms());
200  Real val(0), sum_val(0);
201  for (uint i = 0; i < numMySamples; i++) {
202  for (uint j = 0; j < dimension; j++) {
203  index = i*dimension + j;
204  val += xval[index] * yval[index] / (*scale_)[index];
205  }
206  }
207  // Global sum
208  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
209  return sum_val;
210  }
211 
212  ROL::Ptr<Vector<Real> > clone(void) const {
213  uint numMySamples = static_cast<uint>(AtomVector<Real>::getNumMyAtoms());
215  return ROL::makePtr<DualAtomVector>(
216  ROL::makePtr<std::vector<Real>>(numMySamples*dimension),
218  numMySamples,dimension,scale_);
219  }
220 
221  const Vector<Real> & dual(void) const {
222  uint numMySamples = static_cast<uint>(AtomVector<Real>::getNumMyAtoms());
224  if ( !isDualInitialized_ ) {
225  primal_vec_ = ROL::makePtr<PrimalAtomVector<Real>>(
226  ROL::makePtr<std::vector<Real>>(numMySamples*dimension),
228  numMySamples,dimension,scale_);
229  isDualInitialized_ = true;
230  }
231  uint index = 0;
232  for (uint i = 0; i < numMySamples; i++) {
233  for (uint j = 0; j < dimension; j++) {
234  index = i*dimension + j;
235  (*(primal_vec_->getVector()))[index]
236  = (*(StdVector<Real>::getVector()))[index] / (*scale_)[index];
237  }
238  }
239  return *primal_vec_;
240  }
241 };
242 
243 } // namespace ROL
244 
245 #endif
const ROL::Ptr< std::vector< Real > > scale_
std::vector< Real >::size_type uint
typename PV< Real >::size_type size_type
void scale(const Real alpha)
Compute where .
Provides the std::vector implementation of the ROL::Vector interface.
int getDimension(void) const
ROL::Ptr< const std::vector< Real > > getAtom(const int i) const
Provides the std::vector implementation of the ROL::Vector interface.
void setAtom(const int i, const std::vector< Real > &pt)
Real dot(const Vector< Real > &x) const
Compute where .
Ptr< const std::vector< Element > > getVector() const
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
std::vector< Real >::size_type uint
AtomVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const int numMySamples, const int dimension)
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Real dot(const Vector< Real > &x) const
Compute where .
const int numMySamples_
const Ptr< BatchManager< Real > > getBatchManager(void) const
int getNumMyAtoms(void) const
DualAtomVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const int numMySamples, const int dimension, const ROL::Ptr< std::vector< Real > > &scale)
PrimalAtomVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const int numMySamples, const int dimension, const ROL::Ptr< std::vector< Real > > &scale)
const ROL::Ptr< std::vector< Real > > scale_
std::vector< Real >::size_type uint
ROL::Ptr< PrimalAtomVector< Real > > primal_vec_
int dimension(void) const
Return dimension of the vector space.
constexpr auto dim
ROL::Ptr< DualAtomVector< Real > > dual_vec_
const int dimension_