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 STK_ExodusReaderFactory::STK_ExodusReaderFactory()
65 : fileName_(
""), restartIndex_(0), userMeshScaling_(false), meshScaleFactor_(0.0)
68 STK_ExodusReaderFactory::STK_ExodusReaderFactory(
const std::string & fileName,
int restartIndex)
69 : fileName_(fileName), restartIndex_(restartIndex), userMeshScaling_(false), meshScaleFactor_(0.0)
74 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildMesh()");
79 RCP<STK_Interface> mesh = buildUncommitedMesh(parallelMach);
85 mesh->initialize(parallelMach,
false);
87 completeMeshConstruction(*mesh,parallelMach);
98 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::buildUncomittedMesh()");
104 stk::io::StkMeshIoBroker* meshData =
new stk::io::StkMeshIoBroker(parallelMach);
105 meshData->property_add(Ioss::Property(
"LOWER_CASE_VARIABLE_NAMES",
false));
106 meshData->add_mesh_database(fileName_,
"exodusII", stk::io::READ_MESH);
107 meshData->create_input_mesh();
108 RCP<stk::mesh::MetaData> metaData = meshData->meta_data_rcp();
111 RCP<STK_Interface> mesh =
rcp(
new STK_Interface(metaData));
112 mesh->initializeFromMetaData();
113 mesh->instantiateBulkData(parallelMach);
114 meshData->set_bulk_data(mesh->getBulkData());
118 meshData->add_all_mesh_fields_as_input_fields();
122 mesh->getMetaData()->declare_attribute_with_delete(meshData);
125 registerElementBlocks(*mesh,*meshData);
126 registerSidesets(*mesh);
127 registerNodesets(*mesh);
129 mesh->addPeriodicBCs(periodicBCVec_);
134 void STK_ExodusReaderFactory::completeMeshConstruction(STK_Interface & mesh,stk::ParallelMachine parallelMach)
const
136 PANZER_FUNC_TIME_MONITOR(
"panzer::STK_ExodusReaderFactory::completeMeshConstruction()");
141 if(not mesh.isInitialized())
142 mesh.initialize(parallelMach);
145 stk::mesh::MetaData & metaData = *mesh.getMetaData();
146 stk::mesh::BulkData & bulkData = *mesh.getBulkData();
147 stk::io::StkMeshIoBroker * meshData =
148 const_cast<stk::io::StkMeshIoBroker *
>(metaData.get_attribute<stk::io::StkMeshIoBroker>());
156 meshData->populate_bulk_data();
160 if (userMeshScaling_)
162 stk::mesh::Field<double,stk::mesh::Cartesian>* coord_field =
163 metaData.get_field<stk::mesh::Field<double, stk::mesh::Cartesian> >(stk::topology::NODE_RANK,
"coordinates");
165 stk::mesh::Selector select_all_local = metaData.locally_owned_part() | metaData.globally_shared_part();
166 stk::mesh::BucketVector
const& my_node_buckets = bulkData.get_buckets(stk::topology::NODE_RANK, select_all_local);
168 int mesh_dim = mesh.getDimension();
171 const double inv_msf = 1.0/meshScaleFactor_;
172 for (
size_t i=0; i < my_node_buckets.size(); ++i)
174 stk::mesh::Bucket& b = *(my_node_buckets[i]);
175 double* coordinate_data = field_data( *coord_field, b );
177 for (
size_t j=0; j < b.size(); ++j) {
178 for (
int k=0; k < mesh_dim; ++k) {
179 coordinate_data[mesh_dim*j + k] *= inv_msf;
187 int restartIndex = restartIndex_;
189 std::pair<int,double> lastTimeStep = meshData->get_input_io_region()->get_max_time();
190 restartIndex = 1+restartIndex+lastTimeStep.first;
194 meshData->read_defined_input_fields(restartIndex);
196 mesh.buildSubcells();
197 mesh.buildLocalElementIDs();
199 if (userMeshScaling_) {
200 stk::mesh::Field<double,stk::mesh::Cartesian>* coord_field =
201 metaData.get_field<stk::mesh::Field<double, stk::mesh::Cartesian> >(stk::topology::NODE_RANK,
"coordinates");
202 std::vector< const stk::mesh::FieldBase *> fields;
203 fields.push_back(coord_field);
205 stk::mesh::communicate_field_data(bulkData, fields);
209 mesh.setInitialStateTime(meshData->get_input_io_region()->get_state_time(restartIndex));
211 mesh.setInitialStateTime(0.0);
217 this->rebalance(mesh);
225 "Error, the parameter {name=\"File Name\","
227 "\nis required in parameter (sub)list \""<< paramList->
name() <<
"\"."
235 paramList->
set<
int>(
"Restart Index", -1);
237 if(!paramList->
isSublist(
"Periodic BCs"))
238 paramList->
sublist(
"Periodic BCs");
242 p_bcs.
set<
int>(
"Count", 0);
246 setMyParamList(paramList);
248 fileName_ = paramList->
get<std::string>(
"File Name");
250 restartIndex_ = paramList->
get<
int>(
"Restart Index");
255 meshScaleFactor_ = paramList->
get<
double>(
"Scale Factor");
256 userMeshScaling_ =
true;
260 parsePeriodicBCList(Teuchos::rcpFromRef(paramList->
sublist(
"Periodic BCs")),periodicBCVec_);
268 if(validParams==Teuchos::null) {
270 validParams->
set<std::string>(
"File Name",
"<file name not set>",
"Name of exodus file to be read",
273 validParams->
set<
int>(
"Restart Index",-1,
"Index of solution to read in",
276 validParams->
set<
double>(
"Scale Factor", 1.0,
"Scale factor to apply to mesh after read",
280 bcs.
set<
int>(
"Count",0);
286 void STK_ExodusReaderFactory::registerElementBlocks(STK_Interface & mesh,stk::io::StkMeshIoBroker & meshData)
const
290 RCP<stk::mesh::MetaData> femMetaData = mesh.getMetaData();
295 const Ioss::ElementBlockContainer & elem_blocks = meshData.get_input_io_region()->get_element_blocks();
296 for(Ioss::ElementBlockContainer::const_iterator itr=elem_blocks.begin();itr!=elem_blocks.end();++itr) {
297 Ioss::GroupingEntity * entity = *itr;
298 const std::string & name = entity->name();
300 const stk::mesh::Part * part = femMetaData->get_part(name);
301 const CellTopologyData * ct = femMetaData->get_cell_topology(*part).getCellTopologyData();
304 mesh.addElementBlock(part->name(),ct);
308 template <
typename SetType>
309 void buildSetNames(
const SetType & setData,std::vector<std::string> & names)
312 for(
typename SetType::const_iterator itr=setData.begin();itr!=setData.end();++itr) {
313 Ioss::GroupingEntity * entity = *itr;
314 names.push_back(entity->name());
318 void STK_ExodusReaderFactory::registerSidesets(STK_Interface & mesh)
const
322 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
323 const stk::mesh::PartVector & parts = metaData->get_parts();
325 stk::mesh::PartVector::const_iterator partItr;
326 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
327 const stk::mesh::Part * part = *partItr;
328 const stk::mesh::PartVector & subsets = part->subsets();
330 const CellTopologyData * ct = metaData->get_cell_topology(*part).getCellTopologyData();
334 if(part->primary_entity_rank()==mesh.getSideRank() && ct==0 && subsets.size()>0) {
336 "STK_ExodusReaderFactory::registerSidesets error - part \"" << part->name() <<
337 "\" has more than one subset");
340 const stk::mesh::Part * ss_part = subsets[0];
342 const CellTopologyData * ss_ct = metaData->get_cell_topology(*ss_part).getCellTopologyData();
346 mesh.addSideset(part->name(),ss_ct);
351 void STK_ExodusReaderFactory::registerNodesets(STK_Interface & mesh)
const
355 RCP<stk::mesh::MetaData> metaData = mesh.getMetaData();
356 const stk::mesh::PartVector & parts = metaData->get_parts();
358 stk::mesh::PartVector::const_iterator partItr;
359 for(partItr=parts.begin();partItr!=parts.end();++partItr) {
360 const stk::mesh::Part * part = *partItr;
361 const CellTopologyData * ct = metaData->get_cell_topology(*part).getCellTopologyData();
365 if(part->primary_entity_rank()==mesh.getNodeRank() && ct==0) {
369 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)