{ "cells": [ { "cell_type": "markdown", "id": "a3461285-3f18-4ba7-9619-4ead5fa207c5", "metadata": {}, "source": [ "# Intialize" ] }, { "cell_type": "code", "execution_count": 1, "id": "2c4d4199-c6fb-4634-9520-cacfce177bd8", "metadata": {}, "outputs": [], "source": [ "from classy_sz import Class as Class_sz" ] }, { "cell_type": "markdown", "id": "3b4c4879-1779-4f43-bec6-dbe87343f1b2", "metadata": {}, "source": [ "# Baseline parameterization" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3decd95-ed51-4f4e-a506-2b05a7a8c531", "metadata": {}, "outputs": [], "source": [ "# the baseline parameterization is:\n", "\n", "cosmo_params = {\n", "'omega_b': 0.02242,\n", "'omega_cdm': 0.11933,\n", "'H0': 67.66, # use H0 because this is what is used by the emulators and to avoid any ambiguity when comparing with camb. \n", "'tau_reio': 0.0561,\n", "'ln10^{10}A_s': 3.047,\n", "'n_s': 0.9665 \n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "adc6d04e-dc76-4a34-af9c-2c9a7f1f55fb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 855 ms, sys: 173 ms, total: 1.03 s\n", "Wall time: 173 ms\n" ] } ], "source": [ "%%time \n", "classy_sz = Class_sz()\n", "classy_sz.set(cosmo_params)\n", "classy_sz.set({\n", "'output':'tCl,lCl,pCl',\n", "})\n", "classy_sz.compute_class_szfast()" ] }, { "cell_type": "markdown", "id": "2c8b1cd5-fb2f-4aa6-9a08-ad4711142e76", "metadata": {}, "source": [ "### collect derived parameters\n", "\n", "Here are some examples. More parameters are available." ] }, { "cell_type": "code", "execution_count": 4, "id": "0dceaf32-6070-4111-a38d-9aef384e1d7b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8118792418260571" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.sigma8()" ] }, { "cell_type": "code", "execution_count": 5, "id": "b5799c52-0603-4fde-a747-cb652a09b239", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.044172067466906" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.Neff()" ] }, { "cell_type": "code", "execution_count": 6, "id": "09435a8d-e12d-41df-b623-f9c47d03cf76", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'A_s': 2.105209331337507e-09}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['A_s'])" ] }, { "cell_type": "code", "execution_count": 7, "id": "ec42bf86-98d4-4dad-9ed9-38a8381f3fa1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'logA': 3.047}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['logA'])" ] }, { "cell_type": "code", "execution_count": 8, "id": "f980188d-a454-488c-b5ff-00d6e6b89f4a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ln10^{10}A_s': 3.047}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['ln10^{10}A_s'])" ] }, { "cell_type": "code", "execution_count": 9, "id": "9bab16e4-2b94-4373-8133-3f31596e9665", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Neff': 3.044172067466906}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['Neff'])" ] }, { "cell_type": "code", "execution_count": 10, "id": "29e17d38-8b6a-4667-b972-159eb3cde110", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Omega_m': 0.30964144154550644}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['Omega_m'])" ] }, { "cell_type": "markdown", "id": "9f248183-715f-40c1-9b5e-9986a84e5b73", "metadata": {}, "source": [ "Note that to access some neutrinos parameters, you may need to run the background solver (always skipped by default in the fast mode).\n", "\n", "This is done by setting `skip_background_and_thermo: 0` (i.e., do not skip background and thermo modules)" ] }, { "cell_type": "code", "execution_count": 11, "id": "9da78eaa-fa8c-4510-b02f-4169f06ce798", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 749 ms, sys: 190 ms, total: 939 ms\n", "Wall time: 174 ms\n" ] } ], "source": [ "%%time \n", "classy_sz = Class_sz()\n", "classy_sz.set(cosmo_params)\n", "classy_sz.set({\n", "'output':'tCl,lCl,pCl',\n", "'skip_background_and_thermo': 0,\n", "\n", "# tell class what neutrino model you are working with (be careful, make sure it is consistent with your emulator choice!)\n", "'N_ncdm': 1,\n", "'m_ncdm': 0.02,\n", "'deg_ncdm': 3,\n", "})\n", "classy_sz.compute_class_szfast()" ] }, { "cell_type": "code", "execution_count": 12, "id": "6a027b28-d271-4e00-a6c2-42720eeb273b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'m_ncdm_tot': 0.060025303775261975}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['m_ncdm_tot'])" ] }, { "cell_type": "code", "execution_count": 13, "id": "fdc16b71-18b2-499c-9326-c63d52ea8b2f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.0014077779095836118, 0.0014071844582622115)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.Omega_nu, 3.*0.02/93.14/classy_sz.h()**2" ] }, { "cell_type": "markdown", "id": "33b61257-33f1-4ff0-83a9-f29a1e16686a", "metadata": {}, "source": [ "### Matter and total densities\n", "\n", "In `class_sz`, we follow the same conventions as `class`.\n", "\n", "The budget equation is:\n", "\n", "$$\n", "\\Omega_{tot} = 1 = \\Omega_{m} + \\Omega_{r} + \\Omega_{\\lambda}\n", "$$\n", "\n", "With \n", "\n", "$$\n", "\\Omega_{m} = \\Omega_{c} + \\Omega_{b} + \\Omega_{ncdm}^{nr}\n", "$$\n", "\n", "with $\\Omega_{ncdm}^{nr} = (\\rho_{ncdm} - 3p_{ncdm})/\\rho_{tot}$ the non-relativistic contribution of other species (like massive neutrinos).\n", "\n", "and \n", "\n", "$$\n", "\\Omega_{r} = \\Omega_{g} + 3p_{ncdm}/\\rho_{tot} + \\Omega_{ur}\n", "$$\n", "\n", "with \n", "\n", "$$\n", "\\Omega_{ur} = N_{ur}\\frac{7}{8} \\left(\\frac{4}{11}\\right)^{4/3}\\Omega_{g}\n", "$$\n", "\n", "We also denote\n", "\n", "$$\n", "\\Omega_{ncdm,0} = \\frac{\\rho_{ncdm,0}}{\\rho_{tot,0}} = \\frac{M_{ncdm,tot}}{93.14h^2\\,[\\mathrm{eV}]}\n", "$$\n", "\n" ] }, { "cell_type": "markdown", "id": "a72e1dc7-8ef9-4718-b955-c4d5731f3a37", "metadata": {}, "source": [ "### Accessing the list of set params" ] }, { "cell_type": "code", "execution_count": 14, "id": "415c574c-3cd5-4225-89b4-7a7a07e6e703", "metadata": {}, "outputs": [], "source": [ "# the baseline parameterization is:\n", "\n", "cosmo_params = {\n", "'omega_b': 0.02242,\n", "'omega_cdm': 0.11933,\n", "'H0': 67.66, # use H0 because this is what is used by the emulators and to avoid any ambiguity when comparing with camb. \n", "'tau_reio': 0.0561,\n", "'ln10^{10}A_s': 3.047,\n", "'n_s': 0.9665 \n", "}" ] }, { "cell_type": "code", "execution_count": 15, "id": "bb7972e7-f899-4c41-9e59-d6637eaedcb1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 371 ms, sys: 87.1 ms, total: 458 ms\n", "Wall time: 73.3 ms\n" ] } ], "source": [ "%%time \n", "classy_sz = Class_sz()\n", "classy_sz.set(cosmo_params)\n", "classy_sz.set({\n", "'output':'tCl,lCl,pCl',\n", "})\n", "classy_sz.compute_class_szfast()" ] }, { "cell_type": "code", "execution_count": 16, "id": "8fde65dc-ab8b-4456-b5a5-50ec64171f14", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'output': 'tCl,lCl,pCl',\n", " 'skip_input': 1,\n", " 'skip_background_and_thermo': 1,\n", " 'skip_pknl': 1,\n", " 'skip_pkl': 1,\n", " 'skip_chi': 1,\n", " 'skip_hubble': 1,\n", " 'skip_class_sz': 1,\n", " 'skip_sigma8_at_z': 1,\n", " 'skip_sigma8_and_der': 0,\n", " 'skip_cmb': 0,\n", " 'cosmo_model': 6,\n", " 'jax': 0,\n", " 'classy_sz_verbose': 'none',\n", " 'ndim_masses': 500,\n", " 'ndim_redshifts': 100,\n", " 'sBBN file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//bbn/PRIMAT21_class_format.dat',\n", " 'A10_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/class_sz_lnIgnfw-and-d2lnIgnfw-vs-lnell-over-ell500_A10.txt',\n", " 'P13_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/class_sz_lnIgnfw-and-d2lnIgnfw-vs-lnell-over-ell500_P13.txt',\n", " 'Tinker_et_al_10_alpha_consistency_msyriac_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/Tinker_et_al_10_alpha_consistency_msyriac.txt',\n", " 'full_path_to_dndz_gal': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/WISC_bin3_ngal_example1.txt',\n", " 'full_path_and_prefix_to_dndz_ngal': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/WISC_bin3_ngal_example',\n", " 'full_path_to_source_dndz_gal': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/WISC_bin3.txt',\n", " 'cib_Snu_file_snu': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/filtered_snu_planck_fine.txt',\n", " 'cib_Snu_file_z': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/filtered_snu_planck_z_fine.txt',\n", " 'cib_Snu_file_nu': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/filtered_snu_planck_nu_fine.txt',\n", " 'SZ_cat_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/SZ_cat.txt',\n", " 'sz_selection_function_thetas_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/SZ_thetas.txt',\n", " 'sz_selection_function_skyfracs_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/SZ_skyfracs.txt',\n", " 'sz_selection_function_ylims_file': '/Users/boris/class_sz_data_directory/class_sz/class-sz//class_sz_auxiliary_files/includes/SZ_ylims.txt',\n", " 'omega_b': 0.02242,\n", " 'omega_cdm': 0.11933,\n", " 'H0': 67.66,\n", " 'tau_reio': 0.0561,\n", " 'ln10^{10}A_s': 3.047,\n", " 'n_s': 0.9665}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.pars" ] }, { "cell_type": "markdown", "id": "950511cf-1388-4137-bf51-5882ffe2a788", "metadata": {}, "source": [ "# Using different names" ] }, { "cell_type": "code", "execution_count": 27, "id": "addb021c-90a9-4962-800f-2894bbe817ad", "metadata": {}, "outputs": [], "source": [ "# the baseline parameterization is:\n", "\n", "cosmo_params = {\n", "'omega_b': 0.02242, # here we can use ombh2 rather than omega_b\n", "'omch2': 0.10933, # here we can use omch2 rather than omega_cdm\n", "'H_0': 67.66, # use H0 or H_0 because this is what is used by the emulators and to avoid any ambiguity when comparing with camb. \n", "'tau_reio': 0.0561,\n", "'logA': 3.047, # here we can use logA rather than ln10^{10}A_s\n", "'ns': 0.9665 # here we can use ns rather than ns\n", "}" ] }, { "cell_type": "code", "execution_count": 28, "id": "1bdff897-c909-42d3-95e5-64bf4b7775dc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sigma8: 0.7670913521238548\n", "CPU times: user 702 ms, sys: 124 ms, total: 826 ms\n", "Wall time: 124 ms\n" ] } ], "source": [ "%%time \n", "classy_sz = Class_sz()\n", "classy_sz.set(cosmo_params)\n", "classy_sz.set({\n", "'output':'tCl,lCl,pCl',\n", "})\n", "classy_sz.compute_class_szfast()\n", "\n", "print('sigma8:',classy_sz.sigma8())" ] }, { "cell_type": "markdown", "id": "7111a6af-f20a-40c0-9f72-88b28602d7a7", "metadata": {}, "source": [ "# Passing $\\sigma_8$ instead of $A_s$\n", "\n", "classy_sz can have $\\sigma_8$ as an input parameter. \n", "\n", "In this case the calculation takes a bit longer because it requires a root finding step to get the adequate value of $A_s$. " ] }, { "cell_type": "code", "execution_count": 29, "id": "35f57dc3-f03d-4f3a-9f11-1d84668a75d3", "metadata": {}, "outputs": [], "source": [ "cosmo_params_with_sigma_8 = {\n", "'omega_b': 0.02242,\n", "'omega_cdm': 0.11933,\n", "'H0': 67.66, # use H0 because this is what is used by the emulators and to avoid any ambiguity when comparing with camb. \n", "'tau_reio': 0.0561,\n", "'sigma8': 0.8119,\n", "'n_s': 0.9665 \n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 30, "id": "a52d85a1-220d-4595-bf76-9742cadd0104", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.61 s, sys: 234 ms, total: 1.84 s\n", "Wall time: 281 ms\n" ] } ], "source": [ "%%time \n", "classy_sz = Class_sz()\n", "classy_sz.set(cosmo_params_with_sigma_8)\n", "classy_sz.set({\n", "'output':'tCl,lCl,pCl',\n", "})\n", "classy_sz.compute_class_szfast()" ] }, { "cell_type": "code", "execution_count": 31, "id": "ae955e32-10c1-4058-8fec-14d09a814719", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'A_s': 2.1053170341400974e-09}" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['A_s'])" ] }, { "cell_type": "code", "execution_count": 32, "id": "748e458c-c012-4d5b-880d-f7fca156719f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'logA': 3.047051158830638}" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['logA'])" ] }, { "cell_type": "code", "execution_count": 33, "id": "58accd00-f257-44f8-9a31-db03cfe8929d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Neff': 3.0441720915141284}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_current_derived_parameters(['Neff'])" ] }, { "cell_type": "markdown", "id": "dbb1384d-5be8-4ab3-8ac4-01cb6838b509", "metadata": {}, "source": [ "# Compute with Jax" ] }, { "cell_type": "code", "execution_count": 1, "id": "8a60c68e-9f9f-42be-976d-87aa330bd053", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "from classy_sz import Class as Class_sz\n", "import jax.numpy as jnp\n", "from jax import jacfwd" ] }, { "cell_type": "code", "execution_count": 2, "id": "136142b8-4698-467a-9587-e3adec96d5c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 823 ms, sys: 182 ms, total: 1.01 s\n", "Wall time: 1.4 s\n" ] } ], "source": [ "%%time\n", "cosmo_params = {\n", "'omega_b': 0.02242,\n", "'omega_cdm': 0.11933,\n", "'H0': 67.66, # use H0 because this is what is used by the emulators.\n", "'tau_reio': 0.0561,\n", "'ln10^{10}A_s': 3.047,\n", "'n_s': 0.9665,\n", "}\n", "\n", "# initialize computation\n", "classy_sz = Class_sz()\n", "classy_sz.set(cosmo_params)\n", "classy_sz.set({\n", "'output':' ',\n", "'jax': 1,\n", "})\n", "classy_sz.compute_class_szfast()" ] }, { "cell_type": "code", "execution_count": 3, "id": "c967b09d-f152-45d4-942f-5dcb045d8603", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Array([1.04148978e+00, 8.11879242e-01, 2.45858174e-01, 7.84135042e+00,\n", " 3.04417207e+00, 2.80969597e+02, 1.08869035e+03, 1.44643192e+02,\n", " 1.38876674e+04, 2.81716002e+02, 1.08487887e+03, 1.44977738e+02,\n", " 1.38867784e+04, 1.47204456e+02, 1.37886580e+01, 2.73113427e+01,\n", " 2.56297550e+01], dtype=float64)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classy_sz.get_sigma8_and_der(params_values_dict=cosmo_params)" ] }, { "cell_type": "markdown", "id": "af3209d6-070b-4c9a-adcd-5cb3c40de943", "metadata": {}, "source": [ "ordering:\n", "\n", "```\n", "cosmopower_derived_params_names = ['100*theta_s',\n", " 'sigma8',\n", " 'YHe',\n", " 'z_reio',\n", " 'Neff',\n", " 'tau_rec',\n", " 'z_rec',\n", " 'rs_rec',\n", " 'ra_rec',\n", " 'tau_star',\n", " 'z_star',\n", " 'rs_star',\n", " 'ra_star',\n", " 'rs_drag']\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "id": "a7dc1659-507a-4fc9-94b9-7e67a0187c98", "metadata": {}, "outputs": [], "source": [ "Omega_c = 0.26\n", "\n", "def z_CMB(Omega_c):\n", " omega_cdm = Omega_c*(cosmo_params['H0']/100.)**2\n", " cosmo_params.update({'omega_cdm':omega_cdm})\n", " z_CMB = classy_sz.get_sigma8_and_der(params_values_dict=cosmo_params)[6]\n", " return z_CMB" ] }, { "cell_type": "code", "execution_count": 5, "id": "82ea85fa-91d6-4af1-ab7c-5926f88dcc4f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Array(1088.66830151, dtype=float64)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z_CMB(Omega_c)" ] }, { "cell_type": "code", "execution_count": 6, "id": "5e45085c-3519-4449-af88-47a4891b11a7", "metadata": {}, "outputs": [], "source": [ "dz_CMB = jacfwd(z_CMB)" ] }, { "cell_type": "code", "execution_count": 7, "id": "16568c75-4829-4ff6-8fc0-e63f70a49d21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.03 s, sys: 20.5 ms, total: 1.05 s\n", "Wall time: 1 s\n" ] }, { "data": { "text/plain": [ "Array(33.08539939, dtype=float64)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "dz_CMB(Omega_c)" ] }, { "cell_type": "code", "execution_count": 8, "id": "372c923f-3df4-4bcc-b35c-ef35b95cd765", "metadata": {}, "outputs": [], "source": [ "def D_CMB(Omega_c):\n", " omega_cdm = Omega_c*(cosmo_params['H0']/100.)**2\n", " cosmo_params.update({'omega_cdm':omega_cdm})\n", " ra_rec = classy_sz.get_sigma8_and_der(params_values_dict=cosmo_params)[8]\n", " return ra_rec/(1.+z_CMB(Omega_c))" ] }, { "cell_type": "code", "execution_count": 9, "id": "2741c5f1-1aa4-4fa8-aed2-61e223f74231", "metadata": {}, "outputs": [], "source": [ "dD_CMB = jacfwd(D_CMB)" ] }, { "cell_type": "code", "execution_count": 10, "id": "697d4bb6-b4ba-404e-94fe-caedd1733c1c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Array(-16.7666128, dtype=float64)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dD_CMB(Omega_c)" ] } ], "metadata": { "kernelspec": { "display_name": "Python (venv_class_sz)", "language": "python", "name": "venv_class_sz312" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }