Skip to content

Commit f586e1a

Browse files
tidy
1 parent 14c7ca3 commit f586e1a

File tree

13 files changed

+44
-34
lines changed

13 files changed

+44
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dist/
55

66
# Plots
77
*.pdf
8+
*.png
89

910
# VScode
1011
.vscode/

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ I compared it against the `CAMB-HMcode` version for 100 random sets of cosmologi
6363

6464
These comparisons can be reproduced using the `comparisons/CAMB.py` script.
6565

66+
The power-spectrum suppression caused by baryonic feedback has also been implemented, and the level of agreement between the *suppression* predicted by this code and the same implementation in `CAMB` is as follows:
67+
- LCDM: Mean error: 0.02%; Standard deviation of error: <0.01%; Worst-case error; 0.03%
68+
- nu-k-w(a)-CDM: Mean error: 0.06%; Standard deviation of error: 0.07%; Worst-case error; 0.49% (larger errors strongly correlated with neutrino mass)
69+
70+
The comparisons can be reproduced using the `comparisons/CAMB_feedback.py` script.
71+
6672
While the quoted accuracy of `HMcode-2020` relative to simulations is RMS ~2.5%, note that the accuracy is anti-correlated with neutrino masses (cf. Fig. 2 of [Mead et al. 2021](https://arxiv.org/abs/2009.01858)). The larger discrepancies between the codes for massive neutrinos (2% for ~1eV) may seem worrisome, but here are some reasons why I am not that worried:
6773
- Here, neutrinos are treated as completely cold matter when calculating the linear growth factors, whereas in `CAMB-HMcode` the transition from behaving like radiation to behaving like matter is accounted for in the linear growth.
6874
- Here the *cold* matter power spectrum is taken directly from `CAMB` whereas in `CAMB-HMcode` the *cold* spectrum is calculated approximately from the total matter power spectrum using approximations for the scale-dependent growth rate from [Eisenstein & Hu (1999)](https://arxiv.org/abs/astro-ph/9710252).

comparisons/CAMB.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Standard imports
22
import sys
3+
4+
# Third-part imports
35
import numpy as np
46

57
# Project imports

comparisons/CAMB_Feedback.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# Standard imports
22
import sys
3+
4+
# Third-part imports
35
import numpy as np
46
import matplotlib.pyplot as plt
57

68
# Project imports
7-
sys.path.append('../')
8-
99
import hmcode
1010
import hmcode.camb_stuff as camb_stuff
1111
import hmcode.utility as util
1212

13-
plotting=True
13+
# Make plots or not
14+
plotting = False
1415

1516
# Vary these parameters
1617
vary_Omega_k = True
@@ -30,7 +31,6 @@
3031
m_nu_min, m_nu_max = 0., 1.
3132
log10T_AGN_min, log10T_AGN_max = 7.6, 8.3
3233

33-
3434
# Number of cosmological models to test
3535
try:
3636
ncos = int(sys.argv[1])
@@ -69,7 +69,7 @@
6969
if w0+wa < 0.: break
7070
m_nu = rng.uniform(m_nu_min, m_nu_max) if vary_m_nu else 0.
7171

72-
#Gravity only spectra
72+
### Gravity only spectra ###
7373

7474
# Get stuff from CAMB
7575
_, results_gravity, _, _, _ = camb_stuff.run(zs, Omega_c, Omega_b, Omega_k, h, ns, sigma_8, m_nu, w0, wa)
@@ -83,8 +83,9 @@
8383
# Get the new pyHMcode spectrum
8484
Pk_HMcode_gravity = hmcode.power(k, zs, results_gravity, verbose=verbose)
8585

86+
### ###
8687

87-
# Spectra with feedback
88+
### Spectra with feedback ###
8889

8990
# Get stuff from CAMB
9091
_, results_feedback, _, _, _ = camb_stuff.run(zs, Omega_c, Omega_b, Omega_k, h, ns, sigma_8, m_nu, w0, wa, log10_T_AGN=log10T_AGN)
@@ -98,11 +99,13 @@
9899
# Get the new pyHMcode spectrum
99100
Pk_HMcode_feedback = hmcode.power(k, zs, results_gravity, verbose=verbose, T_AGN=np.power(10, log10T_AGN))
100101

101-
# Calculate suppressions
102+
### ###
102103

104+
# Calculate suppressions
103105
Rk_CAMB=Pk_CAMB_feedback/Pk_CAMB_gravity
104106
Rk_HMcode=Pk_HMcode_feedback/Pk_HMcode_gravity
105107

108+
# Plotting
106109
if plotting:
107110
fig, axs=plt.subplots(nrows=2, sharex=True, figsize=(10,7))
108111
plt.subplots_adjust(hspace=0.001, bottom=0.2)
@@ -119,14 +122,13 @@
119122
for i,z in enumerate(zs):
120123
axs[0].plot(k, Rk_CAMB[i], label=f"CAMB, z={z}", color=f"C{i}", ls='--')
121124
axs[0].plot(k, Rk_HMcode[i], label=f"HMcode-Python, z={z}", color=f"C{i}")
122-
123125
axs[1].plot(k, 1-(Rk_CAMB[i]/Rk_HMcode[i]), label=f"z={z}", color=f"C{i}")
124126

125127
axs[0].legend()
126128
axs[1].legend()
127-
plt.savefig(f"Feedback_Cosmology_{icos}.png")
129+
plt.savefig(f"plots/Feedback_Cosmology_{icos}.png")
130+
plt.close()
128131

129-
130132
# Calculate maximum deviation between pyHMcode and the version in CAMB
131133
max_error = np.max(np.abs(-1.+Rk_HMcode/Rk_CAMB))
132134
max_errors.append(max_error)

hmcode/camb_stuff.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# Standard imports
2-
import numpy as np
3-
41
# Third-party imports
52
import camb
63

74
def run(zs, Omega_c, Omega_b, Omega_k, h, ns, sigma_8,
8-
m_nu=0., w=-1., wa=0., As=2e-9, norm_sigma8=True, kmax_CAMB=200., log10_T_AGN=None, verbose=False):
5+
m_nu=0., w=-1., wa=0., As=2e-9, norm_sigma8=True, kmax_CAMB=200.,
6+
log10_T_AGN=None, verbose=False):
97

108
# Sets cosmological parameters in camb to calculate the linear power spectrum
119
if log10_T_AGN:
12-
pars = camb.CAMBparams(NonLinearModel=camb.nonlinear.Halofit(halofit_version="mead2020_feedback", HMCode_logT_AGN=log10_T_AGN), WantCls=False)
10+
non_linear_model = camb.nonlinear.Halofit(halofit_version="mead2020_feedback",
11+
HMCode_logT_AGN=log10_T_AGN)
1312
else:
14-
pars = camb.CAMBparams(WantCls=False,NonLinearModel=camb.nonlinear.Halofit(halofit_version="mead2020"))
13+
non_linear_model = camb.nonlinear.Halofit(halofit_version="mead2020")
14+
pars = camb.CAMBparams(WantCls=False, NonLinearModel=non_linear_model)
1515
wb, wc = Omega_b*h**2, Omega_c*h**2
1616

1717
# This function sets standard and helium set using BBN consistency
1818
pars.set_cosmology(ombh2=wb, omch2=wc, H0=100.*h, mnu=m_nu, omk=Omega_k)
1919
pars.set_dark_energy(w=w, wa=wa, dark_energy_model='ppf')
2020
pars.InitPower.set_params(As=As, ns=ns, r=0.)
21-
pars.set_matter_power(redshifts=zs, kmax=kmax_CAMB) # Setup the linear matter power spectrum
21+
pars.set_matter_power(redshifts=zs, kmax=kmax_CAMB) # Linear matter power spectrum
2222
Omega_m = pars.omegam # Extract the matter density
2323

2424
# Scale 'As' to be correct for the desired 'sigma_8' value if necessary

hmcode/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Standard imports
12
from math import pi
23

34
# Physical constants

hmcode/cosmology.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Standard imports
1+
# Third-party imports
22
import numpy as np
33
import scipy.integrate as integrate
44

hmcode/hmcode.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,19 @@ def power(k:np.array, zs:np.array, CAMB_results:camb.CAMBdata, T_AGN=None,
146146
print('RMS in matter displacement field: {:.4} Mpc/h'.format(sigmaV))
147147
print('Effective index at collapse scale: {:.4}'.format(neff))
148148

149-
if tweaks: # HMcode parameters (Table 2 of https://arxiv.org/pdf/2009.01858.pdf)
149+
# HMcode parameters (Table 2 of https://arxiv.org/pdf/2009.01858.pdf)
150+
ks = 0.05618*sigma8**-1.013 # One-halo damping wavenumber; equation (17)
151+
if tweaks:
150152
kd = 0.05699*sigma8**-1.089 # Two-halo damping wavenumber; equation (16)
151153
f = 0.2696*sigma8**0.9403 # Two-halo fractional damping; equation (16)
152154
nd = 2.853 # Two-halo damping power; equation (16)
153155
ks = 0.05618*sigma8**-1.013 # One-halo damping wavenumber; equation (17)
154156
eta = 0.1281*sigma8**-0.3644 # Halo bloating parameter; equation (19)
155-
B = 5.196 # Minimum halo concentration; equation (20)
157+
B = 5.196 # Minimum halo concentration; equation (20)
156158
alpha = 1.875*(1.603)**neff # Transition smoothing; equation (23)
157-
else: # Use vanilla-ish halo model if no HMcode tweaks used (still uses 1-halo term suppression)
158-
B, eta = 4., 0.
159-
ks = 0.05618*sigma8**-1.013 # One-halo damping wavenumber; equation (17)
160-
159+
else: # Use vanilla-ish halo model if no HMcode tweaks used (still uses one-halo term suppression)
160+
eta = 0.
161+
B = 4.
161162

162163
if T_AGN and not tweaks:
163164
B = feedback_params['B0']*np.power(10, z*feedback_params['Bz'])
@@ -201,16 +202,15 @@ def power(k:np.array, zs:np.array, CAMB_results:camb.CAMBdata, T_AGN=None,
201202
_, _Pk_1h, _ = hmod.power_spectrum(k, Pk_lin, M, sigmaM, {'m': profile}, simple_twohalo=True)
202203

203204
# HMcode tweaks
205+
# Still uses one-halo term dampening even if tweaks=False
206+
Pk_1h = (k/ks)**4/(1.+(k/ks)**4)*_Pk_1h['m-m'] # One-halo term; equation (17)
204207
if tweaks:
205208
Pk_wig = _get_Pk_wiggle(k, Pk_lin, CAMB_results) # Isolate spectral wiggle; footnote 7
206209
Pk_dwl = Pk_lin-(1.-np.exp(-(k*sigmaV)**2))*Pk_wig # Constuct linear spectrum with smoothed wiggle; equation (15)
207210
Pk_2h = Pk_dwl*(1.-f*(k/kd)**nd/(1.+(k/kd)**nd)) # Two-halo term; equation (16)
208-
Pk_1h = (k/ks)**4/(1.+(k/ks)**4)*_Pk_1h['m-m'] # One-halo term; equation (17)
209211
Pk_hm = (Pk_2h**alpha+Pk_1h**alpha)**(1./alpha) # Total prediction via smoothed sum; equation (23)
210-
else: # Still uses 1halo term dampening even if tweaks=False
211-
Pk_1h = (k/ks)**4/(1.+(k/ks)**4)*_Pk_1h['m-m'] # One-halo term; equation (17)
212+
else:
212213
Pk_hm = Pk_lin+Pk_1h
213-
214214
Pk_HMcode[iz, :] = Pk_hm
215215

216216
if T_AGN and tweaks:

hmcode/linear_growth.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# Standard imports
2-
import numpy as np
3-
41
# Third-party imports
2+
import numpy as np
53
import camb
64

75
# Parameters

hmcode/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Standard imports
1+
# Third-party imports
22
import numpy as np
33

44
def derivative_from_samples(x:float, xs:np.array, fs:np.array) -> float:

0 commit comments

Comments
 (0)