Skip to content

Commit 6c7b45d

Browse files
committed
ElemCorner improvements
- Add 1D edge support for at vertex - Add 2D support/testing for being at an edge (a side) - Add additional testing
1 parent 4a74a03 commit 6c7b45d

3 files changed

Lines changed: 98 additions & 65 deletions

File tree

include/geom/elem_corner.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#include "libmesh/libmesh_common.h"
2424

25-
#if LIBMESH_DIM > 1
26-
2725
#include "libmesh/elem.h"
2826

2927
#include <utility> // std::pair
@@ -115,7 +113,6 @@ class ElemCorner : public std::pair<unsigned short, unsigned short>
115113
second = Elem::invalid_vertex;
116114
}
117115

118-
#if LIBMESH_DIM > 2
119116
/**
120117
* @returns true if at an edge
121118
*/
@@ -128,6 +125,7 @@ class ElemCorner : public std::pair<unsigned short, unsigned short>
128125
{
129126
libmesh_assert_not_equal_to(v1, Elem::invalid_vertex);
130127
libmesh_assert_not_equal_to(v2, Elem::invalid_vertex);
128+
libmesh_assert_not_equal_to(v1, v2);
131129
return (first == v1 && second == v2) || (first == v2 && second == v1);
132130
}
133131
/**
@@ -148,6 +146,7 @@ class ElemCorner : public std::pair<unsigned short, unsigned short>
148146
{
149147
libmesh_assert_not_equal_to(v1, Elem::invalid_vertex);
150148
libmesh_assert_not_equal_to(v2, Elem::invalid_vertex);
149+
libmesh_assert_not_equal_to(v1, v2);
151150
first = v1;
152151
second = v2;
153152
}
@@ -161,7 +160,6 @@ class ElemCorner : public std::pair<unsigned short, unsigned short>
161160
* @returns The edge when at an edge
162161
*/
163162
std::unique_ptr<const Elem> build_edge(const Elem & elem) const;
164-
#endif
165163

166164
/**
167165
* @returns The vertex point when at a vertex
@@ -183,6 +181,4 @@ class ElemCorner : public std::pair<unsigned short, unsigned short>
183181

184182
std::ostream & operator<<(std::ostream & os, const libMesh::ElemCorner & elem_corner);
185183

186-
#endif // LIBMESH_DIM > 1
187-
188184
#endif // LIBMESH_ELEMCORNER_H

src/geom/elem_corner.C

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
// Local includes
2020
#include "libmesh/elem_corner.h"
2121

22-
#if LIBMESH_DIM > 1
2322

2423
namespace libMesh
2524
{
2625

27-
#if LIBMESH_DIM > 2
2826
bool ElemCorner::at_edge(const Elem & elem, const unsigned short e) const
2927
{
28+
libmesh_assert_greater(elem.dim(), 1);
3029
libmesh_assert_less(e, elem.n_edges());
3130
if (!at_edge())
3231
return false;
@@ -36,6 +35,7 @@ bool ElemCorner::at_edge(const Elem & elem, const unsigned short e) const
3635

3736
std::unique_ptr<const Elem> ElemCorner::build_edge(const Elem & elem) const
3837
{
38+
libmesh_assert_greater(elem.dim(), 1);
3939
libmesh_assert(at_edge());
4040
libmesh_assert_less(first, elem.n_vertices());
4141
libmesh_assert_less(second, elem.n_vertices());
@@ -46,17 +46,14 @@ std::unique_ptr<const Elem> ElemCorner::build_edge(const Elem & elem) const
4646

4747
libmesh_error_msg("Element does not contain vertices in ElemCorner");
4848
}
49-
#endif
5049

5150
std::string ElemCorner::print() const
5251
{
5352
std::stringstream oss;
5453
if (at_vertex())
5554
oss << "at vertex " << vertex();
56-
#if LIBMESH_DIM > 2
5755
else if (at_edge())
5856
oss << "at edge with vertices " << first << " and " << second;
59-
#endif
6057
else
6158
oss << "not at corner";
6259
return oss.str();
@@ -68,11 +65,9 @@ bool ElemCorner::is_valid(const Elem & elem, const Point & point, const Real tol
6865
libmesh_assert(second == Elem::invalid_vertex || second < elem.n_vertices());
6966

7067
if (at_vertex())
71-
return vertex_point(elem).absolute_fuzzy_equals(point, tol);
72-
#if LIBMESH_DIM > 2
73-
if (elem.dim() == 3 && at_edge())
68+
return vertex_point(elem).relative_fuzzy_equals(point, tol);
69+
if (at_edge())
7470
return build_edge(elem)->contains_point(point, tol);
75-
#endif
7671

7772
return true;
7873
}
@@ -85,5 +80,3 @@ operator<<(std::ostream & os, const libMesh::ElemCorner & elem_corner)
8580
os << elem_corner.print();
8681
return os;
8782
}
88-
89-
#endif // LIBMESH_DIM > 1

tests/geom/elem_corner_test.C

Lines changed: 92 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,68 +23,112 @@ public:
2323
{
2424
LOG_UNIT_TEST;
2525

26-
// QUAD elem for vertex testing
27-
Mesh mesh_2d (*TestCommWorld);
28-
MeshTools::Generation::build_square (mesh_2d, 1, 1);
29-
const auto & quad = mesh_2d.elem_ref(0);
30-
3126
// Default constructor: unvalid state
3227
const ElemCorner default_corner;
3328
CPPUNIT_ASSERT(default_corner.is_invalid());
3429
CPPUNIT_ASSERT(!default_corner.at_corner());
3530
CPPUNIT_ASSERT(!default_corner.at_vertex());
36-
#if LIBMESH_DIM > 2
3731
CPPUNIT_ASSERT(!default_corner.at_edge());
38-
#endif // LIBMESH_DIM > 2
3932
CPPUNIT_ASSERT_EQUAL((std::string)"not at corner", default_corner.print());
40-
CPPUNIT_ASSERT(default_corner.is_valid(quad, quad.point(0)));
41-
42-
// At vertex
43-
const ElemCorner at_vertex(0, Elem::invalid_vertex);
44-
CPPUNIT_ASSERT(!at_vertex.is_invalid());
45-
CPPUNIT_ASSERT(at_vertex.at_corner());
46-
CPPUNIT_ASSERT(at_vertex.at_vertex());
47-
#if LIBMESH_DIM> 2
48-
CPPUNIT_ASSERT(!at_vertex.at_edge());
49-
CPPUNIT_ASSERT(!at_vertex.at_edge(0, 1));
50-
CPPUNIT_ASSERT(!at_vertex.at_edge(quad, 0));
51-
#endif // LIBMESH_DIM > 2
52-
CPPUNIT_ASSERT(at_vertex.at_vertex(0));
53-
CPPUNIT_ASSERT(!at_vertex.at_vertex(1));
54-
CPPUNIT_ASSERT_EQUAL(at_vertex.vertex(), (unsigned short)0);
55-
CPPUNIT_ASSERT(at_vertex.is_valid(quad, quad.point(0)));
56-
CPPUNIT_ASSERT(!at_vertex.is_valid(quad, quad.point(2)));
33+
34+
// EDGE elem for vertex testing
35+
Mesh mesh_1d (*TestCommWorld);
36+
MeshTools::Generation::build_line(mesh_1d, 1);
37+
const auto edge = mesh_1d.elem_ptr(0);
38+
39+
// At edge vertex
40+
const ElemCorner at_edge_vertex(1, Elem::invalid_vertex);
41+
CPPUNIT_ASSERT(!at_edge_vertex.is_invalid());
42+
CPPUNIT_ASSERT(at_edge_vertex.at_corner());
43+
CPPUNIT_ASSERT(at_edge_vertex.at_vertex());
44+
CPPUNIT_ASSERT(!at_edge_vertex.at_edge());
45+
CPPUNIT_ASSERT(at_edge_vertex.at_vertex(1));
46+
CPPUNIT_ASSERT_EQUAL(at_edge_vertex.vertex(), (unsigned short)1);
47+
if (edge)
48+
for (const auto v : edge->vertex_index_range())
49+
{
50+
CPPUNIT_ASSERT_EQUAL(at_edge_vertex.at_vertex(v), v == 1);
51+
CPPUNIT_ASSERT_EQUAL(at_edge_vertex.is_valid(*edge, edge->point(v)), v == 1);
52+
}
53+
54+
#if LIBMESH_DIM > 1
55+
// QUAD elem for vertex testing
56+
Mesh mesh_2d (*TestCommWorld);
57+
MeshTools::Generation::build_square (mesh_2d, 1, 1);
58+
const auto quad = mesh_2d.elem_ptr(0);
59+
60+
// At quad vertex
61+
const ElemCorner at_quad_vertex(0, Elem::invalid_vertex);
62+
CPPUNIT_ASSERT(!at_quad_vertex.is_invalid());
63+
CPPUNIT_ASSERT(at_quad_vertex.at_corner());
64+
CPPUNIT_ASSERT(at_quad_vertex.at_vertex());
65+
CPPUNIT_ASSERT(!at_quad_vertex.at_edge());
66+
CPPUNIT_ASSERT(!at_quad_vertex.at_edge(0, 1));
67+
CPPUNIT_ASSERT(at_quad_vertex.at_vertex(0));
68+
CPPUNIT_ASSERT(!at_quad_vertex.at_vertex(1));
69+
CPPUNIT_ASSERT_EQUAL(at_quad_vertex.vertex(), (unsigned short)0);
70+
if (quad)
71+
{
72+
for (const auto v : quad->vertex_index_range())
73+
CPPUNIT_ASSERT(default_corner.is_valid(*quad, quad->point(v)));
74+
for (const auto e : quad->edge_index_range())
75+
CPPUNIT_ASSERT(!at_quad_vertex.at_edge(*quad, e));
76+
for (const auto v : quad->vertex_index_range())
77+
CPPUNIT_ASSERT_EQUAL(at_quad_vertex.is_valid(*quad, quad->point(v)), v == 0);
78+
}
79+
80+
// At quad "edge" (a side)
81+
const ElemCorner at_quad_edge(1, 2);
82+
CPPUNIT_ASSERT(!at_quad_edge.is_invalid());
83+
CPPUNIT_ASSERT(at_quad_edge.at_corner());
84+
CPPUNIT_ASSERT(!at_quad_edge.at_vertex());
85+
CPPUNIT_ASSERT(at_quad_edge.at_edge());
86+
CPPUNIT_ASSERT(!at_quad_edge.at_edge(0, 1));
87+
CPPUNIT_ASSERT(at_quad_edge.at_edge(1, 2));
88+
CPPUNIT_ASSERT_EQUAL(at_quad_edge.edge_vertices().first, (unsigned short)1);
89+
CPPUNIT_ASSERT_EQUAL(at_quad_edge.edge_vertices().second, (unsigned short)2);
90+
if (quad)
91+
{
92+
CPPUNIT_ASSERT(at_quad_edge.is_valid(*quad, 0.5 * (quad->point(1) + quad->point(2))));
93+
for (const auto e : quad->edge_index_range())
94+
CPPUNIT_ASSERT_EQUAL(at_quad_edge.at_edge(*quad, e), e == 1);
95+
}
96+
#endif
5797

5898
#if LIBMESH_DIM > 2
5999
// HEX elem for edge testing
60100
Mesh mesh_3d (*TestCommWorld);
61101
MeshTools::Generation::build_cube (mesh_3d, 1, 1, 1);
62-
const auto & hex = mesh_3d.elem_ref(0);
102+
const auto hex = mesh_3d.elem_ptr(0);
63103

64104
// At edge
65-
const ElemCorner at_edge(0, 1);
66-
CPPUNIT_ASSERT(!at_edge.is_invalid());
67-
CPPUNIT_ASSERT(at_edge.at_corner());
68-
CPPUNIT_ASSERT(!at_edge.at_vertex());
69-
CPPUNIT_ASSERT(!at_edge.at_vertex(0));
70-
CPPUNIT_ASSERT(at_edge.at_edge());
71-
CPPUNIT_ASSERT(at_edge.at_edge(0, 1));
72-
CPPUNIT_ASSERT(at_edge.at_edge(1, 0));
73-
CPPUNIT_ASSERT(at_edge.at_edge(hex, 0));
74-
CPPUNIT_ASSERT_EQUAL(at_edge.edge_vertices().first, (unsigned short)0);
75-
CPPUNIT_ASSERT_EQUAL(at_edge.edge_vertices().second, (unsigned short)1);
76-
CPPUNIT_ASSERT(!at_edge.is_invalid());
77-
CPPUNIT_ASSERT(at_edge.is_valid(hex, (hex.point(0) + hex.point(1)) * 0.5));
78-
CPPUNIT_ASSERT(at_edge.is_valid(hex, hex.point(0)));
79-
CPPUNIT_ASSERT(at_edge.is_valid(hex, hex.point(1)));
80-
CPPUNIT_ASSERT(!at_edge.is_valid(hex, (hex.point(2)) * 0.5));
81-
82-
// build_edge
83-
auto edge_elem = at_edge.build_edge(hex);
84-
CPPUNIT_ASSERT((edge_elem->point(0).absolute_fuzzy_equals(hex.point(0))
85-
&& edge_elem->point(1).absolute_fuzzy_equals(hex.point(1))) ||
86-
((edge_elem->point(1).absolute_fuzzy_equals(hex.point(0))
87-
&& edge_elem->point(0).absolute_fuzzy_equals(hex.point(1)))));
105+
const ElemCorner at_hex_edge(0, 1);
106+
CPPUNIT_ASSERT(!at_hex_edge.is_invalid());
107+
CPPUNIT_ASSERT(at_hex_edge.at_corner());
108+
CPPUNIT_ASSERT(!at_hex_edge.at_vertex());
109+
CPPUNIT_ASSERT(!at_hex_edge.at_vertex(0));
110+
CPPUNIT_ASSERT(at_hex_edge.at_edge());
111+
CPPUNIT_ASSERT(at_hex_edge.at_edge(0, 1));
112+
CPPUNIT_ASSERT(at_hex_edge.at_edge(1, 0));
113+
CPPUNIT_ASSERT_EQUAL(at_hex_edge.edge_vertices().first, (unsigned short)0);
114+
CPPUNIT_ASSERT_EQUAL(at_hex_edge.edge_vertices().second, (unsigned short)1);
115+
CPPUNIT_ASSERT(!at_hex_edge.is_invalid());
116+
if (hex)
117+
{
118+
CPPUNIT_ASSERT(at_hex_edge.is_valid(*hex, (hex->point(0) + hex->point(1)) * 0.5));
119+
for (const auto e : hex->edge_index_range())
120+
CPPUNIT_ASSERT_EQUAL(at_hex_edge.at_edge(*hex, e), e == 0);
121+
for (const auto v : hex->vertex_index_range())
122+
CPPUNIT_ASSERT_EQUAL(at_hex_edge.is_valid(*hex, hex->point(v)), v == 0 || v == 1);
123+
CPPUNIT_ASSERT(!at_hex_edge.is_valid(*hex, (hex->point(2)) * 0.5));
124+
125+
// build_edge
126+
auto edge_elem = at_hex_edge.build_edge(*hex);
127+
CPPUNIT_ASSERT((edge_elem->point(0).absolute_fuzzy_equals(hex->point(0))
128+
&& edge_elem->point(1).absolute_fuzzy_equals(hex->point(1))) ||
129+
((edge_elem->point(1).absolute_fuzzy_equals(hex->point(0))
130+
&& edge_elem->point(0).absolute_fuzzy_equals(hex->point(1)))));
131+
}
88132
#endif // LIBMESH_DIM > 2
89133

90134
// For testing non-const methods

0 commit comments

Comments
 (0)