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,
105 : length_(min_and_length.second) {
106 const auto min_position = min_and_length.first;
110 if (O == GridOptions::Normal && strategy == CellSizeStrategy::Largest) {
111 number_of_cells_ = {1, 1, 1};
112 cell_volume_ = length_[0] * length_[1] * length_[2];
133 const int max_cells =
134 (O == GridOptions::Normal)
135 ? std::cbrt(particle_count)
136 : std::max(2, static_cast<int>(std::cbrt(particle_count)));
140 std::array<double, 3> index_factor = {1. / max_interaction_length,
141 1. / max_interaction_length,
142 1. / max_interaction_length};
143 for (std::size_t i = 0; i < number_of_cells_.size(); ++i) {
144 number_of_cells_[i] =
145 (strategy == CellSizeStrategy::Largest)
147 : static_cast<int>(std::floor(length_[i] * index_factor[i])) +
158 (O == GridOptions::Normal ? 1 : 0);
159 if (number_of_cells_[i] == 0) {
160 throw std::runtime_error(
161 "Input error: Your Box is too small for the grid."
162 "\nThe minimal length of the box is given by:\n" +
163 std::to_string(max_interaction_length) +
164 " fm with your current timestep size dt.\n"
165 "If you have large timesteps please reduce them."
166 "\nPlease take a look at your config.");
170 if (number_of_cells_[i] > max_cells) {
171 number_of_cells_[i] = max_cells;
172 index_factor[i] = number_of_cells_[i] / length_[i];
173 while (index_factor[i] * length_[i] >= number_of_cells_[i]) {
174 index_factor[i] = std::nextafter(index_factor[i], 0.);
176 assert(index_factor[i] * length_[i] < number_of_cells_[i]);
177 }
else if (O == GridOptions::PeriodicBoundaries) {
178 if (number_of_cells_[i] == 1) {
179 number_of_cells_[i] = 2;
181 index_factor[i] = number_of_cells_[i] / length_[i];
182 while (index_factor[i] * length_[i] >= number_of_cells_[i]) {
183 index_factor[i] = std::nextafter(index_factor[i], 0.);
185 assert(index_factor[i] * length_[i] < number_of_cells_[i]);
189 cell_volume_ = (length_[0] / number_of_cells_[0]) *
190 (length_[1] / number_of_cells_[1]) *
191 (length_[2] / number_of_cells_[2]);
193 if (O == GridOptions::Normal &&
202 "There would only be ", number_of_cells_,
203 " cells. Therefore the Grid falls back to a single cell / "
205 number_of_cells_ = {1, 1, 1};
206 cell_volume_ = length_[0] * length_[1] * length_[2];
208 cells_.front().reserve(particles.
size());
209 std::copy_if(particles.
begin(), particles.
end(),
210 std::back_inserter(cells_.front()),
212 return p.xsec_scaling_factor(timestep_duration) > 0.0;
216 logg[
LGrid].debug(
"min: ", min_position,
"\nlength: ", length_,
217 "\ncell_volume: ", cell_volume_,
218 "\ncells: ", number_of_cells_,
219 "\nindex_factor: ", index_factor);
223 cells_.resize(number_of_cells_[0] * number_of_cells_[1] *
224 number_of_cells_[2]);
232 std::floor((
p.position()[1] - min_position[0]) * index_factor[0]),
233 std::floor((
p.position()[2] - min_position[1]) * index_factor[1]),
234 std::floor((
p.position()[3] - min_position[2]) * index_factor[2]));
237 for (
const auto &
p : particles) {
238 if (
p.xsec_scaling_factor(timestep_duration) > 0.0) {
239 const auto idx = cell_index_for(
p);
241 if (idx >=
SizeType(cells_.size())) {
244 "\nan out-of-bounds access would be necessary for the "
246 p,
"\nfor a grid with the following parameters:\nmin: ",
247 min_position,
"\nlength: ", length_,
248 "\ncells: ", number_of_cells_,
"\nindex_factor: ", index_factor,
249 "\ncells_.size: ", cells_.size(),
"\nrequested index: ", idx);
250 throw std::runtime_error(
"out-of-bounds grid access on construction");
253 cells_[idx].push_back(
p);
261 template <Gr
idOptions Options>
264 return (z * number_of_cells_[1] + y) * number_of_cells_[0] + x;
267 static const std::initializer_list<GridBase::SizeType>
ZERO{0};
268 static const std::initializer_list<GridBase::SizeType>
ZERO_ONE{0, 1};
276 const std::function<
void(
const ParticleList &)> &search_cell_callback,
277 const std::function<
void(
const ParticleList &,
const ParticleList &)>
278 &neighbor_cell_callback)
const {
279 std::array<SizeType, 3> search_index;
284 for (z = 0; z < number_of_cells_[2]; ++z) {
285 for (y = 0; y < number_of_cells_[1]; ++y) {
286 for (x = 0; x < number_of_cells_[0]; ++x, ++search_cell_index) {
287 assert(search_cell_index == make_index(search_index));
288 assert(search_cell_index >= 0);
289 assert(search_cell_index <
SizeType(cells_.size()));
290 const ParticleList &search = cells_[search_cell_index];
291 search_cell_callback(search);
293 const auto &dz_list = z == number_of_cells_[2] - 1 ?
ZERO :
ZERO_ONE;
294 const auto &dy_list = number_of_cells_[1] == 1
297 : y == number_of_cells_[1] - 1
300 const auto &dx_list = number_of_cells_[0] == 1
303 : x == number_of_cells_[0] - 1
309 const auto di = make_index(dx, dy, dz);
311 neighbor_cell_callback(search, cells_[search_cell_index + di]);
342 const std::function<
void(
const ParticleList &)> &search_cell_callback,
343 const std::function<
void(
const ParticleList &,
const ParticleList &)>
344 &neighbor_cell_callback)
const {
345 std::array<SizeType, 3> search_index;
352 std::array<NeighborLookup, 2> dz_list;
353 std::array<NeighborLookup, 3> dy_list;
354 std::array<NeighborLookup, 3> dx_list;
356 assert(number_of_cells_[2] >= 2);
357 assert(number_of_cells_[1] >= 2);
358 assert(number_of_cells_[0] >= 2);
360 for (z = 0; z < number_of_cells_[2]; ++z) {
361 dz_list[0].index = z;
362 dz_list[1].index = z + 1;
363 if (dz_list[1].index == number_of_cells_[2]) {
364 dz_list[1].index = 0;
366 dz_list[1].wrap = NeedsToWrap::MinusLength;
368 for (y = 0; y < number_of_cells_[1]; ++y) {
369 dy_list[0].index = y;
370 dy_list[1].index = y - 1;
371 dy_list[2].index = y + 1;
372 dy_list[2].wrap = NeedsToWrap::No;
374 dy_list[1] = dy_list[2];
375 dy_list[2].index = number_of_cells_[1] - 1;
376 dy_list[2].wrap = NeedsToWrap::PlusLength;
377 }
else if (dy_list[2].index == number_of_cells_[1]) {
378 dy_list[2].index = 0;
379 dy_list[2].wrap = NeedsToWrap::MinusLength;
381 for (x = 0; x < number_of_cells_[0]; ++x, ++search_cell_index) {
382 dx_list[0].index = x;
383 dx_list[1].index = x - 1;
384 dx_list[2].index = x + 1;
385 dx_list[2].wrap = NeedsToWrap::No;
387 dx_list[1] = dx_list[2];
388 dx_list[2].index = number_of_cells_[0] - 1;
389 dx_list[2].wrap = NeedsToWrap::PlusLength;
390 }
else if (dx_list[2].index == number_of_cells_[0]) {
391 dx_list[2].index = 0;
392 dx_list[2].wrap = NeedsToWrap::MinusLength;
395 assert(search_cell_index == make_index(search_index));
396 assert(search_cell_index >= 0);
397 assert(search_cell_index <
SizeType(cells_.size()));
398 ParticleList search = cells_[search_cell_index];
399 search_cell_callback(search);
401 auto virtual_search_index = search_index;
403 auto current_wrap_vector = wrap_vector;
405 for (
const auto &dz : dz_list) {
406 if (dz.wrap == NeedsToWrap::MinusLength) {
408 wrap_vector[2] = -length_[2];
409 virtual_search_index[2] = -1;
411 for (
const auto &dy : dy_list) {
413 if (dy.wrap == NeedsToWrap::MinusLength) {
414 wrap_vector[1] = -length_[1];
415 virtual_search_index[1] = -1;
416 }
else if (dy.wrap == NeedsToWrap::PlusLength) {
417 wrap_vector[1] = length_[1];
418 virtual_search_index[1] = number_of_cells_[1];
420 for (
const auto &dx : dx_list) {
422 if (dx.wrap == NeedsToWrap::MinusLength) {
423 wrap_vector[0] = -length_[0];
424 virtual_search_index[0] = -1;
425 }
else if (dx.wrap == NeedsToWrap::PlusLength) {
426 wrap_vector[0] = length_[0];
427 virtual_search_index[0] = number_of_cells_[0];
429 assert(dx.index >= 0);
430 assert(dx.index < number_of_cells_[0]);
431 assert(dy.index >= 0);
432 assert(dy.index < number_of_cells_[1]);
433 assert(dz.index >= 0);
434 assert(dz.index < number_of_cells_[2]);
435 const auto neighbor_cell_index =
436 make_index(dx.index, dy.index, dz.index);
437 assert(neighbor_cell_index >= 0);
438 assert(neighbor_cell_index <
SizeType(cells_.size()));
439 if (neighbor_cell_index <= make_index(virtual_search_index)) {
443 if (wrap_vector != current_wrap_vector) {
444 logg[
LGrid].debug(
"translating search cell by ",
445 wrap_vector - current_wrap_vector);
447 p =
p.translated(wrap_vector - current_wrap_vector);
449 current_wrap_vector = wrap_vector;
451 neighbor_cell_callback(search, cells_[neighbor_cell_index]);
453 virtual_search_index[0] = search_index[0];
456 virtual_search_index[1] = search_index[1];
465 const std::pair<std::array<double, 3>, std::array<double, 3>>
467 const Particles &particles,
double max_interaction_length,
470 const std::pair<std::array<double, 3>, std::array<double, 3>>
472 const Particles &particles,
double max_interaction_length,