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