22 static std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
23 auto column = out.tellp();
25 for (
const auto &x : v) {
26 if (out.tellp() - column >= 100) {
36 static std::ostream &operator<<(std::ostream &out,
37 const std::initializer_list<T> &v) {
38 auto column = out.tellp();
40 for (
const auto &x : v) {
41 if (out.tellp() - column >= 100) {
50 template <
typename T, std::
size_t N>
51 static std::ostream &operator<<(std::ostream &out, const std::array<T, N> &a) {
52 auto column = out.tellp();
54 for (
const auto &x : a) {
55 if (out.tellp() - column >= 100) {
70 std::pair<std::array<double, 3>, std::array<double, 3>>
71 GridBase::find_min_and_length(
const Particles &particles) {
72 std::pair<std::array<double, 3>, std::array<double, 3>> r;
73 auto &min_position = r.first;
74 auto &length = r.second;
79 min_position = {{first_position[1], first_position[2], first_position[3]}};
80 auto max_position = min_position;
81 for (
const auto &
p : particles) {
82 const auto &pos =
p.position();
83 min_position[0] = std::min(min_position[0], pos[1]);
84 min_position[1] = std::min(min_position[1], pos[2]);
85 min_position[2] = std::min(min_position[2], pos[3]);
86 max_position[0] = std::max(max_position[0], pos[1]);
87 max_position[1] = std::max(max_position[1], pos[2]);
88 max_position[2] = std::max(max_position[2], pos[3]);
90 length[0] = max_position[0] - min_position[0];
91 length[1] = max_position[1] - min_position[1];
92 length[2] = max_position[2] - min_position[2];
99 template <Gr
idOptions O>
102 const Particles &particles,
double max_interaction_length,
104 : length_(min_and_length.second) {
105 const auto min_position = min_and_length.first;
131 const int max_cells =
133 ? std::cbrt(particle_count)
134 : std::max(2, static_cast<int>(std::cbrt(particle_count)));
138 std::array<double, 3> index_factor = {1. / max_interaction_length,
139 1. / max_interaction_length,
140 1. / max_interaction_length};
145 : static_cast<int>(std::floor(
length_[i] * index_factor[i])) +
164 index_factor[i] = std::nextafter(index_factor[i], 0.);
173 index_factor[i] = std::nextafter(index_factor[i], 0.);
179 const auto &log = logger<LogArea::Grid>();
189 " cells. Therefore the Grid falls back to a single cell / " 194 std::copy_if(particles.
begin(), particles.
end(),
195 std::back_inserter(
cells_.front()),
201 log.debug(
"min: ", min_position,
"\nlength: ",
length_,
215 std::floor((
p.position()[1] - min_position[0]) * index_factor[0]),
216 std::floor((
p.position()[2] - min_position[1]) * index_factor[1]),
217 std::floor((
p.position()[3] - min_position[2]) * index_factor[2]));
220 for (
const auto &
p : particles) {
221 if (
p.xsec_scaling_factor(timestep_duration) > 0.0) {
222 const auto idx = cell_index_for(
p);
227 "\nan out-of-bounds access would be necessary for the " 229 p,
"\nfor a grid with the following parameters:\nmin: ",
230 min_position,
"\nlength: ",
length_,
231 "\ncells: ", number_of_cells_,
"\nindex_factor: ", index_factor,
232 "\ncells_.size: ",
cells_.size(),
"\nrequested index: ", idx);
233 throw std::runtime_error(
"out-of-bounds grid access on construction");
244 template <Gr
idOptions Options>
250 static const std::initializer_list<GridBase::SizeType>
ZERO{0};
251 static const std::initializer_list<GridBase::SizeType>
ZERO_ONE{0, 1};
259 const std::function<
void(
const ParticleList &)> &search_cell_callback,
260 const std::function<
void(
const ParticleList &,
const ParticleList &)>
261 &neighbor_cell_callback)
const {
262 std::array<SizeType, 3> search_index;
268 for (y = 0; y < number_of_cells_[1]; ++y) {
269 for (x = 0; x < number_of_cells_[0]; ++x, ++search_cell_index) {
270 assert(search_cell_index ==
make_index(search_index));
271 assert(search_cell_index >= 0);
273 const ParticleList &search =
cells_[search_cell_index];
274 search_cell_callback(search);
276 const auto &dz_list = z == number_of_cells_[2] - 1 ?
ZERO :
ZERO_ONE;
277 const auto &dy_list = number_of_cells_[1] == 1
280 : y == number_of_cells_[1] - 1
283 const auto &dx_list = number_of_cells_[0] == 1
286 : x == number_of_cells_[0] - 1
294 neighbor_cell_callback(search,
cells_[search_cell_index + di]);
325 const std::function<
void(
const ParticleList &)> &search_cell_callback,
326 const std::function<
void(
const ParticleList &,
const ParticleList &)>
327 &neighbor_cell_callback)
const {
328 const auto &log = logger<LogArea::Grid>();
330 std::array<SizeType, 3> search_index;
337 std::array<NeighborLookup, 2> dz_list;
338 std::array<NeighborLookup, 3> dy_list;
339 std::array<NeighborLookup, 3> dx_list;
346 dz_list[0].index = z;
347 dz_list[1].index = z + 1;
348 if (dz_list[1].index == number_of_cells_[2]) {
349 dz_list[1].index = 0;
353 for (y = 0; y < number_of_cells_[1]; ++y) {
354 dy_list[0].index = y;
355 dy_list[1].index = y - 1;
356 dy_list[2].index = y + 1;
359 dy_list[1] = dy_list[2];
360 dy_list[2].index = number_of_cells_[1] - 1;
362 }
else if (dy_list[2].index == number_of_cells_[1]) {
363 dy_list[2].index = 0;
366 for (x = 0; x < number_of_cells_[0]; ++x, ++search_cell_index) {
367 dx_list[0].index = x;
368 dx_list[1].index = x - 1;
369 dx_list[2].index = x + 1;
372 dx_list[1] = dx_list[2];
373 dx_list[2].index = number_of_cells_[0] - 1;
375 }
else if (dx_list[2].index == number_of_cells_[0]) {
376 dx_list[2].index = 0;
380 assert(search_cell_index ==
make_index(search_index));
381 assert(search_cell_index >= 0);
383 ParticleList search =
cells_[search_cell_index];
384 search_cell_callback(search);
386 auto virtual_search_index = search_index;
388 auto current_wrap_vector = wrap_vector;
390 for (
const auto &dz : dz_list) {
394 virtual_search_index[2] = -1;
396 for (
const auto &dy : dy_list) {
400 virtual_search_index[1] = -1;
403 virtual_search_index[1] = number_of_cells_[1];
405 for (
const auto &dx : dx_list) {
409 virtual_search_index[0] = -1;
412 virtual_search_index[0] = number_of_cells_[0];
414 assert(dx.index >= 0);
415 assert(dx.index < number_of_cells_[0]);
416 assert(dy.index >= 0);
417 assert(dy.index < number_of_cells_[1]);
418 assert(dz.index >= 0);
419 assert(dz.index < number_of_cells_[2]);
420 const auto neighbor_cell_index =
422 assert(neighbor_cell_index >= 0);
424 if (neighbor_cell_index <=
make_index(virtual_search_index)) {
428 if (wrap_vector != current_wrap_vector) {
429 log.debug(
"translating search cell by ",
430 wrap_vector - current_wrap_vector);
432 p = p.
translated(wrap_vector - current_wrap_vector);
434 current_wrap_vector = wrap_vector;
436 neighbor_cell_callback(search,
cells_[neighbor_cell_index]);
438 virtual_search_index[0] = search_index[0];
441 virtual_search_index[1] = search_index[1];
450 const std::pair<std::array<double, 3>, std::array<double, 3>>
452 const Particles &particles,
double max_interaction_length,
455 const std::pair<std::array<double, 3>, std::array<double, 3>>
457 const Particles &particles,
double max_interaction_length,
NeedsToWrap
The options determining what to do if a particle flies out of the grids PlusLength: Used if a periodi...
ParticleData translated(const ThreeVector &delta) const
Translate the particle position.
With ghost cells for periodic boundaries.
The ThreeVector class represents a physical three-vector with the components .
const FourVector & position() const
Get the particle's position in Minkowski space.
static const std::initializer_list< GridBase::SizeType > ZERO_ONE
std::vector< ParticleList > cells_
The cell storage.
ParticleList copy_to_vector() const
Grid(const Particles &particles, double min_cell_length, double timestep_duration, CellSizeStrategy strategy=CellSizeStrategy::Optimal)
Constructs a grid from the given particle list particles.
void iterate_cells(const std::function< void(const ParticleList &)> &search_cell_callback, const std::function< void(const ParticleList &, const ParticleList &)> &neighbor_cell_callback) const
Iterates over all cells in the grid and calls the callback arguments with a search cell and 0 to 13 n...
#define source_location
Hackery that is required to output the location in the source code where the log statement occurs...
double xsec_scaling_factor(double delta_time=0.) const
Return the cross section scaling factor at a given time.
Generic algorithms on containers and ranges.
std::array< int, 3 > number_of_cells_
The number of cells in x, y, and z direction.
int SizeType
A type to store the sizes.
bool all_of(Container &&c, UnaryPredicate &&p)
Convenience wrapper for std::all_of that operates on a complete container.
UnaryFunction for_each(Container &&c, UnaryFunction &&f)
Convenience wrapper for std::for_each that operates on a complete container.
static const std::initializer_list< GridBase::SizeType > MINUS_ONE_ZERO
static const std::initializer_list< GridBase::SizeType > MINUS_ONE_ZERO_ONE
A strust containing the informations needed to search the neighboring cell.
static const std::initializer_list< GridBase::SizeType > ZERO
CellSizeStrategy
Indentifies the strategy of determining the cell size.
The Particles class abstracts the storage and manipulation of particles.
Abstracts a list of cells that partition the particles in the experiment into regions of space that c...
Make cells as large as possible.
SizeType make_index(SizeType x, SizeType y, SizeType z) const
const std::array< double, 3 > length_
The 3 lengths of the complete grid. Used for periodic boundary wrapping.
ParticleData contains the dynamic information of a certain particle.