ROL
ROL_PartitionedVector.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 #include "ROL_Vector.hpp"
45 
46 #ifndef ROL_PARTITIONED_VECTOR_H
47 #define ROL_PARTITIONED_VECTOR_H
48 
55 namespace ROL {
56 
57 template<class Real>
58 class PartitionedVector : public Vector<Real> {
59 
60  typedef Vector<Real> V;
61  typedef Teuchos::RCP<V> RCPV;
63 
64 private:
65  Teuchos::RCP<std::vector<RCPV> > vecs_;
66  mutable std::vector<RCPV> dual_vecs_;
67  mutable Teuchos::RCP<PV> dual_pvec_;
68 public:
69 
70  typedef typename std::vector<PV>::size_type size_type;
71 
72  PartitionedVector( const Teuchos::RCP<std::vector<RCPV> > &vecs ) :
73  vecs_(vecs) {
74  for( size_type i=0; i<vecs_->size(); ++i ) {
75  dual_vecs_.push_back(((*vecs_)[i]->dual()).clone());
76  }
77  }
78 
79  void set( const V &x ) {
80  using Teuchos::dyn_cast;
81  const PV &xs = dyn_cast<const PV>(dyn_cast<const V>(x));
82 
83  for( size_type i=0; i<vecs_->size(); ++i ) {
84  (*vecs_)[i]->set(*xs.get(i));
85  }
86  }
87 
88  void plus( const V &x ) {
89  using Teuchos::dyn_cast;
90  const PV &xs = dyn_cast<const PV>(dyn_cast<const V>(x));
91 
92  for( size_type i=0; i<vecs_->size(); ++i ) {
93  (*vecs_)[i]->plus(*xs.get(i));
94  }
95  }
96 
97  void scale( const Real alpha ) {
98  for( size_type i=0; i<vecs_->size(); ++i ) {
99  (*vecs_)[i]->scale(alpha);
100  }
101  }
102 
103  void axpy( const Real alpha, const V &x ) {
104  using Teuchos::dyn_cast;
105  const PV &xs = dyn_cast<const PV>(x);
106  for( size_type i=0; i<vecs_->size(); ++i ) {
107  (*vecs_)[i]->axpy(alpha,*xs.get(i));
108  }
109  }
110 
111  Real dot( const V &x ) const {
112  using Teuchos::dyn_cast;
113  const PV &xs = dyn_cast<const PV>(x);
114  Real result = 0;
115  for( size_type i=0; i<vecs_->size(); ++i ) {
116  result += (*vecs_)[i]->dot(*xs.get(i));
117  }
118  return result;
119  }
120 
121  Real norm() const {
122  Real result = 0;
123  for( size_type i=0; i<vecs_->size(); ++i ) {
124  result += std::pow((*vecs_)[i]->norm(),2);
125  }
126  return std::sqrt(result);
127  }
128 
129  RCPV clone() const {
130  using Teuchos::RCP;
131  using Teuchos::rcp;
132 
133  RCP<std::vector<RCPV> > clonevec = rcp( new std::vector<RCPV> );
134 
135  for( size_type i=0; i<vecs_->size(); ++i ) {
136  clonevec->push_back((*vecs_)[i]->clone());
137  }
138  return rcp( new PV(clonevec) );
139  }
140 
141  const V& dual(void) const {
142 
143  using Teuchos::rcp;
144 
145  for( size_type i=0; i<vecs_->size(); ++i ) {
146  dual_vecs_[i]->set((*vecs_)[i]->dual());
147  }
148  dual_pvec_ = rcp( new PV( rcp( &dual_vecs_, false ) ) );
149  return *dual_pvec_;
150  }
151 
152  RCPV basis( const int i ) const {
153  using Teuchos::RCP;
154  using Teuchos::rcp;
155  using Teuchos::dyn_cast;
156 
157  RCPV bvec = clone();
158 
159  // Downcast
160  PV &eb = dyn_cast<PV>(*bvec);
161 
162  int begin = 0;
163  int end = 0;
164 
165  // Iterate over subvectors
166  for( size_type j=0; j<vecs_->size(); ++j ) {
167 
168  end += (*vecs_)[j]->dimension();
169 
170  if( begin<= i && i<end ) {
171  eb.set(j, *((*vecs_)[j]->basis(i-begin)) );
172  }
173  else {
174  eb.zero(j);
175  }
176 
177  begin = end;
178 
179  }
180  return bvec;
181  }
182 
183  int dimension() const {
184  int total_dim = 0;
185  for( size_type j=0; j<vecs_->size(); ++j ) {
186  total_dim += (*vecs_)[j]->dimension();
187  }
188  return total_dim;
189  }
190 
191  void zero() {
192  for( size_type j=0; j<vecs_->size(); ++j ) {
193  (*vecs_)[j]->zero();
194  }
195  }
196 
197  // Methods that do not exist in the base class
198 
199  Teuchos::RCP<const Vector<Real> > get(size_type i) const {
200  return (*vecs_)[i];
201  }
202 
203  Teuchos::RCP<Vector<Real> > get(size_type i) {
204  return (*vecs_)[i];
205  }
206 
207  void set(size_type i, const V &x) {
208  (*vecs_)[i]->set(x);
209  }
210 
211  void zero(size_type i) {
212  (*vecs_)[i]->zero();
213  }
214 
215  size_type numVectors() const {
216  return vecs_->size();
217  }
218 
219 };
220 
221 // Helper methods
222 template<class Real>
223 Teuchos::RCP<Vector<Real> > CreatePartitionedVector( Teuchos::RCP<Vector<Real> > &a ) {
224  using Teuchos::RCP;
225  using Teuchos::rcp;
226  typedef RCP<Vector<Real> > RCPV;
227  typedef PartitionedVector<Real> PV;
228 
229  RCPV temp[] = {a};
230  return rcp( new PV( rcp( new std::vector<RCPV>(temp, temp+1) ) ) );
231 }
232 
233 template<class Real>
234 Teuchos::RCP<const Vector<Real> > CreatePartitionedVector( Teuchos::RCP<const Vector<Real> > &a ) {
235  using Teuchos::RCP;
236  using Teuchos::rcp;
237  typedef RCP<const Vector<Real> > RCPV;
238  typedef const PartitionedVector<Real> PV;
239 
240  RCPV temp[] = {a};
241  return rcp( new PV( rcp( new std::vector<RCPV>(temp, temp+1) ) ) );
242 }
243 
244 template<class Real>
245 Teuchos::RCP<Vector<Real> > CreatePartitionedVector( Teuchos::RCP<Vector<Real> > &a,
246  Teuchos::RCP<Vector<Real> > &b ) {
247  using Teuchos::RCP;
248  using Teuchos::rcp;
249  typedef RCP<Vector<Real> > RCPV;
250  typedef PartitionedVector<Real> PV;
251 
252  RCPV temp[] = {a,b};
253  return rcp( new PV( rcp( new std::vector<RCPV>(temp, temp+2) ) ) );
254 }
255 
256 template<class Real>
257 Teuchos::RCP<const Vector<Real> > CreatePartitionedVector( Teuchos::RCP<const Vector<Real> > &a,
258  Teuchos::RCP<const Vector<Real> > &b ) {
259  using Teuchos::RCP;
260  using Teuchos::rcp;
261  typedef RCP<const Vector<Real> > RCPV;
262  typedef const PartitionedVector<Real> PV;
263 
264  RCPV temp[] = {a,b};
265  return rcp( new PV( rcp( new std::vector<RCPV>(temp, temp+2) ) ) );
266 }
267 
268 
269 
270 } // namespace ROL
271 
272 #endif // ROL_PARTITIONED_VECTOR_H
273 
PartitionedVector(const Teuchos::RCP< std::vector< RCPV > > &vecs)
Real norm() const
Returns where .
Defines the linear algebra of vector space on a generic partitioned vector.
const V & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(Teuchos::RCP< Vector< Real > > &a)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real dot(const V &x) const
Compute where .
void zero()
Set to zero vector.
PartitionedVector< Real > PV
void scale(const Real alpha)
Compute where .
int dimension() const
Return dimension of the vector space.
Teuchos::RCP< const Vector< Real > > get(size_type i) const
RCPV basis(const int i) const
Return i-th basis vector.
Teuchos::RCP< std::vector< RCPV > > vecs_
std::vector< RCPV > dual_vecs_
void set(const V &x)
Set where .
RCPV clone() const
Clone to make a new (uninitialized) vector.
std::vector< PV >::size_type size_type
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:196
void plus(const V &x)
Compute , where .
void axpy(const Real alpha, const V &x)
Compute where .