libMesh
nodal_neighbors.C
Go to the documentation of this file.
1 #include <libmesh/libmesh.h>
2 #include <libmesh/node.h>
3 #include <libmesh/mesh_generation.h>
4 #include <libmesh/mesh_tools.h>
5 #include <libmesh/replicated_mesh.h>
6 
7 #include "test_comm.h"
8 #include "libmesh_cppunit.h"
9 
10 
11 using namespace libMesh;
12 
13 class NodalNeighborsTest : public CppUnit::TestCase
14 {
23 public:
24  CPPUNIT_TEST_SUITE( NodalNeighborsTest );
25 
26  CPPUNIT_TEST( testEdge2 );
27  CPPUNIT_TEST( testEdge3 );
28  CPPUNIT_TEST( testEdge4 );
29 
30  CPPUNIT_TEST_SUITE_END();
31 
32 protected:
33 
34  // Builds a 1D mesh with the specified ElemType and number of elements
35  void do_test(unsigned n_elem,
36  ElemType elem_type,
37  dof_id_type * validation_data)
38  {
39  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/1);
40 
42  n_elem,
43  /*xmin=*/0.,
44  /*xmax=*/1.,
45  elem_type);
46 
47  // find_nodal_neighbors() needs a data structure which is prepared by another function
48  std::vector<std::vector<const Elem *>> nodes_to_elem_map;
49  MeshTools::build_nodes_to_elem_map(mesh, nodes_to_elem_map);
50 
51  // Loop over the nodes and call find_nodal_neighbors()
52  {
53  std::vector<const Node*> neighbor_nodes;
54 
55  unsigned ctr = 0;
56  for (const auto & node : mesh.node_ptr_range())
57  {
58  MeshTools::find_nodal_neighbors(mesh, *node, nodes_to_elem_map, neighbor_nodes);
59 
60  // The entries in neighbor_nodes are just sorted according
61  // to memory address, which is somewhat arbitrary, so create
62  // a vector sorted by IDs for test purposes.
63  std::vector<dof_id_type> neighbor_node_ids(neighbor_nodes.size());
64  for (std::size_t i=0; i<neighbor_nodes.size(); ++i)
65  neighbor_node_ids[i] = neighbor_nodes[i]->id();
66  std::sort(neighbor_node_ids.begin(), neighbor_node_ids.end());
67 
68  // Compare to validation_data
69  for (std::size_t j=0; j<neighbor_node_ids.size(); ++j)
70  {
71  CPPUNIT_ASSERT_EQUAL( validation_data[2*ctr + j], neighbor_node_ids[j] );
72  }
73 
74  ++ctr;
75  }
76  }
77  }
78 
79 public:
80  void setUp() {}
81 
82  void tearDown() {}
83 
84  void testEdge2()
85  {
86  // 11 nodes, 2 neighbor entries per node
87  dof_id_type validation_data[22] =
88  {
90  0, 2,
91  1, 3,
92  2, 4,
93  3, 5,
94  4, 6,
95  5, 7,
96  6, 8,
97  7, 9,
98  8, 10,
100  };
101 
102  do_test(/*n_elem=*/10, EDGE2, validation_data);
103  }
104 
105 
106  void testEdge3()
107  {
108  // 11 nodes, 2 neighbor entries per node
109  dof_id_type validation_data[22] =
110  {
112  2, 4,
113  0, 1,
114  4, 6,
115  1, 3,
116  6, 8,
117  3, 5,
118  8, 10,
119  5, 7,
121  7, 9
122  };
123 
124  do_test(/*n_elem=*/5, EDGE3, validation_data);
125  }
126 
127 
128  void testEdge4()
129  {
130  // 10 nodes, 2 neighbor entries per node
131  dof_id_type validation_data[20] =
132  {
134  3, 5,
135  0, 3,
136  1, 2,
137  6, 8,
138  1, 6,
139  4, 5,
141  4, 9,
142  7, 8
143  };
144 
145  do_test(/*n_elem=*/3, EDGE4, validation_data);
146  }
147 
148 };
149 
150 
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::MeshTools::n_elem
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Count up the number of elements of a specific type (as defined by an iterator range).
Definition: mesh_tools.C:705
libMesh::EDGE4
Definition: enum_elem_type.h:37
NodalNeighborsTest
Definition: nodal_neighbors.C:13
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
NodalNeighborsTest::testEdge3
void testEdge3()
Definition: nodal_neighbors.C:106
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::MeshTools::build_nodes_to_elem_map
void build_nodes_to_elem_map(const MeshBase &mesh, std::vector< std::vector< dof_id_type >> &nodes_to_elem_map)
After calling this function the input vector nodes_to_elem_map will contain the node to element conne...
Definition: mesh_tools.C:248
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(NodalNeighborsTest)
NodalNeighborsTest::do_test
void do_test(unsigned n_elem, ElemType elem_type, dof_id_type *validation_data)
Definition: nodal_neighbors.C:35
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::MeshBase::node_ptr_range
virtual SimpleRange< node_iterator > node_ptr_range()=0
NodalNeighborsTest::setUp
void setUp()
Definition: nodal_neighbors.C:80
libMesh::DofObject::invalid_id
static const dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition: dof_object.h:421
libMesh::MeshTools::Generation::build_line
void build_line(UnstructuredMesh &mesh, const unsigned int nx, const Real xmin=0., const Real xmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 1D meshes.
Definition: mesh_generation.C:1480
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::MeshTools::find_nodal_neighbors
void find_nodal_neighbors(const MeshBase &mesh, const Node &n, const std::vector< std::vector< const Elem * >> &nodes_to_elem_map, std::vector< const Node * > &neighbors)
Given a mesh and a node in the mesh, the vector will be filled with every node directly attached to t...
Definition: mesh_tools.C:743
libmesh_cppunit.h
libMesh::EDGE3
Definition: enum_elem_type.h:36
NodalNeighborsTest::testEdge2
void testEdge2()
Definition: nodal_neighbors.C:84
test_comm.h
NodalNeighborsTest::tearDown
void tearDown()
Definition: nodal_neighbors.C:82
NodalNeighborsTest::testEdge4
void testEdge4()
Definition: nodal_neighbors.C:128
libMesh::EDGE2
Definition: enum_elem_type.h:35
libMesh::ElemType
ElemType
Defines an enum for geometric element types.
Definition: enum_elem_type.h:33