Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_LocalMeshUtilities.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 
43 #include "Panzer_NodeType.hpp"
45 #include "Panzer_STK_Interface.hpp"
48 
49 #include "Panzer_HashUtils.hpp"
50 #include "Panzer_LocalMeshInfo.hpp"
52 #include "Panzer_FaceToElement.hpp"
53 
54 #include "Panzer_FieldPattern.hpp"
59 
60 #include "Panzer_ConnManager.hpp"
61 
62 #include "Phalanx_KokkosDeviceTypes.hpp"
63 
64 #include "Teuchos_Assert.hpp"
66 
67 #include "Tpetra_Import.hpp"
68 
69 #include <string>
70 #include <map>
71 #include <vector>
72 #include <unordered_set>
73 
74 namespace panzer_stk
75 {
76 
77 // No external access
78 namespace
79 {
80 
84 Kokkos::DynRankView<double,PHX::Device>
85 buildGhostedNodes(const Tpetra::Import<int,panzer::GlobalOrdinal,panzer::TpetraNodeType> & importer,
86  Kokkos::DynRankView<const double,PHX::Device> owned_nodes)
87 {
88  using Teuchos::RCP;
89  using Teuchos::rcp;
90 
91  typedef Tpetra::MultiVector<double,int,panzer::GlobalOrdinal,panzer::TpetraNodeType> mvec_type;
92  typedef typename mvec_type::dual_view_type dual_view_type;
93 
94  size_t owned_cell_cnt = importer.getSourceMap()->getLocalNumElements();
95  size_t ghstd_cell_cnt = importer.getTargetMap()->getLocalNumElements();
96  int nodes_per_cell = owned_nodes.extent(1);
97  int space_dim = owned_nodes.extent(2);
98 
99  TEUCHOS_ASSERT(owned_nodes.extent(0)==owned_cell_cnt);
100 
101  // build node multivector
102  RCP<mvec_type> owned_nodes_mv = rcp(new mvec_type(importer.getSourceMap(),nodes_per_cell*space_dim));
103  RCP<mvec_type> ghstd_nodes_mv = rcp(new mvec_type(importer.getTargetMap(),nodes_per_cell*space_dim));
104 
105  {
106  auto owned_nodes_view = owned_nodes_mv->getLocalViewDevice(Tpetra::Access::OverwriteAll);
107  Kokkos::parallel_for(owned_cell_cnt, KOKKOS_LAMBDA (size_t i) {
108  int l = 0;
109  for(int j=0;j<nodes_per_cell;j++)
110  for(int k=0;k<space_dim;k++,l++)
111  owned_nodes_view(i,l) = owned_nodes(i,j,k);
112  });
113  }
114 
115  // communicate ghstd nodes
116  ghstd_nodes_mv->doImport(*owned_nodes_mv,importer,Tpetra::INSERT);
117 
118  // copy multivector into ghstd node structure
119  Kokkos::DynRankView<double,PHX::Device> ghstd_nodes("ghstd_nodes",ghstd_cell_cnt,nodes_per_cell,space_dim);
120  {
121  auto ghstd_nodes_view = ghstd_nodes_mv->getLocalViewDevice(Tpetra::Access::ReadOnly);
122  Kokkos::parallel_for(ghstd_cell_cnt, KOKKOS_LAMBDA (size_t i) {
123  int l = 0;
124  for(int j=0;j<nodes_per_cell;j++)
125  for(int k=0;k<space_dim;k++,l++)
126  ghstd_nodes(i,j,k) = ghstd_nodes_view(i,l);
127  } );
128  Kokkos::fence();
129  }
130 
131  return ghstd_nodes;
132 } // end build ghstd nodes
133 void
134 setupLocalMeshBlockInfo(const panzer_stk::STK_Interface & mesh,
135  panzer::ConnManager & conn,
136  const panzer::LocalMeshInfo & mesh_info,
137  const std::string & element_block_name,
138  panzer::LocalMeshBlockInfo & block_info)
139 {
140 
141  // This function identifies all cells in mesh_info that belong to element_block_name
142  // and creates a block_info from it.
143 
144  const int num_parent_owned_cells = mesh_info.num_owned_cells;
145 
146  // Make sure connectivity is setup for interfaces between cells
147  {
148  const shards::CellTopology & topology = *(mesh.getCellTopology(element_block_name));
150  if(topology.getDimension() == 1){
151  cell_pattern = Teuchos::rcp(new panzer::EdgeFieldPattern(topology));
152  } else if(topology.getDimension() == 2){
153  cell_pattern = Teuchos::rcp(new panzer::FaceFieldPattern(topology));
154  } else if(topology.getDimension() == 3){
155  cell_pattern = Teuchos::rcp(new panzer::ElemFieldPattern(topology));
156  }
157 
158  {
159  PANZER_FUNC_TIME_MONITOR("Build connectivity");
160  conn.buildConnectivity(*cell_pattern);
161  }
162  }
163 
164  std::vector<panzer::LocalOrdinal> owned_block_cells;
165  auto local_cells_h = Kokkos::create_mirror_view(mesh_info.local_cells);
166  Kokkos::deep_copy(local_cells_h, mesh_info.local_cells);
167  for(int parent_owned_cell=0;parent_owned_cell<num_parent_owned_cells;++parent_owned_cell){
168  const panzer::LocalOrdinal local_cell = local_cells_h(parent_owned_cell);
169  const bool is_in_block = conn.getBlockId(local_cell) == element_block_name;
170 
171  if(is_in_block){
172  owned_block_cells.push_back(parent_owned_cell);
173  }
174 
175  }
176 
177  if ( owned_block_cells.size() == 0 )
178  return;
179  block_info.num_owned_cells = owned_block_cells.size();
180  block_info.element_block_name = element_block_name;
181  block_info.cell_topology = mesh.getCellTopology(element_block_name);
182  {
183  PANZER_FUNC_TIME_MONITOR("panzer::partitioning_utilities::setupSubLocalMeshInfo");
184  panzer::partitioning_utilities::setupSubLocalMeshInfo(mesh_info, owned_block_cells, block_info);
185  }
186 }
187 
188 
189 void
190 setupLocalMeshSidesetInfo(const panzer_stk::STK_Interface & mesh,
191  panzer::ConnManager& /* conn */,
192  const panzer::LocalMeshInfo & mesh_info,
193  const std::string & element_block_name,
194  const std::string & sideset_name,
195  panzer::LocalMeshSidesetInfo & sideset_info)
196 {
197 
198  // We use these typedefs to make the algorithm slightly more clear
199  using LocalOrdinal = panzer::LocalOrdinal;
200  using ParentOrdinal = int;
201  using SubcellOrdinal = short;
202 
203  // This function identifies all cells in mesh_info that belong to element_block_name
204  // and creates a block_info from it.
205 
206  // This is a list of all entities that make up the 'side'
207  std::vector<stk::mesh::Entity> side_entities;
208 
209  // Grab the side entities associated with this sideset on the mesh
210  // Note: Throws exception if element block or sideset doesn't exist
211  try{
212 
213  mesh.getAllSides(sideset_name, element_block_name, side_entities);
214 
215  } catch(STK_Interface::SidesetException & e) {
216  std::stringstream ss;
217  std::vector<std::string> sideset_names;
218  mesh.getSidesetNames(sideset_names);
219 
220  // build an error message
221  ss << e.what() << "\nChoose existing sideset:\n";
222  for(const auto & optional_sideset_name : sideset_names){
223  ss << "\t\"" << optional_sideset_name << "\"\n";
224  }
225 
226  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(true,std::logic_error,ss.str());
227 
228  } catch(STK_Interface::ElementBlockException & e) {
229  std::stringstream ss;
230  std::vector<std::string> element_block_names;
231  mesh.getElementBlockNames(element_block_names);
232 
233  // build an error message
234  ss << e.what() << "\nChoose existing element block:\n";
235  for(const auto & optional_element_block_name : element_block_names){
236  ss << "\t\"" << optional_element_block_name << "\"\n";
237  }
238 
239  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(true,std::logic_error,ss.str());
240 
241  } catch(std::logic_error & e) {
242  std::stringstream ss;
243  ss << e.what() << "\nUnrecognized logic error.\n";
244 
245  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(true,std::logic_error,ss.str());
246 
247  }
248 
249  // We now have a list of sideset entities, lets unwrap them and create the sideset_info!
250  std::map<ParentOrdinal,std::vector<SubcellOrdinal> > owned_parent_cell_to_subcell_indexes;
251  {
252 
253  // This is the subcell dimension we are trying to line up on the sideset
254  const size_t face_subcell_dimension = static_cast<size_t>(mesh.getCellTopology(element_block_name)->getDimension()-1);
255 
256  // List of local subcell indexes indexed by element:
257  // For example: a Tet (element) would have
258  // - 4 triangular faces (subcell_index 0-3, subcell_dimension=2)
259  // - 6 edges (subcell_index 0-5, subcell_dimension=1)
260  // - 4 nodes (subcell_index 0-3, subcell_dimension=0)
261  // Another example: a Line (element) would have
262  // - 2 nodes (subcell_index 0-1, subcell_dimension=0)
263  // The nodes coincide with the element vertices for these first order examples
264  std::vector<stk::mesh::Entity> elements;
265  std::vector<size_t> subcell_indexes;
266  std::vector<size_t> subcell_dimensions;
267  panzer_stk::workset_utils::getSideElementCascade(mesh, element_block_name, side_entities, subcell_dimensions, subcell_indexes, elements);
268  const size_t num_elements = subcell_dimensions.size();
269 
270  // We need a fast lookup for maping local indexes to parent indexes
271  std::unordered_map<LocalOrdinal,ParentOrdinal> local_to_parent_lookup;
272  auto local_cells_h = Kokkos::create_mirror_view(mesh_info.local_cells);
273  Kokkos::deep_copy(local_cells_h, mesh_info.local_cells);
274  for(ParentOrdinal parent_index=0; parent_index<static_cast<ParentOrdinal>(mesh_info.local_cells.extent(0)); ++parent_index)
275  local_to_parent_lookup[local_cells_h(parent_index)] = parent_index;
276 
277  // Add the subcell indexes for each element on the sideset
278  // TODO: There is a lookup call here to map from local indexes to parent indexes which slows things down. Maybe there is a way around that
279  for(size_t element_index=0; element_index<num_elements; ++element_index) {
280  const size_t subcell_dimension = subcell_dimensions[element_index];
281 
282  // Add subcell to map
283  if(subcell_dimension == face_subcell_dimension){
284  const SubcellOrdinal subcell_index = static_cast<SubcellOrdinal>(subcell_indexes[element_index]);
285  const LocalOrdinal element_local_index = static_cast<LocalOrdinal>(mesh.elementLocalId(elements[element_index]));
286 
287  // Look up the parent cell index using the local cell index
288  const auto itr = local_to_parent_lookup.find(element_local_index);
289  TEUCHOS_ASSERT(itr!= local_to_parent_lookup.end());
290  const ParentOrdinal element_parent_index = itr->second;
291 
292  owned_parent_cell_to_subcell_indexes[element_parent_index].push_back(subcell_index);
293  }
294  }
295  }
296 
297  // We now know the mapping of parent cell indexes to subcell indexes touching the sideset
298 
299  const panzer::LocalOrdinal num_owned_cells = owned_parent_cell_to_subcell_indexes.size();
300 
301  sideset_info.element_block_name = element_block_name;
302  sideset_info.sideset_name = sideset_name;
303  sideset_info.cell_topology = mesh.getCellTopology(element_block_name);
304 
305  sideset_info.num_owned_cells = num_owned_cells;
306 
307  struct face_t{
308  face_t(const ParentOrdinal c0,
309  const ParentOrdinal c1,
310  const SubcellOrdinal sc0,
311  const SubcellOrdinal sc1)
312  {
313  cell_0=c0;
314  cell_1=c1;
315  subcell_index_0=sc0;
316  subcell_index_1=sc1;
317  }
318  ParentOrdinal cell_0;
319  ParentOrdinal cell_1;
320  SubcellOrdinal subcell_index_0;
321  SubcellOrdinal subcell_index_1;
322  };
323 
324 
325  // Figure out how many cells on the other side of the sideset are ghost or virtual
326  std::unordered_set<panzer::LocalOrdinal> owned_parent_cells_set, ghstd_parent_cells_set, virtual_parent_cells_set;
327  std::vector<face_t> faces;
328  {
329  auto cell_to_faces_h = Kokkos::create_mirror_view(mesh_info.cell_to_faces);
330  auto face_to_cells_h = Kokkos::create_mirror_view(mesh_info.face_to_cells);
331  auto face_to_lidx_h = Kokkos::create_mirror_view(mesh_info.face_to_lidx);
332  Kokkos::deep_copy(cell_to_faces_h, mesh_info.cell_to_faces);
333  Kokkos::deep_copy(face_to_cells_h, mesh_info.face_to_cells);
334  Kokkos::deep_copy(face_to_lidx_h, mesh_info.face_to_lidx);
335  panzer::LocalOrdinal parent_virtual_cell_offset = mesh_info.num_owned_cells + mesh_info.num_ghstd_cells;
336  for(const auto & local_cell_index_pair : owned_parent_cell_to_subcell_indexes){
337 
338  const ParentOrdinal parent_cell = local_cell_index_pair.first;
339  const auto & subcell_indexes = local_cell_index_pair.second;
340 
341  owned_parent_cells_set.insert(parent_cell);
342 
343  for(const SubcellOrdinal & subcell_index : subcell_indexes){
344 
345  const LocalOrdinal face = cell_to_faces_h(parent_cell, subcell_index);
346  const LocalOrdinal face_other_side = (face_to_cells_h(face,0) == parent_cell) ? 1 : 0;
347 
348  TEUCHOS_ASSERT(subcell_index == face_to_lidx_h(face, 1-face_other_side));
349 
350  const ParentOrdinal other_side_cell = face_to_cells_h(face, face_other_side);
351  const SubcellOrdinal other_side_subcell_index = face_to_lidx_h(face, face_other_side);
352 
353  faces.push_back(face_t(parent_cell, other_side_cell, subcell_index, other_side_subcell_index));
354 
355  if(other_side_cell >= parent_virtual_cell_offset){
356  virtual_parent_cells_set.insert(other_side_cell);
357  } else {
358  ghstd_parent_cells_set.insert(other_side_cell);
359  }
360  }
361  }
362  }
363 
364  std::vector<ParentOrdinal> all_cells;
365  all_cells.insert(all_cells.end(),owned_parent_cells_set.begin(),owned_parent_cells_set.end());
366  all_cells.insert(all_cells.end(),ghstd_parent_cells_set.begin(),ghstd_parent_cells_set.end());
367  all_cells.insert(all_cells.end(),virtual_parent_cells_set.begin(),virtual_parent_cells_set.end());
368 
369  sideset_info.num_ghstd_cells = ghstd_parent_cells_set.size();
370  sideset_info.num_virtual_cells = virtual_parent_cells_set.size();
371 
372  const LocalOrdinal num_real_cells = sideset_info.num_owned_cells + sideset_info.num_ghstd_cells;
373  const LocalOrdinal num_total_cells = num_real_cells + sideset_info.num_virtual_cells;
374  const LocalOrdinal num_nodes_per_cell = mesh_info.cell_nodes.extent(1);
375  const LocalOrdinal num_dims = mesh_info.cell_nodes.extent(2);
376 
377  // Copy local indexes, global indexes, and cell nodes to sideset info
378  {
379  sideset_info.global_cells = PHX::View<panzer::GlobalOrdinal*>("global_cells", num_total_cells);
380  sideset_info.local_cells = PHX::View<LocalOrdinal*>("local_cells", num_total_cells);
381  sideset_info.cell_nodes = PHX::View<double***>("cell_nodes", num_total_cells, num_nodes_per_cell, num_dims);
382  Kokkos::deep_copy(sideset_info.cell_nodes,0.);
383 
384  typename PHX::View<ParentOrdinal*>::HostMirror all_cells_h("all_cells_h", num_total_cells);
385  PHX::View<ParentOrdinal*> all_cells_d("all_cells_d", num_total_cells);
386  for(LocalOrdinal i=0; i<num_total_cells; ++i)
387  all_cells_h(i) = all_cells[i];
388  Kokkos::deep_copy(all_cells_d, all_cells_h);
389  Kokkos::parallel_for(num_total_cells, KOKKOS_LAMBDA (LocalOrdinal i) {
390  const ParentOrdinal parent_cell = all_cells_d(i);
391  sideset_info.local_cells(i) = mesh_info.local_cells(parent_cell);
392  sideset_info.global_cells(i) = mesh_info.global_cells(parent_cell);
393  for(LocalOrdinal j=0; j<num_nodes_per_cell; ++j)
394  for(LocalOrdinal k=0; k<num_dims; ++k)
395  sideset_info.cell_nodes(i,j,k) = mesh_info.cell_nodes(parent_cell,j,k);
396  });
397  }
398 
399  // Now we have to set the connectivity for the faces.
400 
401  const LocalOrdinal num_faces = faces.size();
402  const LocalOrdinal num_faces_per_cell = mesh_info.cell_to_faces.extent(1);
403 
404  sideset_info.face_to_cells = PHX::View<LocalOrdinal*[2]>("face_to_cells", num_faces);
405  sideset_info.face_to_lidx = PHX::View<LocalOrdinal*[2]>("face_to_lidx", num_faces);
406  sideset_info.cell_to_faces = PHX::View<LocalOrdinal**>("cell_to_faces", num_total_cells, num_faces_per_cell);
407  auto cell_to_faces_h = Kokkos::create_mirror_view(sideset_info.cell_to_faces);
408  auto face_to_cells_h = Kokkos::create_mirror_view(sideset_info.face_to_cells);
409  auto face_to_lidx_h = Kokkos::create_mirror_view(sideset_info.face_to_lidx);
410 
411  // Default the system with invalid cell index - this will be most of the entries
412  Kokkos::deep_copy(cell_to_faces_h, -1);
413 
414  // Lookup for sideset cell index given parent cell index
415  std::unordered_map<ParentOrdinal,ParentOrdinal> parent_to_sideset_lookup;
416  for(unsigned int i=0; i<all_cells.size(); ++i)
417  parent_to_sideset_lookup[all_cells[i]] = i;
418 
419  for(int face_index=0;face_index<num_faces;++face_index){
420  const face_t & face = faces[face_index];
421  const ParentOrdinal & parent_cell_0 = face.cell_0;
422  const ParentOrdinal & parent_cell_1 = face.cell_1;
423 
424  // Convert the parent cell index into a sideset cell index
425  const auto itr_0 = parent_to_sideset_lookup.find(parent_cell_0);
426  TEUCHOS_ASSERT(itr_0 != parent_to_sideset_lookup.end());
427  const ParentOrdinal sideset_cell_0 = itr_0->second;
428 
429  const auto itr_1 = parent_to_sideset_lookup.find(parent_cell_1);
430  TEUCHOS_ASSERT(itr_1 != parent_to_sideset_lookup.end());
431  const ParentOrdinal sideset_cell_1 = itr_1->second;
432 
433 // const ParentOrdinal sideset_cell_0 = std::distance(all_cells.begin(), std::find(all_cells.begin(), all_cells.end(), parent_cell_0));
434 // const ParentOrdinal sideset_cell_1 = std::distance(all_cells.begin(), std::find(all_cells.begin()+num_owned_cells, all_cells.end(), parent_cell_1));
435 
436  face_to_cells_h(face_index,0) = sideset_cell_0;
437  face_to_cells_h(face_index,1) = sideset_cell_1;
438 
439  face_to_lidx_h(face_index,0) = face.subcell_index_0;
440  face_to_lidx_h(face_index,1) = face.subcell_index_1;
441 
442  cell_to_faces_h(sideset_cell_0,face.subcell_index_0) = face_index;
443  cell_to_faces_h(sideset_cell_1,face.subcell_index_1) = face_index;
444 
445  }
446  Kokkos::deep_copy(sideset_info.cell_to_faces, cell_to_faces_h);
447  Kokkos::deep_copy(sideset_info.face_to_cells, face_to_cells_h);
448  Kokkos::deep_copy(sideset_info.face_to_lidx, face_to_lidx_h );
449 
450 }
451 
452 } // namespace
453 
456 {
457  TEUCHOS_FUNC_TIME_MONITOR_DIFF("panzer_stk::generateLocalMeshInfo",GenerateLocalMeshInfo);
458 
459  using Teuchos::RCP;
460  using Teuchos::rcp;
461 
462  //typedef Tpetra::CrsMatrix<int,panzer::LocalOrdinal,panzer::GlobalOrdinal> crs_type;
463  typedef Tpetra::Map<panzer::LocalOrdinal,panzer::GlobalOrdinal,panzer::TpetraNodeType> map_type;
464  typedef Tpetra::Import<panzer::LocalOrdinal,panzer::GlobalOrdinal,panzer::TpetraNodeType> import_type;
465  //typedef Tpetra::MultiVector<double,panzer::LocalOrdinal,panzer::GlobalOrdinal> mvec_type;
466  //typedef Tpetra::MultiVector<panzer::GlobalOrdinal,panzer::LocalOrdinal,panzer::GlobalOrdinal> ordmvec_type;
467 
468  auto mesh_info_rcp = Teuchos::rcp(new panzer::LocalMeshInfo);
469  auto & mesh_info = *mesh_info_rcp;
470 
471  // Make sure the STK interface is valid
473 
474  // This is required by some of the STK stuff
475  TEUCHOS_ASSERT(typeid(panzer::LocalOrdinal) == typeid(int));
476 
478 
479  // This horrible line of code is required since the connection manager only takes rcps of a mesh
480  RCP<const panzer_stk::STK_Interface> mesh_rcp = Teuchos::rcpFromRef(mesh);
481  // We're allowed to do this since the connection manager only exists in this scope... even though it is also an RCP...
482 
483  // extract topology handle
484  RCP<panzer::ConnManager> conn_rcp = rcp(new panzer_stk::STKConnManager(mesh_rcp));
485  panzer::ConnManager & conn = *conn_rcp;
486 
487  PHX::View<panzer::GlobalOrdinal*> owned_cells, ghost_cells, virtual_cells;
488  panzer::fillLocalCellIDs(comm, conn, owned_cells, ghost_cells, virtual_cells);
489 
490  // build cell maps
492 
493  RCP<map_type> owned_cell_map = rcp(new map_type(Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(),owned_cells,0,comm));
494  RCP<map_type> ghstd_cell_map = rcp(new map_type(Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(),ghost_cells,0,comm));
495 
496  // build importer: cell importer, owned to ghstd
497  RCP<import_type> cellimport_own2ghst = rcp(new import_type(owned_cell_map,ghstd_cell_map));
498 
499  // read all the nodes associated with these elements, get ghstd contributions
501 
502  // TODO: This all needs to be rewritten for when element blocks have different cell topologies
503  std::vector<std::string> element_block_names;
504  mesh.getElementBlockNames(element_block_names);
505 
506  const shards::CellTopology & cell_topology = *(mesh.getCellTopology(element_block_names[0]));
507 
508  const int space_dim = cell_topology.getDimension();
509  const int nodes_per_cell = cell_topology.getNodeCount();
510  const int faces_per_cell = cell_topology.getSubcellCount(space_dim-1);
511 
512  Kokkos::DynRankView<double,PHX::Device> owned_nodes("owned_nodes",owned_cells.extent(0),nodes_per_cell,space_dim);
513  {
514  std::vector<std::size_t> localCells(owned_cells.extent(0),Teuchos::OrdinalTraits<std::size_t>::invalid());
515  for(size_t i=0;i<localCells.size();i++)
516  localCells[i] = i;
517  mesh.getElementNodesNoResize(localCells,owned_nodes);
518  }
519 
520  // this builds a ghstd node array
521  Kokkos::DynRankView<double,PHX::Device> ghstd_nodes = buildGhostedNodes(*cellimport_own2ghst,owned_nodes);
522 
523  // build edge to cell neighbor mapping
525 
526  auto owned_cells_h = Kokkos::create_mirror_view(owned_cells);
527  auto ghost_cells_h = Kokkos::create_mirror_view(ghost_cells);
528  Kokkos::deep_copy(owned_cells_h, owned_cells);
529  Kokkos::deep_copy(ghost_cells_h, ghost_cells);
530  std::unordered_map<panzer::GlobalOrdinal,int> global_to_local;
531  global_to_local[-1] = -1; // this is the "no neighbor" flag
532  for(size_t i=0;i<owned_cells.extent(0);i++)
533  global_to_local[owned_cells_h(i)] = i;
534  for(size_t i=0;i<ghost_cells.extent(0);i++)
535  global_to_local[ghost_cells_h(i)] = i+Teuchos::as<int>(owned_cells.extent(0));
536 
537  // this class comes from Mini-PIC and Matt B
539  faceToElement->initialize(conn, comm);
540  auto elems_by_face = faceToElement->getFaceToElementsMap();
541  auto face_to_lidx = faceToElement->getFaceToCellLocalIdxMap();
542 
543  // We also need to consider faces that connect to cells that do not exist, but are needed for boundary conditions
544  // We dub them virtual cell since there should be no geometry associated with them, or topology really
545  // They exist only for datastorage so that they are consistent with 'real' cells from an algorithm perspective
546 
547  // Each virtual face (face linked to a '-1' cell) requires a virtual cell (i.e. turn the '-1' into a virtual cell)
548  // Virtual cells are those that do not exist but are connected to an owned cell
549  // Note - in the future, ghosted cells will also need to connect to virtual cells at boundary conditions, but for the moment we will ignore this.
550 
551  // Iterate over all faces and identify the faces connected to a potential virtual cell
552 
553  const panzer::LocalOrdinal num_owned_cells = owned_cells.extent(0);
554  const panzer::LocalOrdinal num_ghstd_cells = ghost_cells.extent(0);
555  const panzer::LocalOrdinal num_virtual_cells = virtual_cells.extent(0);
556 
557  // total cells and faces include owned, ghosted, and virtual
558  const panzer::LocalOrdinal num_real_cells = num_owned_cells + num_ghstd_cells;
559  const panzer::LocalOrdinal num_total_cells = num_real_cells + num_virtual_cells;
560  const panzer::LocalOrdinal num_total_faces = elems_by_face.extent(0);
561 
562  // Lookup cells connected to a face
563  PHX::View<panzer::LocalOrdinal*[2]> face_to_cells("face_to_cells",num_total_faces);
564 
565  // Lookup local face indexes given cell and left/right state (0/1)
566  PHX::View<panzer::LocalOrdinal*[2]> face_to_localidx("face_to_localidx",num_total_faces);
567 
568  // Lookup face index given a cell and local face index
569  PHX::View<panzer::LocalOrdinal**> cell_to_face("cell_to_face",num_total_cells,faces_per_cell);
570 
571  // Transfer information from 'faceToElement' datasets to local arrays
572  {
573  PANZER_FUNC_TIME_MONITOR_DIFF("Transer faceToElement to local",TransferFaceToElementLocal);
574 
575  int virtual_cell_index = num_real_cells;
576  auto elems_by_face_h = Kokkos::create_mirror_view(elems_by_face);
577  auto face_to_lidx_h = Kokkos::create_mirror_view(face_to_lidx);
578  auto face_to_cells_h = Kokkos::create_mirror_view(face_to_cells);
579  auto face_to_localidx_h = Kokkos::create_mirror_view(face_to_localidx);
580  auto cell_to_face_h = Kokkos::create_mirror_view(cell_to_face);
581  Kokkos::deep_copy(elems_by_face_h, elems_by_face);
582  Kokkos::deep_copy(face_to_lidx_h, face_to_lidx);
583  // initialize with negative one cells that are not associated with a face
584  Kokkos::deep_copy(cell_to_face_h, -1);
585  for(size_t f=0;f<elems_by_face.extent(0);f++) {
586 
587  const panzer::GlobalOrdinal global_c0 = elems_by_face_h(f,0);
588  const panzer::GlobalOrdinal global_c1 = elems_by_face_h(f,1);
589 
590  // make sure that no bonus cells get in here
591  TEUCHOS_ASSERT(global_to_local.find(global_c0)!=global_to_local.end());
592  TEUCHOS_ASSERT(global_to_local.find(global_c1)!=global_to_local.end());
593 
594  auto c0 = global_to_local[global_c0];
595  auto lidx0 = face_to_lidx_h(f,0);
596  auto c1 = global_to_local[global_c1];
597  auto lidx1 = face_to_lidx_h(f,1);
598 
599  // Test for virtual cells
600 
601  // Left cell
602  if(c0 < 0){
603  // Virtual cell - create it!
604  c0 = virtual_cell_index++;
605 
606  // We need the subcell_index to line up between real and virtual cell
607  // This way the face has the same geometry... though the face normal
608  // will point in the wrong direction
609  lidx0 = lidx1;
610  }
611  cell_to_face_h(c0,lidx0) = f;
612 
613 
614  // Right cell
615  if(c1 < 0){
616  // Virtual cell - create it!
617  c1 = virtual_cell_index++;
618 
619  // We need the subcell_index to line up between real and virtual cell
620  // This way the face has the same geometry... though the face normal
621  // will point in the wrong direction
622  lidx1 = lidx0;
623  }
624  cell_to_face_h(c1,lidx1) = f;
625 
626  // Faces point from low cell index to high cell index
627  if(c0<c1){
628  face_to_cells_h(f,0) = c0;
629  face_to_localidx_h(f,0) = lidx0;
630  face_to_cells_h(f,1) = c1;
631  face_to_localidx_h(f,1) = lidx1;
632  } else {
633  face_to_cells_h(f,0) = c1;
634  face_to_localidx_h(f,0) = lidx1;
635  face_to_cells_h(f,1) = c0;
636  face_to_localidx_h(f,1) = lidx0;
637  }
638 
639  // We should avoid having two virtual cells linked together.
640  TEUCHOS_ASSERT(c0<num_real_cells or c1<num_real_cells)
641 
642  }
643  Kokkos::deep_copy(face_to_cells, face_to_cells_h);
644  Kokkos::deep_copy(face_to_localidx, face_to_localidx_h);
645  Kokkos::deep_copy(cell_to_face, cell_to_face_h);
646  }
647  // at this point all the data structures have been built, so now we can "do" DG.
648  // There are two of everything, an "owned" data structured corresponding to "owned"
649  // cells. And a "ghstd" data structure corresponding to ghosted cells
651  {
652  PANZER_FUNC_TIME_MONITOR_DIFF("Assign Indices",AssignIndices);
653  mesh_info.cell_to_faces = cell_to_face;
654  mesh_info.face_to_cells = face_to_cells; // faces
655  mesh_info.face_to_lidx = face_to_localidx;
656  mesh_info.subcell_dimension = space_dim;
657  mesh_info.subcell_index = -1;
658  mesh_info.has_connectivity = true;
659 
660  mesh_info.num_owned_cells = owned_cells.extent(0);
661  mesh_info.num_ghstd_cells = ghost_cells.extent(0);
662  mesh_info.num_virtual_cells = virtual_cells.extent(0);
663 
664  mesh_info.global_cells = PHX::View<panzer::GlobalOrdinal*>("global_cell_indices",num_total_cells);
665  mesh_info.local_cells = PHX::View<panzer::LocalOrdinal*>("local_cell_indices",num_total_cells);
666 
667  Kokkos::parallel_for(num_owned_cells,KOKKOS_LAMBDA (int i) {
668  mesh_info.global_cells(i) = owned_cells(i);
669  mesh_info.local_cells(i) = i;
670  });
671 
672  Kokkos::parallel_for(num_ghstd_cells,KOKKOS_LAMBDA (int i) {
673  mesh_info.global_cells(i+num_owned_cells) = ghost_cells(i);
674  mesh_info.local_cells(i+num_owned_cells) = i+num_owned_cells;
675  });
676 
677  Kokkos::parallel_for(num_virtual_cells,KOKKOS_LAMBDA (int i) {
678  mesh_info.global_cells(i+num_real_cells) = virtual_cells(i);
679  mesh_info.local_cells(i+num_real_cells) = i+num_real_cells;
680  });
681 
682  mesh_info.cell_nodes = PHX::View<double***>("cell_nodes",num_total_cells,nodes_per_cell,space_dim);
683 
684  // Initialize coordinates to zero
685  Kokkos::deep_copy(mesh_info.cell_nodes, 0.);
686 
687  Kokkos::parallel_for(num_owned_cells,KOKKOS_LAMBDA (int i) {
688  for(int j=0;j<nodes_per_cell;++j){
689  for(int k=0;k<space_dim;++k){
690  mesh_info.cell_nodes(i,j,k) = owned_nodes(i,j,k);
691  }
692  }
693  });
694 
695  Kokkos::parallel_for(num_ghstd_cells,KOKKOS_LAMBDA (int i) {
696  for(int j=0;j<nodes_per_cell;++j){
697  for(int k=0;k<space_dim;++k){
698  mesh_info.cell_nodes(i+num_owned_cells,j,k) = ghstd_nodes(i,j,k);
699  }
700  }
701  });
702 
703  // This will backfire at some point, but we're going to make the virtual cell have the same geometry as the cell it interfaces with
704  // This way we can define a virtual cell geometry without extruding the face outside of the domain
705  // TODO BWR Certainly, this is an issue for curved meshes
706  {
707  PANZER_FUNC_TIME_MONITOR_DIFF("Assign geometry traits",AssignGeometryTraits);
708  Kokkos::parallel_for(num_virtual_cells,KOKKOS_LAMBDA (int i) {
709  const panzer::LocalOrdinal virtual_cell = i+num_real_cells;
710  for(int local_face=0; local_face<faces_per_cell; ++local_face){
711  const panzer::LocalOrdinal face = cell_to_face(virtual_cell, local_face);
712  if(face >= 0){
713  const panzer::LocalOrdinal other_side = (face_to_cells(face, 0) == virtual_cell) ? 1 : 0;
714  const panzer::LocalOrdinal real_cell = face_to_cells(face,other_side);
715  for(int j=0;j<nodes_per_cell;++j){
716  for(int k=0;k<space_dim;++k){
717  mesh_info.cell_nodes(virtual_cell,j,k) = mesh_info.cell_nodes(real_cell,j,k);
718  }
719  }
720  break;
721  }
722  }
723  });
724 
725  }
726  }
727 
728  // Setup element blocks and sidesets
729  std::vector<std::string> sideset_names;
730  mesh.getSidesetNames(sideset_names);
731 
732  for(const std::string & element_block_name : element_block_names){
733  PANZER_FUNC_TIME_MONITOR_DIFF("Set up setupLocalMeshBlockInfo",SetupLocalMeshBlockInfo);
734  panzer::LocalMeshBlockInfo & block_info = mesh_info.element_blocks[element_block_name];
735  setupLocalMeshBlockInfo(mesh, conn, mesh_info, element_block_name, block_info);
736  block_info.subcell_dimension = space_dim;
737  block_info.subcell_index = -1;
738  block_info.has_connectivity = true;
739 
740  // Setup sidesets
741  for(const std::string & sideset_name : sideset_names){
742  PANZER_FUNC_TIME_MONITOR_DIFF("Setup LocalMeshSidesetInfo",SetupLocalMeshSidesetInfo);
743  panzer::LocalMeshSidesetInfo & sideset_info = mesh_info.sidesets[element_block_name][sideset_name];
744  setupLocalMeshSidesetInfo(mesh, conn, mesh_info, element_block_name, sideset_name, sideset_info);
745  sideset_info.subcell_dimension = space_dim;
746  sideset_info.subcell_index = -1;
747  sideset_info.has_connectivity = true;
748  }
749 
750  }
751 
752  return mesh_info_rcp;
753 
754 }
755 
756 }
void getSidesetNames(std::vector< std::string > &name) const
void getElementNodesNoResize(const std::vector< std::size_t > &localIds, ArrayT &nodes) const
std::map< std::string, std::map< std::string, LocalMeshSidesetInfo > > sidesets
void getElementBlockNames(std::vector< std::string > &names) const
Teuchos::RCP< panzer::LocalMeshInfo > generateLocalMeshInfo(const panzer_stk::STK_Interface &mesh)
Create a structure containing information about the local portion of a given element block...
Teuchos::RCP< const shards::CellTopology > cell_topology
PHX::View< panzer::LocalOrdinal * > local_cells
void fillLocalCellIDs(const Teuchos::RCP< const Teuchos::Comm< int >> &comm, panzer::ConnManager &conn, PHX::View< panzer::GlobalOrdinal * > &owned_cells, PHX::View< panzer::GlobalOrdinal * > &ghost_cells, PHX::View< panzer::GlobalOrdinal * > &virtual_cells)
Get the owned, ghost and virtual global cell ids for this process.
PHX::View< panzer::LocalOrdinal *[2]> face_to_cells
panzer::LocalOrdinal num_owned_cells
std::size_t elementLocalId(stk::mesh::Entity elmt) const
panzer::LocalOrdinal num_virtual_cells
Teuchos::RCP< const shards::CellTopology > cell_topology
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
PHX::View< panzer::LocalOrdinal *[2]> face_to_lidx
Pure virtual base class for supplying mesh connectivity information to the DOF Manager.
panzer::LocalOrdinal num_ghstd_cells
std::map< std::string, LocalMeshBlockInfo > element_blocks
bool isInitialized() const
Has initialize been called on this mesh object?
void getAllSides(const std::string &sideName, std::vector< stk::mesh::Entity > &sides) const
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
get the comm associated with this mesh
PHX::View< double *** > cell_nodes
#define TEUCHOS_ASSERT(assertion_test)
void getSideElementCascade(const panzer_stk::STK_Interface &mesh, const std::string &blockId, const std::vector< stk::mesh::Entity > &sides, std::vector< std::size_t > &localSubcellDim, std::vector< std::size_t > &localSubcellIds, std::vector< stk::mesh::Entity > &elements)
void setupSubLocalMeshInfo(const panzer::LocalMeshInfoBase &parent_info, const std::vector< panzer::LocalOrdinal > &owned_parent_cells, panzer::LocalMeshInfoBase &sub_info)
PHX::View< panzer::GlobalOrdinal * > global_cells
virtual void buildConnectivity(const FieldPattern &fp)=0
Teuchos::RCP< const shards::CellTopology > getCellTopology(const std::string &eBlock) const
#define TEUCHOS_FUNC_TIME_MONITOR_DIFF(FUNCNAME, DIFF)
virtual std::string getBlockId(LocalOrdinal localElmtId) const =0
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
PHX::View< panzer::LocalOrdinal ** > cell_to_faces