Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_PeriodicBC_Matcher.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
44 
45 #include <stk_mesh/base/GetEntities.hpp>
46 #include <stk_mesh/base/GetBuckets.hpp>
47 #include <stk_mesh/base/FieldBase.hpp>
48 
49 #include "Panzer_NodeType.hpp"
50 #include "Tpetra_Map.hpp"
51 #include "Tpetra_Import.hpp"
52 #include "Tpetra_Vector.hpp"
53 
54 #include "Teuchos_FancyOStream.hpp"
55 
56 namespace panzer_stk {
57 
58 namespace periodic_helpers {
59 
61 getGlobalPairing(const std::vector<std::size_t> & locallyRequiredIds,
62  const std::vector<std::pair<std::size_t,std::size_t> > & locallyMatchedIds,
63  const STK_Interface & mesh,bool failure)
64 {
65  using LO = panzer::LocalOrdinal;
66  using GO = panzer::GlobalOrdinal;
68  using Map = Tpetra::Map<LO,GO,NODE>;
69  using Importer = Tpetra::Import<LO,GO,NODE>;
70 
71  auto comm = mesh.getComm();
72 
73  // this is needed to prevent hanging: it is unfortunately expensive
74  // need a better way!
75  int myVal = failure ? 1 : 0;
76  int sumVal = 0;
77  Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&myVal,&sumVal);
78  TEUCHOS_ASSERT(sumVal==0);
79 
80  std::vector<GO> requiredInts(locallyRequiredIds.size());
81  for(std::size_t i=0;i<requiredInts.size();i++)
82  requiredInts[i] = locallyRequiredIds[i];
83 
84  std::vector<GO> providedInts(locallyMatchedIds.size());
85  for(std::size_t i=0;i<locallyMatchedIds.size();i++)
86  providedInts[i] = locallyMatchedIds[i].first;
87 
88  // maps and communciation all set up
90  Teuchos::RCP<Map> requiredMap = Teuchos::rcp(new Map(computeInternally,Teuchos::ArrayView<const GO>(requiredInts),0,comm));
91  Teuchos::RCP<Map> providedMap = Teuchos::rcp(new Map(computeInternally,Teuchos::ArrayView<const GO>(providedInts),0,comm));
92  Importer importer(providedMap,requiredMap);
93 
94  // this is what to distribute
95  Tpetra::Vector<GO,LO,GO,NODE> providedVector(providedMap);
96  providedVector.sync_host();
97  auto pvHost = providedVector.getLocalViewHost();
98  for(std::size_t i=0;i<locallyMatchedIds.size();i++)
99  pvHost(i,0) = locallyMatchedIds[i].second;
100  providedVector.modify_host();
101 
102  Tpetra::Vector<GO,LO,GO,NODE> requiredVector(requiredMap);
103  requiredVector.doImport(providedVector,importer,Tpetra::INSERT);
104 
105 
107  = Teuchos::rcp(new std::vector<std::pair<std::size_t,std::size_t> >(requiredInts.size()));
108 
109  requiredVector.sync_host();
110  auto rvHost = requiredVector.getLocalViewHost();
111  for(std::size_t i=0;i<result->size();i++) {
112  (*result)[i].first = requiredInts[i];
113  (*result)[i].second = rvHost(i,0);
114  }
115  return result;
116 }
117 
118 
119 
125  const std::string & sideName, const std::string type_)
126 {
129 
130  // grab nodes owned by requested side
132  std::stringstream ss;
133  ss << "Can't find part=\"" << sideName << "\"" << std::endl;
134  stk::mesh::Part * side = metaData->get_part(sideName,ss.str().c_str());
135  stk::mesh::Selector mySides = *side & (metaData->locally_owned_part() | metaData->globally_shared_part());
136 
137  stk::mesh::EntityRank rank;
138  unsigned int offset = 0; // offset to avoid giving nodes, edges, faces the same sideId
139  if(type_ == "coord"){
140  rank = mesh.getNodeRank();
141  } else if(type_ == "edge"){
142  rank = mesh.getEdgeRank();
143  offset = mesh.getMaxEntityId(mesh.getNodeRank());
144  } else if(type_ == "face"){
145  rank = mesh.getFaceRank();
146  offset = mesh.getMaxEntityId(mesh.getNodeRank())+mesh.getMaxEntityId(mesh.getEdgeRank());
147  } else {
148  ss << "Can't do BCs of type " << type_ << std::endl;
149  TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error, ss.str())
150  }
151 
152  std::vector<stk::mesh::Bucket*> const& nodeBuckets =
153  bulkData->get_buckets(rank, mySides);
154 
155  // build id vector
157  std::size_t nodeCount = 0;
158  for(std::size_t b=0;b<nodeBuckets.size();b++)
159  nodeCount += nodeBuckets[b]->size();
160 
162  = Teuchos::rcp(new std::vector<std::size_t>(nodeCount));
163 
164  // loop over node buckets
165  for(std::size_t b=0,index=0;b<nodeBuckets.size();b++) {
166  stk::mesh::Bucket & bucket = *nodeBuckets[b];
167 
168  for(std::size_t n=0;n<bucket.size();n++,index++)
169  (*sideIds)[index] = bulkData->identifier(bucket[n]) + offset;
170  }
171 
172  return sideIds;
173 }
174 
175 std::pair<Teuchos::RCP<std::vector<std::size_t> >,
178  const std::string & sideName, const std::string type_)
179 {
180  unsigned physicalDim = mesh.getDimension();
181 
184 
185  // grab nodes owned by requested side
187  std::stringstream ss;
188  ss << "Can't find part=\"" << sideName << "\"" << std::endl;
189  stk::mesh::Part * side = metaData->get_part(sideName,ss.str().c_str());
190  stk::mesh::Selector mySides = (*side) & metaData->locally_owned_part();
191 
192  stk::mesh::EntityRank rank;
194  unsigned int offset = 0;
195  if(type_ == "coord"){
196  rank = mesh.getNodeRank();
197  field = & mesh.getCoordinatesField();
198  } else if(type_ == "edge"){
199  rank = mesh.getEdgeRank();
200  field = & mesh.getEdgesField();
201  offset = mesh.getMaxEntityId(mesh.getNodeRank());
202  } else if(type_ == "face"){
203  rank = mesh.getFaceRank();
204  field = & mesh.getFacesField();
205  offset = mesh.getMaxEntityId(mesh.getNodeRank())+mesh.getMaxEntityId(mesh.getEdgeRank());
206  } else {
207  ss << "Can't do BCs of type " << type_ << std::endl;
208  TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error, ss.str())
209  }
210 
211  std::vector<stk::mesh::Bucket*> const& nodeBuckets =
212  bulkData->get_buckets(rank, mySides);
213 
214  // build id vector
216  std::size_t nodeCount = 0;
217  for(std::size_t b=0;b<nodeBuckets.size();b++)
218  nodeCount += nodeBuckets[b]->size();
219 
221  = Teuchos::rcp(new std::vector<std::size_t>(nodeCount));
223  = Teuchos::rcp(new std::vector<Teuchos::Tuple<double,3> >(nodeCount));
224 
225  // loop over node buckets
226  for(std::size_t b=0,index=0;b<nodeBuckets.size();b++) {
227  stk::mesh::Bucket & bucket = *nodeBuckets[b];
228  double const* array = stk::mesh::field_data(*field, bucket);
229 
230  for(std::size_t n=0;n<bucket.size();n++,index++) {
231  (*sideIds)[index] = bulkData->identifier(bucket[n]) + offset;
232  Teuchos::Tuple<double,3> & coord = (*sideCoords)[index];
233 
234  // copy coordinates into multi vector
235  for(std::size_t d=0;d<physicalDim;d++)
236  coord[d] = array[physicalDim*n + d];
237 
238  // need to ensure that higher dimensions are properly zeroed
239  // required for 1D periodic boundary conditions
240  for(std::size_t d=physicalDim;d<3;d++)
241  coord[d] = 0;
242  }
243  }
244 
245  return std::make_pair(sideIds,sideCoords);
246 }
247 
248 std::pair<Teuchos::RCP<std::vector<std::size_t> >,
251  const std::string & sideName, const std::string type_)
252 {
253  using Teuchos::RCP;
254  using Teuchos::rcp;
255  using LO = panzer::LocalOrdinal;
256  using GO = panzer::GlobalOrdinal;
258  using Map = Tpetra::Map<LO,GO,NODE>;
259  using Importer = Tpetra::Import<LO,GO,NODE>;
260 
261  // Epetra_MpiComm Comm(mesh.getBulkData()->parallel());
262  auto comm = mesh.getComm();
263 
264  unsigned physicalDim = mesh.getDimension();
265 
266  // grab local IDs and coordinates on this side
267  // and build local epetra vector
269 
270  std::pair<Teuchos::RCP<std::vector<std::size_t> >,
272  getLocalSideIdsAndCoords(mesh,sideName,type_);
273 
274  std::vector<std::size_t> & local_side_ids = *sidePair.first;
275  std::vector<Teuchos::Tuple<double,3> > & local_side_coords = *sidePair.second;
276  int nodeCount = local_side_ids.size();
277 
278  // build local Tpetra objects
280  RCP<Map> idMap_ = rcp(new Map(computeInternally,nodeCount,0,comm));
281  RCP<Tpetra::Vector<GO,LO,GO,NODE>> localIdVec_ = rcp(new Tpetra::Vector<GO,LO,GO,NODE>(idMap_));
282  RCP<Tpetra::MultiVector<double,LO,GO,NODE>> localCoordVec_ = rcp(new Tpetra::MultiVector<double,LO,GO,NODE>(idMap_,physicalDim));
283 
284  // copy local Ids and coords into Tpetra vectors
285  localIdVec_->sync_host();
286  localCoordVec_->sync_host();
287  auto lidHost = localIdVec_->getLocalViewHost();
288  auto lcoordHost = localCoordVec_->getLocalViewHost();
289  for(std::size_t n=0;n<local_side_ids.size();n++) {
290  std::size_t nodeId = local_side_ids[n];
291  Teuchos::Tuple<double,3> & coords = local_side_coords[n];
292 
293  lidHost(n,0) = static_cast<GO>(nodeId);
294  for(unsigned d=0;d<physicalDim;d++)
295  lcoordHost(n,d) = coords[d];
296  }
297  localIdVec_->modify_host();
298  localCoordVec_->modify_host();
299 
300  // fully distribute epetra vector across all processors
301  // (these are "distributed" or "dist" objects)
303 
304  int dist_nodeCount = idMap_->getGlobalNumElements();
305 
306  // build global Tpetra objects
307  RCP<Map> distMap_ = rcp(new Map(dist_nodeCount,0,comm,Tpetra::LocallyReplicated));
308  RCP<Tpetra::Vector<GO,LO,GO,NODE>> distIdVec_ = rcp(new Tpetra::Vector<GO,LO,GO,NODE>(distMap_));
309  RCP<Tpetra::MultiVector<double,LO,GO,NODE>> distCoordVec_ = rcp(new Tpetra::MultiVector<double,LO,GO,NODE>(distMap_,physicalDim));
310 
311  // export to the localVec object from the "vector" object
312  Importer importer_(idMap_,distMap_);
313  distIdVec_->doImport(*localIdVec_,importer_,Tpetra::INSERT);
314  distCoordVec_->doImport(*localCoordVec_,importer_,Tpetra::INSERT);
315 
316  // convert back to generic stl vector objects
318 
320  = Teuchos::rcp(new std::vector<std::size_t>(dist_nodeCount));
322  = Teuchos::rcp(new std::vector<Teuchos::Tuple<double,3> >(dist_nodeCount));
323 
324  // copy local Ids from Tpetra vector
325  distIdVec_->sync_host();
326  distCoordVec_->sync_host();
327  const auto didHost = distIdVec_->getLocalViewHost();
328  const auto dcoordHost = distCoordVec_->getLocalViewHost();
329  for(std::size_t n=0;n<dist_side_ids->size();++n) {
330  (*dist_side_ids)[n] = didHost(n,0);
331 
332  Teuchos::Tuple<double,3> & coords = (*dist_side_coords)[n];
333  for(unsigned d=0;d<physicalDim;++d) {
334  coords[d] = dcoordHost(n,d);
335  }
336  // ensure that higher dimensions are zero
337  for(unsigned d=physicalDim;d<3;++d)
338  coords[d] = 0;
339  }
340 
341  return std::make_pair(dist_side_ids,dist_side_coords);
342 }
343 
344 } // end periodic_helpers
345 
346 } // end panzer_stk
Teuchos::RCP< std::vector< std::size_t > > getLocalSideIds(const STK_Interface &mesh, const std::string &sideName, const std::string type_)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
stk::mesh::EntityId getMaxEntityId(unsigned entityRank) const
get max entity ID of type entityRank
std::pair< Teuchos::RCP< std::vector< std::size_t > >, Teuchos::RCP< std::vector< Teuchos::Tuple< double, 3 > > > > getLocalSideIdsAndCoords(const STK_Interface &mesh, const std::string &sideName, const std::string type_)
PHX::MDField< ScalarT, panzer::Cell, panzer::IP > result
A field that will be used to build up the result of the integral we&#39;re performing.
unsigned getDimension() const
get the dimension
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
stk::mesh::Field< double, stk::mesh::Cartesian > VectorFieldType
Teuchos::RCP< stk::mesh::BulkData > getBulkData() const
Kokkos::Compat::KokkosDeviceWrapperNode< PHX::Device > TpetraNodeType
stk::mesh::EntityRank getFaceRank() const
Teuchos::RCP< stk::mesh::MetaData > getMetaData() const
const VectorFieldType & getEdgesField() const
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field
A field to which we&#39;ll contribute, or in which we&#39;ll store, the result of computing this integral...
stk::mesh::EntityRank getEdgeRank() const
Teuchos::RCP< std::vector< std::pair< std::size_t, std::size_t > > > getGlobalPairing(const std::vector< std::size_t > &locallyRequiredIds, const std::vector< std::pair< std::size_t, std::size_t > > &locallyMatchedIds, const STK_Interface &mesh, bool failure)
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
get the comm associated with this mesh
const VectorFieldType & getFacesField() const
#define TEUCHOS_ASSERT(assertion_test)
stk::mesh::EntityRank getNodeRank() const
std::pair< Teuchos::RCP< std::vector< std::size_t > >, Teuchos::RCP< std::vector< Teuchos::Tuple< double, 3 > > > > getSideIdsAndCoords(const STK_Interface &mesh, const std::string &sideName, const std::string type_)
const VectorFieldType & getCoordinatesField() const