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()
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 double* values);
00205 
00206     T* vector_;
00207     bool isSolution_;
00208     bool deleteVector_;
00209 
00210     int localProc_;
00211     int numProcs_;
00212     std::string dbgprefix_;
00213   };//class Vector_Impl
00214 
00215 } //namespace fei
00216 
00217 //----------------------------------------------------------------------------
00218 template<typename T>
00219 fei::Vector_Impl<T>::Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00220          T* vector, int numLocalEqns,
00221          bool isSolutionVector,
00222                            bool deleteVector)
00223   : Vector_core(vecSpace, numLocalEqns),
00224     vector_(vector),
00225     isSolution_(isSolutionVector),
00226     deleteVector_(deleteVector),
00227     localProc_(0),
00228     numProcs_(1),
00229     dbgprefix_("VecImpl: ")
00230 {
00231   if (strcmp(snl_fei::FEVectorTraits<T>::typeName(), "unsupported")) {
00232     setFEVector(true);
00233   }
00234   else {
00235     setFEVector(false);
00236   }
00237 
00238   localProc_ = fei::localProc(vecSpace->getCommunicator());
00239   numProcs_ = fei::numProcs(vecSpace->getCommunicator());
00240 
00241   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00242     FEI_OSTREAM& os = *output_stream_;
00243     os << dbgprefix_<<" ctor, numLocalEqns="<<numLocalEqns
00244        <<", typeName: "<<typeName()<<FEI_ENDL;
00245   }
00246 }
00247 
00248 //----------------------------------------------------------------------------
00249 template<typename T>
00250 fei::Vector_Impl<T>::~Vector_Impl()
00251 {
00252   if (deleteVector_) delete vector_;
00253 }
00254 
00255 //----------------------------------------------------------------------------
00256 template<typename T>
00257 int fei::Vector_Impl<T>::putScalar(double scalar)
00258 {
00259   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00260     FEI_OSTREAM& os = *output_stream_;
00261     os << dbgprefix_<<"putScalar("<<scalar<<")"<<FEI_ENDL;
00262   }
00263 
00264   if (haveFEVector()) {
00265     if (scalar != 0.0) return(-1);
00266     CHK_ERR( snl_fei::FEVectorTraits<T>::reset(vector_) );
00267   }
00268   else {
00269     CHK_ERR( fei::VectorTraits<T>::setValues(vector_, firstLocalOffset(), scalar) );
00270   }
00271   for(unsigned p=0; p<remotelyOwned().size(); ++p) {
00272     fei::set_values(*(remotelyOwned()[p]), scalar);
00273   }
00274   return(0);
00275 }
00276 
00277 //----------------------------------------------------------------------------
00278 template<typename T>
00279 int fei::Vector_Impl<T>::update(double a,
00280              const fei::Vector* x,
00281              double b)
00282 {
00283   const fei::Vector_Impl<T>* sx = dynamic_cast<const fei::Vector_Impl<T>* >(x);
00284   if (sx != 0) {
00285     const T* tx = sx->getUnderlyingVector();
00286     return( fei::VectorTraits<T>::update(vector_, a, tx, b) );
00287   }
00288   else {
00289     return( -1 );
00290   }
00291 }
00292 
00293 //----------------------------------------------------------------------------
00294 template<typename T>
00295 int fei::Vector_Impl<T>::scatterToOverlap()
00296 {
00297   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00298     FEI_OSTREAM& os = *output_stream_;
00299     os << dbgprefix_<<"scatterToOverlap"<<FEI_ENDL;
00300   }
00301 
00302   return( Vector_core::scatterToOverlap() );
00303 }
00304 
00305 //----------------------------------------------------------------------------
00306 template<typename T>
00307 int fei::Vector_Impl<T>::gatherFromOverlap(bool accumulate)
00308 {
00309   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00310     FEI_OSTREAM& os = *output_stream_;
00311     os << dbgprefix_<<"gatherFromOverlap"<<FEI_ENDL;
00312   }
00313 
00314   return( Vector_core::gatherFromOverlap(accumulate) );
00315 }
00316 
00317 //----------------------------------------------------------------------------
00318 template<typename T>
00319 int fei::Vector_Impl<T>::sumIn(int numValues,
00320             const int* indices, const double* values,
00321             int vectorIndex)
00322 {
00323   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00324     FEI_OSTREAM& os = *output_stream_;
00325     os << dbgprefix_<<"sumIn(n="<<numValues<<")"<<FEI_ENDL;
00326   }
00327 
00328   return( giveToVector(numValues, indices, values, true, vectorIndex) );
00329 }
00330 
00331 //----------------------------------------------------------------------------
00332 template<typename T>
00333 int fei::Vector_Impl<T>::copyIn(int numValues,
00334              const int* indices, const double* values,
00335              int vectorIndex)
00336 {
00337   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00338     FEI_OSTREAM& os = *output_stream_;
00339     os << dbgprefix_<<"copyIn(n="<<numValues<<")"<<FEI_ENDL;
00340   }
00341 
00342   return( giveToVector(numValues, indices, values, false, vectorIndex) );
00343 }
00344 
00345 //----------------------------------------------------------------------------
00346 template<typename T>
00347 int fei::Vector_Impl<T>::giveToUnderlyingVector(int numValues,
00348                  const int* indices,
00349                  const double* values,
00350                  bool sumInto,
00351                  int vectorIndex)
00352 {
00353   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00354     FEI_OSTREAM& os = *output_stream_;
00355     os << dbgprefix_<<"giveToUnderlying(";
00356     for(int i=0; i<numValues; ++i) {
00357       os << "{"<<indices[i]<<","<<values[i]<<"} ";
00358     }
00359     os<<")"<<FEI_ENDL;
00360   }
00361 
00362   int err = fei::VectorTraits<T>::putValuesIn(vector_, firstLocalOffset(),
00363                numValues, indices, values,
00364                sumInto, isSolution_, vectorIndex);
00365   if (err < 0) {
00366     return(err);
00367   }
00368   return(0);
00369 }
00370 
00371 //----------------------------------------------------------------------------
00372 template<typename T>
00373 int fei::Vector_Impl<T>::copyOutOfUnderlyingVector(int numValues,
00374               const int* indices,
00375               double* values,
00376               int vectorIndex) const
00377 {
00378   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00379     FEI_OSTREAM& os = *output_stream_;
00380     os << dbgprefix_<<"copyOutOfUnderlying(n="<<numValues<<")"<<FEI_ENDL;
00381   }
00382 
00383   return( fei::VectorTraits<T>::copyOut(vector_, firstLocalOffset(),
00384                numValues, indices, values,
00385                isSolution_, vectorIndex) );
00386 }
00387 
00388 //----------------------------------------------------------------------------
00389 template<typename T>
00390 int fei::Vector_Impl<T>::sumInFieldData(int fieldID,
00391                int idType,
00392                int numIDs,
00393                const int* IDs,
00394                const double* data,
00395                int vectorIndex)
00396 {
00397   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00398     FEI_OSTREAM& os = *output_stream_;
00399     os << dbgprefix_<<"sumInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00400   }
00401 
00402   return( assembleFieldData(fieldID, idType, numIDs, IDs, data, true, vectorIndex));
00403 }
00404 
00405 //----------------------------------------------------------------------------
00406 template<typename T>
00407 int fei::Vector_Impl<T>::copyInFieldData(int fieldID,
00408           int idType,
00409           int numIDs,
00410           const int* IDs,
00411           const double* data,
00412           int vectorIndex)
00413 {
00414   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00415     FEI_OSTREAM& os = *output_stream_;
00416     os << dbgprefix_<<"copyInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00417   }
00418 
00419   return(assembleFieldData(fieldID, idType, numIDs, IDs, data, false, vectorIndex));
00420 }
00421 
00422 //----------------------------------------------------------------------------
00423 template<typename T>
00424 int fei::Vector_Impl<T>::copyOut_FE(int nodeNumber, int dofOffset, double& value)
00425 {
00426   return( snl_fei::FEVectorTraits<T>::copyOut(vector_, nodeNumber, dofOffset, value) );
00427 }
00428 
00429 //----------------------------------------------------------------------------
00430 template<typename T>
00431 int fei::Vector_Impl<T>::copyOutFieldData(int fieldID,
00432            int idType,
00433            int numIDs,
00434            const int* IDs,
00435            double* data,
00436            int vectorIndex)
00437 {
00438   return( Vector_core::copyOutFieldData(fieldID, idType, numIDs, IDs, data,
00439            vectorIndex));
00440 }
00441 
00442 //----------------------------------------------------------------------------
00443 template<typename T>
00444 int fei::Vector_Impl<T>::writeToFile(const char* filename,
00445             bool matrixMarketFormat)
00446 {
00447   return( Vector_core::writeToFile(filename, matrixMarketFormat) );
00448 }
00449 
00450 //----------------------------------------------------------------------------
00451 template<typename T>
00452 int fei::Vector_Impl<T>::writeToStream(FEI_OSTREAM& ostrm,
00453               bool matrixMarketFormat)
00454 {
00455   return( Vector_core::writeToStream(ostrm, matrixMarketFormat) );
00456 }
00457 
00458 //----------------------------------------------------------------------------
00459 template<typename T>
00460 int fei::Vector_Impl<T>::copyOut(int numValues,
00461         const int* indices,
00462         double* values,
00463         int vectorIndex) const
00464 {
00465   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00466     FEI_OSTREAM& os = *output_stream_;
00467     os << dbgprefix_<<"copyOut(n="<<numValues<<")"<<FEI_ENDL;
00468   }
00469 
00470   return( Vector_core::copyOut(numValues, indices, values, vectorIndex) );
00471 }
00472 
00473 //----------------------------------------------------------------------------
00474 template<typename T>
00475 int fei::Vector_Impl<T>::sumIntoFEVector(int blockID,
00476           int connOffset,
00477           int numNodes,
00478           const int* nodeNumbers,
00479           const int* numIndicesPerNode,
00480           const double* values)
00481 {
00482   return( snl_fei::FEVectorTraits<T>::sumInElemVector(vector_, blockID, connOffset,
00483                 numNodes, nodeNumbers,
00484                 numIndicesPerNode, values) );
00485 }
00486 
00487 #undef fei_file
00488 #define fei_file "unknown_file"
00489 
00490 #endif // _fei_Vector_Impl_hpp_
00491 

Generated on Tue Jul 13 09:27:46 2010 for FEI by  doxygen 1.4.7