libMesh
mesh_stitch.C
Go to the documentation of this file.
1 #include <libmesh/elem.h>
2 #include <libmesh/mesh_generation.h>
3 #include <libmesh/node.h>
4 #include <libmesh/replicated_mesh.h>
5 
6 #include "test_comm.h"
7 #include "libmesh_cppunit.h"
8 
9 
10 using namespace libMesh;
11 
12 
13 class MeshStitchTest : public CppUnit::TestCase {
14 public:
15  CPPUNIT_TEST_SUITE( MeshStitchTest );
16 
17 #if LIBMESH_DIM > 2
18  CPPUNIT_TEST( testMeshStitch );
19 #endif // LIBMESH_DIM > 2
20 
21  CPPUNIT_TEST_SUITE_END();
22 
23 private:
24 
25 public:
26  void setUp()
27  {}
28 
29  void tearDown()
30  {}
31 
33  {
34  // Generate four meshes to be stitched together
36  mesh1(*TestCommWorld),
37  mesh2(*TestCommWorld),
38  mesh3(*TestCommWorld);
39 
40  // Give the meshes different extra integers to make sure those
41  // merge. Reuse names between nodes and elements to make sure
42  // those don't mix. Add some integers before and others after
43  // generation to test flexibility there.
44 
45  std::vector<std::string> names2 {"bar", "baz"};
46  mesh2.add_elem_integers(names2);
47 
48  std::vector<std::string> names3 {"bar", "foo"};
49  mesh3.add_elem_integers(names3);
50 
51  int ps = 2;
52  MeshTools::Generation::build_cube (mesh0, ps, ps, ps, -1, 0, 0, 1, 0, 1, HEX27);
53  MeshTools::Generation::build_cube (mesh1, ps, ps, ps, 0, 1, 0, 1, 0, 1, HEX27);
54  MeshTools::Generation::build_cube (mesh2, ps, ps, ps, -1, 0, -1, 0, 0, 1, HEX27);
55  MeshTools::Generation::build_cube (mesh3, ps, ps, ps, 0, 1, -1, 0, 0, 1, HEX27);
56 
57  struct trivially_copyable_pair // std::pair triggers -Wclass-memaccess
58  {
59  dof_id_type first, second;
60  };
61 
62  mesh0.add_node_integer("baz");
63  unsigned int foo1e_idx = mesh1.add_elem_integer("foo");
64  mesh2.add_elem_datum<trivially_copyable_pair>("qux");
65  unsigned int qux2n_idx = mesh2.add_node_datum<trivially_copyable_pair>("qux");
66  mesh3.add_node_integers(names3);
67 
68  for (const auto & elem : mesh1.element_ptr_range())
69  elem->set_extra_integer(foo1e_idx, 2);
70 
71  for (const auto & node : mesh2.node_ptr_range())
72  node->set_extra_datum<trivially_copyable_pair>
73  (qux2n_idx, {3, 4});
74 
75  // We stitch the meshes in a hierarchical way.
76  mesh0.stitch_meshes(mesh1, 2, 4, TOLERANCE, true, true, false, false);
77  mesh2.stitch_meshes(mesh3, 2, 4, TOLERANCE, true, true, false, false);
78  mesh0.stitch_meshes(mesh2, 1, 3, TOLERANCE, true, true, false, false);
79 
80  CPPUNIT_ASSERT_EQUAL(mesh0.n_elem(), dof_id_type(32));
81  CPPUNIT_ASSERT_EQUAL(mesh0.n_nodes(), dof_id_type(405));
82  CPPUNIT_ASSERT_EQUAL(mesh0.n_elem_integers(), 5u); // that pair counts 2x
83  CPPUNIT_ASSERT_EQUAL(mesh0.n_node_integers(), 5u);
84  std::vector<std::string> all_names {"foo", "bar", "baz", "qux"};
85  std::vector<unsigned int> node_name_indices {4, 3, 0, 1};
86  for (unsigned int i=0; i != 4; ++i)
87  {
88  CPPUNIT_ASSERT(mesh0.has_elem_integer(all_names[i]));
89  CPPUNIT_ASSERT_EQUAL(mesh0.get_elem_integer_index(all_names[i]), i);
90  CPPUNIT_ASSERT(mesh0.has_node_integer(all_names[i]));
91  CPPUNIT_ASSERT_EQUAL(mesh0.get_node_integer_index(all_names[i]), node_name_indices[i]);
92  }
93 
94  unsigned int foo0e_idx = mesh0.get_elem_integer_index("foo");
95  for (const auto & elem : mesh0.element_ptr_range())
96  {
97  CPPUNIT_ASSERT_EQUAL(elem->n_extra_integers(), 5u);
98  const Point c = elem->centroid();
99  if (c(0) > 0 && c(1) > 0) // this came from mesh1
100  CPPUNIT_ASSERT_EQUAL(elem->get_extra_integer(foo0e_idx), dof_id_type(2));
101  else
102  CPPUNIT_ASSERT_EQUAL(elem->get_extra_integer(foo0e_idx), DofObject::invalid_id);
103  }
104 
105  unsigned int qux0n_idx = mesh0.get_node_integer_index("qux");
106  for (const auto & node : mesh0.node_ptr_range())
107  {
108  CPPUNIT_ASSERT_EQUAL(node->n_extra_integers(), 5u);
109  trivially_copyable_pair datum =
110  node->get_extra_datum<trivially_copyable_pair>(qux0n_idx);
111  if ((*node)(0) <= 0 && (*node)(1) < 0) // this came from mesh2
112  {
113  CPPUNIT_ASSERT_EQUAL(datum.first, dof_id_type(3));
114  CPPUNIT_ASSERT_EQUAL(datum.second, dof_id_type(4));
115  }
116  else
117  {
118  CPPUNIT_ASSERT_EQUAL(datum.first, DofObject::invalid_id);
119  CPPUNIT_ASSERT_EQUAL(datum.second, DofObject::invalid_id);
120  }
121  }
122  }
123 };
124 
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
MeshStitchTest
Definition: mesh_stitch.C:13
libMesh::MeshBase::add_elem_integers
std::vector< unsigned int > add_elem_integers(const std::vector< std::string > &names, bool allocate_data=true)
Register integer data (of type dof_id_type) to be added to each element in the mesh,...
Definition: mesh_base.C:191
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::MeshTools::Generation::build_cube
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.
Definition: mesh_generation.C:298
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
libMesh::MeshBase::add_node_integers
std::vector< unsigned int > add_node_integers(const std::vector< std::string > &names, bool allocate_data=true)
Register integer data (of type dof_id_type) to be added to each node in the mesh.
Definition: mesh_base.C:262
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
libMesh::HEX27
Definition: enum_elem_type.h:49
libMesh::DofObject::invalid_id
static const dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition: dof_object.h:421
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(MeshStitchTest)
MeshStitchTest::tearDown
void tearDown()
Definition: mesh_stitch.C:29
MeshStitchTest::setUp
void setUp()
Definition: mesh_stitch.C:26
libmesh_cppunit.h
test_comm.h
MeshStitchTest::testMeshStitch
void testMeshStitch()
Definition: mesh_stitch.C:32