{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from scipy.integrate import quad\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import PyCosmo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.4.3'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PyCosmo.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Age of the Universe\n",
    "\n",
    "We recall the Friedmann equation, which describes the evolution of the scale factor $a(t)$:\n",
    "\\begin{equation}\n",
    "H^2(t) = \\frac{8\\pi G}{3}\\left[ \\rho(t) + \\frac{\\rho_\\mathrm{crit} - \\rho_0}{a^2(t)} \\right],\n",
    "\\end{equation}\n",
    "where $H(t)\\equiv\\dot{a}/a$ is the Hubble rate, $G$ is Newton's constant, $\\rho(t)$ is the energy density in the unverse as a function of time with $\\rho_0$ being its value today. The critical density $\\rho_\\mathrm{crit}\\equiv\\frac{3H_0^2}{8\\pi G}$.\n",
    "1. Assume that the Universe is flat with matter and a cosmological constant, whose energy density remains constant with time. Re-write the Friedmann equation as\n",
    "\t\\begin{equation}\n",
    "\t\\mathrm{d}t = H_0^{-1}\\frac{\\mathrm{d}a}{a}\\left[ \\Omega_\\Lambda + \\frac{1 - \\Omega_\\Lambda}{a^3} \\right]^{-1/2},\n",
    "\t\\end{equation}\n",
    "\twhere $\\Omega_\\Lambda$ is the ratio of the energy density in the cosmological constant to the critical density.\n",
    "2. We can integrate this equation from $a=0$ (when $t=0$) until today ($a=1$) to get the age of the universe today. Compute the integral for the following cases:\n",
    "\t- a universe with only matter $\\Omega_\\Lambda = 0$ (analytically).\n",
    "\t- a universe dominated by dark energy $\\Omega_\\Lambda = 0.7$ (numerically).\n",
    "\t- For a fixed $H_0$, which universe is older?\n",
    "\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Integral: 0.50000\n",
      "Estimated Error: 0.00000\n"
     ]
    }
   ],
   "source": [
    "# Example on how to define functions and intergrate numerically\n",
    "\n",
    "# define a function\n",
    "f = lambda x: x\n",
    "\n",
    "# integrate from 0 to 1\n",
    "i, e = quad(f, 0, 1)\n",
    "\n",
    "# i contains the numerically integrated value\n",
    "# e contains the estimated error\n",
    "print(\"Integral: %.5f\" %(i))\n",
    "print(\"Estimated Error: %.5f\" %(e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO: calculate the age of the universe numerically\n",
    "# by following the example above.\n",
    "# Define the proper function and integrate it numerically"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Angular diameter distance\n",
    "\n",
    "Consider a galaxy of physical (visible) size of 5 kpc (1 pc $\\approx$ 3.26 light-years). What angle would the galaxy subtend if situated at redshift 0.1? Redshift 1.0?\n",
    "\n",
    "1. Do the calculation analytically in a flat universe, that contains only matter $\\Omega_M = 1.0$.\n",
    "\n",
    "2. Use PyCosmo and do the calculation numercally for a universe with the following parameters $\\Omega_M = 0.25$, $\\Omega_\\Lambda=0.7$ and $\\Omega_b=0.05$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the Cosmology\n",
    "Cosmo = PyCosmo.build()\n",
    "#TODO: Set the parameters accordingle\n",
    "Cosmo.set(h=0.7, omega_b=, omega_m=, omega_l_in=\"flat\")\n",
    "\n",
    "# Function to calculate the angular diameter distance (returns result in Mpc)\n",
    "# uses the scaling parameter as input\n",
    "# dist_ang = Cosmo.background.dist_ang_a(a=...)\n",
    "\n",
    "# TODO: calculate the angle of the galaxy"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}