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_FillableVec.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_VectorSpace.hpp>
00022 #include <fei_Reducer.hpp>
00023 #include <fei_Logger.hpp>
00024 #include <fei_Vector.hpp>
00025 #include <fei_Vector_core.hpp>
00026 #include <fei_iosfwd.hpp>
00027 
00028 #undef fei_file
00029 #define fei_file "fei_Vector_Impl.hpp"
00030 
00031 #include <fei_ErrMacros.hpp>
00032 
00033 namespace fei {
00034 
00055   template<typename T>
00056   class Vector_Impl : public fei::Vector, public fei::Vector_core {
00057   public:
00058 
00060     Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00061      T* vector, int numLocalEqns,
00062      bool isSolutionVector=false,
00063            bool deleteVector=false);
00064 
00066     virtual ~Vector_Impl();
00067 
00071     const char* typeName() const { return(fei::VectorTraits<T>::typeName()); }
00072 
00075     int update(double a,
00076          const fei::Vector* x,
00077          double b);
00078 
00085     int scatterToOverlap();
00086 
00090     int gatherFromOverlap(bool accumulate = true);
00091 
00093     int putScalar(double scalar);
00094 
00098     int sumIn(int numValues, const int* indices, const double* values,
00099         int vectorIndex=0);
00100 
00104     int copyIn(int numValues, const int* indices, const double* values,
00105          int vectorIndex=0);
00106 
00109     fei::SharedPtr<fei::VectorSpace> getVectorSpace() const
00110       { return(get_vector_space()); }
00111 
00114     void setVectorSpace(fei::SharedPtr<fei::VectorSpace> vecSpace)
00115     {
00116       set_vector_space( vecSpace );
00117     }
00118 
00125     int sumInFieldData(int fieldID,
00126            int idType,
00127            int numIDs,
00128            const int* IDs,
00129            const double* data,
00130            int vectorIndex=0);
00131 
00138     int copyInFieldData(int fieldID,
00139       int idType,
00140       int numIDs,
00141       const int* IDs,
00142       const double* data,
00143       int vectorIndex=0);
00144 
00151     int copyOutFieldData(int fieldID,
00152        int idType,
00153        int numIDs,
00154        const int* IDs,
00155        double* data,
00156        int vectorIndex=0);
00157 
00158     int writeToFile(const char* filename,
00159         bool matrixMarketFormat=true);
00160 
00161     int writeToStream(FEI_OSTREAM& ostrm,
00162           bool matrixMarketFormat=true);
00163 
00167     void setUnderlyingVector(T* vec)
00168       {
00169   vector_ = vec;
00170       }
00171 
00175     T* getUnderlyingVector() { return( vector_ ); }
00176     const T* getUnderlyingVector() const { return( vector_ ); }
00177 
00178     int copyOut(int numValues,
00179     const int* indices,
00180     double* values,
00181     int vectorIndex=0) const;
00182 
00185     int copyOut_FE(int nodeNumber, int dofOffset, double& value);
00186 
00187   private:
00188     int giveToUnderlyingVector(int numValues,
00189              const int* indices,
00190              const double* values,
00191              bool sumInto=true,
00192              int vectorIndex=0);
00193 
00194     int copyOutOfUnderlyingVector(int numValues,
00195           const int* indices,
00196           double* values,
00197           int vectorIndex=0) const;
00198 
00199     int sumIntoFEVector(int blockID,
00200       int connOffset,
00201       int numNodes,
00202       const int* nodeNumbers,
00203       const int* numIndicesPerNode,
00204       const int* dof_ids,
00205       const double* values);
00206 
00207     T* vector_;
00208     bool isSolution_;
00209     bool deleteVector_;
00210 
00211     int localProc_;
00212     int numProcs_;
00213     std::string dbgprefix_;
00214   };//class Vector_Impl
00215 
00216 } //namespace fei
00217 
00218 //----------------------------------------------------------------------------
00219 template<typename T>
00220 fei::Vector_Impl<T>::Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00221          T* vector, int numLocalEqns,
00222          bool isSolutionVector,
00223                            bool deleteVector)
00224   : Vector_core(vecSpace, numLocalEqns),
00225     vector_(vector),
00226     isSolution_(isSolutionVector),
00227     deleteVector_(deleteVector),
00228     localProc_(0),
00229     numProcs_(1),
00230     dbgprefix_("VecImpl: ")
00231 {
00232   if (strcmp(snl_fei::FEVectorTraits<T>::typeName(), "unsupported")) {
00233     setFEVector(true);
00234   }
00235   else {
00236     setFEVector(false);
00237   }
00238 
00239   localProc_ = fei::localProc(vecSpace->getCommunicator());
00240   numProcs_ = fei::numProcs(vecSpace->getCommunicator());
00241 
00242   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00243     FEI_OSTREAM& os = *output_stream_;
00244     os << dbgprefix_<<" ctor, numLocalEqns="<<numLocalEqns
00245        <<", typeName: "<<typeName()<<FEI_ENDL;
00246   }
00247 }
00248 
00249 //----------------------------------------------------------------------------
00250 template<typename T>
00251 fei::Vector_Impl<T>::~Vector_Impl()
00252 {
00253   if (deleteVector_) delete vector_;
00254 }
00255 
00256 //----------------------------------------------------------------------------
00257 template<typename T>
00258 int fei::Vector_Impl<T>::putScalar(double scalar)
00259 {
00260   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00261     FEI_OSTREAM& os = *output_stream_;
00262     os << dbgprefix_<<"putScalar("<<scalar<<")"<<FEI_ENDL;
00263   }
00264 
00265   if (haveFEVector()) {
00266     if (scalar != 0.0) return(-1);
00267     CHK_ERR( snl_fei::FEVectorTraits<T>::reset(vector_) );
00268   }
00269   else {
00270     CHK_ERR( fei::VectorTraits<T>::setValues(vector_, firstLocalOffset(), scalar) );
00271   }
00272   for(unsigned p=0; p<remotelyOwned().size(); ++p) {
00273     fei::set_values(*(remotelyOwned()[p]), scalar);
00274   }
00275   return(0);
00276 }
00277 
00278 //----------------------------------------------------------------------------
00279 template<typename T>
00280 int fei::Vector_Impl<T>::update(double a,
00281              const fei::Vector* x,
00282              double b)
00283 {
00284   const fei::Vector_Impl<T>* sx = dynamic_cast<const fei::Vector_Impl<T>* >(x);
00285   if (sx != 0) {
00286     const T* tx = sx->getUnderlyingVector();
00287     return( fei::VectorTraits<T>::update(vector_, a, tx, b) );
00288   }
00289   else {
00290     return( -1 );
00291   }
00292 }
00293 
00294 //----------------------------------------------------------------------------
00295 template<typename T>
00296 int fei::Vector_Impl<T>::scatterToOverlap()
00297 {
00298   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00299     FEI_OSTREAM& os = *output_stream_;
00300     os << dbgprefix_<<"scatterToOverlap"<<FEI_ENDL;
00301   }
00302 
00303   return( Vector_core::scatterToOverlap() );
00304 }
00305 
00306 //----------------------------------------------------------------------------
00307 template<typename T>
00308 int fei::Vector_Impl<T>::gatherFromOverlap(bool accumulate)
00309 {
00310   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00311     FEI_OSTREAM& os = *output_stream_;
00312     os << dbgprefix_<<"gatherFromOverlap"<<FEI_ENDL;
00313   }
00314 
00315   return( Vector_core::gatherFromOverlap(accumulate) );
00316 }
00317 
00318 //----------------------------------------------------------------------------
00319 template<typename T>
00320 int fei::Vector_Impl<T>::sumIn(int numValues,
00321             const int* indices, const double* values,
00322             int vectorIndex)
00323 {
00324   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00325     FEI_OSTREAM& os = *output_stream_;
00326     os << dbgprefix_<<"sumIn(n="<<numValues<<")"<<FEI_ENDL;
00327   }
00328 
00329   return( giveToVector(numValues, indices, values, true, vectorIndex) );
00330 }
00331 
00332 //----------------------------------------------------------------------------
00333 template<typename T>
00334 int fei::Vector_Impl<T>::copyIn(int numValues,
00335              const int* indices, const double* values,
00336              int vectorIndex)
00337 {
00338   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00339     FEI_OSTREAM& os = *output_stream_;
00340     os << dbgprefix_<<"copyIn(n="<<numValues<<")"<<FEI_ENDL;
00341   }
00342 
00343   return( giveToVector(numValues, indices, values, false, vectorIndex) );
00344 }
00345 
00346 //----------------------------------------------------------------------------
00347 template<typename T>
00348 int fei::Vector_Impl<T>::giveToUnderlyingVector(int numValues,
00349                  const int* indices,
00350                  const double* values,
00351                  bool sumInto,
00352                  int vectorIndex)
00353 {
00354   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00355     FEI_OSTREAM& os = *output_stream_;
00356     os << dbgprefix_<<"giveToUnderlying(";
00357     for(int i=0; i<numValues; ++i) {
00358       os << "{"<<indices[i]<<","<<values[i]<<"} ";
00359     }
00360     os<<")"<<FEI_ENDL;
00361   }
00362 
00363   int err = fei::VectorTraits<T>::putValuesIn(vector_, firstLocalOffset(),
00364                numValues, indices, values,
00365                sumInto, isSolution_, vectorIndex);
00366   if (err < 0) {
00367     return(err);
00368   }
00369   return(0);
00370 }
00371 
00372 //----------------------------------------------------------------------------
00373 template<typename T>
00374 int fei::Vector_Impl<T>::copyOutOfUnderlyingVector(int numValues,
00375               const int* indices,
00376               double* values,
00377               int vectorIndex) const
00378 {
00379   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00380     FEI_OSTREAM& os = *output_stream_;
00381     os << dbgprefix_<<"copyOutOfUnderlying(n="<<numValues<<")"<<FEI_ENDL;
00382   }
00383 
00384   return( fei::VectorTraits<T>::copyOut(vector_, firstLocalOffset(),
00385                numValues, indices, values,
00386                isSolution_, vectorIndex) );
00387 }
00388 
00389 //----------------------------------------------------------------------------
00390 template<typename T>
00391 int fei::Vector_Impl<T>::sumInFieldData(int fieldID,
00392                int idType,
00393                int numIDs,
00394                const int* IDs,
00395                const double* data,
00396                int vectorIndex)
00397 {
00398   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00399     FEI_OSTREAM& os = *output_stream_;
00400     os << dbgprefix_<<"sumInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00401   }
00402 
00403   return( assembleFieldData(fieldID, idType, numIDs, IDs, data, true, vectorIndex));
00404 }
00405 
00406 //----------------------------------------------------------------------------
00407 template<typename T>
00408 int fei::Vector_Impl<T>::copyInFieldData(int fieldID,
00409           int idType,
00410           int numIDs,
00411           const int* IDs,
00412           const double* data,
00413           int vectorIndex)
00414 {
00415   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00416     FEI_OSTREAM& os = *output_stream_;
00417     os << dbgprefix_<<"copyInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00418   }
00419 
00420   return(assembleFieldData(fieldID, idType, numIDs, IDs, data, false, vectorIndex));
00421 }
00422 
00423 //----------------------------------------------------------------------------
00424 template<typename T>
00425 int fei::Vector_Impl<T>::copyOut_FE(int nodeNumber, int dofOffset, double& value)
00426 {
00427   return( snl_fei::FEVectorTraits<T>::copyOut(vector_, nodeNumber, dofOffset, value) );
00428 }
00429 
00430 //----------------------------------------------------------------------------
00431 template<typename T>
00432 int fei::Vector_Impl<T>::copyOutFieldData(int fieldID,
00433            int idType,
00434            int numIDs,
00435            const int* IDs,
00436            double* data,
00437            int vectorIndex)
00438 {
00439   return( Vector_core::copyOutFieldData(fieldID, idType, numIDs, IDs, data,
00440            vectorIndex));
00441 }
00442 
00443 //----------------------------------------------------------------------------
00444 template<typename T>
00445 int fei::Vector_Impl<T>::writeToFile(const char* filename,
00446             bool matrixMarketFormat)
00447 {
00448   return( Vector_core::writeToFile(filename, matrixMarketFormat) );
00449 }
00450 
00451 //----------------------------------------------------------------------------
00452 template<typename T>
00453 int fei::Vector_Impl<T>::writeToStream(FEI_OSTREAM& ostrm,
00454               bool matrixMarketFormat)
00455 {
00456   return( Vector_core::writeToStream(ostrm, matrixMarketFormat) );
00457 }
00458 
00459 //----------------------------------------------------------------------------
00460 template<typename T>
00461 int fei::Vector_Impl<T>::copyOut(int numValues,
00462         const int* indices,
00463         double* values,
00464         int vectorIndex) const
00465 {
00466   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00467     FEI_OSTREAM& os = *output_stream_;
00468     os << dbgprefix_<<"copyOut(n="<<numValues<<")"<<FEI_ENDL;
00469   }
00470 
00471   return( Vector_core::copyOut(numValues, indices, values, vectorIndex) );
00472 }
00473 
00474 //----------------------------------------------------------------------------
00475 template<typename T>
00476 int fei::Vector_Impl<T>::sumIntoFEVector(int blockID,
00477           int connOffset,
00478           int numNodes,
00479           const int* nodeNumbers,
00480           const int* numIndicesPerNode,
00481           const int* dof_ids,
00482           const double* values)
00483 {
00484   return( snl_fei::FEVectorTraits<T>::sumInElemVector(vector_, blockID, connOffset,
00485                 numNodes, nodeNumbers,
00486                 numIndicesPerNode, dof_ids, values) );
00487 }
00488 
00489 #undef fei_file
00490 #define fei_file "unknown_file"
00491 
00492 #endif // _fei_Vector_Impl_hpp_
00493 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:08:24 2011 for FEI by  doxygen 1.6.3