1#ifndef ANALYZEANDOUTPUT_HEADER
2#define ANALYZEANDOUTPUT_HEADER
39template <
int NDIM,
class T>
42template <
int NDIM,
class T>
53 const auto ic_initial_redshift =
params.ic.initial_redshift;
61 const auto ic_use_gravity_model_GR =
params.ic.use_gravity_model_GR;
62 const auto pofk_subtract_shotnoise =
params.pofk.subtract_shotnoise;
68 auto redshift = p.first;
69 auto binning = p.second;
70 auto pofk_cb = [&](
double k) {
72 double D =
grav->get_D_1LPT(1.0 / (1.0 + redshift), k /
grav->H0_hmpc);
73 double Dini =
grav->get_D_1LPT(1.0 / (1.0 + ic_initial_redshift), k /
grav->H0_hmpc);
74 return pofk_ini * std::pow(D / Dini, 2);
80 cosmo->get_nonlinear_matter_power_spectrum_boost(redshift);
86 fp <<
"# k (h/Mpc) Pcb(k) (Mpc/h)^3 Pcb_linear(k) (Mpc/h)^3 Pcb_NL(k) (Mpc/h)^3 "
87 "ShotnoiseSubtracted = "
88 << std::boolalpha << pofk_subtract_shotnoise <<
"\n";
89 for (
int i = 0; i < binning.n; i++) {
90 double boost = non_linear_boost_of_k ? non_linear_boost_of_k(binning.kbin[i]) : 1.0;
91 fp << std::setw(15) << binning.kbin[i] <<
" ";
92 fp << std::setw(15) << binning.pofk[i] <<
" ";
93 fp << std::setw(15) << pofk_cb(binning.kbin[i]) <<
" ";
94 fp << std::setw(15) << pofk_cb(binning.kbin[i]) * boost <<
" ";
104 for (
auto & p : list) {
105 const double redshift = p.first;
106 const auto & binning = p.second;
107 const double a = 1.0 / (1.0 + redshift);
108 std::string
filename =
params.output.path(
"pofk_" +
params.simulation.name +
"_" + label +
"_z" +
111 fp <<
"# k (h/Mpc) P_" << label <<
"(k) (Mpc/h)^3 P_" << label
112 <<
"_linear(k) (Mpc/h)^3 ShotnoiseSubtracted = " << std::boolalpha << pofk_subtract_shotnoise <<
"\n";
113 for (
int i = 0; i < binning.n; i++) {
114 const double k = binning.kbin[i];
118 const double D =
grav->get_D_1LPT(a, k /
grav->H0_hmpc);
119 const double Dini =
grav->get_D_1LPT(1.0 / (1.0 + ic_initial_redshift), k /
grav->H0_hmpc);
121 std::pow(Ti /
transferdata->get_cdm_baryon_transfer_function(k, a), 2);
123 fp << std::setw(15) << k <<
" " << std::setw(15) << binning.pofk[i] <<
" " << std::setw(15) << plin
133 auto redshift = p.first;
134 auto binning = p.second;
135 auto pofk_total = [&](
double k) {
139 if (ic_use_gravity_model_GR) {
140 double D =
grav->get_D_1LPT(1.0 / (1.0 + redshift), k /
grav->H0_hmpc);
141 double Dini =
grav->get_D_1LPT(1.0 / (1.0 + ic_initial_redshift), k /
grav->H0_hmpc);
142 double D_GR =
grav_ic->get_D_1LPT(1.0 / (1.0 + redshift), k /
grav->H0_hmpc);
143 double Dini_GR =
grav_ic->get_D_1LPT(1.0 / (1.0 + ic_initial_redshift), k /
grav->H0_hmpc);
144 fac = std::pow((D / Dini) / (D_GR / Dini_GR), 2);
147 return fac *
transferdata->get_total_power_spectrum(k, 1.0 / (1.0 + redshift));
158 cosmo->get_nonlinear_matter_power_spectrum_boost(redshift);
161 fp <<
"# k (h/Mpc) P(k) (Mpc/h)^3 P_linear(k) (Mpc/h)^3 P_NL(k) ShotnoiseSubtracted = "
162 << std::boolalpha << pofk_subtract_shotnoise <<
"\n";
163 for (
int i = 0; i < binning.n; i++) {
164 double boost = non_linear_boost_of_k ? non_linear_boost_of_k(binning.kbin[i]) : 1.0;
165 fp << std::setw(15) << binning.kbin[i] <<
" ";
166 fp << std::setw(15) << binning.pofk[i] <<
" ";
167 fp << std::setw(15) << pofk_total(binning.kbin[i]) <<
" ";
168 fp << std::setw(15) << pofk_total(binning.kbin[i]) * boost <<
" ";
177template <
int NDIM,
class T>
179 std::vector<FML::FILEUTILS::GADGET::GadgetSpecies>
species;
181 using P = std::remove_reference_t<
decltype(*
particles.get_particles_ptr())>;
182 auto * p =
particles.get_particles_ptr();
188 if constexpr (FML::PARTICLE::has_get_deltaf_weight<P>())
189 sp.extra_scalars.push_back({
"Weights", [p](
size_t i) {
return double(p[i].get_deltaf_weight()); }});
191 if constexpr (FML::PARTICLE::has_get_mass<P>())
198template <
int NDIM,
class T>
206 const double scale_factor = 1.0 / (1.0 + redshift);
208 const double pos_norm = simulation_boxsize;
209 const double vel_norm = 100 * simulation_boxsize / std::pow(scale_factor, 1.5);
210 const std::string fileprefix = snapshot_folder +
"/" +
"gadget_z" + redshiftstring;
214 std::cout <<
"#=====================================================\n";
215 std::cout <<
"# Output in gadget format\n";
216 std::cout <<
"# fileprefix : " << fileprefix <<
"\n";
217 std::cout <<
"#=====================================================\n";
225 gw.write_gadget_species(fileprefix +
"." + std::to_string(
FML::ThisTask),
231 cosmo->get_OmegaLambda(),
237 gw.write_gadget_single(fileprefix +
"." + std::to_string(
FML::ThisTask),
245 cosmo->get_OmegaLambda(),
253template <
int NDIM,
class T>
255 [[maybe_unused]]
double redshift,
256 [[maybe_unused]] std::string snapshot_folder) {
259 const auto simulation_boxsize = sim.get_parameters().simulation.boxsize;
260 const auto cosmo = sim.get_cosmology();
263 const double scale_factor = 1.0 / (1.0 + redshift);
265 const double pos_norm = simulation_boxsize;
266 const double vel_norm = 100 * simulation_boxsize / std::pow(scale_factor, 1.5);
267 const std::string fileprefix = snapshot_folder +
"/" +
"gadget_z" + redshiftstring;
269 nfiles == 1 ? fileprefix +
".hdf5" : fileprefix +
"." + std::to_string(
FML::ThisTask) +
".hdf5";
273 std::cout <<
"#=====================================================\n";
274 std::cout <<
"# Output in gadget HDF5 format\n";
275 std::cout <<
"# fileprefix : " << fileprefix <<
"\n";
276 std::cout <<
"#=====================================================\n";
280 if (sim.get_species().size() > 1) {
290 cosmo->get_OmegaLambda(),
304 cosmo->get_OmegaLambda(),
309 throw std::runtime_error(
"output_gadget_hdf5: compile with USE_HDF5=true to write HDF5 files");
313template <
int NDIM,
class T>
319 const int bispectrum_nmesh = settings.nmesh;
320 const int bispectrum_nbins = settings.nbins;
321 const std::string bispectrum_density_assignment_method = settings.density_assignment_method;
322 const bool bispectrum_interlacing = settings.interlacing;
323 const bool bispectrum_subtract_shotnoise = settings.subtract_shotnoise;
326 const double kmin = 0.0;
327 const double kmax = 2.0 * M_PI * bispectrum_nmesh / 2;
334 std::cout <<
"#=====================================================\n";
335 std::cout <<
"# Computing bispectrum of particles\n";
336 std::cout <<
"# kmin (h/Mpc) : " << kmin / simulation_boxsize <<
"\n";
337 std::cout <<
"# kmax (h/Mpc) : " <<
kmax / simulation_boxsize <<
"\n";
338 std::cout <<
"# deltak (h/Mpc) : "
339 << (
kmax - kmin) / simulation_boxsize / bispectrum_nbins <<
"\n";
340 std::cout <<
"# bispectrum_nmesh : " << bispectrum_nmesh <<
"\n";
341 std::cout <<
"# bispectrum_nbins : " << bispectrum_nbins <<
"\n";
342 std::cout <<
"# bispectrum_density_assignment_method : " << bispectrum_density_assignment_method <<
"\n";
343 std::cout <<
"# bispectrum_interlacing : " << bispectrum_interlacing <<
"\n";
344 std::cout <<
"# bispectrum_subtract_shotnoise : " << bispectrum_subtract_shotnoise <<
"\n";
345 std::cout <<
"#=====================================================\n";
353 bispectrum_density_assignment_method,
354 bispectrum_interlacing);
355 bofk.
scale(simulation_boxsize);
359 std::string
filename = snapshot_folder +
"/bispectrum_z" + redshiftstring +
".txt";
361 if (not fp.is_open()) {
362 std::cout <<
"Warning: Cannot write bispectrum to file, failed to open [" <<
filename <<
"]\n";
364 fp <<
"# i1 i2 i3 k1 (h/Mpc) k2 (h/Mpc) k3 (h/Mpc) B(k1,k2,k3) "
365 "(Mpc/h)^6 Q(k1,k2,k3) N(k1,k2,k3) \n";
366 for (
int i = 0; i < bispectrum_nbins; i++) {
367 for (
int j = 0; j < bispectrum_nbins; j++) {
368 for (
int k = 0; k < bispectrum_nbins; k++) {
369 double k1 = bofk.
kmean[i];
370 double k2 = bofk.
kmean[j];
371 double k3 = bofk.
kmean[k];
375 fp << std::setw(3) << i <<
" ";
376 fp << std::setw(3) << j <<
" ";
377 fp << std::setw(3) << k <<
" ";
378 fp << std::setw(15) << k1 <<
" ";
379 fp << std::setw(15) << k2 <<
" ";
380 fp << std::setw(15) << k3 <<
" ";
381 fp << std::setw(15) << B123 <<
" ";
382 fp << std::setw(15) << Q123 <<
" ";
383 fp << std::setw(15) << N123 <<
"\n";
394template <
int NDIM,
class T>
405 const double a = 1.0 / (1.0 + redshift);
406 const double aini = 1.0 / (1.0 + ic_initial_redshift);
409 options.noperators = settings.noperators;
410 options.density_assignment_method = settings.density_assignment_method;
411 options.interlacing = settings.interlacing;
412 options.subtract_shotnoise = settings.subtract_shotnoise;
414 options.smoothing_method = settings.smoothing_filter;
420 std::cout <<
"#=====================================================\n";
421 std::cout <<
"# Computing hybrid bias spectra\n";
422 std::cout <<
"# hybridbias_nmesh : " << settings.nmesh <<
"\n";
423 std::cout <<
"# hybridbias_noperators : " << settings.noperators <<
"\n";
424 std::cout <<
"# hybridbias_density_assignment_method : " << settings.density_assignment_method <<
"\n";
425 std::cout <<
"# hybridbias_interlacing : " << settings.interlacing <<
"\n";
426 std::cout <<
"# hybridbias_subtract_shotnoise : " << settings.subtract_shotnoise <<
"\n";
427 std::cout <<
"# hybridbias_smoothing_scale (Mpc/h) : " << settings.smoothing_scale <<
"\n";
428 std::cout <<
"# hybridbias_kcut_over_nyquist : " << settings.kcut_over_nyquist <<
"\n";
429 std::cout <<
"# hybridbias_zeldovich : " << settings.zeldovich <<
"\n";
430 std::cout <<
"# hybridbias_oneloop : " << settings.oneloop <<
"\n";
431 std::cout <<
"#=====================================================\n";
435 auto growth_factor_of_kBox = [&](
double kBox) {
438 const double k = kBox /
boxsize;
451 growth_factor_of_kBox);
455 std::vector<double> pofk_linear(spectra.
pofk[0].n, 0.0);
456 for (
int i = 0; i < spectra.
pofk[0].n; i++) {
457 const double k = spectra.
pofk[0].kbin[i];
459 const double D =
grav->get_D_1LPT(a, k /
grav->H0_hmpc);
460 const double Dini =
grav->get_D_1LPT(
aini, k /
grav->H0_hmpc);
463 pofk_linear[i] =
transferdata->get_cdm_baryon_power_spectrum(k, a);
467 std::ostringstream header;
468 header <<
"# Hybrid bias spectra at z = " << redshift <<
" (boxsize " <<
boxsize <<
" Mpc/h, Npart_1D " <<
Npart_1D
469 <<
", nmesh " << settings.nmesh <<
", " << settings.density_assignment_method
470 << (settings.interlacing ?
" with interlacing" :
"") <<
", shot noise subtracted: " << std::boolalpha
471 << settings.subtract_shotnoise <<
")\n";
472 header <<
"# The tracer power spectrum is P_tt = sum_ab b_a b_b P_ab with b = (1, b1, b2, bs, bnabla) for the "
473 "operators (1, delta_L, delta_L^2 - <delta_L^2>, s^2 - <s^2>, nabla^2 delta_L)\n";
476 {{
"Pcb_linear(k)", pofk_linear}});
482 if (not settings.zeldovich and not settings.oneloop)
487 <<
"Warning: no linear P(k) so we cannot compute the LPT predictions of the hybrid bias spectra\n";
491 auto pofk_linear_of_k = [&](
double k) {
492 if (k < krange.first or k > krange.second)
494 const double D =
grav->get_D_1LPT(a, k /
grav->H0_hmpc);
495 const double Dini =
grav->get_D_1LPT(
aini, k /
grav->H0_hmpc);
499 const double smoothing_scale = settings.smoothing_scale;
500 const std::string filter = settings.smoothing_filter;
501 auto W_operators = [&](
double k) {
504 if (smoothing_scale <= 0.0)
506 const double kR =
k * smoothing_scale;
507 if (filter ==
"gaussian")
508 return std::exp(-0.5 * kR * kR);
509 if (filter ==
"sharpk")
510 return kR < 1.0 ? 1.0 : 0.0;
512 return kR < 1
e-4 ? 1.0 : 3.0 * (std::sin(kR) - kR * std::cos(kR)) / (kR * kR * kR);
515 const double kcut_displacement = M_PI * sim.get_parameters().ic.nmesh /
boxsize;
516 auto W_displacement = [&](
double k) {
return k < kcut_displacement ? 1.0 : 0.0; };
521 const auto & kbin = spectra.pofk[0].kbin;
523 const auto modes = modes_in_bins<NDIM>(spectra.pofk[0], 2.0 * M_PI * 16);
524 double kmode_min = std::numeric_limits<double>::max(), kmode_max = 0.0;
525 for (
int i = 0;
i <
nbins;
i++)
526 for (
auto & m :
modes[
i]) {
527 kmode_min = std::min(kmode_min,
m.first /
boxsize);
528 kmode_max = std::max(kmode_max,
m.first /
boxsize);
531 const int ntable = kmode_max > 0.0 ? 40 : 0;
532 std::vector<double> kall;
533 for (
int j = 0; j < ntable; j++)
534 kall.push_back(std::exp(std::log(kmode_min * 0.999) +
535 std::log(kmode_max * 1.001 / (kmode_min * 0.999)) * j /
double(ntable - 1)));
536 std::vector<int> jbin(nbins, -1);
537 for (
int i = 0;
i <
nbins;
i++)
538 if (modes[i].empty() and kbin[i] > 0.0) {
539 jbin[
i] =
int(kall.size());
540 kall.push_back(kbin[i]);
543 std::vector<double> klocal;
544 std::vector<int> jlocal;
545 for (
int j = 0; j <
int(kall.size()); j++)
547 klocal.push_back(kall[j]);
552 auto write_prediction = [&](
const std::vector<std::vector<double>> & prediction,
553 const std::string &
label,
555 double kcut_displacement_used) {
556 auto theory = spectra;
557 for (
size_t p = 0;
p < theory.pofk.size();
p++) {
558 std::vector<double> all(kall.size(), 0.0);
559 for (
size_t j = 0; j < jlocal.size(); j++)
560 all[jlocal[j]] = prediction[p][j];
562 std::vector<double> logk_table(ntable), p_table(ntable);
563 for (
int j = 0; j < ntable; j++) {
564 logk_table[j] = std::log(kall[j]);
570 for (
int i = 0;
i <
nbins;
i++) {
572 if (not modes[i].empty()) {
574 for (
auto & m :
modes[
i]) {
579 }
else if (jbin[i] >= 0) {
582 theory.pofk[
p].pofk[
i] =
value;
584 theory.shotnoise[
p] = 0.0;
586 std::ostringstream
header;
587 header <<
"# " <<
description <<
" of the hybrid bias spectra at z = " << redshift
588 <<
" (linear P_cb(k,z), operators cut at k = " << kcut <<
" h/Mpc"
589 << (smoothing_scale > 0.0 ?
590 " and smoothed with R = " + std::to_string(smoothing_scale) +
" Mpc/h " + filter :
592 <<
", displacement cut at k = " << kcut_displacement_used <<
" h/Mpc). Same columns as hybridbias_z"
594 <<
".txt (the shot noise is not included; for k < 16 k_fundamental the prediction is averaged over "
595 "the modes in each bin)\n";
596 theory.write(snapshot_folder +
"/hybridbias_" + label +
"_z" +
redshift_to_string(redshift) +
".txt",
598 {{
"Pcb_linear(k)", pofk_linear}});
604 if (settings.zeldovich) {
605 zoptions.
kmax_integration = std::min(std::max(kcut, kcut_displacement), krange.second);
607 write_prediction(zeldovich,
"zeldovich",
"Zel'dovich prediction", kcut_displacement);
609 if (settings.oneloop) {
611 if (
FML::ThisTask == 0 and kcut_displacement > kcut * 1.0001)
612 std::cout <<
"Note: the one-loop prediction uses the operator cut k = " << kcut
613 <<
" h/Mpc also for the displacement field (the IC have modes up to " << kcut_displacement
616 if (settings.oneloop_kernels ==
"model") {
618 const double OmegaM = sim.get_cosmology()->get_OmegaM();
625 }
else if (
grav->get_name() !=
"GR") {
626 const double H0_hmpc =
grav->H0_hmpc;
628 [
grav, H0_hmpc](
double k,
double aa) {
return grav->GeffOverG(aa, k / H0_hmpc); });
632 std::cout <<
"One-loop prediction with the LPT kernels of " <<
description <<
"\n";
635 static std::shared_ptr<MGLoopTables> mg_tables;
636 static std::vector<double> mg_tables_redshifts;
638 mg_tables_redshifts = sim.get_parameters().output.redshifts;
639 std::vector<double> aouts;
640 std::vector<std::function<double(
double)>> pofks;
641 for (
double z : mg_tables_redshifts) {
642 const double aa = 1.0 / (1.0 +
z);
644 pofks.push_back([&, aa](
double k) {
645 if (k < krange.first or k > krange.second)
647 const double D =
grav->get_D_1LPT(aa, k /
grav->H0_hmpc);
648 const double Dini =
grav->get_D_1LPT(
aini, k /
grav->H0_hmpc);
652 mg_tables = std::make_shared<MGLoopTables>(
mg_loop_tables(model, aouts, pofks, W_operators, zoptions));
655 for (
size_t i = 0;
i < mg_tables_redshifts.size();
i++)
656 if (std::abs(mg_tables_redshifts[i] - redshift) < 1e-6 * (1.0 + redshift))
658 std::vector<std::vector<double>> oneloop;
668 {1.0 / (1.0 + redshift)},
673 write_prediction(oneloop,
675 "One-loop LPT (CLEFT, no counterterms; the LPT kernels of " +
description +
") prediction",
679 write_prediction(oneloop,
"oneloop",
"One-loop LPT (CLEFT, no counterterms; EdS kernels) prediction", kcut);
685template <
int NDIM,
class T>
686std::vector<FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning<NDIM>>
693 const int pofk_multipole_nmesh = settings.nmesh;
694 const std::string pofk_multipole_density_assignment_method = settings.density_assignment_method;
695 const bool pofk_multipole_interlacing = settings.interlacing;
696 const bool pofk_multipole_subtract_shotnoise = settings.subtract_shotnoise;
697 const int pofk_multipole_ellmax = settings.ellmax;
704 const double a = 1.0 / (1.0 + redshift);
705 const double velocity_to_displacement = 1.0 / (a * a *
cosmo->HoverH0_of_a(a));
709 std::cout <<
"#=====================================================\n";
710 std::cout <<
"# Computing power-spectrum multipoles of particles\n";
711 std::cout <<
"# pofk_multipole_nmesh : " << pofk_multipole_nmesh <<
"\n";
712 std::cout <<
"# pofk_multipole_density_assignment_method : " << pofk_multipole_density_assignment_method
714 std::cout <<
"# pofk_multipole_interlacing : " << pofk_multipole_interlacing <<
"\n";
715 std::cout <<
"# pofk_multipole_subtract_shotnoise : " << pofk_multipole_subtract_shotnoise <<
"\n";
716 std::cout <<
"#=====================================================\n";
719 std::vector<FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning<NDIM>> Pells(pofk_multipole_ellmax + 1,
720 pofk_multipole_nmesh / 2);
721 Pells[0].subtract_shotnoise = pofk_multipole_subtract_shotnoise;
724 velocity_to_displacement,
726 pofk_multipole_density_assignment_method,
727 pofk_multipole_interlacing);
728 for (
auto & pl : Pells)
729 pl.scale(simulation_boxsize);
734 auto pofk_cb = [&](
double k) {
737 double D =
grav->get_D_1LPT(1.0 / (1.0 + redshift), k /
grav->H0_hmpc);
738 double Dini =
grav->get_D_1LPT(1.0 / (1.0 + ic_initial_redshift), k /
grav->H0_hmpc);
739 return pofk_ini * std::pow(D / Dini, 2);
741 return transferdata->get_cdm_baryon_power_spectrum(k, 1.0 / (1.0 + redshift));
745 auto kvals = Pells[0].kbin;
746 auto pofk_cb_linear = kvals;
747 for (
auto & v : pofk_cb_linear) {
754 std::string
filename = snapshot_folder +
"/pofk_multipole_z" + redshiftstring +
".txt";
756 if (not fp.is_open()) {
757 std::cout <<
"Warning: Cannot write power-spectrum multipoles to file, failed to open [" <<
filename
760 fp <<
"# k (h/Mpc) P0(k) (Mpc/h)^3 P2(k) (Mpc/h)^3 P4(k) (Mpc/h)^3 ... "
761 "P0_kaiser P2_kaiser P4_kaiser ShotnoiseSubtracted = "
762 << std::boolalpha << pofk_multipole_subtract_shotnoise <<
"\n";
763 for (
int i = 0; i < Pells[0].n; i++) {
764 const double k = Pells[0].kbin[i];
765 const double f =
grav->get_f_1LPT(1.0 / (1.0 + redshift), k /
grav->H0_hmpc);
766 fp << std::setw(15) << k <<
" ";
767 for (
size_t ell = 0; ell < Pells.size(); ell += 2)
768 fp << std::setw(15) << Pells[ell].pofk[i] <<
" ";
771 fp << std::setw(15) << pofk_cb_linear_spline(k) * (1.0 + 2.0 / 3.0 *
f +
f *
f / 5.0) <<
" ";
772 fp << std::setw(15) << pofk_cb_linear_spline(k) * (4.0 / 3.0 *
f + 4.0 / 7.0 *
f *
f) <<
" ";
773 fp << std::setw(15) << pofk_cb_linear_spline(k) * (8.0 / 35.0 *
f *
f) <<
" ";
783template <
int NDIM,
class T>
791 const int pofk_nmesh = settings.nmesh;
792 const std::string pofk_density_assignment_method = settings.density_assignment_method;
793 const bool pofk_interlacing = settings.interlacing;
794 const bool pofk_subtract_shotnoise = settings.subtract_shotnoise;
803 std::cout <<
"#=====================================================\n";
804 std::cout <<
"# Computing power-spectrum of particles\n";
805 std::cout <<
"# pofk_nmesh : " << pofk_nmesh <<
"\n";
806 std::cout <<
"# pofk_density_assignment_method : " << pofk_density_assignment_method <<
"\n";
807 std::cout <<
"# pofk_interlacing : " << pofk_interlacing <<
"\n";
808 std::cout <<
"# pofk_subtract_shotnoise : " << pofk_subtract_shotnoise <<
"\n";
809 std::cout <<
"#=====================================================\n";
818 std::vector<std::pair<std::string, FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning<NDIM>>> pofk_species;
826 const auto nleftright =
831 nleftright.second + (pofk_interlacing ? 1 : 0));
832 if (pofk_interlacing) {
837 pofk_density_assignment_method);
843 pofk_density_assignment_method);
852 if (pofk_subtract_shotnoise)
853 for (
int i = 0; i < binning.
n; i++)
854 binning.
pofk[i] -= shotnoise;
855 binning.
scale(simulation_boxsize);
861 double weight_first = 1.0;
867 delta_cb = std::move(delta);
868 weight_first =
info.weight_cb;
872 icb == 1 ? weight_first : 1.0,
883 const double a = 1.0 / (1.0 + redshift);
884 typename std::remove_reference_t<
decltype(sim.
get_neutrinos())>::ScopedEnergyWeights energy_weights(
890 delta_nu.set_fourier_from_index(0, 0.0);
891 const double fnu =
cosmo->get_fMNu();
893 pofk_species.push_back({
"nu", bin(delta_nu, nu_shot)});
896 const double norm = 1.0 / (1.0 - fnu + fnu * e);
900 pofk_species.push_back({
"total", bin(delta_cb, shot)});
903 pofk_species.push_back({
"total", bin(delta_cb, sim.
total_shotnoise())});
927 auto pofk_cb = [&](
double k) {
930 double D =
grav->get_D_1LPT(1.0 / (1.0 + redshift), k /
grav->H0_hmpc);
931 double Dini =
grav->get_D_1LPT(1.0 / (1.0 + ic_initial_redshift), k /
grav->H0_hmpc);
932 return pofk_ini * std::pow(D / Dini, 2);
934 return transferdata->get_cdm_baryon_power_spectrum(k, 1.0 / (1.0 + redshift));
938 auto kvals = pofk_cb_binning.
kbin;
939 auto pofk_cb_linear = kvals;
940 for (
auto & v : pofk_cb_linear) {
946 double nu_shotnoise = 0.0;
948 typename std::remove_reference_t<
decltype(sim.
get_neutrinos())>::ScopedEnergyWeights energy_weights(
950 1.0 / (1.0 + redshift),
952 nu_shotnoise = sim.
get_neutrinos().shotnoise() * std::pow(simulation_boxsize,
NDIM);
961 cosmo->get_nonlinear_matter_power_spectrum_boost(redshift);
963 std::string
filename = snapshot_folder +
"/pofk_z" + redshiftstring +
".txt";
965 if (not fp.is_open()) {
966 std::cout <<
"Warning: Cannot write power-spectrum to file, failed to open [" <<
filename <<
"]\n";
968 fp <<
"# k (h/Mpc) P(k) (Mpc/h)^3 P_linear(k) P_NL(k) (Mpc/h)^3\n";
969 for (
int i = 0; i < pofk_cb_binning.
n; i++) {
970 const double k = pofk_cb_binning.
kbin[i];
971 double boost = non_linear_boost_of_k ? non_linear_boost_of_k(k) : 1.0;
972 fp << std::setw(15) << k <<
" ";
973 fp << std::setw(15) << pofk_cb_binning.
pofk[i] <<
" ";
974 fp << std::setw(15) << pofk_cb_linear_spline(k) <<
" ";
975 fp << std::setw(15) << pofk_cb_linear_spline(k) * boost <<
" ";
981 const double a = 1.0 / (1.0 + redshift);
982 for (
auto & sp : pofk_species) {
983 std::string fname = snapshot_folder +
"/pofk_" + sp.first +
"_z" + redshiftstring +
".txt";
984 std::ofstream fps(fname.c_str());
985 fps <<
"# k (h/Mpc) P_" << sp.first <<
"(k) (Mpc/h)^3 P_" << sp.first <<
"_linear(k)";
986 if (sp.first ==
"nu")
987 fps <<
" [shot noise of the neutrino particles sum w^2/N^2 V = " << nu_shotnoise
988 <<
" (Mpc/h)^3, subtracted: " << std::boolalpha << pofk_subtract_shotnoise <<
"]";
990 for (
int i = 0; i < sp.second.n; i++) {
991 const double k = sp.second.kbin[i];
995 ratio = std::pow(Ti /
transferdata->get_cdm_baryon_transfer_function(k, a), 2);
997 fps << std::setw(15) << k <<
" " << std::setw(15) << sp.second.pofk[i] <<
" " << std::setw(15)
998 << pofk_cb_linear_spline(k) * ratio <<
"\n";
1003 return pofk_cb_binning;
void compute_hybrid_bias_spectra(NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
The hybrid bias (Lagrangian bias basis) spectra P_ab(k) of the operators 1, delta_L,...
void output_pofk_for_every_step(const NBodySimulation< NDIM, T > &sim)
FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< NDIM > compute_power_spectrum(NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
Returns the baryon+CDM P(k) (also written to file)
std::vector< FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< NDIM > > compute_power_spectrum_multipoles(NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
Returns P_ell(k) for ell = 0,1,...,ellmax (also written to file)
void output_gadget_hdf5(NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
Output the particles as GADGET HDF5 files: one file per task, <prefix>.
std::vector< FML::FILEUTILS::GADGET::GadgetSpecies > gadget_species(NBodySimulation< NDIM, T > &sim)
The particle species for the GADGET writers (the types and masses are set in Species....
void output_gadget(NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
void compute_bispectrum(NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
const std::string filename
std::string redshift_to_string(double redshift)
The redshift formatted as used in output filenames.
void info(HaloModel **hmp)
Dense results by default; COMPACT stores one slot per canonical tuple.
double get_bincount(const std::array< int, ORDER > &ik)
Returns the volume factor N123.
std::vector< double > kmean
kmean is the mean of wavenumbers added to the bin
void scale(const double boxsize)
To physical units: k *= 1/boxsize and P *= boxsize^((ORDER-1) NDIM)
double get_spectrum(const std::array< int, ORDER > &ik)
Get the polyspectra at (k[ik1], k[ik2], ...)
bool subtract_shotnoise
Subtract shotnoise? Algorithms (compute_polyspectrum) using this class checks this value.
double get_reduced_spectrum(const std::array< int, ORDER > &ik)
Get the reduced polyspectrum: P / fac where fac = sqrt(P(k1)P(k2)) for order=2 and P(k1)P(k2) + cyc f...
Class for holding the results after binning up a power-spectrum.
bool subtract_shotnoise
Subtract shotnoise or not (used by algorithms)
std::vector< double > kbin
The mean k-value in the bin.
void scale(const double boxsize)
Scale to physical units: k *= 1/Box and pofk *= Box^N.
std::vector< double > pofk
The power-spectrum in the bin.
Write GADGET HDF5 snapshots.
Write files in GADGET format.
Class for holding grids and performing real-to-complex and complex-to-real FFTs using FFTW with MPI.
This is a wrapper class for easy use of GSL splines It is (OpenMP) thread safe as long as splines are...
std::pair< double, double > get_xrange() const
Get the range the spline was created on.
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.
size_t get_npart() const
Number of active particles on the local task.
Vector< T > & get_particles()
Get reference to particle vector.
A particle-mesh N-body simulation (optionally with COLA).
std::shared_ptr< GravityModel< NDIM > > grav
Everything related to gravity: growth factors, computing forces.
Spline power_initial_spline
void for_each_species(F &&f)
Call f(particles, info) for all the species (the particle types differ, so use a generic lambda)
const auto & get_pofk_total_every_step() const
SIMCODE::NeutrinoParticles< NDIM > & get_neutrinos()
const FFTWGrid< NDIM > & get_hybridbias_linear_field() const
The linear density field delta_cb(zini,k) of the initial conditions (only if hybridbias)
std::vector< SIMCODE::SpeciesInfo > species
The particle species (see Species.h): "cb", or "cdm" and "baryon", and "nu".
const Parameters & get_parameters() const
std::shared_ptr< GravityModel< NDIM > > get_gravity_model_ic() const
double neutrino_energy_factor(double a) const
The mean energy per mass of the neutrinos <E/m>(a) with force_neutrino_energy_density,...
std::vector< std::pair< double, PowerSpectrumBinning > > pofk_cb_every_step
std::vector< std::pair< double, PowerSpectrumBinning > > pofk_total_every_step
double species_shotnoise(const SIMCODE::SpeciesInfo &info)
The shot noise of the density field of a species (in units of the box volume): 1/N (sum w^2 / N^2 for...
bool has_baryons() const
Separate CDM and baryon particles? Then get_particles() are the CDM particles.
double total_shotnoise(double a=-1.0)
The shot noise of the total delta_m = (1 - f_nu) delta_cb + f_nu delta_nu with neutrino particles (wi...
double cb_shotnoise()
The shot noise of delta_cb = sum_i w_i delta_i: sum_i w_i^2 / N_i.
std::shared_ptr< Cosmology > get_cosmology() const
bool has_neutrino_particles() const
Massive neutrinos as particles (delta-f method)?
const auto & get_pofk_cb_every_step() const
const Spline & get_power_initial_spline() const
std::shared_ptr< GravityModel< NDIM > > get_gravity_model() const
MPIParticles part
All the particles (CDM+baryons as one species "cb", or the CDM particles if particle_baryons)
const auto & get_pofk_species_every_step() const
std::shared_ptr< LinearTransferData > transferdata
Linear perturbations, transfer functions, initial pofk This is only used if ic_type_of_input == trans...
void for_each_cb_species(F &&f)
Call f(particles, info) for the CDM+baryon species ("cb", or "cdm" and "baryon"; all of type T)
const std::vector< SIMCODE::SpeciesInfo > & get_species() const
The particle species (in the order of the IDs)
std::shared_ptr< Cosmology > cosmo
Everything related to the background evolution.
std::shared_ptr< LinearTransferData > get_transferdata() const
bool neutrino_energy_density() const
Use the energy density of the neutrinos (force_neutrino_energy_density)
std::shared_ptr< GravityModel< NDIM > > grav_ic
The gravity model used to make the IC (GR if ic_use_gravity_model_GR, otherwise grav)
MPIParticles & get_particles()
SIMCODE::NeutrinoParticles< NDIM >::Container & get_neutrino_particles()
Hybrid bias (Lagrangian bias basis) spectra, Modi, Chen & White 2020 (1910.07097),...
MGModel mgmodel_dgp(double OmegaM, double H0rc, bool screening=true)
MGModel mgmodel_gr(double OmegaM)
Convenience constructors.
MGLoopTables mg_loop_tables(const MGModel &model, const std::vector< double > &aout, const std::vector< std::function< double(double)> > &pofk, const std::function< double(double)> &W, const ZeldovichOptions &options, const MGOneLoopSettings &settings={})
The loop functions of oneloop_hybrid_bias_spectra_mg for several output times at once (the expensive ...
MGModel mgmodel_fofr(double OmegaM, double fofr0, double nfofr=1.0, bool screening=true)
std::vector< std::vector< double > > oneloop_hybrid_bias_spectra_mg_from_tables(const MGLoopTables &tab, int io, const std::function< double(double)> &pofk, const std::function< double(double)> &W, const std::vector< double > &kvalues, const ZeldovichOptions &options, std::vector< std::string > *names=nullptr, std::vector< std::array< int, 2 > > *pairs=nullptr)
The one-loop prediction for output number io of the tables (pofk the linear P(k) at that output,...
MGModel mgmodel_mu(double OmegaM, std::function< double(double, double)> mu_of_k_hmpc_a)
std::vector< std::vector< double > > oneloop_hybrid_bias_spectra(const std::function< double(double)> &pofk, const std::function< double(double)> &W, const std::vector< double > &kvalues, const ZeldovichOptions &options, std::vector< std::string > *names=nullptr, std::vector< std::array< int, 2 > > *pairs=nullptr)
The one-loop (CLEFT) prediction for the hybrid bias spectra.
std::vector< std::vector< double > > zeldovich_hybrid_bias_spectra(const std::function< double(double)> &pofk, const std::function< double(double)> &W_operators, const std::function< double(double)> &W_displacement, const std::vector< double > &kvalues, const ZeldovichOptions &options, std::vector< std::string > *names=nullptr, std::vector< std::array< int, 2 > > *pairs=nullptr)
The Zel'dovich prediction for the hybrid bias spectra.
std::vector< std::vector< std::vector< double > > > oneloop_hybrid_bias_spectra_mg(const MGModel &model, const std::vector< double > &aout, const std::vector< std::function< double(double)> > &pofk, const std::function< double(double)> &W, const std::vector< double > &kvalues, const ZeldovichOptions &options, const MGOneLoopSettings &settings={}, std::vector< std::string > *names=nullptr, std::vector< std::array< int, 2 > > *pairs=nullptr)
The one-loop LPT prediction of the hybrid bias spectra in modified gravity with the full scale-depend...
void compute_hybrid_bias_spectra(const T *p, size_t NumPart, size_t NumPartTot, const FFTWGrid< N > &delta_linear_fourier, int Nlattice, int Nmesh, const HybridBiasOptions &options, HybridBiasSpectra< N > &spectra, std::function< double(double)> growth_factor_of_kBox=nullptr, detail::LatticeCoordinateFunction< N, T > lattice_coordinate=nullptr, std::vector< FFTWGrid< N > > *advected_out=nullptr)
Compute the hybrid bias spectra from particles and the linear density field.
void bin_up_power_spectrum(const FFTWGrid< N > &fourier_grid, PowerSpectrumBinning< N > &pofk)
Compute the power-spectrum of a fourier grid.
void compute_power_spectrum_multipoles(int Ngrid, FML::PARTICLE::MPIParticles< T > &part, double velocity_to_displacement, std::vector< PowerSpectrumBinning< N > > &Pell, std::string density_assignment_method, bool interlacing)
A simple power-spectrum estimator for multipoles in simulations.
void compute_bispectrum(int Ngrid, T *part, size_t NumPart, size_t NumPartTotal, BispectrumBinning< N, COMPACT > &bofk, std::string density_assignment_method, bool interlacing)
Computes the bispectrum from particles.
GadgetSpecies make_gadget_species(int type, T *part, size_t npart, size_t npart_total, double mass_fraction)
A GadgetSpecies for an array of particles of class T (with get_pos, and optionally get_vel and get_id...
void linear_combination_fourier(FFTWGrid< N > &a, const FFTWGrid< N > &b, double wa, double wb)
The linear combination a -> wa a + wb b of two grids in fourier space (same layout)
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 ...
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.
void deconvolve_window_function_fourier(FFTWGrid< N > &fourier_grid, std::string density_assignment_method)
Deconvolves the density assignment kernel in Fourier space.
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.
set_mass constexpr double GetMass(Args &... args)
int ThisTask
The MPI task number.
void SumArrayOverTasks(T *value, size_t n)
Inplace sum over tasks of a contiguous array of values.
int NTasks
Total number of MPI tasks.
double species_transfer_function(const std::string &name, const FML::FILEUTILS::LinearTransferData &transferdata, double k_hmpc, double a)
The transfer function of a species from the tables ("total" for the total matter)
FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< NDIM > measure_snapshot_power_spectrum(P *particles, size_t count, size_t total, const PofkSettings &settings, double boxsize)
The same estimator for live single-species outputs and temporary analysis snapshots.
The result: the cross spectra of the advected operators for all pairs a <= b.
void write(const std::string &filename, const std::string &header="", const std::vector< std::pair< std::string, std::vector< double > > > &extra_columns={}) const
Write all the spectra to file (only task 0): k, the number of modes, P_ab for all pairs and then the ...
void scale(double boxsize)
To physical units: k -> k / B, P -> B^N P, and 1/B^2 for every nabla^2 delta in the pair.
std::vector< PowerSpectrumBinning< N > > pofk
One-loop LPT (CLEFT) prediction of the hybrid bias spectra in modified gravity with the full scale- a...
The Zel'dovich (resummed 1LPT) prediction for the hybrid bias basis spectra P_ab(k) of the operators ...
int noperators
How many of the operators (1, delta, delta2, s2, nabla2delta) to use.
double kmin_integration
The range of the k-integrals over P(k) (kmax: where W_op and W_disp are zero, e.g....
ParticleSettings particle
SimulationSettings simulation
PofkMultipoleSettings pofk_multipole
BispectrumSettings bispectrum
HybridBiasSettings hybridbias
What the simulation needs to know about a particle species (one particle container each): cb : CDM an...
ComplexType value(const std::array< int, N > &wave, int generation)
std::vector< P > particles(bool empty_rank)