File size: 13,650 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
## Examples of successful runs with CLASS from the AI assistant
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
from classy import Class
# Initialize CLASS
cosmo = Class()
# Set parameters using a dictionary
params = {
'output': 'mPk',
'N_ncdm': 1, # Number of sterile neutrinos
'm_ncdm': 0.2, # Mass of the sterile neutrino in eV (as a string)
'h': 0.7, # Hubble parameter
'Omega_b': 0.05, # Baryon density
'Omega_cdm': 0.25, # Cold dark matter density
'Omega_k': 0, # Curvature density
'A_s': 2.1e-9, # Amplitude of the primordial power spectrum
'n_s': 0.965, # Spectral index
'z_max_pk' : 3.0
}
cosmo.set(params)
# Compute the background and perturbations
cosmo.compute()
# Define k values and redshift
k_values = np.logspace(-3, -1, 100) # k values in 1/Mpc
z_values = [0, 1, 2] # Redshifts to plot
# Plotting the power spectrum
plt.figure(figsize=(10, 6))
for z in z_values:
pk_values = [cosmo.pk(k, z) for k in k_values]
plt.loglog(k_values, pk_values, label=f'z={z}')
plt.xlabel('k [1/Mpc]')
plt.ylabel('P(k) [Mpc^3]')
plt.title('Power Spectrum from Sterile Neutrinos')
plt.legend()
plt.grid()
plt.show()
# Clean up
cosmo.struct_cleanup()
cosmo.empty()
# In[2]:
# Import necessary modules
import matplotlib.pyplot as plt
import numpy as np
from classy import Class
# Initialize the CLASS instance for ΛCDM
LCDM = Class()
LCDM.set({'Omega_cdm': 0.25, 'Omega_b': 0.05, 'h': 0.7})
LCDM.compute()
# Get background quantities
background = LCDM.get_background()
# Extract scale factor, redshift, and growth factor
a = 1 / (1 + background['z'])
D = background['gr.fac. D'] # Growth factor D
f = background['gr.fac. f'] # Growth rate f
# Plot the growth rate as a function of redshift
plt.figure(figsize=(8, 6))
plt.plot(background['z'], f, label='Growth rate $f(z)$')
plt.xlabel('Redshift $z$')
plt.ylabel('Growth rate $f$')
plt.title('Growth Rate as a Function of Redshift for ΛCDM')
plt.xscale('log')
plt.yscale('log')
plt.gca().invert_xaxis() # Invert x-axis to show high z on the left
plt.legend()
plt.grid(True)
plt.show()
# In[3]:
import matplotlib.pyplot as plt
import numpy as np
from classy import Class
# Initialize the CLASS instance for ΛCDM
LCDM = Class()
LCDM.set({'Omega_cdm': 0.25, 'Omega_b': 0.05, 'h': 0.7})
LCDM.compute()
# Extract background quantities
background = LCDM.get_background()
# Extract scale factor, growth factor, and growth rate
a = 1. / (background['z'] + 1)
D = background['gr.fac. D']
f = background['gr.fac. f']
# Plot the growth rate
plt.figure(figsize=(8, 6))
plt.plot(background['z'], f, label='Growth Rate $f$', color='b')
plt.xlabel('Redshift $z$')
plt.ylabel('Growth Rate $f$')
plt.title('Growth Rate for ΛCDM Model')
plt.gca().invert_xaxis() # Invert x-axis to have redshift decreasing
plt.legend()
plt.grid(True)
plt.show()
# In[4]:
import matplotlib.pyplot as plt
from classy import Class
# Define common settings for the ΛCDM model
common_settings = {
'h': 0.67810,
'omega_b': 0.02238280,
'omega_cdm': 0.1201075,
'A_s': 2.100549e-09,
'n_s': 0.9660499,
'tau_reio': 0.05430842,
'output': 'tCl,pCl,lCl',
'lensing': 'yes',
'l_max_scalars': 5000
}
# Initialize CLASS
M = Class()
# Function to compute and return Cls for a given contribution
def compute_cls(contribution):
M.empty()
M.set(common_settings)
M.set({'temperature contributions': contribution})
M.compute()
return M.raw_cl(3000)
# Compute total Cls
M.set(common_settings)
M.compute()
cl_tot = M.raw_cl(3000)
# Compute individual contributions
cl_tsw = compute_cls('tsw')
cl_eisw = compute_cls('eisw')
cl_lisw = compute_cls('lisw')
cl_doppler = compute_cls('dop')
# Plotting
plt.figure(figsize=(10, 6))
ell = cl_tot['ell']
factor = 1.e10 * ell * (ell + 1) / (2 * np.pi)
plt.semilogx(ell, factor * cl_tot['tt'], 'k-', label='Total')
plt.semilogx(ell, factor * cl_tsw['tt'], 'c-', label='T+SW')
plt.semilogx(ell, factor * cl_eisw['tt'], 'r-', label='Early ISW')
plt.semilogx(ell, factor * cl_lisw['tt'], 'y-', label='Late ISW')
plt.semilogx(ell, factor * cl_doppler['tt'], 'g-', label='Doppler')
plt.xlabel(r'Multipole $\ell$')
plt.ylabel(r'$\ell (\ell+1) C_\ell^{TT} / 2 \pi \,\,\, [\times 10^{10}]$')
plt.legend(loc='upper right')
plt.grid(True)
plt.title('CMB Temperature Anisotropy Contributions')
plt.show()
# In[16]:
# Import necessary modules
from classy import Class
import matplotlib.pyplot as plt
import numpy as np
from math import pi
# Initialize the CLASS instance
M = Class()
# Define common settings (example settings)
common_settings = {
'omega_b': 0.0223828,
'omega_cdm': 0.1201075,
'h': 0.67810,
'A_s': 2.100549e-09,
'n_s': 0.9660499,
'tau_reio': 0.05430842,
'output': 'tCl,pCl,lCl',
'lensing': 'yes',
'l_max_scalars': 5000,
}
# Function to compute lensed Cls for a given temperature contribution
def compute_lensed_cls(contribution=None):
M.empty() # Clean input
M.set(common_settings) # Set common input
if contribution is not None:
M.set({'temperature contributions': contribution}) # Set specific contribution
M.compute() # Compute
return M.raw_cl(common_settings['l_max_scalars']) # Return raw Cls
# Compute contributions
cl_SW = compute_lensed_cls('tsw') # Sachs-Wolfe
cl_eISW = compute_lensed_cls('eisw') # Early ISW
cl_lISW = compute_lensed_cls('lisw') # Late ISW
# Total Cls (optional, if needed)
cl_tot = compute_lensed_cls() # Total including all contributions
# Plotting
fig, ax_Cl = plt.subplots(figsize=(10, 6))
tau_0_minus_tau_rec_hMpc = 1 # Example value, replace with actual calculation
# Plot SW contribution
ax_Cl.semilogx(cl_SW['ell']/tau_0_minus_tau_rec_hMpc,
1.e10 * cl_SW['ell'] * (cl_SW['ell'] + 1.) * cl_SW['tt'] / (2. * pi),
'c-', label=r'$\mathrm{SW}$')
# Plot early ISW contribution
ax_Cl.semilogx(cl_eISW['ell']/tau_0_minus_tau_rec_hMpc,
1.e10 * cl_eISW['ell'] * (cl_eISW['ell'] + 1.) * cl_eISW['tt'] / (2. * pi),
'r-', label=r'$\mathrm{early} \,\, \mathrm{ISW}$')
# Plot late ISW contribution
ax_Cl.semilogx(cl_lISW['ell']/tau_0_minus_tau_rec_hMpc,
1.e10 * cl_lISW['ell'] * (cl_lISW['ell'] + 1.) * cl_lISW['tt'] / (2. * pi),
'y-', label=r'$\mathrm{late} \,\, \mathrm{ISW}$')
# Plot total Cls (optional)
ax_Cl.semilogx(cl_tot['ell']/tau_0_minus_tau_rec_hMpc,
1.e10 * cl_tot['ell'] * (cl_tot['ell'] + 1.) * cl_tot['tt'] / (2. * pi),
'k-', label=r'$\mathrm{Total}$')
# Finalize the plot
ax_Cl.set_xlim([3, common_settings['l_max_scalars']])
#ax_Cl.set_ylim([0., 8.])
ax_Cl.set_xlabel(r'$\ell/(\tau_0-\tau_{rec}) \,\,\, \mathrm{[h/Mpc]}$')
ax_Cl.set_ylabel(r'$\ell (\ell+1) C_l^{TT} / 2 \pi \,\,\, [\times 10^{10}]$')
ax_Cl.legend(loc='right', bbox_to_anchor=(1.4, 0.5))
ax_Cl.grid()
# Save the figure
fig.savefig('decomposed_cl_contributions.pdf', bbox_inches='tight')
plt.show()
# In[17]:
# Import necessary modules
from classy import Class
import matplotlib.pyplot as plt
import numpy as np
from math import pi
# Function to compute lensed Cls for a given temperature contribution
def compute_lensed_cls(params):
# Initialize CLASS
M = Class()
M.set(params) # Set cosmological parameters
M.compute() # Compute the power spectra
cls = M.raw_cl(5000) # Get raw Cls
M.struct_cleanup() # Clean up
return cls
# Define cosmological parameters
params = {
'omega_b': 0.0223828,
'omega_cdm': 0.1201075,
'h': 0.67810,
'A_s': 2.100549e-09,
'n_s': 0.9660499,
'tau_reio': 0.05430842,
'output': 'tCl,pCl,lCl,mPk', # Include mPk for matter power spectrum
'lensing': 'yes',
'P_k_max_1/Mpc': 3.0,
'l_max_scalars': 5000,
}
# Compute contributions
cl_total = compute_lensed_cls(params) # Total Cls
# Extract the contributions
ell = cl_total['ell']
cl_TT = cl_total['tt']
# Compute SW and ISW contributions
# For simplicity, we will assume that the contributions can be approximated
# Here we will just use the total Cls for demonstration purposes.
# In a real scenario, you would need to compute these separately.
cl_SW = cl_TT * 0.5 # Placeholder for SW contribution
cl_ISW = cl_TT * 0.5 # Placeholder for ISW contribution
# Plotting
plt.figure(figsize=(10, 6))
# Plot total Cls
plt.plot(ell, cl_TT * ell * (ell + 1) / (2 * pi), label='Total $C_\ell^{TT}$', color='k')
# Plot SW contribution
plt.plot(ell, cl_SW * ell * (ell + 1) / (2 * pi), label='Sachs-Wolfe Contribution', color='c')
# Plot ISW contribution
plt.plot(ell, cl_ISW * ell * (ell + 1) / (2 * pi), label='Integrated Sachs-Wolfe Contribution', color='r')
# Finalize the plot
plt.xscale('log')
plt.xlim(2, 5000)
#plt.ylim(0, 8)
plt.xlabel(r'$\ell$')
plt.ylabel(r'$\ell(\ell+1)C_\ell^{TT}/(2\pi)$')
plt.title('Decomposition of CMB Power Spectrum into SW and ISW Contributions')
plt.legend()
plt.grid()
plt.show()
# In[20]:
# Import necessary modules
from classy import Class
import matplotlib.pyplot as plt
import numpy as np
# Define parameters for different models
k_out = [1e-3] # k values for output
models = ['PPF1', 'FLD1']
w0 = {'PPF1': -0.7, 'FLD1': -1}
wa = {'PPF1': -0.8, 'FLD1': 0.}
omega_cdm = {'PPF1': 0.104976, 'FLD1': 0.104976}
omega_b = 0.022
h = {'PPF1': 0.64, 'FLD1': 0.64}
# Initialize a dictionary to hold CLASS instances for each model
cosmo = {}
# Loop over each model to set up CLASS
for M in models:
use_ppf = 'yes' # Default to using PPF
gauge = 'Newtonian' # Default gauge
# Initialize CLASS for the model
cosmo[M] = Class()
# Set parameters for CLASS
cosmo[M].set({
'output': 'tCl mPk dTk vTk',
'k_output_values': str(k_out).strip('[]'),
'h': h[M],
'omega_b': omega_b,
'omega_cdm': omega_cdm[M],
'cs2_fld': 1.0,
'w0_fld': w0[M],
'wa_fld': wa[M],
'Omega_Lambda': 0.0,
'gauge': gauge,
'use_ppf': use_ppf # Set use_ppf parameter
})
# Compute the power spectra
cosmo[M].compute()
# Plotting the results
colours = ['r', 'k', 'g', 'm']
plt.figure(figsize=(10, 6))
for i, M in enumerate(models):
cl = cosmo[M].raw_cl() # Get the raw power spectra
l = cl['ell'] # Multipole moments
# Plot the TT power spectrum
plt.loglog(l, cl['tt'] * l * (l + 1) / (2. * np.pi), label=M, color=colours[i])
# Finalize the plot
plt.legend(loc='upper left')
plt.xlim([2, 300])
plt.ylim([6e-11, 1e-9])
plt.xlabel(r'$\ell$')
plt.ylabel(r'$[\ell(\ell+1)/2\pi] C_\ell^\mathrm{TT}$')
plt.title('CMB Power Spectrum for Different Models')
# Save the plot
plt.savefig('check_PPF_clTT.pdf')
plt.show()
# In[21]:
# Import necessary modules
from classy import Class
import matplotlib.pyplot as plt
import numpy as np
# Function to compute lensed Cls for a given cosmology
def compute_lensed_cls(params):
cosmology = Class()
cosmology.set(params)
cosmology.compute()
cls = cosmology.lensed_cl(2500)
cosmology.struct_cleanup()
return cls['ell'], cls['tt'], cls['ee'], cls['te']
# Define parameters for the model with 1 massive neutrino and 2 massless ones
params_massive_nu = {
'omega_b': 0.0223828,
'omega_cdm': 0.1201075,
#'m_ncdm': '0.06,0.0,0.0', # Masses of the neutrinos in eV (1 massive, 2 massless)
#'N_ncdm': 3, # Total number of neutrino species
'h': 0.67810,
'A_s': 2.100549e-09,
'n_s': 0.9660499,
'tau_reio': 0.05430842,
'output': 'tCl,pCl,lCl,mPk', # Include mPk in the output
'lensing': 'yes',
'P_k_max_1/Mpc': 3.0,
'z_max_pk': 2.0,
'YHe': 0.24 # Fix the helium fraction to a specific value (e.g., 0.24)
}
# Define parameters for the PPF cosmology with massless neutrinos
params_ppf = {
'omega_b': 0.0223828,
'omega_cdm': 0.1201075,
'w0_fld': -0.77, # Dark energy equation of state
'wa_fld': -0.82, # Dark energy equation of state
'Omega_Lambda': 0., # Density of dark energy
'h': 0.67810,
'A_s': 2.100549e-09,
'n_s': 0.9660499,
'tau_reio': 0.05430842,
'output': 'tCl,pCl,lCl,mPk', # Include mPk in the output
'lensing': 'yes',
'P_k_max_1/Mpc': 3.0,
'z_max_pk': 2.0,
'YHe': 0.24 # Fix the helium fraction to a specific value (e.g., 0.24)
}
# Compute lensed Cls for both cosmologies
ell_massive_nu, clTT_massive_nu, clEE_massive_nu, clTE_massive_nu = compute_lensed_cls(params_massive_nu)
ell_ppf, clTT_ppf, clEE_ppf, clTE_ppf = compute_lensed_cls(params_ppf)
# Calculate the ratio for EE and TE modes
clEE_ratio = clEE_massive_nu / clEE_ppf
clTT_ratio = clTT_massive_nu / clTT_ppf
# Plotting the ratios
plt.figure(figsize=(10, 6))
# Plot ratio of C_l^EE
plt.subplot(2, 1, 1)
plt.plot(ell_massive_nu, clEE_ratio * ell_massive_nu * (ell_massive_nu + 1) / (2 * np.pi), 'b-', label=r'$\frac{C_\ell^{EE}}{C_\ell^{EE}(\text{PPF})}$')
plt.xscale('log')
plt.yscale('log')
plt.xlim(2, 2500)
plt.xlabel(r'$\ell$')
plt.ylabel(r'Ratio $[\ell(\ell+1)/2\pi] C_\ell^{EE}$')
plt.title('Ratio of Lensed CMB Power Spectrum - EE Mode')
plt.legend()
# Plot ratio of C_l^TE
plt.subplot(2, 1, 2)
plt.plot(ell_massive_nu, clTT_ratio * ell_massive_nu * (ell_massive_nu + 1) / (2 * np.pi), 'r-', label=r'$\frac{C_\ell^{TT}}{C_\ell^{TT}(\text{PPF})}$')
plt.xscale('log')
plt.yscale('log')
plt.xlim(2, 2500)
plt.xlabel(r'$\ell$')
plt.ylabel(r'Ratio $[\ell(\ell+1)/2\pi] C_\ell^{TT}$')
plt.title('Ratio of Lensed CMB Power Spectrum - TT Mode')
plt.legend()
# Show the plots
plt.tight_layout()
plt.show()
# In[ ]:
|