Xyce  6.1
ROL_XyceVector.hpp
Go to the documentation of this file.
1 #ifndef ROL_XYCEVECTOR_H
2 #define ROL_XYCEVECTOR_H
3 
4 // Xyce includes
5 #include "Xyce_config.h"
6 #include "N_UTL_fwd.h"
7 #include "N_ERH_ErrorMgr.h"
8 #include "N_LAS_MultiVector.h"
9 #include "N_LAS_Vector.h"
10 #include "N_PDS_Comm.h"
11 #include "N_PDS_ParMap.h"
12 #include "N_UTL_FeatureTest.h"
13 
14 // Epetra includes
15 #include "Epetra_MultiVector.h"
16 #include "Epetra_Vector.h"
17 #include "Epetra_Map.h"
18 #include "Epetra_LocalMap.h"
19 #include "Epetra_IntVector.h"
20 #include "Epetra_Import.h"
21 #include "Epetra_Export.h"
22 #include "Epetra_Comm.h"
23 
24 #include "ROL_Vector.hpp"
25 
26 #include <math.h>
27 #include <fstream>
28 
29 /** \class Xyce::Linear::XyceVector
30  \brief Implements the ROL::Vector interface for a Xyce::Linear::MultiVector.
31 */
32 
33 namespace Xyce {
34 namespace Linear {
35 
36 template <class Real>
37 class ROL_XyceVector : public ::ROL::Vector<Real> {
38 private:
39  // a vector of pointers to Xyce vectors
40  //Teuchos::RCP< std::vector< MultiVector *> > xyce_multi_vec_;
41  Teuchos::RCP< std::vector<Teuchos::RCP<Vector> > > xyce_multi_vec_;
42  int size_;
43 
44 public:
45  virtual ~ROL_XyceVector() {}
46 
47  // constructor
48  ROL_XyceVector( const int size, const MultiVector & xyce_multi_vec ){
49  MultiVector & xmv = const_cast<MultiVector &>(xyce_multi_vec);
50  size_ = size;
51  xyce_multi_vec_ = Teuchos::rcp( new std::vector< Teuchos::RCP<Vector> >(size_));
52  for (int i=0;i<size_;i++){
53  (*xyce_multi_vec_)[i] = Teuchos::rcp( new Vector( *xmv.pmap(), *xmv.omap() ));
54  }
55  }
56 
57  // copy constructor
58  ROL_XyceVector(const Teuchos::RCP<std::vector<Teuchos::RCP<Vector> > > & xyce_multi_vec) : xyce_multi_vec_(xyce_multi_vec) {
59  size_ = xyce_multi_vec_->size();
60  }
61 
62  // copy constructor accepting vectors of raw pointers
63  ROL_XyceVector(std::vector<Vector *> & xyce_multi_vec){
64  size_ = xyce_multi_vec.size();
65  xyce_multi_vec_ = Teuchos::rcp( new std::vector< Teuchos::RCP<Vector> >(size_));
66  //std::vector<Vector *> & xmv = const_cast<std::vector<Vector *> &>(xyce_multi_vec);
67  for (int i=0;i<size_;i++){
68  (*xyce_multi_vec_)[i] = Teuchos::rcpFromRef(*xyce_multi_vec[i]);
69  }
70  }
71 
72  // access functions
73  Teuchos::RCP<const std::vector<Teuchos::RCP<Vector> > > getVector() const {
74  return this->xyce_multi_vec_;
75  }
76 
77 
78  Teuchos::RCP<std::vector<Teuchos::RCP<Vector> > > getVector() {
79  return this->xyce_multi_vec_;
80  }
81 
82 
83  /** \brief Compute \f$y \leftarrow x + y\f$ where \f$y = \mbox{*this}\f$.
84  */
85  void plus( const ::ROL::Vector<Real> &x ) {
86  const ROL_XyceVector &ex = Teuchos::dyn_cast<const ROL_XyceVector>(x);
87 
88  // assuming x and this are the same size
89  for (int i=0;i<size_;i++){
90  (*xyce_multi_vec_)[i]->addVec( (double)1.0, *( (*ex.getVector())[i] ) );
91  }
92  }
93 
94  /** \brief Compute \f$y \leftarrow \alpha y\f$ where \f$y = \mbox{*this}\f$.
95  */
96  void scale( const Real alpha ) {
97  for (int i=0;i<size_;i++){
98  (*xyce_multi_vec_)[i]->scale( (double)alpha );
99  }
100  }
101 
102  /** \brief Returns \f$ \langle y,x \rangle \f$ where \f$y = \mbox{*this}\f$.
103  */
104  Real dot( const ::ROL::Vector<Real> &x ) const {
105  double val=0.0;
106  const ROL_XyceVector &ex = Teuchos::dyn_cast<const ROL_XyceVector>(x);
107  //xyce_multi_vec_->dotProduct( *ex.getVector() );
108  for (int i=0;i<size_;i++){
109  val += (*xyce_multi_vec_)[i]->dotProduct( *(*ex.getVector())[i] );
110  }
111  return (Real)val;
112  }
113 
114  /** \brief Returns \f$ \| y \| \f$ where \f$y = \mbox{*this}\f$.
115  */
116  Real norm() const {
117  std::vector<double> vals(size_,0.0);
118  double res = 0.0;
119  for (int i=0;i<size_;i++){
120  (*xyce_multi_vec_)[i]->lpNorm(2,&vals[i]);
121  res += vals[i]*vals[i];
122  }
123  return (Real) sqrt(res);
124  }
125 
126  /** \brief Clone to make a new (uninitialized) vector.
127  */
128  Teuchos::RCP< ::ROL::Vector<Real> > clone() const{
129  // return Teuchos::rcp(new ROL_XyceVector( Teuchos::rcp( MultiVector( *(xyce_multi_vec_->pmap()), xyce_multi_vec_->numVectors() ) )) );
130  //return Teuchos::rcp(new ROL_XyceVector( Teuchos::rcp( new std::vector< MultiVector *>(MultiVector(*((*xyce_multi_vec_)[0]) ) ) ) ) );
131 
132  Teuchos::RCP< std::vector<Teuchos::RCP<Vector> > > x = Teuchos::rcp( new std::vector<Teuchos::RCP<Vector> >(size_));
133 
134  //Teuchos::RCP< std::vector< MultiVector * > > x = Teuchos::rcp( new std::vector< MultiVector * >(size_, &*Teuchos::rcp(new Vector( *((*xyce_multi_vec_)[0]->pmap()), *((*xyce_multi_vec_)[0]->omap()) ) ) ));
135 
136  //Teuchos::RCP< std::vector< MultiVector * > > x = Teuchos::rcp( new std::vector< MultiVector * >(size_, dynamic_cast< MultiVector* >(&*newMV(0)) ));
137 
138  //Teuchos::RCP< Vector > temp;
139  for (int i=0;i<size_;i++){
140  (*x)[i] = Teuchos::rcp( new Vector( *((*xyce_multi_vec_)[i]->pmap()), *((*xyce_multi_vec_)[i]->omap()) ) );
141 
142  // Using copy constructor (segfault)
143  //*((*x)[i]) = MultiVector(*((*xyce_multi_vec_)[i]));
144  }
145  //if (is_null(x)) std::cout << "x is null!" << std::endl;
146  return Teuchos::rcp( new ROL_XyceVector( x ));
147 
148  }
149 
150  void randomize() {
151  for (int i=0;i<size_;i++){
152  (*xyce_multi_vec_)[i]->random();
153  }
154  }
155 
156  void print(std::ostream & outStream=std::cout) {
157  for (int i=0;i<size_;i++){
158  (*xyce_multi_vec_)[i]->printPetraObject(outStream);
159  }
160  }
161 
162  /** \brief Compute \f$y \leftarrow \alpha x + y\f$ where \f$y = \mbox{*this}\f$.
163  */
164  virtual void axpy( const Real alpha, const ::ROL::Vector<Real> &x ) {
165  const ROL_XyceVector &ex = Teuchos::dyn_cast<const ROL_XyceVector>(x);
166  for (int i=0;i<size_;i++){
167  (*xyce_multi_vec_)[i]->addVec( (double)alpha, *( (*ex.getVector())[i] ) );
168  }
169  }
170 
171  /** \brief Set to zero vector.
172  */
173  virtual void zero() {
174  for (int i=0;i<size_;i++){
175  (*xyce_multi_vec_)[i]->putScalar( 0.0 );
176  }
177  }
178 
179  virtual void putScalar(Real alpha) {
180  for (int i=0;i<size_;i++){
181  (*xyce_multi_vec_)[i]->putScalar( (double)alpha );
182  }
183  }
184 
185  // /** \brief Set \f$y \leftarrow x\f$ where \f$y = \mbox{*this}\f$.
186  // */
187  // virtual void set( const Vector<Real> &x ) {
188  // const EpetraMultiVector &ex = Teuchos::dyn_cast<const EpetraMultiVector>(x);
189  // epetra_vec_->Scale(1.0,*ex.getVector());
190  // }
191 
192  // Teuchos::RCP<Vector<Real> > basis( const int i ) const {
193  // Teuchos::RCP<EpetraMultiVector> e = Teuchos::rcp( new EpetraMultiVector( Teuchos::rcp(new Epetra_MultiVector(epetra_vec_->Map(),epetra_vec_->NumVectors(),true)) ));
194  // const Epetra_BlockMap & domainMap = e->getVector()->Map();
195 
196  // Epetra_Map linearMap(domainMap.NumGlobalElements(), domainMap.NumMyElements(), 0, domainMap.Comm());
197  // int lid = linearMap.LID(i);
198  // if(lid >=0)
199  // (*e->getVector())[0][lid]= 1.0;
200 
201  // return e;
202 
203  // /*
204 
205 
206  // // Build IntVector of GIDs on all processors.
207  // const Epetra_Comm & comm = domainMap.Comm();
208  // int numMyElements = domainMap.NumMyElements();
209  // Epetra_BlockMap allGidsMap(-1, numMyElements, 1, 0, comm);
210  // Epetra_IntVector allGids(allGidsMap);
211  // for (int j=0; j<numMyElements; j++) {allGids[j] = domainMap.GID(j);}
212 
213  // // Import my GIDs into an all-inclusive map.
214  // int numGlobalElements = domainMap.NumGlobalElements();
215  // Epetra_LocalMap allGidsOnRootMap(numGlobalElements, 0, comm);
216  // Epetra_Import importer(allGidsOnRootMap, allGidsMap);
217  // Epetra_IntVector allGidsOnRoot(allGidsOnRootMap);
218  // allGidsOnRoot.Import(allGids, importer, Insert);
219  // Epetra_Map rootDomainMap(-1, allGidsOnRoot.MyLength(), allGidsOnRoot.Values(), domainMap.IndexBase(), comm);
220 
221  // for (int j = 0; j < this->dimension(); j++) {
222  // // Put 1's in slots
223  // int curGlobalCol = rootDomainMap.GID(i); // Should return same value on all processors
224  // if (domainMap.MyGID(curGlobalCol)){
225  // int curCol = domainMap.LID(curGlobalCol);
226  // (*e->getVector())[0][curCol]= 1.0;
227  // }
228  // }
229 
230  // return e;
231 
232  // */
233  // }
234 
235  // int dimension() const {return epetra_vec_->GlobalLength();}
236 
237 
238 }; // class XyceVector
239 
240 } // namespace Linear
241 } // namespace Xyce
242 
243 
244 #endif
245 
Teuchos::RCP< ::ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
virtual void zero()
Set to zero vector.
Real dot(const ::ROL::Vector< Real > &x) const
Returns where .
ROL_XyceVector(std::vector< Vector * > &xyce_multi_vec)
Pure virtual class to augment a linear system.
void scale(const Real alpha)
Compute where .
void print(std::ostream &outStream=std::cout)
ROL_XyceVector(const int size, const MultiVector &xyce_multi_vec)
Teuchos::RCP< std::vector< Teuchos::RCP< Vector > > > getVector()
Teuchos::RCP< std::vector< Teuchos::RCP< Vector > > > xyce_multi_vec_
void plus(const ::ROL::Vector< Real > &x)
Compute where .
Real norm() const
Returns where .
Teuchos::RCP< const std::vector< Teuchos::RCP< Vector > > > getVector() const
virtual void axpy(const Real alpha, const ::ROL::Vector< Real > &x)
Compute where .
ROL_XyceVector(const Teuchos::RCP< std::vector< Teuchos::RCP< Vector > > > &xyce_multi_vec)
virtual void putScalar(Real alpha)