File size: 5,005 Bytes
bc65052 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# import necessary modules
# uncomment to get plots displayed in notebook
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from classy import Class
from scipy.optimize import fsolve
import math
# In[ ]:
############################################
#
# Varying parameter (others fixed to default)
#
var_name = 'N_ur'
var_array = np.linspace(3.044,5.044,5)
var_num = len(var_array)
var_legend = r'$N_\mathrm{eff}$'
var_figname = 'neff'
#
# Constraints to be matched
#
# As explained in the "Neutrino cosmology" book, CUP, Lesgourgues et al., section 5.3, the goal is to vary
# - omega_cdm by a factor alpha = (1 + coeff*Neff)/(1 + coeff*3.046)
# - h by a factor sqrt*(alpha)
# in order to keep a fixed z_equality(R/M) and z_equality(M/Lambda)
#
omega_b = 0.0223828
omega_cdm_standard = 0.1201075
h_standard = 0.67810
#
# coefficient such that omega_r = omega_gamma (1 + coeff*Neff),
# i.e. such that omega_ur = omega_gamma * coeff * Neff:
# coeff = omega_ur/omega_gamma/Neff_standard
# We could extract omega_ur and omega_gamma on-the-fly within th script,
# but for simplicity we did a preliminary interactive run with background_verbose=2
# and we copied the values given in the budget output.
#
coeff = 1.70961e-05/2.47298e-05/3.044
print ("coeff=",coeff)
#
#############################################
#
# Fixed settings
#
common_settings = {# fixed LambdaCDM parameters
'omega_b':omega_b,
'A_s':2.100549e-09,
'n_s':0.9660499,
'tau_reio':0.05430842,
# output and precision parameters
'output':'tCl,pCl,lCl,mPk',
'lensing':'yes',
'P_k_max_1/Mpc':3.0}
#
##############################################
#
# loop over varying parameter values
#
M = {}
#
for i, N_ur in enumerate(var_array):
#
# rescale omega_cdm and h
#
alpha = (1.+coeff*N_ur)/(1.+coeff*3.044)
omega_cdm = (omega_b + omega_cdm_standard)*alpha - omega_b
h = h_standard*math.sqrt(alpha)
print (' * Compute with %s=%e, %s=%e, %s=%e'%('N_ur',N_ur,'omega_cdm',omega_cdm,'h',h))
#
# call CLASS
#
M[i] = Class()
M[i].set(common_settings)
M[i].set(better_precision_settings)
M[i].set({'N_ur':N_ur})
M[i].set({'omega_cdm':omega_cdm})
M[i].set({'h':h})
M[i].compute()
# In[ ]:
#############################################
#
# extract spectra and plot them
#
#############################################
kvec = np.logspace(-4,np.log10(3),1000) # array of kvec in h/Mpc
twopi = 2.*math.pi
#
# Create figures
#
fig_Pk, ax_Pk = plt.subplots()
fig_TT, ax_TT = plt.subplots()
#
# loop over varying parameter values
#
ll = {}
clM = {}
clTT = {}
pkM = {}
legarray = []
for i, N_ur in enumerate(var_array):
#
alpha = (1.+coeff*N_ur)/(1.+coeff*3.044)
h = 0.67810*math.sqrt(alpha) # this is h
#
# deal with colors and legends
#
if i == 0:
var_color = 'k'
var_alpha = 1.
else:
var_color = plt.cm.Reds(0.8*i/(var_num-1))
#
# get Cls
#
clM[i] = M[i].lensed_cl(2500)
ll[i] = clM[i]['ell'][2:]
clTT[i] = clM[i]['tt'][2:]
#
# store P(k) for common k values
#
pkM[i] = []
# The function .pk(k,z) wants k in 1/Mpc so we must convert kvec for each case with the right h
khvec = kvec*h # This is k in 1/Mpc
for kh in khvec:
pkM[i].append(M[i].pk(kh,0.)*h**3)
#
# plot P(k)
#
if i == 0:
ax_Pk.semilogx(kvec,np.array(pkM[i])/np.array(pkM[0]),
color=var_color,#alpha=var_alpha,
linestyle='-')
else:
ax_Pk.semilogx(kvec,np.array(pkM[i])/np.array(pkM[0]),
color=var_color,#alpha=var_alpha,
linestyle='-',
label=r'$\Delta N_\mathrm{eff}=%g$'%(N_ur-3.044))
#
# plot C_l^TT
#
if i == 0:
ax_TT.semilogx(ll[i],clTT[i]/clTT[0],
color=var_color,alpha=var_alpha,linestyle='-')
else:
ax_TT.semilogx(ll[i],clTT[i]/clTT[0],
color=var_color,alpha=var_alpha,linestyle='-',
label=r'$\Delta N_\mathrm{eff}=%g$'%(N_ur-3.044))
#
# output of P(k) figure
#
ax_Pk.set_xlim([1.e-3,3.])
ax_Pk.set_ylim([0.98,1.20])
ax_Pk.set_xlabel(r'$k \,\,\,\, [h^{-1}\mathrm{Mpc}]$')
ax_Pk.set_ylabel(r'$P(k)/P(k)[N_\mathrm{eff}=3.046]$')
ax_Pk.legend(loc='upper left')
fig_Pk.tight_layout()
fig_Pk.savefig('ratio-%s-Pk.pdf' % var_figname)
#
# output of C_l^TT figure
#
ax_TT.set_xlim([2,2500])
ax_TT.set_ylim([0.850,1.005])
ax_TT.set_xlabel(r'$\mathrm{Multipole} \,\,\,\, \ell$')
ax_TT.set_ylabel(r'$C_\ell^\mathrm{TT}/C_\ell^\mathrm{TT}(N_\mathrm{eff}=3.046)$')
ax_TT.legend(loc='lower left')
fig_TT.tight_layout()
fig_TT.savefig('ratio-%s-cltt.pdf' % var_figname)
|