cosmology
cosmology.py is a Python routine that contains functions relevant for cosmological calculations, including a solver of Friedmann equations.
Adapted from the original cosmology in GW_turbulence (https://github.com/AlbertoRoper/GW_turbulence), created in Nov. 2022
Currently part of the cosmoGW code:
https://github.com/cosmoGW/cosmoGW/ https://github.com/cosmoGW/cosmoGW/blob/main/src/cosmoGW/cosmology.py
Note
For full documentation, visit Read the Docs
To use it, first install cosmoGW:
pip install cosmoGW
Dates
Created: 27/11/2022 (GW_turbulence)
Updated: 21/08/2025 (release cosmoGW 1.0: https://pypi.org/project/cosmoGW)
References
[RoperPol:2018sap]: A. Roper Pol, A. Brandenburg, T. Kahniashvili, A. Kosowsky, S. Mandal, “The timestep constraint in solving the gravitational wave equations sourced by hydromagnetic turbulence,”. Geophys. Astrophys. Fluid Dynamics 114, 1, 130 (2020), arXiv:1807.05479.
[RoperPol:2019wvy]: A. Roper Pol, S. Mandal, A. Brandenburg, T. Kahniashvili, A. Kosowsky, “Numerical simulations of gravitational waves from early-universe turbulence,” Phys. Rev. D 102, 083512 (2020), arXiv:1903.08585.
[RoperPol:2022iel]: A. Roper Pol, C. Caprini, A. Neronov, D. Semikoz, “The gravitational wave signal from primordial magnetic fields in the Pulsar Timing Array frequency band,” Phys. Rev. D 105, 123502 (2022), arXiv:2201.05630.
[He:2022qcs]: Y. He, A. Roper Pol, A. Brandenburg, “Modified propagation of gravitational waves from the early radiation era,” JCAP 06 (2023), 025, arXiv:2212.06082.
[Planck:2018vyg]: [Planck], “Planck 2018 results. VI. Cosmological parameters,” Astron. Astrophys. 641 (2020) A6, Astron. Astrophys. 652 (2021) C4 (erratum), arXiv:1807.06209.
[Rasanen]: Lecture notes on Cosmology by Syksy Räsänen (https://www.mv.helsinki.fi/home/syrasane/cosmo/)
- cosmoGW.cosmology.Hs_fact()
Compute factor for Hubble rate at generation time (radiation era).
The factor is given in units of Hz/\({\rm MeV}^2\), such that the actual Hubble rate is given after multiplying fact x \(\sqrt{g}\) x \(T^2\), being g the number of dof and T the temperature scale (in MeV).
Returns
- factastropy.units.Quantity
Factor in Hz/\({\rm MeV}^2\).
Reference
RoperPol:2022iel, eq. 29
- cosmoGW.cosmology.Hs_from_a(a, dir0='', Neff=3)
Compute Hubble rate during RD era from scale factor, assuming adiabatic expansion:
\[a^3 g_S T^3 = {\rm \ constant}.\]Parameters
- andarray
Array of scale factors
- dir0str, optional
Directory where the file of dof is stored (resources/cosmology/ default).
- Nefffloat, optional
Effective number of neutrino species (default is 3)
Returns
- Hsndarray
Array of Hubble rates during RD era Hs – Hubble rate during RD era
References
See values_0 and thermal_g.
- cosmoGW.cosmology.Hs_val(g=100, T=<Quantity 100. GeV>)
Compute Hubble rate at generation time (radiation era).
Parameters
- gfloat
Relativistic degrees of freedom (default 100).
- Tastropy.units.Quantity
Temperature scale (default 100 GeV).
Returns
- Hsastropy.units.Quantity
Hubble rate in frequency units (Hz).
Reference
RoperPol:2022iel, eq. 29
- cosmoGW.cosmology.Omega_rad_dof(a, dir0='', Neff=3)
Compute radiation energy density ratio due to varying dofs.
Parameters
- andarray
Array of scale factors
- dir0str, optional
Directory where the file of dof is stored (resources/cosmology/ default)
- Nefffloat, optional
Effective number of neutrino species (default is 3)
Returns
- Om_rat_dofndarray
Ratio of radiation energy density due to accounting for T depending dofs
References
He:2022qcs, eq.A.2
- cosmoGW.cosmology.Omega_vs_a(a, dir0='', a0=1, h0=0.6732, OmL0=0.6841, dofs=True, Neff=3)
Compute energy density ratios as a function of scale factor for a universe composed by matter, radiation, and dark energy:
\[\Omega (a) = \Omega_{R}^0 \times a^{-4} + \Omega_{M}^0 \times a^{-3} + \Omega_{L}^0\]Parameters
- andarray
Array of scale factors.
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- a0float, optional
Present scale factor (default 1).
- h0float, optional
Present Hubble rate scaling (default 0.6732).
- OmL0float, optional
Present dark energy content (default 0.6841).
- dofsbool, optional
If True, compensate radiation energy density using dofs.
- Nefffloat, optional
Effective number of neutrino species (default 3).
Returns
- Om_totndarray
Total energy density (normalized).
- Om_radndarray
Radiation energy density (normalized).
- Om_matndarray
Matter energy density (normalized).
References
He:2022qcs, eq.A.2
- cosmoGW.cosmology.RD_dofs(dir0='', Neff=3)
Compute relativistic and adiabatic dofs during the RD era.
Parameters
- dir0str, optional
Directory where the file of dof is stored (‘resources/cosmology/’ directory by default)
- Nefffloat, optional
Effective number of neutrino species (default is 3)
Returns
- Tndarray
Array of temperatures within RD era
- as_Tndarray
Array of scale factors
- gsndarray
Array of relativistic dofs
- gSndarray
Array of adiabatic dofs
References
See values_0 and thermal_g.
- cosmoGW.cosmology.as_a0_rat(g=100, T=<Quantity 100. GeV>, Neff=3)
Compute ratio of scale factors (generation/present) assuming adiabatic expansion of the universe.
Parameters
- gfloat
Number of relativistic degrees of freedom (dof) at the time of generation (default is 100).
- Tastropy.units.Quantity
Temperature scale at the time of generation (default is 100 GeV).
- Nefffloat
Effective number of neutrino species (default is 3).
Returns
- as_a0astropy.units.Quantity
Ratio of scale factors (a*/a0).
Reference
RoperPol:2022iel, eq. 28
- cosmoGW.cosmology.as_fact(Neff=3)
Compute factor for scale factor ratio at generation time assuming adiabatic expansion of the universe.
The factor is in units of MeV and the ratio is obtained by multiplying fact x \(g^{-1/3}/T\), being \(g\) the number of dof and \(T\) the temperature scale (in MeV).
Parameters
- Nefffloat
Effective number of neutrino species (default is 3).
Returns
- factastropy.units.Quantity
Ratio to present-day scale factor, \(T_0 g_{0s}^{1/3}\).
Reference
RoperPol:2022iel, eq. 28
- cosmoGW.cosmology.cut_var(x, x0, xf, inds, inds2)
Cut variable x using indices and add initial/final values. Used in norm_variables_cut function.
Parameters
- xndarray
Input array.
- x0float
Initial value to add.
- xffloat
Final value to add.
- indsndarray
Indices for first cut.
- inds2ndarray
Indices for second cut.
Returns
- yndarray
Cut and extended array.
- cosmoGW.cosmology.dofs_vs_a(a, dir0='', Neff=3)
Compute relativistic and adiabatic dofs for scale factors.
Parameters
- andarray
Array of scale factors
- dir0str, optional
Directory where the file of dof is stored (‘/cosmology/’ directory by default)
- Nefffloat, optional
Effective number of neutrino species (default is 3)
Returns
- gsndarray
Array of relativistic dofs
- gSndarray
Array of adiabatic dofs
References
See values_0 and thermal_g.
- cosmoGW.cosmology.friedmann(a, dir0='', a0=1, h0=0.6732, OmL0=0.6841, dofs=True, Neff=3)
Use Friedmann equations to compute EOS and time derivatives.
Parameters
- andarray
Array of scale factors.
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- a0float, optional
Present scale factor (default 1).
- h0float, optional
Present Hubble rate scaling (default 0.6732).
- OmL0float, optional
Present dark energy content (default 0.6841).
- dofsbool, optional
If True, compensate radiation energy density using dofs.
- Nefffloat, optional
Effective number of neutrino species (default 3).
Returns
- wfloat
Equation of state (p = w rho).
- adfloat
Cosmic time derivative of the scale factor.
- addfloat
Second cosmic time derivative of the scale factor.
- apfloat
Conformal time derivative of the scale factor.
- appfloat
Second conformal time derivative of the scale factor.
References
He:2022qcs, eqs. A.5-A.6
- cosmoGW.cosmology.friedmann_solver(a, dir0='', a0=1.0, h0=0.6732, OmL0=0.6841, dofs=True, Neff=3, return_all=False, save=True, nm_fl='')
Numerically solve Friedmann equations for a(eta) and a(t).
A tutorial is available under cosmology/cosmology.ipynb
Parameters
- andarray
Array of scale factors.
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- a0float, optional
Present scale factor (default 1).
- h0float, optional
Present Hubble rate scaling (default 0.6732).
- OmL0float, optional
Present dark energy content (default 0.6841).
- dofsbool, optional
If True, compensate radiation energy density using dofs.
- Nefffloat, optional
Effective number of neutrino species (default 3).
- return_allbool, optional
If True, return all variables used in the Friedmann solver.
- savebool, optional
If True, save the solutions in the file ‘friedmann/solution#nm_fl.csv’ where #nm_fl is the input file name.
- nm_flstr, optional
File name suffix.
Returns
- tndarray
Cosmic time.
- etandarray
Conformal time.
(other variables if return_all is True)
References
He:2022qcs, appendix A.
- cosmoGW.cosmology.norm_variables_cut(eta_n, HH_n, a_n, Omega, Omega_mat, eta_n_0, dir0='', T=<Quantity 100. GeV>, OmM0=0.31589999999999996, h0=0.6732)
Cut normalized variables between initial time and present time.
Parameters
- eta_nndarray
Normalized conformal time eta/eta_*.
- HH_nndarray
Normalized conformal Hubble rate H/H_*.
- a_nndarray
Normalized scale factor a/a_*.
- Omegandarray
Ratio of total energy to present-time critical energy density.
- Omega_matndarray
Matter energy density (normalized).
- eta_n_0float
Normalized conformal present time.
- Hsfloat
Hubble rate at the initial time.
- astfloat
Scale factor at the initial time.
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- Tfloat, optional
Temperature scale at the initial time in energy units (default is 100 GeV).
- OmM0float, optional
Present-time content of matter (default is 0.3159).
- h0float, optional
Present-time value of the Hubble rate H0 = h0 x 100 km/s/Mpc (default is 67.32 km/s/Mpc based on CMB observations).
Returns
- eta_nnndarray
Normalized conformal time.
- HH_nnndarray
Normalized conformal Hubble rate.
- a_nnndarray
Normalized scale factor.
- Omega_nnndarray
Ratio of total energy to present-time critical energy density.
- Omega_mat_nnndarray
Matter energy density (normalized).
- app_nnndarray
Second time derivative of the scale factor.
- w_nnndarray
Equation of state p/rho.
- cosmoGW.cosmology.normalized_variables(a, eta, ap_a, app_a, dir0='', T=<Quantity 100. GeV>, h0=0.6732)
Compute normalized variables for GW generation initial time, which are required to be used in the Pencil Code.
A tutorial is available under cosmology/cosmology_PC.ipynb
Parameters
- andarray
Scale factors, normalized to present-time a_0 = 1.
- etandarray
Conformal times, normalized to present-time a_0 = 1.
- ap_andarray
Conformal Hubble time a’/a, normalized to present-time a_0 = 1.
- app_andarray
a’’/a, normalized to present-time a_0 = 1.
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- Tastropy.units.Quantity, optional
Temperature scale (default 100 GeV).
- h0float, optional
Present Hubble rate scaling (default 0.6732).
Returns
- a_nndarray
Normalized scale factor a/a_*.
- eta_nndarray
Normalized conformal time eta/eta_*.
- HH_nndarray
Normalized conformal Hubble rate H/H_*.
- app_a_nndarray
Normalized second conformal time derivative of a.
- Omegandarray
Ratio of total energy to present-time critical energy density.
- wndarray
Equation of state.
- eta_n_0float
Normalized conformal present time.
- aEQ_nfloat
Normalized equipartition scale factor.
- aL_nfloat
Normalized dark energy domination scale factor.
- a_acc_nfloat
Normalized scale factor when acceleration starts.
- eta_n_EQfloat
Normalized conformal time at equipartition.
- eta_n_Lfloat
Normalized conformal time at dark energy domination.
- eta_n_accfloat
Normalized conformal time when acceleration starts.
References
He:2022qcs, appendix A. RoperPol:2018sap.
- cosmoGW.cosmology.ratio_app_a_n_factor(a, dir0='', a0=1, h0=0.6732, OmL0=0.6841, dofs=True, Neff=3)
Compute ratio of a’’/a (normalized) to conformal Hubble rate H (normalized) times a_*/a_0 during RD era.
Parameters
- andarray
Array of scale factors.
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- a0float, optional
Present scale factor (default 1).
- h0float, optional
Present Hubble rate scaling (default 0.6732).
- OmL0float, optional
Present dark energy content (default 0.6841).
- dofsbool, optional
If True, compensate radiation energy density using dofs.
- Nefffloat, optional
Effective number of neutrino species (default 3).
Returns
- factorndarray
Ratio of a’’/a to conformal Hubble rate H times a_*/a_0.
References
He:2022qcs, eq. 3.11
- cosmoGW.cosmology.rho_critical(H=<Quantity 3.24077929e-18 Hz>)
Compute critical energy density at a given epoch.
Parameters
- Hastropy.units.Quantity
Hubble rate in units of frequency.
Returns
- rho_castropy.units.Quantity
Critical energy density in GeV/m^3 units.
Reference
RoperPol:2019wvy, eq. 3
- cosmoGW.cosmology.rho_radiation(g=100, T=<Quantity 100. GeV>)
Compute radiation energy density at a given epoch.
Parameters
- gfloat
Relativistic degrees of freedom (default 100).
- Tastropy.units.Quantity
Temperature scale (default 100 GeV).
Returns
- rho_radastropy.units.Quantity
Radiation energy density in GeV/m^3 units.
Reference
RoperPol:2019wvy, eq. 3
- cosmoGW.cosmology.thermal_g(dir0='', T=<Quantity 100. GeV>, s=0, file=True, Neff=3)
Return relativistic or adiabatic degrees of freedom as a function of T according to the thermal history of the Universe.
Note that for T < 0.5 MeV, after neutrino decoupling, entropic and relativistic g are approximately equal.
Parameters
- dir0str, optional
Directory for dof file (default resources/cosmology/).
- Tastropy.units.Quantity, optional
Temperature scale (default 100 GeV).
- sint, optional
0 for relativistic, 1 for adiabatic dof.
- filebool, optional
If True, read dof from file. It reads the file ‘dir0/cosmology/T_gs.csv’ and should be a pandas file with columns: [‘T [GeV]’, ‘g_*’, ‘gS’]
- Nefffloat, optional
Effective number of neutrino species (default 3).
Returns
- gfloat or ndarray
Relativistic or adiabatic degrees of freedom.
Reference
Rasanen - Lecture notes on Cosmology.
- cosmoGW.cosmology.values_0(h0=1.0, neut=False, Neff=3, ret_rad=False)
Return cosmological parameters at present time.
Parameters
- h0float
Hubble rate scaling (default 1).
- neutbool
If True, include neutrinos in relativistic dofs.
- Nefffloat
Effective number of neutrino species (default 3).
- ret_radbool
If True, also return radiation energy density.
Returns
- g0float
Relativistic degrees of freedom (2 if massive neutrinos are assumed).
- g0sfloat
Entropic/adiabatic degrees of freedom (3.91 including neutrinos)
- T0astropy.units.Quantity
Present temperature in MeV, corresponding to 2.72548 K
- H0astropy.units.Quantity
Present Hubble rate in Hz, corresponding to H0 = 100 h0 km/s/Mpc.
- rho_rad0astropy.units.Quantity, optional
Radiation energy density at present time.
- Om_rad0float, optional
Radiation density ratio to critical at present time.
References
Rasanen - Lecture notes on Cosmology.