Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_Interface.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #include <PanzerAdaptersSTK_config.hpp>
12 #include <Panzer_STK_Interface.hpp>
13 
14 #include <Teuchos_as.hpp>
15 #include <Teuchos_RCPStdSharedPtrConversions.hpp>
16 
17 #include <limits>
18 
19 #include <stk_mesh/base/FieldBase.hpp>
20 #include <stk_mesh/base/Comm.hpp>
21 #include <stk_mesh/base/Selector.hpp>
22 #include <stk_mesh/base/GetEntities.hpp>
23 #include <stk_mesh/base/GetBuckets.hpp>
24 #include <stk_mesh/base/MeshBuilder.hpp>
25 #include <stk_mesh/base/CreateAdjacentEntities.hpp>
26 
27 // #include <stk_rebalance/Rebalance.hpp>
28 // #include <stk_rebalance/Partition.hpp>
29 // #include <stk_rebalance/ZoltanPartition.hpp>
30 // #include <stk_rebalance_utils/RebalanceUtils.hpp>
31 
32 #include <stk_util/parallel/ParallelReduce.hpp>
33 #include <stk_util/parallel/CommSparse.hpp>
34 
35 #ifdef PANZER_HAVE_IOSS
36 #include <Ionit_Initializer.h>
37 #include <stk_io/IossBridge.hpp>
38 #include <stk_io/WriteMesh.hpp>
39 #endif
40 
41 #ifdef PANZER_HAVE_PERCEPT
42 #include <percept/PerceptMesh.hpp>
43 #include <adapt/UniformRefinerPattern.hpp>
44 #include <adapt/UniformRefiner.hpp>
45 #endif
46 
48 
49 #include <set>
50 
51 using Teuchos::RCP;
52 using Teuchos::rcp;
53 
54 namespace panzer_stk {
55 
57 ElementDescriptor::ElementDescriptor(stk::mesh::EntityId gid,const std::vector<stk::mesh::EntityId> & nodes)
58  : gid_(gid), nodes_(nodes) {}
60 
64 buildElementDescriptor(stk::mesh::EntityId elmtId,std::vector<stk::mesh::EntityId> & nodes)
65 {
66  return Teuchos::rcp(new ElementDescriptor(elmtId,nodes));
67 }
68 
69 const std::string STK_Interface::coordsString = "coordinates";
70 const std::string STK_Interface::nodesString = "nodes";
71 const std::string STK_Interface::edgesString = "edges";
72 const std::string STK_Interface::facesString = "faces";
73 const std::string STK_Interface::edgeBlockString = "edge_block";
74 const std::string STK_Interface::faceBlockString = "face_block";
75 
77  : dimension_(0), initialized_(false), currentLocalId_(0), initialStateTime_(0.0), currentStateTime_(0.0), useFieldCoordinates_(false)
78 {
79  metaData_ = rcp(new stk::mesh::MetaData());
80  metaData_->use_simple_fields();
81 }
82 
84  : dimension_(0), initialized_(false), currentLocalId_(0), initialStateTime_(0.0), currentStateTime_(0.0), useFieldCoordinates_(false)
85 {
86  metaData_ = metaData;
87  metaData_->use_simple_fields();
88 }
89 
91  : dimension_(dim), initialized_(false), currentLocalId_(0), useFieldCoordinates_(false)
92 {
93  std::vector<std::string> entity_rank_names = stk::mesh::entity_rank_names();
94  entity_rank_names.push_back("FAMILY_TREE");
95 
96  metaData_ = rcp(new stk::mesh::MetaData(dimension_,entity_rank_names));
97  metaData_->use_simple_fields();
98 
100 }
101 
102 void STK_Interface::addSideset(const std::string & name,const CellTopologyData * ctData)
103 {
106 
107  stk::mesh::Part * sideset = metaData_->get_part(name);
108  if(sideset==nullptr)
109  sideset = &metaData_->declare_part_with_topology(name,
110  stk::mesh::get_topology(shards::CellTopology(ctData), dimension_));
111  sidesets_.insert(std::make_pair(name,sideset));
112 }
113 
114 void STK_Interface::addNodeset(const std::string & name)
115 {
118 
119  stk::mesh::Part * nodeset = metaData_->get_part(name);
120  if(nodeset==nullptr) {
121  const CellTopologyData * ctData = shards::getCellTopologyData<shards::Node>();
122  nodeset = &metaData_->declare_part_with_topology(name,
123  stk::mesh::get_topology(shards::CellTopology(ctData), dimension_));
124  }
125  nodesets_.insert(std::make_pair(name,nodeset));
126 }
127 
128 void STK_Interface::addSolutionField(const std::string & fieldName,const std::string & blockId)
129 {
131  "Unknown element block \"" << blockId << "\"");
132  std::pair<std::string,std::string> key = std::make_pair(fieldName,blockId);
133 
134  // add & declare field if not already added...currently assuming linears
135  if(fieldNameToSolution_.find(key)==fieldNameToSolution_.end()) {
136  SolutionFieldType * field = metaData_->get_field<double>(stk::topology::NODE_RANK, fieldName);
137  if(field==0)
138  field = &metaData_->declare_field<double>(stk::topology::NODE_RANK, fieldName);
139  if ( initialized_ ) {
140  metaData_->enable_late_fields();
141  stk::mesh::put_field_on_mesh(*field, metaData_->universal_part(), nullptr);
142  }
144  }
145 }
146 
147 void STK_Interface::addCellField(const std::string & fieldName,const std::string & blockId)
148 {
150  "Unknown element block \"" << blockId << "\"");
151  std::pair<std::string,std::string> key = std::make_pair(fieldName,blockId);
152 
153  // add & declare field if not already added...currently assuming linears
154  if(fieldNameToCellField_.find(key)==fieldNameToCellField_.end()) {
155  SolutionFieldType * field = metaData_->get_field<double>(stk::topology::ELEMENT_RANK, fieldName);
156  if(field==0)
157  field = &metaData_->declare_field<double>(stk::topology::ELEMENT_RANK, fieldName);
158 
159  if ( initialized_ ) {
160  metaData_->enable_late_fields();
161  stk::mesh::put_field_on_mesh(*field, metaData_->universal_part(), nullptr);
162  }
164  }
165 }
166 
167 void STK_Interface::addEdgeField(const std::string & fieldName,const std::string & blockId)
168 {
170  "Unknown element block \"" << blockId << "\"");
171  std::pair<std::string,std::string> key = std::make_pair(fieldName,blockId);
172 
173  // add & declare field if not already added...currently assuming linears
174  if(fieldNameToEdgeField_.find(key)==fieldNameToEdgeField_.end()) {
175  SolutionFieldType * field = metaData_->get_field<double>(stk::topology::EDGE_RANK, fieldName);
176  if(field==0) {
177  field = &metaData_->declare_field<double>(stk::topology::EDGE_RANK, fieldName);
178  }
179 
180  if ( initialized_ ) {
181  metaData_->enable_late_fields();
182  stk::mesh::put_field_on_mesh(*field, metaData_->universal_part(), nullptr);
183  }
185  }
186 }
187 
188 void STK_Interface::addFaceField(const std::string & fieldName,const std::string & blockId)
189 {
191  "Unknown element block \"" << blockId << "\"");
192  std::pair<std::string,std::string> key = std::make_pair(fieldName,blockId);
193 
194  // add & declare field if not already added...currently assuming linears
195  if(fieldNameToFaceField_.find(key)==fieldNameToFaceField_.end()) {
196  SolutionFieldType * field = metaData_->get_field<double>(stk::topology::FACE_RANK, fieldName);
197  if(field==0) {
198  field = &metaData_->declare_field<double>(stk::topology::FACE_RANK, fieldName);
199  }
200 
201  if ( initialized_ ) {
202  metaData_->enable_late_fields();
203  stk::mesh::put_field_on_mesh(*field, metaData_->universal_part(), nullptr);
204  }
206  }
207 }
208 
209 void STK_Interface::addMeshCoordFields(const std::string & blockId,
210  const std::vector<std::string> & coordNames,
211  const std::string & dispPrefix)
212 {
214  TEUCHOS_ASSERT(dimension_==coordNames.size());
217  "Unknown element block \"" << blockId << "\"");
218 
219  // we only allow one alternative coordinate field
220  TEUCHOS_TEST_FOR_EXCEPTION(meshCoordFields_.find(blockId)!=meshCoordFields_.end(),std::invalid_argument,
221  "STK_Interface::addMeshCoordFields: Can't set more than one set of coordinate "
222  "fields for element block \""+blockId+"\".");
223 
224  // Note that there is a distinction between the key which is used for lookups
225  // and the field that lives on the mesh, which is used for printing the displacement.
226 
227  // just copy the coordinate names
228  meshCoordFields_[blockId] = coordNames;
229 
230  // must fill in the displacement fields
231  std::vector<std::string> & dispFields = meshDispFields_[blockId];
232  dispFields.resize(dimension_);
233 
234  for(unsigned i=0;i<dimension_;i++) {
235  std::pair<std::string,std::string> key = std::make_pair(coordNames[i],blockId);
236  std::string dispName = dispPrefix+coordNames[i];
237 
238  dispFields[i] = dispName; // record this field as a
239  // displacement field
240 
241  // add & declare field if not already added...currently assuming linears
242  if(fieldNameToSolution_.find(key)==fieldNameToSolution_.end()) {
243 
244  SolutionFieldType * field = metaData_->get_field<double>(stk::topology::NODE_RANK, dispName);
245  if(field==0) {
246  field = &metaData_->declare_field<double>(stk::topology::NODE_RANK, dispName);
247  }
249  }
250  }
251 }
252 
253 void STK_Interface::addInformationRecords(const std::vector<std::string> & info_records)
254 {
255  informationRecords_.insert(info_records.begin(), info_records.end());
256 }
257 
258 void STK_Interface::initialize(stk::ParallelMachine parallelMach,bool setupIO,
259  const bool buildRefinementSupport)
260 {
262  TEUCHOS_ASSERT(dimension_!=0); // no zero dimensional meshes!
263 
264  if(mpiComm_==Teuchos::null)
265  mpiComm_ = getSafeCommunicator(parallelMach);
266 
267  procRank_ = stk::parallel_machine_rank(*mpiComm_->getRawMpiComm());
268 
269  // associating the field with a part: universal part!
270  stk::mesh::put_field_on_mesh( *coordinatesField_ , metaData_->universal_part(), getDimension(), nullptr);
271  stk::mesh::put_field_on_mesh( *edgesField_ , metaData_->universal_part(), getDimension(), nullptr);
272  if (dimension_ > 2)
273  stk::mesh::put_field_on_mesh( *facesField_ , metaData_->universal_part(), getDimension(), nullptr);
274  stk::mesh::put_field_on_mesh( *processorIdField_ , metaData_->universal_part(), nullptr);
275  stk::mesh::put_field_on_mesh( *loadBalField_ , metaData_->universal_part(), nullptr);
276 
281 
282 #ifdef PANZER_HAVE_IOSS
283  if(setupIO) {
284  // setup Exodus file IO
286 
287  // add element blocks
288  {
289  std::map<std::string, stk::mesh::Part*>::iterator itr;
290  for(itr=elementBlocks_.begin();itr!=elementBlocks_.end();++itr)
291  if(!stk::io::is_part_io_part(*itr->second))
292  stk::io::put_io_part_attribute(*itr->second); // this can only be called once per part
293  }
294 
295  // add edge blocks
296  {
297  std::map<std::string, stk::mesh::Part*>::iterator itr;
298  for(itr=edgeBlocks_.begin();itr!=edgeBlocks_.end();++itr)
299  if(!stk::io::is_part_edge_block_io_part(*itr->second))
300  stk::io::put_edge_block_io_part_attribute(*itr->second); // this can only be called once per part
301  }
302 
303  // add face blocks
304  {
305  std::map<std::string, stk::mesh::Part*>::iterator itr;
306  for(itr=faceBlocks_.begin();itr!=faceBlocks_.end();++itr)
307  if(!stk::io::is_part_face_block_io_part(*itr->second))
308  stk::io::put_face_block_io_part_attribute(*itr->second); // this can only be called once per part
309  }
310 
311  // add side sets
312  {
313  std::map<std::string, stk::mesh::Part*>::iterator itr;
314  for(itr=sidesets_.begin();itr!=sidesets_.end();++itr)
315  if(!stk::io::is_part_io_part(*itr->second))
316  stk::io::put_io_part_attribute(*itr->second); // this can only be called once per part
317  }
318 
319  // add node sets
320  {
321  std::map<std::string, stk::mesh::Part*>::iterator itr;
322  for(itr=nodesets_.begin();itr!=nodesets_.end();++itr)
323  if(!stk::io::is_part_io_part(*itr->second))
324  stk::io::put_io_part_attribute(*itr->second); // this can only be called once per part
325  }
326 
327  // add nodes
328  if(!stk::io::is_part_io_part(*nodesPart_))
329  stk::io::put_io_part_attribute(*nodesPart_);
330 
331  stk::io::set_field_role(*coordinatesField_, Ioss::Field::MESH);
332  stk::io::set_field_output_type(*coordinatesField_, stk::io::FieldOutputType::VECTOR_3D);
333  stk::io::set_field_role(*edgesField_, Ioss::Field::MESH);
334  stk::io::set_field_output_type(*edgesField_, stk::io::FieldOutputType::VECTOR_3D);
335  if (dimension_ > 2) {
336  stk::io::set_field_role(*facesField_, Ioss::Field::MESH);
337  stk::io::set_field_output_type(*facesField_, stk::io::FieldOutputType::VECTOR_3D);
338  }
339  stk::io::set_field_role(*processorIdField_, Ioss::Field::TRANSIENT);
340  // stk::io::set_field_role(*loadBalField_, Ioss::Field::TRANSIENT);
341 
342  }
343 #endif
344 
345  if (buildRefinementSupport) {
346 #ifdef PANZER_HAVE_PERCEPT
347  refinedMesh_ = Teuchos::rcp(new percept::PerceptMesh(this->getMetaData().get(),
348  this->getBulkData().get(),
349  true));
350 
351  breakPattern_ = Teuchos::rcp(new percept::URP_Heterogeneous_3D(*refinedMesh_));
352 #else
353  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
354  "ERROR: Uniform refinement requested. This requires the Percept package to be enabled in Trilinos!");
355 #endif
356  }
357 
358  if(bulkData_==Teuchos::null)
359  instantiateBulkData(*mpiComm_->getRawMpiComm());
360 
361  metaData_->commit();
362 
363  initialized_ = true;
364 }
365 
366 void STK_Interface::initializeFieldsInSTK(const std::map<std::pair<std::string,std::string>,SolutionFieldType*> & nameToField,
367  bool setupIO)
368 {
369  std::set<SolutionFieldType*> uniqueFields;
370  std::map<std::pair<std::string,std::string>,SolutionFieldType*>::const_iterator fieldIter;
371  for(fieldIter=nameToField.begin();fieldIter!=nameToField.end();++fieldIter)
372  uniqueFields.insert(fieldIter->second); // this makes setting up IO easier!
373 
374  {
375  std::set<SolutionFieldType*>::const_iterator uniqueFieldIter;
376  for(uniqueFieldIter=uniqueFields.begin();uniqueFieldIter!=uniqueFields.end();++uniqueFieldIter)
377  stk::mesh::put_field_on_mesh(*(*uniqueFieldIter),metaData_->universal_part(),nullptr);
378  }
379 
380 #ifdef PANZER_HAVE_IOSS
381  if(setupIO) {
382  // add solution fields
383  std::set<SolutionFieldType*>::const_iterator uniqueFieldIter;
384  for(uniqueFieldIter=uniqueFields.begin();uniqueFieldIter!=uniqueFields.end();++uniqueFieldIter)
385  stk::io::set_field_role(*(*uniqueFieldIter), Ioss::Field::TRANSIENT);
386  }
387 #endif
388 }
389 
390 void STK_Interface::instantiateBulkData(stk::ParallelMachine parallelMach)
391 {
392  TEUCHOS_ASSERT(bulkData_==Teuchos::null);
393  if(mpiComm_==Teuchos::null)
394  mpiComm_ = getSafeCommunicator(parallelMach);
395 
396  std::unique_ptr<stk::mesh::BulkData> bulkUPtr = stk::mesh::MeshBuilder(*mpiComm_->getRawMpiComm()).create(Teuchos::get_shared_ptr(metaData_));
397  bulkData_ = rcp(bulkUPtr.release());
398 }
399 
401 {
402  TEUCHOS_TEST_FOR_EXCEPTION(bulkData_==Teuchos::null,std::logic_error,
403  "STK_Interface: Must call \"initialized\" or \"instantiateBulkData\" before \"beginModification\"");
404 
405  bulkData_->modification_begin();
406 }
407 
409 {
410  TEUCHOS_TEST_FOR_EXCEPTION(bulkData_==Teuchos::null,std::logic_error,
411  "STK_Interface: Must call \"initialized\" or \"instantiateBulkData\" before \"endModification\"");
412 
413  // TODO: Resolving sharing this way comes at a cost in performance. The STK
414  // team has decided that users need to declare their own sharing. We should
415  // find where shared entities are being created in Panzer and declare it.
416  // Once this is done, the extra code below can be deleted.
417 
418  stk::CommSparse comm(bulkData_->parallel());
419 
420  for (int phase=0;phase<2;++phase) {
421  for (int i=0;i<bulkData_->parallel_size();++i) {
422  if ( i != bulkData_->parallel_rank() ) {
423  const stk::mesh::BucketVector& buckets = bulkData_->buckets(stk::topology::NODE_RANK);
424  for (size_t j=0;j<buckets.size();++j) {
425  const stk::mesh::Bucket& bucket = *buckets[j];
426  if ( bucket.owned() ) {
427  for (size_t k=0;k<bucket.size();++k) {
428  stk::mesh::EntityKey key = bulkData_->entity_key(bucket[k]);
429  comm.send_buffer(i).pack<stk::mesh::EntityKey>(key);
430  }
431  }
432  }
433  }
434  }
435 
436  if (phase == 0 ) {
437  comm.allocate_buffers();
438  }
439  else {
440  comm.communicate();
441  }
442  }
443 
444  for (int i=0;i<bulkData_->parallel_size();++i) {
445  if ( i != bulkData_->parallel_rank() ) {
446  while(comm.recv_buffer(i).remaining()) {
447  stk::mesh::EntityKey key;
448  comm.recv_buffer(i).unpack<stk::mesh::EntityKey>(key);
449  stk::mesh::Entity node = bulkData_->get_entity(key);
450  if ( bulkData_->is_valid(node) ) {
451  bulkData_->add_node_sharing(node, i);
452  }
453  }
454  }
455  }
456 
457 
458  bulkData_->modification_end();
459 
462 }
463 
464 void STK_Interface::addNode(stk::mesh::EntityId gid, const std::vector<double> & coord)
465 {
466  TEUCHOS_TEST_FOR_EXCEPTION(not isModifiable(),std::logic_error,
467  "STK_Interface::addNode: STK_Interface must be modifiable to add a node");
468  TEUCHOS_TEST_FOR_EXCEPTION(coord.size()!=getDimension(),std::logic_error,
469  "STK_Interface::addNode: number of coordinates in vector must mation dimension");
470  TEUCHOS_TEST_FOR_EXCEPTION(gid==0,std::logic_error,
471  "STK_Interface::addNode: STK has STUPID restriction of no zero GIDs, pick something else");
472  stk::mesh::EntityRank nodeRank = getNodeRank();
473 
474  stk::mesh::Entity node = bulkData_->declare_entity(nodeRank,gid,nodesPartVec_);
475 
476  // set coordinate vector
477  double * fieldCoords = stk::mesh::field_data(*coordinatesField_,node);
478  for(std::size_t i=0;i<coord.size();++i)
479  fieldCoords[i] = coord[i];
480 }
481 
482 void STK_Interface::addEntityToSideset(stk::mesh::Entity entity,stk::mesh::Part * sideset)
483 {
484  std::vector<stk::mesh::Part*> sidesetV;
485  sidesetV.push_back(sideset);
486 
487  bulkData_->change_entity_parts(entity,sidesetV);
488 }
489 
490 void STK_Interface::addEntityToNodeset(stk::mesh::Entity entity,stk::mesh::Part * nodeset)
491 {
492  std::vector<stk::mesh::Part*> nodesetV;
493  nodesetV.push_back(nodeset);
494 
495  bulkData_->change_entity_parts(entity,nodesetV);
496 }
497 
498 void STK_Interface::addEntityToEdgeBlock(stk::mesh::Entity entity,stk::mesh::Part * edgeblock)
499 {
500  std::vector<stk::mesh::Part*> edgeblockV;
501  edgeblockV.push_back(edgeblock);
502 
503  bulkData_->change_entity_parts(entity,edgeblockV);
504 }
505 void STK_Interface::addEntitiesToEdgeBlock(std::vector<stk::mesh::Entity> entities,stk::mesh::Part * edgeblock)
506 {
507  if (entities.size() > 0) {
508  std::vector<stk::mesh::Part*> edgeblockV;
509  edgeblockV.push_back(edgeblock);
510 
511  bulkData_->change_entity_parts(entities,edgeblockV);
512  }
513 }
514 
515 void STK_Interface::addEntityToFaceBlock(stk::mesh::Entity entity,stk::mesh::Part * faceblock)
516 {
517  std::vector<stk::mesh::Part*> faceblockV;
518  faceblockV.push_back(faceblock);
519 
520  bulkData_->change_entity_parts(entity,faceblockV);
521 }
522 void STK_Interface::addEntitiesToFaceBlock(std::vector<stk::mesh::Entity> entities,stk::mesh::Part * faceblock)
523 {
524  if (entities.size() > 0) {
525  std::vector<stk::mesh::Part*> faceblockV;
526  faceblockV.push_back(faceblock);
527 
528  bulkData_->change_entity_parts(entities,faceblockV);
529  }
530 }
531 
532 void STK_Interface::addElement(const Teuchos::RCP<ElementDescriptor> & ed,stk::mesh::Part * block)
533 {
534  std::vector<stk::mesh::Part*> blockVec;
535  blockVec.push_back(block);
536 
537  stk::mesh::EntityRank elementRank = getElementRank();
538  stk::mesh::EntityRank nodeRank = getNodeRank();
539  stk::mesh::Entity element = bulkData_->declare_entity(elementRank,ed->getGID(),blockVec);
540 
541  // build relations that give the mesh structure
542  const std::vector<stk::mesh::EntityId> & nodes = ed->getNodes();
543  for(std::size_t i=0;i<nodes.size();++i) {
544  // add element->node relation
545  stk::mesh::Entity node = bulkData_->get_entity(nodeRank,nodes[i]);
546  TEUCHOS_ASSERT(bulkData_->is_valid(node));
547  bulkData_->declare_relation(element,node,i);
548  }
549 
550  ProcIdData * procId = stk::mesh::field_data(*processorIdField_,element);
551  procId[0] = Teuchos::as<ProcIdData>(procRank_);
552 }
553 
554 
556 {
557  // loop over elements
558  stk::mesh::EntityRank edgeRank = getEdgeRank();
559  std::vector<stk::mesh::Entity> localElmts;
560  getMyElements(localElmts);
561  std::vector<stk::mesh::Entity>::const_iterator itr;
562  for(itr=localElmts.begin();itr!=localElmts.end();++itr) {
563  stk::mesh::Entity element = (*itr);
564  stk::mesh::EntityId gid = bulkData_->identifier(element);
565  std::vector<stk::mesh::EntityId> subcellIds;
566  getSubcellIndices(edgeRank,gid,subcellIds);
567 
568  for(std::size_t i=0;i<subcellIds.size();++i) {
569  stk::mesh::Entity edge = bulkData_->get_entity(edgeRank,subcellIds[i]);
570  stk::mesh::Entity const* relations = bulkData_->begin_nodes(edge);
571 
572  double * node_coord_1 = stk::mesh::field_data(*coordinatesField_,relations[0]);
573  double * node_coord_2 = stk::mesh::field_data(*coordinatesField_,relations[1]);
574 
575  // set coordinate vector
576  double * edgeCoords = stk::mesh::field_data(*edgesField_,edge);
577  for(std::size_t j=0;j<getDimension();++j)
578  edgeCoords[j] = (node_coord_1[j]+node_coord_2[j])/2.0;
579  }
580  }
581 }
582 
584 {
585  // loop over elements
586  stk::mesh::EntityRank faceRank = getFaceRank();
587  std::vector<stk::mesh::Entity> localElmts;
588  getMyElements(localElmts);
589  std::vector<stk::mesh::Entity>::const_iterator itr;
590  for(itr=localElmts.begin();itr!=localElmts.end();++itr) {
591  stk::mesh::Entity element = (*itr);
592  stk::mesh::EntityId gid = elementGlobalId(element);
593  std::vector<stk::mesh::EntityId> subcellIds;
594  getSubcellIndices(faceRank,gid,subcellIds);
595 
596  for(std::size_t i=0;i<subcellIds.size();++i) {
597  stk::mesh::Entity face = bulkData_->get_entity(faceRank,subcellIds[i]);
598  stk::mesh::Entity const* relations = bulkData_->begin_nodes(face);
599  const size_t num_relations = bulkData_->num_nodes(face);
600 
601  // set coordinate vector
602  double * faceCoords = stk::mesh::field_data(*facesField_,face);
603  for(std::size_t j=0;j<getDimension();++j){
604  faceCoords[j] = 0.0;
605  for(std::size_t k=0;k<num_relations;++k)
606  faceCoords[j] += stk::mesh::field_data(*coordinatesField_,relations[k])[j];
607  faceCoords[j] /= double(num_relations);
608  }
609  }
610  }
611 }
612 
613 stk::mesh::Entity STK_Interface::findConnectivityById(stk::mesh::Entity src, stk::mesh::EntityRank tgt_rank, unsigned rel_id) const
614 {
615  const size_t num_rels = bulkData_->num_connectivity(src, tgt_rank);
616  stk::mesh::Entity const* relations = bulkData_->begin(src, tgt_rank);
617  stk::mesh::ConnectivityOrdinal const* ordinals = bulkData_->begin_ordinals(src, tgt_rank);
618  for (size_t i = 0; i < num_rels; ++i) {
619  if (ordinals[i] == static_cast<stk::mesh::ConnectivityOrdinal>(rel_id)) {
620  return relations[i];
621  }
622  }
623 
624  return stk::mesh::Entity();
625 }
626 
628 //
629 // writeToExodus()
630 //
632 void
634 writeToExodus(const std::string& filename,
635  const bool append)
636 {
637  setupExodusFile(filename,append);
638  writeToExodus(0.0);
639 } // end of writeToExodus()
640 
642 //
643 // setupExodusFile()
644 //
646 void
648 setupExodusFile(const std::string& filename,
649  const bool append,
650  const bool append_after_restart_time,
651  const double restart_time)
652 {
653  using std::runtime_error;
654  using stk::io::StkMeshIoBroker;
655  using stk::mesh::FieldVector;
656  using stk::ParallelMachine;
657  using Teuchos::rcp;
658  PANZER_FUNC_TIME_MONITOR("STK_Interface::setupExodusFile(filename)");
659 #ifdef PANZER_HAVE_IOSS
661 
662  ParallelMachine comm = *mpiComm_->getRawMpiComm();
663  meshData_ = rcp(new StkMeshIoBroker(comm));
664  meshData_->set_bulk_data(Teuchos::get_shared_ptr(bulkData_));
665  Ioss::PropertyManager props;
666  props.add(Ioss::Property("LOWER_CASE_VARIABLE_NAMES", "FALSE"));
667  if (append) {
668  if (append_after_restart_time) {
669  meshIndex_ = meshData_->create_output_mesh(filename, stk::io::APPEND_RESULTS,
670  props, restart_time);
671  }
672  else // Append results to the end of the file
673  meshIndex_ = meshData_->create_output_mesh(filename, stk::io::APPEND_RESULTS, props);
674  }
675  else
676  meshIndex_ = meshData_->create_output_mesh(filename, stk::io::WRITE_RESULTS, props);
677  const FieldVector& fields = metaData_->get_fields();
678  for (size_t i(0); i < fields.size(); ++i) {
679  // Do NOT add MESH type stk fields to exodus io, but do add everything
680  // else. This allows us to avoid having to catch a throw for
681  // re-registering coordinates, sidesets, etc... Note that some
682  // fields like LOAD_BAL don't always have a role assigned, so for
683  // roles that point to null, we need to register them as well.
684  auto role = stk::io::get_field_role(*fields[i]);
685  if (role != nullptr) {
686  if (*role != Ioss::Field::MESH)
687  meshData_->add_field(meshIndex_, *fields[i]);
688  } else {
689  meshData_->add_field(meshIndex_, *fields[i]);
690  }
691  }
692 
693  // convert the set to a vector
694  std::vector<std::string> deduped_info_records(informationRecords_.begin(),informationRecords_.end());
695  meshData_->add_info_records(meshIndex_, deduped_info_records);
696 #else
697  TEUCHOS_ASSERT(false)
698 #endif
699 } // end of setupExodusFile()
700 
702 //
703 // writeToExodus()
704 //
706 void
709  double timestep)
710 {
711  using std::cout;
712  using std::endl;
713  using Teuchos::FancyOStream;
714  using Teuchos::rcpFromRef;
715  PANZER_FUNC_TIME_MONITOR("STK_Interface::writeToExodus(timestep)");
716 #ifdef PANZER_HAVE_IOSS
717  if (not meshData_.is_null())
718  {
719  currentStateTime_ = timestep;
720  globalToExodus(GlobalVariable::ADD);
721  meshData_->begin_output_step(meshIndex_, currentStateTime_);
722  meshData_->write_defined_output_fields(meshIndex_);
723  globalToExodus(GlobalVariable::WRITE);
724  meshData_->end_output_step(meshIndex_);
725  }
726  else // if (meshData_.is_null())
727  {
728  FancyOStream out(rcpFromRef(cout));
729  out.setOutputToRootOnly(0);
730  out << "WARNING: Exodus I/O has been disabled or not setup properly; "
731  << "not writing to Exodus." << endl;
732  } // end if (meshData_.is_null()) or not
733 #else
734  TEUCHOS_ASSERT(false);
735 #endif
736 } // end of writeToExodus()
737 
739 //
740 // globalToExodus()
741 //
743 void
744 STK_Interface::
745 globalToExodus(
746  const GlobalVariable& flag)
747 {
748  using std::invalid_argument;
749  using std::string;
750  using Teuchos::Array;
751 
752  // Loop over all the global variables to be added to the Exodus output file.
753  // For each global variable, we determine the data type, and then add or
754  // write it accordingly, depending on the value of flag.
755  for (auto i = globalData_.begin(); i != globalData_.end(); ++i)
756  {
757  const string& name = globalData_.name(i);
758 
759  // Integers.
760  if (globalData_.isType<int>(name))
761  {
762  const auto& value = globalData_.get<int>(name);
763  if (flag == GlobalVariable::ADD)
764  {
765  try
766  {
767  meshData_->add_global(meshIndex_, name, value,
768  stk::util::ParameterType::INTEGER);
769  }
770  catch (...)
771  {
772  return;
773  }
774  }
775  else // if (flag == GlobalVariable::WRITE)
776  meshData_->write_global(meshIndex_, name, value,
777  stk::util::ParameterType::INTEGER);
778  }
779 
780  // Doubles.
781  else if (globalData_.isType<double>(name))
782  {
783  const auto& value = globalData_.get<double>(name);
784  if (flag == GlobalVariable::ADD)
785  {
786  try
787  {
788  meshData_->add_global(meshIndex_, name, value,
789  stk::util::ParameterType::DOUBLE);
790  }
791  catch (...)
792  {
793  return;
794  }
795  }
796  else // if (flag == GlobalVariable::WRITE)
797  meshData_->write_global(meshIndex_, name, value,
798  stk::util::ParameterType::DOUBLE);
799  }
800 
801  // Vectors of integers.
802  else if (globalData_.isType<Array<int>>(name))
803  {
804  const auto& value = globalData_.get<Array<int>>(name).toVector();
805  if (flag == GlobalVariable::ADD)
806  {
807  try
808  {
809  meshData_->add_global(meshIndex_, name, value,
810  stk::util::ParameterType::INTEGERVECTOR);
811  }
812  catch (...)
813  {
814  return;
815  }
816  }
817  else // if (flag == GlobalVariable::WRITE)
818  meshData_->write_global(meshIndex_, name, value,
819  stk::util::ParameterType::INTEGERVECTOR);
820  }
821 
822  // Vectors of doubles.
823  else if (globalData_.isType<Array<double>>(name))
824  {
825  const auto& value = globalData_.get<Array<double>>(name).toVector();
826  if (flag == GlobalVariable::ADD)
827  {
828  try
829  {
830  meshData_->add_global(meshIndex_, name, value,
831  stk::util::ParameterType::DOUBLEVECTOR);
832  }
833  catch (...)
834  {
835  return;
836  }
837  }
838  else // if (flag == GlobalVariable::WRITE)
839  meshData_->write_global(meshIndex_, name, value,
840  stk::util::ParameterType::DOUBLEVECTOR);
841  }
842 
843  // If the data type is something else, throw an exception.
844  else
845  TEUCHOS_TEST_FOR_EXCEPTION(true, invalid_argument,
846  "STK_Interface::globalToExodus(): The global variable to be added " \
847  "to the Exodus output file is of an invalid type. Valid types are " \
848  "int and double, along with std::vectors of those types.")
849  } // end loop over globalData_
850 } // end of globalToExodus()
851 
853 //
854 // addGlobalToExodus()
855 //
857 void
860  const std::string& key,
861  const int& value)
862 {
863  globalData_.set(key, value);
864 } // end of addGlobalToExodus()
865 
867 //
868 // addGlobalToExodus()
869 //
871 void
874  const std::string& key,
875  const double& value)
876 {
877  globalData_.set(key, value);
878 } // end of addGlobalToExodus()
879 
881 //
882 // addGlobalToExodus()
883 //
885 void
888  const std::string& key,
889  const std::vector<int>& value)
890 {
891  using Teuchos::Array;
892  globalData_.set(key, Array<int>(value));
893 } // end of addGlobalToExodus()
894 
896 //
897 // addGlobalToExodus()
898 //
900 void
903  const std::string& key,
904  const std::vector<double>& value)
905 {
906  using Teuchos::Array;
907  globalData_.set(key, Array<double>(value));
908 } // end of addGlobalToExodus()
909 
911 {
912  #ifdef PANZER_HAVE_IOSS
913  return true;
914  #else
915  return false;
916  #endif
917 }
918 
919 void STK_Interface::getElementsSharingNode(stk::mesh::EntityId nodeId,std::vector<stk::mesh::Entity> & elements) const
920 {
921  stk::mesh::EntityRank nodeRank = getNodeRank();
922 
923  // get all relations for node
924  stk::mesh::Entity node = bulkData_->get_entity(nodeRank,nodeId);
925  const size_t numElements = bulkData_->num_elements(node);
926  stk::mesh::Entity const* relations = bulkData_->begin_elements(node);
927 
928  // extract elements sharing nodes
929  elements.insert(elements.end(), relations, relations + numElements);
930 }
931 
932 void STK_Interface::getOwnedElementsSharingNode(stk::mesh::Entity node,std::vector<stk::mesh::Entity> & elements,
933  std::vector<int> & relIds) const
934 {
935  // get all relations for node
936  const size_t numElements = bulkData_->num_elements(node);
937  stk::mesh::Entity const* relations = bulkData_->begin_elements(node);
938  stk::mesh::ConnectivityOrdinal const* rel_ids = bulkData_->begin_element_ordinals(node);
939 
940  // extract elements sharing nodes
941  for (size_t i = 0; i < numElements; ++i) {
942  stk::mesh::Entity element = relations[i];
943 
944  // if owned by this processor
945  if(bulkData_->parallel_owner_rank(element) == static_cast<int>(procRank_)) {
946  elements.push_back(element);
947  relIds.push_back(rel_ids[i]);
948  }
949  }
950 }
951 
952 void STK_Interface::getOwnedElementsSharingNode(stk::mesh::EntityId nodeId,std::vector<stk::mesh::Entity> & elements,
953  std::vector<int> & relIds, unsigned int matchType) const
954 {
955  stk::mesh::EntityRank rank;
956  if(matchType == 0)
957  rank = getNodeRank();
958  else if(matchType == 1)
959  rank = getEdgeRank();
960  else if(matchType == 2)
961  rank = getFaceRank();
962  else
963  TEUCHOS_ASSERT(false);
964 
965  stk::mesh::Entity node = bulkData_->get_entity(rank,nodeId);
966 
967  getOwnedElementsSharingNode(node,elements,relIds);
968 }
969 
970 void STK_Interface::getElementsSharingNodes(const std::vector<stk::mesh::EntityId> nodeIds,std::vector<stk::mesh::Entity> & elements) const
971 {
972  std::vector<stk::mesh::Entity> current;
973 
974  getElementsSharingNode(nodeIds[0],current); // fill it with elements touching first node
975  std::sort(current.begin(),current.end()); // sort for intersection on the pointer
976 
977  // find intersection with remaining nodes
978  for(std::size_t n=1;n<nodeIds.size();++n) {
979  // get elements associated with next node
980  std::vector<stk::mesh::Entity> nextNode;
981  getElementsSharingNode(nodeIds[n],nextNode); // fill it with elements touching first node
982  std::sort(nextNode.begin(),nextNode.end()); // sort for intersection on the pointer ID
983 
984  // intersect next node elements with current element list
985  std::vector<stk::mesh::Entity> intersection(std::min(nextNode.size(),current.size()));
986  std::vector<stk::mesh::Entity>::const_iterator endItr
987  = std::set_intersection(current.begin(),current.end(),
988  nextNode.begin(),nextNode.end(),
989  intersection.begin());
990  std::size_t newLength = endItr-intersection.begin();
991  intersection.resize(newLength);
992 
993  // store intersection
994  current.clear();
995  current = intersection;
996  }
997 
998  // return the elements computed
999  elements = current;
1000 }
1001 
1002 void STK_Interface::getNodeIdsForElement(stk::mesh::Entity element,std::vector<stk::mesh::EntityId> & nodeIds) const
1003 {
1004  stk::mesh::Entity const* nodeRel = getBulkData()->begin_nodes(element);
1005  const size_t numNodes = getBulkData()->num_nodes(element);
1006 
1007  nodeIds.reserve(numNodes);
1008  for(size_t i = 0; i < numNodes; ++i) {
1009  nodeIds.push_back(elementGlobalId(nodeRel[i]));
1010  }
1011 }
1012 
1014 {
1015  entityCounts_.clear();
1016  stk::mesh::comm_mesh_counts(*bulkData_,entityCounts_);
1017 }
1018 
1020 {
1021  // developed to mirror "comm_mesh_counts" in stk_mesh/base/Comm.cpp
1022 
1023  const auto entityRankCount = metaData_->entity_rank_count();
1024  const size_t commCount = 10; // entityRankCount
1025 
1026  TEUCHOS_ASSERT(entityRankCount<10);
1027 
1028  // stk::ParallelMachine mach = bulkData_->parallel();
1029  stk::ParallelMachine mach = *mpiComm_->getRawMpiComm();
1030 
1031  std::vector<stk::mesh::EntityId> local(commCount,0);
1032 
1033  // determine maximum ID for this processor for each entity type
1034  stk::mesh::Selector ownedPart = metaData_->locally_owned_part();
1035  for(stk::mesh::EntityRank i=stk::topology::NODE_RANK;
1036  i < static_cast<stk::mesh::EntityRank>(entityRankCount); ++i) {
1037  std::vector<stk::mesh::Entity> entities;
1038 
1039  stk::mesh::get_selected_entities(ownedPart,bulkData_->buckets(i),entities);
1040 
1041  // determine maximum ID for this processor
1042  std::vector<stk::mesh::Entity>::const_iterator itr;
1043  for(itr=entities.begin();itr!=entities.end();++itr) {
1044  stk::mesh::EntityId id = bulkData_->identifier(*itr);
1045  if(id>local[i])
1046  local[i] = id;
1047  }
1048  }
1049 
1050  // get largest IDs across processors
1051  stk::all_reduce(mach,stk::ReduceMax<10>(&local[0]));
1052  maxEntityId_.assign(local.begin(),local.begin()+entityRankCount+1);
1053 }
1054 
1055 std::size_t STK_Interface::getEntityCounts(unsigned entityRank) const
1056 {
1057  TEUCHOS_TEST_FOR_EXCEPTION(entityRank>=entityCounts_.size(),std::logic_error,
1058  "STK_Interface::getEntityCounts: Entity counts do not include rank: " << entityRank);
1059 
1060  return entityCounts_[entityRank];
1061 }
1062 
1063 stk::mesh::EntityId STK_Interface::getMaxEntityId(unsigned entityRank) const
1064 {
1065  TEUCHOS_TEST_FOR_EXCEPTION(entityRank>=maxEntityId_.size(),std::logic_error,
1066  "STK_Interface::getMaxEntityId: Max entity ids do not include rank: " << entityRank);
1067 
1068  return maxEntityId_[entityRank];
1069 }
1070 
1072 {
1073  stk::mesh::PartVector emptyPartVector;
1074  stk::mesh::create_adjacent_entities(*bulkData_,emptyPartVector);
1075 
1078 
1079  addEdges();
1080  if (dimension_ > 2)
1081  addFaces();
1082 }
1083 
1084 const double * STK_Interface::getNodeCoordinates(stk::mesh::EntityId nodeId) const
1085 {
1086  stk::mesh::Entity node = bulkData_->get_entity(getNodeRank(),nodeId);
1087  return stk::mesh::field_data(*coordinatesField_,node);
1088 }
1089 
1090 const double * STK_Interface::getNodeCoordinates(stk::mesh::Entity node) const
1091 {
1092  return stk::mesh::field_data(*coordinatesField_,node);
1093 }
1094 
1095 void STK_Interface::getSubcellIndices(unsigned entityRank,stk::mesh::EntityId elementId,
1096  std::vector<stk::mesh::EntityId> & subcellIds) const
1097 {
1098  stk::mesh::EntityRank elementRank = getElementRank();
1099  stk::mesh::Entity cell = bulkData_->get_entity(elementRank,elementId);
1100 
1101  TEUCHOS_TEST_FOR_EXCEPTION(!bulkData_->is_valid(cell),std::logic_error,
1102  "STK_Interface::getSubcellIndices: could not find element requested (GID = " << elementId << ")");
1103 
1104  const size_t numSubcells = bulkData_->num_connectivity(cell, static_cast<stk::mesh::EntityRank>(entityRank));
1105  stk::mesh::Entity const* subcells = bulkData_->begin(cell, static_cast<stk::mesh::EntityRank>(entityRank));
1106  subcellIds.clear();
1107  subcellIds.resize(numSubcells,0);
1108 
1109  // loop over relations and fill subcell vector
1110  for(size_t i = 0; i < numSubcells; ++i) {
1111  stk::mesh::Entity subcell = subcells[i];
1112  subcellIds[i] = bulkData_->identifier(subcell);
1113  }
1114 }
1115 
1116 void STK_Interface::getMyElements(std::vector<stk::mesh::Entity> & elements) const
1117 {
1118  // setup local ownership
1119  stk::mesh::Selector ownedPart = metaData_->locally_owned_part();
1120 
1121  // grab elements
1122  stk::mesh::EntityRank elementRank = getElementRank();
1123  stk::mesh::get_selected_entities(ownedPart,bulkData_->buckets(elementRank),elements);
1124 }
1125 
1126 void STK_Interface::getMyElements(const std::string & blockID,std::vector<stk::mesh::Entity> & elements) const
1127 {
1128  stk::mesh::Part * elementBlock = getElementBlockPart(blockID);
1129 
1130  TEUCHOS_TEST_FOR_EXCEPTION(elementBlock==0,std::logic_error,"Could not find element block \"" << blockID << "\"");
1131 
1132  // setup local ownership
1133  // stk::mesh::Selector block = *elementBlock;
1134  stk::mesh::Selector ownedBlock = metaData_->locally_owned_part() & (*elementBlock);
1135 
1136  // grab elements
1137  stk::mesh::EntityRank elementRank = getElementRank();
1138  stk::mesh::get_selected_entities(ownedBlock,bulkData_->buckets(elementRank),elements);
1139 }
1140 
1141 void STK_Interface::getNeighborElements(std::vector<stk::mesh::Entity> & elements) const
1142 {
1143  // setup local ownership
1144  stk::mesh::Selector neighborBlock = (!metaData_->locally_owned_part());
1145 
1146  // grab elements
1147  stk::mesh::EntityRank elementRank = getElementRank();
1148  stk::mesh::get_selected_entities(neighborBlock,bulkData_->buckets(elementRank),elements);
1149 }
1150 
1151 void STK_Interface::getNeighborElements(const std::string & blockID,std::vector<stk::mesh::Entity> & elements) const
1152 {
1153  stk::mesh::Part * elementBlock = getElementBlockPart(blockID);
1154 
1155  TEUCHOS_TEST_FOR_EXCEPTION(elementBlock==0,std::logic_error,"Could not find element block \"" << blockID << "\"");
1156 
1157  // setup local ownership
1158  stk::mesh::Selector neighborBlock = (!metaData_->locally_owned_part()) & (*elementBlock);
1159 
1160  // grab elements
1161  stk::mesh::EntityRank elementRank = getElementRank();
1162  stk::mesh::get_selected_entities(neighborBlock,bulkData_->buckets(elementRank),elements);
1163 }
1164 
1165 void STK_Interface::getMyEdges(std::vector<stk::mesh::Entity> & edges) const
1166 {
1167  // setup local ownership
1168  stk::mesh::Selector ownedPart = metaData_->locally_owned_part();
1169 
1170  // grab elements
1171  stk::mesh::get_selected_entities(ownedPart,bulkData_->buckets(getEdgeRank()),edges);
1172 }
1173 
1174 void STK_Interface::getMyEdges(const std::string & edgeBlockName,std::vector<stk::mesh::Entity> & edges) const
1175 {
1176  stk::mesh::Part * edgeBlockPart = getEdgeBlock(edgeBlockName);
1177  TEUCHOS_TEST_FOR_EXCEPTION(edgeBlockPart==0,std::logic_error,
1178  "Unknown edge block \"" << edgeBlockName << "\"");
1179 
1180  stk::mesh::Selector edge_block = *edgeBlockPart;
1181  stk::mesh::Selector owned_block = metaData_->locally_owned_part() & edge_block;
1182 
1183  // grab elements
1184  stk::mesh::get_selected_entities(owned_block,bulkData_->buckets(getEdgeRank()),edges);
1185 }
1186 
1187 void STK_Interface::getMyEdges(const std::string & edgeBlockName,const std::string & blockName,std::vector<stk::mesh::Entity> & edges) const
1188 {
1189  stk::mesh::Part * edgeBlockPart = getEdgeBlock(edgeBlockName);
1190  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1192  "Unknown edge block \"" << edgeBlockName << "\"");
1194  "Unknown element block \"" << blockName << "\"");
1195 
1196  stk::mesh::Selector edge_block = *edgeBlockPart;
1197  stk::mesh::Selector element_block = *elmtPart;
1198  stk::mesh::Selector owned_block = metaData_->locally_owned_part() & element_block & edge_block;
1199 
1200  // grab elements
1201  stk::mesh::get_selected_entities(owned_block,bulkData_->buckets(getEdgeRank()),edges);
1202 }
1203 
1204 void STK_Interface::getAllEdges(const std::string & edgeBlockName,std::vector<stk::mesh::Entity> & edges) const
1205 {
1206  stk::mesh::Part * edgeBlockPart = getEdgeBlock(edgeBlockName);
1207  TEUCHOS_TEST_FOR_EXCEPTION(edgeBlockPart==0,std::logic_error,
1208  "Unknown edge block \"" << edgeBlockName << "\"");
1209 
1210  stk::mesh::Selector edge_block = *edgeBlockPart;
1211 
1212  // grab elements
1213  stk::mesh::get_selected_entities(edge_block,bulkData_->buckets(getEdgeRank()),edges);
1214 }
1215 
1216 void STK_Interface::getAllEdges(const std::string & edgeBlockName,const std::string & blockName,std::vector<stk::mesh::Entity> & edges) const
1217 {
1218  stk::mesh::Part * edgeBlockPart = getEdgeBlock(edgeBlockName);
1219  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1221  "Unknown edge block \"" << edgeBlockName << "\"");
1223  "Unknown element block \"" << blockName << "\"");
1224 
1225  stk::mesh::Selector edge_block = *edgeBlockPart;
1226  stk::mesh::Selector element_block = *elmtPart;
1227  stk::mesh::Selector element_edge_block = element_block & edge_block;
1228 
1229  // grab elements
1230  stk::mesh::get_selected_entities(element_edge_block,bulkData_->buckets(getEdgeRank()),edges);
1231 }
1232 
1233 void STK_Interface::getMyFaces(std::vector<stk::mesh::Entity> & faces) const
1234 {
1235  // setup local ownership
1236  stk::mesh::Selector ownedPart = metaData_->locally_owned_part();
1237 
1238  // grab elements
1239  stk::mesh::EntityRank faceRank = getFaceRank();
1240  stk::mesh::get_selected_entities(ownedPart,bulkData_->buckets(faceRank),faces);
1241 }
1242 
1243 void STK_Interface::getMyFaces(const std::string & faceBlockName,std::vector<stk::mesh::Entity> & faces) const
1244 {
1245  stk::mesh::Part * faceBlockPart = getFaceBlock(faceBlockName);
1246  TEUCHOS_TEST_FOR_EXCEPTION(faceBlockPart==0,std::logic_error,
1247  "Unknown face block \"" << faceBlockName << "\"");
1248 
1249  stk::mesh::Selector face_block = *faceBlockPart;
1250  stk::mesh::Selector owned_block = metaData_->locally_owned_part() & face_block;
1251 
1252  // grab elements
1253  stk::mesh::get_selected_entities(owned_block,bulkData_->buckets(getFaceRank()),faces);
1254 }
1255 
1256 void STK_Interface::getMyFaces(const std::string & faceBlockName,const std::string & blockName,std::vector<stk::mesh::Entity> & faces) const
1257 {
1258  stk::mesh::Part * faceBlockPart = getFaceBlock(faceBlockName);
1259  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1261  "Unknown face block \"" << faceBlockName << "\"");
1263  "Unknown element block \"" << blockName << "\"");
1264 
1265  stk::mesh::Selector face_block = *faceBlockPart;
1266  stk::mesh::Selector element_block = *elmtPart;
1267  stk::mesh::Selector owned_block = metaData_->locally_owned_part() & element_block & face_block;
1268 
1269  // grab elements
1270  stk::mesh::get_selected_entities(owned_block,bulkData_->buckets(getFaceRank()),faces);
1271 }
1272 
1273 void STK_Interface::getAllFaces(const std::string & faceBlockName,std::vector<stk::mesh::Entity> & faces) const
1274 {
1275  stk::mesh::Part * faceBlockPart = getFaceBlock(faceBlockName);
1276  TEUCHOS_TEST_FOR_EXCEPTION(faceBlockPart==0,std::logic_error,
1277  "Unknown face block \"" << faceBlockName << "\"");
1278 
1279  stk::mesh::Selector face_block = *faceBlockPart;
1280 
1281  // grab elements
1282  stk::mesh::get_selected_entities(face_block,bulkData_->buckets(getFaceRank()),faces);
1283 }
1284 
1285 void STK_Interface::getAllFaces(const std::string & faceBlockName,const std::string & blockName,std::vector<stk::mesh::Entity> & faces) const
1286 {
1287  stk::mesh::Part * faceBlockPart = getFaceBlock(faceBlockName);
1288  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1290  "Unknown face block \"" << faceBlockName << "\"");
1292  "Unknown element block \"" << blockName << "\"");
1293 
1294  stk::mesh::Selector face_block = *faceBlockPart;
1295  stk::mesh::Selector element_block = *elmtPart;
1296  stk::mesh::Selector element_face_block = element_block & face_block;
1297 
1298  // grab elements
1299  stk::mesh::get_selected_entities(element_face_block,bulkData_->buckets(getFaceRank()),faces);
1300 }
1301 
1302 void STK_Interface::getMySides(const std::string & sideName,std::vector<stk::mesh::Entity> & sides) const
1303 {
1304  stk::mesh::Part * sidePart = getSideset(sideName);
1305  TEUCHOS_TEST_FOR_EXCEPTION(sidePart==0,std::logic_error,
1306  "Unknown side set \"" << sideName << "\"");
1307 
1308  stk::mesh::Selector side = *sidePart;
1309  stk::mesh::Selector ownedBlock = metaData_->locally_owned_part() & side;
1310 
1311  // grab elements
1312  stk::mesh::get_selected_entities(ownedBlock,bulkData_->buckets(getSideRank()),sides);
1313 }
1314 
1315 void STK_Interface::getMySides(const std::string & sideName,const std::string & blockName,std::vector<stk::mesh::Entity> & sides) const
1316 {
1317  stk::mesh::Part * sidePart = getSideset(sideName);
1318  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1320  "Unknown side set \"" << sideName << "\"");
1322  "Unknown element block \"" << blockName << "\"");
1323 
1324  stk::mesh::Selector side = *sidePart;
1325  stk::mesh::Selector block = *elmtPart;
1326  stk::mesh::Selector ownedBlock = metaData_->locally_owned_part() & block & side;
1327 
1328  // grab elements
1329  stk::mesh::get_selected_entities(ownedBlock,bulkData_->buckets(getSideRank()),sides);
1330 }
1331 
1332 void STK_Interface::getAllSides(const std::string & sideName,std::vector<stk::mesh::Entity> & sides) const
1333 {
1334  stk::mesh::Part * sidePart = getSideset(sideName);
1335  TEUCHOS_TEST_FOR_EXCEPTION(sidePart==0,std::logic_error,
1336  "Unknown side set \"" << sideName << "\"");
1337 
1338  stk::mesh::Selector side = *sidePart;
1339 
1340  // grab elements
1341  stk::mesh::get_selected_entities(side,bulkData_->buckets(getSideRank()),sides);
1342 }
1343 
1344 void STK_Interface::getAllSides(const std::string & sideName,const std::string & blockName,std::vector<stk::mesh::Entity> & sides) const
1345 {
1346  stk::mesh::Part * sidePart = getSideset(sideName);
1347  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1349  "Unknown side set \"" << sideName << "\"");
1351  "Unknown element block \"" << blockName << "\"");
1352 
1353  stk::mesh::Selector side = *sidePart;
1354  stk::mesh::Selector block = *elmtPart;
1355  stk::mesh::Selector sideBlock = block & side;
1356 
1357  // grab elements
1358  stk::mesh::get_selected_entities(sideBlock,bulkData_->buckets(getSideRank()),sides);
1359 }
1360 
1361 void STK_Interface::getMyNodes(const std::string & nodesetName,const std::string & blockName,std::vector<stk::mesh::Entity> & nodes) const
1362 {
1363  stk::mesh::Part * nodePart = getNodeset(nodesetName);
1364  stk::mesh::Part * elmtPart = getElementBlockPart(blockName);
1366  "Unknown node set \"" << nodesetName << "\"");
1368  "Unknown element block \"" << blockName << "\"");
1369 
1370  stk::mesh::Selector nodeset = *nodePart;
1371  stk::mesh::Selector block = *elmtPart;
1372  stk::mesh::Selector ownedBlock = metaData_->locally_owned_part() & block & nodeset;
1373 
1374  // grab elements
1375  stk::mesh::get_selected_entities(ownedBlock,bulkData_->buckets(getNodeRank()),nodes);
1376 }
1377 
1378 void STK_Interface::getElementBlockNames(std::vector<std::string> & names) const
1379 {
1380  // TEUCHOS_ASSERT(initialized_); // all blocks must have been added
1381 
1382  names.clear();
1383 
1384  // fill vector with automagically ordered string values
1385  std::map<std::string, stk::mesh::Part*>::const_iterator blkItr; // Element blocks
1386  for(blkItr=elementBlocks_.begin();blkItr!=elementBlocks_.end();++blkItr)
1387  names.push_back(blkItr->first);
1388 }
1389 
1390 void STK_Interface::getSidesetNames(std::vector<std::string> & names) const
1391 {
1392  // TEUCHOS_ASSERT(initialized_); // all blocks must have been added
1393 
1394  names.clear();
1395 
1396  // fill vector with automagically ordered string values
1397  std::map<std::string, stk::mesh::Part*>::const_iterator sideItr; // Side sets
1398  for(sideItr=sidesets_.begin();sideItr!=sidesets_.end();++sideItr)
1399  names.push_back(sideItr->first);
1400 }
1401 
1402 void STK_Interface::getNodesetNames(std::vector<std::string> & names) const
1403 {
1404  names.clear();
1405 
1406  // fill vector with automagically ordered string values
1407  std::map<std::string, stk::mesh::Part*>::const_iterator nodeItr; // Node sets
1408  for(nodeItr=nodesets_.begin();nodeItr!=nodesets_.end();++nodeItr)
1409  names.push_back(nodeItr->first);
1410 }
1411 
1412 void STK_Interface::getEdgeBlockNames(std::vector<std::string> & names) const
1413 {
1414  names.clear();
1415 
1416  // fill vector with automagically ordered string values
1417  std::map<std::string, stk::mesh::Part*>::const_iterator edgeBlockItr; // Edge blocks
1418  for(edgeBlockItr=edgeBlocks_.begin();edgeBlockItr!=edgeBlocks_.end();++edgeBlockItr)
1419  names.push_back(edgeBlockItr->first);
1420 }
1421 
1422 void STK_Interface::getFaceBlockNames(std::vector<std::string> & names) const
1423 {
1424  names.clear();
1425 
1426  // fill vector with automagically ordered string values
1427  std::map<std::string, stk::mesh::Part*>::const_iterator faceBlockItr; // Face blocks
1428  for(faceBlockItr=faceBlocks_.begin();faceBlockItr!=faceBlocks_.end();++faceBlockItr)
1429  names.push_back(faceBlockItr->first);
1430 }
1431 
1432 std::size_t STK_Interface::elementLocalId(stk::mesh::Entity elmt) const
1433 {
1434  return elementLocalId(bulkData_->identifier(elmt));
1435  // const std::size_t * fieldCoords = stk::mesh::field_data(*localIdField_,*elmt);
1436  // return fieldCoords[0];
1437 }
1438 
1439 std::size_t STK_Interface::elementLocalId(stk::mesh::EntityId gid) const
1440 {
1441  // stk::mesh::EntityRank elementRank = getElementRank();
1442  // stk::mesh::Entity elmt = bulkData_->get_entity(elementRank,gid);
1443  // TEUCHOS_ASSERT(elmt->owner_rank()==procRank_);
1444  // return elementLocalId(elmt);
1445  std::unordered_map<stk::mesh::EntityId,std::size_t>::const_iterator itr = localIDHash_.find(gid);
1446  TEUCHOS_ASSERT(itr!=localIDHash_.end());
1447  return itr->second;
1448 }
1449 
1450 bool STK_Interface::isEdgeLocal(stk::mesh::Entity edge) const
1451 {
1452  return isEdgeLocal(bulkData_->identifier(edge));
1453 }
1454 
1455 bool STK_Interface::isEdgeLocal(stk::mesh::EntityId gid) const
1456 {
1457  std::unordered_map<stk::mesh::EntityId,std::size_t>::const_iterator itr = localEdgeIDHash_.find(gid);
1458  if (itr==localEdgeIDHash_.end()) {
1459  return false;
1460  }
1461  return true;
1462 }
1463 
1464 std::size_t STK_Interface::edgeLocalId(stk::mesh::Entity edge) const
1465 {
1466  return edgeLocalId(bulkData_->identifier(edge));
1467 }
1468 
1469 std::size_t STK_Interface::edgeLocalId(stk::mesh::EntityId gid) const
1470 {
1471  std::unordered_map<stk::mesh::EntityId,std::size_t>::const_iterator itr = localEdgeIDHash_.find(gid);
1472  TEUCHOS_ASSERT(itr!=localEdgeIDHash_.end());
1473  return itr->second;
1474 }
1475 
1476 bool STK_Interface::isFaceLocal(stk::mesh::Entity face) const
1477 {
1478  return isFaceLocal(bulkData_->identifier(face));
1479 }
1480 
1481 bool STK_Interface::isFaceLocal(stk::mesh::EntityId gid) const
1482 {
1483  std::unordered_map<stk::mesh::EntityId,std::size_t>::const_iterator itr = localFaceIDHash_.find(gid);
1484  if (itr==localFaceIDHash_.end()) {
1485  return false;
1486  }
1487  return true;
1488 }
1489 
1490 std::size_t STK_Interface::faceLocalId(stk::mesh::Entity face) const
1491 {
1492  return faceLocalId(bulkData_->identifier(face));
1493 }
1494 
1495 std::size_t STK_Interface::faceLocalId(stk::mesh::EntityId gid) const
1496 {
1497  std::unordered_map<stk::mesh::EntityId,std::size_t>::const_iterator itr = localFaceIDHash_.find(gid);
1498  TEUCHOS_ASSERT(itr!=localFaceIDHash_.end());
1499  return itr->second;
1500 }
1501 
1502 
1503 std::string STK_Interface::containingBlockId(stk::mesh::Entity elmt) const
1504 {
1505  for(const auto & eb_pair : elementBlocks_)
1506  if(bulkData_->bucket(elmt).member(*(eb_pair.second)))
1507  return eb_pair.first;
1508  return "";
1509 }
1510 
1511 stk::mesh::Field<double> * STK_Interface::getSolutionField(const std::string & fieldName,
1512  const std::string & blockId) const
1513 {
1514  // look up field in map
1515  std::map<std::pair<std::string,std::string>, SolutionFieldType*>::const_iterator
1516  iter = fieldNameToSolution_.find(std::make_pair(fieldName,blockId));
1517 
1518  // check to make sure field was actually found
1519  TEUCHOS_TEST_FOR_EXCEPTION(iter==fieldNameToSolution_.end(),std::runtime_error,
1520  "Solution field name \"" << fieldName << "\" in block ID \"" << blockId << "\" was not found");
1521 
1522  return iter->second;
1523 }
1524 
1525 stk::mesh::Field<double> * STK_Interface::getCellField(const std::string & fieldName,
1526  const std::string & blockId) const
1527 {
1528  // look up field in map
1529  std::map<std::pair<std::string,std::string>, SolutionFieldType*>::const_iterator
1530  iter = fieldNameToCellField_.find(std::make_pair(fieldName,blockId));
1531 
1532  // check to make sure field was actually found
1533  TEUCHOS_TEST_FOR_EXCEPTION(iter==fieldNameToCellField_.end(),std::runtime_error,
1534  "Cell field named \"" << fieldName << "\" in block ID \"" << blockId << "\" was not found");
1535 
1536  return iter->second;
1537 }
1538 
1539 stk::mesh::Field<double> * STK_Interface::getEdgeField(const std::string & fieldName,
1540  const std::string & blockId) const
1541 {
1542  // look up field in map
1543  std::map<std::pair<std::string,std::string>, SolutionFieldType*>::const_iterator
1544  iter = fieldNameToEdgeField_.find(std::make_pair(fieldName,blockId));
1545 
1546  // check to make sure field was actually found
1547  TEUCHOS_TEST_FOR_EXCEPTION(iter==fieldNameToEdgeField_.end(),std::runtime_error,
1548  "Edge field named \"" << fieldName << "\" in block ID \"" << blockId << "\" was not found");
1549 
1550  return iter->second;
1551 }
1552 
1553 stk::mesh::Field<double> * STK_Interface::getFaceField(const std::string & fieldName,
1554  const std::string & blockId) const
1555 {
1556  // look up field in map
1557  std::map<std::pair<std::string,std::string>, SolutionFieldType*>::const_iterator
1558  iter = fieldNameToFaceField_.find(std::make_pair(fieldName,blockId));
1559 
1560  // check to make sure field was actually found
1561  TEUCHOS_TEST_FOR_EXCEPTION(iter==fieldNameToFaceField_.end(),std::runtime_error,
1562  "Face field named \"" << fieldName << "\" in block ID \"" << blockId << "\" was not found");
1563 
1564  return iter->second;
1565 }
1566 
1568 {
1569  using Teuchos::RCP;
1570  using Teuchos::rcp;
1571 
1572  if(orderedElementVector_==Teuchos::null) {
1573  // safe because essentially this is a call to modify a mutable object
1574  const_cast<STK_Interface*>(this)->buildLocalElementIDs();
1575  }
1576 
1578 }
1579 
1580 void STK_Interface::addElementBlock(const std::string & name,const CellTopologyData * ctData)
1581 {
1583 
1584  stk::mesh::Part * block = metaData_->get_part(name);
1585  if(block==0) {
1586  block = &metaData_->declare_part_with_topology(name, stk::mesh::get_topology(shards::CellTopology(ctData), dimension_));
1587  }
1588 
1589  // construct cell topology object for this block
1591  = Teuchos::rcp(new shards::CellTopology(ctData));
1592 
1593  // add element block part and cell topology
1594  elementBlocks_.insert(std::make_pair(name,block));
1595  elementBlockCT_.insert(std::make_pair(name,ct));
1596 }
1597 
1599 {
1600  using Teuchos::RCP;
1601  using Teuchos::rcp;
1602 
1603  if(orderedEdgeVector_==Teuchos::null) {
1604  // safe because essentially this is a call to modify a mutable object
1605  const_cast<STK_Interface*>(this)->buildLocalEdgeIDs();
1606  }
1607 
1608  return orderedEdgeVector_.getConst();
1609 }
1610 
1611 void STK_Interface::addEdgeBlock(const std::string & elemBlockName,
1612  const std::string & edgeBlockName,
1613  const stk::topology & topology)
1614 {
1616 
1617  stk::mesh::Part * edge_block = metaData_->get_part(edgeBlockName);
1618  if(edge_block==0) {
1619  edge_block = &metaData_->declare_part_with_topology(edgeBlockName, topology);
1620  }
1621 
1622  /* There is only one edge block for each edge topology, so declare it
1623  * as a subset of the element block even if it wasn't just created.
1624  */
1625  stk::mesh::Part * elem_block = metaData_->get_part(elemBlockName);
1626  metaData_->declare_part_subset(*elem_block, *edge_block);
1627 
1628  // add edge block part
1629  edgeBlocks_.insert(std::make_pair(edgeBlockName,edge_block));
1630 }
1631 
1632 void STK_Interface::addEdgeBlock(const std::string & elemBlockName,
1633  const std::string & edgeBlockName,
1634  const CellTopologyData * ctData)
1635 {
1637 
1638  addEdgeBlock(elemBlockName,
1639  edgeBlockName,
1640  stk::mesh::get_topology(shards::CellTopology(ctData), dimension_));
1641 }
1642 
1644 {
1645  using Teuchos::RCP;
1646  using Teuchos::rcp;
1647 
1648  if(orderedFaceVector_==Teuchos::null) {
1649  // safe because essentially this is a call to modify a mutable object
1650  const_cast<STK_Interface*>(this)->buildLocalFaceIDs();
1651  }
1652 
1653  return orderedFaceVector_.getConst();
1654 }
1655 
1656 void STK_Interface::addFaceBlock(const std::string & elemBlockName,
1657  const std::string & faceBlockName,
1658  const stk::topology & topology)
1659 {
1661 
1662  stk::mesh::Part * face_block = metaData_->get_part(faceBlockName);
1663  if(face_block==0) {
1664  face_block = &metaData_->declare_part_with_topology(faceBlockName, topology);
1665  }
1666 
1667  /* There is only one face block for each edge topology, so declare it
1668  * as a subset of the element block even if it wasn't just created.
1669  */
1670  stk::mesh::Part * elem_block = metaData_->get_part(elemBlockName);
1671  metaData_->declare_part_subset(*elem_block, *face_block);
1672 
1673  // add face block part
1674  faceBlocks_.insert(std::make_pair(faceBlockName,face_block));
1675 }
1676 
1677 void STK_Interface::addFaceBlock(const std::string & elemBlockName,
1678  const std::string & faceBlockName,
1679  const CellTopologyData * ctData)
1680 {
1682 
1683  addFaceBlock(elemBlockName,
1684  faceBlockName,
1685  stk::mesh::get_topology(shards::CellTopology(ctData), dimension_));
1686 }
1687 
1689 {
1690  dimension_ = metaData_->spatial_dimension();
1691 
1692  // declare coordinates and node parts
1693  coordinatesField_ = &metaData_->declare_field<double>(stk::topology::NODE_RANK, coordsString);
1694  edgesField_ = &metaData_->declare_field<double>(stk::topology::EDGE_RANK, edgesString);
1695  if (dimension_ > 2)
1696  facesField_ = &metaData_->declare_field<double>(stk::topology::FACE_RANK, facesString);
1697  processorIdField_ = &metaData_->declare_field<ProcIdData>(stk::topology::ELEMENT_RANK, "PROC_ID");
1698  loadBalField_ = &metaData_->declare_field<double>(stk::topology::ELEMENT_RANK, "LOAD_BAL");
1699 
1700  // stk::mesh::put_field( *coordinatesField_ , getNodeRank() , metaData_->universal_part() );
1701 
1702  nodesPart_ = &metaData_->declare_part(nodesString,getNodeRank());
1703  nodesPartVec_.push_back(nodesPart_);
1704  edgesPart_ = &metaData_->declare_part(edgesString,getEdgeRank());
1705  edgesPartVec_.push_back(edgesPart_);
1706  if (dimension_ > 2) {
1707  facesPart_ = &metaData_->declare_part(facesString,getFaceRank());
1708  facesPartVec_.push_back(facesPart_);
1709  }
1710 }
1711 
1713 {
1714  currentLocalId_ = 0;
1715 
1716  orderedElementVector_ = Teuchos::null; // forces rebuild of ordered lists
1717 
1718  // might be better (faster) to do this by buckets
1719  std::vector<stk::mesh::Entity> elements;
1720  getMyElements(elements);
1721 
1722  for(std::size_t index=0;index<elements.size();++index) {
1723  stk::mesh::Entity element = elements[index];
1724 
1725  // set processor rank
1726  ProcIdData * procId = stk::mesh::field_data(*processorIdField_,element);
1727  procId[0] = Teuchos::as<ProcIdData>(procRank_);
1728 
1729  localIDHash_[bulkData_->identifier(element)] = currentLocalId_;
1730 
1731  currentLocalId_++;
1732  }
1733 
1734  // copy elements into the ordered element vector
1735  orderedElementVector_ = Teuchos::rcp(new std::vector<stk::mesh::Entity>(elements));
1736 
1737  elements.clear();
1738  getNeighborElements(elements);
1739 
1740  for(std::size_t index=0;index<elements.size();++index) {
1741  stk::mesh::Entity element = elements[index];
1742 
1743  // set processor rank
1744  ProcIdData * procId = stk::mesh::field_data(*processorIdField_,element);
1745  procId[0] = Teuchos::as<ProcIdData>(procRank_);
1746 
1747  localIDHash_[bulkData_->identifier(element)] = currentLocalId_;
1748 
1749  currentLocalId_++;
1750  }
1751 
1752  orderedElementVector_->insert(orderedElementVector_->end(),elements.begin(),elements.end());
1753 }
1754 
1756 {
1757  std::vector<std::string> names;
1758  getElementBlockNames(names);
1759 
1760  for(std::size_t b=0;b<names.size();b++) {
1761  // find user specified block weight, otherwise use 1.0
1762  std::map<std::string,double>::const_iterator bw_itr = blockWeights_.find(names[b]);
1763  double blockWeight = (bw_itr!=blockWeights_.end()) ? bw_itr->second : 1.0;
1764 
1765  std::vector<stk::mesh::Entity> elements;
1766  getMyElements(names[b],elements);
1767 
1768  for(std::size_t index=0;index<elements.size();++index) {
1769  // set local element ID
1770  double * loadBal = stk::mesh::field_data(*loadBalField_,elements[index]);
1771  loadBal[0] = blockWeight;
1772  }
1773  }
1774 }
1775 
1777 {
1778  currentLocalId_ = 0;
1779 
1780  orderedEdgeVector_ = Teuchos::null; // forces rebuild of ordered lists
1781 
1782  // might be better (faster) to do this by buckets
1783  std::vector<stk::mesh::Entity> edges;
1784  getMyEdges(edges);
1785 
1786  for(std::size_t index=0;index<edges.size();++index) {
1787  stk::mesh::Entity edge = edges[index];
1788  localEdgeIDHash_[bulkData_->identifier(edge)] = currentLocalId_;
1789  currentLocalId_++;
1790  }
1791 
1792  // copy edges into the ordered edge vector
1793  orderedEdgeVector_ = Teuchos::rcp(new std::vector<stk::mesh::Entity>(edges));
1794 }
1795 
1797 {
1798  currentLocalId_ = 0;
1799 
1800  orderedFaceVector_ = Teuchos::null; // forces rebuild of ordered lists
1801 
1802  // might be better (faster) to do this by buckets
1803  std::vector<stk::mesh::Entity> faces;
1804  getMyFaces(faces);
1805 
1806  for(std::size_t index=0;index<faces.size();++index) {
1807  stk::mesh::Entity face = faces[index];
1808  localFaceIDHash_[bulkData_->identifier(face)] = currentLocalId_;
1809  currentLocalId_++;
1810  }
1811 
1812  // copy faces into the ordered face vector
1813  orderedFaceVector_ = Teuchos::rcp(new std::vector<stk::mesh::Entity>(faces));
1814 }
1815 
1816 bool
1817 STK_Interface::isMeshCoordField(const std::string & eBlock,
1818  const std::string & fieldName,
1819  int & axis) const
1820 {
1821  std::map<std::string,std::vector<std::string> >::const_iterator blkItr = meshCoordFields_.find(eBlock);
1822  if(blkItr==meshCoordFields_.end()) {
1823  return false;
1824  }
1825 
1826  axis = 0;
1827  for(axis=0;axis<Teuchos::as<int>(blkItr->second.size());axis++) {
1828  if(blkItr->second[axis]==fieldName)
1829  break; // found axis, break
1830  }
1831 
1832  if(axis>=Teuchos::as<int>(blkItr->second.size()))
1833  return false;
1834 
1835  return true;
1836 }
1837 
1838 std::pair<Teuchos::RCP<std::vector<std::pair<std::size_t,std::size_t> > >, Teuchos::RCP<std::vector<unsigned int> > >
1840 {
1842  Teuchos::RCP<std::vector<unsigned int > > type_vec = rcp(new std::vector<unsigned int>);
1843  const std::vector<Teuchos::RCP<const PeriodicBC_MatcherBase> > & matchers = getPeriodicBCVector();
1844  const bool & useBBoxSearch = useBoundingBoxSearch();
1845  std::vector<std::vector<std::string> > matchedSides(3); // (coord,edge,face)
1846 
1847  // build up the vectors by looping over the matched pair
1848  for(std::size_t m=0;m<matchers.size();m++){
1849  unsigned int type;
1850  if(matchers[m]->getType() == "coord")
1851  type = 0;
1852  else if(matchers[m]->getType() == "edge")
1853  type = 1;
1854  else if(matchers[m]->getType() == "face")
1855  type = 2;
1856  else
1857  TEUCHOS_ASSERT(false);
1858 #ifdef PANZER_HAVE_STKSEARCH
1859 
1860  if (useBBoxSearch) {
1861  vec = matchers[m]->getMatchedPair(*this,matchedSides[type],vec);
1862  } else {
1863  vec = matchers[m]->getMatchedPair(*this,vec);
1864  }
1865 #else
1866  TEUCHOS_TEST_FOR_EXCEPTION(useBBoxSearch,std::logic_error,
1867  "panzer::STK_Interface::getPeriodicNodePairing(): Requested bounding box search, but "
1868  "did not compile with STK_SEARCH enabled.");
1869  vec = matchers[m]->getMatchedPair(*this,vec);
1870 #endif
1871  type_vec->insert(type_vec->begin(),vec->size()-type_vec->size(),type);
1872  matchedSides[type].push_back(matchers[m]->getLeftSidesetName());
1873  }
1874 
1875  return std::make_pair(vec,type_vec);
1876 }
1877 
1878 bool STK_Interface::validBlockId(const std::string & blockId) const
1879 {
1880  std::map<std::string, stk::mesh::Part*>::const_iterator blkItr = elementBlocks_.find(blockId);
1881 
1882  return blkItr!=elementBlocks_.end();
1883 }
1884 
1885 void STK_Interface::print(std::ostream & os) const
1886 {
1887  std::vector<std::string> blockNames, sidesetNames, nodesetNames;
1888 
1889  getElementBlockNames(blockNames);
1890  getSidesetNames(sidesetNames);
1891  getNodesetNames(nodesetNames);
1892 
1893  os << "STK Mesh data:\n";
1894  os << " Spatial dim = " << getDimension() << "\n";
1895  if(getDimension()==2)
1896  os << " Entity counts (Nodes, Edges, Cells) = ( "
1897  << getEntityCounts(getNodeRank()) << ", "
1898  << getEntityCounts(getEdgeRank()) << ", "
1899  << getEntityCounts(getElementRank()) << " )\n";
1900  else if(getDimension()==3)
1901  os << " Entity counts (Nodes, Edges, Faces, Cells) = ( "
1902  << getEntityCounts(getNodeRank()) << ", "
1903  << getEntityCounts(getEdgeRank()) << ", "
1904  << getEntityCounts(getSideRank()) << ", "
1905  << getEntityCounts(getElementRank()) << " )\n";
1906  else
1907  os << " Entity counts (Nodes, Cells) = ( "
1908  << getEntityCounts(getNodeRank()) << ", "
1909  << getEntityCounts(getElementRank()) << " )\n";
1910 
1911  os << " Element blocks = ";
1912  for(std::size_t i=0;i<blockNames.size();i++)
1913  os << "\"" << blockNames[i] << "\" ";
1914  os << "\n";
1915  os << " Sidesets = ";
1916  for(std::size_t i=0;i<sidesetNames.size();i++)
1917  os << "\"" << sidesetNames[i] << "\" ";
1918  os << "\n";
1919  os << " Nodesets = ";
1920  for(std::size_t i=0;i<nodesetNames.size();i++)
1921  os << "\"" << nodesetNames[i] << "\" ";
1922  os << std::endl;
1923 
1924  // print out periodic boundary conditions
1925  const std::vector<Teuchos::RCP<const PeriodicBC_MatcherBase> > & bcVector
1926  = getPeriodicBCVector();
1927  if(bcVector.size()!=0) {
1928  os << " Periodic BCs:\n";
1929  for(std::size_t i=0;i<bcVector.size();i++)
1930  os << " " << bcVector[i]->getString() << "\n";
1931  os << std::endl;
1932  }
1933 }
1934 
1935 void STK_Interface::printMetaData(std::ostream & os) const
1936 {
1937  std::vector<std::string> blockNames, sidesetNames, nodesetNames;
1938 
1939  getElementBlockNames(blockNames);
1940  getSidesetNames(sidesetNames);
1941  getNodesetNames(nodesetNames);
1942 
1943  os << "STK Meta data:\n";
1944  os << " Element blocks = ";
1945  for(std::size_t i=0;i<blockNames.size();i++)
1946  os << "\"" << blockNames[i] << "\" ";
1947  os << "\n";
1948  os << " Sidesets = ";
1949  for(std::size_t i=0;i<sidesetNames.size();i++)
1950  os << "\"" << sidesetNames[i] << "\" ";
1951  os << "\n";
1952  os << " Nodesets = ";
1953  for(std::size_t i=0;i<nodesetNames.size();i++)
1954  os << "\"" << nodesetNames[i] << "\" ";
1955  os << std::endl;
1956 
1957  // print out periodic boundary conditions
1958  const std::vector<Teuchos::RCP<const PeriodicBC_MatcherBase> > & bcVector
1959  = getPeriodicBCVector();
1960  if(bcVector.size()!=0) {
1961  os << " Periodic BCs:\n";
1962  for(std::size_t i=0;i<bcVector.size();i++)
1963  os << " " << bcVector[i]->getString() << "\n";
1964  os << std::endl;
1965  }
1966 
1967  // print all available fields in meta data
1968  os << " Fields = ";
1969  const stk::mesh::FieldVector & fv = metaData_->get_fields();
1970  for(std::size_t i=0;i<fv.size();i++)
1971  os << "\"" << fv[i]->name() << "\" ";
1972  os << std::endl;
1973 }
1974 
1976 {
1977  std::map<std::string, Teuchos::RCP<shards::CellTopology> >::const_iterator itr;
1978  itr = elementBlockCT_.find(eBlock);
1979 
1980  if(itr==elementBlockCT_.end()) {
1981  std::stringstream ss;
1982  printMetaData(ss);
1983  TEUCHOS_TEST_FOR_EXCEPTION(itr==elementBlockCT_.end(),std::logic_error,
1984  "STK_Interface::getCellTopology: No such element block \"" +eBlock +"\" available.\n\n"
1985  << "STK Meta Data follows: \n" << ss.str());
1986  }
1987 
1988  return itr->second;
1989 }
1990 
1992 {
1993  MPI_Comm newComm;
1994  const int err = MPI_Comm_dup (parallelMach, &newComm);
1995  TEUCHOS_TEST_FOR_EXCEPTION(err != MPI_SUCCESS, std::runtime_error,
1996  "panzer::STK_Interface: MPI_Comm_dup failed with error \""
1997  << Teuchos::mpiErrorCodeToString (err) << "\".");
1998 
1999  return Teuchos::rcp(new Teuchos::MpiComm<int>(Teuchos::opaqueWrapper (newComm,MPI_Comm_free)));
2000 }
2001 
2003 {
2004  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "Rebalance not currently supported");
2005 #if 0
2006  // make sure weights have been set (a local operation)
2008 
2009  stk::mesh::Selector selector(getMetaData()->universal_part());
2010  stk::mesh::Selector owned_selector(getMetaData()->locally_owned_part());
2011 
2012  Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout));
2013  out.setOutputToRootOnly(0);
2014  out << "Load balance before: " << stk::rebalance::check_balance(*getBulkData(), loadBalField_, getElementRank(), &selector) << std::endl;
2015 
2016  // perform reblance
2017  Teuchos::ParameterList graph;
2018  if(params.begin()!=params.end())
2019  graph.sublist(stk::rebalance::Zoltan::default_parameters_name()) = params;
2020  stk::rebalance::Zoltan zoltan_partition(*mpiComm_->getRawMpiComm(), getDimension(), graph);
2021  stk::rebalance::rebalance(*getBulkData(), owned_selector, &getCoordinatesField(), loadBalField_, zoltan_partition);
2022 
2023  out << "Load balance after: " << stk::rebalance::check_balance(*getBulkData(), loadBalField_, getElementRank(), &selector) << std::endl;
2024 
2025  currentLocalId_ = 0;
2026  orderedElementVector_ = Teuchos::null; // forces rebuild of ordered lists
2027 #endif
2028 }
2029 
2032 {
2033  TEUCHOS_ASSERT(this->isInitialized());
2034  return mpiComm_;
2035 }
2036 
2037 void STK_Interface::refineMesh(const int numberOfLevels, const bool deleteParentElements) {
2038 #ifdef PANZER_HAVE_PERCEPT
2039  TEUCHOS_TEST_FOR_EXCEPTION(numberOfLevels < 1,std::runtime_error,
2040  "ERROR: Number of levels for uniform refinement must be greater than 0");
2041  TEUCHOS_ASSERT(nonnull(refinedMesh_));
2042  TEUCHOS_ASSERT(nonnull(breakPattern_));
2043 
2044  refinedMesh_->setCoordinatesField();
2045 
2046  percept::UniformRefiner breaker(*refinedMesh_,*breakPattern_);
2047  breaker.setRemoveOldElements(deleteParentElements);
2048 
2049  for (int i=0; i < numberOfLevels; ++i)
2050  breaker.doBreak();
2051 
2052 #else
2053  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
2054  "ERROR: Uniform refinement requested. This requires the Percept package to be enabled in Trilinos!");
2055 #endif
2056 }
2057 
2058 
2059 } // end namespace panzer_stk
Teuchos::RCP< ElementDescriptor > buildElementDescriptor(stk::mesh::EntityId elmtId, std::vector< stk::mesh::EntityId > &nodes)
Teuchos::RCP< Teuchos::MpiComm< int > > getSafeCommunicator(stk::ParallelMachine parallelMach) const
void getSidesetNames(std::vector< std::string > &name) const
RCP< const T > getConst() const
std::pair< Teuchos::RCP< std::vector< std::pair< std::size_t, std::size_t > > >, Teuchos::RCP< std::vector< unsigned int > > > getPeriodicNodePairing() const
const bool & useBoundingBoxSearch() const
Teuchos::RCP< std::vector< stk::mesh::Entity > > orderedEdgeVector_
void addNodeset(const std::string &name)
Teuchos::RCP< stk::mesh::BulkData > bulkData_
void getFaceBlockNames(std::vector< std::string > &names) const
void print(std::ostream &os) const
void getMyEdges(std::vector< stk::mesh::Entity > &edges) const
Teuchos::RCP< const std::vector< stk::mesh::Entity > > getElementsOrderedByLID() const
std::vector< stk::mesh::Part * > facesPartVec_
std::map< std::pair< std::string, std::string >, SolutionFieldType * > fieldNameToSolution_
void addEntityToNodeset(stk::mesh::Entity entity, stk::mesh::Part *nodeset)
void addGlobalToExodus(const std::string &key, const int &value)
Add an int global variable to the information to be written to the Exodus output file.
void getElementBlockNames(std::vector< std::string > &names) const
void getSubcellIndices(unsigned entityRank, stk::mesh::EntityId elementId, std::vector< stk::mesh::EntityId > &subcellIds) const
void getElementsSharingNodes(const std::vector< stk::mesh::EntityId > nodeId, std::vector< stk::mesh::Entity > &elements) const
get a set of elements sharing multiple nodes
stk::mesh::Field< double > SolutionFieldType
stk::mesh::Part * getElementBlockPart(const std::string &name) const
get the block part
std::vector< stk::mesh::Part * > edgesPartVec_
bool isFaceLocal(stk::mesh::Entity face) const
std::map< std::string, double > blockWeights_
Teuchos::RCP< const std::vector< stk::mesh::Entity > > getEdgesOrderedByLID() const
basic_FancyOStream< char > FancyOStream
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
std::size_t getEntityCounts(unsigned entityRank) const
get the global counts for the entity of specified rank
void printMetaData(std::ostream &os) const
void addEntityToSideset(stk::mesh::Entity entity, stk::mesh::Part *sideset)
std::map< std::string, stk::mesh::Part * > faceBlocks_
void addSolutionField(const std::string &fieldName, const std::string &blockId)
std::map< std::string, Teuchos::RCP< shards::CellTopology > > elementBlockCT_
void addEntityToFaceBlock(stk::mesh::Entity entity, stk::mesh::Part *faceblock)
stk::mesh::EntityId getMaxEntityId(unsigned entityRank) const
get max entity ID of type entityRank
std::map< std::pair< std::string, std::string >, SolutionFieldType * > fieldNameToFaceField_
std::map< std::string, stk::mesh::Part * > sidesets_
std::vector< std::size_t > entityCounts_
bool validBlockId(const std::string &blockId) const
stk::mesh::Part * getNodeset(const std::string &name) const
void getEdgeBlockNames(std::vector< std::string > &names) const
stk::mesh::Part * getFaceBlock(const std::string &name) const
get the block part
void addInformationRecords(const std::vector< std::string > &info_records)
void getAllFaces(const std::string &faceBlockName, std::vector< stk::mesh::Entity > &faces) const
stk::mesh::Field< double > * getSolutionField(const std::string &fieldName, const std::string &blockId) const
void addEdgeBlock(const std::string &elemBlockName, const std::string &edgeBlockName, const stk::topology &topology)
const double * getNodeCoordinates(stk::mesh::EntityId nodeId) const
stk::mesh::Part * getSideset(const std::string &name) const
static const std::string nodesString
void instantiateBulkData(stk::ParallelMachine parallelMach)
std::set< std::string > informationRecords_
void getMySides(const std::string &sideName, std::vector< stk::mesh::Entity > &sides) const
stk::mesh::Field< double > * getEdgeField(const std::string &fieldName, const std::string &blockId) const
std::size_t elementLocalId(stk::mesh::Entity elmt) const
unsigned getDimension() const
get the dimension
void addElement(const Teuchos::RCP< ElementDescriptor > &ed, stk::mesh::Part *block)
std::map< std::string, std::vector< std::string > > meshDispFields_
void setupExodusFile(const std::string &filename, const bool append=false, const bool append_after_restart_time=false, const double restart_time=0.0)
Set up an output Exodus file for writing results.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
stk::mesh::EntityRank getSideRank() const
void getAllEdges(const std::string &edgeBlockName, std::vector< stk::mesh::Entity > &edges) const
void initialize(stk::ParallelMachine parallelMach, bool setupIO=true, const bool buildRefinementSupport=false)
static const std::string coordsString
Teuchos::RCP< stk::mesh::BulkData > getBulkData() const
std::map< std::string, stk::mesh::Part * > edgeBlocks_
stk::mesh::EntityId elementGlobalId(std::size_t lid) const
std::map< std::string, stk::mesh::Part * > elementBlocks_
std::vector< stk::mesh::Part * > nodesPartVec_
static const std::string edgeBlockString
static const std::string edgesString
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
std::string containingBlockId(stk::mesh::Entity elmt) const
stk::mesh::EntityRank getFaceRank() const
std::size_t edgeLocalId(stk::mesh::Entity elmt) const
void getOwnedElementsSharingNode(stk::mesh::Entity node, std::vector< stk::mesh::Entity > &elements, std::vector< int > &relIds) const
void addNode(stk::mesh::EntityId gid, const std::vector< double > &coord)
void addEntitiesToEdgeBlock(std::vector< stk::mesh::Entity > entities, stk::mesh::Part *edgeblock)
void buildSubcells()
force the mesh to build subcells: edges and faces
Teuchos::RCP< Teuchos::MpiComm< int > > mpiComm_
Teuchos::RCP< stk::mesh::MetaData > metaData_
Teuchos::RCP< stk::mesh::MetaData > getMetaData() const
Teuchos::RCP< std::vector< stk::mesh::Entity > > orderedElementVector_
bool isInitialized() const
Has initialize been called on this mesh object?
TransListIter iter
void writeToExodus(const std::string &filename, const bool append=false)
Write this mesh and associated fields to the given output file.
void getMyNodes(const std::string &sideName, const std::string &blockName, std::vector< stk::mesh::Entity > &nodes) const
bool nonnull(const boost::shared_ptr< T > &p)
void addEdgeField(const std::string &fieldName, const std::string &blockId)
void addMeshCoordFields(const std::string &blockId, const std::vector< std::string > &coordField, const std::string &dispPrefix)
stk::mesh::Entity findConnectivityById(stk::mesh::Entity src, stk::mesh::EntityRank tgt_rank, unsigned rel_id) const
bool isMeshCoordField(const std::string &eBlock, const std::string &fieldName, int &axis) const
void getAllSides(const std::string &sideName, std::vector< stk::mesh::Entity > &sides) const
void addSideset(const std::string &name, const CellTopologyData *ctData)
static const std::string faceBlockString
std::unordered_map< stk::mesh::EntityId, std::size_t > localFaceIDHash_
void addEntityToEdgeBlock(stk::mesh::Entity entity, stk::mesh::Part *edgeblock)
std::map< std::pair< std::string, std::string >, SolutionFieldType * > fieldNameToEdgeField_
stk::mesh::Part * getEdgeBlock(const std::string &name) const
get the block part
std::unordered_map< stk::mesh::EntityId, std::size_t > localEdgeIDHash_
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field
A field to which we&#39;ll contribute, or in which we&#39;ll store, the result of computing this integral...
void rebalance(const Teuchos::ParameterList &params)
std::map< std::string, std::vector< std::string > > meshCoordFields_
stk::mesh::EntityRank getEdgeRank() const
static const std::string facesString
void addEntitiesToFaceBlock(std::vector< stk::mesh::Entity > entities, stk::mesh::Part *faceblock)
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
get the comm associated with this mesh
void initializeFieldsInSTK(const std::map< std::pair< std::string, std::string >, SolutionFieldType * > &nameToField, bool setupIO)
const std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > & getPeriodicBCVector() const
void refineMesh(const int numberOfLevels, const bool deleteParentElements)
stk::mesh::Field< double > * getFaceField(const std::string &fieldName, const std::string &blockId) const
std::size_t faceLocalId(stk::mesh::Entity elmt) const
void addFaceBlock(const std::string &elemBlockName, const std::string &faceBlockName, const stk::topology &topology)
Teuchos::RCP< std::vector< stk::mesh::Entity > > orderedFaceVector_
std::unordered_map< stk::mesh::EntityId, std::size_t > localIDHash_
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
std::vector< stk::mesh::EntityId > maxEntityId_
void getElementsSharingNode(stk::mesh::EntityId nodeId, std::vector< stk::mesh::Entity > &elements) const
get a set of elements sharing a single node
void addFaceField(const std::string &fieldName, const std::string &blockId)
void getMyElements(std::vector< stk::mesh::Entity > &elements) const
#define TEUCHOS_ASSERT(assertion_test)
stk::mesh::EntityRank getNodeRank() const
void getMyFaces(std::vector< stk::mesh::Entity > &faces) const
void addCellField(const std::string &fieldName, const std::string &blockId)
void getNodeIdsForElement(stk::mesh::Entity element, std::vector< stk::mesh::EntityId > &nodeIds) const
get a list of node ids for nodes connected to an element
Teuchos::RCP< const std::vector< stk::mesh::Entity > > getFacesOrderedByLID() const
stk::mesh::EntityRank getElementRank() const
void addElementBlock(const std::string &name, const CellTopologyData *ctData)
stk::mesh::Field< double > * getCellField(const std::string &fieldName, const std::string &blockId) const
const VectorFieldType & getCoordinatesField() const
bool isEdgeLocal(stk::mesh::Entity edge) const
void getNeighborElements(std::vector< stk::mesh::Entity > &elements) const
void getNodesetNames(std::vector< std::string > &name) const
Teuchos::RCP< const shards::CellTopology > getCellTopology(const std::string &eBlock) const
std::map< std::pair< std::string, std::string >, SolutionFieldType * > fieldNameToCellField_
std::map< std::string, stk::mesh::Part * > nodesets_
bool is_null() const