30 #include "Phalanx_KokkosDeviceTypes.hpp"
32 #include "Teuchos_Assert.hpp"
35 #include "Tpetra_Import.hpp"
40 #include <unordered_set>
52 Kokkos::DynRankView<double,PHX::Device>
53 buildGhostedNodes(
const Tpetra::Import<int,panzer::GlobalOrdinal,panzer::TpetraNodeType> & importer,
54 Kokkos::DynRankView<const double,PHX::Device> owned_nodes)
59 typedef Tpetra::MultiVector<double,int,panzer::GlobalOrdinal,panzer::TpetraNodeType> mvec_type;
61 size_t owned_cell_cnt = importer.getSourceMap()->getLocalNumElements();
62 size_t ghstd_cell_cnt = importer.getTargetMap()->getLocalNumElements();
63 int nodes_per_cell = owned_nodes.extent(1);
64 int space_dim = owned_nodes.extent(2);
69 RCP<mvec_type> owned_nodes_mv =
rcp(
new mvec_type(importer.getSourceMap(),nodes_per_cell*space_dim));
70 RCP<mvec_type> ghstd_nodes_mv =
rcp(
new mvec_type(importer.getTargetMap(),nodes_per_cell*space_dim));
73 auto owned_nodes_view = owned_nodes_mv->getLocalViewDevice(Tpetra::Access::OverwriteAll);
74 Kokkos::parallel_for(owned_cell_cnt, KOKKOS_LAMBDA (
size_t i) {
76 for(
int j=0;j<nodes_per_cell;j++)
77 for(
int k=0;k<space_dim;k++,l++)
78 owned_nodes_view(i,l) = owned_nodes(i,j,k);
83 ghstd_nodes_mv->doImport(*owned_nodes_mv,importer,Tpetra::INSERT);
86 Kokkos::DynRankView<double,PHX::Device> ghstd_nodes(
"ghstd_nodes",ghstd_cell_cnt,nodes_per_cell,space_dim);
88 auto ghstd_nodes_view = ghstd_nodes_mv->getLocalViewDevice(Tpetra::Access::ReadOnly);
89 Kokkos::parallel_for(ghstd_cell_cnt, KOKKOS_LAMBDA (
size_t i) {
91 for(
int j=0;j<nodes_per_cell;j++)
92 for(
int k=0;k<space_dim;k++,l++)
93 ghstd_nodes(i,j,k) = ghstd_nodes_view(i,l);
104 const std::string & element_block_name,
115 const shards::CellTopology & topology = *(mesh.
getCellTopology(element_block_name));
117 if(topology.getDimension() == 1){
119 }
else if(topology.getDimension() == 2){
121 }
else if(topology.getDimension() == 3){
126 PANZER_FUNC_TIME_MONITOR(
"Build connectivity");
131 std::vector<panzer::LocalOrdinal> owned_block_cells;
132 auto local_cells_h = Kokkos::create_mirror_view(mesh_info.
local_cells);
133 Kokkos::deep_copy(local_cells_h, mesh_info.
local_cells);
134 for(
int parent_owned_cell=0;parent_owned_cell<num_parent_owned_cells;++parent_owned_cell){
135 const panzer::LocalOrdinal local_cell = local_cells_h(parent_owned_cell);
136 const bool is_in_block = conn.
getBlockId(local_cell) == element_block_name;
139 owned_block_cells.push_back(parent_owned_cell);
144 if ( owned_block_cells.size() == 0 )
150 PANZER_FUNC_TIME_MONITOR(
"panzer::partitioning_utilities::setupSubLocalMeshInfo");
160 const std::string & element_block_name,
161 const std::string & sideset_name,
166 using LocalOrdinal = panzer::LocalOrdinal;
167 using ParentOrdinal = int;
168 using SubcellOrdinal = short;
174 std::vector<stk::mesh::Entity> side_entities;
180 mesh.
getAllSides(sideset_name, element_block_name, side_entities);
182 }
catch(STK_Interface::SidesetException & e) {
183 std::stringstream ss;
184 std::vector<std::string> sideset_names;
188 ss << e.what() <<
"\nChoose existing sideset:\n";
189 for(
const auto & optional_sideset_name : sideset_names){
190 ss <<
"\t\"" << optional_sideset_name <<
"\"\n";
195 }
catch(STK_Interface::ElementBlockException & e) {
196 std::stringstream ss;
197 std::vector<std::string> element_block_names;
201 ss << e.what() <<
"\nChoose existing element block:\n";
202 for(
const auto & optional_element_block_name : element_block_names){
203 ss <<
"\t\"" << optional_element_block_name <<
"\"\n";
208 }
catch(std::logic_error & e) {
209 std::stringstream ss;
210 ss << e.what() <<
"\nUnrecognized logic error.\n";
217 std::map<ParentOrdinal,std::vector<SubcellOrdinal> > owned_parent_cell_to_subcell_indexes;
221 const size_t face_subcell_dimension =
static_cast<size_t>(mesh.
getCellTopology(element_block_name)->getDimension()-1);
231 std::vector<stk::mesh::Entity> elements;
232 std::vector<size_t> subcell_indexes;
233 std::vector<size_t> subcell_dimensions;
235 const size_t num_elements = subcell_dimensions.size();
238 std::unordered_map<LocalOrdinal,ParentOrdinal> local_to_parent_lookup;
239 auto local_cells_h = Kokkos::create_mirror_view(mesh_info.
local_cells);
240 Kokkos::deep_copy(local_cells_h, mesh_info.
local_cells);
241 for(ParentOrdinal parent_index=0; parent_index<static_cast<ParentOrdinal>(mesh_info.
local_cells.extent(0)); ++parent_index)
242 local_to_parent_lookup[local_cells_h(parent_index)] = parent_index;
246 for(
size_t element_index=0; element_index<num_elements; ++element_index) {
247 const size_t subcell_dimension = subcell_dimensions[element_index];
250 if(subcell_dimension == face_subcell_dimension){
251 const SubcellOrdinal subcell_index =
static_cast<SubcellOrdinal
>(subcell_indexes[element_index]);
252 const LocalOrdinal element_local_index =
static_cast<LocalOrdinal
>(mesh.
elementLocalId(elements[element_index]));
255 const auto itr = local_to_parent_lookup.find(element_local_index);
257 const ParentOrdinal element_parent_index = itr->second;
259 owned_parent_cell_to_subcell_indexes[element_parent_index].push_back(subcell_index);
266 const panzer::LocalOrdinal num_owned_cells = owned_parent_cell_to_subcell_indexes.size();
275 face_t(
const ParentOrdinal c0,
276 const ParentOrdinal c1,
277 const SubcellOrdinal sc0,
278 const SubcellOrdinal sc1)
285 ParentOrdinal cell_0;
286 ParentOrdinal cell_1;
287 SubcellOrdinal subcell_index_0;
288 SubcellOrdinal subcell_index_1;
293 std::unordered_set<panzer::LocalOrdinal> owned_parent_cells_set, ghstd_parent_cells_set, virtual_parent_cells_set;
294 std::vector<face_t> faces;
296 auto cell_to_faces_h = Kokkos::create_mirror_view(mesh_info.
cell_to_faces);
297 auto face_to_cells_h = Kokkos::create_mirror_view(mesh_info.
face_to_cells);
298 auto face_to_lidx_h = Kokkos::create_mirror_view(mesh_info.
face_to_lidx);
301 Kokkos::deep_copy(face_to_lidx_h, mesh_info.
face_to_lidx);
303 for(
const auto & local_cell_index_pair : owned_parent_cell_to_subcell_indexes){
305 const ParentOrdinal parent_cell = local_cell_index_pair.first;
306 const auto & subcell_indexes = local_cell_index_pair.second;
308 owned_parent_cells_set.insert(parent_cell);
310 for(
const SubcellOrdinal & subcell_index : subcell_indexes){
312 const LocalOrdinal face = cell_to_faces_h(parent_cell, subcell_index);
313 const LocalOrdinal face_other_side = (face_to_cells_h(face,0) == parent_cell) ? 1 : 0;
315 TEUCHOS_ASSERT(subcell_index == face_to_lidx_h(face, 1-face_other_side));
317 const ParentOrdinal other_side_cell = face_to_cells_h(face, face_other_side);
318 const SubcellOrdinal other_side_subcell_index = face_to_lidx_h(face, face_other_side);
320 faces.push_back(face_t(parent_cell, other_side_cell, subcell_index, other_side_subcell_index));
322 if(other_side_cell >= parent_virtual_cell_offset){
323 virtual_parent_cells_set.insert(other_side_cell);
325 ghstd_parent_cells_set.insert(other_side_cell);
331 std::vector<ParentOrdinal> all_cells;
332 all_cells.insert(all_cells.end(),owned_parent_cells_set.begin(),owned_parent_cells_set.end());
333 all_cells.insert(all_cells.end(),ghstd_parent_cells_set.begin(),ghstd_parent_cells_set.end());
334 all_cells.insert(all_cells.end(),virtual_parent_cells_set.begin(),virtual_parent_cells_set.end());
340 const LocalOrdinal num_total_cells = num_real_cells + sideset_info.
num_virtual_cells;
341 const LocalOrdinal num_nodes_per_cell = mesh_info.
cell_nodes.extent(1);
342 const LocalOrdinal num_dims = mesh_info.
cell_nodes.extent(2);
346 sideset_info.
global_cells = PHX::View<panzer::GlobalOrdinal*>(
"global_cells", num_total_cells);
347 sideset_info.
local_cells = PHX::View<LocalOrdinal*>(
"local_cells", num_total_cells);
348 sideset_info.
cell_nodes = PHX::View<double***>(
"cell_nodes", num_total_cells, num_nodes_per_cell, num_dims);
349 Kokkos::deep_copy(sideset_info.
cell_nodes,0.);
351 typename PHX::View<ParentOrdinal*>::HostMirror all_cells_h(
"all_cells_h", num_total_cells);
352 PHX::View<ParentOrdinal*> all_cells_d(
"all_cells_d", num_total_cells);
353 for(LocalOrdinal i=0; i<num_total_cells; ++i)
354 all_cells_h(i) = all_cells[i];
355 Kokkos::deep_copy(all_cells_d, all_cells_h);
356 Kokkos::parallel_for(num_total_cells, KOKKOS_LAMBDA (LocalOrdinal i) {
357 const ParentOrdinal parent_cell = all_cells_d(i);
360 for(LocalOrdinal j=0; j<num_nodes_per_cell; ++j)
361 for(LocalOrdinal k=0; k<num_dims; ++k)
368 const LocalOrdinal num_faces = faces.size();
369 const LocalOrdinal num_faces_per_cell = mesh_info.
cell_to_faces.extent(1);
371 sideset_info.
face_to_cells = PHX::View<LocalOrdinal*[2]>(
"face_to_cells", num_faces);
372 sideset_info.
face_to_lidx = PHX::View<LocalOrdinal*[2]>(
"face_to_lidx", num_faces);
373 sideset_info.
cell_to_faces = PHX::View<LocalOrdinal**>(
"cell_to_faces", num_total_cells, num_faces_per_cell);
374 auto cell_to_faces_h = Kokkos::create_mirror_view(sideset_info.
cell_to_faces);
375 auto face_to_cells_h = Kokkos::create_mirror_view(sideset_info.
face_to_cells);
376 auto face_to_lidx_h = Kokkos::create_mirror_view(sideset_info.
face_to_lidx);
379 Kokkos::deep_copy(cell_to_faces_h, -1);
382 std::unordered_map<ParentOrdinal,ParentOrdinal> parent_to_sideset_lookup;
383 for(
unsigned int i=0; i<all_cells.size(); ++i)
384 parent_to_sideset_lookup[all_cells[i]] = i;
386 for(
int face_index=0;face_index<num_faces;++face_index){
387 const face_t & face = faces[face_index];
388 const ParentOrdinal & parent_cell_0 = face.cell_0;
389 const ParentOrdinal & parent_cell_1 = face.cell_1;
392 const auto itr_0 = parent_to_sideset_lookup.find(parent_cell_0);
394 const ParentOrdinal sideset_cell_0 = itr_0->second;
396 const auto itr_1 = parent_to_sideset_lookup.find(parent_cell_1);
398 const ParentOrdinal sideset_cell_1 = itr_1->second;
403 face_to_cells_h(face_index,0) = sideset_cell_0;
404 face_to_cells_h(face_index,1) = sideset_cell_1;
406 face_to_lidx_h(face_index,0) = face.subcell_index_0;
407 face_to_lidx_h(face_index,1) = face.subcell_index_1;
409 cell_to_faces_h(sideset_cell_0,face.subcell_index_0) = face_index;
410 cell_to_faces_h(sideset_cell_1,face.subcell_index_1) = face_index;
413 Kokkos::deep_copy(sideset_info.
cell_to_faces, cell_to_faces_h);
414 Kokkos::deep_copy(sideset_info.
face_to_cells, face_to_cells_h);
415 Kokkos::deep_copy(sideset_info.
face_to_lidx, face_to_lidx_h );
430 typedef Tpetra::Map<panzer::LocalOrdinal,panzer::GlobalOrdinal,panzer::TpetraNodeType> map_type;
431 typedef Tpetra::Import<panzer::LocalOrdinal,panzer::GlobalOrdinal,panzer::TpetraNodeType> import_type;
436 auto & mesh_info = *mesh_info_rcp;
454 PHX::View<panzer::GlobalOrdinal*> owned_cells, ghost_cells, virtual_cells;
464 RCP<import_type> cellimport_own2ghst =
rcp(
new import_type(owned_cell_map,ghstd_cell_map));
470 std::vector<std::string> element_block_names;
473 const shards::CellTopology & cell_topology = *(mesh.
getCellTopology(element_block_names[0]));
475 const int space_dim = cell_topology.getDimension();
476 const int nodes_per_cell = cell_topology.getNodeCount();
477 const int faces_per_cell = cell_topology.getSubcellCount(space_dim-1);
479 Kokkos::DynRankView<double,PHX::Device> owned_nodes(
"owned_nodes",owned_cells.extent(0),nodes_per_cell,space_dim);
482 for(
size_t i=0;i<localCells.size();i++)
488 Kokkos::DynRankView<double,PHX::Device> ghstd_nodes = buildGhostedNodes(*cellimport_own2ghst,owned_nodes);
493 auto owned_cells_h = Kokkos::create_mirror_view(owned_cells);
494 auto ghost_cells_h = Kokkos::create_mirror_view(ghost_cells);
495 Kokkos::deep_copy(owned_cells_h, owned_cells);
496 Kokkos::deep_copy(ghost_cells_h, ghost_cells);
497 std::unordered_map<panzer::GlobalOrdinal,int> global_to_local;
498 global_to_local[-1] = -1;
499 for(
size_t i=0;i<owned_cells.extent(0);i++)
500 global_to_local[owned_cells_h(i)] = i;
501 for(
size_t i=0;i<ghost_cells.extent(0);i++)
502 global_to_local[ghost_cells_h(i)] = i+Teuchos::as<int>(owned_cells.extent(0));
506 faceToElement->initialize(conn, comm);
507 auto elems_by_face = faceToElement->getFaceToElementsMap();
508 auto face_to_lidx = faceToElement->getFaceToCellLocalIdxMap();
520 const panzer::LocalOrdinal num_owned_cells = owned_cells.extent(0);
521 const panzer::LocalOrdinal num_ghstd_cells = ghost_cells.extent(0);
522 const panzer::LocalOrdinal num_virtual_cells = virtual_cells.extent(0);
525 const panzer::LocalOrdinal num_real_cells = num_owned_cells + num_ghstd_cells;
526 const panzer::LocalOrdinal num_total_cells = num_real_cells + num_virtual_cells;
527 const panzer::LocalOrdinal num_total_faces = elems_by_face.extent(0);
530 PHX::View<panzer::LocalOrdinal*[2]> face_to_cells(
"face_to_cells",num_total_faces);
533 PHX::View<panzer::LocalOrdinal*[2]> face_to_localidx(
"face_to_localidx",num_total_faces);
536 PHX::View<panzer::LocalOrdinal**> cell_to_face(
"cell_to_face",num_total_cells,faces_per_cell);
540 PANZER_FUNC_TIME_MONITOR_DIFF(
"Transer faceToElement to local",TransferFaceToElementLocal);
542 int virtual_cell_index = num_real_cells;
543 auto elems_by_face_h = Kokkos::create_mirror_view(elems_by_face);
544 auto face_to_lidx_h = Kokkos::create_mirror_view(face_to_lidx);
545 auto face_to_cells_h = Kokkos::create_mirror_view(face_to_cells);
546 auto face_to_localidx_h = Kokkos::create_mirror_view(face_to_localidx);
547 auto cell_to_face_h = Kokkos::create_mirror_view(cell_to_face);
548 Kokkos::deep_copy(elems_by_face_h, elems_by_face);
549 Kokkos::deep_copy(face_to_lidx_h, face_to_lidx);
551 Kokkos::deep_copy(cell_to_face_h, -1);
552 for(
size_t f=0;f<elems_by_face.extent(0);f++) {
554 const panzer::GlobalOrdinal global_c0 = elems_by_face_h(f,0);
555 const panzer::GlobalOrdinal global_c1 = elems_by_face_h(f,1);
558 TEUCHOS_ASSERT(global_to_local.find(global_c0)!=global_to_local.end());
559 TEUCHOS_ASSERT(global_to_local.find(global_c1)!=global_to_local.end());
561 auto c0 = global_to_local[global_c0];
562 auto lidx0 = face_to_lidx_h(f,0);
563 auto c1 = global_to_local[global_c1];
564 auto lidx1 = face_to_lidx_h(f,1);
571 c0 = virtual_cell_index++;
578 cell_to_face_h(c0,lidx0) = f;
584 c1 = virtual_cell_index++;
591 cell_to_face_h(c1,lidx1) = f;
595 face_to_cells_h(f,0) = c0;
596 face_to_localidx_h(f,0) = lidx0;
597 face_to_cells_h(f,1) = c1;
598 face_to_localidx_h(f,1) = lidx1;
600 face_to_cells_h(f,0) = c1;
601 face_to_localidx_h(f,0) = lidx1;
602 face_to_cells_h(f,1) = c0;
603 face_to_localidx_h(f,1) = lidx0;
610 Kokkos::deep_copy(face_to_cells, face_to_cells_h);
611 Kokkos::deep_copy(face_to_localidx, face_to_localidx_h);
612 Kokkos::deep_copy(cell_to_face, cell_to_face_h);
619 PANZER_FUNC_TIME_MONITOR_DIFF(
"Assign Indices",AssignIndices);
631 mesh_info.
global_cells = PHX::View<panzer::GlobalOrdinal*>(
"global_cell_indices",num_total_cells);
632 mesh_info.
local_cells = PHX::View<panzer::LocalOrdinal*>(
"local_cell_indices",num_total_cells);
634 Kokkos::parallel_for(num_owned_cells,KOKKOS_LAMBDA (
int i) {
639 Kokkos::parallel_for(num_ghstd_cells,KOKKOS_LAMBDA (
int i) {
640 mesh_info.
global_cells(i+num_owned_cells) = ghost_cells(i);
641 mesh_info.
local_cells(i+num_owned_cells) = i+num_owned_cells;
644 Kokkos::parallel_for(num_virtual_cells,KOKKOS_LAMBDA (
int i) {
645 mesh_info.
global_cells(i+num_real_cells) = virtual_cells(i);
646 mesh_info.
local_cells(i+num_real_cells) = i+num_real_cells;
649 mesh_info.
cell_nodes = PHX::View<double***>(
"cell_nodes",num_total_cells,nodes_per_cell,space_dim);
654 Kokkos::parallel_for(num_owned_cells,KOKKOS_LAMBDA (
int i) {
655 for(
int j=0;j<nodes_per_cell;++j){
656 for(
int k=0;k<space_dim;++k){
657 mesh_info.
cell_nodes(i,j,k) = owned_nodes(i,j,k);
662 Kokkos::parallel_for(num_ghstd_cells,KOKKOS_LAMBDA (
int i) {
663 for(
int j=0;j<nodes_per_cell;++j){
664 for(
int k=0;k<space_dim;++k){
665 mesh_info.
cell_nodes(i+num_owned_cells,j,k) = ghstd_nodes(i,j,k);
674 PANZER_FUNC_TIME_MONITOR_DIFF(
"Assign geometry traits",AssignGeometryTraits);
675 Kokkos::parallel_for(num_virtual_cells,KOKKOS_LAMBDA (
int i) {
676 const panzer::LocalOrdinal virtual_cell = i+num_real_cells;
677 for(
int local_face=0; local_face<faces_per_cell; ++local_face){
678 const panzer::LocalOrdinal face = cell_to_face(virtual_cell, local_face);
680 const panzer::LocalOrdinal other_side = (face_to_cells(face, 0) == virtual_cell) ? 1 : 0;
681 const panzer::LocalOrdinal real_cell = face_to_cells(face,other_side);
682 for(
int j=0;j<nodes_per_cell;++j){
683 for(
int k=0;k<space_dim;++k){
696 std::vector<std::string> sideset_names;
699 for(
const std::string & element_block_name : element_block_names){
700 PANZER_FUNC_TIME_MONITOR_DIFF(
"Set up setupLocalMeshBlockInfo",SetupLocalMeshBlockInfo);
702 setupLocalMeshBlockInfo(mesh, conn, mesh_info, element_block_name, block_info);
708 for(
const std::string & sideset_name : sideset_names){
709 PANZER_FUNC_TIME_MONITOR_DIFF(
"Setup LocalMeshSidesetInfo",SetupLocalMeshSidesetInfo);
711 setupLocalMeshSidesetInfo(mesh, conn, mesh_info, element_block_name, sideset_name, sideset_info);
719 return mesh_info_rcp;
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
std::string element_block_name
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
std::string element_block_name
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