libMesh
nodal_neighbors.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <libmesh/libmesh.h>
8 #include <libmesh/node.h>
9 #include <libmesh/mesh_generation.h>
10 #include <libmesh/mesh_tools.h>
11 #include <libmesh/replicated_mesh.h>
12 
13 #include "test_comm.h"
14 
15 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
16 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
17 // warnings. These can be ignored in GCC as long as we wrap the
18 // offending code in appropriate pragmas. We can't get away with a
19 // single ignore_warnings.h inclusion at the beginning of this file,
20 // since the libmesh headers pull in a restore_warnings.h at some
21 // point. We also don't bother restoring warnings at the end of this
22 // file since it's not a header.
23 #include <libmesh/ignore_warnings.h>
24 
25 using namespace libMesh;
26 
27 class NodalNeighborsTest : public CppUnit::TestCase
28 {
37 public:
38  CPPUNIT_TEST_SUITE( NodalNeighborsTest );
39 
40  CPPUNIT_TEST( testEdge2 );
41  CPPUNIT_TEST( testEdge3 );
42  CPPUNIT_TEST( testEdge4 );
43 
44  CPPUNIT_TEST_SUITE_END();
45 
46 protected:
47 
48  // Builds a 1D mesh with the specified ElemType and number of elements
49  void do_test(unsigned n_elem,
50  ElemType elem_type,
51  dof_id_type * validation_data)
52  {
53  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/1);
54 
56  n_elem,
57  /*xmin=*/0.,
58  /*xmax=*/1.,
59  elem_type);
60 
61  // find_nodal_neighbors() needs a data structure which is prepared by another function
62  std::vector<std::vector<const Elem *>> nodes_to_elem_map;
63  MeshTools::build_nodes_to_elem_map(mesh, nodes_to_elem_map);
64 
65  // Loop over the nodes and call find_nodal_neighbors()
66  {
67  std::vector<const Node*> neighbor_nodes;
68 
69  unsigned ctr = 0;
70  for (const auto & node : mesh.node_ptr_range())
71  {
72  MeshTools::find_nodal_neighbors(mesh, *node, nodes_to_elem_map, neighbor_nodes);
73 
74  // The entries in neighbor_nodes are just sorted according
75  // to memory address, which is somewhat arbitrary, so create
76  // a vector sorted by IDs for test purposes.
77  std::vector<dof_id_type> neighbor_node_ids(neighbor_nodes.size());
78  for (std::size_t i=0; i<neighbor_nodes.size(); ++i)
79  neighbor_node_ids[i] = neighbor_nodes[i]->id();
80  std::sort(neighbor_node_ids.begin(), neighbor_node_ids.end());
81 
82  // Compare to validation_data
83  for (std::size_t j=0; j<neighbor_node_ids.size(); ++j)
84  {
85  CPPUNIT_ASSERT_EQUAL( validation_data[2*ctr + j], neighbor_node_ids[j] );
86  }
87 
88  ++ctr;
89  }
90  }
91  }
92 
93 public:
94  void setUp() {}
95 
96  void tearDown() {}
97 
98  void testEdge2()
99  {
100  // 11 nodes, 2 neighbor entries per node
101  dof_id_type validation_data[22] =
102  {
104  0, 2,
105  1, 3,
106  2, 4,
107  3, 5,
108  4, 6,
109  5, 7,
110  6, 8,
111  7, 9,
112  8, 10,
114  };
115 
116  do_test(/*n_elem=*/10, EDGE2, validation_data);
117  }
118 
119 
120  void testEdge3()
121  {
122  // 11 nodes, 2 neighbor entries per node
123  dof_id_type validation_data[22] =
124  {
126  2, 4,
127  0, 1,
128  4, 6,
129  1, 3,
130  6, 8,
131  3, 5,
132  8, 10,
133  5, 7,
135  7, 9
136  };
137 
138  do_test(/*n_elem=*/5, EDGE3, validation_data);
139  }
140 
141 
142  void testEdge4()
143  {
144  // 10 nodes, 2 neighbor entries per node
145  dof_id_type validation_data[20] =
146  {
148  3, 5,
149  0, 3,
150  1, 2,
151  6, 8,
152  1, 6,
153  4, 5,
155  4, 9,
156  7, 8
157  };
158 
159  do_test(/*n_elem=*/3, EDGE4, validation_data);
160  }
161 
162 };
163 
164 
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
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:656
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
ElemType
Defines an enum for geometric element types.
virtual SimpleRange< node_iterator > node_ptr_range() libmesh_override
MeshBase & mesh
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:257
The libMesh namespace provides an interface to certain functionality in the library.
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:699
void do_test(unsigned n_elem, ElemType elem_type, dof_id_type *validation_data)
static const dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition: dof_object.h:324
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.
CPPUNIT_TEST_SUITE_REGISTRATION(NodalNeighborsTest)
uint8_t dof_id_type
Definition: id_types.h:64