{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Schwarzschild spacetime\n",
    "\n",
    "This notebook shows how you can do component computations in Cadabra. It is a translation of the\n",
    "Cadabra notebook https://cadabra.science/notebooks/schwarzschild.html to Jupyter. \n",
    "\n",
    "The first thing we always need to do is to declare the coordinates used, and the names of the indices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}\\text{Attached property Coordinate to}\\left[r, t, \\phi, \\theta\\right].$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "${}\\text{Attached property Indices(position=fixed) to}\\left[\\mu, \\nu, \\rho, \\sigma, \\lambda, \\kappa, \\chi, \\gamma\\right].$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "${}\\text{Attached property PartialDerivative to}\\partial{\\#}.$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "{r,t,\\phi,\\theta}::Coordinate;\n",
    "{\\mu,\\nu,\\rho,\\sigma,\\lambda,\\kappa,\\chi,\\gamma}::Indices(values={t,r,\\phi,\\theta}, position=fixed);\n",
    "\\partial{#}::PartialDerivative;\n",
    "g_{\\mu\\nu}::Metric.\n",
    "g^{\\mu\\nu}::InverseMetric."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below is the Schwarzschild metric in standard coordinates. Note how the components are\n",
    "given in terms of substitution rules, and how the inverse metric is computed. \n",
    "The \\algo{complete} algorithm adds the rules for the inverse metric to the rules for the metric."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}\\left[g_{t t} = -1+2M {r}^{-1}, g_{r r} = {\\left(1-2M {r}^{-1}\\right)}^{-1}, g_{\\theta \\theta} = {r}^{2}, g_{\\phi \\phi} = {r}^{2} {\\left(\\sin{\\theta}\\right)}^{2}, g^{t t} = {\\left(2M {r}^{-1}-1\\right)}^{-1}, g^{r r} = -2M {r}^{-1}+1, g^{\\phi \\phi} = {\\left({r}^{2} {\\left(\\sin{\\theta}\\right)}^{2}\\right)}^{-1}, g^{\\theta \\theta} = {r}^{-2}\\right]$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ss:= { g_{t t} = -(1-2 M/r),   \n",
    "       g_{r r} = 1/(1-2 M/r), \n",
    "       g_{\\theta\\theta} = r**2, \n",
    "       g_{\\phi\\phi}=r**2 \\sin(\\theta)**2\n",
    "     }.\n",
    "\n",
    "complete(ss, $g^{\\mu\\nu}$);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now compute the Christoffel symbols. We give Cadabra the expression for the \n",
    "Christoffel symbols in terms of the metric, and then evaluate the components of the\n",
    "metric using the `evaluate` algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}\\Gamma^{\\mu}\\,_{\\nu \\rho} = \\frac{1}{2}g^{\\mu \\sigma} \\left(\\partial_{\\rho}{g_{\\nu \\sigma}}+\\partial_{\\nu}{g_{\\rho \\sigma}}-\\partial_{\\sigma}{g_{\\nu \\rho}}\\right)$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "${}\\Gamma^{\\mu}\\,_{\\nu \\rho} = \\square{}_{\\nu}{}_{\\rho}{}^{\\mu}\\left\\{\\begin{aligned}\\square{}_{\\phi}{}_{r}{}^{\\phi}& = {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\theta}{}^{\\phi}& = {\\left(\\tan{\\theta}\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{r}{}^{\\theta}& = {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{r}{}^{r}& = M {\\left(r \\left(2M-r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{r}{}^{t}& = M {\\left(r \\left(-2M+r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{\\phi}{}^{\\phi}& = {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{\\phi}{}^{\\phi}& = {\\left(\\tan{\\theta}\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{\\theta}{}^{\\theta}& = {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{t}{}^{t}& = M {\\left(r \\left(-2M+r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\phi}{}^{r}& = \\left(2M-r\\right) {\\left(\\sin{\\theta}\\right)}^{2}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\phi}{}^{\\theta}& =  - \\frac{1}{2}\\sin\\left(2\\theta\\right)\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{\\theta}{}^{r}& = 2M-r\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{t}{}^{r}& = M \\left(-2M+r\\right) {r}^{-3}\\\\[-.5ex]\n",
       "\\end{aligned}\\right.\n",
       "$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ch:= \\Gamma^{\\mu}_{\\nu\\rho} = 1/2 g^{\\mu\\sigma} ( \n",
    "                                   \\partial_{\\rho}{g_{\\nu\\sigma}} \n",
    "                                  +\\partial_{\\nu}{g_{\\rho\\sigma}}\n",
    "                                  -\\partial_{\\sigma}{g_{\\nu\\rho}} ):\n",
    "                          \n",
    "evaluate(ch, ss, rhsonly=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Continuing from here we can compute the Riemann tensor components. Again, we start\n",
    "by giving this tensor in terms of the Christoffel symbols. We then subsitute the \n",
    "Christoffel symbols just found, and work out any remaining component substitions using\n",
    "`evaluate` (the computation takes a few seconds, essentially because of the round\n",
    "trips through Sympy)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}R^{\\rho}\\,_{\\sigma \\mu \\nu} = \\partial_{\\mu}{\\Gamma^{\\rho}\\,_{\\nu \\sigma}}-\\partial_{\\nu}{\\Gamma^{\\rho}\\,_{\\mu \\sigma}}+\\Gamma^{\\rho}\\,_{\\mu \\lambda} \\Gamma^{\\lambda}\\,_{\\nu \\sigma}-\\Gamma^{\\rho}\\,_{\\nu \\lambda} \\Gamma^{\\lambda}\\,_{\\mu \\sigma}$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rm:= R^{\\rho}_{\\sigma\\mu\\nu} = \\partial_{\\mu}{\\Gamma^{\\rho}_{\\nu\\sigma}}\n",
    "                                  -\\partial_{\\nu}{\\Gamma^{\\rho}_{\\mu\\sigma}}\n",
    "                                  +\\Gamma^{\\rho}_{\\mu\\lambda} \\Gamma^{\\lambda}_{\\nu\\sigma}\n",
    "                                  -\\Gamma^{\\rho}_{\\nu\\lambda} \\Gamma^{\\lambda}_{\\mu\\sigma};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}R^{\\rho}\\,_{\\sigma \\mu \\nu} = \\square{}_{\\nu}{}_{\\sigma}{}^{\\rho}{}_{\\mu}\\left\\{\\begin{aligned}\\square{}_{t}{}_{t}{}^{r}{}_{r}& = 2M \\left(2M-r\\right) {r}^{-4}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{\\theta}{}^{r}{}_{r}& = -M {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\phi}{}^{\\theta}{}_{\\theta}& = 2M {\\left(\\sin{\\theta}\\right)}^{2} {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\phi}{}^{r}{}_{r}& = -M {\\left(\\sin{\\theta}\\right)}^{2} {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{r}{}^{t}{}_{r}& = 2M {\\left({r}^{2} \\left(2M-r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\theta}{}^{\\phi}{}_{\\theta}& = -2M {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{t}{}^{r}{}_{t}& = 2M \\left(-2M+r\\right) {r}^{-4}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{\\theta}{}^{r}{}_{\\theta}& = M {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{\\phi}{}^{\\theta}{}_{\\phi}& = -2M {\\left(\\sin{\\theta}\\right)}^{2} {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{\\phi}{}^{r}{}_{\\phi}& = M {\\left(\\sin{\\theta}\\right)}^{2} {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{r}{}^{t}{}_{t}& = 2M {\\left({r}^{2} \\left(-2M+r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{r}{}^{\\theta}{}_{\\theta}& = M {\\left({r}^{2} \\left(2M-r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{\\theta}{}^{\\phi}{}_{\\phi}& = 2M {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{r}{}_{r}{}^{\\phi}{}_{\\phi}& = M {\\left({r}^{2} \\left(2M-r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{t}{}^{\\phi}{}_{\\phi}& = M \\left(-2M+r\\right) {r}^{-4}\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{t}{}^{\\theta}{}_{\\theta}& = M \\left(-2M+r\\right) {r}^{-4}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{\\phi}{}^{t}{}_{t}& = -M {\\left(\\sin{\\theta}\\right)}^{2} {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{\\theta}{}^{t}{}_{t}& = -M {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{r}{}^{\\phi}{}_{r}& = M {\\left({r}^{2} \\left(-2M+r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\phi}{}_{t}{}^{\\phi}{}_{t}& = M \\left(2M-r\\right) {r}^{-4}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{r}{}^{\\theta}{}_{r}& = M {\\left({r}^{2} \\left(-2M+r\\right)\\right)}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{\\theta}{}_{t}{}^{\\theta}{}_{t}& = M \\left(2M-r\\right) {r}^{-4}\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{\\phi}{}^{t}{}_{\\phi}& = M {\\left(\\sin{\\theta}\\right)}^{2} {r}^{-1}\\\\[-.5ex]\n",
       "\\square{}_{t}{}_{\\theta}{}^{t}{}_{\\theta}& = M {r}^{-1}\\\\[-.5ex]\n",
       "\\end{aligned}\\right.\n",
       "$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "substitute(rm, ch)\n",
    "evaluate(rm, ss, rhsonly=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Ricci tensor should of course vanish as the Schwarzschild solution is a vacuum\n",
    "solution. Following the same logic as above, this is easily verified:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}R_{\\sigma \\nu} = R^{\\rho}\\,_{\\sigma \\rho \\nu}$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "${}R_{\\sigma \\nu} = 0$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "rc:= R_{\\sigma\\nu} = R^{\\rho}_{\\sigma\\rho\\nu};\n",
    "substitute(rc, rm)\n",
    "evaluate(rc, ss, rhsonly=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "More interesting is the Kretschmann scalar. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}K = R^{\\mu}\\,_{\\nu \\rho \\sigma} R^{\\lambda}\\,_{\\kappa \\gamma \\chi} g_{\\mu \\lambda} g^{\\nu \\kappa} g^{\\rho \\gamma} g^{\\sigma \\chi}$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "K:= K = R^{\\mu}_{\\nu\\rho\\sigma} R^{\\lambda}_{\\kappa\\gamma\\chi} \n",
    "           g_{\\mu\\lambda} g^{\\nu\\kappa} g^{\\rho\\gamma} g^{\\sigma\\chi};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "${}K = 48{M}^{2} {r}^{-6}$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "substitute(K, rm)\n",
    "evaluate(K, ss, rhsonly=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This shows that $r=0$ is a true singularity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cadabra2",
   "language": "python",
   "name": "cadabra2"
  },
  "language_info": {
   "codemirror_mode": "cadabra",
   "file_extension": ".ipynb",
   "mimetype": "text/cadabra",
   "name": "cadabra2",
   "pygments_lexer": "cadabra"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
