FML
Loading...
Searching...
No Matches
AnalyzeOutput.h File Reference

Go to the source code of this file.

Functions

template<int NDIM, class T >
void output_pofk_for_every_step (const NBodySimulation< NDIM, T > &sim)
 
template<int NDIM, class T >
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.h): CDM (or CDM+baryons) as type 1, baryons as type 2, neutrinos as type 3 (with their delta-f weights as an extra field in HDF5).
 
template<int NDIM, class T >
void output_gadget (NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
 
template<int NDIM, class T >
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>.
 
template<int NDIM, class T >
void compute_bispectrum (NBodySimulation< NDIM, T > &sim, double redshift, std::string snapshot_folder)
 
template<int NDIM, class T >
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, delta_L^2, s^2 and nabla^2 delta_L advected with the CDM+baryon particles (written to file).
 
template<int NDIM, class T >
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)
 
template<int NDIM, class T >
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)
 

Function Documentation

◆ compute_bispectrum()

template<int NDIM, class T >
void compute_bispectrum ( NBodySimulation< NDIM, T > & sim,
double redshift,
std::string snapshot_folder )

Definition at line 314 of file AnalyzeOutput.h.

314 {
315
316 const std::string redshiftstring = redshift_to_string(redshift);
317 const auto & settings = sim.get_parameters().bispectrum;
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;
324 auto & part = sim.get_particles();
325
326 const double kmin = 0.0;
327 const double kmax = 2.0 * M_PI * bispectrum_nmesh / 2;
329 FML::CORRELATIONFUNCTIONS::BispectrumBinning<NDIM> bofk(kmin, kmax, bispectrum_nbins, bin_type);
330 bofk.subtract_shotnoise = bispectrum_subtract_shotnoise;
331
332 if (FML::ThisTask == 0) {
333 std::cout << "\n";
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";
346 }
347
349 part.get_particles_ptr(),
350 part.get_npart(),
351 part.get_npart_total(),
352 bofk,
353 bispectrum_density_assignment_method,
354 bispectrum_interlacing);
355 bofk.scale(simulation_boxsize);
356
357 // Output to file
358 if (FML::ThisTask == 0) {
359 std::string filename = snapshot_folder + "/bispectrum_z" + redshiftstring + ".txt";
360 std::ofstream fp(filename.c_str());
361 if (not fp.is_open()) {
362 std::cout << "Warning: Cannot write bispectrum to file, failed to open [" << filename << "]\n";
363 } else {
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];
372 double B123 = bofk.get_spectrum(i, j, k);
373 double Q123 = bofk.get_reduced_spectrum(i, j, k);
374 double N123 = bofk.get_bincount(i, j, 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";
384 }
385 }
386 }
387 }
388 }
389}
const std::string filename
Definition Main.cpp:63
std::string redshift_to_string(double redshift)
The redshift formatted as used in output filenames.
Definition OutputUtils.h:7
Dense results by default; COMPACT stores one slot per canonical tuple.
const Parameters & get_parameters() const
Definition Simulation.h:197
MPIParticles & get_particles()
Definition Simulation.h:198
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.
int ThisTask
The MPI task number.
Definition Global.cpp:39
const double kmax
Definition test_fnl.cpp:43

◆ compute_hybrid_bias_spectra()

template<int NDIM, class T >
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, delta_L^2, s^2 and nabla^2 delta_L advected with the CDM+baryon particles (written to file).

delta_L is the linear field of the initial conditions scaled to the output redshift with the 1LPT growth factor D(k,z) / D(k,zini) of the gravity model

Definition at line 395 of file AnalyzeOutput.h.

395 {
397 const auto & settings = sim.get_parameters().hybridbias;
398 const double boxsize = sim.get_parameters().simulation.boxsize;
400 const int Npart_1D = sim.get_parameters().particle.Npart_1D;
401 const auto grav = sim.get_gravity_model();
402 const auto transferdata = sim.get_transferdata();
403 const auto & power_initial_spline = sim.get_power_initial_spline();
404 auto & part = sim.get_particles();
405 const double a = 1.0 / (1.0 + redshift);
406 const double aini = 1.0 / (1.0 + ic_initial_redshift);
407
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;
413 options.smoothing_scale = settings.smoothing_scale / boxsize;
414 options.smoothing_method = settings.smoothing_filter;
415 // The spherical cut of the linear field (in units of 1/box)
416 options.kcut = settings.kcut_over_nyquist * M_PI * Npart_1D;
417
418 if (FML::ThisTask == 0) {
419 std::cout << "\n";
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";
432 }
433
434 // delta_L(k,z) = D(k,z) / D(k,zini) delta_L(k,zini)
435 auto growth_factor_of_kBox = [&](double kBox) {
436 if (kBox <= 0.0)
437 return 1.0;
438 const double k = kBox / boxsize;
439 return grav->get_D_1LPT(a, k / grav->H0_hmpc) / grav->get_D_1LPT(aini, k / grav->H0_hmpc);
440 };
441
444 part.get_npart(),
445 part.get_npart_total(),
447 Npart_1D,
448 settings.nmesh,
449 options,
450 spectra,
451 growth_factor_of_kBox);
452 spectra.scale(boxsize);
453
454 // The linear P_cb(k,z) for comparison (= P_{delta,delta} on large scales)
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];
458 if (power_initial_spline) {
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);
461 pofk_linear[i] = power_initial_spline(k) * std::pow(D / Dini, 2);
462 } else if (transferdata) {
463 pofk_linear[i] = transferdata->get_cdm_baryon_power_spectrum(k, a);
464 }
465 }
466
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";
474 spectra.write(snapshot_folder + "/hybridbias_z" + redshift_to_string(redshift) + ".txt",
475 header.str(),
476 {{"Pcb_linear(k)", pofk_linear}});
477
478 //=============================================================
479 // The LPT predictions (Zel'dovich and/or one-loop) with the same linear P(k,z) and the same cut and smoothing of
480 // the operators
481 //=============================================================
482 if (not settings.zeldovich and not settings.oneloop)
483 return;
484 if (not power_initial_spline) {
485 if (FML::ThisTask == 0)
486 std::cout
487 << "Warning: no linear P(k) so we cannot compute the LPT predictions of the hybrid bias spectra\n";
488 return;
489 }
490 const auto krange = power_initial_spline.get_xrange();
491 auto pofk_linear_of_k = [&](double k) {
492 if (k < krange.first or k > krange.second)
493 return 0.0;
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);
496 return power_initial_spline(k) * std::pow(D / Dini, 2);
497 };
498 const double kcut = options.kcut / boxsize;
499 const double smoothing_scale = settings.smoothing_scale;
500 const std::string filter = settings.smoothing_filter;
501 auto W_operators = [&](double k) {
502 if (k >= kcut)
503 return 0.0;
504 if (smoothing_scale <= 0.0)
505 return 1.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;
511 // tophat
512 return kR < 1e-4 ? 1.0 : 3.0 * (std::sin(kR) - kR * std::cos(kR)) / (kR * kR * kR);
513 };
514 // The displacement field of the IC has the modes of the IC grid (we approximate this by a spherical cut)
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; };
517
518 // In the bins at low k with few modes we average the prediction over the modes in the bin (as the measurement
519 // does) instead of evaluating it at the mean k of the bin (this matters for the steep nabla^2 spectra). The
520 // prediction is smooth, so we evaluate it on a log-spaced table spanning these modes and spline it
521 const auto & kbin = spectra.pofk[0].kbin;
522 const int nbins = int(kbin.size());
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);
529 }
530 // The k-values to evaluate the prediction at (in h/Mpc): first the table, then the centers of the other bins
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]);
541 }
542 // Each task computes a part of the k-values
543 std::vector<double> klocal;
544 std::vector<int> jlocal;
545 for (int j = 0; j < int(kall.size()); j++)
546 if (j % FML::NTasks == FML::ThisTask) {
547 klocal.push_back(kall[j]);
548 jlocal.push_back(j);
549 }
550
551 // Bin up the prediction at the local k-values and write it to file
552 auto write_prediction = [&](const std::vector<std::vector<double>> & prediction,
553 const std::string & label,
554 const std::string & description,
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];
561 FML::SumArrayOverTasks(all.data(), all.size());
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]);
565 p_table[j] = all[j];
566 }
568 if (ntable > 0)
569 p_spline = FML::INTERPOLATION::SPLINE::Spline(logk_table, p_table, "Hybrid bias prediction");
570 for (int i = 0; i < nbins; i++) {
571 double value = 0.0;
572 if (not modes[i].empty()) {
573 double wsum = 0.0;
574 for (auto & m : modes[i]) {
575 value += m.second * p_spline(std::log(m.first / boxsize));
576 wsum += m.second;
577 }
578 value /= wsum;
579 } else if (jbin[i] >= 0) {
580 value = all[jbin[i]];
581 }
582 theory.pofk[p].pofk[i] = value;
583 }
584 theory.shotnoise[p] = 0.0;
585 }
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 :
591 std::string(""))
592 << ", displacement cut at k = " << kcut_displacement_used << " h/Mpc). Same columns as hybridbias_z"
593 << redshift_to_string(redshift)
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",
597 header.str(),
598 {{"Pcb_linear(k)", pofk_linear}});
599 };
600
601 ZeldovichOptions zoptions;
602 zoptions.noperators = settings.noperators;
603 zoptions.kmin_integration = std::max(1e-5, krange.first);
604 if (settings.zeldovich) {
605 zoptions.kmax_integration = std::min(std::max(kcut, kcut_displacement), krange.second);
606 auto zeldovich = zeldovich_hybrid_bias_spectra(pofk_linear_of_k, W_operators, W_displacement, klocal, zoptions);
607 write_prediction(zeldovich, "zeldovich", "Zel'dovich prediction", kcut_displacement);
608 }
609 if (settings.oneloop) {
610 // One-loop LPT: the same filter for the operators and the displacement (the operator cut)
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
614 << " h/Mpc)\n";
615 zoptions.kmax_integration = std::min(kcut, krange.second);
616 if (settings.oneloop_kernels == "model") {
617 // The scale- and time-dependent LPT kernels of the gravity model (flat LCDM background)
618 const double OmegaM = sim.get_cosmology()->get_OmegaM();
620 std::string description = "LCDM";
621 if (auto fofr = std::dynamic_pointer_cast<GravityModelFofR<NDIM>>(grav)) {
622 model = mgmodel_fofr(OmegaM, fofr->get_fofr0(), fofr->get_nfofr(), fofr->get_use_screening());
623 } else if (auto dgp = std::dynamic_pointer_cast<GravityModelDGP<NDIM>>(grav)) {
624 model = mgmodel_dgp(OmegaM, dgp->get_rcH0(), dgp->get_use_screening());
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); });
629 }
630 description = model.name();
631 if (FML::ThisTask == 0)
632 std::cout << "One-loop prediction with the LPT kernels of " << description << "\n";
633 // The loop functions are computed for all the outputs at the first output (the kernels are integrated
634 // from early times once)
635 static std::shared_ptr<MGLoopTables> mg_tables;
636 static std::vector<double> mg_tables_redshifts;
637 if (not mg_tables) {
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);
643 aouts.push_back(aa);
644 pofks.push_back([&, aa](double k) {
645 if (k < krange.first or k > krange.second)
646 return 0.0;
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);
649 return power_initial_spline(k) * std::pow(D / Dini, 2);
650 });
651 }
652 mg_tables = std::make_shared<MGLoopTables>(mg_loop_tables(model, aouts, pofks, W_operators, zoptions));
653 }
654 int io = -1;
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))
657 io = int(i);
658 std::vector<std::vector<double>> oneloop;
659 if (io >= 0)
661 io,
662 pofk_linear_of_k,
663 W_operators,
664 klocal,
665 zoptions);
666 else
667 oneloop = oneloop_hybrid_bias_spectra_mg(model,
668 {1.0 / (1.0 + redshift)},
669 {pofk_linear_of_k},
670 W_operators,
671 klocal,
672 zoptions)[0];
673 write_prediction(oneloop,
674 "oneloop",
675 "One-loop LPT (CLEFT, no counterterms; the LPT kernels of " + description + ") prediction",
676 kcut);
677 } else {
678 auto oneloop = oneloop_hybrid_bias_spectra(pofk_linear_of_k, W_operators, klocal, zoptions);
679 write_prediction(oneloop, "oneloop", "One-loop LPT (CLEFT, no counterterms; EdS kernels) prediction", kcut);
680 }
681 }
682}
const int Npart_1D
Definition Main.cpp:50
const double OmegaM
Definition Main.cpp:65
const double aini
Definition Main.cpp:41
std::string description
const double boxsize
Definition TestHDF5.cpp:47
This is a wrapper class for easy use of GSL splines It is (OpenMP) thread safe as long as splines are...
Definition Spline.h:106
const FFTWGrid< NDIM > & get_hybridbias_linear_field() const
The linear density field delta_cb(zini,k) of the initial conditions (only if hybridbias)
Definition Simulation.h:275
std::shared_ptr< Cosmology > get_cosmology() const
Definition Simulation.h:264
const Spline & get_power_initial_spline() const
Definition Simulation.h:268
std::shared_ptr< GravityModel< NDIM > > get_gravity_model() const
Definition Simulation.h:265
std::shared_ptr< LinearTransferData > get_transferdata() const
Definition Simulation.h:267
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 SumArrayOverTasks(T *value, size_t n)
Inplace sum over tasks of a contiguous array of values.
Definition Global.h:248
int NTasks
Total number of MPI tasks.
Definition Global.cpp:40
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.
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....
std::vector< double > redshifts
ComplexType value(const std::array< int, N > &wave, int generation)
Options options()

◆ compute_power_spectrum()

template<int NDIM, class T >
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)

Definition at line 785 of file AnalyzeOutput.h.

785 {
786
787 const std::string redshiftstring = redshift_to_string(redshift);
788 const auto & settings = sim.get_parameters().pofk;
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;
795 const auto transferdata = sim.get_transferdata();
796 const auto & power_initial_spline = sim.get_power_initial_spline();
797 const auto grav = sim.get_gravity_model();
798 const auto & cosmo = grav->get_cosmo();
799 auto & part = sim.get_particles();
800
801 if (FML::ThisTask == 0) {
802 std::cout << "\n";
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";
810 }
811
812 //=============================================================
813 // Compute baryon+CDM power-spectrum
814 //=============================================================
815 FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning<NDIM> pofk_cb_binning(pofk_nmesh / 2);
816 pofk_cb_binning.subtract_shotnoise = pofk_subtract_shotnoise;
817 // Separate CDM and baryon particles: P(k) of each and of delta_cb = w_c delta_c + w_b delta_b
818 std::vector<std::pair<std::string, FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning<NDIM>>> pofk_species;
819 if (sim.get_species().size() == 1) {
820 pofk_cb_binning = SIMCODE::measure_snapshot_power_spectrum<NDIM>(part.get_particles_ptr(),
821 part.get_npart(),
822 part.get_npart_total(),
823 settings,
824 simulation_boxsize);
825 } else {
826 const auto nleftright =
828 auto delta_of = [&](auto & particles) {
830 nleftright.first,
831 nleftright.second + (pofk_interlacing ? 1 : 0));
832 if (pofk_interlacing) {
834 particles.get_npart(),
835 particles.get_npart_total(),
836 grid,
837 pofk_density_assignment_method);
838 } else {
840 particles.get_npart(),
841 particles.get_npart_total(),
842 grid,
843 pofk_density_assignment_method);
844 grid.fftw_r2c();
845 }
847 return grid;
848 };
849 auto bin = [&](const FML::GRID::FFTWGrid<NDIM> & grid, double shotnoise) {
852 if (pofk_subtract_shotnoise)
853 for (int i = 0; i < binning.n; i++)
854 binning.pofk[i] -= shotnoise;
855 binning.scale(simulation_boxsize);
856 return binning;
857 };
858 // delta_cb = sum_i w_i delta_i over the CDM and baryon species (and P(k) of each when there are several)
860 int icb = 0;
861 double weight_first = 1.0;
862 sim.for_each_cb_species([&](auto & particles, const SIMCODE::SpeciesInfo & info) {
863 auto delta = delta_of(particles);
864 if (sim.has_baryons())
865 pofk_species.push_back({info.name, bin(delta, sim.species_shotnoise(info))});
866 if (icb == 0) {
867 delta_cb = std::move(delta);
868 weight_first = info.weight_cb;
869 } else {
871 delta,
872 icb == 1 ? weight_first : 1.0,
873 info.weight_cb);
874 }
875 icb++;
876 });
877 pofk_cb_binning = bin(delta_cb, sim.cb_shotnoise());
878 // Neutrino particles: P_nu(k) and the total P(k) of delta_m = (1 - f_nu) delta_cb + f_nu delta_nu
879 if (sim.has_neutrino_particles()) {
880 // With force_neutrino_energy_density: the energy-density contrast of the neutrinos, and the total
881 // is the energy-density contrast of all matter, ((1 - f_nu) delta_cb + f_nu e delta_nu) / (1 - f_nu +
882 // f_nu e) with e = <E/m>(a)
883 const double a = 1.0 / (1.0 + redshift);
884 typename std::remove_reference_t<decltype(sim.get_neutrinos())>::ScopedEnergyWeights energy_weights(
885 sim.get_neutrinos(),
886 a,
888 auto delta_nu = delta_of(sim.get_neutrino_particles());
889 if (FML::ThisTask == 0)
890 delta_nu.set_fourier_from_index(0, 0.0);
891 const double fnu = cosmo->get_fMNu();
892 const double nu_shot = sim.get_neutrinos().shotnoise();
893 pofk_species.push_back({"nu", bin(delta_nu, nu_shot)});
894 if (sim.neutrino_energy_density()) {
895 const double e = sim.neutrino_energy_factor(a);
896 const double norm = 1.0 / (1.0 - fnu + fnu * e);
897 FML::GRID::linear_combination_fourier<NDIM>(delta_cb, delta_nu, (1.0 - fnu) * norm, fnu * e * norm);
898 const double shot =
899 ((1.0 - fnu) * (1.0 - fnu) * sim.cb_shotnoise() + fnu * fnu * e * e * nu_shot) * norm * norm;
900 pofk_species.push_back({"total", bin(delta_cb, shot)});
901 } else {
902 FML::GRID::linear_combination_fourier<NDIM>(delta_cb, delta_nu, 1.0 - fnu, fnu);
903 pofk_species.push_back({"total", bin(delta_cb, sim.total_shotnoise())});
904 }
905 }
906 }
907
908 /* ...or do it this way for which we can compute the total power-spectrum by adding on the neutrinos
909 const auto nleftright =
910 FML::INTERPOLATION::get_extra_slices_needed_for_density_assignment(pofk_density_assignment_method); const int nleft
911 = nleftright.first; const int nright = nleftright.second + (pofk_interlacing ? 1 : 0); FML::GRID::FFTWGrid<NDIM>
912 density_grid_fourier(pofk_nmesh, nleft, nright);
913 FML::INTERPOLATION::particles_to_fourier_grid(part.get_particles_ptr(),
914 part.get_npart(),
915 part.get_npart_total(),
916 density_grid_fourier,
917 pofk_density_assignment_method,
918 pofk_interlacing);
919 FML::INTERPOLATION::deconvolve_window_function_fourier<NDIM>(density_grid_fourier, pofk_density_assignment_method);
920 FML::CORRELATIONFUNCTIONS::bin_up_power_spectrum(density_grid_fourier, pofk_cb_binning);
921 pofk_cb_binning.scale(simulation_boxsize);
922 */
923
924 //=============================================================
925 // Compute linear predictions
926 //=============================================================
927 auto pofk_cb = [&](double k) {
928 if (power_initial_spline) {
929 double pofk_ini = power_initial_spline(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);
933 } else if (transferdata) {
934 return transferdata->get_cdm_baryon_power_spectrum(k, 1.0 / (1.0 + redshift));
935 }
936 return 0.0;
937 };
938 auto kvals = pofk_cb_binning.kbin;
939 auto pofk_cb_linear = kvals;
940 for (auto & v : pofk_cb_linear) {
941 v = pofk_cb(v);
942 }
943 FML::INTERPOLATION::SPLINE::Spline pofk_cb_linear_spline(kvals, pofk_cb_linear, "Pcb(k) linear spline");
944
945 // The shot noise of the (weighted) neutrino particles (collective, so computed on all tasks)
946 double nu_shotnoise = 0.0;
947 if (sim.has_neutrino_particles()) {
948 typename std::remove_reference_t<decltype(sim.get_neutrinos())>::ScopedEnergyWeights energy_weights(
949 sim.get_neutrinos(),
950 1.0 / (1.0 + redshift),
952 nu_shotnoise = sim.get_neutrinos().shotnoise() * std::pow(simulation_boxsize, NDIM);
953 }
954
955 // Output to file
956 if (FML::ThisTask == 0) {
957
958 // Get non-linear boost (if this is implemented for the model)
959 // Currently we only have EuclidEmulator2 predictions for LCDM and w0waCDM
960 FML::INTERPOLATION::SPLINE::Spline non_linear_boost_of_k =
961 cosmo->get_nonlinear_matter_power_spectrum_boost(redshift);
962
963 std::string filename = snapshot_folder + "/pofk_z" + redshiftstring + ".txt";
964 std::ofstream fp(filename.c_str());
965 if (not fp.is_open()) {
966 std::cout << "Warning: Cannot write power-spectrum to file, failed to open [" << filename << "]\n";
967 } else {
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 << " ";
976 fp << "\n";
977 }
978 }
979
980 // The species (linear theory: P_cb (T_i / T_cb)^2 from the transfer functions)
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 << "]";
989 fps << "\n";
990 for (int i = 0; i < sp.second.n; i++) {
991 const double k = sp.second.kbin[i];
992 double ratio = 1.0;
993 if (transferdata) {
994 const double Ti = SIMCODE::species_transfer_function(sp.first, *transferdata, k, a);
995 ratio = std::pow(Ti / transferdata->get_cdm_baryon_transfer_function(k, a), 2);
996 }
997 fps << std::setw(15) << k << " " << std::setw(15) << sp.second.pofk[i] << " " << std::setw(15)
998 << pofk_cb_linear_spline(k) * ratio << "\n";
999 }
1000 }
1001 }
1002
1003 return pofk_cb_binning;
1004}
const int NDIM
void info(HaloModel **hmp)
Definition Wrapper.cpp:88
Class for holding the results after binning up a power-spectrum.
Class for holding grids and performing real-to-complex and complex-to-real FFTs using FFTW with MPI.
Definition FFTWGrid.h:98
SIMCODE::NeutrinoParticles< NDIM > & get_neutrinos()
Definition Simulation.h:204
double neutrino_energy_factor(double a) const
The mean energy per mass of the neutrinos <E/m>(a) with force_neutrino_energy_density,...
Definition Simulation.h:255
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...
Definition Simulation.h:229
bool has_baryons() const
Separate CDM and baryon particles? Then get_particles() are the CDM particles.
Definition Simulation.h:200
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...
Definition Simulation.h:245
double cb_shotnoise()
The shot noise of delta_cb = sum_i w_i delta_i: sum_i w_i^2 / N_i.
Definition Simulation.h:235
bool has_neutrino_particles() const
Massive neutrinos as particles (delta-f method)?
Definition Simulation.h:203
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)
Definition Simulation.h:211
const std::vector< SIMCODE::SpeciesInfo > & get_species() const
The particle species (in the order of the IDs)
Definition Simulation.h:208
bool neutrino_energy_density() const
Use the energy density of the neutrinos (force_neutrino_energy_density)
Definition Simulation.h:263
SIMCODE::NeutrinoParticles< NDIM >::Container & get_neutrino_particles()
Definition Simulation.h:205
void bin_up_power_spectrum(const FFTWGrid< N > &fourier_grid, PowerSpectrumBinning< N > &pofk)
Compute the power-spectrum of a fourier grid.
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.
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)
Definition Species.h:63
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.
What the simulation needs to know about a particle species (one particle container each): cb : CDM an...
Definition Species.h:24
std::vector< P > particles(bool empty_rank)
Vector grid(size_t n)
double norm(Vector a)

◆ compute_power_spectrum_multipoles()

template<int NDIM, class T >
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)

Definition at line 687 of file AnalyzeOutput.h.

687 {
688
689 const std::string redshiftstring = redshift_to_string(redshift);
690 const auto & settings = sim.get_parameters().pofk_multipole;
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;
698 const auto transferdata = sim.get_transferdata();
699 const auto & power_initial_spline = sim.get_power_initial_spline();
700 const auto grav = sim.get_gravity_model();
701 const auto cosmo = sim.get_cosmology();
702 auto & part = sim.get_particles();
703
704 const double a = 1.0 / (1.0 + redshift);
705 const double velocity_to_displacement = 1.0 / (a * a * cosmo->HoverH0_of_a(a));
706
707 if (FML::ThisTask == 0) {
708 std::cout << "\n";
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
713 << "\n";
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";
717 }
718
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;
723 part,
724 velocity_to_displacement,
725 Pells,
726 pofk_multipole_density_assignment_method,
727 pofk_multipole_interlacing);
728 for (auto & pl : Pells)
729 pl.scale(simulation_boxsize);
730
731 //=============================================================
732 // Compute linear predictions
733 //=============================================================
734 auto pofk_cb = [&](double k) {
735 if (power_initial_spline) {
736 double pofk_ini = power_initial_spline(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);
740 } else if (transferdata) {
741 return transferdata->get_cdm_baryon_power_spectrum(k, 1.0 / (1.0 + redshift));
742 }
743 return 0.0;
744 };
745 auto kvals = Pells[0].kbin;
746 auto pofk_cb_linear = kvals;
747 for (auto & v : pofk_cb_linear) {
748 v = pofk_cb(v);
749 }
750 FML::INTERPOLATION::SPLINE::Spline pofk_cb_linear_spline(kvals, pofk_cb_linear, "Pcb(k) linear spline");
751
752 // Output to file
753 if (FML::ThisTask == 0) {
754 std::string filename = snapshot_folder + "/pofk_multipole_z" + redshiftstring + ".txt";
755 std::ofstream fp(filename.c_str());
756 if (not fp.is_open()) {
757 std::cout << "Warning: Cannot write power-spectrum multipoles to file, failed to open [" << filename
758 << "]\n";
759 } else {
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] << " ";
769
770 // Kaiser approximation
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) << " ";
774 fp << "\n";
775 }
776 }
777 }
778
779 return Pells;
780}
double f
Definition Minimizer.cpp:28
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.

◆ gadget_species()

template<int NDIM, class T >
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.h): CDM (or CDM+baryons) as type 1, baryons as type 2, neutrinos as type 3 (with their delta-f weights as an extra field in HDF5).

Particles with a mass (particle_baryon_relative_mode = mass, mass_ini) have their masses written (MASS block, Masses)

Definition at line 178 of file AnalyzeOutput.h.

178 {
179 std::vector<FML::FILEUTILS::GADGET::GadgetSpecies> species;
180 sim.for_each_species([&](auto & particles, const SIMCODE::SpeciesInfo & info) {
181 using P = std::remove_reference_t<decltype(*particles.get_particles_ptr())>;
182 auto * p = particles.get_particles_ptr();
184 p,
185 particles.get_npart(),
186 particles.get_npart_total(),
187 info.mass_fraction);
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()); }});
190 // The mass-encoded baryon-CDM relative mode: per-particle masses (MassTable 0 for these types)
191 if constexpr (FML::PARTICLE::has_get_mass<P>())
192 sp.get_mass = [p](size_t i) { return double(FML::PARTICLE::GetMass(p[i])); };
193 species.push_back(sp);
194 });
195 return species;
196}
void for_each_species(F &&f)
Call f(particles, info) for all the species (the particle types differ, so use a generic lambda)
Definition Simulation.h:221
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...
set_mass constexpr double GetMass(Args &... args)

◆ output_gadget()

template<int NDIM, class T >
void output_gadget ( NBodySimulation< NDIM, T > & sim,
double redshift,
std::string snapshot_folder )

Definition at line 199 of file AnalyzeOutput.h.

199 {
200
201 const std::string redshiftstring = redshift_to_string(redshift);
203 const auto cosmo = sim.get_cosmology();
204 auto & part = sim.get_particles();
205
206 const double scale_factor = 1.0 / (1.0 + redshift);
207 const int nfiles = FML::NTasks;
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;
211
212 if (FML::ThisTask == 0) {
213 std::cout << "\n";
214 std::cout << "#=====================================================\n";
215 std::cout << "# Output in gadget format\n";
216 std::cout << "# fileprefix : " << fileprefix << "\n";
217 std::cout << "#=====================================================\n";
218 }
219
221 if (sim.get_species().size() > 1) {
222 // CDM (or CDM+baryons) as type 1, baryons as type 2 and neutrinos as type 3 (collisionless types: type 0 is
223 // SPH gas in GADGET)
224 const double OmegaM = cosmo->get_OmegaM();
225 gw.write_gadget_species(fileprefix + "." + std::to_string(FML::ThisTask),
226 gadget_species(sim),
227 nfiles,
228 scale_factor,
229 simulation_boxsize,
230 OmegaM,
231 cosmo->get_OmegaLambda(),
232 cosmo->get_h(),
233 pos_norm,
234 vel_norm);
235 return;
236 }
237 gw.write_gadget_single(fileprefix + "." + std::to_string(FML::ThisTask),
238 part.get_particles_ptr(),
239 part.get_npart(),
240 part.get_npart_total(),
241 nfiles,
242 scale_factor,
243 simulation_boxsize,
244 cosmo->get_OmegaM(),
245 cosmo->get_OmegaLambda(),
246 cosmo->get_h(),
247 pos_norm,
248 vel_norm);
249}
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....
Write files in GADGET format.

◆ output_gadget_hdf5()

template<int NDIM, class T >
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>.

<task>.hdf5 (or <prefix>.hdf5 if we only have one task)

Definition at line 254 of file AnalyzeOutput.h.

256 {
257#ifdef USE_HDF5
258 const std::string redshiftstring = redshift_to_string(redshift);
260 const auto cosmo = sim.get_cosmology();
261 auto & part = sim.get_particles();
262
263 const double scale_factor = 1.0 / (1.0 + redshift);
264 const int nfiles = FML::NTasks;
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;
268 const std::string filename =
269 nfiles == 1 ? fileprefix + ".hdf5" : fileprefix + "." + std::to_string(FML::ThisTask) + ".hdf5";
270
271 if (FML::ThisTask == 0) {
272 std::cout << "\n";
273 std::cout << "#=====================================================\n";
274 std::cout << "# Output in gadget HDF5 format\n";
275 std::cout << "# fileprefix : " << fileprefix << "\n";
276 std::cout << "#=====================================================\n";
277 }
278
280 if (sim.get_species().size() > 1) {
281 // CDM (or CDM+baryons) as PartType1, baryons as PartType2 and neutrinos as PartType3 (with their delta-f
282 // weights as PartType3/Weights)
283 const double OmegaM = cosmo->get_OmegaM();
284 gw.write_gadget_species(filename,
285 gadget_species(sim),
286 nfiles,
287 scale_factor,
288 simulation_boxsize,
289 OmegaM,
290 cosmo->get_OmegaLambda(),
291 cosmo->get_h(),
292 pos_norm,
293 vel_norm);
294 return;
295 }
296 gw.write_gadget_single(filename,
297 part.get_particles_ptr(),
298 part.get_npart(),
299 part.get_npart_total(),
300 nfiles,
301 scale_factor,
302 simulation_boxsize,
303 cosmo->get_OmegaM(),
304 cosmo->get_OmegaLambda(),
305 cosmo->get_h(),
306 pos_norm,
307 vel_norm);
308#else
309 throw std::runtime_error("output_gadget_hdf5: compile with USE_HDF5=true to write HDF5 files");
310#endif
311}

◆ output_pofk_for_every_step()

template<int NDIM, class T >
void output_pofk_for_every_step ( const NBodySimulation< NDIM, T > & sim)

Definition at line 43 of file AnalyzeOutput.h.

43 {
44
45 // Only task 0 outputs
46 if (FML::ThisTask > 0)
47 return;
48
49 //=============================================================
50 // Fetch parameters
51 //=============================================================
52 const auto & params = sim.get_parameters();
53 const auto ic_initial_redshift = params.ic.initial_redshift;
54 const auto & pofk_cb_every_step = sim.get_pofk_cb_every_step();
55 const auto & pofk_total_every_step = sim.get_pofk_total_every_step();
56 const auto grav = sim.get_gravity_model();
57 const auto & cosmo = grav->get_cosmo();
58 const auto grav_ic = sim.get_gravity_model_ic();
59 const auto transferdata = sim.get_transferdata();
60 const auto & power_initial_spline = sim.get_power_initial_spline();
61 const auto ic_use_gravity_model_GR = params.ic.use_gravity_model_GR;
62 const auto pofk_subtract_shotnoise = params.pofk.subtract_shotnoise;
63
64 //=============================================================
65 // Output all CMB+baryon Pofk
66 //=============================================================
67 for (auto & p : pofk_cb_every_step) {
68 auto redshift = p.first;
69 auto binning = p.second;
70 auto pofk_cb = [&](double k) {
71 double pofk_ini = power_initial_spline(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);
75 };
76
77 // Get non-linear boost (if this is implemented for the model)
78 // Currently we only have EuclidEmulator2 predictions for LCDM and w0waCDM
79 FML::INTERPOLATION::SPLINE::Spline non_linear_boost_of_k =
80 cosmo->get_nonlinear_matter_power_spectrum_boost(redshift);
81
82 std::string filename =
83 params.output.path("pofk_" + params.simulation.name + "_cb_z" + redshift_to_string(redshift) + ".txt");
84
85 std::ofstream fp(filename.c_str());
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 << " ";
95 fp << "\n";
96 }
97 }
98
99 //=============================================================
100 // The species when there are several (cdm, baryon, nu): P(k) of each, with linear theory from the transfer
101 // functions P_i(k,z) = P_cb(k,z) (T_i(k,z) / T_cb(k,z))^2
102 //=============================================================
103 for (auto & [label, list] : sim.get_pofk_species_every_step()) {
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" +
109 redshift_to_string(redshift) + ".txt");
110 std::ofstream fp(filename.c_str());
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];
115 double plin = 0.0;
116 if (transferdata) {
117 const double Ti = SIMCODE::species_transfer_function(label, *transferdata, k, a);
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);
120 plin = power_initial_spline(k) * std::pow(D / Dini, 2) *
121 std::pow(Ti / transferdata->get_cdm_baryon_transfer_function(k, a), 2);
122 }
123 fp << std::setw(15) << k << " " << std::setw(15) << binning.pofk[i] << " " << std::setw(15) << plin
124 << "\n";
125 }
126 }
127 }
128
129 //=============================================================
130 // Output all total Pofk
131 //=============================================================
132 for (auto & p : pofk_total_every_step) {
133 auto redshift = p.first;
134 auto binning = p.second;
135 auto pofk_total = [&](double k) {
136 double fac = 1.0;
137 // If ic_use_gravity_model_GR then transferdata is that of GR so
138 // we need to rescale it with growthfactors
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);
145 }
146 if (transferdata)
147 return fac * transferdata->get_total_power_spectrum(k, 1.0 / (1.0 + redshift));
148 // If we don't have transfer data we don't have this info so just output 0.0
149 return 0.0;
150 };
151
152 std::string filename =
153 params.output.path("pofk_" + params.simulation.name + "_total_z" + redshift_to_string(redshift) + ".txt");
154
155 // Get non-linear boost (if this is implemented for the model)
156 // Currently we only have EuclidEmulator2 predictions for LCDM and w0waCDM
157 FML::INTERPOLATION::SPLINE::Spline non_linear_boost_of_k =
158 cosmo->get_nonlinear_matter_power_spectrum_boost(redshift);
159
160 std::ofstream fp(filename.c_str());
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 << " ";
169 fp << "\n";
170 }
171 }
172}
const auto & get_pofk_total_every_step() const
Definition Simulation.h:270
std::shared_ptr< GravityModel< NDIM > > get_gravity_model_ic() const
Definition Simulation.h:266
const auto & get_pofk_cb_every_step() const
Definition Simulation.h:269