https://mooseframework.inl.gov
Ray.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 "RayTracingCommon.h"
14 
15 // MOOSE includes
16 #include "MooseError.h"
17 #include "MooseTypes.h"
18 
19 // libMesh Includes
20 #include "libmesh/parallel.h"
21 
22 // Forward declarations
23 namespace libMesh
24 {
25 class Elem;
26 }
27 class RayTracingStudy;
28 // Friend access to Ray
29 class TraceRay;
30 class TestRayLots;
31 // Friend access to ChangeDirectionKey for accessing changeDirection()
33 // Friend access to ChangeStartDirectionKey for accessing changeStartDirection()
34 class RayKernelBase;
35 // Friend access to NonResetCountersKey for accessing constructor/reset without counter reset
36 namespace MooseUtils
37 {
38 template <class T>
39 class SharedPool;
40 }
41 
43 typedef unsigned long int RayID;
45 #ifdef SINGLE_PRECISION_RAY
46 typedef float RayData;
47 #else
48 typedef libMesh::Real RayData;
49 #endif
50 typedef unsigned int RayDataIndex;
52 
56 class Ray
57 {
58 public:
64  {
68  };
69 
75  {
76  friend class RayKernelBase;
79  };
80 
88  {
89  friend class RayTracingStudy;
90  friend class MooseUtils::SharedPool<Ray>;
91  friend class TestRayLots;
94  };
95 
111  const RayID id,
112  const std::size_t data_size,
113  const std::size_t aux_data_size,
114  const bool reset,
115  const ConstructRayKey &);
116 
132  void reset(RayTracingStudy * study,
133  const RayID id,
134  const std::size_t data_size,
135  const std::size_t aux_data_size,
136  const bool reset,
137  const ConstructRayKey &);
138 
149  Ray(const Ray * const other, const ConstructRayKey &);
150 
162  void reset(const Ray * const other, const ConstructRayKey &);
163 
170  Ray & operator=(const Ray &) = delete;
177  Ray() = delete;
184  Ray(const Ray & other) = delete;
185 
191  bool operator==(const Ray & other) const { return equalityHelper(other, true); }
197  bool operator!=(const Ray & other) const { return equalityHelper(other, false); }
198 
200  static const RayDataIndex INVALID_RAY_DATA_INDEX = static_cast<RayDataIndex>(-1);
202  static const RayID INVALID_RAY_ID = static_cast<RayID>(-1);
203 
207  RayID id() const { return _id; }
211  bool invalidID() const { return _id == INVALID_RAY_ID; }
212 
221  const Point & currentPoint() const { return _current_point; }
226 
237  void changeDirection(const Point & direction, const ChangeDirectionKey);
238 
249  void
250  changeStartDirection(const Point & start, const Point & direction, const ChangeStartDirectionKey);
251 
255  const Point & direction() const { return _direction; }
260 
267  std::vector<RayData> & data();
274  const std::vector<RayData> & data() const;
281  RayData & data(const std::size_t i);
288  const RayData & data(const std::size_t i) const;
289 
296  std::vector<RayData> & auxData();
303  const std::vector<RayData> & auxData() const;
310  RayData & auxData(const std::size_t i);
317  const RayData & auxData(const std::size_t i) const;
318 
331  void setStart(const Point & starting_point,
332  const Elem * starting_elem = nullptr,
333  const unsigned short starting_incoming_side = RayTracingCommon::invalid_side);
341  void setStartingDirection(const Point & starting_direction);
352  void setStartingEndPoint(const Point & starting_end_point);
364  void setStartingMaxDistance(const Real starting_max_distance);
373  void setStationary();
374 
383  void invalidateStartingElem();
403  void clearStartingInfo();
404 
410  void resetCounters();
411 
425  const Elem * currentElem() const { return _current_elem; }
426 
435  unsigned short currentIncomingSide() const { return _current_incoming_side; }
445  {
447  }
448 
454  bool endSet() const { return _end_set; }
463  bool atEnd() const;
473  Point endPoint() const;
474 
478  unsigned int processorCrossings() const { return _processor_crossings; }
479 
483  unsigned int intersections() const { return _intersections; }
484 
488  Real distance() const { return _distance; }
495  Real maxDistance() const { return _max_distance; }
499  bool maxDistanceSet() const { return _max_distance != std::numeric_limits<Real>::max(); }
500 
504  inline bool stationary() const;
505 
509  bool shouldContinue() const { return _should_continue; }
513  void setShouldContinue(const bool should_continue) { _should_continue = should_continue; }
514 
518  bool trajectoryChanged() const { return _trajectory_changed; }
522  unsigned int trajectoryChanges() const { return _trajectory_changes; }
523 
527  std::string getInfo() const;
528 
532  bool hasTraced() const
533  {
534  return (bool)_distance || (bool)_processor_crossings || (bool)_intersections;
535  }
536 
540  const RayTracingStudy & study() const { return _study; }
541 
542 private:
546  void changeID(const RayID id) { _id = id; }
547 
551  void invalidateCurrentElem() { _current_elem = nullptr; }
552 
556  void setCurrentPoint(const Point & current_point) { _current_point = current_point; }
561 
565  void setDirection(const Point & direction) { _direction = direction; }
570 
574  void setCurrentElem(const Elem * current_elem) { _current_elem = current_elem; }
575 
579  void setCurrentIncomingSide(const unsigned short current_incoming_side)
580  {
581  _current_incoming_side = current_incoming_side;
582  }
587 
591  void setTrajectoryChanged(const bool trajectory_changed)
592  {
593  _trajectory_changed = trajectory_changed;
594  }
595 
600 
605 
610 
614  void addDistance(const Real add_distance) { _distance += add_distance; }
618  void changeMaxDistance(const Real max_distance) { _max_distance = max_distance; }
622  void invalidateMaxDistance() { _max_distance = std::numeric_limits<Real>::max(); }
623 
627  void errorIfTracing(const std::string & reason) const;
631  void errorWhenInitializing(const std::string & reason) const;
632 
636  void resetCountersInternal();
637 
641  bool equalityHelper(const Ray & other, const bool equal) const;
642 
647 
650 
660 
662  Point _direction;
663 
677  const Elem * _current_elem;
678 
687  unsigned short _current_incoming_side;
688 
691  bool _end_set;
692 
694  unsigned int _processor_crossings;
695 
697  unsigned int _intersections;
698 
700  unsigned int _trajectory_changes;
703 
708 
711 
714  mutable std::vector<RayData> _data;
715 
718  mutable std::vector<RayData> _aux_data;
719 
722 
724  long padding[8];
725 
726  // TraceRay is the only object that should be executing Rays and therefore needs access
727  friend class TraceRay;
728  // Packing needs access to changing the internal counters during the trace
729  friend class Parallel::Packing<std::shared_ptr<Ray>>;
730  // Allows for testing of equality methods
731  friend class TestRayLots;
732  // Data helpers needs to be able to access the internal methods for a Ray for store/load
733  friend void dataStore(std::ostream & stream, std::shared_ptr<Ray> & ray, void * context);
734  friend void dataLoad(std::istream & stream, std::shared_ptr<Ray> & ray, void * context);
735 };
736 
737 bool
739 {
740  const bool stationary = _max_distance == 0;
741  if (stationary)
742  mooseAssert(_intersections == 0, "Should be zero");
743  return stationary;
744 }
745 
750 namespace libMesh
751 {
752 namespace Parallel
753 {
754 template <>
755 class Packing<std::shared_ptr<Ray>>
756 {
757 public:
758  typedef Real buffer_type;
759 
760  static unsigned int packed_size(typename std::vector<Real>::const_iterator in);
761  static unsigned int packable_size(const std::shared_ptr<Ray> & ray, const void *);
762  static unsigned int size(const std::size_t data_size, const std::size_t aux_data_size);
763 
764  template <typename Iter, typename Context>
765  static void pack(const std::shared_ptr<Ray> & object, Iter data_out, const Context * context);
766 
767  template <typename BufferIter, typename Context>
768  static std::shared_ptr<Ray> unpack(BufferIter in, Context * context);
769 };
770 
771 } // namespace Parallel
772 
773 } // namespace libMesh
774 
775 void dataStore(std::ostream & stream, std::shared_ptr<Ray> & ray, void * context);
776 void dataLoad(std::istream & stream, std::shared_ptr<Ray> & ray, void * context);
static const unsigned short invalid_side
Identifier for an invalid side index.
void setCurrentPoint(const Point &current_point)
Sets the Ray&#39;s current point.
Definition: Ray.h:556
RayID id() const
Gets the Ray&#39;s ID.
Definition: Ray.h:207
std::vector< RayData > & data()
Gets a writeable reference to the Ray&#39;s data.
Definition: Ray.C:403
static const RayDataIndex INVALID_RAY_DATA_INDEX
Invalid index into a Ray&#39;s data.
Definition: Ray.h:200
unsigned int processorCrossings() const
Gets the number of times this Ray has crossed a processor.
Definition: Ray.h:478
bool invalidID() const
Whether or not the Ray&#39;s ID is invalid.
Definition: Ray.h:211
void invalidateMaxDistance()
Invalidates the Ray&#39;s max distance.
Definition: Ray.h:622
long padding[8]
Extra padding to avoid false sharing.
Definition: Ray.h:724
unsigned long int RayID
Type for a Ray&#39;s ID.
Definition: Ray.h:43
Class that is used as a parameter to the public constructors/reset methods.
Definition: Ray.h:87
bool operator==(const Ray &other) const
Equality operator.
Definition: Ray.h:191
bool trajectoryChanged() const
Whether or not this Ray has had its trajectory changed.
Definition: Ray.h:518
ChangeStartDirectionKey(const ChangeStartDirectionKey &)
Definition: Ray.h:78
BufferType pack(const ValueType value)
Packs value into a value of type BufferType at a byte level, to be unpacked with the unpack() routine...
bool hasTraced() const
Whether or not a Ray has begun tracing.
Definition: Ray.h:532
Point _direction
Direction of the Ray.
Definition: Ray.h:662
Base object for the RayKernel syntax.
Definition: RayKernelBase.h:27
std::string getInfo() const
Helper function for getting information about the Ray.
Definition: Ray.C:469
std::vector< RayData > & auxData()
Gets a writeable reference to the Ray&#39;s auxilary data.
Definition: Ray.C:437
void setDirection(const Point &direction)
Sets the Ray&#39;s direction.
Definition: Ray.h:565
void invalidateStartingElem()
Invalidates a Ray&#39;s starting element.
Definition: Ray.C:339
void setTrajectoryChanged(const bool trajectory_changed)
Set whether or not this Ray has had its trajectory changed.
Definition: Ray.h:591
unsigned int trajectoryChanges() const
Gets the number of trajectory changes this Ray has had.
Definition: Ray.h:522
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
const RayTracingStudy & study() const
Get the RayTracingStudy associated with this Ray.
Definition: Ray.h:540
unsigned int _processor_crossings
Number of times this Ray has been communicated.
Definition: Ray.h:694
void changeDirection(const Point &direction, const ChangeDirectionKey)
This method is for internal use only.
Definition: Ray.C:196
const Point & currentPoint() const
Gets the point that the Ray is currently at.
Definition: Ray.h:221
const Elem * currentElem() const
Gets the current element that the Ray is in.
Definition: Ray.h:425
void unpack(const BufferType value_as_buffer_type, ValueType &value)
Unpacks value_as_buffer_type (which is packed with pack()) into value at a byte level.
void changeMaxDistance(const Real max_distance)
Changes the Ray&#39;s max distance to be traveled.
Definition: Ray.h:618
bool atEnd() const
Whether or not the Ray is at the user-defined end point.
Definition: Ray.C:176
bool operator!=(const Ray &other) const
Non-equal operator.
Definition: Ray.h:197
bool stationary() const
Definition: Ray.h:738
bool invalidCurrentPoint() const
Whether or not the point that the Ray is currently at is valid.
Definition: Ray.h:225
Point _current_point
Current point of the Ray.
Definition: Ray.h:659
void changeStartDirection(const Point &start, const Point &direction, const ChangeStartDirectionKey)
This method is for internal use only.
Definition: Ray.C:206
void addTrajectoryChange()
Increment the Ray&#39;s trajectory change counter.
Definition: Ray.h:609
bool maxDistanceSet() const
Whether or not the distance has been set via setStartingMaxDistance()
Definition: Ray.h:499
bool invalidCurrentIncomingSide() const
Whether or not the Ray&#39;s current incoming side is invalid.
Definition: Ray.h:444
unsigned int RayDataIndex
Type for the index into the data and aux data on a Ray.
Definition: Ray.h:51
friend void dataStore(std::ostream &stream, std::shared_ptr< Ray > &ray, void *context)
Definition: Ray.C:699
void setStartingEndPoint(const Point &starting_end_point)
Sets the starting end point to starting_point for a Ray.
Definition: Ray.C:285
void addIntersection()
Increment the Ray&#39;s intersection counter.
Definition: Ray.h:604
void setCurrentIncomingSide(const unsigned short current_incoming_side)
Change a Ray&#39;s incoming side.
Definition: Ray.h:579
void setStartingDirection(const Point &starting_direction)
Sets the starting direction to starting_direction for a Ray.
Definition: Ray.C:268
std::vector< RayData > _aux_data
Auxiliary data that is carried with the ray This is mutable so that we can resize it if needed within...
Definition: Ray.h:718
ChangeDirectionKey(const ChangeDirectionKey &)
Definition: Ray.h:67
void clearStartingInfo()
Clears the starting information set on the Ray:
Definition: Ray.C:353
bool _should_continue
Wether or not the Ray should continue to be traced (not sent in parallel)
Definition: Ray.h:710
RayTracingStudy & _study
The RayTracingStudy that owns this Ray (not sent in parallel)
Definition: Ray.h:721
void invalidateCurrentIncomingSide()
Invalidates the Ray&#39;s current incoming side.
Definition: Ray.h:586
Context
Basic datastructure for a ray that will traverse the mesh.
Definition: Ray.h:56
void reset(RayTracingStudy *study, const RayID id, const std::size_t data_size, const std::size_t aux_data_size, const bool reset, const ConstructRayKey &)
Resets a Ray for internal use only.
Ray()=delete
Deleted default constructor.
bool invalidDirection() const
Whether or not the Ray&#39;s direction is set to invalid.
Definition: Ray.h:259
void setStartingMaxDistance(const Real starting_max_distance)
Sets the maximum distance this Ray should travel to starting_max_distance.
Definition: Ray.C:310
bool shouldContinue() const
Whether or not this Ray should continue.
Definition: Ray.h:509
void setShouldContinue(const bool should_continue)
Sets whether or not this Ray should continue.
Definition: Ray.h:513
bool endSet() const
Whether or not the user has set an end point for this Ray.
Definition: Ray.h:454
Real _max_distance
Maximum distance the Ray is allowed to travel.
Definition: Ray.h:707
void invalidateDirection()
Invalidates the Ray&#39;s current direction.
Definition: Ray.h:569
Ray & operator=(const Ray &)=delete
Deleted copy operator.
void resetCountersInternal()
Reset all of the internal counters.
Definition: Ray.C:392
unsigned short currentIncomingSide() const
Get a Ray&#39;s current incoming side.
Definition: Ray.h:435
void setStationary()
Sets the Ray to be stationary (max distance = 0).
Definition: Ray.C:325
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void resetCounters()
Clears the internal counters on the Ray so that the Ray can be traced again.
Definition: Ray.C:384
void clearStartingInfoInternal()
Clears the starting information.
Definition: Ray.C:360
Class that is used as a parameter to changeStartDirection() that allows only RayKernelBase methods to...
Definition: Ray.h:74
Real distance() const
Gets the distance this Ray has traveled.
Definition: Ray.h:488
Class that is used as a parameter to changeDirection() that allows only RayBC methods to call changeD...
Definition: Ray.h:63
void addProcessorCrossing()
Increment the Ray&#39;s processor crossing counter.
Definition: Ray.h:599
void invalidateStartingIncomingSide()
Invalidates a Ray&#39;s starting incoming side.
Definition: Ray.C:346
const Elem * _current_elem
Current element that the Ray is in.
Definition: Ray.h:677
const Point & direction() const
Gets the Ray&#39;s direction.
Definition: Ray.h:255
class infix_ostream_iterator if void
ConstructRayKey(const ConstructRayKey &)
Definition: Ray.h:93
bool equalityHelper(const Ray &other, const bool equal) const
Helper for the equality operators.
Definition: Ray.C:126
Real maxDistance() const
Gets the max distance this Ray is allowed to travel.
Definition: Ray.h:495
RayID _id
A unique ID for this Ray.
Definition: Ray.h:649
void addDistance(const Real add_distance)
Adds to the distance this Ray has traveled.
Definition: Ray.h:614
Real _distance
Total distance this Ray has traveled.
Definition: Ray.h:705
friend void dataLoad(std::istream &stream, std::shared_ptr< Ray > &ray, void *context)
Definition: Ray.C:724
unsigned int _intersections
Number of intersections done for this Ray.
Definition: Ray.h:697
float RayData
Type for a Ray&#39;s data.
Definition: Ray.h:46
std::vector< RayData > _data
The data that is carried with the Ray This is mutable so that we can resize it if needed within const...
Definition: Ray.h:714
bool _trajectory_changed
Whether or not this Ray had its trajectory changed (not sent in parallel)
Definition: Ray.h:702
static const libMesh::Point invalid_point(invalid_distance, invalid_distance, invalid_distance)
Identifier for an invalid point.
void setStart(const Point &starting_point, const Elem *starting_elem=nullptr, const unsigned short starting_incoming_side=RayTracingCommon::invalid_side)
Sets the information pretaining to the start point for the Ray.
Definition: Ray.C:219
unsigned int _trajectory_changes
Number of times this Ray has had its trajectory changed.
Definition: Ray.h:700
bool _end_set
Whether or not the user has set an end point for this Ray (via limiting its distance with setStarting...
Definition: Ray.h:691
Base class for the RayBC syntax.
void changeID(const RayID id)
Changes the Ray&#39;s ID.
Definition: Ray.h:546
void setCurrentElem(const Elem *current_elem)
Change a Ray&#39;s current elem.
Definition: Ray.h:574
unsigned short _current_incoming_side
The side of _current_elem that the Ray is incoming on (if any).
Definition: Ray.h:687
unsigned int intersections() const
Gets the number of intersections this Ray has done.
Definition: Ray.h:483
void errorWhenInitializing(const std::string &reason) const
Produces a useful error for use when initializing a Ray.
Definition: Ray.C:378
Point endPoint() const
Gets the user-set end point for the Ray, if set.
Definition: Ray.C:186
Traces Rays through the mesh on a single processor.
Definition: TraceRay.h:46
static const RayID INVALID_RAY_ID
Invalid Ray ID.
Definition: Ray.h:202
void invalidateCurrentElem()
Invalidates the Ray&#39;s current element.
Definition: Ray.h:551
void invalidateCurrentPoint()
Invalidates the Ray&#39;s current point.
Definition: Ray.h:560
void errorIfTracing(const std::string &reason) const
Produces a useful error if a Ray has started tracing.
Definition: Ray.C:371
Base class for Ray tracing studies that will generate Rays and then propagate all of them to terminat...