fei_Vector_Local.cpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2007 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 #include "fei_Vector_Local.hpp"
00010 #include "fei_sstream.hpp"
00011 #include "fei_fstream.hpp"
00012 #include <fei_ErrMacros.hpp>
00013 
00014 #include <algorithm>
00015 
00016 #undef fei_file
00017 #define fei_file "fei_Vector_Local.cpp"
00018 
00019 namespace fei {
00020 
00021 Vector_Local::Vector_Local(fei::SharedPtr<fei::VectorSpace> vecSpace)
00022  : vecSpace_(vecSpace),
00023    coefs_(),
00024    global_to_local_(),
00025    work_indices_()
00026 {
00027   int numCoefs = vecSpace_->getNumIndices_SharedAndOwned();
00028   coefs_.resize(numCoefs);
00029 
00030   std::vector<int> indices;
00031   vecSpace_->getIndices_SharedAndOwned(indices);
00032 
00033   std::sort(indices.begin(), indices.end());
00034 
00035   for(int i=0; i<numCoefs; ++i) {
00036     global_to_local_.insert(std::make_pair(indices[i], i));
00037   }
00038 }
00039 
00040 Vector_Local::~Vector_Local()
00041 {
00042 }
00043 
00044 int
00045 Vector_Local::update(double a,
00046                const fei::Vector* x,
00047                double b)
00048 {
00049   FEI_CERR << "Vector_Local::update NOT IMPLEMENTED."<<FEI_ENDL;
00050   return(-1);
00051 }
00052 
00053 int
00054 Vector_Local::scatterToOverlap()
00055 { return(0); }
00056 
00057 int
00058 Vector_Local::gatherFromOverlap(bool accumulate)
00059 { (void)accumulate; return(0); }
00060 
00061 int
00062 Vector_Local::putScalar(double scalar)
00063 {
00064   for(size_t i=0; i<coefs_.size(); ++i) coefs_[i] = scalar;
00065   return(0);
00066 }
00067 
00068 int
00069 Vector_Local::giveToVector(int numValues, const int* indices,
00070                            const double* values,
00071                            bool sumInto, int vectorIndex)
00072 {
00073   if (vectorIndex != 0) {
00074     FEI_CERR << "fei::Vector_Local ERROR, vectorIndex!=0. Report to Alan Williams."<<FEI_ENDL;
00075     return(-1);
00076   }
00077 
00078   for(int i=0; i<numValues; ++i) {
00079     std::map<int,int>::iterator
00080      iter = global_to_local_.find(indices[i]);
00081     if (iter == global_to_local_.end()) {
00082       FEI_CERR << "fei::Vector_Local ERROR, eqn "<<indices[i]<<" not found "
00083          << "locally."<<FEI_ENDL;
00084       return(-1);
00085     }
00086 
00087     if (sumInto) {
00088       coefs_[iter->second] += values[i];
00089     }
00090     else {
00091       coefs_[iter->second] = values[i];
00092     }
00093   }
00094 
00095   return(0);
00096 }
00097 
00098 int
00099 Vector_Local::sumIn(int numValues, const int* indices, const double* values,
00100               int vectorIndex)
00101 {
00102   return( giveToVector(numValues, indices, values, true, vectorIndex) );
00103 }
00104 
00105 int
00106 Vector_Local::copyIn(int numValues, const int* indices, const double* values,
00107                int vectorIndex)
00108 {
00109   return( giveToVector(numValues, indices, values, false, vectorIndex) );
00110 }
00111 
00112 fei::SharedPtr<fei::VectorSpace>
00113 Vector_Local::getVectorSpace()
00114 { return( vecSpace_ ); }
00115 
00116 void
00117 Vector_Local::setVectorSpace(fei::SharedPtr<fei::VectorSpace> vecSpace)
00118 { vecSpace_ = vecSpace; }
00119 
00120 int
00121 Vector_Local::assembleFieldData(int fieldID,
00122                        int idType,
00123                        int numIDs,
00124                        const int* IDs,
00125                        const double* data,
00126                        bool sumInto,
00127                        int vectorIndex)
00128 {
00129   int fieldSize = vecSpace_->getFieldSize(fieldID);
00130 
00131   work_indices_.resize(numIDs*fieldSize);
00132   int* indicesPtr = &work_indices_[0];
00133 
00134   CHK_ERR( vecSpace_->getGlobalIndices(numIDs, IDs, idType, fieldID,
00135                                         indicesPtr) );
00136 
00137   CHK_ERR( giveToVector(numIDs*fieldSize, indicesPtr, data, sumInto, vectorIndex) );
00138 
00139   return(0);
00140 }
00141 
00142 int
00143 Vector_Local::sumInFieldData(int fieldID,
00144                        int idType,
00145                        int numIDs,
00146                        const int* IDs,
00147                        const double* data,
00148                        int vectorIndex)
00149 {
00150   return(assembleFieldData(fieldID, idType, numIDs, IDs,
00151                            data, true, vectorIndex));
00152 }
00153 
00154 int
00155 Vector_Local::copyInFieldData(int fieldID,
00156                         int idType,
00157                         int numIDs,
00158                         const int* IDs,
00159                         const double* data,
00160                         int vectorIndex)
00161 {
00162   return(assembleFieldData(fieldID, idType, numIDs, IDs,
00163                            data, false, vectorIndex));
00164 }
00165 
00166 int
00167 Vector_Local::copyOutFieldData(int fieldID,
00168                          int idType,
00169                          int numIDs,
00170                          const int* IDs,
00171                          double* data,
00172                          int vectorIndex)
00173 {
00174   int fieldSize = vecSpace_->getFieldSize(fieldID);
00175 
00176   work_indices_.resize(numIDs*fieldSize);
00177   int* indicesPtr = &work_indices_[0];
00178 
00179   CHK_ERR( vecSpace_->getGlobalIndices(numIDs, IDs, idType, fieldID,
00180                                         indicesPtr) );
00181 
00182   for(int i=0; i<(int)work_indices_.size(); ++i) {
00183     std::map<int,int>::iterator
00184       iter = global_to_local_.find(work_indices_[i]);
00185     if (iter == global_to_local_.end()) {
00186       FEI_CERR << "fei::Vector_Local::copyOut ERROR, eqn "<<work_indices_[i]<<" not found "
00187          << "locally."<<FEI_ENDL;
00188       return(-1);
00189     }
00190 
00191     data[i] = coefs_[iter->second];
00192   }
00193 
00194   return(0);
00195 }
00196 
00197 int
00198 Vector_Local::copyOut(int numValues, const int* indices,
00199                       double* values, int vectorIndex) const
00200 {
00201   if (vectorIndex != 0) {
00202     FEI_CERR << "fei::Vector_Local ERROR, vectorIndex!=0. Report to Alan Williams."<<FEI_ENDL;
00203     return(-1);
00204   }
00205 
00206   for(int i=0; i<numValues; ++i) {
00207     std::map<int,int>::const_iterator
00208      iter = global_to_local_.find(indices[i]);
00209     if (iter == global_to_local_.end()) {
00210       FEI_CERR << "fei::Vector_Local::copyOut ERROR, eqn "<<indices[i]<<" not found "
00211          << "locally."<<FEI_ENDL;
00212       return(-1);
00213     }
00214 
00215     values[i] = coefs_[iter->second];
00216   }
00217 
00218   return(0);
00219 }
00220 
00221 std::vector<double>&
00222 Vector_Local::getCoefs()
00223 {
00224   return(coefs_);
00225 }
00226 
00227 int
00228 Vector_Local::writeToFile(const char* filename,
00229                     bool matrixMarketFormat)
00230 {
00231   int local_proc = fei::localProc(vecSpace_->getCommunicator());
00232   FEI_OSTRINGSTREAM osstr;
00233   osstr << filename << "." << local_proc;
00234   std::string fullname = osstr.str();
00235   FEI_OFSTREAM ofstr(fullname.c_str(), IOS_OUT);
00236 
00237   return( writeToStream(ofstr, matrixMarketFormat) );
00238 }
00239 
00240 int
00241 Vector_Local::writeToStream(FEI_OSTREAM& ostrm,
00242                       bool matrixMarketFormat)
00243 {
00244   static char mmbanner[] = "%%MatrixMarket matrix array real general";
00245 
00246   if (matrixMarketFormat) {
00247     ostrm << mmbanner << FEI_ENDL;
00248     ostrm << coefs_.size() << " 1" << FEI_ENDL;
00249   }
00250   else {
00251     ostrm << coefs_.size() << FEI_ENDL;
00252   }
00253 
00254   ostrm.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
00255   ostrm.precision(13);
00256 
00257   std::map<int,int>::iterator
00258     iter = global_to_local_.begin();
00259 
00260   for(unsigned i=0; i<coefs_.size(); ++i) {
00261     if (matrixMarketFormat) {
00262       ostrm << coefs_[i] << FEI_ENDL;
00263     }
00264     else {
00265       ostrm << iter->first << " " << coefs_[i] << FEI_ENDL;
00266       ++iter;
00267     }
00268   }
00269 
00270   return(0);
00271 }
00272 
00273 }//namespace fei
00274 

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