All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Xpetra_EpetraMapExtractor.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //             Xpetra: A linear algebra interface package
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact
00039 //                    Jeremie Gaidamour (jngaida@sandia.gov)
00040 //                    Jonathan Hu       (jhu@sandia.gov)
00041 //                    Ray Tuminaro      (rstumin@sandia.gov)
00042 //
00043 // ***********************************************************************
00044 //
00045 // @HEADER
00046 /*
00047  * Xpetra_EpetraMapExtractor.hpp
00048  *
00049  *  Created on: Aug 22, 2011
00050  *      Author: wiesner
00051  */
00052 
00053 // WARNING: This code is experimental. Backwards compatibility should not be expected.
00054 
00055 #ifndef XPETRA_EPETRAMAPEXTRACTOR_HPP_
00056 #define XPETRA_EPETRAMAPEXTRACTOR_HPP_
00057 
00058 #include <Xpetra_MapExtractor.hpp>
00059 
00060 namespace Xpetra
00061 {
00062   class EpetraMapExtractor : public Xpetra::MapExtractor<double,int,int>
00063   {
00064     typedef Xpetra::Map<int,int> MapClass;
00065     typedef Xpetra::Vector<double,int,int> VectorClass;
00066     typedef Xpetra::MultiVector<double,int,int> MultiVectorClass;
00067     typedef Xpetra::Import<int,int> ImportClass;
00068 
00069   public:
00071     EpetraMapExtractor(const Teuchos::RCP<const MapClass>& fullmap, const std::vector<Teuchos::RCP<const MapClass> >& maps)
00072     {
00073       XPETRA_RCP_DYNAMIC_CAST(const EpetraMap, fullmap, efullmap, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00074 
00075       fullmap_ = efullmap;
00076 
00077       unsigned int numMaps = maps.size();
00078       maps_.empty();
00079       importer_.resize(numMaps);
00080       for (unsigned i=0; i<numMaps; ++i)
00081       {
00082         if (maps[i]!=Teuchos::null)
00083         {
00084           XPETRA_RCP_DYNAMIC_CAST(const EpetraMap, maps[i], emapi, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00085           maps_.push_back(emapi);
00086           importer_[i] = Xpetra::ImportFactory<int,int>::Build(efullmap,emapi);
00087         }
00088       }
00089       TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency()==false,std::logic_error,"logic error. full map and sub maps are inconsistently distributed over the processors.");
00090     }
00091 
00093     EpetraMapExtractor(const Teuchos::RCP<const Xpetra::EpetraMap>& fullmap, const std::vector<Teuchos::RCP<const Xpetra::EpetraMap> >& maps)
00094     {
00095       XPETRA_RCP_DYNAMIC_CAST(const EpetraMap, fullmap, efullmap, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00096 
00097       fullmap_ = efullmap;
00098       maps_    = maps;
00099 
00100       importer_.resize(maps_.size());
00101       for (unsigned i=0; i< importer_.size(); ++i)
00102       {
00103         if (maps_[i]!=Teuchos::null)
00104         {
00105           importer_[i] = Xpetra::ImportFactory<int,int>::Build(fullmap,maps_[i]);
00106           std::cout << *importer_[i] << std::endl;
00107         }
00108       }
00109       TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency()==false,std::logic_error,"logic error. full map and sub maps are inconsistently distributed over the processors.");
00110     }
00111 
00113     virtual inline ~EpetraMapExtractor() {};
00114 
00115     void InsertVector(const Xpetra::EpetraVector& partial, size_t block, Xpetra::EpetraVector& full) const
00116     {
00117       if (maps_[block] == Teuchos::null)
00118         std::cout << "null map at block" << block << std::endl;
00119 
00120       full.doExport(partial,*importer_[block],Xpetra::INSERT);
00121     }
00122 
00123     void InsertVector(Teuchos::RCP<const VectorClass>& partial, size_t block, Teuchos::RCP<VectorClass>& full) const
00124     {
00125       XPETRA_RCP_DYNAMIC_CAST(EpetraVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00126       XPETRA_RCP_DYNAMIC_CAST(const EpetraVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00127 
00128       InsertVector(*epartial,block,*efull);
00129     }
00130 
00131     void InsertVector(Teuchos::RCP<VectorClass>& partial,size_t block, Teuchos::RCP<VectorClass>& full) const
00132     {
00133       XPETRA_RCP_DYNAMIC_CAST(EpetraVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00134       XPETRA_RCP_DYNAMIC_CAST(EpetraVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00135 
00136       InsertVector(*epartial,block,*efull);
00137     }
00138 
00139     void InsertVector(const Xpetra::EpetraMultiVector& partial,size_t block, Xpetra::EpetraMultiVector& full) const
00140     {
00141       if (maps_[block] == Teuchos::null)
00142         std::cout << "null map at block" << block << std::endl;
00143 
00144       full.doExport(partial,*importer_[block],Xpetra::INSERT);
00145     }
00146 
00147     void InsertVector(Teuchos::RCP<const MultiVectorClass>& partial,size_t block, Teuchos::RCP<MultiVectorClass>& full) const
00148     {
00149       XPETRA_RCP_DYNAMIC_CAST(EpetraMultiVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00150       XPETRA_RCP_DYNAMIC_CAST(const EpetraMultiVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00151 
00152       InsertVector(*epartial,block,*efull);
00153     }
00154 
00155     void InsertVector(Teuchos::RCP<MultiVectorClass>& partial,size_t block, Teuchos::RCP<MultiVectorClass>& full) const
00156     {
00157       XPETRA_RCP_DYNAMIC_CAST(EpetraMultiVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00158       XPETRA_RCP_DYNAMIC_CAST(EpetraMultiVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00159 
00160       InsertVector(*epartial,block,*efull);
00161     }
00162 
00163     void ExtractVector(const Xpetra::EpetraVector& full, size_t block, Xpetra::EpetraVector& partial) const
00164     {
00165       if (maps_[block] == Teuchos::null)
00166         std::cout << "null map at block " << block << std::endl;
00167       partial.doImport(full, *importer_[block], Xpetra::INSERT);
00168     }
00169 
00170     void ExtractVector(Teuchos::RCP<const VectorClass>& full, size_t block, Teuchos::RCP<VectorClass>& partial) const
00171     {
00172       XPETRA_RCP_DYNAMIC_CAST(const EpetraVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00173       XPETRA_RCP_DYNAMIC_CAST(      EpetraVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00174 
00175       ExtractVector(*efull,block,*epartial);
00176     }
00177 
00178     void ExtractVector(Teuchos::RCP<VectorClass>& full, size_t block, Teuchos::RCP<VectorClass>& partial) const
00179     {
00180       XPETRA_RCP_DYNAMIC_CAST(EpetraVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00181       XPETRA_RCP_DYNAMIC_CAST(EpetraVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00182 
00183       ExtractVector(*efull,block,*epartial);
00184     }
00185 
00186     virtual Teuchos::RCP<VectorClass> ExtractVector(Teuchos::RCP<const VectorClass>& full, size_t block) const
00187     {
00188       XPETRA_RCP_DYNAMIC_CAST(const EpetraVector, full, efull, "Xpetra::EpetraMapextractor constructors only accepts Xpetra::EpetraMap as input arguments.");
00189       TEUCHOS_TEST_FOR_EXCEPTION( maps_[block] == Teuchos::null, Xpetra::Exceptions::RuntimeError,
00190             "ExtractVector: maps_[" << block << "]=Teuchos::null" );
00191       const Teuchos::RCP<EpetraVector> ret = Teuchos::rcp(new Xpetra::EpetraVector(getMap(block),true));
00192       ExtractVector(*efull,block,*ret);
00193       return ret;
00194     }
00195 
00196     virtual Teuchos::RCP<VectorClass> ExtractVector(Teuchos::RCP<VectorClass>& full, size_t block) const
00197     {
00198       XPETRA_RCP_DYNAMIC_CAST(EpetraVector, full, efull, "Xpetra::EpetraMapextractor constructors only accepts Xpetra::EpetraMap as input arguments.");
00199       TEUCHOS_TEST_FOR_EXCEPTION( maps_[block] == Teuchos::null, Xpetra::Exceptions::RuntimeError,
00200             "ExtractVector: maps_[" << block << "]=Teuchos::null" );
00201       const Teuchos::RCP<EpetraVector> ret = Teuchos::rcp(new Xpetra::EpetraVector(getMap(block),true));
00202       ExtractVector(*efull,block,*ret);
00203       return ret;
00204     }
00205 
00206     void ExtractVector(const Xpetra::EpetraMultiVector& full, size_t block, Xpetra::EpetraMultiVector& partial) const
00207     {
00208       if (maps_[block] == Teuchos::null)
00209         std::cout << "null map at block " << block << std::endl;
00210       partial.doImport(full, *importer_[block], Xpetra::INSERT);
00211     }
00212 
00213     virtual void ExtractVector(Teuchos::RCP<const MultiVectorClass>& full, size_t block, Teuchos::RCP<MultiVectorClass>& partial) const
00214     {
00215       XPETRA_RCP_DYNAMIC_CAST(const EpetraMultiVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00216       XPETRA_RCP_DYNAMIC_CAST(      EpetraMultiVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00217 
00218       ExtractVector(*efull,block,*epartial);
00219     }
00220 
00221     virtual void ExtractVector(Teuchos::RCP<      MultiVectorClass>& full, size_t block, Teuchos::RCP<MultiVectorClass>& partial) const
00222     {
00223       XPETRA_RCP_DYNAMIC_CAST(      EpetraMultiVector, full, efull, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00224       XPETRA_RCP_DYNAMIC_CAST(      EpetraMultiVector, partial, epartial, "Xpetra::EpetraMapextractor constructors only accept Xpetra::EpetraMap as input arguments.");
00225 
00226       ExtractVector(*efull,block,*epartial);
00227     }
00228 
00229     virtual Teuchos::RCP<MultiVectorClass> ExtractVector(Teuchos::RCP<const MultiVectorClass>& full, size_t block) const
00230     {
00231       XPETRA_RCP_DYNAMIC_CAST(const EpetraMultiVector, full, efull, "Xpetra::EpetraMapextractor constructors only accepts Xpetra::EpetraMap as input arguments.");
00232       TEUCHOS_TEST_FOR_EXCEPTION( maps_[block] == Teuchos::null, Xpetra::Exceptions::RuntimeError,
00233             "ExtractVector: maps_[" << block << "]=Teuchos::null" );
00234       const Teuchos::RCP<EpetraMultiVector> ret = Teuchos::rcp(new Xpetra::EpetraMultiVector(getMap(block),full->getNumVectors(),true));
00235       ExtractVector(*efull,block,*ret);
00236       return ret;
00237     }
00238 
00239     virtual Teuchos::RCP<MultiVectorClass> ExtractVector(Teuchos::RCP<MultiVectorClass>& full, size_t block) const
00240     {
00241       XPETRA_RCP_DYNAMIC_CAST(EpetraMultiVector, full, efull, "Xpetra::EpetraMapextractor constructors only accepts Xpetra::EpetraMap as input arguments.");
00242       TEUCHOS_TEST_FOR_EXCEPTION( maps_[block] == Teuchos::null, Xpetra::Exceptions::RuntimeError,
00243             "ExtractVector: maps_[" << block << "]=Teuchos::null" );
00244       const Teuchos::RCP<EpetraMultiVector> ret = Teuchos::rcp(new Xpetra::EpetraMultiVector(getMap(block),full->getNumVectors(),true));
00245       ExtractVector(*efull,block,*ret);
00246       return ret;
00247     }
00248 
00249     virtual Teuchos::RCP<VectorClass> getVector(size_t i) const
00250     {
00251       return Teuchos::rcp(new Xpetra::EpetraVector(getMap(i),true));
00252     }
00253 
00254     virtual Teuchos::RCP<MultiVectorClass> getVector(size_t i, size_t numvec) const
00255     {
00256       return Teuchos::rcp(new Xpetra::EpetraMultiVector(getMap(i),numvec,true));
00257     }
00258 
00261 
00263     size_t NumMaps() const { return maps_.size(); }
00264 
00266     const Teuchos::RCP<const MapClass> getMap(size_t i) const { return maps_[i]; }
00267 
00269     const Teuchos::RCP<const MapClass> getFullMap() const { return fullmap_; }
00270 
00271     size_t getMapIndexForGID(int gid) const {
00272 
00273       for(size_t i = 0; i < NumMaps(); i++) {
00274         if(getMap(i)->isNodeGlobalElement(gid) == true)
00275           return i;
00276       }
00277       TEUCHOS_TEST_FOR_EXCEPTION( false, Xpetra::Exceptions::RuntimeError,
00278                   "getMapIndexForGID: GID " << gid << " is not contained by a map in mapextractor." );
00279       return 0;
00280     }
00282   private:
00283 
00284     virtual bool CheckConsistency() const {
00285       const Teuchos::RCP<const MapClass> fullMap = getFullMap();
00286 
00287       for(size_t i = 0; i<NumMaps(); i++) {
00288         const Teuchos::RCP<const MapClass> map = getMap(i);
00289 
00290         Teuchos::ArrayView< const int > mapGids = map->getNodeElementList();
00291         Teuchos::ArrayView< const int >::const_iterator it;
00292         for(it = mapGids.begin(); it!=mapGids.end(); it++) {
00293           if(fullMap->isNodeGlobalElement(*it)==false)
00294             return false; // Global ID (*it) not found locally on this proc in fullMap -> error
00295         }
00296       }
00297       return true;
00298     }
00299 
00300   protected:
00301     std::vector<Teuchos::RCP<const Xpetra::EpetraMap > > maps_;
00302     Teuchos::RCP<const EpetraMap > fullmap_;
00303     std::vector<Teuchos::RCP<ImportClass > > importer_;
00304   };
00305 }
00306 
00307 
00308 #endif /* XPETRA_EPETRAMAPEXTRACTOR_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines