https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
ClaimRays Class Reference

Helper object for claiming Rays. More...

#include <ClaimRays.h>

Inheritance diagram for ClaimRays:
[legend]

Public Member Functions

 ClaimRays (RayTracingStudy &study, const std::vector< std::shared_ptr< Ray > > &rays, std::vector< std::shared_ptr< Ray > > &local_rays, const bool do_exchange)
 Constructor.
 
virtual void meshChanged () override
 Call on mesh changes to reinit the necessary data structures.
 
void claim ()
 Claim the Rays.
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static InputParameters validParams ()
 

Protected Member Functions

virtual void init ()
 Initialize the object.
 
virtual void preClaim ()
 Entry point before claim()
 
virtual void postClaim ()
 Entry point after claim()
 
virtual void prePossiblyClaimRay (const std::shared_ptr< Ray > &)
 Entry point before possibly claiming a Ray.
 
virtual void postClaimRay (std::shared_ptr< Ray > &ray, const Elem *elem)
 Entry point for acting on a Ray after it is claimed.
 
virtual RayID getID (const std::shared_ptr< Ray > &ray) const
 Gets an ID associated with the Ray for claiming purposes.
 
const libMesh::BoundingBoxinflatedBoundingBox (const processor_id_type pid) const
 Get the inflated bounding box for rank \pid.
 

Protected Attributes

MooseMesh_mesh
 The mesh.
 
const processor_id_type _pid
 This processor ID.
 
const bool _do_exchange
 Whether or not the Rays need to be initially exchanged.
 
RayTracingStudy_study
 The RayTracingStudy.
 
ParallelStudy< std::shared_ptr< Ray >, Ray > & _parallel_study
 The ParallelStudy, used as the context for communicating rays.
 
const Parallel::Communicator & _communicator
 
FEProblemBase_mci_feproblem
 

Private Member Functions

void buildBoundingBoxes ()
 Builds the bounding boxes (_inflated_bboxes).
 
void buildPointNeighbors ()
 Build the map of elements to all of their point neighbors.
 
void possiblyClaim (const std::shared_ptr< Ray > &obj)
 Possibly claim a Ray.
 
void verifyClaiming ()
 Verifies that the claiming process succeeded.
 
const ElemclaimPoint (const Point &point, const RayID id, const Elem *elem)
 Try to claim a spatial point.
 

Private Attributes

const std::vector< std::shared_ptr< Ray > > & _rays
 The Rays that need to be searched to possibly claimed.
 
std::vector< std::shared_ptr< Ray > > & _local_rays
 The local Rays that are claimed.
 
std::unique_ptr< libMesh::PointLocatorBase_point_locator = nullptr
 The point locator.
 
std::vector< libMesh::BoundingBox_inflated_bboxes
 The inflated bounding boxes for all processors.
 
std::unordered_map< dof_id_type, std::vector< const Elem * > > _elem_point_neighbors
 Map of point neighbors for each element.
 
bool _needs_init
 Whether or not an init is needed (bounding boxes, neighbors)
 

Detailed Description

Helper object for claiming Rays.

Definition at line 34 of file ClaimRays.h.

Constructor & Destructor Documentation

◆ ClaimRays()

ClaimRays::ClaimRays ( RayTracingStudy study,
const std::vector< std::shared_ptr< Ray > > &  rays,
std::vector< std::shared_ptr< Ray > > &  local_rays,
const bool  do_exchange 
)

Constructor.

Parameters
studyThe RayTracingStudy
parallel_studyThe base parallel study
meshThe MooseMesh
raysThe vector of Rays that need to be claimed
local_raysInsertion point for Rays that have been claimed
do_exchangeWhether or not an exchange is needed, i.e., if "rays" still needs to be filled by objects on other processors

Definition at line 24 of file ClaimRays.C.

28 : ParallelObject(study.comm()),
30 _mesh(study.mesh()),
31 _pid(comm().rank()),
32 _do_exchange(do_exchange),
33 _study(study),
35 _rays(rays),
36 _local_rays(local_rays),
37 _needs_init(true)
38{
39}
const std::vector< std::shared_ptr< Ray > > & _rays
The Rays that need to be searched to possibly claimed.
Definition ClaimRays.h:157
bool _needs_init
Whether or not an init is needed (bounding boxes, neighbors)
Definition ClaimRays.h:171
MooseMesh & _mesh
The mesh.
Definition ClaimRays.h:110
RayTracingStudy & _study
The RayTracingStudy.
Definition ClaimRays.h:118
ParallelStudy< std::shared_ptr< Ray >, Ray > & _parallel_study
The ParallelStudy, used as the context for communicating rays.
Definition ClaimRays.h:120
const processor_id_type _pid
This processor ID.
Definition ClaimRays.h:112
std::vector< std::shared_ptr< Ray > > & _local_rays
The local Rays that are claimed.
Definition ClaimRays.h:159
const bool _do_exchange
Whether or not the Rays need to be initially exchanged.
Definition ClaimRays.h:115
const InputParameters & parameters() const
MooseMesh & mesh()
ParallelStudy< std::shared_ptr< Ray >, Ray > * parallelStudy()
The underlying parallel study: used for the context for calling the packed range routines.
const Parallel::Communicator & comm() const

Member Function Documentation

◆ buildBoundingBoxes()

void ClaimRays::buildBoundingBoxes ( )
private

Builds the bounding boxes (_inflated_bboxes).

Definition at line 181 of file ClaimRays.C.

182{
183 // Local bounding box
185
186 // Gather the bounding boxes of all processors
187 std::vector<std::pair<Point, Point>> bb_points = {static_cast<std::pair<Point, Point>>(bbox)};
188 comm().allgather(bb_points, true);
189
190 // Inflate the local bboxes by a bit and store
191 _inflated_bboxes.resize(comm().size());
192 for (processor_id_type pid = 0; pid < comm().size(); ++pid)
193 {
194 BoundingBox pid_bbox = static_cast<BoundingBox>(bb_points[pid]);
195 pid_bbox.scale(0.01);
196 _inflated_bboxes[pid] = pid_bbox;
197 }
198}
std::vector< libMesh::BoundingBox > _inflated_bboxes
The inflated bounding boxes for all processors.
Definition ClaimRays.h:165
MeshBase & getMesh()
processor_id_type size() const
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
void scale(const Real factor)
libMesh::BoundingBox create_local_bounding_box(const MeshBase &mesh)
uint8_t processor_id_type

Referenced by init().

◆ buildPointNeighbors()

void ClaimRays::buildPointNeighbors ( )
private

Build the map of elements to all of their point neighbors.

TODO: Move this eventually into MooseMesh, MeshBase, or FEProblemBase

Definition at line 201 of file ClaimRays.C.

202{
203 _elem_point_neighbors.clear();
204 const auto & node_to_elem_map = _mesh.nodeToElemMap();
205
206 for (const auto & elem : _mesh.getMesh().active_element_ptr_range())
207 {
208 auto & fill = _elem_point_neighbors[elem->id()];
209 for (unsigned int v = 0; v < elem->n_vertices(); ++v)
210 {
211 const auto & node = elem->node_ptr(v);
212 for (const auto & neighbor_id : libmesh_map_find(node_to_elem_map, node->id()))
213 {
214 if (neighbor_id == elem->id())
215 continue;
216
217 const auto & neighbor = _mesh.elemPtr(neighbor_id);
218 if (std::count(fill.begin(), fill.end(), neighbor) == 0)
219 fill.emplace_back(neighbor);
220 }
221 }
222 }
223}
const double v
std::unordered_map< dof_id_type, std::vector< const Elem * > > _elem_point_neighbors
Map of point neighbors for each element.
Definition ClaimRays.h:168
virtual Elem * elemPtr(const dof_id_type i)
const std::unordered_map< dof_id_type, std::vector< dof_id_type > > & nodeToElemMap()
T & getMesh(MooseMesh &mesh)
function to cast mesh
Definition SCM.h:35

Referenced by init().

◆ claim()

void ClaimRays::claim ( )

Claim the Rays.

do_exchange sets whether or not an exchange is needed, i.e., if _rays still needs to be filled by objects on other processors

Definition at line 42 of file ClaimRays.C.

43{
44 if (_needs_init)
45 {
46 init();
47 _needs_init = false;
48 }
49
50 preClaim();
51
52 // Clear these as we're about to fill
53 _local_rays.clear();
54
55 // Grab the point locator
57 _point_locator->enable_out_of_mesh_mode();
58
59 // Exchange: filter Rays into processors that _may_ claim them
60 std::unordered_map<processor_id_type, std::vector<std::shared_ptr<Ray>>> rays_to_send;
61 if (_do_exchange)
62 for (processor_id_type pid = 0; pid < comm().size(); ++pid)
63 if (_pid != pid)
64 {
65 const BoundingBox & pid_bbox = inflatedBoundingBox(pid);
66 for (auto & ray : _rays)
67 if (pid_bbox.contains_point(ray->currentPoint()))
68 rays_to_send[pid].push_back(ray);
69 }
70
71 // Functor for possibly claiming a vector of Rays
72 auto claim_functor =
73 [&](processor_id_type /* pid */, const std::vector<std::shared_ptr<Ray>> & rays)
74 {
75 for (auto & ray : rays)
76 possiblyClaim(ray);
77 };
78
79 // Send the relevant Rays to everyone and then attempt to claim the ones that we receive
80 if (_do_exchange)
81 Parallel::push_parallel_packed_range(comm(), rays_to_send, &_parallel_study, claim_functor);
82
83 // Attempt to claim the locally generated rays in _rays
84 claim_functor(_pid, _rays);
85
86 // Verify the claiming if the study so desires
87 if (_study.verifyRays())
89
90 postClaim();
91}
void possiblyClaim(const std::shared_ptr< Ray > &obj)
Possibly claim a Ray.
Definition ClaimRays.C:94
const libMesh::BoundingBox & inflatedBoundingBox(const processor_id_type pid) const
Get the inflated bounding box for rank \pid.
Definition ClaimRays.h:104
virtual void init()
Initialize the object.
Definition ClaimRays.C:168
virtual void postClaim()
Entry point after claim()
Definition ClaimRays.h:78
std::unique_ptr< libMesh::PointLocatorBase > _point_locator
The point locator.
Definition ClaimRays.h:162
virtual void preClaim()
Entry point before claim()
Definition ClaimRays.h:74
void verifyClaiming()
Verifies that the claiming process succeeded.
Definition ClaimRays.C:226
bool verifyRays() const
Whether or not to verify if Rays have valid information before being traced.
static std::unique_ptr< PointLocatorBase > build(PointLocatorType t, const MeshBase &mesh, const PointLocatorBase *master=nullptr)
if(subdm)
TREE_LOCAL_ELEMENTS

Referenced by RepeatableRayStudyBase::claimRaysInternal(), and TestPICRayStudy::generateRays().

◆ claimPoint()

const Elem * ClaimRays::claimPoint ( const Point point,
const RayID  id,
const Elem elem 
)
private

Try to claim a spatial point.

Parameters
pointThe point to claim
idAn ID associated with the point
elemThe local element to first consider for this processor's ownership
Returns
The element that contains the point if we claim the point, nullptr if we don't claim it

Definition at line 108 of file ClaimRays.C.

109{
110 if (elem)
111 {
112 // Looking for smallest (even ID Ray) or largest (odd ID Ray) elem id
113 const bool smallest = id % 2 == 0;
114
115 // Start with the element we found, as it is a valid candidate
116 const Elem * extremum_elem = elem;
117
118 // All point neighbors for this element
119 mooseAssert(_elem_point_neighbors.count(elem->id()), "Not in point neighbor map");
120 const auto & neighbors = _elem_point_neighbors.at(elem->id());
121
122 // Find element that matches the extremum criteria
123 for (const auto & neighbor : neighbors)
124 {
125 mooseAssert(neighbor->active(), "Inactive neighbor");
126
127 if ((smallest && neighbor->id() < extremum_elem->id()) || // satisfies
128 (!smallest && neighbor->id() > extremum_elem->id())) // ...one of the id checks
129 if (neighbor->contains_point(point)) // and also contains the point
130 extremum_elem = neighbor;
131 }
132
133 // Claim the object if we own the extremum elem
134 if (extremum_elem->processor_id() == _pid)
135 {
136 mooseAssert(extremum_elem->active(), "Inactive element");
137 return extremum_elem;
138 }
139 }
140
141 return nullptr;
142}
processor_id_type processor_id() const
dof_id_type id() const
bool active() const

Referenced by possiblyClaim().

◆ getID()

virtual RayID ClaimRays::getID ( const std::shared_ptr< Ray > &  ray) const
inlineprotectedvirtual

Gets an ID associated with the Ray for claiming purposes.

Defaults to the Ray's ID.

To break ties in claiming (when multiple processors have elements that contain a point, say on an element's side on a processor boundary), we pick the smallest element ID when this ID is even and the largest element ID is odd. It is possible that the same Rays can be generated with different IDs, in which case the user may want to use a different ID for this process.

Definition at line 99 of file ClaimRays.h.

99{ return ray->id(); }

Referenced by possiblyClaim(), and verifyClaiming().

◆ inflatedBoundingBox()

const libMesh::BoundingBox & ClaimRays::inflatedBoundingBox ( const processor_id_type  pid) const
inlineprotected

Get the inflated bounding box for rank \pid.

Definition at line 104 of file ClaimRays.h.

105 {
106 return _inflated_bboxes[pid];
107 }

Referenced by claim().

◆ init()

void ClaimRays::init ( )
protectedvirtual

Initialize the object.

Definition at line 168 of file ClaimRays.C.

169{
172}
void buildPointNeighbors()
Build the map of elements to all of their point neighbors.
Definition ClaimRays.C:201
void buildBoundingBoxes()
Builds the bounding boxes (_inflated_bboxes).
Definition ClaimRays.C:181

Referenced by claim().

◆ meshChanged()

void ClaimRays::meshChanged ( )
overridevirtual

Call on mesh changes to reinit the necessary data structures.

Reimplemented from MeshChangedInterface.

Definition at line 175 of file ClaimRays.C.

176{
177 _needs_init = true;
178}

◆ possiblyClaim()

void ClaimRays::possiblyClaim ( const std::shared_ptr< Ray > &  obj)
private

Possibly claim a Ray.

Definition at line 94 of file ClaimRays.C.

95{
97
98 const auto elem =
99 claimPoint(ray->currentPoint(), getID(ray), (*_point_locator)(ray->currentPoint()));
100 if (elem)
101 {
102 _local_rays.push_back(ray);
103 postClaimRay(_local_rays.back(), elem);
104 }
105}
virtual void postClaimRay(std::shared_ptr< Ray > &ray, const Elem *elem)
Entry point for acting on a Ray after it is claimed.
Definition ClaimRays.C:145
virtual RayID getID(const std::shared_ptr< Ray > &ray) const
Gets an ID associated with the Ray for claiming purposes.
Definition ClaimRays.h:99
virtual void prePossiblyClaimRay(const std::shared_ptr< Ray > &)
Entry point before possibly claiming a Ray.
Definition ClaimRays.h:82
const Elem * claimPoint(const Point &point, const RayID id, const Elem *elem)
Try to claim a spatial point.
Definition ClaimRays.C:108

Referenced by claim().

◆ postClaim()

virtual void ClaimRays::postClaim ( )
inlineprotectedvirtual

Entry point after claim()

Definition at line 78 of file ClaimRays.h.

78{}

Referenced by claim().

◆ postClaimRay()

void ClaimRays::postClaimRay ( std::shared_ptr< Ray > &  ray,
const Elem elem 
)
protectedvirtual

Entry point for acting on a Ray after it is claimed.

Definition at line 145 of file ClaimRays.C.

146{
147 mooseAssert(_mesh.queryElemPtr(elem->id()) == elem, "Mesh doesn't contain elem");
148 mooseAssert(elem->active(), "Inactive element");
149
150 // If the incoming side is set and is not incoming, or if it is not set at all, see
151 // if we can find an incoming side that is valid.
152 auto starting_incoming_side = RayTracingCommon::invalid_side;
153 if (!(!ray->invalidCurrentIncomingSide() &&
154 _study.elemSide(*elem, ray->currentIncomingSide()).contains_point(ray->currentPoint()) &&
155 _study.sideIsIncoming(elem, ray->currentIncomingSide(), ray->direction(), /* tid = */ 0)))
156 for (const auto s : elem->side_index_range())
157 if (_study.elemSide(*elem, s).contains_point(ray->currentPoint()) &&
158 _study.sideIsIncoming(elem, s, ray->direction(), /* tid = */ 0))
159 {
160 starting_incoming_side = s;
161 break;
162 }
163
164 ray->setStart(ray->currentPoint(), elem, starting_incoming_side);
165}
virtual Elem * queryElemPtr(const dof_id_type i)
bool sideIsIncoming(const Elem *const elem, const unsigned short side, const Point &direction, const THREAD_ID tid)
Whether or not side is incoming on element elem in direction direction.
const libMesh::Elem & elemSide(const libMesh::Elem &elem, const unsigned int s, const THREAD_ID tid=0)
Get an element's side pointer without excessive memory allocation.
virtual bool contains_point(const Point &p, Real tol=TOLERANCE) const
static const unsigned short invalid_side
Identifier for an invalid side index.

Referenced by possiblyClaim().

◆ preClaim()

virtual void ClaimRays::preClaim ( )
inlineprotectedvirtual

Entry point before claim()

Definition at line 74 of file ClaimRays.h.

74{}

Referenced by claim().

◆ prePossiblyClaimRay()

virtual void ClaimRays::prePossiblyClaimRay ( const std::shared_ptr< Ray > &  )
inlineprotectedvirtual

Entry point before possibly claiming a Ray.

Definition at line 82 of file ClaimRays.h.

82{}

Referenced by possiblyClaim().

◆ verifyClaiming()

void ClaimRays::verifyClaiming ( )
private

Verifies that the claiming process succeeded.

That is, all Rays were claimed once and only once.

Definition at line 226 of file ClaimRays.C.

227{
228 // NOTE for all of the following: we use char here in place of bool.
229 // This is because bool is not instantiated as a StandardType in
230 // TIMPI due to the fun of std::vector<bool>
231
232 // Map from Ray ID -> whether or not it was generated (false) or
233 // claimed/possibly also generated (true)
234 std::map<RayID, char> local_map;
235 auto add_to_local_map =
236 [this, &local_map](const std::vector<std::shared_ptr<Ray>> & rays, const bool claimed_rays)
237 {
238 for (const auto & ray : rays)
239 {
240 const auto id = getID(ray);
241
242 // Try to insert into the map
243 auto emplace_pair = local_map.emplace(id, claimed_rays);
244
245 // If it already exists but has not been claimed yet, set it to being claimed
246 if (!emplace_pair.second && claimed_rays)
247 {
248 mooseAssert(!emplace_pair.first->second,
249 "Ray was claimed more than once on a single processor");
250 emplace_pair.first->second = true;
251 }
252 }
253 };
254
255 // Build the local_map
256 add_to_local_map(_rays, false);
257 add_to_local_map(_local_rays, true);
258
259 // Build the structure to send the local generation/claiming information to rank 0
260 std::map<processor_id_type, std::vector<std::pair<RayID, char>>> send_info;
261 if (local_map.size())
262 send_info.emplace(std::piecewise_construct,
263 std::forward_as_tuple(0),
264 std::forward_as_tuple(local_map.begin(), local_map.end()));
265
266 // The mapping (filled on rank 0) from Ray ID -> (processor id, claiming status)
267 std::map<RayID, std::vector<std::pair<processor_id_type, char>>> global_map;
268
269 // Functor for receiving the generation/claiming information
270 auto receive_functor = [&global_map](processor_id_type pid,
271 const std::vector<std::pair<RayID, char>> & id_claimed_pairs)
272 {
273 for (const auto & id_claimed_pair : id_claimed_pairs)
274 global_map[id_claimed_pair.first].emplace_back(pid, id_claimed_pair.second);
275 };
276
277 // Send claiming information to rank 0
278 Parallel::push_parallel_vector_data(comm(), send_info, receive_functor);
279
280 // Rank 0 will make sure everything looks good
281 if (_pid == 0)
282 for (const auto & id_pairs_pair : global_map)
283 {
284 const RayID id = id_pairs_pair.first;
285 const std::vector<std::pair<processor_id_type, char>> & pid_claimed_pairs =
286 id_pairs_pair.second;
287
288 std::vector<processor_id_type> claimed_pids;
289 for (const auto & pid_claimed_pair : pid_claimed_pairs)
290 if (pid_claimed_pair.second)
291 claimed_pids.push_back(pid_claimed_pair.first);
292
293 if (claimed_pids.size() == 0)
294 _study.mooseError("Failed to claim the Ray with ID ", id);
295 mooseAssert(claimed_pids.size() == 1, "Ray was claimed on multiple processors");
296 }
297}
unsigned long int RayID
Type for a Ray's ID.
Definition Ray.h:44
void mooseError(Args &&... args) const

Referenced by claim().

Member Data Documentation

◆ _do_exchange

const bool ClaimRays::_do_exchange
protected

Whether or not the Rays need to be initially exchanged.

Definition at line 115 of file ClaimRays.h.

Referenced by claim().

◆ _elem_point_neighbors

std::unordered_map<dof_id_type, std::vector<const Elem *> > ClaimRays::_elem_point_neighbors
private

Map of point neighbors for each element.

Definition at line 168 of file ClaimRays.h.

Referenced by buildPointNeighbors(), and claimPoint().

◆ _inflated_bboxes

std::vector<libMesh::BoundingBox> ClaimRays::_inflated_bboxes
private

The inflated bounding boxes for all processors.

Definition at line 165 of file ClaimRays.h.

Referenced by buildBoundingBoxes(), and inflatedBoundingBox().

◆ _local_rays

std::vector<std::shared_ptr<Ray> >& ClaimRays::_local_rays
private

The local Rays that are claimed.

Definition at line 159 of file ClaimRays.h.

Referenced by claim(), possiblyClaim(), and verifyClaiming().

◆ _mesh

MooseMesh& ClaimRays::_mesh
protected

The mesh.

Definition at line 110 of file ClaimRays.h.

Referenced by buildBoundingBoxes(), buildPointNeighbors(), claim(), and postClaimRay().

◆ _needs_init

bool ClaimRays::_needs_init
private

Whether or not an init is needed (bounding boxes, neighbors)

Definition at line 171 of file ClaimRays.h.

Referenced by claim(), and meshChanged().

◆ _parallel_study

ParallelStudy<std::shared_ptr<Ray>, Ray>& ClaimRays::_parallel_study
protected

The ParallelStudy, used as the context for communicating rays.

Definition at line 120 of file ClaimRays.h.

Referenced by claim().

◆ _pid

const processor_id_type ClaimRays::_pid
protected

This processor ID.

Definition at line 112 of file ClaimRays.h.

Referenced by claim(), claimPoint(), and verifyClaiming().

◆ _point_locator

std::unique_ptr<libMesh::PointLocatorBase> ClaimRays::_point_locator = nullptr
private

The point locator.

Definition at line 162 of file ClaimRays.h.

Referenced by claim(), and possiblyClaim().

◆ _rays

const std::vector<std::shared_ptr<Ray> >& ClaimRays::_rays
private

The Rays that need to be searched to possibly claimed.

Definition at line 157 of file ClaimRays.h.

Referenced by claim(), and verifyClaiming().

◆ _study

RayTracingStudy& ClaimRays::_study
protected

The RayTracingStudy.

Definition at line 118 of file ClaimRays.h.

Referenced by claim(), postClaimRay(), and verifyClaiming().


The documentation for this class was generated from the following files: