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) {
66 static constexpr
int LGrid = LogArea::Grid::id;
71 std::pair<std::array<double, 3>, std::array<double, 3>>
73 std::pair<std::array<double, 3>, std::array<double, 3>> r;
74 auto &min_position = r.first;
75 auto &length = r.second;
80 min_position = {{first_position[1], first_position[2], first_position[3]}};
81 auto max_position = min_position;
82 for (
const auto &
p : particles) {
83 const auto &pos =
p.position();
84 min_position[0] = std::min(min_position[0], pos[1]);
85 min_position[1] = std::min(min_position[1], pos[2]);
86 min_position[2] = std::min(min_position[2], pos[3]);
87 max_position[0] = std::max(max_position[0], pos[1]);
88 max_position[1] = std::max(max_position[1], pos[2]);
89 max_position[2] = std::max(max_position[2], pos[3]);
91 length[0] = max_position[0] - min_position[0];
92 length[1] = max_position[1] - min_position[1];
93 length[2] = max_position[2] - min_position[2];
100 template <Gr
idOptions O>
103 const Particles &particles,
double max_interaction_length,
106 : length_(min_and_length.second) {
107 const auto min_position = min_and_length.first;
127 const int max_cells =
129 ? std::cbrt(particle_count)
130 : std::max(2,
static_cast<int>(std::cbrt(particle_count)));
135 std::array<double, 3> index_factor = {1. / max_interaction_length,
136 1. / max_interaction_length,
137 1. / max_interaction_length};
142 :
static_cast<int>(std::floor(
length_[i] * index_factor[i]));
154 std::string error_box_too_small =
155 "Input error: Your box is too small for the grid.\n"
156 "The minimal length of the box is given by: " +
157 std::to_string(2 * max_interaction_length) +
158 " fm with the given timestep size.\n"
159 "If you have large timesteps please reduce them.\n"
160 "A larger box or the use of testparticles also helps.\n"
161 "Please take a look at your config.";
162 throw std::runtime_error(error_box_too_small);
170 if (
length_[i] >= max_interaction_length) {
175 index_factor[i] = std::nextafter(index_factor[i], 0.);
191 " cells. Therefore the Grid falls back to a single cell / "
197 std::copy_if(particles.
begin(), particles.
end(),
198 std::back_inserter(
cells_.front()),
200 return p.xsec_scaling_factor(timestep_duration) > 0.0;
212 "\nindex_factor: ", index_factor);
225 std::floor((
p.position()[1] - min_position[0]) * index_factor[0]),
226 std::floor((
p.position()[2] - min_position[1]) * index_factor[1]),
227 std::floor((
p.position()[3] - min_position[2]) * index_factor[2]));
230 for (
const auto &
p : particles) {
231 if (
p.xsec_scaling_factor(timestep_duration) > 0.0) {
232 const auto idx = cell_index_for(
p);
237 "\nan out-of-bounds access would be necessary for the "
239 p,
"\nfor a grid with the following parameters:\nmin: ",
240 min_position,
"\nlength: ",
length_,
242 "\ncells_.size: ",
cells_.size(),
"\nrequested index: ", idx);
243 throw std::runtime_error(
"out-of-bounds grid access on construction");
254 template <Gr
idOptions Options>
257 return (z * number_of_cells_[1] + y) * number_of_cells_[0] + x;
260 static const std::initializer_list<GridBase::SizeType>
ZERO{0};
261 static const std::initializer_list<GridBase::SizeType>
ZERO_ONE{0, 1};
269 const std::function<
void(
const ParticleList &)> &search_cell_callback,
270 const std::function<
void(
const ParticleList &,
const ParticleList &)>
271 &neighbor_cell_callback)
const {
272 std::array<SizeType, 3> search_index;
277 for (z = 0; z < number_of_cells_[2]; ++z) {
278 for (y = 0; y < number_of_cells_[1]; ++y) {
279 for (x = 0; x < number_of_cells_[0]; ++x, ++search_cell_index) {
280 assert(search_cell_index == make_index(search_index));
281 assert(search_cell_index >= 0);
282 assert(search_cell_index <
SizeType(cells_.size()));
283 const ParticleList &search = cells_[search_cell_index];
284 search_cell_callback(search);
286 const auto &dz_list = z == number_of_cells_[2] - 1 ?
ZERO :
ZERO_ONE;
287 const auto &dy_list = number_of_cells_[1] == 1
290 : y == number_of_cells_[1] - 1
293 const auto &dx_list = number_of_cells_[0] == 1
296 : x == number_of_cells_[0] - 1
302 const auto di = make_index(dx, dy, dz);
304 neighbor_cell_callback(search, cells_[search_cell_index + di]);
335 const std::function<
void(
const ParticleList &)> &search_cell_callback,
336 const std::function<
void(
const ParticleList &,
const ParticleList &)>
337 &neighbor_cell_callback)
const {
338 std::array<SizeType, 3> search_index;
345 std::array<NeighborLookup, 2> dz_list;
346 std::array<NeighborLookup, 3> dy_list;
347 std::array<NeighborLookup, 3> dx_list;
349 assert(number_of_cells_[2] >= 2);
350 assert(number_of_cells_[1] >= 2);
351 assert(number_of_cells_[0] >= 2);
353 for (z = 0; z < number_of_cells_[2]; ++z) {
354 dz_list[0].index = z;
355 dz_list[1].index = z + 1;
356 if (dz_list[1].index == number_of_cells_[2]) {
357 dz_list[1].index = 0;
361 for (y = 0; y < number_of_cells_[1]; ++y) {
362 dy_list[0].index = y;
363 dy_list[1].index = y - 1;
364 dy_list[2].index = y + 1;
367 dy_list[1] = dy_list[2];
368 dy_list[2].index = number_of_cells_[1] - 1;
370 }
else if (dy_list[2].index == number_of_cells_[1]) {
371 dy_list[2].index = 0;
374 for (x = 0; x < number_of_cells_[0]; ++x, ++search_cell_index) {
375 dx_list[0].index = x;
376 dx_list[1].index = x - 1;
377 dx_list[2].index = x + 1;
380 dx_list[1] = dx_list[2];
381 dx_list[2].index = number_of_cells_[0] - 1;
383 }
else if (dx_list[2].index == number_of_cells_[0]) {
384 dx_list[2].index = 0;
388 assert(search_cell_index == make_index(search_index));
389 assert(search_cell_index >= 0);
390 assert(search_cell_index <
SizeType(cells_.size()));
391 ParticleList search = cells_[search_cell_index];
392 search_cell_callback(search);
394 auto virtual_search_index = search_index;
396 auto current_wrap_vector = wrap_vector;
398 for (
const auto &dz : dz_list) {
401 wrap_vector[2] = -length_[2];
402 virtual_search_index[2] = -1;
404 for (
const auto &dy : dy_list) {
407 wrap_vector[1] = -length_[1];
408 virtual_search_index[1] = -1;
410 wrap_vector[1] = length_[1];
411 virtual_search_index[1] = number_of_cells_[1];
413 for (
const auto &dx : dx_list) {
416 wrap_vector[0] = -length_[0];
417 virtual_search_index[0] = -1;
419 wrap_vector[0] = length_[0];
420 virtual_search_index[0] = number_of_cells_[0];
422 assert(dx.index >= 0);
423 assert(dx.index < number_of_cells_[0]);
424 assert(dy.index >= 0);
425 assert(dy.index < number_of_cells_[1]);
426 assert(dz.index >= 0);
427 assert(dz.index < number_of_cells_[2]);
428 const auto neighbor_cell_index =
429 make_index(dx.index, dy.index, dz.index);
430 assert(neighbor_cell_index >= 0);
431 assert(neighbor_cell_index <
SizeType(cells_.size()));
432 if (neighbor_cell_index <= make_index(virtual_search_index)) {
436 if (wrap_vector != current_wrap_vector) {
437 logg[
LGrid].debug(
"translating search cell by ",
438 wrap_vector - current_wrap_vector);
440 p =
p.translated(wrap_vector - current_wrap_vector);
442 current_wrap_vector = wrap_vector;
444 neighbor_cell_callback(search, cells_[neighbor_cell_index]);
446 virtual_search_index[0] = search_index[0];
449 virtual_search_index[1] = search_index[1];
458 const std::pair<std::array<double, 3>, std::array<double, 3>>
460 const Particles &particles,
double max_interaction_length,
464 const std::pair<std::array<double, 3>, std::array<double, 3>>
466 const Particles &particles,
double max_interaction_length,
Generic algorithms on containers and ranges.
int SizeType
A type to store the sizes.
static std::pair< std::array< double, 3 >, std::array< double, 3 > > find_min_and_length(const Particles &particles)
const std::array< double, 3 > length_
The 3 lengths of the complete grid. Used for periodic boundary wrapping.
std::array< int, 3 > number_of_cells_
The number of cells in x, y, and z direction.
double cell_volume_
The volume of a single cell.
std::vector< ParticleList > cells_
The cell storage.
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...
Grid(const Particles &particles, double min_cell_length, double timestep_duration, CellNumberLimitation limit, CellSizeStrategy strategy=CellSizeStrategy::Optimal)
Constructs a grid from the given particle list particles.
SizeType make_index(SizeType x, SizeType y, SizeType z) const
ParticleData contains the dynamic information of a certain particle.
const FourVector & position() const
Get the particle's position in Minkowski space.
The Particles class abstracts the storage and manipulation of particles.
ParticleList copy_to_vector() const
The ThreeVector class represents a physical three-vector with the components .
#define SMASH_SOURCE_LOCATION
Hackery that is required to output the location in the source code where the log statement occurs.
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
static const std::initializer_list< GridBase::SizeType > ZERO_ONE
UnaryFunction for_each(Container &&c, UnaryFunction &&f)
Convenience wrapper for std::for_each that operates on a complete container.
NeedsToWrap
The options determining what to do if a particle flies out of the grids PlusLength: Used if a periodi...
CellNumberLimitation
Identifies whether the number of cells should be limited.
@ ParticleNumber
Limit the number of cells to the number of particles.
bool all_of(Container &&c, UnaryPredicate &&p)
Convenience wrapper for std::all_of that operates on a complete container.
@ Normal
Without ghost cells.
@ PeriodicBoundaries
With ghost cells for periodic boundaries.
static const std::initializer_list< GridBase::SizeType > MINUS_ONE_ZERO
static const std::initializer_list< GridBase::SizeType > ZERO
static constexpr int LGrid
CellSizeStrategy
Indentifies the strategy of determining the cell size.
@ Largest
Make cells as large as possible.
static const std::initializer_list< GridBase::SizeType > MINUS_ONE_ZERO_ONE
A strust containing the informations needed to search the neighboring cell.
Grid< GridOptions::PeriodicBoundaries >::SizeType index
Index of the cell.
NeedsToWrap wrap
Option to determine the neighbors of the cells on the boundary.