{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ``phytorch.cosmology``\n",
"\n",
"> **⚠️ Warning: under construction!**\n",
"\n",
"This notebook demonstrates the various functionalities of ``phytorch.cosmology``, which are mainly related to cosmographic distance calculations.\n",
"\n",
"We first import the various external modules we'll need. Setting the default datatype to `double` will prevent some numerical issues which otherwise might look like bugs :)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:35.652340Z",
"start_time": "2022-11-11T00:51:34.424339Z"
},
"scrolled": false
},
"outputs": [],
"source": [
"import torch\n",
"import numpy as np\n",
"\n",
"from matplotlib import pyplot as plt\n",
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"%config InlineBackend.rc = {'figure.dpi': 96}\n",
"\n",
"\n",
"torch.set_default_dtype(torch.double)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation and basics\n",
"\n",
"To initialise a cosmology class, one needs to first select the concrete cosmological model (ΛCDM, wCDM, etc.) and a driver (analytic, numerical integration, etc.) which performs the cosmographic calculaitons. If cosmography is not your thing, there is an \"abstract\" driver under `phytorch.cosmology.drivers.abstract`, which we'll use to demonstrate basic operations.\n",
"\n",
"Cosmologies are initialised by giving values for the required parameters as **keyword-only** arguments:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:38.097706Z",
"start_time": "2022-11-11T00:51:35.654203Z"
},
"scrolled": false
},
"outputs": [],
"source": [
"from phytorch.cosmology.drivers import abstract\n",
"\n",
"c = abstract.LambdaCDMR(Om0=0.31, Ode0=0.68, Or0=1e-4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now visualise how the various relative densities evolve:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:38.106114Z",
"start_time": "2022-11-11T00:51:38.101674Z"
},
"scrolled": false
},
"outputs": [],
"source": [
"z = torch.logspace(-2, 5, 71)\n",
"\n",
"Om = c.Om(z)\n",
"Ode = c.Ode(z)\n",
"Or = c.Or(z)\n",
"Ok = c.Ok(z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The returned values are tensors, which can be plotted (behind the scenes):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:39.020862Z",
"start_time": "2022-11-11T00:51:38.110835Z"
},
"nbsphinx": "hidden",
"scrolled": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig = plt.figure(figsize=(5, 3))\n",
"plt.axhline(1, ls='dashed', c='black', lw=0.5)\n",
"plt.loglog(z, Om, label=r'$\\Omega_{\\mathrm{m}}(z)$')\n",
"plt.loglog(z, Ode, label=r'$\\Omega_{\\Lambda}(z)$')\n",
"plt.loglog(z, Or, label=r'$\\Omega_{\\mathrm{r}}(z)$')\n",
"plt.loglog(z, Ok, label=r'$\\Omega_{\\mathrm{k}}(z)$')\n",
"plt.xlabel('redshift, $z$')\n",
"plt.legend();"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:39.422118Z",
"start_time": "2022-11-11T00:51:39.022441Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The parameters can be tensors as well and can be safely modified after instantiation:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:39.426485Z",
"start_time": "2022-11-11T00:51:39.423835Z"
},
"scrolled": false
},
"outputs": [],
"source": [
"c.Om0 = torch.tensor([0, 0.3, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All calculations broadcast as you'd expect. For example, we can calculate the dimensionless Hubble parameter $E(z, \\Omega_{\\mathrm{m} 0}, \\Omega_{\\lambda 0}, \\Omega_{\\mathrm{r} 0})$ for the three different matter densities over a range of redshifts, one can do"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:39.433539Z",
"start_time": "2022-11-11T00:51:39.428346Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([71, 3])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"E = c.efunc(z.unsqueeze(-1))\n",
"E.shape"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:40.027675Z",
"start_time": "2022-11-11T00:51:39.436416Z"
},
"nbsphinx": "hidden",
"scrolled": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig = plt.figure(figsize=(5, 3))\n",
"plt.axhline(1, ls='dashed', lw=0.5, color='k')\n",
"for _E, _Om0 in zip(E.unbind(-1), c.Om0):\n",
" plt.loglog(z, _E, label=r'$\\Omega_{\\mathrm{m} 0} = ' f'{_Om0.item():.1f}' '$')\n",
"plt.xlabel('redshift, $z$')\n",
"plt.ylabel('$E(z)$')\n",
"plt.legend();"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:40.380741Z",
"start_time": "2022-11-11T00:51:40.029328Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Unitful quantities\n",
"\n",
"One can also calculate unitful quantities. In cosmology, units are set by the Hubble parameter $H_0$, which by default is taken to be $100 \\, \\mathrm{km}/\\mathrm{s}/\\mathrm{Mpc}$. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Distance calculations"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:40.933060Z",
"start_time": "2022-11-11T00:51:40.382827Z"
},
"pycharm": {
"is_executing": true
},
"scrolled": false
},
"outputs": [],
"source": [
"from phytorch.cosmology.drivers.analytic import LambdaCDM\n",
"\n",
"c = LambdaCDM(Om0=0.31, Ode0=0.68)\n",
"\n",
"z = torch.logspace(-3, 5, 81)\n",
"\n",
"dc = c.comoving_distance_dimless(z)\n",
"dl = c.luminosity_distance_dimless(z)\n",
"da = c.angular_diameter_distance_dimless(z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot behind the scenes:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:41.259334Z",
"start_time": "2022-11-11T00:51:40.934603Z"
},
"nbsphinx": "hidden",
"scrolled": false,
"tags": [
"nbsphinx-thumbnail"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig = plt.figure(figsize=(5, 3))\n",
"plt.axhline(1, ls='dashed', c='black', lw=0.5)\n",
"plt.loglog(z, dc, label='comoving')\n",
"plt.loglog(z, dl, label='luminosity')\n",
"plt.loglog(z, da, label='angular diameter')\n",
"plt.xlabel('redshift, $z$')\n",
"plt.ylabel('distance / Hubble distance')\n",
"plt.legend();"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2022-11-11T00:51:41.473625Z",
"start_time": "2022-11-11T00:51:41.260630Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Distance units and unitful distances"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### More cosmological models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Gradients\n",
"## Module"
]
}
],
"metadata": {
"celltoolbar": "Edit 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.10.8"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 1
}