13 #include <Teuchos_RCPStdSharedPtrConversions.hpp>
14 #include <PanzerAdaptersSTK_config.hpp>
19 #ifdef PANZER_HAVE_IOSS
21 #include <Ionit_Initializer.h>
22 #include <Ioss_ElementBlock.h>
23 #include <Ioss_EdgeBlock.h>
24 #include <Ioss_FaceBlock.h>
25 #include <Ioss_Region.h>
26 #include <stk_mesh/base/GetBuckets.hpp>
27 #include <stk_io/StkMeshIoBroker.hpp>
28 #include <stk_io/IossBridge.hpp>
29 #include <stk_mesh/base/FieldParallel.hpp>
31 #ifdef PANZER_HAVE_UMR
32 #include <Ioumr_DatabaseIO.hpp>
35 #include "Teuchos_StandardParameterEntryValidators.hpp"
37 namespace panzer_stk {
39 int getMeshDimension(
const std::string & meshStr,
40 stk::ParallelMachine parallelMach,
41 const std::string & typeStr)
43 stk::io::StkMeshIoBroker meshData(parallelMach);
44 meshData.use_simple_fields();
45 meshData.property_add(Ioss::Property(
"LOWER_CASE_VARIABLE_NAMES",
false));
46 meshData.add_mesh_database(meshStr, fileTypeToIOSSType(typeStr), stk::io::READ_MESH);
47 meshData.create_input_mesh();
48 return Teuchos::as<int>(meshData.meta_data_ptr()->spatial_dimension());
51 std::string fileTypeToIOSSType(
const std::string & fileType)
54 if (fileType==
"Exodus")
55 IOSSType =
"exodusii";
56 #ifdef PANZER_HAVE_UMR
57 else if (fileType==
"Exodus Refinement")
58 IOSSType =
"Refinement";
60 else if (fileType==
"Pamgen")
66 STK_ExodusReaderFactory::STK_ExodusReaderFactory()
67 : fileName_(
""), fileType_(
""), restartIndex_(0), userMeshScaling_(false), keepPerceptData_(false),
68 keepPerceptParentElements_(false), rebalancing_(
"default"),
69 meshScaleFactor_(0.0), levelsOfRefinement_(0),
70 createEdgeBlocks_(false), createFaceBlocks_(false), geometryName_(
"")
73 STK_ExodusReaderFactory::STK_ExodusReaderFactory(
const std::string & fileName,
74 const int restartIndex)
75 : fileName_(fileName), fileType_(
"Exodus"), restartIndex_(restartIndex), userMeshScaling_(false),
76 keepPerceptData_(false), keepPerceptParentElements_(false), rebalancing_(
"default"),
77 meshScaleFactor_(0.0), levelsOfRefinement_(0), createEdgeBlocks_(false), createFaceBlocks_(false), geometryName_(
"")
82 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildMesh()");
87 RCP<STK_Interface> mesh = buildUncommitedMesh(parallelMach);
93 mesh->initialize(parallelMach,
false,doPerceptRefinement());
95 completeMeshConstruction(*mesh,parallelMach);
106 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildUncomittedMesh()");
112 stk::io::StkMeshIoBroker* meshData =
new stk::io::StkMeshIoBroker(parallelMach);
113 meshData->use_simple_fields();
114 meshData->property_add(Ioss::Property(
"LOWER_CASE_VARIABLE_NAMES",
false));
117 std::vector<std::string> entity_rank_names = stk::mesh::entity_rank_names();
118 entity_rank_names.push_back(
"FAMILY_TREE");
119 meshData->set_rank_name_vector(entity_rank_names);
121 #ifdef PANZER_HAVE_UMR
123 Ioumr::IOFactory::factory();
125 meshData->property_add(Ioss::Property(
"GEOMETRY_FILE", geometryName_));
126 meshData->property_add(Ioss::Property(
"NUMBER_REFINEMENTS", levelsOfRefinement_));
129 meshData->add_mesh_database(fileName_, fileTypeToIOSSType(fileType_), stk::io::READ_MESH);
131 meshData->create_input_mesh();
132 RCP<stk::mesh::MetaData> metaData =
Teuchos::rcp(meshData->meta_data_ptr());
134 RCP<STK_Interface> mesh =
rcp(
new STK_Interface(metaData));
135 mesh->initializeFromMetaData();
136 mesh->instantiateBulkData(parallelMach);
137 meshData->set_bulk_data(Teuchos::get_shared_ptr(mesh->getBulkData()));
141 meshData->add_all_mesh_fields_as_input_fields();
145 mesh->getMetaData()->declare_attribute_with_delete(meshData);
148 registerElementBlocks(*mesh,*meshData);
149 registerSidesets(*mesh);
150 registerNodesets(*mesh);
152 if (createEdgeBlocks_) {
153 registerEdgeBlocks(*mesh,*meshData);
155 if (createFaceBlocks_ && mesh->getMetaData()->spatial_dimension() > 2) {
156 registerFaceBlocks(*mesh,*meshData);
159 buildMetaData(parallelMach, *mesh);
161 mesh->addPeriodicBCs(periodicBCVec_);
162 mesh->setBoundingBoxSearchFlag(useBBoxSearch_);
167 void STK_ExodusReaderFactory::completeMeshConstruction(STK_Interface & mesh,stk::ParallelMachine parallelMach)
const
169 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::completeMeshConstruction()");
175 if(not mesh.isInitialized()) {
176 mesh.initialize(parallelMach,
true,doPerceptRefinement());
180 stk::mesh::MetaData & metaData = *mesh.getMetaData();
181 stk::mesh::BulkData & bulkData = *mesh.getBulkData();
182 stk::io::StkMeshIoBroker * meshData =
183 const_cast<stk::io::StkMeshIoBroker *
>(metaData.get_attribute<stk::io::StkMeshIoBroker>());
191 meshData->populate_bulk_data();
193 if (doPerceptRefinement()) {
194 const bool deleteParentElements = !keepPerceptParentElements_;
195 mesh.refineMesh(levelsOfRefinement_,deleteParentElements);
200 if (userMeshScaling_)
202 stk::mesh::Field<double>* coord_field = metaData.get_field<
double>(stk::topology::NODE_RANK,
"coordinates");
204 stk::mesh::Selector select_all_local = metaData.locally_owned_part() | metaData.globally_shared_part();
205 stk::mesh::BucketVector
const& my_node_buckets = bulkData.get_buckets(stk::topology::NODE_RANK, select_all_local);
207 int mesh_dim = mesh.getDimension();
210 const double inv_msf = 1.0/meshScaleFactor_;
211 for (
size_t i=0; i < my_node_buckets.size(); ++i)
213 stk::mesh::Bucket& b = *(my_node_buckets[i]);
214 double* coordinate_data = field_data( *coord_field, b );
216 for (
size_t j=0; j < b.size(); ++j) {
217 for (
int k=0; k < mesh_dim; ++k) {
218 coordinate_data[mesh_dim*j + k] *= inv_msf;
226 int restartIndex = restartIndex_;
228 std::pair<int,double> lastTimeStep = meshData->get_input_ioss_region()->get_max_time();
229 restartIndex = 1+restartIndex+lastTimeStep.first;
233 meshData->read_defined_input_fields(restartIndex);
235 mesh.buildSubcells();
236 mesh.buildLocalElementIDs();
238 mesh.beginModification();
239 if (createEdgeBlocks_) {
240 mesh.buildLocalEdgeIDs();
243 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
244 mesh.buildLocalFaceIDs();
247 mesh.endModification();
249 if (userMeshScaling_) {
250 stk::mesh::Field<double>* coord_field = metaData.get_field<
double>(stk::topology::NODE_RANK,
"coordinates");
251 std::vector< const stk::mesh::FieldBase *> fields;
252 fields.push_back(coord_field);
254 stk::mesh::communicate_field_data(bulkData, fields);
258 mesh.setInitialStateTime(meshData->get_input_ioss_region()->get_state_time(restartIndex));
260 mesh.setInitialStateTime(0.0);
265 if(rebalancing_ ==
"default")
267 this->rebalance(mesh);
268 else if(rebalancing_ !=
"none")
271 "ERROR: Rebalancing was not set to a valid choice");
280 "Error, the parameter {name=\"File Name\","
282 "\nis required in parameter (sub)list \""<< paramList->
name() <<
"\"."
290 paramList->
set<
int>(
"Restart Index", -1);
293 paramList->
set(
"File Type",
"Exodus");
295 if(!paramList->
isSublist(
"Periodic BCs"))
296 paramList->
sublist(
"Periodic BCs");
300 p_bcs.
set<
int>(
"Count", 0);
302 if(!paramList->
isParameter(
"Levels of Uniform Refinement"))
303 paramList->
set<
int>(
"Levels of Uniform Refinement", 0);
306 paramList->
set<
bool>(
"Keep Percept Data",
false);
308 if(!paramList->
isParameter(
"Keep Percept Parent Elements"))
309 paramList->
set<
bool>(
"Keep Percept Parent Elements",
false);
312 paramList->
set<std::string>(
"Rebalancing",
"default");
316 paramList->
set<
bool>(
"Create Edge Blocks",
false);
320 paramList->
set<
bool>(
"Create Face Blocks",
false);
323 paramList->
set(
"Geometry File Name",
"");
327 setMyParamList(paramList);
329 fileName_ = paramList->
get<std::string>(
"File Name");
331 geometryName_ = paramList->
get<std::string>(
"Geometry File Name");
333 restartIndex_ = paramList->
get<
int>(
"Restart Index");
335 fileType_ = paramList->
get<std::string>(
"File Type");
340 meshScaleFactor_ = paramList->
get<
double>(
"Scale Factor");
341 userMeshScaling_ =
true;
345 parsePeriodicBCList(Teuchos::rcpFromRef(paramList->
sublist(
"Periodic BCs")),periodicBCVec_,useBBoxSearch_);
347 keepPerceptData_ = paramList->
get<
bool>(
"Keep Percept Data");
349 keepPerceptParentElements_ = paramList->
get<
bool>(
"Keep Percept Parent Elements");
351 rebalancing_ = paramList->
get<std::string>(
"Rebalancing");
353 levelsOfRefinement_ = paramList->
get<
int>(
"Levels of Uniform Refinement");
355 createEdgeBlocks_ = paramList->
get<
bool>(
"Create Edge Blocks");
356 createFaceBlocks_ = paramList->
get<
bool>(
"Create Face Blocks");
364 if(validParams==Teuchos::null) {
366 validParams->
set<std::string>(
"File Name",
"<file name not set>",
"Name of exodus file to be read",
368 validParams->
set<std::string>(
"Geometry File Name",
"<file name not set>",
"Name of geometry file for refinement",
371 validParams->
set<
int>(
"Restart Index",-1,
"Index of solution to read in",
374 validParams->
set<std::string>(
"File Type",
"Exodus",
375 "Choose input file type - either \"Exodus\", \"Exodus Refinement\" or \"Pamgen\"",
377 #ifdef PANZER_HAVE_UMR
382 validParams->
set<
double>(
"Scale Factor", 1.0,
"Scale factor to apply to mesh after read",
386 bcs.
set<
int>(
"Count",0);
388 validParams->
set(
"Levels of Uniform Refinement",0,
"Number of levels of inline uniform mesh refinement");
390 validParams->
set(
"Keep Percept Data",
false,
"Keep the Percept mesh after uniform refinement is applied");
392 validParams->
set(
"Keep Percept Parent Elements",
false,
"Keep the parent element information in the Percept data");
394 validParams->
set(
"Rebalancing",
"default",
"The type of rebalancing to be performed on the mesh after creation (default, none)");
397 validParams->
set(
"Create Edge Blocks",
false,
"Create or copy edge blocks in the mesh");
398 validParams->
set(
"Create Face Blocks",
false,
"Create or copy face blocks in the mesh");
404 void STK_ExodusReaderFactory::registerElementBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
408 RCP<stk::mesh::MetaData> femMetaData = mesh.getMetaData();
413 const Ioss::ElementBlockContainer & elem_blocks = meshData.get_input_ioss_region()->get_element_blocks();
414 for(Ioss::ElementBlockContainer::const_iterator itr=elem_blocks.begin();itr!=elem_blocks.end();++itr) {
415 Ioss::GroupingEntity * entity = *itr;
416 const std::string & name = entity->name();
418 const stk::mesh::Part * part = femMetaData->get_part(name);
419 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(femMetaData->get_topology(*part));
420 const CellTopologyData * ct = cellTopo.getCellTopologyData();
423 mesh.addElementBlock(part->name(),ct);
425 if (createEdgeBlocks_) {
426 createUniqueEdgeTopologyMap(mesh, part);
428 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
429 createUniqueFaceTopologyMap(mesh, part);
434 template <
typename SetType>
435 void buildSetNames(
const SetType & setData,std::vector<std::string> & names)
438 for(
typename SetType::const_iterator itr=setData.begin();itr!=setData.end();++itr) {
439 Ioss::GroupingEntity * entity = *itr;
440 names.push_back(entity->name());
444 void STK_ExodusReaderFactory::registerSidesets(STK_Interface & mesh)
const
448 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
449 const stk::mesh::PartVector & parts = metaData->get_parts();
451 stk::mesh::PartVector::const_iterator partItr;
452 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
453 const stk::mesh::Part * part = *partItr;
454 const stk::mesh::PartVector & subsets = part->subsets();
455 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
456 const CellTopologyData * ct = cellTopo.getCellTopologyData();
460 if(part->primary_entity_rank()==mesh.getSideRank() && ct==0 && subsets.size()>0) {
462 "STK_ExodusReaderFactory::registerSidesets error - part \"" << part->name() <<
463 "\" has more than one subset");
466 const stk::mesh::Part * ss_part = subsets[0];
467 shards::CellTopology ss_cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*ss_part));
468 const CellTopologyData * ss_ct = ss_cellTopo.getCellTopologyData();
472 mesh.addSideset(part->name(),ss_ct);
477 void STK_ExodusReaderFactory::registerNodesets(STK_Interface & mesh)
const
481 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
482 const stk::mesh::PartVector & parts = metaData->get_parts();
484 stk::mesh::PartVector::const_iterator partItr;
485 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
486 const stk::mesh::Part * part = *partItr;
487 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
488 const CellTopologyData * ct = cellTopo.getCellTopologyData();
492 if(part->primary_entity_rank()==mesh.getNodeRank() && ct==0) {
496 mesh.addNodeset(part->name());
501 void STK_ExodusReaderFactory::registerEdgeBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
505 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
513 const Ioss::EdgeBlockContainer & edge_blocks = meshData.get_input_ioss_region()->get_edge_blocks();
514 for(Ioss::EdgeBlockContainer::const_iterator ebc_iter=edge_blocks.begin();ebc_iter!=edge_blocks.end();++ebc_iter) {
515 Ioss::GroupingEntity * entity = *ebc_iter;
516 const stk::mesh::Part * edgeBlockPart = metaData->get_part(entity->name());
517 const stk::topology edgeBlockTopo = metaData->get_topology(*edgeBlockPart);
519 for (
auto ebuet_iter : elemBlockUniqueEdgeTopologies_) {
520 std::string elemBlockName = ebuet_iter.first;
521 std::vector<stk::topology> uniqueEdgeTopologies = ebuet_iter.second;
523 auto find_result = std::find(uniqueEdgeTopologies.begin(), uniqueEdgeTopologies.end(), edgeBlockTopo);
524 if (find_result != uniqueEdgeTopologies.end()) {
525 mesh.addEdgeBlock(elemBlockName, edgeBlockPart->name(), edgeBlockTopo);
531 void STK_ExodusReaderFactory::registerFaceBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
535 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
543 const Ioss::FaceBlockContainer & face_blocks = meshData.get_input_ioss_region()->get_face_blocks();
544 for(Ioss::FaceBlockContainer::const_iterator fbc_itr=face_blocks.begin();fbc_itr!=face_blocks.end();++fbc_itr) {
545 Ioss::GroupingEntity * entity = *fbc_itr;
546 const stk::mesh::Part * faceBlockPart = metaData->get_part(entity->name());
547 const stk::topology faceBlockTopo = metaData->get_topology(*faceBlockPart);
549 for (
auto ebuft_iter : elemBlockUniqueFaceTopologies_) {
550 std::string elemBlockName = ebuft_iter.first;
551 std::vector<stk::topology> uniqueFaceTopologies = ebuft_iter.second;
553 auto find_result = std::find(uniqueFaceTopologies.begin(), uniqueFaceTopologies.end(), faceBlockTopo);
554 if (find_result != uniqueFaceTopologies.end()) {
555 mesh.addFaceBlock(elemBlockName, faceBlockPart->name(), faceBlockTopo);
561 bool topo_less (stk::topology &i,stk::topology &j) {
return (i.value() < j.value()); }
562 bool topo_equal (stk::topology &i,stk::topology &j) {
return (i.value() == j.value()); }
564 void STK_ExodusReaderFactory::createUniqueEdgeTopologyMap(STK_Interface & mesh,
const stk::mesh::Part *elemBlockPart)
const
571 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
572 const stk::topology elemBlockTopo = metaData->get_topology(*elemBlockPart);
574 std::vector<stk::topology> edge_topologies;
575 for (
unsigned i=0;i<elemBlockTopo.num_edges();i++) {
576 edge_topologies.push_back(elemBlockTopo.edge_topology(i));
578 std::sort(edge_topologies.begin(), edge_topologies.end(), topo_less);
579 std::vector<stk::topology>::iterator new_end;
580 new_end = std::unique(edge_topologies.begin(), edge_topologies.end(), topo_equal);
581 edge_topologies.resize( std::distance(edge_topologies.begin(),new_end) );
583 elemBlockUniqueEdgeTopologies_[elemBlockPart->name()] = edge_topologies;
586 void STK_ExodusReaderFactory::createUniqueFaceTopologyMap(STK_Interface & mesh,
const stk::mesh::Part *elemBlockPart)
const
593 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
594 const stk::topology elemBlockTopo = metaData->get_topology(*elemBlockPart);
596 std::vector<stk::topology> face_topologies;
597 for (
unsigned i=0;i<elemBlockTopo.num_faces();i++) {
598 face_topologies.push_back(elemBlockTopo.face_topology(i));
600 std::sort(face_topologies.begin(), face_topologies.end(), topo_less);
601 std::vector<stk::topology>::iterator new_end;
602 new_end = std::unique(face_topologies.begin(), face_topologies.end(), topo_equal);
603 face_topologies.resize( std::distance(face_topologies.begin(),new_end) );
605 elemBlockUniqueFaceTopologies_[elemBlockPart->name()] = face_topologies;
610 void STK_ExodusReaderFactory::addEdgeBlocks(STK_Interface & mesh)
const
619 for (
auto iter : elemBlockUniqueEdgeTopologies_) {
620 std::string elemBlockName =
iter.first;
621 std::vector<stk::topology> uniqueEdgeTopologies =
iter.second;
623 for (
auto topo : uniqueEdgeTopologies ) {
624 const stk::mesh::Part * elemBlockPart = metaData->get_part(elemBlockName);
625 const stk::mesh::Part & edgeTopoPart = metaData->get_topology_root_part(topo);
627 stk::mesh::Selector owned_block;
628 owned_block = *elemBlockPart;
629 owned_block &= edgeTopoPart;
630 owned_block &= metaData->locally_owned_part();
633 stk::mesh::Part * edge_block = mesh.getEdgeBlock(edge_block_name);
635 std::vector<stk::mesh::Entity> all_edges_for_topo;
636 bulkData->get_entities(mesh.getEdgeRank(),owned_block,all_edges_for_topo);
637 mesh.addEntitiesToEdgeBlock(all_edges_for_topo, edge_block);
644 void STK_ExodusReaderFactory::addFaceBlocks(STK_Interface & mesh)
const
653 for (
auto iter : elemBlockUniqueFaceTopologies_) {
654 std::string elemBlockName =
iter.first;
655 std::vector<stk::topology> uniqueFaceTopologies =
iter.second;
657 for (
auto topo : uniqueFaceTopologies ) {
658 const stk::mesh::Part * elemBlockPart = metaData->get_part(elemBlockName);
659 const stk::mesh::Part & faceTopoPart = metaData->get_topology_root_part(topo);
661 stk::mesh::Selector owned_block;
662 owned_block = *elemBlockPart;
663 owned_block &= faceTopoPart;
664 owned_block &= metaData->locally_owned_part();
667 stk::mesh::Part * face_block = mesh.getFaceBlock(face_block_name);
669 std::vector<stk::mesh::Entity> all_faces_for_topo;
670 bulkData->get_entities(mesh.getFaceRank(),owned_block,all_faces_for_topo);
671 mesh.addEntitiesToFaceBlock(all_faces_for_topo, face_block);
676 void STK_ExodusReaderFactory::buildMetaData(stk::ParallelMachine , STK_Interface & mesh)
const
678 if (createEdgeBlocks_) {
682 for (
auto iter : elemBlockUniqueEdgeTopologies_) {
683 std::string elemBlockName =
iter.first;
684 std::vector<stk::topology> uniqueEdgeTopologies =
iter.second;
686 for (
auto topo : uniqueEdgeTopologies ) {
688 mesh.addEdgeBlock(elemBlockName, edge_block_name, topo);
692 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
696 for (
auto iter : elemBlockUniqueFaceTopologies_) {
697 std::string elemBlockName =
iter.first;
698 std::vector<stk::topology> uniqueFaceTopologies =
iter.second;
700 for (
auto topo : uniqueFaceTopologies ) {
702 mesh.addFaceBlock(elemBlockName, face_block_name, topo);
708 bool STK_ExodusReaderFactory::doPerceptRefinement()
const
710 return (fileType_!=
"Exodus Refinement") && (levelsOfRefinement_ > 0);
713 std::string STK_ExodusReaderFactory::mkBlockName(std::string base, std::string topo_name)
const
716 name = topo_name+
"_"+base;
717 std::transform(name.begin(), name.end(), name.begin(),
719 {
return char(std::tolower(c)); });
RCP< const T > getConst() const
const std::string & name() const
std::string currentParametersString() const
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static const std::string nodesString
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool isSublist(const std::string &name) const
static const std::string edgeBlockString
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
static const std::string faceBlockString
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)