FEI Version of the Day
fei_Vector_Impl.hpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 
00044 #ifndef _fei_Vector_Impl_hpp_
00045 #define _fei_Vector_Impl_hpp_
00046 
00047 #include <fei_macros.hpp>
00048 #include <fei_VectorTraits.hpp>
00049 
00050 #include <fei_VectorTraits_FillableVec.hpp>
00051 #include <fei_VectorTraits_LinSysCore.hpp>
00052 #include <fei_VectorTraits_LinProbMgr.hpp>
00053 #include <fei_VectorTraits_FEData.hpp>
00054 #include <snl_fei_FEVectorTraits.hpp>
00055 #include <snl_fei_FEVectorTraits_FED.hpp>
00056 #include <fei_VectorSpace.hpp>
00057 #include <fei_Reducer.hpp>
00058 #include <fei_Logger.hpp>
00059 #include <fei_Vector.hpp>
00060 #include <fei_Vector_core.hpp>
00061 #include <fei_iosfwd.hpp>
00062 
00063 #undef fei_file
00064 #define fei_file "fei_Vector_Impl.hpp"
00065 
00066 #include <fei_ErrMacros.hpp>
00067 
00068 namespace fei {
00069 
00090   template<typename T>
00091   class Vector_Impl : public fei::Vector, public fei::Vector_core {
00092   public:
00093 
00095     Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00096      T* vector, int numLocalEqns,
00097      bool isSolutionVector=false,
00098            bool deleteVector=false);
00099 
00101     virtual ~Vector_Impl();
00102 
00106     const char* typeName() const { return(fei::VectorTraits<T>::typeName()); }
00107 
00110     int update(double a,
00111          const fei::Vector* x,
00112          double b);
00113 
00120     int scatterToOverlap();
00121 
00125     int gatherFromOverlap(bool accumulate = true);
00126 
00128     int putScalar(double scalar);
00129 
00133     int sumIn(int numValues, const int* indices, const double* values,
00134         int vectorIndex=0);
00135 
00139     int copyIn(int numValues, const int* indices, const double* values,
00140          int vectorIndex=0);
00141 
00144     fei::SharedPtr<fei::VectorSpace> getVectorSpace() const
00145       { return(get_vector_space()); }
00146 
00149     void setVectorSpace(fei::SharedPtr<fei::VectorSpace> vecSpace)
00150     {
00151       set_vector_space( vecSpace );
00152     }
00153 
00160     int sumInFieldData(int fieldID,
00161            int idType,
00162            int numIDs,
00163            const int* IDs,
00164            const double* data,
00165            int vectorIndex=0);
00166 
00173     int copyInFieldData(int fieldID,
00174       int idType,
00175       int numIDs,
00176       const int* IDs,
00177       const double* data,
00178       int vectorIndex=0);
00179 
00186     int copyOutFieldData(int fieldID,
00187        int idType,
00188        int numIDs,
00189        const int* IDs,
00190        double* data,
00191        int vectorIndex=0);
00192 
00193     int writeToFile(const char* filename,
00194         bool matrixMarketFormat=true);
00195 
00196     int writeToStream(FEI_OSTREAM& ostrm,
00197           bool matrixMarketFormat=true);
00198 
00202     void setUnderlyingVector(T* vec)
00203       {
00204   vector_ = vec;
00205       }
00206 
00210     T* getUnderlyingVector() { return( vector_ ); }
00211     const T* getUnderlyingVector() const { return( vector_ ); }
00212 
00213     int copyOut(int numValues,
00214     const int* indices,
00215     double* values,
00216     int vectorIndex=0) const;
00217 
00220     int copyOut_FE(int nodeNumber, int dofOffset, double& value);
00221 
00222   private:
00223     int giveToUnderlyingVector(int numValues,
00224              const int* indices,
00225              const double* values,
00226              bool sumInto=true,
00227              int vectorIndex=0);
00228 
00229     int copyOutOfUnderlyingVector(int numValues,
00230           const int* indices,
00231           double* values,
00232           int vectorIndex=0) const;
00233 
00234     int sumIntoFEVector(int blockID,
00235       int connOffset,
00236       int numNodes,
00237       const int* nodeNumbers,
00238       const int* numIndicesPerNode,
00239       const int* dof_ids,
00240       const double* values);
00241 
00242     T* vector_;
00243     bool isSolution_;
00244     bool deleteVector_;
00245 
00246     int localProc_;
00247     int numProcs_;
00248     std::string dbgprefix_;
00249   };//class Vector_Impl
00250 
00251 } //namespace fei
00252 
00253 //----------------------------------------------------------------------------
00254 template<typename T>
00255 fei::Vector_Impl<T>::Vector_Impl(fei::SharedPtr<fei::VectorSpace> vecSpace,
00256          T* vector, int numLocalEqns,
00257          bool isSolutionVector,
00258                            bool deleteVector)
00259   : Vector_core(vecSpace, numLocalEqns),
00260     vector_(vector),
00261     isSolution_(isSolutionVector),
00262     deleteVector_(deleteVector),
00263     localProc_(0),
00264     numProcs_(1),
00265     dbgprefix_("VecImpl: ")
00266 {
00267   if (strcmp(snl_fei::FEVectorTraits<T>::typeName(), "unsupported")) {
00268     setFEVector(true);
00269   }
00270   else {
00271     setFEVector(false);
00272   }
00273 
00274   localProc_ = fei::localProc(vecSpace->getCommunicator());
00275   numProcs_ = fei::numProcs(vecSpace->getCommunicator());
00276 
00277   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00278     FEI_OSTREAM& os = *output_stream_;
00279     os << dbgprefix_<<" ctor, numLocalEqns="<<numLocalEqns
00280        <<", typeName: "<<typeName()<<FEI_ENDL;
00281   }
00282 }
00283 
00284 //----------------------------------------------------------------------------
00285 template<typename T>
00286 fei::Vector_Impl<T>::~Vector_Impl()
00287 {
00288   if (deleteVector_) delete vector_;
00289 }
00290 
00291 //----------------------------------------------------------------------------
00292 template<typename T>
00293 int fei::Vector_Impl<T>::putScalar(double scalar)
00294 {
00295   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00296     FEI_OSTREAM& os = *output_stream_;
00297     os << dbgprefix_<<"putScalar("<<scalar<<")"<<FEI_ENDL;
00298   }
00299 
00300   if (haveFEVector()) {
00301     if (scalar != 0.0) return(-1);
00302     CHK_ERR( snl_fei::FEVectorTraits<T>::reset(vector_) );
00303   }
00304   else {
00305     CHK_ERR( fei::VectorTraits<T>::setValues(vector_, firstLocalOffset(), scalar) );
00306   }
00307   for(unsigned p=0; p<remotelyOwned().size(); ++p) {
00308     fei::set_values(*(remotelyOwned()[p]), scalar);
00309   }
00310   return(0);
00311 }
00312 
00313 //----------------------------------------------------------------------------
00314 template<typename T>
00315 int fei::Vector_Impl<T>::update(double a,
00316              const fei::Vector* x,
00317              double b)
00318 {
00319   const fei::Vector_Impl<T>* sx = dynamic_cast<const fei::Vector_Impl<T>* >(x);
00320   if (sx != 0) {
00321     const T* tx = sx->getUnderlyingVector();
00322     return( fei::VectorTraits<T>::update(vector_, a, tx, b) );
00323   }
00324   else {
00325     return( -1 );
00326   }
00327 }
00328 
00329 //----------------------------------------------------------------------------
00330 template<typename T>
00331 int fei::Vector_Impl<T>::scatterToOverlap()
00332 {
00333   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00334     FEI_OSTREAM& os = *output_stream_;
00335     os << dbgprefix_<<"scatterToOverlap"<<FEI_ENDL;
00336   }
00337 
00338   return( Vector_core::scatterToOverlap() );
00339 }
00340 
00341 //----------------------------------------------------------------------------
00342 template<typename T>
00343 int fei::Vector_Impl<T>::gatherFromOverlap(bool accumulate)
00344 {
00345   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00346     FEI_OSTREAM& os = *output_stream_;
00347     os << dbgprefix_<<"gatherFromOverlap"<<FEI_ENDL;
00348   }
00349 
00350   return( Vector_core::gatherFromOverlap(accumulate) );
00351 }
00352 
00353 //----------------------------------------------------------------------------
00354 template<typename T>
00355 int fei::Vector_Impl<T>::sumIn(int numValues,
00356             const int* indices, const double* values,
00357             int vectorIndex)
00358 {
00359   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00360     FEI_OSTREAM& os = *output_stream_;
00361     os << dbgprefix_<<"sumIn(n="<<numValues<<")"<<FEI_ENDL;
00362   }
00363 
00364   return( giveToVector(numValues, indices, values, true, vectorIndex) );
00365 }
00366 
00367 //----------------------------------------------------------------------------
00368 template<typename T>
00369 int fei::Vector_Impl<T>::copyIn(int numValues,
00370              const int* indices, const double* values,
00371              int vectorIndex)
00372 {
00373   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00374     FEI_OSTREAM& os = *output_stream_;
00375     os << dbgprefix_<<"copyIn(n="<<numValues<<")"<<FEI_ENDL;
00376   }
00377 
00378   return( giveToVector(numValues, indices, values, false, vectorIndex) );
00379 }
00380 
00381 //----------------------------------------------------------------------------
00382 template<typename T>
00383 int fei::Vector_Impl<T>::giveToUnderlyingVector(int numValues,
00384                  const int* indices,
00385                  const double* values,
00386                  bool sumInto,
00387                  int vectorIndex)
00388 {
00389   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00390     FEI_OSTREAM& os = *output_stream_;
00391     os << dbgprefix_<<"giveToUnderlying(";
00392     for(int i=0; i<numValues; ++i) {
00393       os << "{"<<indices[i]<<","<<values[i]<<"} ";
00394     }
00395     os<<")"<<FEI_ENDL;
00396   }
00397 
00398   int err = fei::VectorTraits<T>::putValuesIn(vector_, firstLocalOffset(),
00399                numValues, indices, values,
00400                sumInto, isSolution_, vectorIndex);
00401   if (err < 0) {
00402     return(err);
00403   }
00404   return(0);
00405 }
00406 
00407 //----------------------------------------------------------------------------
00408 template<typename T>
00409 int fei::Vector_Impl<T>::copyOutOfUnderlyingVector(int numValues,
00410               const int* indices,
00411               double* values,
00412               int vectorIndex) const
00413 {
00414   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00415     FEI_OSTREAM& os = *output_stream_;
00416     os << dbgprefix_<<"copyOutOfUnderlying(n="<<numValues<<")"<<FEI_ENDL;
00417   }
00418 
00419   return( fei::VectorTraits<T>::copyOut(vector_, firstLocalOffset(),
00420                numValues, indices, values,
00421                isSolution_, vectorIndex) );
00422 }
00423 
00424 //----------------------------------------------------------------------------
00425 template<typename T>
00426 int fei::Vector_Impl<T>::sumInFieldData(int fieldID,
00427                int idType,
00428                int numIDs,
00429                const int* IDs,
00430                const double* data,
00431                int vectorIndex)
00432 {
00433   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00434     FEI_OSTREAM& os = *output_stream_;
00435     os << dbgprefix_<<"sumInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00436   }
00437 
00438   return( assembleFieldData(fieldID, idType, numIDs, IDs, data, true, vectorIndex));
00439 }
00440 
00441 //----------------------------------------------------------------------------
00442 template<typename T>
00443 int fei::Vector_Impl<T>::copyInFieldData(int fieldID,
00444           int idType,
00445           int numIDs,
00446           const int* IDs,
00447           const double* data,
00448           int vectorIndex)
00449 {
00450   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00451     FEI_OSTREAM& os = *output_stream_;
00452     os << dbgprefix_<<"copyInFieldData(n="<<numIDs<<")"<<FEI_ENDL;
00453   }
00454 
00455   return(assembleFieldData(fieldID, idType, numIDs, IDs, data, false, vectorIndex));
00456 }
00457 
00458 //----------------------------------------------------------------------------
00459 template<typename T>
00460 int fei::Vector_Impl<T>::copyOut_FE(int nodeNumber, int dofOffset, double& value)
00461 {
00462   return( snl_fei::FEVectorTraits<T>::copyOut(vector_, nodeNumber, dofOffset, value) );
00463 }
00464 
00465 //----------------------------------------------------------------------------
00466 template<typename T>
00467 int fei::Vector_Impl<T>::copyOutFieldData(int fieldID,
00468            int idType,
00469            int numIDs,
00470            const int* IDs,
00471            double* data,
00472            int vectorIndex)
00473 {
00474   return( Vector_core::copyOutFieldData(fieldID, idType, numIDs, IDs, data,
00475            vectorIndex));
00476 }
00477 
00478 //----------------------------------------------------------------------------
00479 template<typename T>
00480 int fei::Vector_Impl<T>::writeToFile(const char* filename,
00481             bool matrixMarketFormat)
00482 {
00483   return( Vector_core::writeToFile(filename, matrixMarketFormat) );
00484 }
00485 
00486 //----------------------------------------------------------------------------
00487 template<typename T>
00488 int fei::Vector_Impl<T>::writeToStream(FEI_OSTREAM& ostrm,
00489               bool matrixMarketFormat)
00490 {
00491   return( Vector_core::writeToStream(ostrm, matrixMarketFormat) );
00492 }
00493 
00494 //----------------------------------------------------------------------------
00495 template<typename T>
00496 int fei::Vector_Impl<T>::copyOut(int numValues,
00497         const int* indices,
00498         double* values,
00499         int vectorIndex) const
00500 {
00501   if (output_level_ >= fei::BRIEF_LOGS && output_stream_ != NULL) {
00502     FEI_OSTREAM& os = *output_stream_;
00503     os << dbgprefix_<<"copyOut(n="<<numValues<<")"<<FEI_ENDL;
00504   }
00505 
00506   return( Vector_core::copyOut(numValues, indices, values, vectorIndex) );
00507 }
00508 
00509 //----------------------------------------------------------------------------
00510 template<typename T>
00511 int fei::Vector_Impl<T>::sumIntoFEVector(int blockID,
00512           int connOffset,
00513           int numNodes,
00514           const int* nodeNumbers,
00515           const int* numIndicesPerNode,
00516           const int* dof_ids,
00517           const double* values)
00518 {
00519   return( snl_fei::FEVectorTraits<T>::sumInElemVector(vector_, blockID, connOffset,
00520                 numNodes, nodeNumbers,
00521                 numIndicesPerNode, dof_ids, values) );
00522 }
00523 
00524 #undef fei_file
00525 #define fei_file "unknown_file"
00526 
00527 #endif // _fei_Vector_Impl_hpp_
00528 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends