1#ifndef PARTICLEGRIDINTERPOLATION_HEADER
2#define PARTICLEGRIDINTERPOLATION_HEADER
64 namespace INTERPOLATION {
84 template <
int N,
int ORDER,
class T>
88 std::vector<FloatType> & interpolated_values);
103 template <
int N,
int ORDER,
class T>
108 std::array<std::vector<FloatType>, N> & interpolated_values_vec);
122 template <
int N,
class T>
126 std::vector<FloatType> & interpolated_values,
141 template <
int N,
class T>
146 std::array<std::vector<FloatType>, N> & interpolated_values_vec,
154 template <
int N,
int ORDER,
class T,
class Apply>
160 template <
int N,
class T,
class Apply>
179 template <
int N,
class T>
184 std::string density_assignment_method);
199 template <
int N,
int ORDER,
class T>
209 template <
int N,
class T>
214 std::string density_assignment_method,
215 const std::array<double, N> & assignment_offset,
216 bool wrap_x =
false);
218 template <
int N,
int ORDER,
class T>
223 const std::array<double, N> & assignment_offset,
224 bool wrap_x =
false);
227 template <
int N,
int ORDER,
class T,
bool SHIFTED>
228 void particles_to_grid_impl(
const T * part,
232 const std::array<double, N> & assignment_offset,
237 template <
int N,
class T>
242 std::string density_assignment_method);
259 template <
int N,
class T>
264 std::string density_assignment_method,
279 template <
int N,
int ORDER>
282 const std::function<
FloatType(std::array<double, N> &)> & convolution_kernel);
289 return method ==
"NGP" or method ==
"CIC" or method ==
"TSC" or method ==
"PCS" or method ==
"PQS";
292 template <
int N,
class T>
297 std::string density_assignment_method) {
299 (
"[particles_to_grid] Unknown density assignment method [" + density_assignment_method +
300 "] (NGP, CIC, TSC, PCS or PQS)\n")
302 if (density_assignment_method.compare(
"NGP") == 0)
304 if (density_assignment_method.compare(
"CIC") == 0)
306 if (density_assignment_method.compare(
"TSC") == 0)
308 if (density_assignment_method.compare(
"PCS") == 0)
310 if (density_assignment_method.compare(
"PQS") == 0)
314 template <
int N,
class T>
319 std::string density_assignment_method,
320 const std::array<double, N> & assignment_offset,
323 (
"[particles_to_grid] Unknown density assignment method [" + density_assignment_method +
324 "] (NGP, CIC, TSC, PCS or PQS)\n")
326 if (density_assignment_method.compare(
"NGP") == 0)
328 if (density_assignment_method.compare(
"CIC") == 0)
330 if (density_assignment_method.compare(
"TSC") == 0)
332 if (density_assignment_method.compare(
"PCS") == 0)
334 if (density_assignment_method.compare(
"PQS") == 0)
338 template <
int N,
class T>
342 std::vector<FloatType> & interpolated_values,
345 (
"[interpolate_grid_to_particle_positions] Unknown interpolation method [" +
363 if (density_assignment_method.compare(
"NGP") == 0)
365 if (density_assignment_method.compare(
"CIC") == 0)
367 if (density_assignment_method.compare(
"TSC") == 0)
369 if (density_assignment_method.compare(
"PCS") == 0)
371 if (density_assignment_method.compare(
"PQS") == 0)
373 assert_mpi(
false,
"[interpolation_order_from_name] Unknown density assignment method\n");
388 inline std::pair<int, int>
391 if (density_assignment_method.compare(
"NGP") == 0)
393 if (density_assignment_method.compare(
"CIC") == 0)
395 if (density_assignment_method.compare(
"TSC") == 0)
397 if (density_assignment_method.compare(
"PCS") == 0)
399 if (density_assignment_method.compare(
"PQS") == 0)
401 assert_mpi(p > 0,
"[extra_slices_needed_density_assignment] Unknown density assignment method\n");
404#ifdef CELLCENTERSHIFTED
406 return {p / 2, p / 2};
408 return {p / 2, p / 2};
411 return {p / 2, p / 2 + 1};
413 return {p / 2 - 1, p / 2};
426#ifdef CELLCENTERSHIFTED
428 return {ORDER / 2, ORDER / 2};
430 return {ORDER / 2, ORDER / 2};
433 return {ORDER / 2, ORDER / 2 + 1};
435 return {ORDER / 2 - 1, ORDER / 2};
445 inline double kernel([[maybe_unused]]
double x) {
446 static_assert(ORDER > 0 and ORDER <= 5,
"Error: kernel order is not implemented\n");
452 return (x <= 0.5) ? 1.0 : 0.0;
457 return (x < 1.0) ? 1.0 - x : 0.0;
462 return (x < 0.5) ? 0.75 - x * x : (x < 1.5 ? 0.5 * (1.5 - x) * (1.5 - x) : 0.0);
467 return (x < 1.0) ? 2.0 / 3.0 + x * x * (-1.0 + 0.5 * x) :
468 ((x < 2.0) ? (2 - x) * (2 - x) * (2 - x) / 6.0 : 0.0);
474 115.0 / 192.0 + 0.25 * x * x * (x * x - 2.5) :
476 (55 + 4 * x * (5 - 2 * x * (15 + 2 * (-5 + x) * x))) / 96.0 :
477 ((x < 2.5) ? (5 - 2.0 * x) * (5 - 2.0 * x) * (5 - 2.0 * x) * (5 - 2.0 * x) / 384. : 0.0));
495 assert_mpi(Ngrid > 0,
"[get_window_function_fourier] Ngrid must be positive\n");
501 const double knyquist = M_PI * Ngrid;
502 std::function<double(std::array<double, N> &)> window_function = [=](std::array<double, N> & kvec) {
504 for (
int idim = 0; idim < N; idim++) {
505 const double koverkny = M_PI / 2. * (kvec[idim] / knyquist);
506 w *= koverkny == 0.0 ? 1.0 : std::sin(koverkny) / (koverkny);
510 for (
int i = 0; i < p; i++)
515 return window_function;
527 assert_mpi(Ngrid > 0,
"[WindowFunctionTable] Ngrid must be positive\n");
528 const double twopi = 2.0 * M_PI;
529 const double knyquist = M_PI * Ngrid;
530 for (
int n = -Ngrid / 2; n <= Ngrid / 2; n++) {
531 const double k = twopi * double(n);
532 const double koverkny = M_PI / 2. * (k / knyquist);
533 table[n + Ngrid / 2] = koverkny == 0.0 ? 1.0 : std::sin(koverkny) / (koverkny);
536 double operator()(
const std::array<double, N> & kvec)
const {
538 for (
int idim = 0; idim < N; idim++) {
540 const double n = kvec[idim] * (0.5 / M_PI);
541 w *= table[int(n + (n >= 0.0 ? 0.5 : -0.5)) + Ngrid / 2];
544 for (
int i = 0; i < p; i++)
552 std::vector<double> table;
565 const auto Ngrid = fourier_grid.
get_nmesh();
568 assert_mpi(Ngrid > 0,
"[deconvolve_window_function_fourier] Ngrid must be positive\n");
573#pragma omp parallel for
575 for (
int islice = 0; islice < Local_nx; islice++) {
576 for (
auto && fourier_index : fourier_grid.
get_fourier_range(islice, islice + 1)) {
578 auto w = window_function(kvec);
598 template <
int N,
int ORDER,
class T>
600 detail::particles_to_grid_impl<N, ORDER, T, false>(part, NumPart, NumPartTot, density, {},
false);
603 template <
int N,
int ORDER,
class T>
608 const std::array<double, N> & assignment_offset,
610 assert_mpi(not wrap_x or
FML::NTasks == 1,
"[particles_to_grid] Wrapped x offsets require one task");
611 const int Nmesh = density.get_nmesh();
612 assert_mpi(
Nmesh > 0,
"[particles_to_grid] Offset deposition needs an allocated grid");
613 bool shifted =
false;
614 for (
double offset : assignment_offset) {
615 assert_mpi(std::isfinite(offset) and offset >= 0.0 and offset < 1.0 /
Nmesh,
616 "[particles_to_grid] Assignment offsets must be in [0, 1/Nmesh)");
617 shifted = shifted or offset != 0.0;
620 assert_mpi(
FML::NTasks == 1 or assignment_offset[0] == 0.0 or
621 density.get_n_extra_slices_right() >= nextra.second + 1,
622 "[particles_to_grid] Positive x offset needs one additional right slice");
624 detail::particles_to_grid_impl<N, ORDER, T, true>(part,
636 template <
class Position>
637 inline Position assignment_coordinate(Position coordinate,
double offset,
int dimension,
bool wrap_x) {
638 coordinate += offset;
639 if (dimension > 0 or wrap_x)
645 template <
int N,
int ORDER,
class T,
bool SHIFTED>
646 void detail::particles_to_grid_impl(
const T * part,
650 const std::array<double, N> & assignment_offset,
654 assert_mpi(
density.get_n_extra_slices_left() >= nextra.first and
655 density.get_n_extra_slices_right() >= nextra.second,
656 "[particles_to_grid] Too few extra slices\n");
666 constexpr int widthtondim =
FML::power(ORDER, N);
669 const auto Local_nx =
density.get_local_nx();
670 const auto Local_x_start =
density.get_local_x_start();
674 const int spare_right =
density.get_n_extra_slices_right() - nextra.second;
675 assert_mpi(
FML::NTasks == 1 or Local_nx >= std::max(nextra.first, nextra.second + std::max(0, spare_right)),
676 "[particles_to_grid] Fewer slices on a task than extra slices needed by the kernel: the "
677 "contributions to the extra slices would reach beyond the neighbor task (use fewer tasks or a "
681 constexpr bool has_deltaf_weight = FML::PARTICLE::has_get_deltaf_weight<T>();
684 density.fill_real_grid(has_deltaf_weight ? 0.0 : -1.0);
687 double norm_fac = std::pow((
double)
Nmesh, N) / double(NumPartTot);
691 constexpr bool has_mass = FML::PARTICLE::has_get_mass<T>() and not has_deltaf_weight;
692 static_assert(not(
FML::PARTICLE::has_get_mass<T>() and has_deltaf_weight),
693 "[
particles_to_grid] A particle cannot have both get_mass and get_deltaf_weight");
694 if constexpr (has_mass) {
695 double mean_mass = 0.0;
696 for (
size_t i = 0;
i < NumPart;
i++) {
700 mean_mass /= double(NumPartTot);
701 norm_fac /= mean_mass;
706 auto * density_raw =
density.get_real_grid();
707 auto add_particle = [&](
size_t i,
auto atomic_tag) {
708 constexpr bool ATOMIC =
decltype(atomic_tag)
::value;
715 if constexpr (has_mass)
717 if constexpr (has_deltaf_weight)
718 mass = FML::PARTICLE::GetDeltafWeight(part[i]);
720 std::array<double, N>
x;
721 std::array<int, N> ix;
722 [[maybe_unused]] std::array<int, N> ix_nbor;
723 for (
int idim = 0; idim <
N; idim++) {
725 if constexpr (SHIFTED)
726 x[idim] = assignment_coordinate(pos[idim], assignment_offset[idim], idim, wrap_x) *
Nmesh;
730 ix[idim] = (
int)x[idim];
736 ix[0] -=
int(Local_x_start);
739 if constexpr (
ORDER == 1)
749 }
else if (ix[0] >
int(Local_nx) - 1 + spare_right) {
750 x[0] += ix[0] - (
int(Local_nx) - 1 + spare_right);
751 ix[0] =
int(Local_nx) - 1 + spare_right;
754 for (
int idim = 1; idim <
N; idim++) {
755 if (ix[idim] ==
Nmesh)
761 std::array<int, N> xstart;
762 if (ORDER % 2 == 0) {
763 for (
int idim = 0; idim <
N; idim++) {
764 xstart[idim] = -
ORDER / 2 + 1;
765#ifdef CELLCENTERSHIFTED
766 xstart[idim] = -
ORDER / 2;
772#ifndef CELLCENTERSHIFTED
773 for (
int idim = 0; idim <
N; idim++) {
774 xstart[idim] = -
ORDER / 2;
780 for (
int idim = 0; idim <
N; idim++)
781 xstart[idim] = -ORDER / 2;
787 [[maybe_unused]] std::array<std::array<double, ORDER>,
N> w1d;
788 [[maybe_unused]] std::array<std::array<int, ORDER>,
N> i1d;
789 if constexpr (
ORDER > 1) {
790 for (
int idim = 0; idim <
N; idim++) {
791 for (
int j = 0; j <
ORDER; j++) {
792 const int go_left_right_or_stay = xstart[idim] + j;
793#ifdef CELLCENTERSHIFTED
794 const double dx = std::fabs(-x[idim] + go_left_right_or_stay + 0.5);
796 const double dx = std::fabs(-x[idim] + go_left_right_or_stay);
802 int icoord = ix[idim] + go_left_right_or_stay;
809 i1d[idim][j] = icoord;
815 [[maybe_unused]]
double sumweights = 0.0;
816 for (
int i = 0;
i < widthtondim;
i++) {
818 std::array<int, N> icoord;
819 if constexpr (
ORDER == 1) {
822 for (
int idim = 0, n = 1; idim <
N; idim++,
n *=
ORDER) {
825 icoord[idim] = i1d[idim][j];
832 double mass_to_add =
w * norm_fac * mass;
833 if constexpr (ATOMIC) {
837 density_raw[
index] += mass_to_add;
839 density_raw[
index] += mass_to_add;
849 std::fabs(sumweights - 1.0) < 1e-3,
850 "[particles_to_grid] Possible problem with particles to grid: weights does not sum to unity!");
868 const int nthreads = omp_get_max_threads();
870 const int nthreads = 1;
873 for (
size_t i = 0;
i < NumPart;
i++)
874 add_particle(i, std::false_type{});
876 const int Local_nx =
density.get_local_nx();
877 int nchunks = Local_nx / (2 *
ORDER + 2);
880 const bool chunked = nchunks >= 4 and NumPart < size_t(std::numeric_limits<uint32_t>::max());
883#pragma omp parallel for
885 for (
size_t i = 0;
i < NumPart;
i++)
886 add_particle(i, std::true_type{});
889 auto chunk_of = [&](
size_t i) {
891 auto coordinate =
pos[0];
892 if constexpr (SHIFTED)
893 coordinate = assignment_coordinate(coordinate, assignment_offset[0], 0, wrap_x);
894 int ix0 =
int(coordinate *
Nmesh) -
int(Local_x_start);
895 ix0 = std::max(0, std::min(ix0, Local_nx - 1));
896 return int((
long long)(ix0)*nchunks / Local_nx);
899 std::vector<size_t> start(nchunks + 1, 0);
900 std::vector<uint32_t> chunk(NumPart);
902#pragma omp parallel for
904 for (
size_t i = 0;
i < NumPart;
i++)
905 chunk[i] = uint32_t(chunk_of(i));
906 for (
size_t i = 0;
i < NumPart;
i++)
907 start[chunk[i] + 1]++;
908 for (
int c = 0; c < nchunks; c++)
909 start[c + 1] += start[c];
910 std::vector<uint32_t>
order(NumPart);
912 std::vector<size_t> next(start.begin(), start.end() - 1);
913 for (
size_t i = 0;
i < NumPart;
i++)
914 order[next[chunk[i]]++] = uint32_t(i);
916 std::vector<uint32_t>().swap(chunk);
917 for (
int phase = 0; phase < 2; phase++) {
919#pragma omp parallel for schedule(dynamic, 1)
921 for (
int c = phase; c < nchunks; c += 2)
922 for (
size_t k = start[c];
k < start[c + 1];
k++)
923 add_particle(order[k], std::false_type{});
933 template <
int N,
int ORDER,
class T>
938 std::array<std::vector<FloatType>, N> & interpolated_values_vec) {
939 for (
auto & values : interpolated_values_vec)
940 if (values.size() < NumPart)
941 values.resize(NumPart);
942 auto store = [&](
size_t index,
const std::array<FloatType, N> &
value) {
943 for (
int dimension = 0; dimension < N; dimension++)
944 interpolated_values_vec[dimension][index] =
value[dimension];
950 template <
int N,
int ORDER,
class T,
class Apply>
957 assert_mpi(grid_vec.size() > 0,
958 "[interpolate_grid_to_particle_positions] Grid vector has to be already allocated!\n");
959 for (
auto & g : grid_vec) {
960 assert_mpi(g.get_nmesh() > 0,
961 "[interpolate_grid_to_particle_positions] All grids has to be already allocated!\n");
962 assert_mpi(g.get_nmesh() == grid_vec[0].get_nmesh(),
963 "[interpolate_grid_to_particle_positions] All grids has to have the same size!\n");
965 for (
auto & g : grid_vec) {
966 assert_mpi(g.get_n_extra_slices_left() >= nextra.first and
967 g.get_n_extra_slices_right() >= nextra.second,
968 "[interpolate_grid_to_particle_positions] Too few extra slices in some of the grids\n");
972 constexpr int widthtondim =
FML::power(ORDER, N);
975 const auto Local_nx = grid_vec[0].get_local_nx();
976 const auto Local_x_start = grid_vec[0].get_local_x_start();
977 const int Nmesh = grid_vec[0].get_nmesh();
978 const int spare_right = std::max(0, grid_vec[0].get_n_extra_slices_right() - nextra.second);
979 assert_mpi(
FML::NTasks == 1 or Local_nx >= std::max(nextra.first, nextra.second + spare_right),
980 "[interpolate_grid_to_particle_positions] Fewer slices on a task than extra slices needed by "
981 "the kernel: the extra slices cannot be filled from the neighbor task (use fewer tasks or a "
984 double max_result = 0.0;
987#pragma omp parallel for reduction(max : max_result)
989 for (
size_t ind = 0; ind < NumPart; ind++) {
993 std::array<double, N> x;
994 for (
int idim = 0; idim < N; idim++)
995 x[idim] = pos[idim] *
Nmesh;
999 std::array<int, N> ix;
1000 for (
int idim = 0; idim < N; idim++) {
1001 ix[idim] = int(x[idim]);
1005 if (ix[0] >= (Local_x_start + Local_nx + spare_right))
1006 ix[0] = int(Local_x_start + Local_nx + spare_right) - 1;
1007 if (ix[0] < Local_x_start)
1008 ix[0] = int(Local_x_start);
1010 if (ix[idim] ==
Nmesh)
1011 ix[idim] =
Nmesh - 1;
1016 for (
int idim = 0; idim < N; idim++) {
1017 x[idim] -= ix[idim];
1021 ix[0] -= int(Local_x_start);
1025 std::array<int, N> xstart;
1026 if (ORDER % 2 == 0) {
1027 for (
int idim = 0; idim < N; idim++) {
1028 xstart[idim] = -ORDER / 2 + 1;
1029#ifdef CELLCENTERSHIFTED
1030 xstart[idim] = -ORDER / 2;
1036#ifndef CELLCENTERSHIFTED
1037 for (
int idim = 0; idim < N; idim++) {
1038 xstart[idim] = -ORDER / 2;
1044 for (
int idim = 0; idim < N; idim++)
1045 xstart[idim] = -ORDER / 2;
1051 std::array<std::array<double, ORDER>, N> w1d;
1052 std::array<std::array<int, ORDER>, N> i1d;
1053 for (
int idim = 0; idim < N; idim++) {
1054 for (
int j = 0; j < ORDER; j++) {
1055 const int go_left_right_or_stay = ORDER == 1 ? 0 : xstart[idim] + j;
1056#ifdef CELLCENTERSHIFTED
1057 const double dx = std::fabs(-x[idim] + go_left_right_or_stay + 0.5);
1059 const double dx = std::fabs(-x[idim] + go_left_right_or_stay);
1064 int icoord = ix[idim] + go_left_right_or_stay;
1066 if (icoord >=
Nmesh)
1071 i1d[idim][j] = icoord;
1076 std::array<double, N>
value;
1078 [[maybe_unused]]
double sumweight = 0;
1079 for (
int i = 0; i < widthtondim; i++) {
1081 std::array<int, N> icoord;
1082 for (
int idim = 0, n = 1; idim < N; idim++, n *= ORDER) {
1083 const int j = i / n % ORDER;
1085 icoord[idim] = i1d[idim][j];
1089 for (
int idim = 0; idim < N; idim++)
1090 value[idim] += grid_vec[idim].get_real(icoord) * w;
1094#ifdef DEBUG_INTERPOL
1096 assert_mpi(std::fabs(sumweight - 1.0) < 1e-3,
1097 "[interpolate_grid_to_particle_positions] Possible problem with interpolation: weights does "
1098 "not sum to unity!");
1102 std::array<FloatType, N> interpolated_value;
1103 for (
int idim = 0; idim < N; idim++)
1104 interpolated_value[idim] =
value[idim];
1105 max_result = std::max(max_result, apply(ind, interpolated_value));
1110 template <
int N,
class T>
1114 std::array<std::vector<FloatType>, N> & interpolated_values,
1117 (
"[interpolate_grid_vector_to_particle_positions] Unknown interpolation method [" +
1132 template <
int N,
class T,
class Apply>
1139 (
"[interpolate_grid_vector_to_particle_positions_apply] Unknown interpolation method [" +
1153 template <
int N,
int ORDER,
class T>
1157 std::vector<FloatType> & interpolated_values) {
1160 assert_mpi(
grid.get_nmesh() > 0,
1161 "[interpolate_grid_to_particle_positions] Grid has to be already allocated!\n");
1162 assert_mpi(
grid.get_n_extra_slices_left() >= nextra.first and
1163 grid.get_n_extra_slices_right() >= nextra.second,
1164 "[interpolate_grid_to_particle_positions] Too few extra slices\n");
1167 constexpr int widthtondim =
FML::power(ORDER, N);
1170 const auto Local_nx =
grid.get_local_nx();
1171 const auto Local_x_start =
grid.get_local_x_start();
1173 const int spare_right = std::max(0,
grid.get_n_extra_slices_right() - nextra.second);
1174 assert_mpi(
FML::NTasks == 1 or Local_nx >= std::max(nextra.first, nextra.second + spare_right),
1175 "[interpolate_grid_to_particle_positions] Fewer slices on a task than extra slices needed by "
1176 "the kernel: the extra slices cannot be filled from the neighbor task (use fewer tasks or a "
1180 interpolated_values.resize(NumPart);
1183#pragma omp parallel for
1185 for (
size_t ind = 0; ind < NumPart; ind++) {
1189 std::array<double, N> x;
1190 for (
int idim = 0; idim < N; idim++)
1191 x[idim] = pos[idim] *
Nmesh;
1195 std::array<int, N> ix;
1196 [[maybe_unused]] std::array<int, N> ix_nbor;
1197 for (
int idim = 0; idim < N; idim++) {
1198 ix[idim] = int(x[idim]);
1202 if (ix[0] >= (Local_x_start + Local_nx + spare_right))
1203 ix[0] = int(Local_x_start + Local_nx + spare_right) - 1;
1204 if (ix[0] < Local_x_start)
1205 ix[0] = int(Local_x_start);
1207 if (ix[idim] ==
Nmesh)
1208 ix[idim] =
Nmesh - 1;
1213 for (
int idim = 0; idim < N; idim++) {
1214 x[idim] -= ix[idim];
1218 ix[0] -= int(Local_x_start);
1222 std::array<int, N> xstart;
1223 if (ORDER % 2 == 0) {
1224 for (
int idim = 0; idim < N; idim++) {
1225 xstart[idim] = -ORDER / 2 + 1;
1226#ifdef CELLCENTERSHIFTED
1227 xstart[idim] = -ORDER / 2;
1233#ifndef CELLCENTERSHIFTED
1234 for (
int idim = 0; idim < N; idim++) {
1235 xstart[idim] = -ORDER / 2;
1241 for (
int idim = 0; idim < N; idim++)
1242 xstart[idim] = -ORDER / 2;
1248 [[maybe_unused]]
double sumweight = 0;
1249 for (
int i = 0; i < widthtondim; i++) {
1251 std::array<int, N> icoord;
1252 if constexpr (ORDER == 1) {
1255 for (
int idim = 0, n = 1; idim < N; idim++, n *= ORDER) {
1256 int go_left_right_or_stay = xstart[idim] + (i / n % ORDER);
1257 ix_nbor[idim] = ix[idim] + go_left_right_or_stay;
1258#ifdef CELLCENTERSHIFTED
1259 double dx = std::fabs(-x[idim] + go_left_right_or_stay + 0.5);
1261 double dx = std::fabs(-x[idim] + go_left_right_or_stay);
1267 icoord[0] = ix_nbor[0];
1268 for (
int idim = 1; idim < N; idim++) {
1269 icoord[idim] = ix_nbor[idim];
1270 if (icoord[idim] >=
Nmesh)
1271 icoord[idim] -=
Nmesh;
1272 if (icoord[idim] < 0)
1273 icoord[idim] +=
Nmesh;
1282#ifdef DEBUG_INTERPOL
1284 assert_mpi(std::fabs(sumweight - 1.0) < 1e-3,
1285 "[interpolate_grid_to_particle_positions] Possible problem with interpolation: weights does "
1286 "not sum to unity!");
1290 interpolated_values[ind] =
value;
1302 auto Local_nx = density.get_local_nx();
1303 auto num_cells_slice = density.get_ntot_real_slice_alloc();
1304 int n_extra_left = density.get_n_extra_slices_left();
1305 int n_extra_right = density.get_n_extra_slices_right();
1311 std::vector<FloatType> buffer(num_cells_slice);
1314 for (
int i = 0; i < n_extra_right; i++) {
1315 FloatType * extra_slice_right = density.get_real_grid_right() + num_cells_slice * i;
1316 FloatType * slice_left = density.get_real_grid() + num_cells_slice * i;
1325 MPI_Sendrecv(&(extra_slice_right[0]),
1338 temp = extra_slice_right;
1342 for (
int j = 0; j < num_cells_slice; j++) {
1343 slice_left[j] += (temp[j] - background);
1348 for (
int i = 1; i <= n_extra_left; i++) {
1349 FloatType * extra_slice_left = density.get_real_grid() - i * num_cells_slice;
1350 FloatType * slice_right = density.get_real_grid() + num_cells_slice * (Local_nx - i);
1359 MPI_Sendrecv(&(extra_slice_left[0]),
1372 temp = extra_slice_left;
1376 for (
int j = 0; j < num_cells_slice; j++) {
1377 slice_right[j] += (temp[j] - background);
1396 template <
int N,
int ORDER>
1399 const std::function<
FloatType(std::array<double, N> &)> & convolution_kernel) {
1404 "[convolve_grid_with_kernel] Too few extra slices\n");
1405 assert_mpi(grid_in.
get_nmesh() > 0,
"[convolve_grid_with_kernel] Grid has to be already allocated!\n");
1408 constexpr int widthtondim =
FML::power(ORDER, N);
1409 std::array<int, N> xstart;
1411 xstart.fill(-ORDER / 2 + 1);
1413 xstart.fill(-ORDER / 2);
1425#pragma omp parallel for
1427 for (
int islice = 0; islice < Local_nx; islice++) {
1428 std::array<double, N> dx;
1429 for (
auto && real_index : grid_in.
get_real_range(islice, islice + 1)) {
1432 auto ix = grid_in.coord_from_index(real_index);
1435 [[maybe_unused]] std::array<int, N> ix_nbor;
1437 for (
int idim = 1; idim < N; idim++) {
1438 ix_nbor[idim] = ix[idim] + 1;
1439 if (ix_nbor[idim] >=
Nmesh)
1440 ix_nbor[idim] -=
Nmesh;
1445 for (
int i = 0; i < widthtondim; i++) {
1446 std::array<int, N> icoord;
1448 if constexpr (ORDER == 1) {
1451 for (
int idim = 0, n = 1; idim < N; idim++, n *= ORDER) {
1452 int go_left_right_or_stay = xstart[idim] + (i / n % ORDER);
1453 ix_nbor[idim] = ix[idim] + go_left_right_or_stay;
1454 dx[idim] = go_left_right_or_stay;
1456 w = convolution_kernel(dx);
1459 icoord[0] = ix_nbor[0];
1460 for (
int idim = 1; idim < N; idim++) {
1461 icoord[idim] = ix_nbor[idim];
1462 if (icoord[idim] >=
Nmesh)
1463 icoord[idim] -=
Nmesh;
1464 if (icoord[idim] < 0)
1465 icoord[idim] +=
Nmesh;
1479 template <
int N,
class T>
1484 std::string density_assignment_method) {
1486 auto Ngrid = density_grid_fourier.
get_nmesh();
1491 int nleft = nleftright.first;
1492 int nright = nleftright.second + 1;
1497 density_grid_fourier =
FFTWGrid<N>(Ngrid, nleft, nright);
1499 "FFTWGrid::particles_to_grid_interlacing::density_grid_fourier (reallocated)");
1506 const double shift = 1.0 / double(2 * Ngrid);
1507 std::array<double, N> assignment_offset;
1508 assignment_offset.fill(shift);
1509 FFTWGrid<N> density_grid_fourier2(Ngrid, nleft, nright);
1511 "FFTWGrid::particles_to_fourier_grid_interlacing::density_grid_fourier2");
1515 density_grid_fourier2,
1516 density_assignment_method,
1527#pragma omp parallel for
1529 for (
int islice = 0; islice < Local_nx; islice++) {
1530 const std::complex<FML::GRID::FloatType>
I(0, 1);
1531 for (
auto && fourier_index : density_grid_fourier.
get_fourier_range(islice, islice + 1)) {
1537 auto ksum = kvec[0];
1538 for (
int idim = 1; idim < N; idim++)
1540 auto norm = std::exp(
I * FML::GRID::FloatType(ksum * shift));
1544 (grid1 +
norm * grid2) / FML::GRID::FloatType(2.0));
1549 template <
int N,
class T>
1554 std::string density_assignment_method,
1561 density_grid_fourier,
1562 density_assignment_method);
1592 std::vector<std::array<double, NDIM>> shifts;
1595 if (interlacing_order <= 1) {
1596 shifts.push_back(std::array<double, NDIM>{});
1601 if constexpr (
NDIM == 1) {
1602 for (
int i = 0; i < interlacing_order; i++)
1603 shifts.push_back({i /
double(interlacing_order) * dx});
1608 if (interlacing_order == 2) {
1609 std::array<double, NDIM> shift{};
1610 shifts.push_back(shift);
1611 shift.fill(0.5 * dx);
1612 shifts.push_back(shift);
1619 std::array<double, NDIM> shift{};
1621 for (
int idim = 0; idim <
NDIM; idim++) {
1622 int bit = (
i >> idim) & 1;
1624 shift[idim] = 0.5 *
dx * bit;
1627 shifts.push_back(shift);
1633 const int n1d =
int(std::round(std::pow(
double(interlacing_order), 1.0 /
NDIM)));
1635 for (
int i = 0;
i < interlacing_order;
i++) {
1636 std::array<double, NDIM> shift{};
1637 for (
int idim = 0, n = 1; idim <
NDIM; idim++,
n *= n1d)
1638 shift[idim] = ((i / n) % n1d) / double(n1d) *
dx;
1639 shifts.push_back(shift);
1647 while ((1 << (log2order + 1)) <= interlacing_order)
1649 if ((1 << log2order) == interlacing_order and log2order >=
NDIM) {
1651 std::array<double, NDIM> shift{};
1652 for (
int idim = 0; idim <
NDIM; idim++)
1653 shift[idim] = 0.5 * dx * ((i >> idim) & 1);
1654 shifts.push_back(shift);
1656 for (
int level = 1; level <= log2order -
NDIM; level++) {
1657 const double extra =
dx / std::pow(2.0, level + 1);
1658 const size_t nold = shifts.size();
1659 for (
size_t j = 0; j < nold; j++) {
1660 auto shift = shifts[j];
1661 for (
int idim = 0; idim <
NDIM; idim++)
1662 shift[idim] += extra;
1663 shifts.push_back(shift);
1669 throw std::runtime_error(
"[generate_interlacing_shifts] Interlacing order " +
1670 std::to_string(interlacing_order) +
" is not supported in " +
1671 std::to_string(
NDIM) +
"D");
1691 template <
int N,
class T>
1696 std::string density_assignment_method,
1697 int interlacing_order) {
1699 const int Ngrid = density_grid_fourier.
get_nmesh();
1700 assert_mpi(Ngrid > 0,
"[particles_to_fourier_grid_interlaced] Grid must be allocated\n");
1704 const int nleft = nleftright.first;
1705 const int nright = nleftright.second + (interlacing_order > 1 ? 1 : 0);
1708 density_grid_fourier =
FFTWGrid<N>(Ngrid, nleft, nright);
1710 "FFTWGrid::particles_to_fourier_grid_interlaced::density_grid_fourier (reallocated)");
1713 if (interlacing_order <= 1) {
1714 particles_to_grid<N, T>(part, NumPart, NumPartTot, density_grid_fourier, density_assignment_method);
1719 const auto shifts = generate_interlacing_shifts<N>(interlacing_order, 1.0 /
double(Ngrid));
1723 grid_shifted.
add_memory_label(
"FFTWGrid::particles_to_fourier_grid_interlaced::grid_shifted");
1726 for (
auto & shift : shifts) {
1727 particles_to_grid<N, T>(part,
1731 density_assignment_method,
1739 const auto Local_nx = density_grid_fourier.
get_local_nx();
1741#pragma omp parallel for
1743 for (
int islice = 0; islice < Local_nx; islice++) {
1744 for (
auto && fourier_index : density_grid_fourier.
get_fourier_range(islice, islice + 1)) {
FML::GRID::FFTWGrid< N > FFTWGrid
const std::string interpolation_method
Class for holding grids and performing real-to-complex and complex-to-real FFTs using FFTW with MPI.
int get_nmesh() const
Number of grid-cells per dimension.
void add_memory_label(std::string label)
For memory logging: add a label to the grid.
ptrdiff_t get_local_x_start() const
The x-slice the first local slice has in the global grid.
FloatType get_real(const std::array< int, N > &coord) const
Fetch value in grid by (local) integer coordinate.
ptrdiff_t get_local_nx() const
Number of local x-slices in the real grid.
void set_fourier_from_index(const IndexIntType ind, const ComplexType value)
Set value of cell in fourier grid using (local) index.
ComplexType get_fourier_from_index(const IndexIntType index) const
Fetch value in fourier grid by (local) index.
std::array< double, N > get_fourier_wavevector_from_index(const IndexIntType index) const
Get the wave-vector of a grid-cell in Fourier space (for physical [k] multiply by 1/Boxsize)
FourierRange get_fourier_range(int islice_begin=0, int islice_end=0) const
Range iterator for going through all active cells in the main fourier grid by index [ e....
void fftw_r2c()
Perform real-to-complex fourier transform.
void set_grid_status_real(bool grid_is_a_real_grid)
Set the status of the grid: is it currently a real grid or a fourier grid?
void set_real(const std::array< int, N > &coord, const FloatType value)
Set value in grid from (local) integer coordinate in Local_nx x [0,Nmesh)^(Ndim-1)
int get_n_extra_slices_right() const
How many extra slices we have allocated to the right.
int get_n_extra_slices_left() const
How many extra slices we have allocated to the left.
void fill_fourier_grid(const ComplexType val)
Fill the whole fourier-grid with a constant value.
RealRange get_real_range(int islice_begin=0, int islice_end=0) const
Range iterator for going through all active cells in the main real real grid by index [ e....
The Fourier window of the density assignment, , from a table of the 1D factors (the wavevector compo...
WindowFunctionTable(std::string density_assignment_method, int Ngrid)
double operator()(const std::array< double, N > &kvec) const
size_t checked_bytes(Count count, Count extent, Count limit=std::numeric_limits< size_t >::max())
void shift_grid_fourier(FFTWGrid< N > &fourier_grid, const std::array< double, N > &shift, double sign=+1.0)
Translate a field given in Fourier space: , i.e.
This namespace deals with interpolation and density assignment.
double kernel(double x)
Internal method.
double kernel< 3 >(double x)
The TSC kernel.
void particles_to_fourier_grid_interlaced(const T *part, size_t NumPart, size_t NumPartTot, FFTWGrid< N > &density_grid_fourier, std::string density_assignment_method, int interlacing_order)
Assign particles to a grid with interlacing of any (supported) order and return the density contrast ...
void convolve_grid_with_kernel(const FFTWGrid< N > &grid_in, FFTWGrid< N > &grid_out, const std::function< FloatType(std::array< double, N > &)> &convolution_kernel)
Convolve a grid with a kernel.
void particles_to_fourier_grid(const T *part, size_t NumPart, size_t NumPartTot, FFTWGrid< N > &density_grid_fourier, std::string density_assignment_method, bool interlacing)
Assign particles to grid to compute the over density.
void interpolate_grid_vector_to_particle_positions(const std::array< FFTWGrid< N >, N > &grid_vec, const T *part, size_t NumPart, std::array< std::vector< FloatType >, N > &interpolated_values_vec)
Interpolate a vector of grids (e.g.
std::function< double(std::array< double, N > &)> get_window_function(std::string density_assignment_method, int Ngrid)
This returns the a function giving the window function for a given density assignment method as funct...
std::pair< int, int > get_extra_slices_needed_for_density_assignment(std::string density_assignment_method)
Compute how many extra slices we need in the FFTWGrid for a given density assignment / interpolation ...
double kernel< 5 >(double x)
The PQS kernel.
FML::GRID::FloatType FloatType
void add_contribution_from_extra_slices(FFTWGrid< N > &density, double background=-1.0)
Internal method. For communication between tasks needed when adding particles to grid.
double interpolate_grid_vector_to_particle_positions_apply(const std::array< FFTWGrid< N >, N > &grid, const T *part, size_t NumPart, Apply &&apply)
Gather a grid vector and immediately consume each interpolated value, without output arrays.
bool is_known_interpolation_method(const std::string &method)
Is the name a known density assignment / interpolation method (NGP, CIC, TSC, PCS,...
void particles_to_fourier_grid_interlacing(const T *part, size_t NumPart, size_t NumPartTot, FFTWGrid< N > &density_grid_fourier, std::string density_assignment_method)
Internal method.
FML::GRID::FFTWGrid< N > FFTWGrid
void interpolate_grid_to_particle_positions(const FFTWGrid< N > &grid, const T *part, size_t NumPart, std::vector< FloatType > &interpolated_values)
Interpolate a grid to a set of positions given by the positions of particles.
int interpolation_order_from_name(std::string density_assignment_method)
Get the interpolation order from a string holding the density_assignment_method (NGP,...
void deconvolve_window_function_fourier(FFTWGrid< N > &fourier_grid, std::string density_assignment_method)
Deconvolves the density assignment kernel in Fourier space.
double kernel< 1 >(double x)
The NGP kernel.
double kernel< 2 >(double x)
The CIC kernel.
std::pair< int, int > get_extra_slices_needed_by_order()
Compute how many extra slices we need in the FFTWGrid for a given density assignment order.
std::vector< std::array< double, NDIM > > generate_interlacing_shifts(int interlacing_order, double dx)
Generate the set of shifts used for interlacing (alias reduction by averaging the density field compu...
void particles_to_grid(const T *part, size_t NumPart, size_t NumPartTot, FFTWGrid< N > &density, std::string density_assignment_method)
Assign particles to a grid to compute the over density field delta.
double kernel< 4 >(double x)
The PCS kernel.
U wrap_coordinate_periodic(U x)
Wrap a coordinate periodically into [0,1) assuming it is in [-1,2) (i.e.
get_vel constexpr double * GetPos(...)
set_mass constexpr double GetMass(Args &... args)
FML_UNITS_INLINE constexpr double density
constexpr long long int power(int base, int exponent)
Simple integer a^b power-function by squaring (b >= 0; returns 0 for b < 0 instead of recursing forev...
int ThisTask
The MPI task number.
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).
void SumOverTasks(T *value)
Inplace sum over tasks of a single value.
int NTasks
Total number of MPI tasks.
ComplexType value(const std::array< int, N > &wave, int generation)