Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_GlobalIndexer_EpetraUtilities.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #include "PanzerDofMgr_config.hpp"
12 
13 #ifdef PANZER_HAVE_EPETRA
14 
16 
17 namespace panzer {
18 
20 buildGhostedFieldReducedVectorEpetra(const GlobalIndexer & ugi)
21 {
22  typedef Epetra_BlockMap Map;
23  typedef Epetra_IntVector IntVector;
24 
25  std::vector<panzer::GlobalOrdinal> indices;
26  std::vector<std::string> blocks;
27 
28  ugi.getOwnedAndGhostedIndices(indices);
29  ugi.getElementBlockIds(blocks);
30 
31  std::vector<int> fieldNumbers(indices.size(),-1);
32 
33  const Teuchos::RCP<const Teuchos::MpiComm<int> > mpiComm = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(ugi.getComm());
35  if (mpiComm != Teuchos::null)
36  comm = Teuchos::rcp(new Epetra_MpiComm(*mpiComm->getRawMpiComm()));
37 
38  Teuchos::RCP<Map> ghostedMap
39  = Teuchos::rcp(new Map(-1, static_cast<int>(indices.size()), Teuchos::arrayViewFromVector(indices).getRawPtr(),
41 
42  // build a map from local ids to a field number
43  for(std::size_t blk=0;blk<blocks.size();blk++) {
44  std::string blockId = blocks[blk];
45 
46  const std::vector<panzer::LocalOrdinal> & elements = ugi.getElementBlock(blockId);
47  const std::vector<int> & fields = ugi.getBlockFieldNumbers(blockId);
48 
49  // loop over all elements, and set field number in output array
50  std::vector<panzer::GlobalOrdinal> gids(fields.size());
51  for(std::size_t e=0;e<elements.size();e++) {
52  ugi.getElementGIDs(elements[e],gids);
53 
54  for(std::size_t f=0;f<fields.size();f++) {
55  int fieldNum = fields[f];
56  panzer::GlobalOrdinal gid = gids[f];
57  std::size_t lid = ghostedMap->LID(gid); // hash table lookup
58 
59  fieldNumbers[lid] = fieldNum;
60  }
61  }
62  }
63 
64  // produce a reduced vector containing only fields known by this processor
65  std::vector<panzer::GlobalOrdinal> reducedIndices;
66  std::vector<int> reducedFieldNumbers;
67  for(std::size_t i=0;i<fieldNumbers.size();i++) {
68  if(fieldNumbers[i]>-1) {
69  reducedIndices.push_back(indices[i]);
70  reducedFieldNumbers.push_back(fieldNumbers[i]);
71  }
72  }
73 
74  Teuchos::RCP<Map> reducedMap
75  = Teuchos::rcp(new Map(-1, static_cast<int>(reducedIndices.size()), Teuchos::arrayViewFromVector(reducedIndices).getRawPtr(),
77  return Teuchos::rcp(new IntVector(Copy,*reducedMap,Teuchos::arrayViewFromVector(reducedFieldNumbers).getRawPtr()));
78 }
79 
80 void buildGhostedFieldVectorEpetra(const GlobalIndexer & ugi,
81  std::vector<int> & fieldNumbers,
82  const Teuchos::RCP<const Epetra_IntVector> & reducedVec)
83 {
84  typedef Epetra_IntVector IntVector;
85 
87 
88  fieldNumbers.resize(dest->MyLength());
89  Teuchos::ArrayView<int> av = Teuchos::arrayViewFromVector(fieldNumbers);
90  dest->ExtractCopy(av.getRawPtr());
91 }
92 
94 buildGhostedFieldVectorEpetra(const GlobalIndexer & ugi,
95  const Teuchos::RCP<const Epetra_IntVector> & reducedVec)
96 {
97  typedef Epetra_BlockMap Map;
98  typedef Epetra_IntVector IntVector;
99  typedef Epetra_Import Importer;
100 
101  // first step: get a reduced field number vector and build a map to
102  // contain the full field number vector
104 
105  Teuchos::RCP<Map> destMap;
106  {
107  std::vector<panzer::GlobalOrdinal> indices;
108  ugi.getOwnedAndGhostedIndices(indices);
109 
110  const Teuchos::RCP<const Teuchos::MpiComm<int> > mpiComm = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(ugi.getComm());
112  if (mpiComm != Teuchos::null)
113  comm = Teuchos::rcp(new Epetra_MpiComm(*mpiComm->getRawMpiComm()));
114 
115  destMap = Teuchos::rcp(new Map(-1, static_cast<int>(indices.size()), Teuchos::arrayViewFromVector(indices).getRawPtr(),
116  1, Teuchos::OrdinalTraits<int>::zero(), *comm));
117  }
118 
119  Teuchos::RCP<const IntVector> source = reducedVec;
120  if(source==Teuchos::null)
122  Teuchos::RCP<const Map> sourceMap = Teuchos::rcpFromRef(source->Map());
123 
124  // second step: perform the global communciation required to fix the
125  // interface conditions (where this processor doesn't know what field
126  // some indices are)
128  Teuchos::RCP<IntVector> dest = Teuchos::rcp(new IntVector(*destMap));
129  Importer importer(*destMap,*sourceMap);
130 
131  dest->Import(*source,importer,Insert);
132 
133  return dest;
134 }
135 
137  : ugi_(ugi)
138 {
139  gh_reducedFieldVector_ = buildGhostedFieldReducedVectorEpetra(*ugi_);
140  gh_fieldVector_ = buildGhostedFieldVectorEpetra(*ugi_,gh_reducedFieldVector_);
141 }
142 
143 
145 {
146  // build (unghosted) vector and map
147  std::vector<panzer::GlobalOrdinal> indices;
148  ugi_->getOwnedIndices(indices);
149 
150  const Teuchos::RCP<const Teuchos::MpiComm<int> > mpiComm = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(ugi_->getComm(),true);
152  if (mpiComm != Teuchos::null)
153  comm = Teuchos::rcp(new Epetra_MpiComm(*mpiComm->getRawMpiComm()));
154 
155  Teuchos::RCP<Map> destMap
156  = Teuchos::rcp(new Map(-1, static_cast<int>(indices.size()),
157  Teuchos::arrayViewFromVector(indices).getRawPtr(),
158  // &indices.begin(),
159  1, Teuchos::OrdinalTraits<int>::zero(), *comm));
160 
161  Teuchos::RCP<IntVector> localFieldVector = Teuchos::rcp(new IntVector(*destMap));
162 
163  Epetra_Import importer(*destMap,source.Map());
164  localFieldVector->Import(source,importer,Insert);
165 
166  fieldVector_ = localFieldVector;
167 }
168 
170 ArrayToFieldVectorEpetra::getFieldMap(const std::string & fieldName) const
171 {
172  return getFieldMap(ugi_->getFieldNum(fieldName));
173 }
174 
176 ArrayToFieldVectorEpetra::getFieldMap(int fieldNum) const
177 {
178  if(fieldMaps_[fieldNum]==Teuchos::null) {
179  // if neccessary build field vector
180  if(fieldVector_==Teuchos::null)
182 
183  fieldMaps_[fieldNum] = panzer::getFieldMapEpetra(fieldNum,*fieldVector_);
184  }
185 
186  return Teuchos::rcp_dynamic_cast<const Epetra_Map>(fieldMaps_[fieldNum]);
187 }
188 
190 getFieldMapEpetra(int fieldNum,const Epetra_IntVector & fieldTVector)
191 {
192  Teuchos::RCP<const Epetra_BlockMap> origMap = Teuchos::rcpFromRef(fieldTVector.Map());
193  std::vector<int> fieldVector(fieldTVector.MyLength());
194  Teuchos::ArrayView<int> av = Teuchos::arrayViewFromVector(fieldVector);
195  fieldTVector.ExtractCopy(av.getRawPtr());
196 
197  std::vector<int> mapVector;
198  for(std::size_t i=0;i<fieldVector.size();i++) {
199  if(fieldVector[i]==fieldNum)
200  mapVector.push_back(origMap->GID(i));
201  }
202 
204  = Teuchos::rcp(new Epetra_BlockMap(-1, static_cast<int>(mapVector.size()), Teuchos::arrayViewFromVector(mapVector).getRawPtr(), 1, Teuchos::OrdinalTraits<int>::zero(), origMap->Comm()));
205 
206  return finalMap;
207 }
208 
209 } // end namespace panzer
210 
211 #endif //end PANZER_HAVE_EPETRA
std::map< int, Teuchos::RCP< const Map > > fieldMaps_
(unghosted) field vector (as needed)
Teuchos::RCP< Epetra_IntVector > buildGhostedFieldReducedVectorEpetra(const GlobalIndexer &ugi)
Teuchos::RCP< const IntVector > fieldVector_
Maps for each field (as needed)
Teuchos::RCP< const Epetra_IntVector > buildGhostedFieldVectorEpetra(const GlobalIndexer &ugi, const Teuchos::RCP< const Epetra_IntVector > &reducedVec=Teuchos::null)
int ExtractCopy(int *V) const
Teuchos::RCP< const Epetra_Map > getFieldMap(const std::string &fieldName) const
void buildFieldVector(const Epetra_IntVector &source) const
build unghosted field vector from ghosted field vector
Teuchos::RCP< const IntVector > gh_fieldVector_
ghosted reduced field vector
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int GID(int LID) const
T * getRawPtr() const
const Epetra_Comm & Comm() const
Teuchos::RCP< const GlobalIndexer > ugi_
DOF mapping.
Teuchos::RCP< const Epetra_BlockMap > getFieldMapEpetra(int fieldNum, const Epetra_IntVector &fieldVector)
ArrayToFieldVectorEpetra()
Maps for each field (as needed)
int MyLength() const