ROL
ROL_EpetraMultiVector.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_EPETRAMULTIVECTOR_H
45 #define ROL_EPETRAMULTIVECTOR_H
46 
47 #include "Epetra_MultiVector.h"
48 #include "Epetra_Map.h"
49 #include "Epetra_LocalMap.h"
50 #include "Epetra_IntVector.h"
51 #include "Epetra_Import.h"
52 #include "ROL_Vector.hpp"
53 
58 namespace ROL {
59 
60 template <class Real>
61 class EpetraMultiVector : public Vector<Real> {
62 private:
63 
64  Teuchos::RCP<Epetra_MultiVector> epetra_vec_;
65 
66 public:
67  virtual ~EpetraMultiVector() {}
68 
69  EpetraMultiVector(const Teuchos::RCP<Epetra_MultiVector> & epetra_vec) : epetra_vec_(epetra_vec) {}
70 
73  void plus( const Vector<Real> &x ) {
74  EpetraMultiVector &ex = Teuchos::dyn_cast<EpetraMultiVector>(const_cast <Vector<Real>&>(x));
75  Teuchos::RCP<const Epetra_MultiVector> xvalptr = ex.getVector();
76  epetra_vec_->Update( 1.0, *xvalptr, 1.0 );
77  }
78 
81  void scale( const Real alpha ) {
82  epetra_vec_->Scale( (double)alpha );
83  }
84 
87  Real dot( const Vector<Real> &x ) const {
88  double val[1];
89  EpetraMultiVector &ex = Teuchos::dyn_cast<EpetraMultiVector>(const_cast <Vector<Real>&>(x));
90  Teuchos::RCP<const Epetra_MultiVector> xvalptr = ex.getVector();
91  epetra_vec_->Dot( *xvalptr, val );
92  return (Real)val[0];
93  }
94 
97  Real norm() const {
98  double val[1];
99  epetra_vec_->Dot( *epetra_vec_, val );
100  return (Real)sqrt(val[0]);
101  }
102 
105  Teuchos::RCP<Vector<Real> > clone() const{
106  return Teuchos::rcp(new EpetraMultiVector(
107  Teuchos::rcp(new Epetra_MultiVector(epetra_vec_->Map(),epetra_vec_->NumVectors(),false)) ));
108  }
109 
112  virtual void axpy( const Real alpha, const Vector<Real> &x ) {
113  EpetraMultiVector &ex = Teuchos::dyn_cast<EpetraMultiVector>(const_cast <Vector<Real>&>(x));
114  Teuchos::RCP<const Epetra_MultiVector> xvalptr = ex.getVector();
115  epetra_vec_->Update( alpha, *xvalptr, 1.0 );
116  }
117 
120  virtual void zero() {
121  epetra_vec_->PutScalar(0.0);
122  }
123 
126  virtual void set( const Vector<Real> &x ) {
127  EpetraMultiVector &ex = Teuchos::dyn_cast<EpetraMultiVector>(const_cast <Vector<Real>&>(x));
128  Teuchos::RCP<const Epetra_MultiVector> xvalptr = ex.getVector();
129  epetra_vec_->Scale(1.0,*xvalptr);
130  }
131 
132  Teuchos::RCP<const Epetra_MultiVector> getVector() const {
133  return this->epetra_vec_;
134  }
135 
136  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
137  Teuchos::RCP<EpetraMultiVector> e = Teuchos::rcp( new EpetraMultiVector( Teuchos::rcp(new Epetra_MultiVector(epetra_vec_->Map(),epetra_vec_->NumVectors(),true)) ));
138  const Epetra_BlockMap & domainMap = const_cast <Epetra_BlockMap &> (const_cast <Epetra_MultiVector &> ((*e->getVector())).Map());
139 
140  // Build IntVector of GIDs on all processors.
141  const Epetra_Comm & comm = domainMap.Comm();
142  int numMyElements = domainMap.NumMyElements();
143  Epetra_BlockMap allGidsMap(-1, numMyElements, 1, 0, comm);
144  Epetra_IntVector allGids(allGidsMap);
145  for (int j=0; j<numMyElements; j++) {allGids[j] = domainMap.GID(j);}
146 
147  // Import my GIDs into an all-inclusive map.
148  int numGlobalElements = domainMap.NumGlobalElements();
149  Epetra_LocalMap allGidsOnRootMap(numGlobalElements, 0, comm);
150  Epetra_Import importer(allGidsOnRootMap, allGidsMap);
151  Epetra_IntVector allGidsOnRoot(allGidsOnRootMap);
152  allGidsOnRoot.Import(allGids, importer, Insert);
153  Epetra_Map rootDomainMap(-1, allGidsOnRoot.MyLength(), allGidsOnRoot.Values(), domainMap.IndexBase(), comm);
154 
155  for (int j = 0; j < this->dimension(); j++) {
156  // Put 1's in slots
157  int curGlobalCol = rootDomainMap.GID(i); // Should return same value on all processors
158  if (domainMap.MyGID(curGlobalCol)){
159  int curCol = domainMap.LID(curGlobalCol);
160  (const_cast <Epetra_MultiVector &> (*e->getVector()))[0][curCol]= 1.0;
161  }
162  }
163 
164  return e;
165  }
166 
167  int dimension() const {return epetra_vec_->GlobalLength();}
168 
169 
170 }; // class EpetraMultiVector
171 
172 } // namespace ROL
173 
174 #endif
175 
virtual void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:70
Real dot(const Vector< Real > &x) const
Returns where .
Teuchos::RCP< Epetra_MultiVector > epetra_vec_
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
virtual void set(const Vector< Real > &x)
Set where .
void scale(const Real alpha)
Compute where .
Real norm() const
Returns where .
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Implements the ROL::Vector interface for an Epetra_MultiVector.
void plus(const Vector< Real > &x)
Compute where .
Teuchos::RCP< const Epetra_MultiVector > getVector() const
int dimension() const
Return dimension of the vector space.
virtual void zero()
Set to zero vector.
EpetraMultiVector(const Teuchos::RCP< Epetra_MultiVector > &epetra_vec)