{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Calculate cosmological distances with CCL\n", "In this example, we will calculate various cosmological distances for an example cosmology." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pylab as plt\n", "import pyccl as ccl\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up a Cosmology object\n", "`Cosmology` objects contain the parameters and metadata needed as inputs to most functions. Each `Cosmology` object has a set of cosmological parameters attached to it. In this example, we will only use the parameters of a vanilla LCDM model, but simple extensions (like curvature, neutrino mass, and w0/wa) are also supported.\n", "\n", "`Cosmology` objects also contain precomputed data (e.g. splines) to help speed-up certain calculations. As such, `Cosmology` objects are supposed to be immutable; you should create a new `Cosmology` object when you want to change the values of any cosmological parameters." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "cosmo = ccl.Cosmology(Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=2.1e-9, n_s=0.96)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pyccl.Cosmology(Omega_c=0.27, Omega_b=0.045, h=0.67, n_s=0.96, sigma8=None, A_s=2.1e-09, Omega_k=0.0, Omega_g=None, Neff=3.046, w0=-1.0, wa=0.0, T_CMB=None, bcm_log10Mc=14.079181246047625, bcm_etab=0.5, bcm_ks=55.0, mu_0=0.0, sigma_0=0.0, m_nu=0.0, m_nu_type=None, z_mg=None, df_mg=None, transfer_function='boltzmann_camb', matter_power_spectrum='halofit', baryons_power_spectrum='nobaryons', mass_function='tinker10', halo_concentration='duffy2008', emulator_neutrinos='strict')\n" ] } ], "source": [ "print(cosmo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, a number of cosmological parameters have been set to default values, or derived from the input parameters. Some, like `sigma8`, have been left undefined; this is because calculating them from the input parameters is non-trivial, so this will only be done if needed (or if the user explicitly requests it)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Parameter values can be accessed from the `Cosmology` object contains, like so:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.27\n" ] } ], "source": [ "print(cosmo['Omega_c'])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Cosmological Distances\n", "\n", "With a cosmology in hand, we can begin performing some calculations. We can start with the most basic measure, the comoving radial distance. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1962.939114147973" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = 0.5 \n", "ccl.comoving_radial_distance(cosmo, 1/(1+z)) # Mpc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that all distance function calls require scale factors, not redshifts. This function can take a `numpy` array of values as well." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 436.69850865, 851.39441882, 1243.78927092,\n", " 1614.09913469, 1962.93911415, 2291.20727664, 2599.98207275,\n", " 2890.43970306, 3163.79085733])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zs = np.arange(0, 1, 0.1)\n", "ccl.comoving_radial_distance(cosmo, 1/(1+zs))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "CCL also supports calculation of the comoving angular distance. In flat spacetime (like the cosmology we have here) it is the same as the radial distance. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1962.939114147973" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ccl.comoving_angular_distance(cosmo, 1/(1+z))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we create a cosmology with curvature, we'll get a different result. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Radial Dist. = 1992.53 Mpc \t Angular Dist. = 1999.12 Mpc\n" ] } ], "source": [ "curved_cosmo = ccl.Cosmology(Omega_k = 0.1, Omega_c=0.17, Omega_b=0.045, h=0.67, A_s=2.1e-9, n_s=0.96)\n", "\n", "chi_rad = ccl.comoving_radial_distance(curved_cosmo, 1/(1+z))\n", "chi_curved = ccl.comoving_angular_distance(curved_cosmo, 1/(1+z))\n", "\n", "print ('Radial Dist. = %.2f Mpc \\t Angular Dist. = %.2f Mpc'%(chi_rad, chi_curved))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "CCL explictly supports the calculation of the luminosity distance and the distance modulus too:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Luminosity Dist = 2944.41 Mpc \t Distance Modulus = 42.34 \n" ] } ], "source": [ "chi_lum = ccl.luminosity_distance(cosmo, 1/(1+z))\n", "DM = ccl.distance_modulus(cosmo, 1/(1+z))\n", "print('Luminosity Dist = %.2f Mpc \\t Distance Modulus = %.2f ' % (chi_lum, DM))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, CCL supports an inverse operation, which calculates the scale factor for a given comoving distance:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6666639275736294" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ccl.scale_factor_of_chi(cosmo, 1962.96)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }