FEI Version of the Day
fei_Vector_Impl.hpp
00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #ifndef _fei_Vector_Impl_hpp_
00010 #define _fei_Vector_Impl_hpp_
00011 
00012 #include <fei_macros.hpp>
00013 #include <fei_VectorTraits.hpp>
00014 
00015 #include <fei_VectorTraits_CSVec.hpp>
00016 #include <fei_VectorTraits_LinSysCore.hpp>
00017 #include <fei_VectorTraits_LinProbMgr.hpp>
00018 #include <fei_VectorTraits_FEData.hpp>
00019 #include <snl_fei_FEVectorTraits.hpp>
00020 #include <snl_fei_FEVectorTraits_FED.hpp>
00021 #include <fei_SharedIDs.hpp>
00022 #include <fei_VectorSpace.hpp>
00023 #include <fei_Reducer.hpp>
00024 #include <fei_Logger.hpp>
00025 #include <fei_Vector.hpp>
00026 #include <fei_Vector_core.hpp>
00027 #include <fei_iosfwd.hpp>
00028 
00029 #undef fei_file
00030 #define fei_file "fei_Vector_Impl.hpp"
00031 
00032 #include <fei_ErrMacros.hpp>
00033 
00034 namespace fei {
00035 
00056   template<typename T>
00057   class Vector_Impl : public fei::Vector, public fei::Vector_core {
00058   public:
00059 
00061     Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00062      T* vector, int numLocalEqns,
00063      bool isSolutionVector=false,
00064            bool deleteVector=false);
00065 
00067     virtual ~Vector_Impl();
00068 
00072     const char* typeName() const { return(fei::VectorTraits<T>::typeName()); }
00073 
00076     int update(double a,
00077          const fei::Vector* x,
00078          double b);
00079 
00086     int scatterToOverlap();
00087 
00088     void setCommSizes();
00089 
00093     int gatherFromOverlap(bool accumulate = true);
00094 
00096     int putScalar(double scalar);
00097 
00101     int sumIn(int numValues, const int* indices, const double* values,
00102         int vectorIndex=0);
00103 
00107     int copyIn(int numValues, const int* indices, const double* values,
00108          int vectorIndex=0);
00109 
00112     fei::SharedPtr<fei::VectorSpace> getVectorSpace() const
00113       { return(get_vector_space()); }
00114 
00117     void setVectorSpace(fei::SharedPtr<fei::VectorSpace> vecSpace)
00118     {
00119       set_vector_space( vecSpace );
00120     }
00121 
00128     int sumInFieldData(int fieldID,
00129            int idType,
00130            int numIDs,
00131            const int* IDs,
00132            const double* data,
00133            int vectorIndex=0);
00134 
00141     int copyInFieldData(int fieldID,
00142       int idType,
00143       int numIDs,
00144       const int* IDs,
00145       const double* data,
00146       int vectorIndex=0);
00147 
00148     int copyInFieldDataLocalIDs(int fieldID,
00149       int idType,
00150       int numIDs,
00151       const int* localIDs,
00152       const double* data,
00153       int vectorIndex=0);
00154 
00161     int copyOutFieldData(int fieldID,
00162        int idType,
00163        int numIDs,
00164        const int* IDs,
00165        double* data,
00166        int vectorIndex=0);
00167 
00168     int writeToFile(const char* filename,
00169         bool matrixMarketFormat=true);
00170 
00171     int writeToStream(FEI_OSTREAM& ostrm,
00172           bool matrixMarketFormat=true);
00173 
00177     void setUnderlyingVector(T* vec)
00178       {
00179   vector_ = vec;
00180       }
00181 
00185     T* getUnderlyingVector() { return( vector_ ); }
00186     const T* getUnderlyingVector() const { return( vector_ ); }
00187 
00188     int copyOut(int numValues,
00189     const int* indices,
00190     double* values,
00191     int vectorIndex=0) const;
00192 
00195     int copyOut_FE(int nodeNumber, int dofOffset, double& value);
00196 
00197   private:
00198     int giveToUnderlyingVector(int numValues,
00199              const int* indices,
00200              const double* values,
00201              bool sumInto=true,
00202              int vectorIndex=0);
00203 
00204     int copyOutOfUnderlyingVector(int numValues,
00205           const int* indices,
00206           double* values,
00207           int vectorIndex=0) const;
00208 
00209     int sumIntoFEVector(int blockID,
00210       int connOffset,
00211       int numNodes,
00212       const int* nodeNumbers,
00213       const int* numIndicesPerNode,
00214       const int* dof_ids,
00215       const double* values);
00216 
00217     T* vector_;
00218     bool isSolution_;
00219     bool deleteVector_;
00220 
00221     int localProc_;
00222     int numProcs_;
00223     std::string dbgprefix_;
00224   };//class Vector_Impl
00225 
00226 } //namespace fei
00227 
00228 //----------------------------------------------------------------------------
00229 template<typename T>
00230 fei::Vector_Impl<T>::Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00231          T* vector, int numLocalEqns,
00232          bool isSolutionVector,
00233                            bool deleteVector)
00234   : Vector_core(vecSpace, numLocalEqns),
00235     vector_(vector),
00236     isSolution_(isSolutionVector),
00237     deleteVector_(deleteVector),
00238     localProc_(0),
00239     numProcs_(1),
00240     dbgprefix_("VecImpl: ")
00241 {
00242   if (strcmp(snl_fei::FEVectorTraits<T>::typeName(), "unsupported")) {
00243     setFEVector(true);
00244   }
00245   else {
00246     setFEVector(false);
00247   }
00248 
00249   localProc_ = fei::localProc(vecSpace->getCommunicator());
00250   numProcs_ = fei::numProcs(vecSpace->getCommunicator());
00251 
00252   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00253     FEI_OSTREAM& os = *output_stream_;
00254     os << dbgprefix_<<" ctor, numLocalEqns="<<numLocalEqns
00255        <<", typeName: "<<typeName()<<FEI_ENDL;
00256   }
00257 
00258   std::vector<int> idTypes;
00259   vecSpace->getIDTypes(idTypes);
00260   std::vector<int> eqns;
00261   std::vector<double> zeros;
00262   for(size_t i=0; i<idTypes.size(); ++i) {
00263     int idType = idTypes[i];
00264     fei::SharedIDs<int>& sharedIDs = vecSpace->getSharedIDs(idType);
00265     const fei::SharedIDs<int>::map_type& idMap = sharedIDs.getSharedIDs();
00266     fei::SharedIDs<int>::map_type::const_iterator
00267       iter = idMap.begin(), iterEnd = idMap.end();
00268     for(; iter!=iterEnd; ++iter) {
00269       int ID = iter->first;
00270       int eqn;
00271       vecSpace->getGlobalIndex(idType, ID, eqn);
00272       int ndof = vecSpace->getNumDegreesOfFreedom(idType, ID);
00273       eqns.resize(ndof);
00274       zeros.resize(ndof, 0.0);
00275       for(int j=0; j<ndof; ++j) eqns[j] = eqn+j;
00276       if (!isSolutionVector) {
00277         sumIn(ndof, &eqns[0], &zeros[0]);
00278       }
00279       else {
00280         copyIn(ndof, &eqns[0], &zeros[0]);
00281       }
00282     }
00283   }
00284 
00285   setCommSizes();
00286   std::vector<CSVec*>& remoteVecs = remotelyOwned();
00287   for(size_t i=0; i<remoteVecs.size(); ++i) {
00288     remoteVecs[i]->clear();
00289   }
00290 }
00291 
00292 //----------------------------------------------------------------------------
00293 template<typename T>
00294 fei::Vector_Impl<T>::~Vector_Impl()
00295 {
00296   if (deleteVector_) delete vector_;
00297 }
00298 
00299 //----------------------------------------------------------------------------
00300 template<typename T>
00301 int fei::Vector_Impl<T>::putScalar(double scalar)
00302 {
00303   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00304     FEI_OSTREAM& os = *output_stream_;
00305     os << dbgprefix_<<"putScalar("<<scalar<<")"<<FEI_ENDL;
00306   }
00307 
00308   if (haveFEVector()) {
00309     if (scalar != 0.0) return(-1);
00310     CHK_ERR( snl_fei::FEVectorTraits<T>::reset(vector_) );
00311   }
00312   else {
00313     CHK_ERR( fei::VectorTraits<T>::setValues(vector_, firstLocalOffset(), scalar) );
00314   }
00315   for(unsigned p=0; p<remotelyOwned().size(); ++p) {
00316     fei::set_values(*(remotelyOwned()[p]), scalar);
00317   }
00318   return(0);
00319 }
00320 
00321 //----------------------------------------------------------------------------
00322 template<typename T>
00323 int fei::Vector_Impl<T>::update(double a,
00324              const fei::Vector* x,
00325              double b)
00326 {
00327   const fei::Vector_Impl<T>* sx = dynamic_cast<const fei::Vector_Impl<T>* >(x);
00328   if (sx != 0) {
00329     const T* tx = sx->getUnderlyingVector();
00330     return( fei::VectorTraits<T>::update(vector_, a, tx, b) );
00331   }
00332   else {
00333     return( -1 );
00334   }
00335 }
00336 
00337 //----------------------------------------------------------------------------
00338 template<typename T>
00339 int fei::Vector_Impl<T>::scatterToOverlap()
00340 {
00341   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00342     FEI_OSTREAM& os = *output_stream_;
00343     os << dbgprefix_<<"scatterToOverlap"<<FEI_ENDL;
00344   }
00345 
00346   return( Vector_core::scatterToOverlap() );
00347 }
00348 
00349 //----------------------------------------------------------------------------
00350 template<typename T>
00351 void fei::Vector_Impl<T>::setCommSizes()
00352 {
00353   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00354     FEI_OSTREAM& os = *output_stream_;
00355     os << dbgprefix_<<"setCommSizes"<<FEI_ENDL;
00356   }
00357 
00358   Vector_core::setCommSizes();
00359 }
00360 
00361 //----------------------------------------------------------------------------
00362 template<typename T>
00363 int fei::Vector_Impl<T>::gatherFromOverlap(bool accumulate)
00364 {
00365   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00366     FEI_OSTREAM& os = *output_stream_;
00367     os << dbgprefix_<<"gatherFromOverlap"<<FEI_ENDL;
00368   }
00369 
00370   return( Vector_core::gatherFromOverlap(accumulate) );
00371 }
00372 
00373 //----------------------------------------------------------------------------
00374 template<typename T>
00375 int fei::Vector_Impl<T>::sumIn(int numValues,
00376             const int* indices, const double* values,
00377             int vectorIndex)
00378 {
00379   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00380     FEI_OSTREAM& os = *output_stream_;
00381     os << dbgprefix_<<"sumIn(n="<<numValues<<")"<<FEI_ENDL;
00382   }
00383 
00384   return( giveToVector(numValues, indices, values, true, vectorIndex) );
00385 }
00386 
00387 //----------------------------------------------------------------------------
00388 template<typename T>
00389 int fei::Vector_Impl<T>::copyIn(int numValues,
00390              const int* indices, const double* values,
00391              int vectorIndex)
00392 {
00393   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00394     FEI_OSTREAM& os = *output_stream_;
00395     os << dbgprefix_<<"copyIn(n="<<numValues<<")"<<FEI_ENDL;
00396   }
00397 
00398   return( giveToVector(numValues, indices, values, false, vectorIndex) );
00399 }
00400 
00401 //----------------------------------------------------------------------------
00402 template<typename T>
00403 int fei::Vector_Impl<T>::giveToUnderlyingVector(int numValues,
00404                  const int* indices,
00405                  const double* values,
00406                  bool sumInto,
00407                  int vectorIndex)
00408 {
00409   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00410     FEI_OSTREAM& os = *output_stream_;
00411     os << dbgprefix_<<"giveToUnderlying(";
00412     for(int i=0; i<numValues; ++i) {
00413       os << "{"<<indices[i]<<","<<values[i]<<"} ";
00414     }
00415     os<<")"<<FEI_ENDL;
00416   }
00417 
00418   int err = fei::VectorTraits<T>::putValuesIn(vector_, firstLocalOffset(),
00419                numValues, indices, values,
00420                sumInto, isSolution_, vectorIndex);
00421   if (err < 0) {
00422     return(err);
00423   }
00424   return(0);
00425 }
00426 
00427 //----------------------------------------------------------------------------
00428 template<typename T>
00429 int fei::Vector_Impl<T>::copyOutOfUnderlyingVector(int numValues,
00430               const int* indices,
00431               double* values,
00432               int vectorIndex) const
00433 {
00434   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00435     FEI_OSTREAM& os = *output_stream_;
00436     os << dbgprefix_<<"copyOutOfUnderlying(n="<<numValues<<")"<<FEI_ENDL;
00437   }
00438 
00439   return( fei::VectorTraits<T>::copyOut(vector_, firstLocalOffset(),
00440                numValues, indices, values,
00441                isSolution_, vectorIndex) );
00442 }
00443 
00444 //----------------------------------------------------------------------------
00445 template<typename T>
00446 int fei::Vector_Impl<T>::sumInFieldData(int fieldID,
00447                int idType,
00448                int numIDs,
00449                const int* IDs,
00450                const double* data,
00451                int vectorIndex)
00452 {
00453   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00454     FEI_OSTREAM& os = *output_stream_;
00455     os << dbgprefix_<<"sumInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00456   }
00457 
00458   return( assembleFieldData(fieldID, idType, numIDs, IDs, data, true, vectorIndex));
00459 }
00460 
00461 //----------------------------------------------------------------------------
00462 template<typename T>
00463 int fei::Vector_Impl<T>::copyInFieldData(int fieldID,
00464           int idType,
00465           int numIDs,
00466           const int* IDs,
00467           const double* data,
00468           int vectorIndex)
00469 {
00470   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00471     FEI_OSTREAM& os = *output_stream_;
00472     os << dbgprefix_<<"copyInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00473   }
00474 
00475   return(assembleFieldData(fieldID, idType, numIDs, IDs, data, false, vectorIndex));
00476 }
00477 
00478 //----------------------------------------------------------------------------
00479 template<typename T>
00480 int fei::Vector_Impl<T>::copyInFieldDataLocalIDs(int fieldID,
00481           int idType,
00482           int numIDs,
00483           const int* localIDs,
00484           const double* data,
00485           int vectorIndex)
00486 {
00487   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00488     FEI_OSTREAM& os = *output_stream_;
00489     os << dbgprefix_<<"copyInFieldDataLocalIDs(n="<<numIDs<<")"<<FEI_ENDL;
00490   }
00491 
00492   return(assembleFieldDataLocalIDs(fieldID, idType, numIDs, localIDs, data, false, vectorIndex));
00493 }
00494 
00495 //----------------------------------------------------------------------------
00496 template<typename T>
00497 int fei::Vector_Impl<T>::copyOut_FE(int nodeNumber, int dofOffset, double& value)
00498 {
00499   return( snl_fei::FEVectorTraits<T>::copyOut(vector_, nodeNumber, dofOffset, value) );
00500 }
00501 
00502 //----------------------------------------------------------------------------
00503 template<typename T>
00504 int fei::Vector_Impl<T>::copyOutFieldData(int fieldID,
00505            int idType,
00506            int numIDs,
00507            const int* IDs,
00508            double* data,
00509            int vectorIndex)
00510 {
00511   return( Vector_core::copyOutFieldData(fieldID, idType, numIDs, IDs, data,
00512            vectorIndex));
00513 }
00514 
00515 //----------------------------------------------------------------------------
00516 template<typename T>
00517 int fei::Vector_Impl<T>::writeToFile(const char* filename,
00518             bool matrixMarketFormat)
00519 {
00520   return( Vector_core::writeToFile(filename, matrixMarketFormat) );
00521 }
00522 
00523 //----------------------------------------------------------------------------
00524 template<typename T>
00525 int fei::Vector_Impl<T>::writeToStream(FEI_OSTREAM& ostrm,
00526               bool matrixMarketFormat)
00527 {
00528   return( Vector_core::writeToStream(ostrm, matrixMarketFormat) );
00529 }
00530 
00531 //----------------------------------------------------------------------------
00532 template<typename T>
00533 int fei::Vector_Impl<T>::copyOut(int numValues,
00534         const int* indices,
00535         double* values,
00536         int vectorIndex) const
00537 {
00538   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00539     FEI_OSTREAM& os = *output_stream_;
00540     os << dbgprefix_<<"copyOut(n="<<numValues<<")"<<FEI_ENDL;
00541   }
00542 
00543   return( Vector_core::copyOut(numValues, indices, values, vectorIndex) );
00544 }
00545 
00546 //----------------------------------------------------------------------------
00547 template<typename T>
00548 int fei::Vector_Impl<T>::sumIntoFEVector(int blockID,
00549           int connOffset,
00550           int numNodes,
00551           const int* nodeNumbers,
00552           const int* numIndicesPerNode,
00553           const int* dof_ids,
00554           const double* values)
00555 {
00556   return( snl_fei::FEVectorTraits<T>::sumInElemVector(vector_, blockID, connOffset,
00557                 numNodes, nodeNumbers,
00558                 numIndicesPerNode, dof_ids, values) );
00559 }
00560 
00561 #undef fei_file
00562 #define fei_file "unknown_file"
00563 
00564 #endif // _fei_Vector_Impl_hpp_
00565 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends