FEI Version of the Day
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::console_out() << "Vector_Local::update NOT IMPLEMENTED."<<FEI_ENDL;
00050   return(-1);
00051 }
00052 
00053 int
00054 Vector_Local::scatterToOverlap()
00055 { return(0); }
00056 
00057 void
00058 Vector_Local::setCommSizes()
00059 {
00060 }
00061 
00062 int
00063 Vector_Local::gatherFromOverlap(bool accumulate)
00064 { (void)accumulate; return(0); }
00065 
00066 int
00067 Vector_Local::putScalar(double scalar)
00068 {
00069   for(size_t i=0; i<coefs_.size(); ++i) coefs_[i] = scalar;
00070   return(0);
00071 }
00072 
00073 int
00074 Vector_Local::giveToVector(int numValues, const int* indices,
00075                            const double* values,
00076                            bool sumInto, int vectorIndex)
00077 {
00078   if (vectorIndex != 0) {
00079     fei::console_out() << "fei::Vector_Local ERROR, vectorIndex!=0. Report to Alan Williams."<<FEI_ENDL;
00080     return(-1);
00081   }
00082 
00083   for(int i=0; i<numValues; ++i) {
00084     std::map<int,int>::iterator
00085      iter = global_to_local_.find(indices[i]);
00086     if (iter == global_to_local_.end()) {
00087       fei::console_out() << "fei::Vector_Local ERROR, eqn "<<indices[i]<<" not found "
00088          << "locally."<<FEI_ENDL;
00089       return(-1);
00090     }
00091 
00092     if (sumInto) {
00093       coefs_[iter->second] += values[i];
00094     }
00095     else {
00096       coefs_[iter->second] = values[i];
00097     }
00098   }
00099 
00100   return(0);
00101 }
00102 
00103 int
00104 Vector_Local::sumIn(int numValues, const int* indices, const double* values,
00105               int vectorIndex)
00106 {
00107   return( giveToVector(numValues, indices, values, true, vectorIndex) );
00108 }
00109 
00110 int
00111 Vector_Local::copyIn(int numValues, const int* indices, const double* values,
00112                int vectorIndex)
00113 {
00114   return( giveToVector(numValues, indices, values, false, vectorIndex) );
00115 }
00116 
00117 fei::SharedPtr<fei::VectorSpace>
00118 Vector_Local::getVectorSpace() const
00119 { return( vecSpace_ ); }
00120 
00121 void
00122 Vector_Local::setVectorSpace(fei::SharedPtr<fei::VectorSpace> vecSpace)
00123 { vecSpace_ = vecSpace; }
00124 
00125 int
00126 Vector_Local::assembleFieldData(int fieldID,
00127                        int idType,
00128                        int numIDs,
00129                        const int* IDs,
00130                        const double* data,
00131                        bool sumInto,
00132                        int vectorIndex)
00133 {
00134   int fieldSize = vecSpace_->getFieldSize(fieldID);
00135 
00136   work_indices_.resize(numIDs*fieldSize);
00137   int* indicesPtr = &work_indices_[0];
00138 
00139   CHK_ERR( vecSpace_->getGlobalIndices(numIDs, IDs, idType, fieldID,
00140                                         indicesPtr) );
00141 
00142   CHK_ERR( giveToVector(numIDs*fieldSize, indicesPtr, data, sumInto, vectorIndex) );
00143 
00144   return(0);
00145 }
00146 
00147 int
00148 Vector_Local::assembleFieldDataLocalIDs(int fieldID,
00149                        int idType,
00150                        int numIDs,
00151                        const int* localIDs,
00152                        const double* data,
00153                        bool sumInto,
00154                        int vectorIndex)
00155 {
00156   int fieldSize = vecSpace_->getFieldSize(fieldID);
00157 
00158   work_indices_.resize(numIDs*fieldSize);
00159   int* indicesPtr = &work_indices_[0];
00160 
00161   CHK_ERR( vecSpace_->getGlobalIndicesLocalIDs(numIDs, localIDs, idType, fieldID,
00162                                         indicesPtr) );
00163 
00164   CHK_ERR( giveToVector(numIDs*fieldSize, indicesPtr, data, sumInto, vectorIndex) );
00165 
00166   return(0);
00167 }
00168 
00169 int
00170 Vector_Local::sumInFieldData(int fieldID,
00171                        int idType,
00172                        int numIDs,
00173                        const int* IDs,
00174                        const double* data,
00175                        int vectorIndex)
00176 {
00177   return(assembleFieldData(fieldID, idType, numIDs, IDs,
00178                            data, true, vectorIndex));
00179 }
00180 
00181 int
00182 Vector_Local::copyInFieldData(int fieldID,
00183                         int idType,
00184                         int numIDs,
00185                         const int* IDs,
00186                         const double* data,
00187                         int vectorIndex)
00188 {
00189   return(assembleFieldData(fieldID, idType, numIDs, IDs,
00190                            data, false, vectorIndex));
00191 }
00192 
00193 int
00194 Vector_Local::copyInFieldDataLocalIDs(int fieldID,
00195                         int idType,
00196                         int numIDs,
00197                         const int* localIDs,
00198                         const double* data,
00199                         int vectorIndex)
00200 {
00201   return(assembleFieldDataLocalIDs(fieldID, idType, numIDs, localIDs,
00202                            data, false, vectorIndex));
00203 }
00204 
00205 int
00206 Vector_Local::copyOutFieldData(int fieldID,
00207                          int idType,
00208                          int numIDs,
00209                          const int* IDs,
00210                          double* data,
00211                          int vectorIndex)
00212 {
00213   int fieldSize = vecSpace_->getFieldSize(fieldID);
00214 
00215   work_indices_.resize(numIDs*fieldSize);
00216   int* indicesPtr = &work_indices_[0];
00217 
00218   CHK_ERR( vecSpace_->getGlobalIndices(numIDs, IDs, idType, fieldID,
00219                                         indicesPtr) );
00220 
00221   for(int i=0; i<(int)work_indices_.size(); ++i) {
00222     std::map<int,int>::iterator
00223       iter = global_to_local_.find(work_indices_[i]);
00224     if (iter == global_to_local_.end()) {
00225       fei::console_out() << "fei::Vector_Local::copyOut ERROR, eqn "<<work_indices_[i]<<" not found "
00226          << "locally."<<FEI_ENDL;
00227       return(-1);
00228     }
00229 
00230     data[i] = coefs_[iter->second];
00231   }
00232 
00233   return(0);
00234 }
00235 
00236 int
00237 Vector_Local::copyOut(int numValues, const int* indices,
00238                       double* values, int vectorIndex) const
00239 {
00240   if (vectorIndex != 0) {
00241     fei::console_out() << "fei::Vector_Local ERROR, vectorIndex!=0. Report to Alan Williams."<<FEI_ENDL;
00242     return(-1);
00243   }
00244 
00245   for(int i=0; i<numValues; ++i) {
00246     std::map<int,int>::const_iterator
00247      iter = global_to_local_.find(indices[i]);
00248     if (iter == global_to_local_.end()) {
00249       fei::console_out() << "fei::Vector_Local::copyOut ERROR, eqn "<<indices[i]<<" not found "
00250          << "locally."<<FEI_ENDL;
00251       return(-1);
00252     }
00253 
00254     values[i] = coefs_[iter->second];
00255   }
00256 
00257   return(0);
00258 }
00259 
00260 std::vector<double>&
00261 Vector_Local::getCoefs()
00262 {
00263   return(coefs_);
00264 }
00265 
00266 int
00267 Vector_Local::writeToFile(const char* filename,
00268                     bool matrixMarketFormat)
00269 {
00270   int local_proc = fei::localProc(vecSpace_->getCommunicator());
00271   FEI_OSTRINGSTREAM osstr;
00272   osstr << filename << "." << local_proc;
00273   std::string fullname = osstr.str();
00274   FEI_OFSTREAM ofstr(fullname.c_str(), IOS_OUT);
00275 
00276   return( writeToStream(ofstr, matrixMarketFormat) );
00277 }
00278 
00279 int
00280 Vector_Local::writeToStream(FEI_OSTREAM& ostrm,
00281                       bool matrixMarketFormat)
00282 {
00283   static char mmbanner[] = "%%MatrixMarket matrix array real general";
00284 
00285   if (matrixMarketFormat) {
00286     ostrm << mmbanner << FEI_ENDL;
00287     ostrm << coefs_.size() << " 1" << FEI_ENDL;
00288   }
00289   else {
00290     ostrm << coefs_.size() << FEI_ENDL;
00291   }
00292 
00293   ostrm.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
00294   ostrm.precision(13);
00295 
00296   std::map<int,int>::iterator
00297     iter = global_to_local_.begin();
00298 
00299   for(unsigned i=0; i<coefs_.size(); ++i) {
00300     if (matrixMarketFormat) {
00301       ostrm << coefs_[i] << FEI_ENDL;
00302     }
00303     else {
00304       ostrm << iter->first << " " << coefs_[i] << FEI_ENDL;
00305       ++iter;
00306     }
00307   }
00308 
00309   return(0);
00310 }
00311 
00312 }//namespace fei
00313 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends