FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fei_Vector_Impl.hpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #ifndef _fei_Vector_Impl_hpp_
10 #define _fei_Vector_Impl_hpp_
11 
12 #include <fei_macros.hpp>
13 #include <fei_VectorTraits.hpp>
14 
15 #include <fei_VectorTraits_CSVec.hpp>
16 #include <fei_VectorTraits_LinSysCore.hpp>
17 #include <fei_VectorTraits_LinProbMgr.hpp>
18 #include <fei_VectorTraits_FEData.hpp>
19 #include <snl_fei_FEVectorTraits.hpp>
20 #include <snl_fei_FEVectorTraits_FED.hpp>
21 #include <fei_SharedIDs.hpp>
22 #include <fei_VectorSpace.hpp>
23 #include <fei_Reducer.hpp>
24 #include <fei_Logger.hpp>
25 #include <fei_Vector.hpp>
26 #include <fei_Vector_core.hpp>
27 #include <fei_iosfwd.hpp>
28 
29 #undef fei_file
30 #define fei_file "fei_Vector_Impl.hpp"
31 
32 #include <fei_ErrMacros.hpp>
33 
34 namespace fei {
35 
56  template<typename T>
57  class Vector_Impl : public fei::Vector, public fei::Vector_core {
58  public:
59 
62  T* vector, int numLocalEqns,
63  bool isSolutionVector=false,
64  bool deleteVector=false);
65 
67  virtual ~Vector_Impl();
68 
72  const char* typeName() const { return(fei::VectorTraits<T>::typeName()); }
73 
76  int update(double a,
77  const fei::Vector* x,
78  double b);
79 
86  int scatterToOverlap();
87 
88  void setCommSizes();
89 
93  int gatherFromOverlap(bool accumulate = true);
94 
96  int putScalar(double scalar);
97 
101  int sumIn(int numValues, const int* indices, const double* values,
102  int vectorIndex=0);
103 
107  int copyIn(int numValues, const int* indices, const double* values,
108  int vectorIndex=0);
109 
113  { return(get_vector_space()); }
114 
118  {
119  set_vector_space( vecSpace );
120  }
121 
128  int sumInFieldData(int fieldID,
129  int idType,
130  int numIDs,
131  const int* IDs,
132  const double* data,
133  int vectorIndex=0);
134 
141  int copyInFieldData(int fieldID,
142  int idType,
143  int numIDs,
144  const int* IDs,
145  const double* data,
146  int vectorIndex=0);
147 
148  int copyInFieldDataLocalIDs(int fieldID,
149  int idType,
150  int numIDs,
151  const int* localIDs,
152  const double* data,
153  int vectorIndex=0);
154 
161  int copyOutFieldData(int fieldID,
162  int idType,
163  int numIDs,
164  const int* IDs,
165  double* data,
166  int vectorIndex=0);
167 
168  int writeToFile(const char* filename,
169  bool matrixMarketFormat=true);
170 
171  int writeToStream(FEI_OSTREAM& ostrm,
172  bool matrixMarketFormat=true);
173 
177  void setUnderlyingVector(T* vec)
178  {
179  vector_ = vec;
180  }
181 
185  T* getUnderlyingVector() { return( vector_ ); }
186  const T* getUnderlyingVector() const { return( vector_ ); }
187 
188  int copyOut(int numValues,
189  const int* indices,
190  double* values,
191  int vectorIndex=0) const;
192 
195  int copyOut_FE(int nodeNumber, int dofOffset, double& value);
196 
197  private:
198  int giveToUnderlyingVector(int numValues,
199  const int* indices,
200  const double* values,
201  bool sumInto=true,
202  int vectorIndex=0);
203 
204  int copyOutOfUnderlyingVector(int numValues,
205  const int* indices,
206  double* values,
207  int vectorIndex=0) const;
208 
209  int sumIntoFEVector(int blockID,
210  int connOffset,
211  int numNodes,
212  const int* nodeNumbers,
213  const int* numIndicesPerNode,
214  const int* dof_ids,
215  const double* values);
216 
217  T* vector_;
218  bool isSolution_;
219  bool deleteVector_;
220 
221  int localProc_;
222  int numProcs_;
223  std::string dbgprefix_;
224  };//class Vector_Impl
225 
226 } //namespace fei
227 
228 //----------------------------------------------------------------------------
229 template<typename T>
231  T* vector, int numLocalEqns,
232  bool isSolutionVector,
233  bool deleteVector)
234  : Vector_core(vecSpace, numLocalEqns),
235  vector_(vector),
236  isSolution_(isSolutionVector),
237  deleteVector_(deleteVector),
238  localProc_(0),
239  numProcs_(1),
240  dbgprefix_("VecImpl: ")
241 {
242  if (strcmp(snl_fei::FEVectorTraits<T>::typeName(), "unsupported")) {
243  setFEVector(true);
244  }
245  else {
246  setFEVector(false);
247  }
248 
249  localProc_ = fei::localProc(vecSpace->getCommunicator());
250  numProcs_ = fei::numProcs(vecSpace->getCommunicator());
251 
252  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
253  FEI_OSTREAM& os = *output_stream_;
254  os << dbgprefix_<<" ctor, numLocalEqns="<<numLocalEqns
255  <<", typeName: "<<typeName()<<FEI_ENDL;
256  }
257 
258  std::vector<int> idTypes;
259  vecSpace->getIDTypes(idTypes);
260  std::vector<int> eqns;
261  std::vector<double> zeros;
262  for(size_t i=0; i<idTypes.size(); ++i) {
263  int idType = idTypes[i];
264  fei::SharedIDs<int>& sharedIDs = vecSpace->getSharedIDs(idType);
265  const fei::SharedIDs<int>::map_type& idMap = sharedIDs.getSharedIDs();
267  iter = idMap.begin(), iterEnd = idMap.end();
268  for(; iter!=iterEnd; ++iter) {
269  int ID = iter->first;
270  int eqn;
271  vecSpace->getGlobalIndex(idType, ID, eqn);
272  int ndof = vecSpace->getNumDegreesOfFreedom(idType, ID);
273  eqns.resize(ndof);
274  zeros.resize(ndof, 0.0);
275  for(int j=0; j<ndof; ++j) eqns[j] = eqn+j;
276  if (!isSolutionVector) {
277  sumIn(ndof, &eqns[0], &zeros[0]);
278  }
279  else {
280  copyIn(ndof, &eqns[0], &zeros[0]);
281  }
282  }
283  }
284 
285  setCommSizes();
286  std::vector<CSVec*>& remoteVecs = remotelyOwned();
287  for(size_t i=0; i<remoteVecs.size(); ++i) {
288  remoteVecs[i]->clear();
289  }
290 }
291 
292 //----------------------------------------------------------------------------
293 template<typename T>
295 {
296  if (deleteVector_) delete vector_;
297 }
298 
299 //----------------------------------------------------------------------------
300 template<typename T>
301 int fei::Vector_Impl<T>::putScalar(double scalar)
302 {
303  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
304  FEI_OSTREAM& os = *output_stream_;
305  os << dbgprefix_<<"putScalar("<<scalar<<")"<<FEI_ENDL;
306  }
307 
308  if (haveFEVector()) {
309  if (scalar != 0.0) return(-1);
310  CHK_ERR( snl_fei::FEVectorTraits<T>::reset(vector_) );
311  }
312  else {
313  CHK_ERR( fei::VectorTraits<T>::setValues(vector_, firstLocalOffset(), scalar) );
314  }
315  for(unsigned p=0; p<remotelyOwned().size(); ++p) {
316  fei::set_values(*(remotelyOwned()[p]), scalar);
317  }
318  return(0);
319 }
320 
321 //----------------------------------------------------------------------------
322 template<typename T>
323 int fei::Vector_Impl<T>::update(double a,
324  const fei::Vector* x,
325  double b)
326 {
327  const fei::Vector_Impl<T>* sx = dynamic_cast<const fei::Vector_Impl<T>* >(x);
328  if (sx != 0) {
329  const T* tx = sx->getUnderlyingVector();
330  return( fei::VectorTraits<T>::update(vector_, a, tx, b) );
331  }
332  else {
333  return( -1 );
334  }
335 }
336 
337 //----------------------------------------------------------------------------
338 template<typename T>
340 {
341  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
342  FEI_OSTREAM& os = *output_stream_;
343  os << dbgprefix_<<"scatterToOverlap"<<FEI_ENDL;
344  }
345 
346  return( Vector_core::scatterToOverlap() );
347 }
348 
349 //----------------------------------------------------------------------------
350 template<typename T>
352 {
353  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
354  FEI_OSTREAM& os = *output_stream_;
355  os << dbgprefix_<<"setCommSizes"<<FEI_ENDL;
356  }
357 
358  Vector_core::setCommSizes();
359 }
360 
361 //----------------------------------------------------------------------------
362 template<typename T>
363 int fei::Vector_Impl<T>::gatherFromOverlap(bool accumulate)
364 {
365  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
366  FEI_OSTREAM& os = *output_stream_;
367  os << dbgprefix_<<"gatherFromOverlap"<<FEI_ENDL;
368  }
369 
370  return( Vector_core::gatherFromOverlap(accumulate) );
371 }
372 
373 //----------------------------------------------------------------------------
374 template<typename T>
375 int fei::Vector_Impl<T>::sumIn(int numValues,
376  const int* indices, const double* values,
377  int vectorIndex)
378 {
379  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
380  FEI_OSTREAM& os = *output_stream_;
381  os << dbgprefix_<<"sumIn(n="<<numValues<<")"<<FEI_ENDL;
382  }
383 
384  return( giveToVector(numValues, indices, values, true, vectorIndex) );
385 }
386 
387 //----------------------------------------------------------------------------
388 template<typename T>
389 int fei::Vector_Impl<T>::copyIn(int numValues,
390  const int* indices, const double* values,
391  int vectorIndex)
392 {
393  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
394  FEI_OSTREAM& os = *output_stream_;
395  os << dbgprefix_<<"copyIn(n="<<numValues<<")"<<FEI_ENDL;
396  }
397 
398  return( giveToVector(numValues, indices, values, false, vectorIndex) );
399 }
400 
401 //----------------------------------------------------------------------------
402 template<typename T>
404  const int* indices,
405  const double* values,
406  bool sumInto,
407  int vectorIndex)
408 {
409  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
410  FEI_OSTREAM& os = *output_stream_;
411  os << dbgprefix_<<"giveToUnderlying(";
412  for(int i=0; i<numValues; ++i) {
413  os << "{"<<indices[i]<<","<<values[i]<<"} ";
414  }
415  os<<")"<<FEI_ENDL;
416  }
417 
418  int err = fei::VectorTraits<T>::putValuesIn(vector_, firstLocalOffset(),
419  numValues, indices, values,
420  sumInto, isSolution_, vectorIndex);
421  if (err < 0) {
422  return(err);
423  }
424  return(0);
425 }
426 
427 //----------------------------------------------------------------------------
428 template<typename T>
430  const int* indices,
431  double* values,
432  int vectorIndex) const
433 {
434  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
435  FEI_OSTREAM& os = *output_stream_;
436  os << dbgprefix_<<"copyOutOfUnderlying(n="<<numValues<<")"<<FEI_ENDL;
437  }
438 
439  return( fei::VectorTraits<T>::copyOut(vector_, firstLocalOffset(),
440  numValues, indices, values,
441  isSolution_, vectorIndex) );
442 }
443 
444 //----------------------------------------------------------------------------
445 template<typename T>
447  int idType,
448  int numIDs,
449  const int* IDs,
450  const double* data,
451  int vectorIndex)
452 {
453  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
454  FEI_OSTREAM& os = *output_stream_;
455  os << dbgprefix_<<"sumInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
456  }
457 
458  return( assembleFieldData(fieldID, idType, numIDs, IDs, data, true, vectorIndex));
459 }
460 
461 //----------------------------------------------------------------------------
462 template<typename T>
464  int idType,
465  int numIDs,
466  const int* IDs,
467  const double* data,
468  int vectorIndex)
469 {
470  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
471  FEI_OSTREAM& os = *output_stream_;
472  os << dbgprefix_<<"copyInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
473  }
474 
475  return(assembleFieldData(fieldID, idType, numIDs, IDs, data, false, vectorIndex));
476 }
477 
478 //----------------------------------------------------------------------------
479 template<typename T>
481  int idType,
482  int numIDs,
483  const int* localIDs,
484  const double* data,
485  int vectorIndex)
486 {
487  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
488  FEI_OSTREAM& os = *output_stream_;
489  os << dbgprefix_<<"copyInFieldDataLocalIDs(n="<<numIDs<<")"<<FEI_ENDL;
490  }
491 
492  return(assembleFieldDataLocalIDs(fieldID, idType, numIDs, localIDs, data, false, vectorIndex));
493 }
494 
495 //----------------------------------------------------------------------------
496 template<typename T>
497 int fei::Vector_Impl<T>::copyOut_FE(int nodeNumber, int dofOffset, double& value)
498 {
499  return( snl_fei::FEVectorTraits<T>::copyOut(vector_, nodeNumber, dofOffset, value) );
500 }
501 
502 //----------------------------------------------------------------------------
503 template<typename T>
505  int idType,
506  int numIDs,
507  const int* IDs,
508  double* data,
509  int vectorIndex)
510 {
511  return( Vector_core::copyOutFieldData(fieldID, idType, numIDs, IDs, data,
512  vectorIndex));
513 }
514 
515 //----------------------------------------------------------------------------
516 template<typename T>
517 int fei::Vector_Impl<T>::writeToFile(const char* filename,
518  bool matrixMarketFormat)
519 {
520  return( Vector_core::writeToFile(filename, matrixMarketFormat) );
521 }
522 
523 //----------------------------------------------------------------------------
524 template<typename T>
525 int fei::Vector_Impl<T>::writeToStream(FEI_OSTREAM& ostrm,
526  bool matrixMarketFormat)
527 {
528  return( Vector_core::writeToStream(ostrm, matrixMarketFormat) );
529 }
530 
531 //----------------------------------------------------------------------------
532 template<typename T>
533 int fei::Vector_Impl<T>::copyOut(int numValues,
534  const int* indices,
535  double* values,
536  int vectorIndex) const
537 {
538  if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
539  FEI_OSTREAM& os = *output_stream_;
540  os << dbgprefix_<<"copyOut(n="<<numValues<<")"<<FEI_ENDL;
541  }
542 
543  return( Vector_core::copyOut(numValues, indices, values, vectorIndex) );
544 }
545 
546 //----------------------------------------------------------------------------
547 template<typename T>
549  int connOffset,
550  int numNodes,
551  const int* nodeNumbers,
552  const int* numIndicesPerNode,
553  const int* dof_ids,
554  const double* values)
555 {
556  return( snl_fei::FEVectorTraits<T>::sumInElemVector(vector_, blockID, connOffset,
557  numNodes, nodeNumbers,
558  numIndicesPerNode, dof_ids, values) );
559 }
560 
561 #undef fei_file
562 #define fei_file "unknown_file"
563 
564 #endif // _fei_Vector_Impl_hpp_
565 
MPI_Comm getCommunicator() const
static int putValuesIn(T *vec, int firstLocalOffset, int numValues, const int *indices, const double *values, bool sum_into, bool isSolnVector=false, int vectorIndex=0)
std::map< T, std::set< int > > map_type
map_type & getSharedIDs()
fei::SharedPtr< fei::VectorSpace > getVectorSpace() const
int writeToStream(FEI_OSTREAM &ostrm, bool matrixMarketFormat=true)
int sumInFieldData(int fieldID, int idType, int numIDs, const int *IDs, const double *data, int vectorIndex=0)
int copyOut(int numValues, const int *indices, double *values, int vectorIndex=0) const
void set_vector_space(fei::SharedPtr< fei::VectorSpace > vspace)
int getGlobalIndex(int idType, int ID, int fieldID, int fieldOffset, int whichComponentOfField, int &globalIndex)
int copyOutFieldData(int fieldID, int idType, int numIDs, const int *IDs, double *data, int vectorIndex=0)
void getIDTypes(std::vector< int > &idTypes) const
int putScalar(double scalar)
int copyIn(int numValues, const int *indices, const double *values, int vectorIndex=0)
const char * typeName() const
int update(double a, const fei::Vector *x, double b)
int copyInFieldData(int fieldID, int idType, int numIDs, const int *IDs, const double *data, int vectorIndex=0)
fei::SharedPtr< fei::VectorSpace > get_vector_space() const
T * getUnderlyingVector()
int localProc(MPI_Comm comm)
int gatherFromOverlap(bool accumulate=true)
int writeToFile(const char *filename, bool matrixMarketFormat=true)
void setUnderlyingVector(T *vec)
int sumIn(int numValues, const int *indices, const double *values, int vectorIndex=0)
int getNumDegreesOfFreedom(int idType, int ID)
void setVectorSpace(fei::SharedPtr< fei::VectorSpace > vecSpace)
std::string typeName(const T &t)
int numProcs(MPI_Comm comm)
int copyOut_FE(int nodeNumber, int dofOffset, double &value)
Vector_Impl(fei::SharedPtr< fei::VectorSpace > vecSpace, T *vector, int numLocalEqns, bool isSolutionVector=false, bool deleteVector=false)