Debian Codegraph @

packages/rol/adapters/epetra/src/vector/ROL_EpetraMultiVector.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef ROL_EPETRAMULTIVECTOR_H
11 #define ROL_EPETRAMULTIVECTOR_H
12
13 #include "Epetra_MultiVector.h"
14 #include "Epetra_Map.h"
15 #include "Epetra_LocalMap.h"
16 #include "Epetra_IntVector.h"
17 #include "Epetra_Import.h"
18 #include "ROL_Vector.hpp"
19
20 /** \class ROL::EpetraMultiVector
21 \brief Implements the ROL::Vector interface for an Epetra_MultiVector.
22 */
23
24 namespace ROL {
25
26 template <class Real>
27 class EpetraMultiVector : public Vector<Real> {
28 private:
29
30 ROL::Ptr<Epetra_MultiVector> epetra_vec_;
31
32 public:
33 virtual ~EpetraMultiVector() {}
34
35 EpetraMultiVector(const ROL::Ptr<Epetra_MultiVector> & epetra_vec) : epetra_vec_(epetra_vec) {}
36
37 /** \brief Compute \f$y \leftarrow x + y\f$ where \f$y = \mbox{*this}\f$.
38 */
39 void plus( const Vector<Real> &x ) {
40 const EpetraMultiVector &ex = dynamic_cast<const EpetraMultiVector&>(x);
41 epetra_vec_->Update( 1.0, *ex.getVector(), 1.0 );
42 }
43
44 /** \brief Compute \f$y \leftarrow \alpha y\f$ where \f$y = \mbox{*this}\f$.
45 */
46 void scale( const Real alpha ) {
47 epetra_vec_->Scale( (double)alpha );
48 }
49
50 /** \brief Returns \f$ \langle y,x \rangle \f$ where \f$y = \mbox{*this}\f$.
51 */
52 Real dot( const Vector<Real> &x ) const {
53 double val;
54 const EpetraMultiVector &ex = dynamic_cast<const EpetraMultiVector&>(x);
55 epetra_vec_->Dot( *ex.getVector(), &val );
56 return (Real)val;
57 }
58
59 /** \brief Returns \f$ \| y \| \f$ where \f$y = \mbox{*this}\f$.
60 */
61 Real norm() const {
62 double val;
63 epetra_vec_->Norm2(&val);
64 return (Real) val;
65 }
66
67 /** \brief Clone to make a new (uninitialized) vector.
68 */
69 ROL::Ptr<Vector<Real> > clone() const{
70 return ROL::makePtr<EpetraMultiVector>(
71 ROL::makePtr<Epetra_MultiVector>(epetra_vec_->Map(),epetra_vec_->NumVectors(),false) );
72 }
73
74 /** \brief Compute \f$y \leftarrow \alpha x + y\f$ where \f$y = \mbox{*this}\f$.
75 */
76 void axpy( const Real alpha, const Vector<Real> &x ) {
77 const EpetraMultiVector &ex = dynamic_cast<const EpetraMultiVector&>(x);
78 epetra_vec_->Update( alpha, *ex.getVector(), 1.0 );
79 }
80
81 /** \brief Set to zero vector.
82 */
83 void zero() {
84 epetra_vec_->PutScalar(0.0);
85 }
86
87 void PutScalar(const Real C) {
88 epetra_vec_->PutScalar(static_cast<double>(C));
89 }
90
91 void setScalar(const Real C) {
92 epetra_vec_->PutScalar(static_cast<double>(C));
93 }
94
95 void randomize(const Real l=0.0, const Real u=1.0) {
96 epetra_vec_->Random(); // Random numbers between -1 and 1
97 this->scale(static_cast<Real>(0.5)*(u-l)); // Random numbers between (l-u)/2 and (u-l)/2
98 Ptr<Vector<Real>> copy = this->clone();
99 copy->setScalar(static_cast<Real>(0.5)*(u+l)); // Constant vector with values (u+l)/2
100 this->plus(*copy); // Random numbers between l and u
101 }
102
103 /** \brief Set \f$y \leftarrow x\f$ where \f$y = \mbox{*this}\f$.
104 */
105 void set( const Vector<Real> &x ) {
106 const EpetraMultiVector &ex = dynamic_cast<const EpetraMultiVector&>(x);
107 epetra_vec_->Scale(1.0,*ex.getVector());
108 }
109
110 ROL::Ptr<const Epetra_MultiVector> getVector() const {
111 return this->epetra_vec_;
112 }
113
114 ROL::Ptr<Epetra_MultiVector> getVector() {
115 return this->epetra_vec_;
116 }
117
118 ROL::Ptr<Vector<Real> > basis( const int i ) const {
119 ROL::Ptr<EpetraMultiVector> e =
120 ROL::makePtr<EpetraMultiVector>( ROL::makePtr<Epetra_MultiVector>(epetra_vec_->Map(),epetra_vec_->NumVectors(),true));
121 const Epetra_BlockMap & domainMap = e->getVector()->Map();
122
123 Epetra_Map linearMap(domainMap.NumGlobalElements(), domainMap.NumMyElements(), 0, domainMap.Comm());
124 int lid = linearMap.LID(i);
125 if(lid >=0)
126 (*e->getVector())[0][lid]= 1.0;
127
128 return e;
129
130 /*
131
132
133 // Build IntVector of GIDs on all processors.
134 const Epetra_Comm & comm = domainMap.Comm();
135 int numMyElements = domainMap.NumMyElements();
136 Epetra_BlockMap allGidsMap(-1, numMyElements, 1, 0, comm);
137 Epetra_IntVector allGids(allGidsMap);
138 for (int j=0; j<numMyElements; j++) {allGids[j] = domainMap.GID(j);}
139
140 // Import my GIDs into an all-inclusive map.
141 int numGlobalElements = domainMap.NumGlobalElements();
142 Epetra_LocalMap allGidsOnRootMap(numGlobalElements, 0, comm);
143 Epetra_Import importer(allGidsOnRootMap, allGidsMap);
144 Epetra_IntVector allGidsOnRoot(allGidsOnRootMap);
145 allGidsOnRoot.Import(allGids, importer, Insert);
146 Epetra_Map rootDomainMap(-1, allGidsOnRoot.MyLength(), allGidsOnRoot.Values(), domainMap.IndexBase(), comm);
147
148 for (int j = 0; j < this->dimension(); j++) {
149 // Put 1's in slots
150 int curGlobalCol = rootDomainMap.GID(i); // Should return same value on all processors
151 if (domainMap.MyGID(curGlobalCol)){
152 int curCol = domainMap.LID(curGlobalCol);
153 (*e->getVector())[0][curCol]= 1.0;
154 }
155 }
156
157 return e;
158
159 */
160 }
161
162 int dimension() const {return epetra_vec_->GlobalLength();}
163
164
165 }; // class EpetraMultiVector
166
167 } // namespace ROL
168
169 #endif
170