{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "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": 1,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pylab as plt\n",
    "import pyccl as ccl"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "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": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "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": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameters\n",
      "----------\n",
      "        Omega_c: 0.27\n",
      "        Omega_b: 0.045\n",
      "        Omega_m: 0.315\n",
      "        Omega_k: 0.0\n",
      "        Omega_l: 0.684927855904\n",
      "             w0: -1.0\n",
      "             wa: 0.0\n",
      "             H0: 67.0\n",
      "              h: 0.67\n",
      "            A_s: 2.1e-09\n",
      "            n_s: 0.96\n",
      "       N_nu_rel: 3.046\n",
      "      N_nu_mass: 0.0\n",
      "            mnu: 0.0\n",
      "   Omega_n_mass: 0.0\n",
      "    Omega_n_rel: 1.70947512533e-05\n",
      "          T_CMB: 2.725\n",
      "        Omega_g: 5.50493446829e-05\n",
      "         z_star: nan\n",
      "    has_mgrowth: False\n",
      "\n",
      "Precomputed data\n",
      "----------------\n",
      "  has_distances: False\n",
      "     has_growth: False\n",
      "      has_power: False\n",
      "      has_sigma: False\n",
      "\n",
      "Status\n",
      "------\n",
      "status(0): C\n"
     ]
    }
   ],
   "source": [
    "print cosmo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "As you can see, a number of cosmological parameters have been set to default values, or derived from the input parameters. Some, like `sigma_8`, 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": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Parameter values can be accessed from the `Parameters` object that the `Cosmology` object contains, like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.27\n"
     ]
    }
   ],
   "source": [
    "print cosmo.params['Omega_c']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": 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": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1962.9612048132053"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = 0.5 \n",
    "ccl.comoving_radial_distance(cosmo, 1/(1+z)) # Mpc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "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": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([    0.        ,   436.69947016,   851.39817459,  1243.79753712,\n",
       "        1614.11360802,  1962.96120481,  2291.23813831,  2600.02282463,\n",
       "        2890.49121176,  3163.8538544 ])"
      ]
     },
     "execution_count": 6,
     "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": {
    "deletable": true,
    "editable": true
   },
   "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": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1962.9612048132053"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ccl.comoving_angular_distance(cosmo, 1/(1+z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "If we create a cosmology with curvature, we'll get a different result. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Radial Dist. = 1992.55 Mpc \t Angular Dist. = 1999.14 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": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "CCL explictly supports the calculation of the luminosity distance and the distance modulus too:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Luminosity Dist = 2944.44 Mpc \t Distance Modulus = 42.35 \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": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Finally, CCL supports an inverse operation, which calculates the scale factor for a given comoving distance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6666668249534857"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ccl.scale_factor_of_chi(cosmo, 1962.96)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
