44 #include <Teuchos_TimeMonitor.hpp> 45 #include <PanzerAdaptersSTK_config.hpp> 50 #ifdef PANZER_HAVE_IOSS 52 #include <Ionit_Initializer.h> 53 #include <Ioss_ElementBlock.h> 54 #include <Ioss_Region.h> 55 #include <stk_mesh/base/GetBuckets.hpp> 56 #include <stk_io/StkMeshIoBroker.hpp> 57 #include <stk_io/IossBridge.hpp> 58 #include <stk_mesh/base/FieldParallel.hpp> 60 #include "Teuchos_StandardParameterEntryValidators.hpp" 64 int getMeshDimension(
const std::string & meshStr,
65 stk::ParallelMachine parallelMach,
68 stk::io::StkMeshIoBroker meshData(parallelMach);
69 meshData.property_add(Ioss::Property(
"LOWER_CASE_VARIABLE_NAMES",
false));
71 meshData.add_mesh_database(meshStr,
"exodusII", stk::io::READ_MESH);
73 meshData.add_mesh_database(meshStr,
"pamgen", stk::io::READ_MESH);
74 meshData.create_input_mesh();
75 return Teuchos::as<int>(meshData.meta_data_rcp()->spatial_dimension());
78 STK_ExodusReaderFactory::STK_ExodusReaderFactory()
79 : fileName_(
""), restartIndex_(0), isExodus_(true), userMeshScaling_(false), keepPerceptData_(false),
80 keepPerceptParentElements_(false), rebalancing_(
"default"), meshScaleFactor_(0.0), levelsOfRefinement_(0),
81 createEdgeBlocks_(false), createFaceBlocks_(false)
84 STK_ExodusReaderFactory::STK_ExodusReaderFactory(
const std::string & fileName,
85 const int restartIndex,
87 : fileName_(fileName), restartIndex_(restartIndex), isExodus_(isExodus), userMeshScaling_(false),
88 keepPerceptData_(false), keepPerceptParentElements_(false), rebalancing_(
"default"),
89 meshScaleFactor_(0.0), levelsOfRefinement_(0), createEdgeBlocks_(false), createFaceBlocks_(false)
92 Teuchos::RCP<STK_Interface> STK_ExodusReaderFactory::buildMesh(stk::ParallelMachine parallelMach)
const 94 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildMesh()");
99 RCP<STK_Interface> mesh = buildUncommitedMesh(parallelMach);
105 const bool buildRefinementSupport = levelsOfRefinement_ > 0 ? true :
false;
106 mesh->initialize(parallelMach,
false,buildRefinementSupport);
108 completeMeshConstruction(*mesh,parallelMach);
117 Teuchos::RCP<STK_Interface> STK_ExodusReaderFactory::buildUncommitedMesh(stk::ParallelMachine parallelMach)
const 119 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildUncomittedMesh()");
125 stk::io::StkMeshIoBroker* meshData =
new stk::io::StkMeshIoBroker(parallelMach);
126 meshData->property_add(Ioss::Property(
"LOWER_CASE_VARIABLE_NAMES",
false));
129 std::vector<std::string> entity_rank_names = stk::mesh::entity_rank_names();
130 entity_rank_names.push_back(
"FAMILY_TREE");
131 meshData->set_rank_name_vector(entity_rank_names);
134 meshData->add_mesh_database(fileName_,
"exodusII", stk::io::READ_MESH);
136 meshData->add_mesh_database(fileName_,
"pamgen", stk::io::READ_MESH);
138 meshData->create_input_mesh();
139 RCP<stk::mesh::MetaData> metaData = meshData->meta_data_rcp();
141 RCP<STK_Interface> mesh = rcp(
new STK_Interface(metaData));
142 mesh->initializeFromMetaData();
143 mesh->instantiateBulkData(parallelMach);
144 meshData->set_bulk_data(mesh->getBulkData());
148 meshData->add_all_mesh_fields_as_input_fields();
152 mesh->getMetaData()->declare_attribute_with_delete(meshData);
155 registerElementBlocks(*mesh,*meshData);
156 registerSidesets(*mesh);
157 registerNodesets(*mesh);
159 if (createEdgeBlocks_) {
160 registerEdgeBlocks(*mesh);
162 if (createFaceBlocks_ && mesh->getMetaData()->spatial_dimension() > 2) {
163 registerFaceBlocks(*mesh);
166 buildMetaData(parallelMach, *mesh);
168 mesh->addPeriodicBCs(periodicBCVec_);
173 void STK_ExodusReaderFactory::completeMeshConstruction(STK_Interface & mesh,stk::ParallelMachine parallelMach)
const 175 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::completeMeshConstruction()");
181 if(not mesh.isInitialized()) {
182 const bool buildRefinementSupport = levelsOfRefinement_ > 0 ? true :
false;
183 mesh.initialize(parallelMach,
true,buildRefinementSupport);
187 stk::mesh::MetaData & metaData = *mesh.getMetaData();
188 stk::mesh::BulkData & bulkData = *mesh.getBulkData();
189 stk::io::StkMeshIoBroker * meshData =
190 const_cast<stk::io::StkMeshIoBroker *
>(metaData.get_attribute<stk::io::StkMeshIoBroker>());
194 TEUCHOS_ASSERT(metaData.remove_attribute(meshData));
198 meshData->populate_bulk_data();
200 const bool deleteParentElements = !keepPerceptParentElements_;
201 if (levelsOfRefinement_ > 0)
202 mesh.refineMesh(levelsOfRefinement_,deleteParentElements);
206 if (userMeshScaling_)
208 stk::mesh::Field<double,stk::mesh::Cartesian>* coord_field =
209 metaData.get_field<stk::mesh::Field<double, stk::mesh::Cartesian> >(stk::topology::NODE_RANK,
"coordinates");
211 stk::mesh::Selector select_all_local = metaData.locally_owned_part() | metaData.globally_shared_part();
212 stk::mesh::BucketVector
const& my_node_buckets = bulkData.get_buckets(stk::topology::NODE_RANK, select_all_local);
214 int mesh_dim = mesh.getDimension();
217 const double inv_msf = 1.0/meshScaleFactor_;
218 for (
size_t i=0; i < my_node_buckets.size(); ++i)
220 stk::mesh::Bucket& b = *(my_node_buckets[i]);
221 double* coordinate_data = field_data( *coord_field, b );
223 for (
size_t j=0; j < b.size(); ++j) {
224 for (
int k=0; k < mesh_dim; ++k) {
225 coordinate_data[mesh_dim*j + k] *= inv_msf;
233 int restartIndex = restartIndex_;
235 std::pair<int,double> lastTimeStep = meshData->get_input_io_region()->get_max_time();
236 restartIndex = 1+restartIndex+lastTimeStep.first;
240 meshData->read_defined_input_fields(restartIndex);
242 mesh.buildSubcells();
243 mesh.buildLocalElementIDs();
244 if (createEdgeBlocks_) {
245 mesh.buildLocalEdgeIDs();
247 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
248 mesh.buildLocalFaceIDs();
251 mesh.beginModification();
252 if (createEdgeBlocks_) {
255 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
258 mesh.endModification();
260 if (userMeshScaling_) {
261 stk::mesh::Field<double,stk::mesh::Cartesian>* coord_field =
262 metaData.get_field<stk::mesh::Field<double, stk::mesh::Cartesian> >(stk::topology::NODE_RANK,
"coordinates");
263 std::vector< const stk::mesh::FieldBase *> fields;
264 fields.push_back(coord_field);
266 stk::mesh::communicate_field_data(bulkData, fields);
270 mesh.setInitialStateTime(meshData->get_input_io_region()->get_state_time(restartIndex));
272 mesh.setInitialStateTime(0.0);
277 if(rebalancing_ ==
"default")
279 this->rebalance(mesh);
280 else if(rebalancing_ !=
"none")
282 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
283 "ERROR: Rebalancing was not set to a valid choice");
288 void STK_ExodusReaderFactory::setParameterList(
const Teuchos::RCP<Teuchos::ParameterList> & paramList)
290 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(!paramList->isParameter(
"File Name"),
291 Teuchos::Exceptions::InvalidParameterName,
292 "Error, the parameter {name=\"File Name\"," 294 "\nis required in parameter (sub)list \""<< paramList->name() <<
"\"." 295 "\n\nThe parsed parameter parameter list is: \n" << paramList->currentParametersString()
301 if(!paramList->isParameter(
"Restart Index"))
302 paramList->set<
int>(
"Restart Index", -1);
304 if(!paramList->isParameter(
"File Type"))
305 paramList->set(
"File Type",
"Exodus");
307 if(!paramList->isSublist(
"Periodic BCs"))
308 paramList->sublist(
"Periodic BCs");
310 Teuchos::ParameterList& p_bcs = paramList->sublist(
"Periodic BCs");
311 if (!p_bcs.isParameter(
"Count"))
312 p_bcs.set<
int>(
"Count", 0);
314 if(!paramList->isParameter(
"Levels of Uniform Refinement"))
315 paramList->set<
int>(
"Levels of Uniform Refinement", 0);
317 if(!paramList->isParameter(
"Keep Percept Data"))
318 paramList->set<
bool>(
"Keep Percept Data",
false);
320 if(!paramList->isParameter(
"Keep Percept Parent Elements"))
321 paramList->set<
bool>(
"Keep Percept Parent Elements",
false);
323 if(!paramList->isParameter(
"Rebalancing"))
324 paramList->set<std::string>(
"Rebalancing",
"default");
326 if(!paramList->isParameter(
"Create Edge Blocks"))
328 paramList->set<
bool>(
"Create Edge Blocks",
false);
330 if(!paramList->isParameter(
"Create Face Blocks"))
332 paramList->set<
bool>(
"Create Face Blocks",
false);
334 paramList->validateParameters(*getValidParameters(),0);
336 setMyParamList(paramList);
338 fileName_ = paramList->get<std::string>(
"File Name");
340 restartIndex_ = paramList->get<
int>(
"Restart Index");
343 const auto fileType = paramList->get<std::string>(
"File Type");
344 isExodus_ = fileType ==
"Exodus";
348 if (paramList->isParameter(
"Scale Factor"))
350 meshScaleFactor_ = paramList->get<
double>(
"Scale Factor");
351 userMeshScaling_ =
true;
355 parsePeriodicBCList(Teuchos::rcpFromRef(paramList->sublist(
"Periodic BCs")),periodicBCVec_);
357 keepPerceptData_ = paramList->get<
bool>(
"Keep Percept Data");
359 keepPerceptParentElements_ = paramList->get<
bool>(
"Keep Percept Parent Elements");
361 rebalancing_ = paramList->get<std::string>(
"Rebalancing");
363 levelsOfRefinement_ = paramList->get<
int>(
"Levels of Uniform Refinement");
365 createEdgeBlocks_ = paramList->get<
bool>(
"Create Edge Blocks");
366 createFaceBlocks_ = paramList->get<
bool>(
"Create Face Blocks");
370 Teuchos::RCP<const Teuchos::ParameterList> STK_ExodusReaderFactory::getValidParameters()
const 372 static Teuchos::RCP<Teuchos::ParameterList> validParams;
374 if(validParams==Teuchos::null) {
375 validParams = Teuchos::rcp(
new Teuchos::ParameterList);
376 validParams->set<std::string>(
"File Name",
"<file name not set>",
"Name of exodus file to be read",
377 Teuchos::rcp(
new Teuchos::FileNameValidator));
379 validParams->set<
int>(
"Restart Index",-1,
"Index of solution to read in",
380 Teuchos::rcp(
new Teuchos::AnyNumberParameterEntryValidator(Teuchos::AnyNumberParameterEntryValidator::PREFER_INT,Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes(
true))));
382 Teuchos::setStringToIntegralParameter<int>(
"File Type",
384 "Choose input file type - either \"Exodus\" or \"Pamgen\"",
385 Teuchos::tuple<std::string>(
"Exodus",
"Pamgen"),
389 validParams->set<
double>(
"Scale Factor", 1.0,
"Scale factor to apply to mesh after read",
390 Teuchos::rcp(
new Teuchos::AnyNumberParameterEntryValidator(Teuchos::AnyNumberParameterEntryValidator::PREFER_DOUBLE,Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes(
true))));
392 Teuchos::ParameterList & bcs = validParams->sublist(
"Periodic BCs");
393 bcs.set<
int>(
"Count",0);
395 validParams->set(
"Levels of Uniform Refinement",0,
"Number of levels of inline uniform mesh refinement");
397 validParams->set(
"Keep Percept Data",
false,
"Keep the Percept mesh after uniform refinement is applied");
399 validParams->set(
"Keep Percept Parent Elements",
false,
"Keep the parent element information in the Percept data");
401 validParams->set(
"Rebalancing",
"default",
"The type of rebalancing to be performed on the mesh after creation (default, none)");
404 validParams->set(
"Create Edge Blocks",
false,
"Create or copy edge blocks in the mesh");
405 validParams->set(
"Create Face Blocks",
false,
"Create or copy face blocks in the mesh");
408 return validParams.getConst();
411 void STK_ExodusReaderFactory::registerElementBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const 415 RCP<stk::mesh::MetaData> femMetaData = mesh.getMetaData();
420 const Ioss::ElementBlockContainer & elem_blocks = meshData.get_input_io_region()->get_element_blocks();
421 for(Ioss::ElementBlockContainer::const_iterator itr=elem_blocks.begin();itr!=elem_blocks.end();++itr) {
422 Ioss::GroupingEntity * entity = *itr;
423 const std::string & name = entity->name();
425 const stk::mesh::Part * part = femMetaData->get_part(name);
426 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(femMetaData->get_topology(*part));
427 const CellTopologyData * ct = cellTopo.getCellTopologyData();
429 TEUCHOS_ASSERT(ct!=0);
430 mesh.addElementBlock(part->name(),ct);
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) {
461 TEUCHOS_TEST_FOR_EXCEPTION(subsets.size()!=1,std::runtime_error,
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)
const 505 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
506 const stk::mesh::PartVector & parts = metaData->get_parts();
508 stk::mesh::PartVector::const_iterator partItr;
509 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
510 const stk::mesh::Part * part = *partItr;
511 const stk::mesh::PartVector & subsets = part->subsets();
512 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
513 const CellTopologyData * ct = cellTopo.getCellTopologyData();
515 if(part->primary_entity_rank()==mesh.getEdgeRank() && ct==0 && subsets.size()>0) {
516 TEUCHOS_TEST_FOR_EXCEPTION(subsets.size()!=1,std::runtime_error,
517 "STK_ExodusReaderFactory::registerEdgeBlocks error - part \"" << part->name() <<
518 "\" has more than one subset");
520 if (stk::io::has_edge_block_part_attribute(*part) && stk::io::get_edge_block_part_attribute(*part)) {
522 const stk::mesh::Part * edge_part = subsets[0];
523 shards::CellTopology edge_cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*edge_part));
524 const CellTopologyData * edge_ct = edge_cellTopo.getCellTopologyData();
528 mesh.addEdgeBlock(part->name(),edge_ct);
535 void STK_ExodusReaderFactory::registerFaceBlocks(STK_Interface & mesh)
const 539 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
540 const stk::mesh::PartVector & parts = metaData->get_parts();
542 stk::mesh::PartVector::const_iterator partItr;
543 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
544 const stk::mesh::Part * part = *partItr;
545 const stk::mesh::PartVector & subsets = part->subsets();
546 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
547 const CellTopologyData * ct = cellTopo.getCellTopologyData();
549 if(part->primary_entity_rank()==mesh.getFaceRank() && ct==0 && subsets.size()>0) {
550 TEUCHOS_TEST_FOR_EXCEPTION(subsets.size()!=1,std::runtime_error,
551 "STK_ExodusReaderFactory::registerFaceBlocks error - part \"" << part->name() <<
552 "\" has more than one subset");
554 if (stk::io::has_face_block_part_attribute(*part) && stk::io::get_face_block_part_attribute(*part)) {
556 const stk::mesh::Part * face_part = subsets[0];
557 shards::CellTopology face_cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*face_part));
558 const CellTopologyData * face_ct = face_cellTopo.getCellTopologyData();
562 mesh.addFaceBlock(part->name(),face_ct);
571 void STK_ExodusReaderFactory::addEdgeBlocks(STK_Interface & mesh)
const 575 Teuchos::RCP<stk::mesh::BulkData> bulkData = mesh.getBulkData();
576 Teuchos::RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
578 std::vector<stk::mesh::Entity> edges;
579 bulkData->get_entities(mesh.getEdgeRank(),metaData->locally_owned_part(),edges);
580 mesh.addEntitiesToEdgeBlock(edges, edge_block);
585 void STK_ExodusReaderFactory::addFaceBlocks(STK_Interface & mesh)
const 589 Teuchos::RCP<stk::mesh::BulkData> bulkData = mesh.getBulkData();
590 Teuchos::RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
592 std::vector<stk::mesh::Entity> faces;
593 bulkData->get_entities(mesh.getFaceRank(),metaData->locally_owned_part(),faces);
594 mesh.addEntitiesToFaceBlock(faces, face_block);
597 void STK_ExodusReaderFactory::buildMetaData(stk::ParallelMachine , STK_Interface & mesh)
const 599 std::vector<std::string> block_names;
600 mesh.getElementBlockNames(block_names);
602 const CellTopologyData *ctd = mesh.getCellTopology(block_names[0])->getCellTopologyData();
604 if (createEdgeBlocks_) {
607 const CellTopologyData * edge_ctd = shards::CellTopology(ctd).getBaseCellTopologyData(1,0);
611 if (createFaceBlocks_ && mesh.getMetaData()->spatial_dimension() > 2) {
614 const CellTopologyData * face_ctd = shards::CellTopology(ctd).getBaseCellTopologyData(2,0);
static const std::string nodesString
static const std::string edgeBlockString
static const std::string faceBlockString