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 class PeriodicRayBC;
36 // Friend access to NonResetCountersKey for accessing constructor/reset without counter reset
37 namespace MooseUtils
38 {
39 template <class T>
40 class SharedPool;
41 }
42 
44 typedef unsigned long int RayID;
46 #ifdef SINGLE_PRECISION_RAY
47 typedef float RayData;
48 #else
49 typedef libMesh::Real RayData;
50 #endif
51 typedef unsigned int RayDataIndex;
53 
57 class Ray
58 {
59 public:
65  {
69  };
70 
76  {
77  friend class RayKernelBase;
80  };
81 
87  {
88  friend class PeriodicRayBC;
91  };
92 
100  {
101  friend class RayTracingStudy;
103  friend class TestRayLots;
106  };
107 
123  const RayID id,
124  const std::size_t data_size,
125  const std::size_t aux_data_size,
126  const bool reset,
127  const ConstructRayKey &);
128 
144  void reset(RayTracingStudy * study,
145  const RayID id,
146  const std::size_t data_size,
147  const std::size_t aux_data_size,
148  const bool reset,
149  const ConstructRayKey &);
150 
161  Ray(const Ray * const other, const ConstructRayKey &);
162 
174  void reset(const Ray * const other, const ConstructRayKey &);
175 
182  Ray & operator=(const Ray &) = delete;
189  Ray() = delete;
196  Ray(const Ray & other) = delete;
197 
203  bool operator==(const Ray & other) const { return equalityHelper(other, true); }
209  bool operator!=(const Ray & other) const { return equalityHelper(other, false); }
210 
212  static const RayDataIndex INVALID_RAY_DATA_INDEX = static_cast<RayDataIndex>(-1);
214  static const RayID INVALID_RAY_ID = static_cast<RayID>(-1);
215 
219  RayID id() const { return _id; }
223  bool invalidID() const { return _id == INVALID_RAY_ID; }
224 
233  const Point & currentPoint() const { return _current_point; }
238 
249  void changeDirection(const Point & direction, const ChangeDirectionKey);
250 
261  void
262  changeStartDirection(const Point & start, const Point & direction, const ChangeStartDirectionKey);
263 
271  void changePointElemSide(const Point & point,
272  const Elem & elem,
273  const unsigned int side,
274  const ChangePointElemSideKey);
275 
279  const Point & direction() const { return _direction; }
284 
291  std::vector<RayData> & data();
298  const std::vector<RayData> & data() const;
305  RayData & data(const std::size_t i);
312  const RayData & data(const std::size_t i) const;
313 
320  std::vector<RayData> & auxData();
327  const std::vector<RayData> & auxData() const;
334  RayData & auxData(const std::size_t i);
341  const RayData & auxData(const std::size_t i) const;
342 
355  void setStart(const Point & starting_point,
356  const Elem * starting_elem = nullptr,
357  const unsigned short starting_incoming_side = RayTracingCommon::invalid_side);
365  void setStartingDirection(const Point & starting_direction);
376  void setStartingEndPoint(const Point & starting_end_point);
388  void setStartingMaxDistance(const Real starting_max_distance);
397  void setStationary();
398 
407  void invalidateStartingElem();
427  void clearStartingInfo();
428 
434  void resetCounters();
435 
449  const Elem * currentElem() const { return _current_elem; }
450 
459  unsigned short currentIncomingSide() const { return _current_incoming_side; }
469  {
471  }
472 
478  bool endSet() const { return _end_set; }
487  bool atEnd() const;
497  Point endPoint() const;
498 
502  unsigned int processorCrossings() const { return _processor_crossings; }
503 
507  unsigned int intersections() const { return _intersections; }
508 
512  Real distance() const { return _distance; }
519  Real maxDistance() const { return _max_distance; }
523  bool maxDistanceSet() const { return _max_distance != std::numeric_limits<Real>::max(); }
524 
528  inline bool stationary() const;
529 
533  bool shouldContinue() const { return _should_continue; }
537  void setShouldContinue(const bool should_continue) { _should_continue = should_continue; }
538 
542  bool trajectoryChanged() const { return _trajectory_changed; }
546  unsigned int trajectoryChanges() const { return _trajectory_changes; }
547 
551  std::string getInfo() const;
552 
556  bool hasTraced() const
557  {
558  return (bool)_distance || (bool)_processor_crossings || (bool)_intersections;
559  }
560 
564  const RayTracingStudy & study() const { return _study; }
565 
566 private:
570  void changeID(const RayID id) { _id = id; }
571 
575  void invalidateCurrentElem() { _current_elem = nullptr; }
576 
580  void setCurrentPoint(const Point & current_point) { _current_point = current_point; }
585 
589  void setDirection(const Point & direction) { _direction = direction; }
594 
598  void setCurrentElem(const Elem * current_elem) { _current_elem = current_elem; }
599 
603  void setCurrentIncomingSide(const unsigned short current_incoming_side)
604  {
605  _current_incoming_side = current_incoming_side;
606  }
611 
615  void setTrajectoryChanged(const bool trajectory_changed)
616  {
617  _trajectory_changed = trajectory_changed;
618  }
619 
624 
629 
634 
638  void addDistance(const Real add_distance) { _distance += add_distance; }
642  void changeMaxDistance(const Real max_distance) { _max_distance = max_distance; }
646  void invalidateMaxDistance() { _max_distance = std::numeric_limits<Real>::max(); }
647 
651  void errorIfTracing(const std::string & reason) const;
655  void errorWhenInitializing(const std::string & reason) const;
656 
660  void resetCountersInternal();
661 
665  bool equalityHelper(const Ray & other, const bool equal) const;
666 
671 
674 
684 
686  Point _direction;
687 
701  const Elem * _current_elem;
702 
711  unsigned short _current_incoming_side;
712 
717  bool _end_set;
720 
722  unsigned int _processor_crossings;
723 
725  unsigned int _intersections;
726 
728  unsigned int _trajectory_changes;
729 
734 
737  mutable std::vector<RayData> _data;
738 
741  mutable std::vector<RayData> _aux_data;
742 
745 
746  // TraceRay is the only object that should be executing Rays and therefore needs access
747  friend class TraceRay;
748  // Packing needs access to changing the internal counters during the trace
749  friend class Parallel::Packing<std::shared_ptr<Ray>>;
750  // Allows for testing of equality methods
751  friend class TestRayLots;
752  // Data helpers needs to be able to access the internal methods for a Ray for store/load
753  friend void dataStore(std::ostream & stream, std::shared_ptr<Ray> & ray, void * context);
754  friend void dataLoad(std::istream & stream, std::shared_ptr<Ray> & ray, void * context);
755 };
756 
757 bool
759 {
760  const bool stationary = _max_distance == 0;
761  if (stationary)
762  mooseAssert(_intersections == 0, "Should be zero");
763  return stationary;
764 }
765 
770 namespace libMesh
771 {
772 namespace Parallel
773 {
774 template <>
775 class Packing<std::shared_ptr<Ray>>
776 {
777 public:
778  typedef Real buffer_type;
779 
780  static unsigned int packed_size(typename std::vector<Real>::const_iterator in);
781  static unsigned int packable_size(const std::shared_ptr<Ray> & ray, const void *);
782  static unsigned int size(const std::size_t data_size, const std::size_t aux_data_size);
783 
784  template <typename Iter, typename Context>
785  static void pack(const std::shared_ptr<Ray> & object, Iter data_out, const Context * context);
786 
787  template <typename BufferIter, typename Context>
788  static std::shared_ptr<Ray> unpack(BufferIter in, Context * context);
789 };
790 
791 } // namespace Parallel
792 
793 } // namespace libMesh
794 
795 void dataStore(std::ostream & stream, std::shared_ptr<Ray> & ray, void * context);
796 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 changePointElemSide(const Point &point, const Elem &elem, const unsigned int side, const ChangePointElemSideKey)
This method is for internal use only.
Definition: Ray.C:219
void setCurrentPoint(const Point &current_point)
Sets the Ray&#39;s current point.
Definition: Ray.h:580
RayID id() const
Gets the Ray&#39;s ID.
Definition: Ray.h:219
std::vector< RayData > & data()
Gets a writeable reference to the Ray&#39;s data.
Definition: Ray.C:417
static const RayDataIndex INVALID_RAY_DATA_INDEX
Invalid index into a Ray&#39;s data.
Definition: Ray.h:212
unsigned int processorCrossings() const
Gets the number of times this Ray has crossed a processor.
Definition: Ray.h:502
bool invalidID() const
Whether or not the Ray&#39;s ID is invalid.
Definition: Ray.h:223
void invalidateMaxDistance()
Invalidates the Ray&#39;s max distance.
Definition: Ray.h:646
unsigned long int RayID
Type for a Ray&#39;s ID.
Definition: Ray.h:44
Class that is used as a parameter to the public constructors/reset methods.
Definition: Ray.h:99
bool operator==(const Ray &other) const
Equality operator.
Definition: Ray.h:203
RayBC that enforces periodic boundaries.
Definition: PeriodicRayBC.h:17
bool trajectoryChanged() const
Whether or not this Ray has had its trajectory changed.
Definition: Ray.h:542
ChangeStartDirectionKey(const ChangeStartDirectionKey &)
Definition: Ray.h:79
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:556
Point _direction
Direction of the Ray.
Definition: Ray.h:686
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:483
std::vector< RayData > & auxData()
Gets a writeable reference to the Ray&#39;s auxilary data.
Definition: Ray.C:451
void setDirection(const Point &direction)
Sets the Ray&#39;s direction.
Definition: Ray.h:589
void invalidateStartingElem()
Invalidates a Ray&#39;s starting element.
Definition: Ray.C:353
void setTrajectoryChanged(const bool trajectory_changed)
Set whether or not this Ray has had its trajectory changed.
Definition: Ray.h:615
unsigned int trajectoryChanges() const
Gets the number of trajectory changes this Ray has had.
Definition: Ray.h:546
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:564
unsigned int _processor_crossings
Number of times this Ray has been communicated.
Definition: Ray.h:722
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:233
const Elem * currentElem() const
Gets the current element that the Ray is in.
Definition: Ray.h:449
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:642
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:209
bool stationary() const
Definition: Ray.h:758
bool invalidCurrentPoint() const
Whether or not the point that the Ray is currently at is valid.
Definition: Ray.h:237
Point _current_point
Current point of the Ray.
Definition: Ray.h:683
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:633
bool maxDistanceSet() const
Whether or not the distance has been set via setStartingMaxDistance()
Definition: Ray.h:523
bool invalidCurrentIncomingSide() const
Whether or not the Ray&#39;s current incoming side is invalid.
Definition: Ray.h:468
unsigned int RayDataIndex
Type for the index into the data and aux data on a Ray.
Definition: Ray.h:52
friend void dataStore(std::ostream &stream, std::shared_ptr< Ray > &ray, void *context)
Definition: Ray.C:713
void setStartingEndPoint(const Point &starting_end_point)
Sets the starting end point to starting_point for a Ray.
Definition: Ray.C:299
void addIntersection()
Increment the Ray&#39;s intersection counter.
Definition: Ray.h:628
void setCurrentIncomingSide(const unsigned short current_incoming_side)
Change a Ray&#39;s incoming side.
Definition: Ray.h:603
void setStartingDirection(const Point &starting_direction)
Sets the starting direction to starting_direction for a Ray.
Definition: Ray.C:282
ChangePointElemSideKey(const ChangePointElemSideKey &)
Definition: Ray.h:90
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:741
ChangeDirectionKey(const ChangeDirectionKey &)
Definition: Ray.h:68
void clearStartingInfo()
Clears the starting information set on the Ray:
Definition: Ray.C:367
bool _should_continue
Wether or not the Ray should continue to be traced (not sent in parallel)
Definition: Ray.h:719
RayTracingStudy & _study
The RayTracingStudy that owns this Ray (not sent in parallel)
Definition: Ray.h:744
void invalidateCurrentIncomingSide()
Invalidates the Ray&#39;s current incoming side.
Definition: Ray.h:610
Context
Basic datastructure for a ray that will traverse the mesh.
Definition: Ray.h:57
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:283
void setStartingMaxDistance(const Real starting_max_distance)
Sets the maximum distance this Ray should travel to starting_max_distance.
Definition: Ray.C:324
bool shouldContinue() const
Whether or not this Ray should continue.
Definition: Ray.h:533
void setShouldContinue(const bool should_continue)
Sets whether or not this Ray should continue.
Definition: Ray.h:537
bool endSet() const
Whether or not the user has set an end point for this Ray.
Definition: Ray.h:478
Real _max_distance
Maximum distance the Ray is allowed to travel.
Definition: Ray.h:733
void invalidateDirection()
Invalidates the Ray&#39;s current direction.
Definition: Ray.h:593
Ray & operator=(const Ray &)=delete
Deleted copy operator.
void resetCountersInternal()
Reset all of the internal counters.
Definition: Ray.C:406
unsigned short currentIncomingSide() const
Get a Ray&#39;s current incoming side.
Definition: Ray.h:459
void setStationary()
Sets the Ray to be stationary (max distance = 0).
Definition: Ray.C:339
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:398
void clearStartingInfoInternal()
Clears the starting information.
Definition: Ray.C:374
Class that is used as a parameter to changeStartDirection() that allows only RayKernelBase methods to...
Definition: Ray.h:75
Real distance() const
Gets the distance this Ray has traveled.
Definition: Ray.h:512
Class that is used as a parameter to changeDirection() that allows only RayBC methods to call changeD...
Definition: Ray.h:64
void addProcessorCrossing()
Increment the Ray&#39;s processor crossing counter.
Definition: Ray.h:623
void invalidateStartingIncomingSide()
Invalidates a Ray&#39;s starting incoming side.
Definition: Ray.C:360
const Elem * _current_elem
Current element that the Ray is in.
Definition: Ray.h:701
const Point & direction() const
Gets the Ray&#39;s direction.
Definition: Ray.h:279
class infix_ostream_iterator if void
ConstructRayKey(const ConstructRayKey &)
Definition: Ray.h:105
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:519
RayID _id
A unique ID for this Ray.
Definition: Ray.h:673
Class that is used as a parameter to changePointElem() that allows only PeriodicRayBC methods to call...
Definition: Ray.h:86
void addDistance(const Real add_distance)
Adds to the distance this Ray has traveled.
Definition: Ray.h:638
Real _distance
Total distance this Ray has traveled.
Definition: Ray.h:731
friend void dataLoad(std::istream &stream, std::shared_ptr< Ray > &ray, void *context)
Definition: Ray.C:738
unsigned int _intersections
Number of intersections done for this Ray.
Definition: Ray.h:725
float RayData
Type for a Ray&#39;s data.
Definition: Ray.h:47
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:737
bool _trajectory_changed
Whether or not this Ray had its trajectory changed (not sent in parallel)
Definition: Ray.h:714
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:233
unsigned int _trajectory_changes
Number of times this Ray has had its trajectory changed.
Definition: Ray.h:728
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:717
Base class for the RayBC syntax.
void changeID(const RayID id)
Changes the Ray&#39;s ID.
Definition: Ray.h:570
void setCurrentElem(const Elem *current_elem)
Change a Ray&#39;s current elem.
Definition: Ray.h:598
unsigned short _current_incoming_side
The side of _current_elem that the Ray is incoming on (if any).
Definition: Ray.h:711
unsigned int intersections() const
Gets the number of intersections this Ray has done.
Definition: Ray.h:507
void errorWhenInitializing(const std::string &reason) const
Produces a useful error for use when initializing a Ray.
Definition: Ray.C:392
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:214
void invalidateCurrentElem()
Invalidates the Ray&#39;s current element.
Definition: Ray.h:575
void invalidateCurrentPoint()
Invalidates the Ray&#39;s current point.
Definition: Ray.h:584
void errorIfTracing(const std::string &reason) const
Produces a useful error if a Ray has started tracing.
Definition: Ray.C:385
Base class for Ray tracing studies that will generate Rays and then propagate all of them to terminat...