Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions include/openmc/random_ray/random_ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ namespace openmc {

// Container for MPI exchange
struct RayBufferContainer {
// A ray is always buffered in the state it had when it entered its current
// cell (position, geometry state, distance_travelled, is_active), together
// with the number of segments of that cell it has been attenuated over since.
// The receiving rank traces the cell again from this state and skips those
// segments (see RandomRay::event_advance_ray).
Position position;
Direction direction;
double distance_travelled;
// Distance the ray still has to advance from the buffered geometry state to
// reach `position`. Applied on the receiving rank via move_distance(), which
// uses each coordinate level's own direction.
double advance_distance;
int n_segments_done;
// Number of times this ray has been handed to another rank. The ownership
// early-return in attenuate_flux_inner() happens before n_event() is
// incremented, so max_particle_events cannot bound a forwarding loop.
int n_transfers;
vector<float> angular_flux;
int surface;
bool is_active;
// A ray sent from its sampling site needs the owner's starting flux.
bool needs_initial_flux;
uint64_t ray_id;
int n_event; // Number of events (surface crossings) the ray has undergone

Expand Down Expand Up @@ -62,10 +66,11 @@ struct RayExchangeData {
Position position;
Direction direction;
double distance_travelled;
double advance_distance;
int n_segments_done;
int n_transfers;
int surface;
bool is_active;
bool needs_initial_flux;
uint64_t ray_id;
int n_event; // Number of events (surface crossings) the ray has undergone

Expand Down Expand Up @@ -127,7 +132,8 @@ class RandomRay : public Particle {
SourceSite sample_s2();

bool has_left_subdomain();
void pack_ray_for_buffer(double distance_buffer, double advance_distance);
void pack_ray_for_buffer(
double distance_travelled, bool is_active, bool needs_initial_flux = false);

//----------------------------------------------------------------------------
// Static data members
Expand Down Expand Up @@ -163,6 +169,15 @@ class RandomRay : public Particle {
bool is_active_ {false};
bool is_alive_ {true};
bool is_local_ {true};

// Used to hand a ray to another MPI rank part way through a cell (see
// event_advance_ray): the state of the ray when it entered its current cell,
// the number of segments of that cell it has been attenuated over, and the
// number of those that the rank the ray came from already took care of
double entry_distance_travelled_ {0};
bool entry_is_active_ {false};
int n_segments_done_ {0};
int n_segments_to_skip_ {0};
}; // class RandomRay

} // namespace openmc
Expand Down
105 changes: 50 additions & 55 deletions src/random_ray/random_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ void RandomRay::event_advance_ray()
return;
}

// Record the state of the ray on entry to this cell. If the ray leaves this
// MPI rank's subdomain part way through the cell (between two bins of a
// source region mesh), it is handed over in this state, and the next rank
// traces the cell again and skips the segments attenuated here. Every
// segment is then the same as without domain decomposition, which would not
// be the case if the next rank started a new ray trace from a point inside
// the cell.
entry_distance_travelled_ = distance_travelled_;
entry_is_active_ = is_active_;
n_segments_done_ = 0;

if (is_active_) {
// If the ray is in the active length, need to check if it has
// reached its maximum termination distance. If so, reduce
Expand Down Expand Up @@ -362,6 +373,10 @@ void RandomRay::event_advance_ray()
}
}

// The ray has crossed the whole cell, so there is nothing to skip in the
// next one
n_segments_to_skip_ = 0;

// Advance particle
for (int j = 0; j < n_coord(); ++j) {
coord(j).r() += distance * coord(j).u();
Expand All @@ -373,19 +388,20 @@ void RandomRay::attenuate_flux(double distance, bool is_active, double offset)
// Lookup base source region index
int64_t sr = domain_->lookup_base_source_region_idx(*this);

// Initialize values needed to buffer ray for domain decomposition
double mesh_partial_length = 0.0;
double tiny_multiplier = 0.0;

// Perform ray tracing across mesh
// Determine the mesh index for the base source region, if any
int mesh_idx = domain_->lookup_mesh_idx(sr);

if (mesh_idx == C_NONE) {
// If there's no mesh being applied to this cell, then
// we just attenuate the flux as normal, and set
// the mesh bin to 0
attenuate_flux_inner(distance, is_active, sr, 0, r());
// the mesh bin to 0 (unless the rank this ray came from already did so)
if (n_segments_done_ >= n_segments_to_skip_) {
attenuate_flux_inner(distance, is_active, sr, 0, r());
}
if (!has_left_subdomain()) {
n_segments_done_++;
}
} else {
// If there is a mesh being applied to this cell, then
// we loop over all the bin crossings and attenuate
Expand Down Expand Up @@ -417,55 +433,35 @@ void RandomRay::attenuate_flux(double distance, bool is_active, double offset)
}
#endif

// Very flat angles can result in very small physical lengths,
// despite the TINY_BIT adjustment for Position start. If this happens at
// an MPI boundary, this can cause rays to bounce back and forth
// indefinitely. Very small lengths are therefore skipped. This is a
// domain decomposition workaround, so it is gated on being decomposed:
// applying it unconditionally would silently drop these segments (and
// their volume contribution) for every existing serial user.
if (mpi::n_procs > 1 && physical_length <= TINY_BIT) {
// Skip the segments that the rank this ray came from already attenuated
// the ray over. They are still counted above, because this rank has ray
// traced them again and that is part of its load.
if (n_segments_done_ < n_segments_to_skip_) {
start += physical_length * u();
n_segments_done_++;
continue;
}

attenuate_flux_inner(
physical_length, is_active, sr, mesh_bins_[b], start);

start += physical_length * u();

// If ray has left MPI subdomain, stop transport
// and calculate position
// If the ray has left the MPI subdomain, stop transport here
if (has_left_subdomain()) {
for (int i = 0; i <= b - 1; i++) {
mesh_partial_length += mesh_fractional_lengths_[i];
}

if (b > 0) {
// If ray is stopped within mesh of base source region,
// need to add TINY_BIT to account for deleted length
tiny_multiplier = 1.0;
// Reset last surface crossed to none if ray is stopped
// within mesh of base source region
surface() = 0;
}

mesh_partial_length =
tiny_multiplier * TINY_BIT + reduced_distance * mesh_partial_length;
break;
}

start += physical_length * u();
n_segments_done_++;
}
}

// If ray has left my subdomain, buffer ray state
// If the ray has left my subdomain, buffer it in the state it had on entry
// to this cell. The receiving rank skips the first n_segments_done_ segments.
if (has_left_subdomain()) {
double advance_distance = offset + mesh_partial_length;
double distance_buffer = distance_travelled_ + mesh_partial_length;

#ifdef OPENMC_DAGMC_ENABLED
history().rollback_last_intersection();
#endif
pack_ray_for_buffer(distance_buffer, advance_distance);
pack_ray_for_buffer(entry_distance_travelled_, entry_is_active_);
wgt() = 0.0;
}
}
Expand Down Expand Up @@ -915,6 +911,7 @@ void RandomRay::restart_ray(FlatSourceDomain* domain, RayExchangeData& data,
n_event() = data.n_event;

is_active_ = data.is_active;
n_segments_to_skip_ = data.n_segments_done;

wgt() = 1.0;

Expand Down Expand Up @@ -943,13 +940,10 @@ void RandomRay::restart_ray(FlatSourceDomain* domain, RayExchangeData& data,

u() = data.direction;

// The buffered coordinate levels hold the ray's state at the last geometry
// update, so the ray still has to be advanced to the point at which it left
// the previous subdomain. Use move_distance() rather than adding a single
// top-level displacement to every level: a rotated universe or lattice fill
// gives each level its own direction, and translating a lower level by the
// top-level displacement would put it somewhere else entirely.
move_distance(data.advance_distance);
// The ray is now back at the point where it entered the cell it was in when
// it left the previous subdomain. It is not moved to the point where it left:
// event_advance_ray() traces that cell again and skips the segments that the
// previous rank attenuated the ray over.

#ifdef OPENMC_DAGMC_ENABLED
// Restore DAGMC fields
Expand All @@ -976,8 +970,8 @@ void RandomRay::restart_ray(FlatSourceDomain* domain, RayExchangeData& data,
if (cell_born() == C_NONE)
cell_born() = lowest_coord().cell();

// Set ray's angular flux to value before subdomain change
if (distance_travelled_ > 0.0 || is_active_) {
// Restore the angular flux carried by a ray already in transport.
if (!data.needs_initial_flux) {
for (int g = 0; g < negroups_; g++) {
angular_flux_[g] = angular_flux[g];
}
Expand Down Expand Up @@ -1057,7 +1051,7 @@ void RandomRay::initialize_ray(uint64_t ray_id, FlatSourceDomain* domain)
for (int g = 0; g < negroups_; g++) {
angular_flux_[g] = 0.0;
}
pack_ray_for_buffer(0.0, 0.0);
pack_ray_for_buffer(0.0, is_active_, true);
is_local_ = false;
return;
}
Expand Down Expand Up @@ -1130,18 +1124,19 @@ bool RandomRay::has_left_subdomain()
}

void RandomRay::pack_ray_for_buffer(
double distance_buffer, double advance_distance)
double distance_travelled, bool is_active, bool needs_initial_flux)
{
// Recorded for diagnostics only; the receiving rank reconstructs the
// position by advancing the buffered geometry state (see restart_ray)
exchange_data_.position = r() + advance_distance * u();
exchange_data_.advance_distance = advance_distance;
// Recorded for diagnostics only; the receiving rank takes the position from
// the buffered geometry state (see restart_ray)
exchange_data_.position = r();
exchange_data_.n_transfers = ++n_transfers_;
exchange_data_.direction = u();
exchange_data_.angular_flux = angular_flux_;
exchange_data_.distance_travelled = distance_buffer;
exchange_data_.distance_travelled = distance_travelled;
exchange_data_.n_segments_done = n_segments_done_;
exchange_data_.surface = surface();
exchange_data_.is_active = is_active_;
exchange_data_.is_active = is_active;
exchange_data_.needs_initial_flux = needs_initial_flux;
exchange_data_.ray_id = id();
exchange_data_.n_event = n_event();

Expand Down
10 changes: 5 additions & 5 deletions src/random_ray/ray_bank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ void RayBank::buffer_ray_data_to_send(RandomRay& ray, FlatSourceDomain* domain)
rd.position = rbc.position;
rd.direction = rbc.direction;
rd.distance_travelled = rbc.distance_travelled;
rd.advance_distance = rbc.advance_distance;
rd.n_segments_done = rbc.n_segments_done;
rd.n_transfers = rbc.n_transfers;
rd.surface = rbc.surface;
rd.is_active = rbc.is_active;
rd.needs_initial_flux = rbc.needs_initial_flux;
rd.ray_id = rbc.ray_id;
rd.n_event = rbc.n_event;
rd.n_coord = rbc.n_coord;
Expand Down Expand Up @@ -227,12 +228,11 @@ void RayBank::communicate_rays()
for (auto& [receiving_rank, buffers] : ray_send_buffer_) {
int num_rays = buffers.count;

// Check neighbor list and add if not already known
// Filter out rays that are sampled elsewhere
// Record neighbors reached during transport. A sampling site can belong
// to any rank.
bool has_transported_rays = false;
for (int i = 0; i < num_rays; i++) {
if (buffers.ray_data[i].distance_travelled > 0.0 ||
buffers.ray_data[i].is_active) {
if (!buffers.ray_data[i].needs_initial_flux) {
has_transported_rays = true;
break;
}
Expand Down
Loading