Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_Quad8ToQuad4MeshFactory.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #include "PanzerAdaptersSTK_config.hpp"
46 #include "Panzer_STK_Interface.hpp"
47 #include "Teuchos_TimeMonitor.hpp"
48 #include "Teuchos_StandardParameterEntryValidators.hpp" // for plist validation
49 #include <stk_mesh/base/FieldBase.hpp>
50 #include <stk_mesh/base/DumpMeshInfo.hpp>
51 
52 // #define ENABLE_UNIFORM
53 
54 using Teuchos::RCP;
55 using Teuchos::rcp;
56 
57 namespace panzer_stk {
58 
59 Quad8ToQuad4MeshFactory::Quad8ToQuad4MeshFactory(const std::string& quad8MeshFileName,
60  stk::ParallelMachine mpi_comm,
61  const bool print_debug)
62  : createEdgeBlocks_(false),
63  print_debug_(print_debug)
64 {
65  panzer_stk::STK_ExodusReaderFactory factory;
67  pl->set("File Name",quad8MeshFileName);
68  factory.setParameterList(pl);
69  quad8Mesh_ = factory.buildMesh(mpi_comm);
70 
72 }
73 
75  const bool print_debug)
76  : quad8Mesh_(quad8Mesh),
77  createEdgeBlocks_(false),
78  print_debug_(print_debug)
79 {
81 }
82 
84 Teuchos::RCP<STK_Interface> Quad8ToQuad4MeshFactory::buildMesh(stk::ParallelMachine parallelMach) const
85 {
86  PANZER_FUNC_TIME_MONITOR("panzer::Quad8ToQuad4MeshFactory::buildMesh()");
87 
88  // Make sure the Quad8 and Quad4 Comms match
89  {
90  int result = MPI_UNEQUAL;
91  MPI_Comm_compare(parallelMach, quad8Mesh_->getBulkData()->parallel(), &result);
92  TEUCHOS_ASSERT(result != MPI_UNEQUAL);
93  }
94 
95  // build all meta data
96  RCP<STK_Interface> mesh = this->buildUncommitedMesh(parallelMach);
97 
98  // commit meta data
99  mesh->initialize(parallelMach);
100 
101  // build bulk data
102  this->completeMeshConstruction(*mesh,parallelMach);
103 
104  return mesh;
105 }
106 
108 {
109  PANZER_FUNC_TIME_MONITOR("panzer::Quad8ToQuad4MeshFactory::buildUncomittedMesh()");
110 
111  RCP<STK_Interface> mesh = rcp(new STK_Interface(2));
112 
113  machRank_ = stk::parallel_machine_rank(parallelMach);
114  machSize_ = stk::parallel_machine_size(parallelMach);
115 
116  // build meta information: blocks and side set setups
117  this->buildMetaData(parallelMach,*mesh);
118 
119  mesh->addPeriodicBCs(periodicBCVec_);
120  mesh->setBoundingBoxSearchFlag(useBBoxSearch_);
121 
122  return mesh;
123 }
124 
125 void Quad8ToQuad4MeshFactory::completeMeshConstruction(STK_Interface & mesh,stk::ParallelMachine parallelMach) const
126 {
127  PANZER_FUNC_TIME_MONITOR("panzer::Quad8ToQuad4MeshFactory::completeMeshConstruction()");
128 
129  if(not mesh.isInitialized())
130  mesh.initialize(parallelMach);
131 
132  // add node and element information
133  this->buildElements(parallelMach,mesh);
134 
135  // finish up the edges
136 #ifndef ENABLE_UNIFORM
137  mesh.buildSubcells();
138 #endif
139  mesh.buildLocalElementIDs();
140  if(createEdgeBlocks_) {
141  mesh.buildLocalEdgeIDs();
142  }
143 
144  // now that edges are built, sidsets can be added
145 #ifndef ENABLE_UNIFORM
146  this->addSideSets(mesh);
147 #endif
148 
149  // add nodesets
150  this->addNodeSets(mesh);
151 
152  if(createEdgeBlocks_) {
153  this->addEdgeBlocks(mesh);
154  }
155 
156  // Copy field data
157  this->copyCellFieldData(mesh);
158 
159  // calls Stk_MeshFactory::rebalance
160  // this->rebalance(mesh);
161 }
162 
165 {
167 
168  setMyParamList(paramList);
169 
170  // offsetGIDs_ = (paramList->get<std::string>("Offset mesh GIDs above 32-bit int limit") == "ON") ? true : false;
171 
172  createEdgeBlocks_ = paramList->get<bool>("Create Edge Blocks");
173 
174  // read in periodic boundary conditions
175  parsePeriodicBCList(Teuchos::rcpFromRef(paramList->sublist("Periodic BCs")),periodicBCVec_,useBBoxSearch_);
176 }
177 
180 {
181  static RCP<Teuchos::ParameterList> defaultParams;
182 
183  // fill with default values
184  if(defaultParams == Teuchos::null) {
185  defaultParams = rcp(new Teuchos::ParameterList);
186 
187  defaultParams->set<std::string>("Offset mesh GIDs above 32-bit int limit", "OFF",
188  "If 64-bit GIDs are supported, the mesh element and node global indices will start at a value greater than 32-bit limit.",
189  rcp(new Teuchos::StringValidator(Teuchos::tuple<std::string>("OFF", "ON"))));
190 
191  // default to false for backward compatibility
192  defaultParams->set<bool>("Create Edge Blocks",false,"Create edge blocks in the mesh");
193 
194  Teuchos::ParameterList & bcs = defaultParams->sublist("Periodic BCs");
195  bcs.set<int>("Count",0); // no default periodic boundary conditions
196  }
197 
198  return defaultParams;
199 }
200 
201 void Quad8ToQuad4MeshFactory::buildMetaData(stk::ParallelMachine /* parallelMach */, STK_Interface & mesh) const
202 {
203  if (print_debug_) {
204  std::cout << "\n\n**** DEBUG: begin printing source Quad8 exodus file metadata ****\n";
205  stk::mesh::impl::dump_all_meta_info(*(quad8Mesh_->getMetaData()), std::cout);
206  std::cout << "\n\n**** DEBUG: end printing source Quad8 exodus file metadata ****\n";
207  }
208 
209  // Required topologies
210  using QuadTopo = shards::Quadrilateral<4>;
211  const CellTopologyData * ctd = shards::getCellTopologyData<QuadTopo>();
212  const CellTopologyData * side_ctd = shards::CellTopology(ctd).getBaseCellTopologyData(1,0);
213 
214  // Add in element blocks
215  std::vector<std::string> element_block_names;
216  quad8Mesh_->getElementBlockNames(element_block_names);
217  {
218  for (const auto& n : element_block_names)
219  mesh.addElementBlock(n,ctd);
220  }
221 
222  // Add in sidesets
223  {
224  std::vector<std::string> sideset_names;
225  quad8Mesh_->getSidesetNames(sideset_names);
226  for (const auto& n : sideset_names)
227  mesh.addSideset(n,side_ctd);
228  }
229 
230  // Add in nodesets
231  {
232  std::vector<std::string> nodeset_names;
233  quad8Mesh_->getNodesetNames(nodeset_names);
234  for (const auto& n : nodeset_names)
235  mesh.addNodeset(n);
236  }
237 
238  if(createEdgeBlocks_) {
239  const CellTopologyData * edge_ctd = shards::CellTopology(ctd).getBaseCellTopologyData(1,0);
240  std::vector<std::string> element_block_names;
241  quad8Mesh_->getElementBlockNames(element_block_names);
242  for (const auto& block_name : element_block_names)
243  mesh.addEdgeBlock(block_name,edgeBlockName_,edge_ctd);
244  }
245 
246  // Add in nodal fields
247  {
248  const auto& fields = quad8Mesh_->getMetaData()->get_fields(mesh.getNodeRank());
249  for (const auto& f : fields) {
250  if (print_debug_)
251  std::cout << "Field=" << f->name() << ", rank=" << f->entity_rank() << std::endl;
252 
253  // Cull out the coordinate fields. That is a default field in
254  // stk that is automatically created by stk.
255  if (f->name() != "coordinates") {
256  for (const auto& n : element_block_names)
257  mesh.addSolutionField(f->name(),n);
258  }
259  }
260  }
261 
262  // Add in element fields
263  {
264  const auto& fields = quad8Mesh_->getMetaData()->get_fields(mesh.getElementRank());
265  for (const auto& f : fields) {
266  if (print_debug_)
267  std::cout << "Add Cell Field=" << f->name() << ", rank=" << f->entity_rank() << std::endl;
268 
269  for (const auto& n : element_block_names)
270  mesh.addCellField(f->name(),n);
271  }
272  }
273 
274  // NOTE: skipping edge and face fields for now. Can't error out since sidesets count as edge fields.
275  // TEUCHOS_TEST_FOR_EXCEPTION(quad8Mesh_->getMetaData()->get_fields(mesh.getEdgeRank()).size() != 0,std::runtime_error,
276  // "ERROR: the Quad8 mesh contains edge fields\""
277  // << quad8Mesh_->getMetaData()->get_fields(mesh.getEdgeRank())[0]->name()
278  // << "\". Edge fields are not supported in Quad8ToQuad4!");
279 
280  if (print_debug_) {
281  std::cout << "\n\n**** DEBUG: begin printing source Quad4 exodus file metadata ****\n";
282  stk::mesh::impl::dump_all_meta_info(*(mesh.getMetaData()), std::cout);
283  std::cout << "\n\n**** DEBUG: end printing source Quad4 exodus file metadata ****\n";
284  }
285 }
286 
287 void Quad8ToQuad4MeshFactory::buildElements(stk::ParallelMachine parallelMach,STK_Interface & mesh) const
288 {
289  mesh.beginModification();
290 
291  auto metadata = mesh.getMetaData();
292  auto bulkdata = mesh.getBulkData();
293 
294  // Loop over element blocks
295  std::vector<std::string> block_names;
296  quad8Mesh_->getElementBlockNames(block_names);
297  for (const auto& block_name : block_names) {
298 
299  // Get the elements on this process
300  std::vector<stk::mesh::Entity> elements;
301  quad8Mesh_->getMyElements(block_name,elements);
302 
303  if (print_debug_) {
304  std::cout << "*************************************************" << std::endl;
305  std::cout << "block_name=" << block_name << ", num_my_elements=" << elements.size() << std::endl;
306  std::cout << "*************************************************" << std::endl;
307  }
308 
309  for (const auto& element : elements) {
310 
311  const auto element_gid = quad8Mesh_->getBulkData()->identifier(element);
312 
313  if (print_debug_) {
314  std::cout << "rank=" << machRank_
315  << ", block=" << block_name
316  << ", element entity_id=" << element
317  << ", gid=" << element_gid << std::endl;
318  }
319 
320  // Register nodes with the mesh
321  std::vector<stk::mesh::EntityId> nodes(4);
322  for (int i=0; i < 4; ++i) {
323  // NOTE: this assumes that the Quad4 topology is nested in
324  // the Quad8 as an extended topology - i.e. the first four
325  // nodes of the quad8 topology are the corresponding quad4
326  // nodes. Shards topologies enforce this throught the concept
327  // of extended topologies.
328  stk::mesh::Entity node_entity = quad8Mesh_->findConnectivityById(element,mesh.getNodeRank(),i);
329  TEUCHOS_ASSERT(node_entity.is_local_offset_valid());
330  const auto node_gid = quad8Mesh_->getBulkData()->identifier(node_entity);
331  const double* node_coords = quad8Mesh_->getNodeCoordinates(node_entity);
332  std::vector<double> coords_vec(2);
333  coords_vec[0] = node_coords[0];
334  coords_vec[1] = node_coords[1];
335  mesh.addNode(node_gid,coords_vec);
336  nodes[i]=node_gid;
337  if (print_debug_) {
338  std::cout << "elem gid=" << quad8Mesh_->getBulkData()->identifier(element)
339  << ", node_gid=" << node_gid << ", (" << coords_vec[0] << "," << coords_vec[1] << ")\n";
340  }
341  }
342 
343  // Register element with the element block
344  auto element_descriptor = panzer_stk::buildElementDescriptor(element_gid,nodes);
345  auto element_block_part = mesh.getMetaData()->get_part(block_name);
346  mesh.addElement(element_descriptor,element_block_part);
347  }
348  }
349  mesh.endModification();
350 }
351 
353 {
354  mesh.beginModification();
355 
356  // Loop over sidesets
357  std::vector<std::string> sideset_names;
358  quad8Mesh_->getSidesetNames(sideset_names);
359  for (const auto& sideset_name : sideset_names) {
360 
361  stk::mesh::Part* sideset_part = mesh.getSideset(sideset_name);
362 
363  std::vector<stk::mesh::Entity> q8_sides;
364  quad8Mesh_->getMySides(sideset_name,q8_sides);
365 
366  // Loop over edges
367  for (const auto q8_edge : q8_sides) {
368  // The edge numbering scheme uses the element/node gids, so it
369  // should be consistent between the quad8 and quad4 meshes
370  // since we used the same gids. We use this fact to populate
371  // the quad4 sidesets.
372  stk::mesh::EntityId edge_gid = quad8Mesh_->getBulkData()->identifier(q8_edge);
373  stk::mesh::Entity q4_edge = mesh.getBulkData()->get_entity(mesh.getEdgeRank(),edge_gid);
374  mesh.addEntityToSideset(q4_edge,sideset_part);
375  }
376  }
377 
378  mesh.endModification();
379 }
380 
382 {}
383 
385 {
386  mesh.beginModification();
387 
390 
391  stk::mesh::Part * edge_block = mesh.getEdgeBlock(edgeBlockName_);
392 
393  stk::mesh::Selector owned_block = metaData->locally_owned_part();
394 
395  std::vector<stk::mesh::Entity> edges;
396  bulkData->get_entities(mesh.getEdgeRank(), owned_block, edges);
397  mesh.addEntitiesToEdgeBlock(edges, edge_block);
398 
399  mesh.endModification();
400 }
401 
403 {
404  // Vector of pointers to field data
405  const auto& fields = q4_mesh.getMetaData()->get_fields(q4_mesh.getElementRank());
406 
407  // loop over fields and add the data to the new mesh.
408  for (const auto& field : fields) {
409 
410  if (print_debug_)
411  std::cout << "Adding field values for \"" << *field << "\" to the Quad4 mesh!\n";
412 
413  auto field_name = field->name();
414 
415  // Divide into scalar and vector fields, ignoring any other types
416  // for now.
417  if (field->type_is<double>() &&
418  field_name != "coordinates" &&
419  field_name != "PROC_ID" &&
420  field_name != "LOAD_BAL") {
421 
422  // Loop over element blocks
423  std::vector<std::string> block_names;
424  quad8Mesh_->getElementBlockNames(block_names);
425  for (const auto& block : block_names) {
426 
427  auto* q4_field = q4_mesh.getCellField(field_name,block);
428  TEUCHOS_ASSERT(q4_field != nullptr);
429  // The q8 mesh doesn't have the field names set up, so a query
430  // fails. Go to stk directly in this case.
431  auto* q8_field = quad8Mesh_->getMetaData()->get_field(quad8Mesh_->getElementRank(),field_name);
432 #ifdef PANZER_DEBUG
433  TEUCHOS_ASSERT(q8_field != nullptr);
434 #endif
435 
436  // Get the elements for this block.
437  std::vector<stk::mesh::Entity> q4_elements;
438  q4_mesh.getMyElements(block,q4_elements);
439  std::vector<stk::mesh::Entity> q8_elements;
440  quad8Mesh_->getMyElements(block,q8_elements);
441  TEUCHOS_ASSERT(q4_elements.size() == q8_elements.size());
442 
443  for (size_t i=0; i < q4_elements.size(); ++i) {
444 #ifdef PANZER_DEBUG
445  TEUCHOS_ASSERT(q4_mesh.getBulkData()->identifier(q4_elements[i]) ==
446  quad8Mesh_->getBulkData()->identifier(q8_elements[i]));
447 #endif
448 
449  double* const q4_val = static_cast<double*>(stk::mesh::field_data(*q4_field,q4_elements[i]));
450  const double* const q8_val = static_cast<double*>(stk::mesh::field_data(*q8_field,q8_elements[i]));
451  *q4_val = *q8_val;
452 
453  if (print_debug_) {
454  std::cout << "field=" << field_name << ", block=" << block
455  << ", q4e(" << q4_mesh.getBulkData()->identifier(q4_elements[i]) << ") = " << *q4_val
456  << ", q8e(" << quad8Mesh_->getBulkData()->identifier(q8_elements[i]) << ") = " << *q8_val
457  << std::endl;
458  }
459 
460  }
461  }
462  }
463  }
464 }
465 
466 } // end panzer_stk
Teuchos::RCP< ElementDescriptor > buildElementDescriptor(stk::mesh::EntityId elmtId, std::vector< stk::mesh::EntityId > &nodes)
void getSidesetNames(std::vector< std::string > &name) const
void addNodeset(const std::string &name)
static void parsePeriodicBCList(const Teuchos::RCP< Teuchos::ParameterList > &pl, std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > &periodicBC, bool &useBBoxSearch)
void getElementBlockNames(std::vector< std::string > &names) 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)
void addEntityToSideset(stk::mesh::Entity entity, stk::mesh::Part *sideset)
void addSolutionField(const std::string &fieldName, const std::string &blockId)
Teuchos::RCP< STK_Interface > buildMesh(stk::ParallelMachine parallelMach) const
Build the mesh object.
void buildMetaData(stk::ParallelMachine parallelMach, STK_Interface &mesh) const
Teuchos::RCP< panzer_stk::STK_Interface > quad8Mesh_
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
void getMySides(const std::string &sideName, std::vector< stk::mesh::Entity > &sides) const
PHX::MDField< ScalarT, panzer::Cell, panzer::IP > result
A field that will be used to build up the result of the integral we&#39;re performing.
void addElement(const Teuchos::RCP< ElementDescriptor > &ed, stk::mesh::Part *block)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void initialize(stk::ParallelMachine parallelMach, bool setupIO=true, const bool buildRefinementSupport=false)
void buildElements(stk::ParallelMachine parallelMach, STK_Interface &mesh) const
Teuchos::RCP< stk::mesh::BulkData > getBulkData() const
void setMyParamList(const RCP< ParameterList > &paramList)
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
static const std::string edgeBlockString
virtual void completeMeshConstruction(STK_Interface &mesh, stk::ParallelMachine parallelMach) 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< stk::mesh::MetaData > getMetaData() const
bool isInitialized() const
Has initialize been called on this mesh object?
stk::mesh::Entity findConnectivityById(stk::mesh::Entity src, stk::mesh::EntityRank tgt_rank, unsigned rel_id) const
void addSideset(const std::string &name, const CellTopologyData *ctData)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Derived from ParameterListAcceptor.
stk::mesh::Part * getEdgeBlock(const std::string &name) const
get the block part
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...
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > periodicBCVec_
stk::mesh::EntityRank getEdgeRank() const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &paramList)
Derived from ParameterListAcceptor.
Quad8ToQuad4MeshFactory(const std::string &quad8MeshFileName, stk::ParallelMachine mpi_comm=MPI_COMM_WORLD, const bool print_debug=false)
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
virtual Teuchos::RCP< STK_Interface > buildUncommitedMesh(stk::ParallelMachine parallelMach) const
void getMyElements(std::vector< stk::mesh::Entity > &elements) const
#define TEUCHOS_ASSERT(assertion_test)
stk::mesh::EntityRank getNodeRank() const
void addCellField(const std::string &fieldName, const std::string &blockId)
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
void getNodesetNames(std::vector< std::string > &name) const