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 <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"
62 namespace panzer_stk {
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), meshScaleFactor_(0.0),
80 levelsOfRefinement_(0)
83 STK_ExodusReaderFactory::STK_ExodusReaderFactory(
const std::string & fileName,
84 const int restartIndex,
86 : fileName_(fileName), restartIndex_(restartIndex), isExodus_(isExodus), userMeshScaling_(false), meshScaleFactor_(0.0),
87 levelsOfRefinement_(0)
92 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildMesh()");
97 RCP<STK_Interface> mesh = buildUncommitedMesh(parallelMach);
103 const bool buildRefinementSupport = levelsOfRefinement_ > 0 ?
true :
false;
104 mesh->initialize(parallelMach,
false,buildRefinementSupport);
106 completeMeshConstruction(*mesh,parallelMach);
117 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildUncomittedMesh()");
123 stk::io::StkMeshIoBroker* meshData =
new stk::io::StkMeshIoBroker(parallelMach);
124 meshData->property_add(Ioss::Property(
"LOWER_CASE_VARIABLE_NAMES",
false));
127 std::vector<std::string> entity_rank_names = stk::mesh::entity_rank_names();
128 entity_rank_names.push_back(
"FAMILY_TREE");
129 meshData->set_rank_name_vector(entity_rank_names);
132 meshData->add_mesh_database(fileName_,
"exodusII", stk::io::READ_MESH);
134 meshData->add_mesh_database(fileName_,
"pamgen", stk::io::READ_MESH);
136 meshData->create_input_mesh();
137 RCP<stk::mesh::MetaData> metaData = meshData->meta_data_rcp();
139 RCP<STK_Interface> mesh =
rcp(
new STK_Interface(metaData));
140 mesh->initializeFromMetaData();
141 mesh->instantiateBulkData(parallelMach);
142 meshData->set_bulk_data(mesh->getBulkData());
146 meshData->add_all_mesh_fields_as_input_fields();
150 mesh->getMetaData()->declare_attribute_with_delete(meshData);
153 registerElementBlocks(*mesh,*meshData);
154 registerSidesets(*mesh);
155 registerNodesets(*mesh);
157 mesh->addPeriodicBCs(periodicBCVec_);
162 void STK_ExodusReaderFactory::completeMeshConstruction(STK_Interface & mesh,stk::ParallelMachine parallelMach)
const
164 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::completeMeshConstruction()");
170 if(not mesh.isInitialized()) {
171 const bool buildRefinementSupport = levelsOfRefinement_ > 0 ?
true :
false;
172 mesh.initialize(parallelMach,
true,buildRefinementSupport);
176 stk::mesh::MetaData & metaData = *mesh.getMetaData();
177 stk::mesh::BulkData & bulkData = *mesh.getBulkData();
178 stk::io::StkMeshIoBroker * meshData =
179 const_cast<stk::io::StkMeshIoBroker *
>(metaData.get_attribute<stk::io::StkMeshIoBroker>());
187 meshData->populate_bulk_data();
189 const bool deleteParentElements =
true;
190 if (levelsOfRefinement_ > 0)
191 mesh.refineMesh(levelsOfRefinement_,deleteParentElements);
195 if (userMeshScaling_)
197 stk::mesh::Field<double,stk::mesh::Cartesian>* coord_field =
198 metaData.get_field<stk::mesh::Field<double, stk::mesh::Cartesian> >(stk::topology::NODE_RANK,
"coordinates");
200 stk::mesh::Selector select_all_local = metaData.locally_owned_part() | metaData.globally_shared_part();
201 stk::mesh::BucketVector
const& my_node_buckets = bulkData.get_buckets(stk::topology::NODE_RANK, select_all_local);
203 int mesh_dim = mesh.getDimension();
206 const double inv_msf = 1.0/meshScaleFactor_;
207 for (
size_t i=0; i < my_node_buckets.size(); ++i)
209 stk::mesh::Bucket& b = *(my_node_buckets[i]);
210 double* coordinate_data = field_data( *coord_field, b );
212 for (
size_t j=0; j < b.size(); ++j) {
213 for (
int k=0; k < mesh_dim; ++k) {
214 coordinate_data[mesh_dim*j + k] *= inv_msf;
222 int restartIndex = restartIndex_;
224 std::pair<int,double> lastTimeStep = meshData->get_input_io_region()->get_max_time();
225 restartIndex = 1+restartIndex+lastTimeStep.first;
229 meshData->read_defined_input_fields(restartIndex);
231 mesh.buildSubcells();
232 mesh.buildLocalElementIDs();
234 if (userMeshScaling_) {
235 stk::mesh::Field<double,stk::mesh::Cartesian>* coord_field =
236 metaData.get_field<stk::mesh::Field<double, stk::mesh::Cartesian> >(stk::topology::NODE_RANK,
"coordinates");
237 std::vector< const stk::mesh::FieldBase *> fields;
238 fields.push_back(coord_field);
240 stk::mesh::communicate_field_data(bulkData, fields);
244 mesh.setInitialStateTime(meshData->get_input_io_region()->get_state_time(restartIndex));
246 mesh.setInitialStateTime(0.0);
252 this->rebalance(mesh);
260 "Error, the parameter {name=\"File Name\","
262 "\nis required in parameter (sub)list \""<< paramList->
name() <<
"\"."
270 paramList->
set<
int>(
"Restart Index", -1);
273 paramList->
set(
"File Type",
"Exodus");
275 if(!paramList->
isSublist(
"Periodic BCs"))
276 paramList->
sublist(
"Periodic BCs");
280 p_bcs.
set<
int>(
"Count", 0);
282 if(!paramList->
isParameter(
"Levels of Uniform Refinement"))
283 paramList->
set<
int>(
"Levels of Uniform Refinement", 0);
287 setMyParamList(paramList);
289 fileName_ = paramList->
get<std::string>(
"File Name");
291 restartIndex_ = paramList->
get<
int>(
"Restart Index");
294 const auto fileType = paramList->
get<std::string>(
"File Type");
295 isExodus_ = fileType ==
"Exodus";
301 meshScaleFactor_ = paramList->
get<
double>(
"Scale Factor");
302 userMeshScaling_ =
true;
306 parsePeriodicBCList(Teuchos::rcpFromRef(paramList->
sublist(
"Periodic BCs")),periodicBCVec_);
308 levelsOfRefinement_ = paramList->
get<
int>(
"Levels of Uniform Refinement");
316 if(validParams==Teuchos::null) {
318 validParams->
set<std::string>(
"File Name",
"<file name not set>",
"Name of exodus file to be read",
321 validParams->
set<
int>(
"Restart Index",-1,
"Index of solution to read in",
324 Teuchos::setStringToIntegralParameter<int>(
"File Type",
326 "Choose input file type - either \"Exodus\" or \"Pamgen\"",
327 Teuchos::tuple<std::string>(
"Exodus",
"Pamgen"),
331 validParams->
set<
double>(
"Scale Factor", 1.0,
"Scale factor to apply to mesh after read",
335 bcs.
set<
int>(
"Count",0);
337 validParams->
set(
"Levels of Uniform Refinement",0,
"Number of levels of inline uniform mesh refinement");
343 void STK_ExodusReaderFactory::registerElementBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
347 RCP<stk::mesh::MetaData> femMetaData = mesh.getMetaData();
352 const Ioss::ElementBlockContainer & elem_blocks = meshData.get_input_io_region()->get_element_blocks();
353 for(Ioss::ElementBlockContainer::const_iterator itr=elem_blocks.begin();itr!=elem_blocks.end();++itr) {
354 Ioss::GroupingEntity * entity = *itr;
355 const std::string & name = entity->name();
357 const stk::mesh::Part * part = femMetaData->get_part(name);
358 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(femMetaData->get_topology(*part));
359 const CellTopologyData * ct = cellTopo.getCellTopologyData();
362 mesh.addElementBlock(part->name(),ct);
366 template <
typename SetType>
367 void buildSetNames(
const SetType & setData,std::vector<std::string> & names)
370 for(
typename SetType::const_iterator itr=setData.begin();itr!=setData.end();++itr) {
371 Ioss::GroupingEntity * entity = *itr;
372 names.push_back(entity->name());
376 void STK_ExodusReaderFactory::registerSidesets(STK_Interface & mesh)
const
380 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
381 const stk::mesh::PartVector & parts = metaData->get_parts();
383 stk::mesh::PartVector::const_iterator partItr;
384 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
385 const stk::mesh::Part * part = *partItr;
386 const stk::mesh::PartVector & subsets = part->subsets();
387 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
388 const CellTopologyData * ct = cellTopo.getCellTopologyData();
392 if(part->primary_entity_rank()==mesh.getSideRank() && ct==0 && subsets.size()>0) {
394 "STK_ExodusReaderFactory::registerSidesets error - part \"" << part->name() <<
395 "\" has more than one subset");
398 const stk::mesh::Part * ss_part = subsets[0];
399 shards::CellTopology ss_cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*ss_part));
400 const CellTopologyData * ss_ct = ss_cellTopo.getCellTopologyData();
404 mesh.addSideset(part->name(),ss_ct);
409 void STK_ExodusReaderFactory::registerNodesets(STK_Interface & mesh)
const
413 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
414 const stk::mesh::PartVector & parts = metaData->get_parts();
416 stk::mesh::PartVector::const_iterator partItr;
417 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
418 const stk::mesh::Part * part = *partItr;
419 shards::CellTopology cellTopo = stk::mesh::get_cell_topology(metaData->get_topology(*part));
420 const CellTopologyData * ct = cellTopo.getCellTopologyData();
424 if(part->primary_entity_rank()==mesh.getNodeRank() && ct==0) {
428 mesh.addNodeset(part->name());
RCP< const T > getConst() const
const std::string & name() const
std::string currentParametersString() const
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
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)