45 template <
int N,
class T>
48 template <
int N,
class T>
49 void DriftParticles(T * p,
size_t NumPart,
double delta_time,
bool periodic_box =
true);
51 template <
int N,
class T>
57 template <
int N,
class T>
67 std::string density_assignment_method_used,
68 double norm_poisson_equation,
74 std::string density_assignment_method_used,
75 double norm_poisson_equation,
76 const ForceOptions &
options = {});
93 if (x >= 1.0 or x < 0.0) {
95 if (x >= 1.0 or x < 0.0)
102 return stencil_order == 2 or stencil_order == 4 or stencil_order == 6;
108 std::string force_interpolation_method,
110 const ForceOptions &
options = {});
115 double norm_poisson_equation,
116 int n_boundary_slices_to_be_allocated,
117 const ForceOptions &
options = {});
142 template <
int N,
class T>
146 std::string density_assignment_method,
147 double norm_poisson_equation,
151 const bool periodic_box =
true;
157 density_grid_real.
add_memory_label(
"FFTWGrid::KickDriftKickNBodyStep::density_grid_real");
163 density_assignment_method);
166 std::array<FFTWGrid<N>, N> force_real;
169 density_assignment_method,
170 norm_poisson_equation,
174 KickParticles(force_real, part, delta_time * 0.5, density_assignment_method);
184 density_assignment_method);
189 density_assignment_method,
190 norm_poisson_equation,
194 KickParticles(force_real, part, delta_time * 0.5, density_assignment_method);
211 template <
int N,
class T>
215 std::string density_assignment_method,
216 double norm_poisson_equation,
220 const bool periodic_box =
true;
223 const double w1 = 1.0 / (2 - std::pow(2.0, 1.0 / 3.0));
224 const double w0 = 1.0 - 2.0 * w1;
225 const double c1 = w1 / 2.0, c4 = c1;
226 const double c2 = (w0 + w1) / 2.0, c3 = c2;
227 const double d1 = w1, d3 = d1;
228 const double d2 = w0;
231 assert(std::fabs(c1 + c2 + c3 + c4 - 1.0) < 1e-10);
232 assert(std::fabs(d1 + d2 + d3 - 1.0) < 1e-10);
238 density_grid_real.
add_memory_label(
"FFTWGrid::YoshidaNBodyStep::density_grid_real");
242 auto one_step = [&](
double delta_time_pos,
double delta_time_vel,
double norm_poisson) {
251 density_assignment_method);
253 std::array<FFTWGrid<N>, N> force_real;
256 density_assignment_method,
261 KickParticles(force_real, part, delta_time_vel, density_assignment_method);
265 one_step(delta_time * c1, delta_time * d1, norm_poisson_equation);
266 one_step(delta_time * c2, delta_time * d2, norm_poisson_equation);
267 one_step(delta_time * c3, delta_time * d3, norm_poisson_equation);
292 std::string density_assignment_method_used,
293 double norm_poisson_equation,
297 FFTWGrid<N> density_grid_fourier = density_grid_real;
298 density_grid_fourier.
add_memory_label(
"FFTWGrid::compute_force_from_density_real::density_grid_fourier");
303 density_assignment_method_used,
304 norm_poisson_equation,
327 std::string density_assignment_method_used,
328 double norm_poisson_equation,
332 const int LAPLACE_KERNEL =
options.laplace_kernel;
333 const int GRADIENT_KERNEL =
options.gradient_kernel;
342 const bool DECONVOLVE =
options.deconvolve;
344 const double knyquist = M_PI *
Nmesh;
345 [[maybe_unused]]
auto window_function = [&](std::array<double, N> & kvec) ->
double {
347 for (
int idim = 0; idim < N; idim++) {
348 const double koverkny = M_PI / 2. * (kvec[idim] / knyquist);
349 w *= koverkny == 0.0 ? 1.0 : std::sin(koverkny) / (koverkny);
353 for (
int i = 0; i < order; i++)
360 std::vector<double> gradient_kernel(2 * (
Nmesh / 2 + 1), 0.0);
361 for (
int i = -
Nmesh / 2; i <=
Nmesh / 2; i++) {
362 double k_j = 2.0 * M_PI * i;
367 for (
int idim = 0; idim < N; idim++) {
368 force_real[idim] = density_grid_fourier;
369 force_real[idim].
add_memory_label(
"FFTWGrid::compute_force_from_density_fourier::force_real_" +
370 std::to_string(idim));
371 force_real[idim].set_grid_status_real(idim == 0);
376 if constexpr (N == 3) {
378 const int nover2 =
Nmesh / 2;
379 const int nover2plus1 =
Nmesh / 2 + 1;
380 const double twopi = 2.0 * M_PI;
382#pragma omp parallel for
384 for (
int islice = 0; islice < Local_nx; islice++) {
385 std::array<double, N> kvec;
386 std::array<int, N> k_vec_integer;
387 std::complex<FML::GRID::FloatType>
I(0, 1);
388 const int ix = islice + int(Local_x_start);
389 k_vec_integer[0] = ix <= nover2 ? ix : ix -
Nmesh;
390 kvec[0] = twopi * double(k_vec_integer[0]);
391 for (
int iy = 0; iy <
Nmesh; iy++) {
392 k_vec_integer[1] = iy <= nover2 ? iy : iy -
Nmesh;
393 kvec[1] = twopi * double(k_vec_integer[1]);
394 size_t fourier_index = (size_t(islice) *
Nmesh + iy) * nover2plus1;
395 for (
int iz = 0; iz < nover2plus1; iz++, fourier_index++) {
396 if (Local_x_start == 0 and fourier_index == 0) {
397 for (
int idim = 0; idim < N; idim++)
398 force_real[idim].set_fourier_from_index(0, 0.0);
401 k_vec_integer[2] = iz <= nover2 ? iz : iz -
Nmesh;
402 kvec[2] = twopi * double(k_vec_integer[2]);
403 const double kmag2 = kvec[0] * kvec[0] + kvec[1] * kvec[1] + kvec[2] * kvec[2];
404 auto value = force_real[0].get_fourier_from_index(fourier_index);
406 value *= -1.0 / kmag2;
412 double W = window_table(kvec);
415 for (
int idim = 0; idim < N; idim++) {
416 force_real[idim].set_fourier_from_index(
419 FML::GRID::FloatType(gradient_kernel[
Nmesh / 2 + k_vec_integer[idim]] *
420 norm_poisson_equation));
425 for (
int idim = 0; idim < N; idim++)
426 force_real[idim].fftw_c2r();
432#pragma omp parallel for
434 for (
int islice = 0; islice < Local_nx; islice++) {
435 std::array<double, N> kvec;
436 std::complex<FML::GRID::FloatType>
I(0, 1);
437 for (
auto && fourier_index : force_real[0].get_fourier_range(islice, islice + 1)) {
438 if (Local_x_start == 0 and fourier_index == 0) {
439 for (
int idim = 0; idim < N; idim++)
440 force_real[idim].set_fourier_from_index(0, 0.0);
445 auto k_vec_integer = force_real[0].get_fourier_integer_wavevector_from_index(fourier_index);
447 for (
int idim = 0; idim < N; idim++) {
448 kvec[idim] = k_vec_integer[idim] * 2.0 * M_PI;
449 kmag2 += kvec[idim] * kvec[idim];
451 auto value = force_real[0].get_fourier_from_index(fourier_index);
453 force_real[0].get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
457 value *= -1.0 / kmag2;
464 double W = window_function(kvec);
470 for (
int idim = 0; idim < N; idim++) {
471 force_real[idim].set_fourier_from_index(
474 FML::GRID::FloatType(gradient_kernel[
Nmesh / 2 + k_vec_integer[idim]] *
475 norm_poisson_equation));
481 for (
int idim = 0; idim < N; idim++)
482 force_real[idim].fftw_c2r();
497 template <
int N,
class T>
501 if (delta_time == 0.0)
505 assert_mpi(FML::PARTICLE::GetNDIM(T()) == N,
506 "[DriftParticles] NDIM of particles and of grid does not match");
529 template <
int N,
class T>
530 void DriftParticles(T * p,
size_t NumPart,
double delta_time,
bool periodic_box) {
532 if (delta_time == 0.0)
536 assert_mpi(FML::PARTICLE::GetNDIM(T()) == N,
537 "[DriftParticles] NDIM of particles and of grid does not match");
538 static_assert(FML::PARTICLE::has_get_pos<T>(),
539 "[DriftParticles] Particle class must have a get_pos method to use this method");
540 static_assert(FML::PARTICLE::has_get_vel<T>(),
541 "[DriftParticles] Particle class must have a get_vel method to use this method");
543 double max_disp = 0.0;
545#pragma omp parallel for reduction(max : max_disp)
547 for (
size_t i = 0; i < NumPart; i++) {
550 for (
int idim = 0; idim < N; idim++) {
551 double disp = vel[idim] * delta_time;
553 max_disp = std::max(max_disp, std::abs(disp));
563 std::cout <<
"[Drift] Max displacement: " << max_disp <<
"\n";
582 template <
int N,
class T>
587 if (delta_time == 0.0)
613 template <
int N,
class T>
621 if (delta_time == 0.0)
625 assert_mpi(FML::PARTICLE::GetNDIM(T()) == N,
"[KickParticles] Dimension of particle and grid do not match");
626 static_assert(FML::PARTICLE::has_get_vel<T>(),
627 "[KickParticles] Particle must have velocity to use this method");
631 constexpr bool free_force_grids =
false;
634 for (
int idim = 0; idim < N; idim++) {
635 force_grid[idim].communicate_boundaries();
639 auto kick = [&](
size_t index,
const std::array<FML::GRID::FloatType, N> & force) {
641 double max_change = 0.0;
642 for (
int idim = 0; idim < N; idim++) {
643 const double dvel = -force[idim] * delta_time;
644 max_change = std::max(max_change, std::abs(dvel));
655 if (free_force_grids) {
656 for (
int idim = 0; idim < N; idim++) {
657 force_grid[idim].free();
664 std::cout <<
"[Kick] Max delta_vel : " << max_dvel <<
"\n";
667 template <
int N,
class T>
678 std::vector<double> velocity_norms,
679 std::function<
double(
double)> velocity_norm_1LPT_of_kBox =
nullptr,
680 const FFTWGrid<N> * delta_fourier_2LPT_source =
nullptr,
681 const FFTWGrid<N> * delta_fourier_1LPT_stored =
nullptr,
682 const FFTWGrid<N> * mass_contrast_fourier =
nullptr,
683 std::function<
double(
double)> velocity_norm_2LPT_of_kBox =
nullptr);
716 template <
int N,
class T>
724 std::function<
double(
double)> Pofk_of_kBox_over_Pofk_primordal,
725 std::function<
double(
double)> Pofk_of_kBox_over_volume_primordial,
732 std::vector<double> velocity_norms) {
736 "[NBodyInitialConditions] Invalid parameters");
741 delta_fourier.
add_memory_label(
"FFTWGrid::NBodyInitialConditions::delta_fourier");
746 auto Pofk_of_kBox_over_volume = [&](
double kBox) {
749 return Pofk_of_kBox_over_Pofk_primordal(kBox) * Pofk_of_kBox_over_volume_primordial(kBox);
753 Pofk_of_kBox_over_volume,
759 Pofk_of_kBox_over_Pofk_primordal,
760 Pofk_of_kBox_over_volume_primordial,
767 std::vector<FFTWGrid<N>> phi_nLPT_potentials;
818 template <
int N,
class T>
829 std::vector<double> velocity_norms,
830 std::function<
double(
double)> velocity_norm_1LPT_of_kBox,
834 std::function<
double(
double)> velocity_norm_2LPT_of_kBox) {
839 std::cout <<
"#=====================================================\n";
841 std::cout <<
"# .___ _________ \n";
842 std::cout <<
"# | | \\_ ___ \\ \n";
843 std::cout <<
"# | | / \\ \\/ \n";
844 std::cout <<
"# | | \\ \\____ \n";
845 std::cout <<
"# |___| \\______ / \n";
846 std::cout <<
"# \\/ \n";
848 std::cout <<
"# Generating initial conditions for N-body\n";
849 std::cout <<
"# Order in LPT = " << LPT_order <<
"\n";
850 std::cout <<
"# The boxsize is " <<
box <<
" comoving Mpc/h\n";
851 std::cout <<
"# The initial redshift zini = " <<
zini <<
"\n";
852 if (FML::PARTICLE::has_get_pos<T>())
853 std::cout <<
"# Particle has [Position] x_code = x / Box ("
855 if (FML::PARTICLE::has_get_vel<T>())
856 std::cout <<
"# Particle has [Velocity] v_code = a^2 dxdt / (H0 Box) ("
858 if (FML::PARTICLE::has_set_mass<T>())
860 if (FML::PARTICLE::has_set_id<T>())
862 if (FML::PARTICLE::has_get_D_1LPT<T>())
863 std::cout <<
"# Particle has [1LPT Displacement field] ("
864 <<
sizeof(FML::PARTICLE::GetD_1LPT(tmp)[0]) * N <<
" bytes)\n";
865 if (FML::PARTICLE::has_get_D_2LPT<T>())
866 std::cout <<
"# Particle has [2LPT Displacement field] ("
868 if (FML::PARTICLE::has_get_D_3LPTa<T>())
869 std::cout <<
"# Particle has [3LPTa Displacement field] ("
871 if (FML::PARTICLE::has_get_D_3LPTb<T>())
872 std::cout <<
"# Particle has [3LPTb Displacement field] ("
874 if (FML::PARTICLE::has_get_q<T>())
875 std::cout <<
"# Particle has [Lagrangian position] ("
877 std::cout <<
"# Total size of particle is " << FML::PARTICLE::GetSize(tmp) <<
" bytes\n";
878 std::cout <<
"# We will make " <<
Npart_1D <<
"^" << N <<
" particles\n";
879 std::cout <<
"# Plus a buffer with room for " << (
buffer_factor - 1.0) * 100.0 <<
"% more particles\n";
880 std::cout <<
"# We will allocate ~"
883 <<
" MB per task for the particles\n";
885 std::cout <<
"#=====================================================\n";
891 assert_mpi(
Nmesh > 0,
"[NBodyInitialConditions] delta_fourier has to be already allocated");
892 assert_mpi(delta_fourier_2LPT_source ==
nullptr or LPT_order <= 2,
893 "[NBodyInitialConditions] A separate 2LPT source is only implemented for LPT_order <= 2");
894 assert_mpi(LPT_order == 1 or LPT_order == 2 or LPT_order == 3,
895 "[NBodyInitialConditions] Only 1LPT, 2LPT and 3LPT implemented so valid choices here are "
896 "LPT_order = 1, 2 or 3");
898 const size_t n_velocity_norms_needed = LPT_order == 3 ? 4 : size_t(LPT_order);
899 assert_mpi(velocity_norms.size() >= n_velocity_norms_needed,
900 "[NBodyInitialConditions] velocity_norms must have (at least) one factor per displacement "
901 "field: 1 for LPT_order = 1, 2 for LPT_order = 2 and 4 (1LPT, 2LPT, 3LPTa, 3LPTb) for "
904 const auto nextra_cic =
908 "[NBodyInitialConditions] We use CIC interpolation in this routine so the grid needs to have "
909 "at least one extra slice on the right");
912 assert_mpi(FML::PARTICLE::GetNDIM(T()) == N,
913 "[NBodyInitialConditions] NDIM of particles and of grid does not match");
914 assert_mpi(FML::PARTICLE::has_get_pos<T>(),
915 "[NBodyInitialConditions] Particle class must have a get_pos method");
918 const double aini = 1.0 / (1.0 +
zini);
924 if (LPT_order == 1) {
927 }
else if (LPT_order == 2) {
932 delta_fourier_2LPT_source ? *delta_fourier_2LPT_source : delta_fourier,
934 }
else if (LPT_order == 3) {
937 const bool ignore_3LPT_curl_term =
true;
938 std::array<FFTWGrid<N>, N> phi_3LPT_Avec_fourier;
944 phi_3LPT_Avec_fourier,
945 ignore_3LPT_curl_term);
952 auto comp_displacement = [&]([[maybe_unused]]
int nLPT,
954 std::array<std::vector<FML::GRID::FloatType>, N> & displacements_nLPT) {
956 std::array<FFTWGrid<N>, N> Psi_nLPT_vector;
958 for (
int idim = 0; idim < N; idim++) {
959 Psi_nLPT_vector[idim].communicate_boundaries();
972 std::array<std::vector<FML::GRID::FloatType>, N> stored_1LPT;
974 auto add_displacement = [&]([[maybe_unused]]
int nLPT,
976 std::array<std::vector<FML::GRID::FloatType>, N> & displacements_nLPT,
978 const std::array<std::vector<FML::GRID::FloatType>, N> * velocities_nLPT =
982 double max_disp_nLPT = 0.0;
983 double max_vel_nLPT = 0.0;
986#pragma omp parallel for reduction(max : max_disp_nLPT, max_vel_nLPT)
988 for (
size_t ind = 0; ind < part.
get_npart(); ind++) {
991 std::array<double, N> disp;
992 for (
int idim = 0; idim < N; idim++) {
993 disp[idim] = displacements_nLPT[idim][ind];
999 std::cout <<
"Adding " << std::to_string(nLPT) <<
"LPT position to particle\n";
1000 for (
int idim = 0; idim < N; idim++) {
1001 const double dpos_nLPT = disp[idim];
1002 pos[idim] += dpos_nLPT;
1008 if (std::fabs(dpos_nLPT) > max_disp_nLPT)
1009 max_disp_nLPT = std::fabs(dpos_nLPT);
1013 if constexpr (FML::PARTICLE::has_get_vel<T>()) {
1015 std::cout <<
"Adding " << std::to_string(nLPT) <<
"LPT velocity to particle\n";
1017 for (
int idim = 0; idim < N; idim++) {
1019 velocities_nLPT ? double((*velocities_nLPT)[idim][ind]) : vfac_nLPT * disp[idim];
1022 if (std::fabs(dvel) > max_vel_nLPT)
1023 max_vel_nLPT = std::fabs(dvel);
1030 if constexpr (FML::PARTICLE::has_get_D_1LPT<T>()) {
1032 std::cout <<
"Storing 1LPT displacement field in particle\n";
1033 auto * D = FML::PARTICLE::GetD_1LPT(part_ptr[ind]);
1034 for (
int idim = 0; idim < N; idim++) {
1035 D[idim] = delta_fourier_1LPT_stored ? stored_1LPT[idim][ind] : disp[idim];
1041 if constexpr (FML::PARTICLE::has_get_D_2LPT<T>()) {
1043 std::cout <<
"Storing 2LPT displacement field in particle\n";
1045 for (
int idim = 0; idim < N; idim++) {
1046 D2[idim] = disp[idim];
1051 if (nLPT == 3 and type ==
'a') {
1052 if constexpr (FML::PARTICLE::has_get_D_3LPTa<T>()) {
1054 std::cout <<
"Storing 3LPTa displacement field in particle\n";
1056 for (
int idim = 0; idim < N; idim++) {
1057 D3a[idim] = disp[idim];
1062 if (nLPT == 3 and type ==
'b') {
1063 if constexpr (FML::PARTICLE::has_get_D_3LPTb<T>()) {
1065 std::cout <<
"Storing 3LPTb displacement field in particle\n";
1067 for (
int idim = 0; idim < N; idim++) {
1068 D3b[idim] = disp[idim];
1078 std::cout <<
"Maximum " << std::to_string(nLPT) <<
"LPT displacements: " << max_disp_nLPT *
box
1081 std::cout <<
"Maximum " << std::to_string(nLPT)
1082 <<
"LPT velocity: " << max_vel_nLPT * 100.0 *
box /
aini <<
" km/s peculiar\n";
1090 if constexpr (FML::PARTICLE::has_set_id<T>()) {
1092 std::cout <<
"Storing unique ID in particle\n";
1093 long long int npart_local = part.
get_npart();
1095 long long int id_start = 0;
1097 id_start += part_per_task[i];
1098 long long int count = 0;
1099 for (
auto & p : part) {
1105 if constexpr (FML::PARTICLE::has_set_mass<T>()) {
1107 std::cout <<
"Storing Mass (1.0) in particle\n";
1108 for (
auto & p : part) {
1114 if constexpr (FML::PARTICLE::has_get_q<T>()) {
1116 std::cout <<
"Storing Lagrangian position q in particle\n";
1117 for (
auto & p : part) {
1120 for (
int idim = 0; idim < N; idim++)
1121 q[idim] = pos[idim];
1126 if (mass_contrast_fourier) {
1127 if constexpr (FML::PARTICLE::has_set_mass<T>()) {
1130 eps.communicate_boundaries();
1131 std::vector<FML::GRID::FloatType> eps_at_part;
1137 double eps_min = 1e100, eps_max = -1e100, eps_mean = 0.0;
1139 for (
auto & p : part) {
1140 const double e = eps_at_part[ind++];
1142 eps_min = std::min(eps_min, e);
1143 eps_max = std::max(eps_max, e);
1151 std::cout <<
"Storing Mass 1 + eps(q) in particle: eps in [" << eps_min <<
", " << eps_max
1152 <<
"], mean " << eps_mean <<
"\n";
1154 assert_mpi(
false,
"[NBodyInitialConditions] mass_contrast_fourier needs particles with set_mass");
1159 auto comp_scaledependent_velocity = [&](
int nLPT,
1161 const std::function<double(
double)> & norm_of_kBox,
1162 std::array<std::vector<FML::GRID::FloatType>, N> & velocities) {
1166#pragma omp parallel for
1168 for (
int islice = 0; islice < Local_nx; islice++) {
1169 [[maybe_unused]]
double kmag;
1170 [[maybe_unused]] std::array<double, N> kvec;
1173 const double norm = kmag > 0.0 ? norm_of_kBox(kmag) : 0.0;
1176 FML::GRID::FloatType(
norm));
1179 comp_displacement(nLPT, phi_vel, velocities);
1184 std::array<std::vector<FML::GRID::FloatType>, N> displacements_1LPT;
1185 std::array<std::vector<FML::GRID::FloatType>, N> velocities_1LPT;
1186 if (LPT_order >= 1) {
1189 if (velocity_norm_1LPT_of_kBox)
1190 comp_scaledependent_velocity(nLPT, phi_1LPT, velocity_norm_1LPT_of_kBox, velocities_1LPT);
1191 comp_displacement(nLPT, phi_1LPT, displacements_1LPT);
1192 if (delta_fourier_1LPT_stored and FML::PARTICLE::has_get_D_1LPT<T>()) {
1195 comp_displacement(nLPT, phi_stored, stored_1LPT);
1199 std::array<std::vector<FML::GRID::FloatType>, N> displacements_2LPT;
1200 std::array<std::vector<FML::GRID::FloatType>, N> velocities_2LPT;
1201 if (LPT_order >= 2) {
1204 if (phi_nLPT_potentials.size() > 0)
1205 phi_nLPT_potentials[0] = phi_2LPT;
1207 if (velocity_norm_2LPT_of_kBox)
1208 comp_scaledependent_velocity(nLPT, phi_2LPT, velocity_norm_2LPT_of_kBox, velocities_2LPT);
1209 comp_displacement(nLPT, phi_2LPT, displacements_2LPT);
1212 std::array<std::vector<FML::GRID::FloatType>, N> displacements_3LPTa;
1213 if (LPT_order >= 3) {
1216 if (phi_nLPT_potentials.size() > 1)
1217 phi_nLPT_potentials[1] = phi_3LPTa;
1218 comp_displacement(nLPT, phi_3LPTa, displacements_3LPTa);
1221 std::array<std::vector<FML::GRID::FloatType>, N> displacements_3LPTb;
1222 if (LPT_order >= 3) {
1225 if (phi_nLPT_potentials.size() > 2)
1226 phi_nLPT_potentials[2] = phi_3LPTb;
1227 comp_displacement(nLPT, phi_3LPTb, displacements_3LPTb);
1230 if (LPT_order >= 1) {
1232 add_displacement(nLPT,
1236 velocity_norm_1LPT_of_kBox ? &velocities_1LPT :
nullptr);
1237 for (
auto & d : displacements_1LPT) {
1241 for (
auto & d : velocities_1LPT) {
1247 if (LPT_order >= 2) {
1249 add_displacement(nLPT,
1253 velocity_norm_2LPT_of_kBox ? &velocities_2LPT :
nullptr);
1254 for (
auto & d : displacements_2LPT) {
1258 for (
auto & d : velocities_2LPT) {
1264 if (LPT_order >= 3) {
1266 add_displacement(nLPT,
'a', displacements_3LPTa, velocity_norms[2]);
1267 for (
auto & d : displacements_3LPTa) {
1273 if (LPT_order >= 3) {
1275 add_displacement(nLPT,
'b', displacements_3LPTb, velocity_norms[3]);
1276 for (
auto & d : displacements_3LPTb) {
1304 std::function<
double(
double)> coupling_factor_of_kBox) {
1306 auto coupling_factor_of_kBox_spline =
1309 density_mg_fourier = density_fourier;
1311#pragma omp parallel for
1313 for (
int islice = 0; islice < Local_nx; islice++) {
1314 [[maybe_unused]]
double kmag;
1315 [[maybe_unused]] std::array<double, N> kvec;
1316 for (
auto && fourier_index : density_mg_fourier.
get_fourier_range(islice, islice + 1)) {
1321 auto coupling = coupling_factor_of_kBox_spline(kmag);
1356 std::function<
double(
double)> coupling_factor_of_kBox,
1357 std::function<
double(
double)> screening_factor_of_newtonian_potential,
1358 double poisson_norm) {
1364 density_mg_fourier = density_fourier;
1368#pragma omp parallel for
1370 for (
int islice = 0; islice < Local_nx; islice++) {
1371 [[maybe_unused]]
double kmag2;
1372 [[maybe_unused]] std::array<double, N> kvec;
1373 for (
auto && fourier_index : density_mg_fourier.
get_fourier_range(islice, islice + 1)) {
1377 value *= -poisson_norm / kmag2;
1384 if (Local_x_start == 0)
1388 auto delta_real = density_fourier;
1392 delta_real.fftw_c2r();
1396#pragma omp parallel for
1398 for (
int islice = 0; islice < Local_nx; islice++) {
1399 for (
auto && real_index : density_mg_fourier.
get_real_range(islice, islice + 1)) {
1401 auto delta = delta_real.get_real_from_index(real_index);
1402 auto screening_factor = screening_factor_of_newtonian_potential(phi_newton);
1412#pragma omp parallel for
1414 for (
int islice = 0; islice < Local_nx; islice++) {
1415 [[maybe_unused]]
double kmag;
1416 [[maybe_unused]] std::array<double, N> kvec;
1417 for (
auto && fourier_index : density_mg_fourier.
get_fourier_range(islice, islice + 1)) {
1424 auto coupling = coupling_factor_of_kBox(kmag);
1457 std::function<
double(
double)> coupling_factor_of_kBox,
1458 std::function<
double(
double)> screening_factor_of_density,
1459 double smoothing_scale,
1460 std::string smoothing_method) {
1465 density_mg_fourier = density_fourier;
1468 if (smoothing_scale > 0.0)
1476#pragma omp parallel for
1478 for (
int islice = 0; islice < Local_nx; islice++) {
1479 for (
auto && real_index : density_mg_fourier.
get_real_range(islice, islice + 1)) {
1481 auto screening_factor = screening_factor_of_density(1.0 + delta);
1491#pragma omp parallel for
1493 for (
int islice = 0; islice < Local_nx; islice++) {
1494 [[maybe_unused]]
double kmag;
1495 [[maybe_unused]] std::array<double, N> kvec;
1496 for (
auto && fourier_index : density_mg_fourier.
get_fourier_range(islice, islice + 1)) {
1503 auto coupling = coupling_factor_of_kBox(kmag);
1531 std::string force_interpolation_method,
1538 "[compute_force_from_potential_real] Unknown stencil_order: only 2, 4 and 6 are implemented");
1541 "[compute_force_from_potential_real] The potential needs at least stencil_order/2 extra slices "
1542 "on the left and on the right (n_boundary_slices_to_be_allocated in "
1543 "compute_potential_real_from_density_fourier)");
1548 const double one_over_h = double(
Nmesh);
1554 potential_deconvolved = potential_real;
1556 "FFTWGrid::compute_force_from_potential_real::potential_deconvolved");
1559#pragma omp parallel for
1561 for (
int islice = 0; islice < Local_nx; islice++) {
1563 std::array<double, N> kvec;
1564 for (
auto && fourier_index : potential_deconvolved.
get_fourier_range(islice, islice + 1)) {
1565 if (Local_x_start == 0 and fourier_index == 0)
1568 const double W = window_table(kvec);
1584 for (
int idim = 0; idim < N; idim++)
1589 auto add_to_coord = [=](std::array<int, N> & coord,
int idim,
int iplus) {
1590 coord[idim] += iplus;
1593 coord[idim] -=
Nmesh;
1595 coord[idim] +=
Nmesh;
1597 if (coord[idim] >=
Nmesh)
1598 coord[idim] -=
Nmesh;
1599 if (coord[idim] < 0)
1600 coord[idim] +=
Nmesh;
1605#pragma omp parallel for
1607 for (
int islice = 0; islice < Local_nx; islice++) {
1608 for (
auto && real_index :
potential.get_real_range(islice, islice + 1)) {
1611 [[maybe_unused]] std::array<FML::GRID::FloatType, N> dPhidx_pm1{};
1612 [[maybe_unused]] std::array<FML::GRID::FloatType, N> dPhidx_pm2{};
1613 [[maybe_unused]] std::array<FML::GRID::FloatType, N> dPhidx_pm3{};
1616 if (stencil_order >= 2) {
1617 for (
int idim = 0; idim < N; idim++) {
1620 auto coord_plus = coord;
1621 add_to_coord(coord_plus, idim, +1);
1622 auto coord_minus = coord;
1623 add_to_coord(coord_minus, idim, -1);
1626 auto Phi_i_plus_one =
potential.get_real(coord_plus);
1627 auto Phi_i_minus_one =
potential.get_real(coord_minus);
1628 dPhidx_pm1[idim] = (Phi_i_plus_one - Phi_i_minus_one) / 2.0;
1633 if (stencil_order >= 4) {
1634 for (
int idim = 0; idim < N; idim++) {
1637 auto coord_plus = coord;
1638 add_to_coord(coord_plus, idim, +2);
1639 auto coord_minus = coord;
1640 add_to_coord(coord_minus, idim, -2);
1643 auto Phi_i_plus_two =
potential.get_real(coord_plus);
1644 auto Phi_i_minus_two =
potential.get_real(coord_minus);
1645 dPhidx_pm2[idim] = (Phi_i_plus_two - Phi_i_minus_two) / 4.0;
1650 if (stencil_order >= 6) {
1651 for (
int idim = 0; idim < N; idim++) {
1654 auto coord_plus = coord;
1655 add_to_coord(coord_plus, idim, +3);
1656 auto coord_minus = coord;
1657 add_to_coord(coord_minus, idim, -3);
1660 auto Phi_i_plus_three =
potential.get_real(coord_plus);
1661 auto Phi_i_minus_three =
potential.get_real(coord_minus);
1662 dPhidx_pm3[idim] = (Phi_i_plus_three - Phi_i_minus_three) / 6.0;
1667 if (stencil_order == 2) {
1668 for (
int idim = 0; idim < N; idim++) {
1669 auto DPhi = dPhidx_pm1[idim] * one_over_h;
1670 force_grid_real[idim].set_real_from_index(real_index, DPhi);
1672 }
else if (stencil_order == 4) {
1673 for (
int idim = 0; idim < N; idim++) {
1674 auto DPhi = (4.0 / 3.0 * dPhidx_pm1[idim] - dPhidx_pm2[idim] / 3.0) * one_over_h;
1675 force_grid_real[idim].set_real_from_index(real_index, DPhi);
1677 }
else if (stencil_order == 6) {
1678 for (
int idim = 0; idim < N; idim++) {
1680 (0.1 * dPhidx_pm3[idim] - 0.6 * dPhidx_pm2[idim] + 1.5 * dPhidx_pm1[idim]) * one_over_h;
1681 force_grid_real[idim].set_real_from_index(real_index, DPhi);
1703 const double dx = 1.0 / double(
Nmesh);
1706 return -1.0 / kmag2;
1711 for (
int idim = 0; idim <
NDIM; idim++) {
1712 double _sin_over_two = std::sin(kvec[idim] * dx / 2.0);
1713 sum1 += _sin_over_two * _sin_over_two;
1715 sum1 *= 4.0 / (dx * dx);
1723 for (
int idim = 0; idim <
NDIM; idim++) {
1724 double _sin = std::sin(kvec[idim] * dx);
1725 double _cos = std::cos(kvec[idim] * dx);
1729 sum2 += _sin * _sin * (9.0 - 8.0 * (_cos - 1.0) - _sin * _sin);
1738 sum2 *= 1.0 / (9.0 * dx * dx);
1743 FML::assert_mpi(
false,
"Unknown KERNEL in greens_function_laplace_operator_fourier");
1766 const double dx = 1.0 / double(
Nmesh);
1773 return FML::COSMOLOGY::LPT::detail::odd_derivative_wavevector<1>({k_j},
Nmesh)[0];
1777 return std::sin(k_j * dx) / dx;
1781 return (8.0 * std::sin(k_j * dx) - std::sin(2.0 * k_j * dx)) / (6.0 * dx);
1785 FML::assert_mpi(
false,
"Unknown KERNEL in gradient_kernel_fourier");
1811 double norm_poisson_equation,
1812 int n_boundary_slices_to_be_allocated,
1816 const int LAPLACE_KERNEL =
options.laplace_kernel;
1818 const auto Local_nx = density_grid_fourier.
get_local_nx();
1823 potential_real =
FFTWGrid<N>(
Nmesh, n_boundary_slices_to_be_allocated, n_boundary_slices_to_be_allocated);
1827#pragma omp parallel for
1829 for (
int islice = 0; islice < Local_nx; islice++) {
1831 std::array<double, N> kvec;
1832 for (
auto && fourier_index : density_grid_fourier.
get_fourier_range(islice, islice + 1)) {
1833 if (Local_x_start == 0 and fourier_index == 0) {
1842 value *= -1.0 / kmag2;
1847 value * FML::GRID::FloatType(norm_poisson_equation));
1869 compute_sigma_of_R(std::function<
double(
double)> Pofk_of_kBox_over_volume,
double R_mpch,
double boxsize_mpch) {
1875 const double kBoxmin = 1e-5 * boxsize_mpch;
1876 const double kBoxmax = 1e2 * boxsize_mpch;
1878 ODEFunction deriv = [&](
double logkBox, [[maybe_unused]]
const double * sigma2,
double * dsigma2dlogk) {
1879 const double kBox = std::exp(logkBox);
1880 const double dimless_pofk = kBox * kBox * kBox / (2.0 * M_PI * M_PI) * Pofk_of_kBox_over_volume(kBox);
1881 const double kR = kBox * R_mpch / boxsize_mpch;
1882 const double window = kR > 0.0 ? 3.0 * (std::sin(kR) - kR * std::cos(kR)) / (kR * kR * kR) : 1.0;
1883 dsigma2dlogk[0] = dimless_pofk * window * window;
1889 DVector logkarr{std::log(kBoxmin), std::log(kBoxmax)};
1893 ode.
solve(deriv, logkarr, sigmaini);
1896 return std::sqrt(sigma2);
FML::SOLVERS::FFTLog::DVector DVector
const std::string type_of_random_field
const double buffer_factor
const std::string interpolation_method
FML::SOLVERS::ODESOLVER::ODEFunction ODEFunction
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 communicate_boundaries()
Send slices to the neighboring CPUs which stores them in the left and right extra slice storage (with...
void fftw_c2r()
Perform complex-to-real fourier transform.
void set_real_from_index(const IndexIntType ind, const FloatType value)
Set value in grid from (local) index.
void add_memory_label(std::string label)
For memory logging: add a label to the grid.
FloatType get_real_from_index(const IndexIntType index) const
Fetch value in grid by (local) index.
ptrdiff_t get_local_x_start() const
The x-slice the first local slice has in the global grid.
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.
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 get_fourier_wavevector_and_norm_by_index(const IndexIntType ind, std::array< double, N > &kvec, double &kmag) const
From index in the grid get the k-vector and the magnitude of it.
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 get_fourier_wavevector_and_norm2_by_index(const IndexIntType ind, std::array< double, N > &kvec, double &kmag2) const
From index in the grid get the k-vector and the norm of it (square magnitude)
FML::INTERPOLATION::SPLINE::Spline make_fourier_spline(std::function< double(double)> function_of_kBox, std::string label, int sampling_factor=4) const
std::function can be slow so for looping through a fourier grid and evaluating a function f(k) in eve...
std::array< int, N > get_coord_from_index(const IndexIntType index_real) const
Get the (local) cell coordinates from a local index.
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.
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...
A container class for holding particles that are distributed across many CPUs and to easily deal with...
void create_particle_grid(int Npart_1D, double buffer_factor, double xmin_local, double xmax_local)
Create particles in a rectangular grid spread across all tasks buffer_factor is how much extra to al...
size_t get_npart_total() const
Total number of active particles across all tasks.
T * get_particles_ptr()
Get a pointer to the first particle.
void info()
Show some info about the class.
void communicate_particles(size_t max_message_bytes=std::numeric_limits< int >::max(), bool force_general=false)
Communicate particles across CPU boundaries.
size_t get_npart() const
Number of active particles on the local task.
This is a class for having a unified interface for random numbers in the library.
This is a wrapper around the GSL library to easily solve ODEs and return the data in whatever format ...
double get_final_data_by_component(int icomponent) const
Get the solution for a given component at the end point.
void solve(ODEFunctionPointer ode_equation, void *parameters, const DVector &xarr, const DVector &yinitial, const gsl_odeiv2_step_type *stepper=ODESOLVER_FIDUCIAL_STEPPER, ODEFunctionPointerJacobian jacobian=nullptr)
void compute_3LPT_potential_fourier(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_1LPT_fourier, FFTWGrid< N > &phi_2LPT_fourier, FFTWGrid< N > &phi_3LPT_a_fourier, FFTWGrid< N > &phi_3LPT_b_fourier, std::array< FFTWGrid< N >, N > &phi_3LPT_Avec_fourier, bool ignore_curl_term, Dealiasing dealiasing=Dealiasing::None)
Optional Padding uses a >=2N work mesh for all nonlinear sources, retaining the full intermediate sec...
void compute_1LPT_potential_fourier(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_1LPT_fourier)
Generate the 1LPT potential defined as and .
void compute_2LPT_potential_fourier(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_2LPT_fourier, Dealiasing dealiasing=Dealiasing::None)
Optional Padding uses a >=3N/2 work mesh for the quadratic source.
void from_LPT_potential_to_displacement_vector(const FFTWGrid< N > &phi_fourier, std::array< FFTWGrid< N >, N > &psi_real, double DoverDini=1.0)
Generate the displaceement field from the LPT potential .
void smoothing_filter_fourier_space(FFTWGrid< N > &fourier_grid, double smoothing_scale, std::string smoothing_method)
This namespace deals with interpolation and density assignment.
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::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 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.
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 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.
@ DISCRETE_GREENS_FUNCTION_FROM_TWO_POINT
@ CONTINUOUS_GREENS_FUNCTION
@ DISCRETE_GREENS_FUNCTION_FROM_FOUR_POINT
void KickDriftKickNBodyStep(int Nmesh, MPIParticles< T > &part, double delta_time, std::string density_assignment_method, double norm_poisson_equation, const ForceOptions &options={})
Take a N-body step with a simple Kick-Drift-Kick method (this method serves mainly as an example for ...
void KickParticles(std::array< FFTWGrid< N >, N > &force_grid, MPIParticles< T > &part, double delta_time, std::string interpolation_method)
This moves the particle velocities according to .
void compute_delta_fifth_force(const FFTWGrid< N > &density_fourier, FFTWGrid< N > &density_mg_fourier, std::function< double(double)> coupling_factor_of_kBox)
This method computes the fifth-force potential for modified gravity models using the linear approxima...
void periodic_wrap_unit_box(F &x)
Periodic wrap of a coordinate to [0,1).
void compute_delta_fifth_force_density_screening(const FFTWGrid< N > &density_fourier, FFTWGrid< N > &density_mg_fourier, std::function< double(double)> coupling_factor_of_kBox, std::function< double(double)> screening_factor_of_density, double smoothing_scale, std::string smoothing_method)
This method computes the fifth-force potential for modified gravity models which has a screening mech...
void compute_delta_fifth_force_potential_screening(const FFTWGrid< N > &density_fourier, FFTWGrid< N > &density_mg_fourier, std::function< double(double)> coupling_factor_of_kBox, std::function< double(double)> screening_factor_of_newtonian_potential, double poisson_norm)
This method computes the fifth-force potential for modified gravity models which has a screening mech...
void compute_force_from_density_fourier(const FFTWGrid< N > &density_grid_fourier, std::array< FFTWGrid< N >, N > &force_real, std::string density_assignment_method_used, double norm_poisson_equation, const ForceOptions &options={})
Take a density grid in fourier space and returns the force where Different choices for what kernel ...
double gradient_kernel_fourier(double k_j, int Nmesh, int KERNEL)
This function returns the equivalent of D/i = k_vec for different kernels.
double compute_sigma_of_R(std::function< double(double)> Pofk_of_kBox_over_volume, double R_mpch, double boxsize_mpch)
This computes the standard deviation of linear fluctuations smoothed over a sphere of radius,...
FML::GRID::FFTWGrid< N > FFTWGrid
double greens_function_laplace_operator_fourier(double kmag2, std::array< double, NDIM > &kvec, int Nmesh, int KERNEL)
This function returns the equivalent of -1/k^2 for different kernels.
void compute_force_from_density_real(const FFTWGrid< N > &density_grid_real, std::array< FFTWGrid< N >, N > &force_real, std::string density_assignment_method_used, double norm_poisson_equation, const ForceOptions &options={})
Take a density grid in real space and returns the force where Different choices for what kernel to ...
void compute_potential_real_from_density_fourier(const FFTWGrid< N > &density_grid_fourier, FFTWGrid< N > &potential_real, double norm_poisson_equation, int n_boundary_slices_to_be_allocated, const ForceOptions &options={})
Compute Phi(x) from the density field in fourier space Phi = F^-1( 1/D^2 * delta * norm ) The kernel ...
void compute_force_from_potential_real(FFTWGrid< N > &potential_real, std::array< FFTWGrid< N >, N > &force_grid_real, std::string force_interpolation_method, int stencil_order, const ForceOptions &options={})
Compute the force DPhi from the potential using finite differencing If options.deconvolve is set we d...
void YoshidaNBodyStep(int Nmesh, MPIParticles< T > &part, double delta_time, std::string density_assignment_method, double norm_poisson_equation, const ForceOptions &options={})
Take a N-body step with a 4th order symplectic Yoshida method.
void DriftParticles(FML::PARTICLE::MPIParticles< T > &part, double delta_time, bool periodic_box=true)
void NBodyInitialConditions(MPIParticles< T > &part, int Npart_1D, double buffer_factor, const FFTWGrid< N > &delta_fourier, std::vector< FFTWGrid< N > > &phi_nLPT_potentials, int LPT_order, double box, double zini, std::vector< double > velocity_norms, std::function< double(double)> velocity_norm_1LPT_of_kBox=nullptr, const FFTWGrid< N > *delta_fourier_2LPT_source=nullptr, const FFTWGrid< N > *delta_fourier_1LPT_stored=nullptr, const FFTWGrid< N > *mass_contrast_fourier=nullptr, std::function< double(double)> velocity_norm_2LPT_of_kBox=nullptr)
Generate particles from a given initial density field using Lagrangian perturbation theory.
@ DISCRETE_TWO_POINT_GRADIENT
@ DISCRETE_FOUR_POINT_GRADIENT
bool is_valid_finite_difference_stencil_order(int stencil_order)
The finite difference stencils implemented in compute_force_from_potential_real.
get_vel constexpr double * GetPos(...)
constexpr double * GetLagrangianPos(...)
constexpr double * GetD_3LPTa(...)
constexpr void SetMass(...)
set_id constexpr int GetID(...)
constexpr double * GetVel(...)
constexpr double * GetD_3LPTb(...)
constexpr double * GetD_2LPT(...)
set_mass constexpr double GetMass(Args &... args)
constexpr void SetID(...)
void generate_gaussian_random_field_fourier(FFTWGrid< N > &grid, RandomGenerator *rng, std::function< double(double)> Pofk_of_kBox_over_volume, bool fix_amplitude)
void generate_nonlocal_gaussian_random_field_fourier_cosmology(FFTWGrid< N > &delta_fourier, RandomGenerator *rng, std::function< double(double)> Pofk_of_kBox_over_Pofk_primordal, std::function< double(double)> Pofk_of_kBox_over_volume_primordial, bool fix_amplitude, double fNL, std::string type_of_fnl, double u=0.0, std::vector< double > kernel_values={}, const GAUSSIAN::RandomFieldOptions &random_options={})
This computes a non-gaussian density field at any redshift.
std::vector< double > DVector
std::function< int(double, const double *, double *)> ODEFunction
double xmax_domain
The local extent of the domain (global domain goes from 0 to 1)
void MinOverTasks(T *value)
Inplace min over tasks of a single value.
void MaxOverTasks(T *value)
Inplace max over tasks of a single value.
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.
std::vector< T > GatherFromTasks(T *value)
Gather a single value from all tasks. Value from task ThisTask is stored in values[ThisTask].
void SumOverTasks(T *value)
Inplace sum over tasks of a single value.
double xmin_domain
The local extent of the domain (global domain goes from 0 to 1)
int NTasks
Total number of MPI tasks.
Per-call force discretization. No process-wide mutable defaults.
ComplexType value(const std::array< int, N > &wave, int generation)
std::mt19937_64 rng(1234)