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();
249 const bool find_and_set_shared_nodes_in_stk =
false;
250 mesh.endModification(find_and_set_shared_nodes_in_stk);
253 if (userMeshScaling_) {
254 stk::mesh::Field<double>* coord_field = metaData.get_field<
double>(stk::topology::NODE_RANK,
"coordinates");
255 std::vector< const stk::mesh::FieldBase *> fields;
256 fields.push_back(coord_field);
258 stk::mesh::communicate_field_data(bulkData, fields);
262 mesh.setInitialStateTime(meshData->get_input_ioss_region()->get_state_time(restartIndex));
264 mesh.setInitialStateTime(0.0);
269 if(rebalancing_ ==
"default")
271 this->rebalance(mesh);
272 else if(rebalancing_ !=
"none")
275 "ERROR: Rebalancing was not set to a valid choice");
284 "Error, the parameter {name=\"File Name\","
286 "\nis required in parameter (sub)list \""<< paramList->
name() <<
"\"."
294 paramList->
set<
int>(
"Restart Index", -1);
297 paramList->
set(
"File Type",
"Exodus");
299 if(!paramList->
isSublist(
"Periodic BCs"))
300 paramList->
sublist(
"Periodic BCs");
304 p_bcs.
set<
int>(
"Count", 0);
306 if(!paramList->
isParameter(
"Levels of Uniform Refinement"))
307 paramList->
set<
int>(
"Levels of Uniform Refinement", 0);
310 paramList->
set<
bool>(
"Keep Percept Data",
false);
312 if(!paramList->
isParameter(
"Keep Percept Parent Elements"))
313 paramList->
set<
bool>(
"Keep Percept Parent Elements",
false);
316 paramList->
set<std::string>(
"Rebalancing",
"default");
320 paramList->
set<
bool>(
"Create Edge Blocks",
false);
324 paramList->
set<
bool>(
"Create Face Blocks",
false);
327 paramList->
set(
"Geometry File Name",
"");
331 setMyParamList(paramList);
333 fileName_ = paramList->
get<std::string>(
"File Name");
335 geometryName_ = paramList->
get<std::string>(
"Geometry File Name");
337 restartIndex_ = paramList->
get<
int>(
"Restart Index");
339 fileType_ = paramList->
get<std::string>(
"File Type");
344 meshScaleFactor_ = paramList->
get<
double>(
"Scale Factor");
345 userMeshScaling_ =
true;
349 parsePeriodicBCList(Teuchos::rcpFromRef(paramList->
sublist(
"Periodic BCs")),periodicBCVec_,useBBoxSearch_);
351 keepPerceptData_ = paramList->
get<
bool>(
"Keep Percept Data");
353 keepPerceptParentElements_ = paramList->
get<
bool>(
"Keep Percept Parent Elements");
355 rebalancing_ = paramList->
get<std::string>(
"Rebalancing");
357 levelsOfRefinement_ = paramList->
get<
int>(
"Levels of Uniform Refinement");
359 createEdgeBlocks_ = paramList->
get<
bool>(
"Create Edge Blocks");
360 createFaceBlocks_ = paramList->
get<
bool>(
"Create Face Blocks");
368 if(validParams==Teuchos::null) {
370 validParams->
set<std::string>(
"File Name",
"<file name not set>",
"Name of exodus file to be read",
372 validParams->
set<std::string>(
"Geometry File Name",
"<file name not set>",
"Name of geometry file for refinement",
375 validParams->
set<
int>(
"Restart Index",-1,
"Index of solution to read in",
378 validParams->
set<std::string>(
"File Type",
"Exodus",
379 "Choose input file type - either \"Exodus\", \"Exodus Refinement\" or \"Pamgen\"",
381 #ifdef PANZER_HAVE_UMR
386 validParams->
set<
double>(
"Scale Factor", 1.0,
"Scale factor to apply to mesh after read",
390 bcs.
set<
int>(
"Count",0);
392 validParams->
set(
"Levels of Uniform Refinement",0,
"Number of levels of inline uniform mesh refinement");
394 validParams->
set(
"Keep Percept Data",
false,
"Keep the Percept mesh after uniform refinement is applied");
396 validParams->
set(
"Keep Percept Parent Elements",
false,
"Keep the parent element information in the Percept data");
398 validParams->
set(
"Rebalancing",
"default",
"The type of rebalancing to be performed on the mesh after creation (default, none)");
401 validParams->
set(
"Create Edge Blocks",
false,
"Create or copy edge blocks in the mesh");
402 validParams->
set(
"Create Face Blocks",
false,
"Create or copy face blocks in the mesh");
408 void STK_ExodusReaderFactory::registerElementBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
412 RCP<stk::mesh::MetaData> femMetaData = mesh.getMetaData();
417 const Ioss::ElementBlockContainer & elem_blocks = meshData.get_input_ioss_region()->get_element_blocks();
418 for(Ioss::ElementBlockContainer::const_iterator itr=elem_blocks.begin();itr!=elem_blocks.end();++itr) {
419 Ioss::GroupingEntity * entity = *itr;
420 const std::string & name = entity->name();
422 const stk::mesh::Part * part = femMetaData->get_part(name);
423 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(femMetaData->get_topology(*part));
424 const CellTopologyData * ct = cellTopo.getCellTopologyData();
427 mesh.addElementBlock(part->name(),ct);
429 if (createEdgeBlocks_) {
430 createUniqueEdgeTopologyMap(mesh, part);
432 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
433 createUniqueFaceTopologyMap(mesh, part);
438 template <
typename SetType>
439 void buildSetNames(
const SetType & setData,std::vector<std::string> & names)
442 for(
typename SetType::const_iterator itr=setData.begin();itr!=setData.end();++itr) {
443 Ioss::GroupingEntity * entity = *itr;
444 names.push_back(entity->name());
448 void STK_ExodusReaderFactory::registerSidesets(STK_Interface & mesh)
const
452 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
453 const stk::mesh::PartVector & parts = metaData->get_parts();
455 stk::mesh::PartVector::const_iterator partItr;
456 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
457 const stk::mesh::Part * part = *partItr;
458 const stk::mesh::PartVector & subsets = part->subsets();
459 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
460 const CellTopologyData * ct = cellTopo.getCellTopologyData();
464 if(part->primary_entity_rank()==mesh.getSideRank() && ct==0 && subsets.size()>0) {
466 "STK_ExodusReaderFactory::registerSidesets error - part \"" << part->name() <<
467 "\" has more than one subset");
470 const stk::mesh::Part * ss_part = subsets[0];
471 shards::CellTopology ss_cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*ss_part));
472 const CellTopologyData * ss_ct = ss_cellTopo.getCellTopologyData();
476 mesh.addSideset(part->name(),ss_ct);
481 void STK_ExodusReaderFactory::registerNodesets(STK_Interface & mesh)
const
485 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
486 const stk::mesh::PartVector & parts = metaData->get_parts();
488 stk::mesh::PartVector::const_iterator partItr;
489 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
490 const stk::mesh::Part * part = *partItr;
491 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
492 const CellTopologyData * ct = cellTopo.getCellTopologyData();
496 if(part->primary_entity_rank()==mesh.getNodeRank() && ct==0) {
500 mesh.addNodeset(part->name());
505 void STK_ExodusReaderFactory::registerEdgeBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
509 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
517 const Ioss::EdgeBlockContainer & edge_blocks = meshData.get_input_ioss_region()->get_edge_blocks();
518 for(Ioss::EdgeBlockContainer::const_iterator ebc_iter=edge_blocks.begin();ebc_iter!=edge_blocks.end();++ebc_iter) {
519 Ioss::GroupingEntity * entity = *ebc_iter;
520 const stk::mesh::Part * edgeBlockPart = metaData->get_part(entity->name());
521 const stk::topology edgeBlockTopo = metaData->get_topology(*edgeBlockPart);
523 for (
auto ebuet_iter : elemBlockUniqueEdgeTopologies_) {
524 std::string elemBlockName = ebuet_iter.first;
525 std::vector<stk::topology> uniqueEdgeTopologies = ebuet_iter.second;
527 auto find_result = std::find(uniqueEdgeTopologies.begin(), uniqueEdgeTopologies.end(), edgeBlockTopo);
528 if (find_result != uniqueEdgeTopologies.end()) {
529 mesh.addEdgeBlock(elemBlockName, edgeBlockPart->name(), edgeBlockTopo);
535 void STK_ExodusReaderFactory::registerFaceBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
539 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
547 const Ioss::FaceBlockContainer & face_blocks = meshData.get_input_ioss_region()->get_face_blocks();
548 for(Ioss::FaceBlockContainer::const_iterator fbc_itr=face_blocks.begin();fbc_itr!=face_blocks.end();++fbc_itr) {
549 Ioss::GroupingEntity * entity = *fbc_itr;
550 const stk::mesh::Part * faceBlockPart = metaData->get_part(entity->name());
551 const stk::topology faceBlockTopo = metaData->get_topology(*faceBlockPart);
553 for (
auto ebuft_iter : elemBlockUniqueFaceTopologies_) {
554 std::string elemBlockName = ebuft_iter.first;
555 std::vector<stk::topology> uniqueFaceTopologies = ebuft_iter.second;
557 auto find_result = std::find(uniqueFaceTopologies.begin(), uniqueFaceTopologies.end(), faceBlockTopo);
558 if (find_result != uniqueFaceTopologies.end()) {
559 mesh.addFaceBlock(elemBlockName, faceBlockPart->name(), faceBlockTopo);
565 bool topo_less (stk::topology &i,stk::topology &j) {
return (i.value() < j.value()); }
566 bool topo_equal (stk::topology &i,stk::topology &j) {
return (i.value() == j.value()); }
568 void STK_ExodusReaderFactory::createUniqueEdgeTopologyMap(STK_Interface & mesh,
const stk::mesh::Part *elemBlockPart)
const
575 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
576 const stk::topology elemBlockTopo = metaData->get_topology(*elemBlockPart);
578 std::vector<stk::topology> edge_topologies;
579 for (
unsigned i=0;i<elemBlockTopo.num_edges();i++) {
580 edge_topologies.push_back(elemBlockTopo.edge_topology(i));
582 std::sort(edge_topologies.begin(), edge_topologies.end(), topo_less);
583 std::vector<stk::topology>::iterator new_end;
584 new_end = std::unique(edge_topologies.begin(), edge_topologies.end(), topo_equal);
585 edge_topologies.resize( std::distance(edge_topologies.begin(),new_end) );
587 elemBlockUniqueEdgeTopologies_[elemBlockPart->name()] = edge_topologies;
590 void STK_ExodusReaderFactory::createUniqueFaceTopologyMap(STK_Interface & mesh,
const stk::mesh::Part *elemBlockPart)
const
597 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
598 const stk::topology elemBlockTopo = metaData->get_topology(*elemBlockPart);
600 std::vector<stk::topology> face_topologies;
601 for (
unsigned i=0;i<elemBlockTopo.num_faces();i++) {
602 face_topologies.push_back(elemBlockTopo.face_topology(i));
604 std::sort(face_topologies.begin(), face_topologies.end(), topo_less);
605 std::vector<stk::topology>::iterator new_end;
606 new_end = std::unique(face_topologies.begin(), face_topologies.end(), topo_equal);
607 face_topologies.resize( std::distance(face_topologies.begin(),new_end) );
609 elemBlockUniqueFaceTopologies_[elemBlockPart->name()] = face_topologies;
614 void STK_ExodusReaderFactory::addEdgeBlocks(STK_Interface & mesh)
const
626 for (
auto iter : elemBlockUniqueEdgeTopologies_) {
627 std::string elemBlockName =
iter.first;
628 std::vector<stk::topology> uniqueEdgeTopologies =
iter.second;
630 for (
auto topo : uniqueEdgeTopologies ) {
631 const stk::mesh::Part * elemBlockPart = metaData->get_part(elemBlockName);
632 const stk::mesh::Part & edgeTopoPart = metaData->get_topology_root_part(topo);
634 stk::mesh::Selector owned_block;
635 owned_block = *elemBlockPart;
636 owned_block &= edgeTopoPart;
639 stk::mesh::Part * edge_block = mesh.getEdgeBlock(edge_block_name);
641 bulkData->change_entity_parts(owned_block, mesh.getEdgeRank(), stk::mesh::PartVector{edge_block});
648 void STK_ExodusReaderFactory::addFaceBlocks(STK_Interface & mesh)
const
660 for (
auto iter : elemBlockUniqueFaceTopologies_) {
661 std::string elemBlockName =
iter.first;
662 std::vector<stk::topology> uniqueFaceTopologies =
iter.second;
664 for (
auto topo : uniqueFaceTopologies ) {
665 const stk::mesh::Part * elemBlockPart = metaData->get_part(elemBlockName);
666 const stk::mesh::Part & faceTopoPart = metaData->get_topology_root_part(topo);
668 stk::mesh::Selector owned_block;
669 owned_block = *elemBlockPart;
670 owned_block &= faceTopoPart;
673 stk::mesh::Part * face_block = mesh.getFaceBlock(face_block_name);
675 bulkData->change_entity_parts(owned_block, mesh.getFaceRank(), stk::mesh::PartVector{face_block});
680 void STK_ExodusReaderFactory::buildMetaData(stk::ParallelMachine , STK_Interface & mesh)
const
682 if (createEdgeBlocks_) {
686 for (
auto iter : elemBlockUniqueEdgeTopologies_) {
687 std::string elemBlockName =
iter.first;
688 std::vector<stk::topology> uniqueEdgeTopologies =
iter.second;
690 for (
auto topo : uniqueEdgeTopologies ) {
692 mesh.addEdgeBlock(elemBlockName, edge_block_name, topo);
696 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
700 for (
auto iter : elemBlockUniqueFaceTopologies_) {
701 std::string elemBlockName =
iter.first;
702 std::vector<stk::topology> uniqueFaceTopologies =
iter.second;
704 for (
auto topo : uniqueFaceTopologies ) {
706 mesh.addFaceBlock(elemBlockName, face_block_name, topo);
712 bool STK_ExodusReaderFactory::doPerceptRefinement()
const
714 return (fileType_!=
"Exodus Refinement") && (levelsOfRefinement_ > 0);
717 std::string STK_ExodusReaderFactory::mkBlockName(std::string base, std::string topo_name)
const
720 name = topo_name+
"_"+base;
721 std::transform(name.begin(), name.end(), name.begin(),
723 {
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)