https://mooseframework.inl.gov
TraceRay.h
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 #pragma once
11 
12 // Local includes
13 #include "DebugRay.h"
14 #include "ElemExtrema.h"
15 #include "NeighborInfo.h"
16 #include "RayTracingCommon.h"
17 #include "TraceRayBndElement.h"
18 
19 // MOOSE Includes
20 #include "MooseMesh.h"
21 #include "MooseHashing.h"
22 #include "MooseTypes.h"
23 #include "StaticallyAllocatedSet.h"
24 
25 // libMesh includes
26 #include "libmesh/enum_elem_type.h"
27 #include "libmesh/elem_side_builder.h"
28 
29 // Forward declarations
30 class Ray;
32 class RayKernelBase;
33 class RayTracingStudy;
34 class RayTracingObject;
35 struct TraceData;
36 namespace libMesh
37 {
38 class BoundaryInfo;
39 class Edge;
40 class Mesh;
41 }
42 
46 class TraceRay
47 {
48 public:
49  TraceRay(RayTracingStudy & study, const THREAD_ID tid);
50 
51  virtual ~TraceRay() {}
52 
57  {
58  INTERSECTIONS = 0, // intersections
59  FACE_HITS = 1, // intersections at an element face
60  VERTEX_HITS = 2, // intersections at an element vertex
61  EDGE_HITS = 3, // intersections at an element edge
62  MOVED_THROUGH_NEIGHBORS = 4, // intersections through point/vertex/edge neighbors
63  BACKFACE_CULLING_SUCCESSES = 5, // face intersection calls that backface culling worked on
64  BACKFACE_CULLING_FAILURES = 6, // face interesction calls that backface culling failed on
65  INTERSECTION_CALLS = 7, // face intersection calls made
66  VERTEX_NEIGHBOR_BUILDS = 8, // builds for neighbors at a vertex
67  VERTEX_NEIGHBOR_LOOKUPS = 9, // lookups for neighbors at a vertex
68  EDGE_NEIGHBOR_BUILDS = 10, // builds for neighbors on an edge
69  EDGE_NEIGHBOR_LOOKUPS = 11, // lookups for neighborson an edge
70  POINT_NEIGHBOR_BUILDS = 12, // builds for point neighbors (non edge/vertex)
71  FAILED_TRACES = 13, // rays that fail (allowed when _tolerate_failure == true),
72  ENDED_STATIONARY = 14 // rays the end because they are stationary
73  };
74 
78  void preExecute();
79 
83  void meshChanged();
84 
88  void trace(const std::shared_ptr<Ray> & ray);
89 
93  std::vector<unsigned long long int> results() const { return _results; }
94 
105  void setBackfaceCulling(const bool backface_culling) { _backface_culling = backface_culling; }
106 
110  const std::shared_ptr<Ray> * const & currentRay() const { return _current_ray; }
114  const Elem * const & currentElem() const { return _current_elem; }
121  const Point & currentIntersectionPoint() const { return _intersection_point; }
127  const Point & currentIncomingPoint() const { return _incoming_point; }
133  const unsigned short & currentIncomingSide() const { return _incoming_side; }
140  const unsigned short & currentIntersectedSide() const { return _intersected_side; }
155  const BoundaryID & currentBoundaryID() const { return _current_bnd_id; }
160 
166  const std::vector<NeighborInfo> & getVertexNeighbors(const Elem * elem, const Node * vertex);
172  const std::vector<NeighborInfo> & getVertexNeighbors(const Elem * elem,
173  const unsigned short vertex);
180  const std::vector<NeighborInfo> &
181  getEdgeNeighbors(const Elem * elem,
182  const std::pair<const Node *, const Node *> & vertices,
183  const Point & point);
190  const std::vector<NeighborInfo> &
191  getEdgeNeighbors(const Elem * elem,
192  const std::pair<unsigned short, unsigned short> & vertices,
193  const Point & point);
199  const std::vector<NeighborInfo> & getPointNeighbors(const Elem * elem, const Point & point);
203  const std::vector<NeighborInfo> &
204  getNeighbors(const Elem * elem, const ElemExtrema & extrema, const Point & point);
205 
206 private:
210  void onSegment(const std::shared_ptr<Ray> & ray);
211 
215  void onBoundary(const std::shared_ptr<Ray> & ray, const bool external);
216 
221  void onCompleteTrace(const std::shared_ptr<Ray> & ray);
226  void onContinueTrace(const std::shared_ptr<Ray> & /* ray */);
231  void onTrajectoryChanged(const std::shared_ptr<Ray> & ray);
237  void onSubdomainChanged(const std::shared_ptr<Ray> & ray, const bool same_ray);
238 
244  std::string failTraceMessage(const std::string & reason, const int line = -1);
245 
252  void failTrace(const std::string & reason, const bool warning, const int line = -1);
253 
258  {
259  NO_EXIT = 0, // doesn't exit
260  HIT_FACE = 1, // exits through a face
261  HIT_VERTEX = 2, // exits through a vertex
262  HIT_EDGE = 3 // exits through an edge
263  };
264 
278  ExitsElemResult exitsElem(const Elem * elem,
279  const ElemType elem_type,
280  const unsigned short incoming_side,
281  Point & intersection_point,
282  unsigned short & intersected_side,
283  ElemExtrema & intersected_extrema,
284  Real & intersection_distance,
285  const Point * normals);
286 
302  template <typename T, typename FirstOrderT>
303  typename std::enable_if<!std::is_base_of<Edge, T>::value, bool>::type
304  exitsElem(const Elem * elem,
305  const unsigned short incoming_side,
306  Point & intersection_point,
307  unsigned short & intersected_side,
308  ElemExtrema & intersected_extrema,
309  Real & intersection_distance,
310  const Point * normals);
311 
327  template <typename T, typename FirstOrderT>
328  typename std::enable_if<std::is_base_of<Edge, T>::value, bool>::type
329  exitsElem(const Elem * elem,
330  const unsigned short incoming_side,
331  Point & intersection_point,
332  unsigned short & intersected_side,
333  ElemExtrema & intersected_extrema,
334  Real & intersection_distance,
335  const Point * normals);
336 
345  ExitsElemResult moveThroughNeighbors(const std::vector<NeighborInfo> & neighbors,
346  const Elem * last_elem,
347  const Elem *& best_elem,
348  unsigned short & best_elem_incoming_side);
349 
360  ExitsElemResult moveThroughNeighbor(const NeighborInfo & neighbor_info,
361  unsigned short & incoming_side,
362  Point & intersection_point,
363  unsigned short & intersected_side,
364  ElemExtrema & intersected_extrema,
365  Real & intersection_distance);
366 
371  void applyOnExternalBoundary(const std::shared_ptr<Ray> & ray);
376  void applyOnInternalBoundary(const std::shared_ptr<Ray> & ray);
389  void possiblyAddToBoundaryElems(const Elem * elem,
390  const unsigned short side,
391  const std::vector<BoundaryID> & bnd_ids,
392  const ElemExtrema & extrema);
400  void continueTraceOffProcessor(const std::shared_ptr<Ray> & ray);
401 
412  void findExternalBoundarySide(unsigned short & boundary_side,
413  ElemExtrema & boundary_extrema,
414  const Elem *& boundary_elem);
415 
419  void storeExitsElemResult(const ExitsElemResult result);
420 
426  Real subdomainHmax(const Elem * elem) const;
427 
433  void postRayTracingObject(const std::shared_ptr<Ray> & ray, const RayTracingObject * rto);
434 
440  const unsigned int _dim;
442  const BoundaryInfo & _boundary_info;
447 
450 
453 
455  const std::shared_ptr<Ray> * _current_ray;
457  const Elem * _current_elem;
459  const Elem * _last_elem;
467  unsigned short _current_elem_n_sides;
471  unsigned short _incoming_side;
474 
478  unsigned short _intersected_side;
487 
490 
492  const Point * _current_normals;
493 
495  std::vector<BoundaryID> _boundary_ids;
497  std::vector<TraceRayBndElement> _boundary_elems;
498 
500  std::unordered_map<const Node *, std::vector<NeighborInfo>> _vertex_neighbors;
502  std::unordered_map<std::pair<const Node *, const Node *>,
503  std::pair<bool, std::vector<NeighborInfo>>>
505 
507  std::vector<NeighborInfo> _point_neighbor_helper;
511  std::vector<const Elem *> _neighbor_active_neighbor_children;
512 
514  std::vector<unsigned long long int> _results;
515 
518 
520  std::vector<RayBoundaryConditionBase *> _on_boundary_ray_bcs;
522  std::vector<std::size_t> _on_boundary_apply_index;
523 
528 
530  std::set<RayKernelBase *> _old_ray_kernels;
531 
532 #ifdef DEBUG_RAY_MESH_IF
533  Mesh * _debug_mesh;
534  Parallel::Communicator _debug_comm;
535  unsigned int _debug_node_count;
536 #endif
537 };
538 
539 // Helper for a mooseAssert() with useful trace information
540 #ifdef NDEBUG
541 #define traceAssert(asserted, msg) ((void)0)
542 #else
543 #define traceAssert(asserted, msg) \
544  do \
545  { \
546  if (!(asserted)) \
547  mooseAssert(asserted, failTraceMessage(msg, __LINE__)); \
548  } while (0)
549 #endif
const std::vector< NeighborInfo > & getPointNeighbors(const Elem *elem, const Point &point)
Get the point neighbors.
Definition: TraceRay.C:1983
const Point & currentIntersectionPoint() const
Gets the current intersection point for the Ray that is being traced.
Definition: TraceRay.h:121
const std::vector< NeighborInfo > & getEdgeNeighbors(const Elem *elem, const std::pair< const Node *, const Node *> &vertices, const Point &point)
Get the neighbors at an edge.
RayTracingStudy & _study
The RayTracingStudy.
Definition: TraceRay.h:436
ElemType
void setBackfaceCulling(const bool backface_culling)
Enable or disable the use of element normals for backface culling through the getElemNormals() method...
Definition: TraceRay.h:105
ExitsElemResult exitsElem(const Elem *elem, const ElemType elem_type, const unsigned short incoming_side, Point &intersection_point, unsigned short &intersected_side, ElemExtrema &intersected_extrema, Real &intersection_distance, const Point *normals)
Determines if _current_ray moving in direction _direction exits elem.
unsigned short _current_elem_n_sides
The number of sides on the current elem, used to avoid elem->n_sides() virtual calls.
Definition: TraceRay.h:467
std::unordered_map< std::pair< const Node *, const Node * >, std::pair< bool, std::vector< NeighborInfo > > > _edge_neighbors
The cached edge neighbors.
Definition: TraceRay.h:504
ElemExtrema _intersected_extrema
The work point for the intersected vertex/edge vertices of the current Ray, if any.
Definition: TraceRay.h:480
void onTrajectoryChanged(const std::shared_ptr< Ray > &ray)
Called when a Ray&#39;s trajectory changes.
Definition: TraceRay.C:1695
void findExternalBoundarySide(unsigned short &boundary_side, ElemExtrema &boundary_extrema, const Elem *&boundary_elem)
Finds (if any) an element side that is on the boundary and is outgoing at _intersection_point that is...
Definition: TraceRay.C:950
void applyOnExternalBoundary(const std::shared_ptr< Ray > &ray)
Gets and applies external boundary conditions in _current_elem on side _intersected_side at _intersec...
Definition: TraceRay.C:746
Mesh * _debug_mesh
Definition: TraceRay.h:533
std::vector< std::size_t > _on_boundary_apply_index
Reusable for which boundary elements to apply for a specific RayBC in onBoundary() ...
Definition: TraceRay.h:522
libMesh::ElemType _current_elem_type
The current elem type (constant on subdomain), used to avoid elem->type() calls.
Definition: TraceRay.h:465
void onSegment(const std::shared_ptr< Ray > &ray)
Called on a single segment traced by a Ray.
Definition: TraceRay.C:2025
std::string failTraceMessage(const std::string &reason, const int line=-1)
Creates a useful error string with current tracing information.
Definition: TraceRay.C:1758
Data structure that stores information for output of a partial trace of a Ray on a processor...
Definition: TraceData.h:42
void onContinueTrace(const std::shared_ptr< Ray > &)
Called when a Ray is continuing to trace after segment.
Definition: TraceRay.C:1655
unsigned int _debug_node_count
Definition: TraceRay.h:535
void onSubdomainChanged(const std::shared_ptr< Ray > &ray, const bool same_ray)
Called when the subdomain changes.
Definition: TraceRay.C:1724
Base class for a MooseObject used in ray tracing.
MooseUtils::StaticallyAllocatedSet< const Elem *, MAX_POINT_NEIGHBORS > _neighbor_set
Definition: TraceRay.h:508
void possiblyAddToBoundaryElems(const Elem *elem, const unsigned short side, const std::vector< BoundaryID > &bnd_ids, const ElemExtrema &extrema)
Helper for possibly storing boundary information in _boundary_elems, which is storage for boundary el...
Definition: TraceRay.C:917
Base object for the RayKernel syntax.
Definition: RayKernelBase.h:27
Helper for defining if at an element&#39;s edge, vertex, or neither.
Definition: ElemExtrema.h:25
Parallel::Communicator _debug_comm
Definition: TraceRay.h:534
const Elem *const & currentElem() const
Gets the element that the current Ray is being traced in.
Definition: TraceRay.h:114
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
Struct for containing the necessary information about a cached neighbor for ray tracing.
Definition: NeighborInfo.h:27
ElemExtrema _last_intersected_extrema
The intersected vertex/edge vertices for the previous intersection, if any.
Definition: TraceRay.h:482
const unsigned int _dim
The mesh dimension.
Definition: TraceRay.h:440
void postRayTracingObject(const std::shared_ptr< Ray > &ray, const RayTracingObject *rto)
Called after executing a RayTracingObject (RayBCs and RayKernels)
Definition: TraceRay.C:2196
bool _backface_culling
Whether or not to use element normals for backface culling.
Definition: TraceRay.h:449
virtual ~TraceRay()
Definition: TraceRay.h:51
void onBoundary(const std::shared_ptr< Ray > &ray, const bool external)
Called when a Ray hits a boundary.
Definition: TraceRay.C:2082
const SubdomainID & currentSubdomainID() const
Gets the subdomain of the current element that the Ray is being traced in.
Definition: TraceRay.h:159
void trace(const std::shared_ptr< Ray > &ray)
Traces a ray.
Definition: TraceRay.C:1019
libMesh::ElemSideBuilder _elem_side_builder
Helper for building element sides without excessive allocation.
Definition: TraceRay.h:517
Point _incoming_point
The incoming point of the current Ray.
Definition: TraceRay.h:469
const Point * _current_normals
The normals for the current element for backface culling (pointer to the first normal - optional) ...
Definition: TraceRay.h:492
const Point & currentIncomingPoint() const
Gets the current incoming point for the Ray that is being traced.
Definition: TraceRay.h:127
uint8_t processor_id_type
bool _exits_elem
Whether or not the current trace exits an element.
Definition: TraceRay.h:489
unsigned short _incoming_side
The incoming side of the current Ray.
Definition: TraceRay.h:471
std::vector< BoundaryID > _boundary_ids
Reusable vector for calling _boundary_info.boundary_ids()
Definition: TraceRay.h:495
Real subdomainHmax(const Elem *elem) const
Get the approximate subdomain hmax for an element.
Definition: TraceRay.C:2188
const unsigned short & currentIntersectedSide() const
Gets the side that the current Ray intersected.
Definition: TraceRay.h:140
std::set< RayKernelBase * > _old_ray_kernels
Helper for avoiding calling preTrace() on the same RayKernel multiple times.
Definition: TraceRay.h:530
boundary_id_type BoundaryID
const processor_id_type _pid
The processor id.
Definition: TraceRay.h:444
void preExecute()
Should be called immediately before calling any traces.
Definition: TraceRay.C:61
const unsigned short & currentIncomingSide() const
Gets the current incoming side for the Ray that is being traced.
Definition: TraceRay.h:133
MooseMesh & _mesh
The MooseMesh.
Definition: TraceRay.h:438
bool _has_ray_kernels
Whether or not the RayTracingStudy has any RayKernels.
Definition: TraceRay.h:525
std::vector< unsigned long long int > _results
Results over all of the local traces, indexed by TraceRayResult.
Definition: TraceRay.h:514
ExitsElemResult moveThroughNeighbor(const NeighborInfo &neighbor_info, unsigned short &incoming_side, Point &intersection_point, unsigned short &intersected_side, ElemExtrema &intersected_extrema, Real &intersection_distance)
Sees if a Ray can move through a neighbor (vertex/edge/point)
Definition: TraceRay.C:698
const Elem * _last_elem
The last element the current Ray was traced in.
Definition: TraceRay.h:459
ExitsElemResult moveThroughNeighbors(const std::vector< NeighborInfo > &neighbors, const Elem *last_elem, const Elem *&best_elem, unsigned short &best_elem_incoming_side)
Moves the Ray though neighbors (vertex/edge/point)
Definition: TraceRay.C:585
Basic datastructure for a ray that will traverse the mesh.
Definition: Ray.h:56
TraceData * _current_cached_trace
The TraceData for the current cached trace (if any)
Definition: TraceRay.h:452
void onCompleteTrace(const std::shared_ptr< Ray > &ray)
Called when a Ray is finished tracing (whenever !ray->shouldContinue())
Definition: TraceRay.C:1627
TraceRay(RayTracingStudy &study, const THREAD_ID tid)
Definition: TraceRay.C:47
std::vector< const Elem * > _neighbor_active_neighbor_children
Definition: TraceRay.h:511
std::vector< RayBoundaryConditionBase * > _on_boundary_ray_bcs
Reusable for getting the RayBCs in onBoundary()
Definition: TraceRay.h:520
const std::shared_ptr< Ray > * _current_ray
The current ray being traced.
Definition: TraceRay.h:455
TraceRayResult
Enum for the various results reported by this object.
Definition: TraceRay.h:56
const Elem * _current_elem
The element the current Ray is being traced in.
Definition: TraceRay.h:457
bool _is_rectangular_domain
Whether or not the domain is rectangular (defined perfectly by its bounding box)
Definition: TraceRay.h:527
const BoundaryID & currentBoundaryID() const
Gets the BoundaryID of the boundary that the Ray intersected and is being applied a boundary conditio...
Definition: TraceRay.h:155
const std::vector< NeighborInfo > & getNeighbors(const Elem *elem, const ElemExtrema &extrema, const Point &point)
Get the point/vertex/edge neighbors depending on extrema.
Definition: TraceRay.C:1973
const Real & currentIntersectionDistance() const
Gets the current intersection distance for the Ray that is being traced.
Definition: TraceRay.h:150
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< TraceRayBndElement > _boundary_elems
Boundary elements that need RayBCs to be applied.
Definition: TraceRay.h:497
const THREAD_ID _tid
The thread id.
Definition: TraceRay.h:446
void applyOnInternalBoundary(const std::shared_ptr< Ray > &ray)
Gets and applies internal boundary conditions (if any) from _current_elem, _last_elem, and any other point neighbors that have internal sidesets at _intersection_point.
Definition: TraceRay.C:805
MooseUtils::StaticallyAllocatedSet< const Elem *, MAX_POINT_NEIGHBORS > _neighbor_next_untested_set
Definition: TraceRay.h:510
std::unordered_map< const Node *, std::vector< NeighborInfo > > _vertex_neighbors
The cached vertex neighbors.
Definition: TraceRay.h:500
void failTrace(const std::string &reason, const bool warning, const int line=-1)
Specialized mooseError for a failed Ray trace with detailed information regarding the trace...
Definition: TraceRay.C:1806
MooseUtils::StaticallyAllocatedSet< const Elem *, MAX_POINT_NEIGHBORS > _neighbor_untested_set
Definition: TraceRay.h:509
Real _current_subdomain_hmax
The current subdomain hmax.
Definition: TraceRay.h:463
ExitsElemResult
Enum for the different exit results for exitElem()
Definition: TraceRay.h:257
std::vector< unsigned long long int > results() const
Get the various results reported by this object, indexed by TraceRayResult.
Definition: TraceRay.h:93
unsigned short _intersected_side
The work point for the intersected side of the current Ray.
Definition: TraceRay.h:478
SubdomainID _current_subdomain_id
The current SubdomainID.
Definition: TraceRay.h:461
const BoundaryInfo & _boundary_info
The BoundaryInfo for the mesh.
Definition: TraceRay.h:442
BoundaryID _current_bnd_id
The current BoundaryID (used when calling RayBoundaryConditionBase::onBoundary()) ...
Definition: TraceRay.h:486
const std::vector< NeighborInfo > & getVertexNeighbors(const Elem *elem, const Node *vertex)
Gets the neighbors at a vertex.
const std::shared_ptr< Ray > *const & currentRay() const
Gets the current Ray that is being traced.
Definition: TraceRay.h:110
Base class for the RayBC syntax.
void continueTraceOffProcessor(const std::shared_ptr< Ray > &ray)
Sets up a ray to continue tracing off processor.
Definition: TraceRay.C:1670
Real _intersection_distance
The work point for the intersection distance of the current Ray.
Definition: TraceRay.h:484
const ElemExtrema & currentIntersectedExtrema() const
Gets the element extrema (vertex/edge) that the current Ray intersected.
Definition: TraceRay.h:144
void meshChanged()
Called on mesh change.
Definition: TraceRay.C:75
std::vector< NeighborInfo > _point_neighbor_helper
Reusable for building neighbors.
Definition: TraceRay.h:507
Traces Rays through the mesh on a single processor.
Definition: TraceRay.h:46
void storeExitsElemResult(const ExitsElemResult result)
Stores the result given by an intersection in _results as necessary.
Definition: TraceRay.C:2012
bool _should_continue
Whether or not the current Ray should continue.
Definition: TraceRay.h:473
unsigned int THREAD_ID
Base class for Ray tracing studies that will generate Rays and then propagate all of them to terminat...
Point _intersection_point
The work point for the intersection of the current Ray.
Definition: TraceRay.h:476