https://mooseframework.inl.gov
TraceRayToolsTest.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "gtest/gtest.h"
11 
12 #include "TraceRayTools.h"
13 
14 #include "libmesh/mesh_generation.h"
15 #include "libmesh/replicated_mesh.h"
16 
17 std::unique_ptr<UnstructuredMesh>
18 traceRayToolsTestMesh(const int type)
19 {
20  Parallel::Communicator comm;
21  std::unique_ptr<UnstructuredMesh> mesh = std::make_unique<ReplicatedMesh>(comm);
22 
23  const unsigned int n = 2;
24  const Real min = 0;
25  const Real max = 1;
26  switch ((ElemType)type)
27  {
28  case EDGE2:
29  case EDGE3:
30  case EDGE4:
31  MeshTools::Generation::build_line(*mesh, n, min, max, (ElemType)type);
32  break;
33  case QUAD4:
34  case QUAD8:
35  case QUAD9:
36  case TRI3:
37  case TRI6:
38  case TRI7:
39  MeshTools::Generation::build_square(*mesh, n, n, min, max, min, max, (ElemType)type);
40  break;
41  case HEX8:
42  case HEX20:
43  case HEX27:
44  case TET4:
45  case TET10:
46  case TET14:
47  case PYRAMID5:
48  case PYRAMID13:
49  case PYRAMID14:
50  case PRISM6:
51  case PRISM15:
52  case PRISM18:
53  MeshTools::Generation::build_cube(
54  *mesh, n, n, n, min, max, min, max, min, max, (ElemType)type);
55  break;
56  default:
57  break;
58  }
59 
60  if (mesh)
61  mesh->prepare_for_use();
62 
63  return mesh;
64 }
65 
66 TEST(TraceRayToolsTest, withinEdge)
67 {
68  ElemExtrema extrema;
69 
70  for (const auto type : TraceRayTools::TRACEABLE_ELEMTYPES)
71  {
72  auto mesh = traceRayToolsTestMesh(type);
73  EXPECT_TRUE(mesh);
74 
75  if (mesh->mesh_dimension() != 3)
76  continue;
77 
78  for (const auto elem : mesh->element_ptr_range())
79  {
80  for (const auto e : elem->edge_index_range())
81  {
82  extrema.invalidate();
83  EXPECT_TRUE(TraceRayTools::withinEdge(elem, elem->build_edge_ptr(e)->vertex_average(), extrema));
84  EXPECT_TRUE(extrema.atEdge(elem->nodes_on_edge(e)[0], elem->nodes_on_edge(e)[1]));
85  }
86  for (const auto n : elem->node_index_range())
87  if (elem->is_vertex(n))
88  {
89  extrema.invalidate();
90  EXPECT_TRUE(TraceRayTools::withinEdge(elem, elem->point(n), extrema));
91 
92  bool extrema_correct = false;
93  for (const auto e : elem->edge_index_range())
94  if (!extrema_correct && elem->is_node_on_edge(n, e))
95  extrema_correct =
96  extrema.atEdge(elem->nodes_on_edge(e)[0], elem->nodes_on_edge(e)[1]);
97  EXPECT_TRUE(extrema_correct);
98  }
99  }
100  }
101 }
102 
103 TEST(TraceRayToolsTest, withinEdgeOnSide)
104 {
105  ElemExtrema extrema;
106 
107  for (const auto type : TraceRayTools::TRACEABLE_ELEMTYPES)
108  {
109  auto mesh = traceRayToolsTestMesh(type);
110  EXPECT_TRUE(mesh);
111 
112  if (mesh->mesh_dimension() != 3)
113  continue;
114 
115  for (const auto elem : mesh->element_ptr_range())
116  {
117  for (const auto s : elem->side_index_range())
118  {
119  extrema.invalidate();
120  EXPECT_FALSE(
121  TraceRayTools::withinEdgeOnSide(elem, elem->build_side_ptr(s)->vertex_average(), s, extrema));
122  EXPECT_TRUE(extrema.isInvalid());
123 
124  for (const auto e : elem->edge_index_range())
125  if (elem->is_edge_on_side(e, s))
126  {
127  extrema.invalidate();
129  elem, elem->build_edge_ptr(e)->vertex_average(), s, extrema));
130  }
131  }
132  for (const auto n : elem->node_index_range())
133  if (elem->is_vertex(n))
134  for (const auto s : elem->side_index_range())
135  if (elem->is_node_on_side(n, s))
136  {
137  extrema.invalidate();
138  EXPECT_TRUE(TraceRayTools::withinEdgeOnSide(elem, elem->point(n), s, extrema));
139 
140  bool extrema_correct = false;
141  for (const auto e : elem->edge_index_range())
142  if (!extrema_correct && elem->is_node_on_edge(n, e))
143  extrema_correct =
144  extrema.atEdge(elem->nodes_on_edge(e)[0], elem->nodes_on_edge(e)[1]);
145  EXPECT_TRUE(extrema_correct);
146  }
147  }
148  }
149 }
150 
151 TEST(TraceRayToolsTest, atVertex)
152 {
153  for (const auto type : TraceRayTools::TRACEABLE_ELEMTYPES)
154  {
155  auto mesh = traceRayToolsTestMesh(type);
156  EXPECT_TRUE(mesh);
157 
158  for (const auto elem : mesh->element_ptr_range())
159  {
160  EXPECT_EQ(TraceRayTools::atVertex(elem, elem->vertex_average()), RayTracingCommon::invalid_vertex);
161  for (const auto n : elem->node_index_range())
162  if (elem->is_vertex(n))
163  {
164  EXPECT_EQ((unsigned short)n, TraceRayTools::atVertex(elem, elem->point(n)));
165  }
166  }
167  }
168 }
169 
170 TEST(TraceRayToolsTest, atVertexOnSide)
171 {
172  for (const auto type : TraceRayTools::TRACEABLE_ELEMTYPES)
173  {
174  auto mesh = traceRayToolsTestMesh(type);
175  EXPECT_TRUE(mesh);
176 
177  for (const auto elem : mesh->element_ptr_range())
178  for (const auto s : elem->side_index_range())
179  {
180  if (elem->dim() > 1)
181  {
182  EXPECT_EQ(TraceRayTools::atVertexOnSide(elem, elem->build_side_ptr(s)->vertex_average(), s),
184  }
185 
186  for (const auto n : elem->nodes_on_side(s))
187  if (elem->is_vertex(n))
188  {
189  EXPECT_EQ(TraceRayTools::atVertexOnSide(elem, elem->point(n), s), (unsigned short)n);
190  }
191  }
192  }
193 }
194 
195 TEST(TraceRayToolsTest, findPointNeighbors)
196 {
200  std::vector<const Elem *> active_neighbor_children;
201  std::vector<NeighborInfo> neighbor_info;
202  std::set<const Elem *> libmesh_neighbor_set;
203 
204  for (const auto type : TraceRayTools::TRACEABLE_ELEMTYPES)
205  {
206  auto mesh = traceRayToolsTestMesh(type);
207  EXPECT_TRUE(mesh);
208 
209  for (const auto elem : mesh->element_ptr_range())
210  {
211  for (const auto n : elem->node_index_range())
212  {
214  elem->point(n),
215  neighbor_set,
216  neighbor_untested_set,
217  neighbor_next_untested_set,
218  active_neighbor_children,
219  neighbor_info);
220  elem->find_point_neighbors(elem->point(n), libmesh_neighbor_set);
221  for (const auto & info : neighbor_info)
222  {
223  for (const auto s : info._sides)
224  EXPECT_TRUE(info._elem->build_side_ptr(s)->contains_point(elem->point(n)));
225  EXPECT_TRUE(libmesh_neighbor_set.count(info._elem));
226  }
227  }
228 
229  for (const auto s : elem->side_index_range())
230  {
231  const auto centroid = elem->build_side_ptr(s)->vertex_average();
233  centroid,
234  neighbor_set,
235  neighbor_untested_set,
236  neighbor_next_untested_set,
237  active_neighbor_children,
238  neighbor_info);
239  elem->find_point_neighbors(centroid, libmesh_neighbor_set);
240  for (const auto & info : neighbor_info)
241  {
242  for (const auto other_s : info._sides)
243  EXPECT_TRUE(info._elem->build_side_ptr(other_s)->contains_point(centroid));
244  EXPECT_TRUE(libmesh_neighbor_set.count(info._elem));
245  }
246  }
247 
248  if (elem->dim() == 3)
249  for (const auto e : elem->edge_index_range())
250  {
251  const auto centroid = elem->build_edge_ptr(e)->vertex_average();
253  centroid,
254  neighbor_set,
255  neighbor_untested_set,
256  neighbor_next_untested_set,
257  active_neighbor_children,
258  neighbor_info);
259  elem->find_point_neighbors(centroid, libmesh_neighbor_set);
260  for (const auto & info : neighbor_info)
261  {
262  for (const auto s : info._sides)
263  EXPECT_TRUE(info._elem->build_side_ptr(s)->contains_point(centroid));
264  EXPECT_TRUE(libmesh_neighbor_set.count(info._elem));
265  }
266  }
267  }
268  }
269 }
270 
271 TEST(TraceRayToolsTest, isWithinSegment)
272 {
273  EXPECT_TRUE(TraceRayTools::isWithinSegment(Point(0, 1, 2), Point(1, 1, 2), Point(0.5, 1, 2)));
274  EXPECT_TRUE(TraceRayTools::isWithinSegment(Point(5, 6, 7), Point(8, 9, 10), Point(5, 6, 7)));
275  EXPECT_TRUE(TraceRayTools::isWithinSegment(Point(2, 3, 4), Point(4, 5, 6), Point(4, 5, 6)));
276  EXPECT_FALSE(TraceRayTools::isWithinSegment(Point(1, 2, 3), Point(4, 5, 6), Point(10, 0, 0)));
277 }
ElemType
static const unsigned short invalid_vertex
Identifier for an invalid vertex index.
bool withinEdgeOnSide(const Elem *const elem, const Point &point, const unsigned short side, ElemExtrema &extrema)
Determines if a point is within an edge on the side of an element.
void findPointNeighbors(const Elem *const elem, const Point &point, MooseUtils::StaticallyAllocatedSet< const Elem *, MAX_POINT_NEIGHBORS > &neighbor_set, MooseUtils::StaticallyAllocatedSet< const Elem *, MAX_POINT_NEIGHBORS > &untested_set, MooseUtils::StaticallyAllocatedSet< const Elem *, MAX_POINT_NEIGHBORS > &next_untested_set, std::vector< const Elem *> active_neighbor_children, std::vector< NeighborInfo > &info)
Rewrite of the find_point_neighbors function in libMesh, instead using a statically allocated set: re...
QUAD8
HEX8
MPI_Info info
EDGE4
bool isWithinSegment(const Point &segment1, const Point &segment2, const Point &point, const Real tolerance=TRACE_TOLERANCE)
Checks whether or not a point is within a line segment.
TET10
TEST(TraceRayToolsTest, withinEdge)
PRISM15
MeshBase & mesh
bool withinEdge(const Elem *elem, const Point &point, ElemExtrema &extrema, const Real tolerance=TRACE_TOLERANCE)
Determines if a point is within an edge on an element.
Helper for defining if at an element&#39;s edge, vertex, or neither.
Definition: ElemExtrema.h:25
HEX20
bool isInvalid() const
Definition: ElemExtrema.h:48
unsigned short atVertexOnSide(const Elem *elem, const Point &point, const unsigned short side)
Determines if a point is at a vertex on the side of en element.
auto max(const L &left, const R &right)
TRI3
QUAD4
TET4
TRI6
std::unique_ptr< UnstructuredMesh > traceRayToolsTestMesh(const int type)
const std::set< int > TRACEABLE_ELEMTYPES
The element types that are traceable.
Definition: TraceRayTools.C:42
HEX27
TET14
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
EDGE2
void invalidate()
Invalidates the current state.
Definition: ElemExtrema.h:87
PYRAMID5
TRI7
QUAD9
PYRAMID13
PRISM6
PRISM18
unsigned short atVertex(const Elem *elem, const Point &point)
Determines if a point is at a vertex of an element.
auto min(const L &left, const R &right)
EDGE3
bool atEdge() const
Definition: ElemExtrema.h:71
PYRAMID14