FML
Namespaces | Data Structures | Typedefs | Functions | Variables
FML::COSMOLOGY Namespace Reference

This namespace deals with various cosmology specific things: background evolution, perturbation theory, recombination history etc. More...

Namespaces

namespace  HALOMODEL
 
namespace  LPT
 This namespace contains things related to Lagrangian Perturbation Theory (displacement fields, reconstruction, initial conditions etc.)
 
namespace  SPHERICALCOLLAPSE
 

Data Structures

struct  _PerturbationSystemInfo
 Struct for keeping track over what quantities we have at what index when solving the linear Einstein-Boltzmann system. More...
 
class  BackgroundCosmology
 Computing the background evolution of our Universe (LCDM). More...
 
class  Perturbations
 Class for solving the linear perturbations (LCDM) in temperature, baryon, CDM, massless neutrinos etc. More...
 
class  PowerSpectrum
 Computing power-spectra (matter, CMB, correlation functions etc.) in linear perturbation theory. More...
 
class  RecombinationHistory
 For computing the recombination history of our Universe. More...
 

Typedefs

using ParameterMap = FML::UTILS::ParameterMap
 
using ODEFunction = FML::SOLVERS::ODESOLVER::ODEFunction
 
using ODESolver = FML::SOLVERS::ODESOLVER::ODESolver
 
using Spline = FML::INTERPOLATION::SPLINE::Spline
 
using DVector = std::vector< double >
 
using BackgroundCosmology = FML::COSMOLOGY::BackgroundCosmology
 
using RecombinationHistory = FML::COSMOLOGY::RecombinationHistory
 
using ODEFunctionJacobian = FML::SOLVERS::ODESOLVER::ODEFunctionJacobian
 
using Spline2D = FML::INTERPOLATION::SPLINE::Spline2D
 
using DVector2D = std::vector< DVector >
 
typedef struct FML::COSMOLOGY::_PerturbationSystemInfo PerturbationSystemInfo
 Struct for keeping track over what quantities we have at what index when solving the linear Einstein-Boltzmann system. More...
 
using Perturbations = FML::COSMOLOGY::Perturbations
 

Functions

double correlation_function_single (double r, std::function< double(double)> &Delta_P, double kmin, double kmax)
 
std::pair< DVector, DVectorcorrelation_function_single_fftlog (std::function< double(double)> &Delta_P, double rmin, double rmax, int ngrid)
 
std::pair< DVector, DVectorcorrelation_function_single_fftw (std::function< double(double)> &Delta_P, double rmin, double rmax, int ngrid_min)
 
template<class T >
void particles_to_redshiftspace (FML::PARTICLE::MPIParticles< T > &part, std::vector< double > line_of_sight_direction, double velocity_to_displacement)
 This takes a set of particles and displace them from realspace to redshiftspace Using a fixed line of sight direction DeltaX = (v * r)r * velocity_to_displacement If velocities are peculiar then velocity_to_displacement = 1/(100 * aH/H0 * box) with box in Mpc/h You can revert back to real-space by calling the method again with velocity_to_displacement being negative. More...
 

Variables

FML::UTILS::ConstantsAndUnits Constants ("SI")
 

Detailed Description

This namespace deals with various cosmology specific things: background evolution, perturbation theory, recombination history etc.

Typedef Documentation

◆ BackgroundCosmology

Definition at line 31 of file Perturbations.h.

◆ DVector

typedef std::vector< double > FML::COSMOLOGY::DVector

Definition at line 37 of file BackgroundCosmology.h.

◆ DVector2D

typedef std::vector< DVector > FML::COSMOLOGY::DVector2D

Definition at line 39 of file Perturbations.h.

◆ ODEFunction

Definition at line 34 of file BackgroundCosmology.h.

◆ ODEFunctionJacobian

Definition at line 35 of file Perturbations.h.

◆ ODESolver

Definition at line 35 of file BackgroundCosmology.h.

◆ ParameterMap

Definition at line 33 of file BackgroundCosmology.h.

◆ Perturbations

Definition at line 43 of file PowerSpectrum.h.

◆ PerturbationSystemInfo

Struct for keeping track over what quantities we have at what index when solving the linear Einstein-Boltzmann system.

◆ RecombinationHistory

Definition at line 32 of file Perturbations.h.

◆ Spline

Definition at line 36 of file BackgroundCosmology.h.

◆ Spline2D

Definition at line 37 of file Perturbations.h.

Function Documentation

◆ correlation_function_single()

double FML::COSMOLOGY::correlation_function_single ( double  r,
std::function< double(double)> &  Delta_P,
double  kmin,
double  kmax 
)

Definition at line 920 of file PowerSpectrum.cpp.

920 {
921
922 ODEFunction dxidlogk_func = [&](double logkr, [[maybe_unused]] const double * y, double * dxidlogkr) {
923 double kr = std::exp(logkr);
924 dxidlogkr[0] = std::sin(kr) / (kr)*Delta_P(kr / r);
925 return GSL_SUCCESS;
926 };
927
928 DVector xi_ini{0.0};
929 DVector range{std::log(kmin * r), std::log(kmax * r)};
930 ODESolver xi_ode(1e-5, 1e-9, 1e-9);
931 xi_ode.solve(dxidlogk_func, range, xi_ini);
932
933 return xi_ode.get_final_data_by_component(0);
934 }
FML::INTERPOLATION::SPLINE::DVector DVector
Definition: Simulation.h:39
This is a wrapper around the GSL library to easily solve ODEs and return the data in whatever format ...
Definition: ODESolver.h:87
FML::SOLVERS::ODESOLVER::ODEFunction ODEFunction
return y
Definition: Global.h:265
const double exp
Definition: Global.h:259

◆ correlation_function_single_fftlog()

std::pair< DVector, DVector > FML::COSMOLOGY::correlation_function_single_fftlog ( std::function< double(double)> &  Delta_P,
double  rmin,
double  rmax,
int  ngrid 
)

Definition at line 938 of file PowerSpectrum.cpp.

941 {
942 // We want xi(r) for rmin ... rmax. We solve for it in rmin/padding ... rmax * padding
943 // to avoid ringing/aliasing close to the edges. A factor of 15 seems to be enough
944 // The number of points sets the accuracy. The fiducial choice below is to ensure
945 // 1% accuracy around the BAO peak
946 const double r0 = std::sqrt(rmin * rmax);
947 const double paddingfactor = 15.0;
948 const double k0 = 1.0 / r0;
949
950 // Set ranges and make a log-spaced k array
951 const double kmin_fft = k0 * r0 / rmax / paddingfactor;
952 const double kmax_fft = k0 * r0 / rmin * paddingfactor;
953 DVector k_array = FML::MATH::linspace(std::log(kmin_fft), std::log(kmax_fft), ngrid);
954 for (auto & k : k_array)
955 k = std::exp(k);
956
957 // Fill P(k) array
958 DVector Pk_array(ngrid);
959 for (int i = 0; i < ngrid; i++) {
960 Pk_array[i] = Delta_P(k_array[i]) / std::pow(k_array[i], 3) * (2.0 * M_PI * M_PI);
961 }
962
963 // FFTLog algorithm
964 auto res = FML::SOLVERS::FFTLog::ComputeCorrelationFunction(k_array, Pk_array);
965
966 return res;
967 }
std::vector< double > DVector
DVector linspace(double xmin, double xmax, int num)
Python linspace. Generate a lineary spaced array.
Definition: Math.cpp:269
std::pair< DVector, DVector > ComputeCorrelationFunction(const DVector &k, const DVector &pk)
Compute the correlation function xi(r) from a power spectrum P(k), sampled at logarithmically spaced ...
Definition: FFTLog.cpp:181

◆ correlation_function_single_fftw()

std::pair< DVector, DVector > FML::COSMOLOGY::correlation_function_single_fftw ( std::function< double(double)> &  Delta_P,
double  rmin,
double  rmax,
int  ngrid_min 
)

Definition at line 970 of file PowerSpectrum.cpp.

973 {
974
975 // Set the boxsize and the grid resolution
976 double Box = 4.0 * rmax;
977 int ngrid = std::max(int(4.0 * (Box / rmin)), ngrid_min);
978
979 // Find closest power of two for optimal FFTW
980 for (int N = 2;; N *= 2) {
981 if (N >= ngrid or N > 1e9) {
982 ngrid = N;
983 break;
984 }
985 }
986
987 // Set up grid in k-space and fill it with the source
988 fftw_complex * source = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * ngrid);
989 for (int i = 0; i < ngrid; i++) {
990 double im = 0.0;
991 if (i == 0 or i == ngrid / 2) {
992 // Zero mode and largest mode
993 im = 0.0;
994 } else if (i < ngrid / 2) {
995 // Positive modes
996 double k = 2.0 * M_PI / Box * i;
997 im = -Delta_P(k) / (k * k) / 2.0;
998 } else {
999 // Negative modes
1000 double k = 2.0 * M_PI / Box * (i - ngrid);
1001 im = +Delta_P(-k) / (k * k) / 2.0;
1002 }
1003 source[i][0] = 0.0;
1004 source[i][1] = im;
1005 }
1006
1007 // Transform to real space to get r*xi(r)
1008 fftw_plan plan = fftw_plan_dft_1d(ngrid, source, source, FFTW_BACKWARD, FFTW_MEASURE);
1009 fftw_execute(plan);
1010 fftw_destroy_plan(plan);
1011
1012 // Normalize to get xi (i = 0 omitted to avoid division by 0)
1013 for (int i = 0; i < ngrid; i++) {
1014 double r = i * Box / double(ngrid);
1015 source[i][0] *= 2.0 * M_PI / Box;
1016 if (i > 0)
1017 source[i][0] /= r;
1018 }
1019
1020 // Make a spline (we only keep the
1021 DVector r_arr;
1022 DVector xi_arr;
1023 r_arr.reserve(ngrid);
1024 xi_arr.reserve(ngrid);
1025 for (int i = 0; i < ngrid; i++) {
1026 double r = i * Box / double(ngrid);
1027 if (r >= rmin and r <= rmax) {
1028 r_arr.push_back(r);
1029 xi_arr.push_back(source[i][0]);
1030 }
1031 }
1032
1033 // Clean up FFTW
1034 fftw_free(source);
1035
1036 return {r_arr, xi_arr};
1037 }

◆ particles_to_redshiftspace()

template<class T >
void FML::COSMOLOGY::particles_to_redshiftspace ( FML::PARTICLE::MPIParticles< T > &  part,
std::vector< double >  line_of_sight_direction,
double  velocity_to_displacement 
)

This takes a set of particles and displace them from realspace to redshiftspace Using a fixed line of sight direction DeltaX = (v * r)r * velocity_to_displacement If velocities are peculiar then velocity_to_displacement = 1/(100 * aH/H0 * box) with box in Mpc/h You can revert back to real-space by calling the method again with velocity_to_displacement being negative.

Template Parameters
TThe particle class
Parameters
[out]partMPIParticle container
[in]line_of_sight_directionThe fixed line of sight vector, e.g. (0,0,1) for the z-axis
[in]velocity_to_displacementConvert from user velocity to RSD shift.

Definition at line 244 of file Reconstruction.h.

246 {
247
248 // Fetch how many dimensjons we are working in
249 const int N = FML::PARTICLE::GetNDIM(T());
250
251 // Check that velocities really exists (i.e. get_vel is not just set to return a nullptr)
252 static_assert(FML::PARTICLE::has_get_pos<T>(),
253 "[particles_to_redshiftspace] Partices must have positions to use this method");
254 static_assert(FML::PARTICLE::has_get_vel<T>(),
255 "[particles_to_redshiftspace] Partices must have velocity to use this method");
256
257 // Periodic box? Yes, this is only meant to be used with simulation boxes
258 const bool periodic_box = true;
259
260 // Make sure line_of_sight_direction is a unit vector
261 double norm = 0.0;
262 for (int idim = 0; idim < N; idim++) {
263 norm += line_of_sight_direction[idim] * line_of_sight_direction[idim];
264 }
265 norm = std::sqrt(norm);
266 assert_mpi(norm > 0.0, "[particles_to_redshiftspace] Line of sight vector cannot be the zero vector\n");
267 for (int idim = 0; idim < N; idim++) {
268 line_of_sight_direction[idim] /= norm;
269 }
270
271 double max_disp = 0.0;
272 auto NumPart = part.get_npart();
273 auto * p = part.get_particles_ptr();
274#ifdef USE_OMP
275#pragma omp parallel for reduction(max : max_disp)
276#endif
277 for (size_t i = 0; i < NumPart; i++) {
278 auto * pos = FML::PARTICLE::GetPos(p[i]);
279 auto * vel = FML::PARTICLE::GetVel(p[i]);
280 double vdotr = 0.0;
281 for (int idim = 0; idim < N; idim++) {
282 vdotr += vel[idim] * line_of_sight_direction[idim];
283 }
284 max_disp = std::max(max_disp, std::fabs(vdotr));
285 for (int idim = 0; idim < N; idim++) {
286 pos[idim] += vdotr * line_of_sight_direction[idim] * velocity_to_displacement;
287 // Periodic boundary conditions
288 if (periodic_box) {
289 if (pos[idim] < 0.0)
290 pos[idim] += 1.0;
291 if (pos[idim] >= 1.0)
292 pos[idim] -= 1.0;
293 }
294 }
295 }
296
297 // Only need to communicate if we have shifted along the x-axis
298 if (line_of_sight_direction[0] != 0.0)
300
301 // If velocity_to_displacement < 0 we are going the other way around
302 FML::MaxOverTasks(&max_disp);
303 max_disp *= velocity_to_displacement;
304 if (FML::ThisTask == 0)
305 std::cout << "[particles_to_redshiftspace] Maximum displacement "
306 << (velocity_to_displacement < 0 ? "(reversing) : " : " : ") << max_disp << "\n";
307 }
T * get_particles_ptr()
Get a pointer to the first particle.
Definition: MPIParticles.h:451
void communicate_particles()
Communicate particles across CPU boundaries.
Definition: MPIParticles.h:607
size_t get_npart() const
Number of active particles on the local task.
Definition: MPIParticles.h:436
get_vel constexpr double * GetPos(...)
constexpr double * GetVel(...)
void MaxOverTasks(T *value)
Inplace max over tasks of a single value.
Definition: Global.h:121
int ThisTask
The MPI task number.
Definition: Global.cpp:33

Variable Documentation

◆ Constants

FML::UTILS::ConstantsAndUnits FML::COSMOLOGY::Constants ( "SI"  )

Definition at line 40 of file BackgroundCosmology.h.