libMesh
which_node_am_i_test.C
Go to the documentation of this file.
1 #include <libmesh/elem.h>
2 #include <libmesh/reference_elem.h>
3 
4 // Unit test headers
5 #include "stream_redirector.h"
6 #include "libmesh_cppunit.h"
7 
8 using namespace libMesh;
9 
10 class WhichNodeAmITest : public CppUnit::TestCase
11 {
12 
13 public:
14  CPPUNIT_TEST_SUITE( WhichNodeAmITest );
15 #if LIBMESH_DIM > 2
16  CPPUNIT_TEST( testPyramids );
17  CPPUNIT_TEST( testPrisms );
18  CPPUNIT_TEST( testTets );
19  CPPUNIT_TEST( testHexes );
20 #endif
21  CPPUNIT_TEST_SUITE_END();
22 
23 public:
24 
25  void testPyramids()
26  {
27  // The last node on the right side (1) should be node 4 (apex node).
28  const Elem & pyr5 = ReferenceElem::get(PYRAMID5);
29  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4), pyr5.which_node_am_i(/*side=*/1, /*node=*/2));
30 
31  // Test the libmesh_asserts when they are enabled and exceptions
32  // are available. If exceptions aren't available, libmesh_assert
33  // simply aborts, so we can't unit test in that case.
34 #if !defined(NDEBUG) && defined(LIBMESH_ENABLE_EXCEPTIONS)
35  try
36  {
37  // Avoid sending confusing error messages to the console.
38  StreamRedirector stream_redirector;
39 
40  // Asking for the 4th node on a triangular face should throw.
41  unsigned int n = pyr5.which_node_am_i(1, 3);
42 
43  // We shouldn't get here if the line above throws. If we do
44  // get here, there's no way this assert will pass.
45  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(-1), n);
46  }
47  catch (...) {}
48 #endif
49 
50 #ifdef NDEBUG
51  // In optimized mode, we expect to get the "dummy" value 99.
52  unsigned int n = pyr5.which_node_am_i(1, 3);
53  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(99), n);
54 #endif
55 
56  // The last node on the right side (1) should be node 10.
57  const Elem & pyr13 = ReferenceElem::get(PYRAMID13);
58  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(10), pyr13.which_node_am_i(/*side=*/1, /*node=*/5));
59 
60  // The central node of the base should be node 13
61  const Elem & pyr14 = ReferenceElem::get(PYRAMID14);
62  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(13), pyr14.which_node_am_i(/*side=*/4, /*node=*/8));
63  }
64 
65 
66 
67  void testPrisms()
68  {
69  // A PRISM6 has four nodes on some sides and three nodes on others
70  const Elem & prism6 = ReferenceElem::get(PRISM6);
71  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4), prism6.which_node_am_i(/*side=*/4, /*node=*/1));
72 
73  // Test the libmesh_asserts when they are enabled and exceptions
74  // are available. If exceptions aren't available, libmesh_assert
75  // simply aborts, so we can't unit test in that case.
76 #if !defined(NDEBUG) && defined(LIBMESH_ENABLE_EXCEPTIONS)
77  try
78  {
79  // Avoid sending confusing error messages to the console.
80  StreamRedirector stream_redirector;
81 
82  // Asks for the 3rd node on a Tri face which only has
83  // indices 0, 1, and 2. Should throw an exception
84  // (libmesh_assert throws an exception) when NDEBUG is not
85  // defined.
86  unsigned int n = prism6.which_node_am_i(0, 3);
87 
88  // We shouldn't get here if the line above throws. If we do
89  // get here, there's no way this assert will pass.
90  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(-1), n);
91  }
92  catch (...) {}
93 #endif
94 
95 #ifdef NDEBUG
96  // In optimized mode, we expect to get the "dummy" value 99.
97  unsigned int n = prism6.which_node_am_i(0, 3);
98  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(99), n);
99 #endif
100 
101  // Test the Prism15.
102  const Elem & prism15 = ReferenceElem::get(PRISM15);
103  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), prism15.which_node_am_i(/*side=*/1, /*node=*/3));
104  }
105 
106 
107 
108  void testTets()
109  {
110  const Elem & tet4 = ReferenceElem::get(TET4);
111  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), tet4.which_node_am_i(/*side=*/0, /*node=*/0));
112 
113  // Node 4 is a mid-edge node on side 1.
114  const Elem & tet10 = ReferenceElem::get(TET10);
115  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4), tet10.which_node_am_i(/*side=*/1, /*node=*/3));
116  }
117 
118 
119 
120  void testHexes()
121  {
122  // Top left node on back side.
123  const Elem & hex8 = ReferenceElem::get(HEX8);
124  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(7), hex8.which_node_am_i(/*side=*/3, /*node=*/2));
125 
126  const Elem & hex20 = ReferenceElem::get(HEX20);
127  const Elem & hex27 = ReferenceElem::get(HEX27);
128 
129  // The vertices (i.e. the first 4 nodes on each side should match for all of the Hex types.
130  for (unsigned int side=0; side<hex8.n_sides(); ++side)
131  for (unsigned int node=0; node<4; ++node)
132  {
133  // Make sure the Hex8 and Hex20 implementations agree.
134  CPPUNIT_ASSERT_EQUAL(hex8.which_node_am_i(side, node),
135  hex20.which_node_am_i(side, node));
136 
137  // Make sure the Hex20 and Hex27 implementations agree.
138  CPPUNIT_ASSERT_EQUAL(hex20.which_node_am_i(side, node),
139  hex27.which_node_am_i(side, node));
140  }
141  }
142 };
143 
libMesh::HEX20
Definition: enum_elem_type.h:48
libMesh::PRISM6
Definition: enum_elem_type.h:50
libMesh::Elem::which_node_am_i
virtual unsigned int which_node_am_i(unsigned int side, unsigned int side_node) const =0
libMesh::HEX8
Definition: enum_elem_type.h:47
WhichNodeAmITest::testPrisms
void testPrisms()
Definition: which_node_am_i_test.C:67
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::TET10
Definition: enum_elem_type.h:46
WhichNodeAmITest::testPyramids
void testPyramids()
Definition: which_node_am_i_test.C:25
libMesh::TET4
Definition: enum_elem_type.h:45
libMesh::PRISM15
Definition: enum_elem_type.h:51
libMesh::HEX27
Definition: enum_elem_type.h:49
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(WhichNodeAmITest)
stream_redirector.h
WhichNodeAmITest
Definition: which_node_am_i_test.C:10
libMesh::ReferenceElem::get
const Elem & get(const ElemType type_in)
Definition: reference_elem.C:237
libMesh::PYRAMID5
Definition: enum_elem_type.h:53
libmesh_cppunit.h
WhichNodeAmITest::testHexes
void testHexes()
Definition: which_node_am_i_test.C:120
WhichNodeAmITest::testTets
void testTets()
Definition: which_node_am_i_test.C:108
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
StreamRedirector
This class uses RAII to control redirecting the libMesh::err stream to NULL and restoring it around s...
Definition: stream_redirector.h:8
libMesh::Elem::n_sides
virtual unsigned int n_sides() const =0
libMesh::PYRAMID13
Definition: enum_elem_type.h:54
libMesh::PYRAMID14
Definition: enum_elem_type.h:55