{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calibrating the velocity field against observations "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright (C) 2024 Richard Stiskalek\n",
    "# This program is free software; you can redistribute it and/or modify it\n",
    "# under the terms of the GNU General Public License as published by the\n",
    "# Free Software Foundation; either version 3 of the License, or (at your\n",
    "# option) any later version.\n",
    "#\n",
    "# This program is distributed in the hope that it will be useful, but\n",
    "# WITHOUT ANY WARRANTY; without even the implied warranty of\n",
    "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General\n",
    "# Public License for more details.\n",
    "#\n",
    "# You should have received a copy of the GNU General Public License along\n",
    "# with this program; if not, write to the Free Software Foundation, Inc.,\n",
    "# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from flow_bulk import *\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Enclosed monopole"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33732 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "cols = plt.rcParams['axes.prop_cycle'].by_key()['color']\n",
    "\n",
    "# Carrick+2015 must be multiplied by beta = 0.43\n",
    "r, Vmono = read_enclosed_monopole(\"Carrick2015\")\n",
    "Vmono *= 0.4\n",
    "plt.plot(r, Vmono[0], label=\"Carrick+2015\", color=cols[0])\n",
    "\n",
    "# r, Vmono = read_enclosed_monopole(\"Lilow2024\")\n",
    "# plt.plot(r, Vmono[0], label=\"Lilow+2024\", color=cols[1])\n",
    "\n",
    "# r, Vmono = read_enclosed_monopole(\"csiborg2X\")\n",
    "# ylow, ymed, yhigh = np.percentile(Vmono, [16, 50, 84], axis=0)\n",
    "# plt.fill_between(r, ylow, yhigh, color=cols[2], alpha=0.5, label=\"Manticore\")\n",
    "\n",
    "\n",
    "\n",
    "plt.xlabel(r\"$R ~ [\\mathrm{Mpc} / h]$\")\n",
    "plt.ylabel(r\"$V_{\\mathrm{mono}}(r \\leq R) ~ [\\mathrm{km} / \\mathrm{s}]$\")\n",
    "# plt.legend(loc=\"upper left\")\n",
    "\n",
    "plt.xlim(r.min(), r.max())\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"../../plots/enclosed_monopole.png\", dpi=450)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Enclosed overdensity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 127948 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "cols = plt.rcParamsDefault['axes.prop_cycle'].by_key()['color']\n",
    "\n",
    "r, overdensity = read_enclosed_density(\"csiborg1\")\n",
    "c = cols[0]\n",
    "ymin = np.percentile(overdensity, 16, axis=0)\n",
    "ymed = np.median(overdensity, axis=0)\n",
    "ymax = np.percentile(overdensity, 84, axis=0)\n",
    "plt.fill_between(r, ymin, ymax, color=c, alpha=0.5, label=\"CB1\")\n",
    "\n",
    "\n",
    "r, overdensity = read_enclosed_density(\"csiborg2_main\")\n",
    "c = cols[1]\n",
    "ymin = np.percentile(overdensity, 16, axis=0)\n",
    "ymed = np.median(overdensity, axis=0)\n",
    "ymax = np.percentile(overdensity, 84, axis=0)\n",
    "plt.fill_between(r, ymin, ymax, color=c, alpha=0.5, label=\"CB2\")\n",
    "\n",
    "\n",
    "r, overdensity = read_enclosed_density(\"csiborg2_random\")\n",
    "c = cols[2]\n",
    "ymin = np.percentile(overdensity, 16, axis=0)\n",
    "ymed = np.median(overdensity, axis=0)\n",
    "ymax = np.percentile(overdensity, 84, axis=0)\n",
    "plt.fill_between(r, ymin, ymax, color=c, alpha=0.25, label=\"Random\", zorder=-1)\n",
    "\n",
    "r, overdensity = read_enclosed_density(\"csiborg2X\")\n",
    "c = cols[3]\n",
    "ymin = np.percentile(overdensity, 16, axis=0)\n",
    "ymed = np.median(overdensity, axis=0)\n",
    "ymax = np.percentile(overdensity, 84, axis=0)\n",
    "plt.fill_between(r, ymin, ymax, color=c, alpha=0.5, label=\"Manticore\")\n",
    "\n",
    "plt.xlabel(r\"$R ~ [\\mathrm{Mpc} / h]$\")\n",
    "plt.ylabel(r\"$\\delta_{\\rm enc} (r \\leq R)$\")\n",
    "plt.legend()\n",
    "\n",
    "plt.xlim(0, r.max())\n",
    "plt.ylim(-0.15, 0.075)\n",
    "plt.axhline(0, c='k', ls='--', lw=1)\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"../../plots/enclosed_density.png\", dpi=450)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Enclosed bulk flow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(101, 500, 3)\n"
     ]
    }
   ],
   "source": [
    "r, Vmag, l, b = read_enclosed_flow(\"csiborg1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 100, 3)\n"
     ]
    }
   ],
   "source": [
    "r, Vmag, l, b = read_enclosed_flow(\"csiborg2X\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 191776 characters]",
      "text/plain": [
       "<Figure size 1400x500 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axs = plt.subplots(1, 3, figsize=(14, 5))\n",
    "cols = plt.rcParamsDefault['axes.prop_cycle'].by_key()['color']\n",
    "thin_line_kwargs = {\"lw\": 0.3, \"zorder\": 0, \"ls\": \"dotted\"}\n",
    "\n",
    "# CSiBORG1\n",
    "r, Vmag, l, b = read_enclosed_flow(\"csiborg1\")\n",
    "c = cols[0]\n",
    "for i, y in enumerate([Vmag, l, b]):\n",
    "    ymin, ymed, ymax = np.percentile(y, [16, 50, 84], axis=0)\n",
    "    axs[i].fill_between(r, ymin, ymax, color=c, alpha=0.5, label=\"CB1\")\n",
    "\n",
    "# CSiBORG2\n",
    "r, Vmag, l, b = read_enclosed_flow(\"csiborg2_main\")\n",
    "c = cols[1]\n",
    "for i, y in enumerate([Vmag, l, b]):\n",
    "    ymin, ymed, ymax = np.percentile(y, [16, 50, 84], axis=0)\n",
    "    axs[i].fill_between(r, ymin, ymax, color=c, alpha=0.5, label=\"CB2\")\n",
    "\n",
    "# Manticore\n",
    "r, Vmag, l, b = read_enclosed_flow(\"csiborg2X\")\n",
    "c = cols[3]\n",
    "for i, y in enumerate([Vmag, l, b]):\n",
    "    ymin, ymed, ymax = np.percentile(y, [16, 50, 84], axis=0)\n",
    "    axs[i].fill_between(r, ymin, ymax, color=c, alpha=0.5, label=\"Manticore\")\n",
    "\n",
    "\n",
    "# Random\n",
    "r, Vmag, l, b = read_enclosed_flow(\"csiborg2_random\")\n",
    "c = cols[2]\n",
    "ymin = np.percentile(Vmag, 16, axis=0)\n",
    "ymax = np.percentile(Vmag, 84, axis=0)\n",
    "ymed = np.median(Vmag, axis=0)\n",
    "axs[0].fill_between(r, ymin, ymax, color=c, alpha=0.35, zorder=-1,\n",
    "                    label=\"Random\")\n",
    "\n",
    "\n",
    "axs[0].legend()\n",
    "for i in range(3):\n",
    "    axs[i].set_xlabel(r\"$R ~ [\\mathrm{Mpc} / h]$\")\n",
    "    axs[i].set_xlim(0, r.max())\n",
    "\n",
    "axs[0].set_ylim(0)\n",
    "axs[0].set_ylabel(r\"$V_{\\rm enc} (r \\leq R) ~ [\\mathrm{km} / \\mathrm{s}]$\")\n",
    "axs[1].set_ylabel(r\"$l_{\\rm enc} (r \\leq R) ~ [\\mathrm{deg}]$\")\n",
    "axs[2].set_ylabel(r\"$b_{\\rm enc} (r \\leq R) ~ [\\mathrm{deg}]$\")\n",
    "axs[1].set_ylim(0, 360)\n",
    "\n",
    "fig.tight_layout()\n",
    "fig.savefig(\"../../plots/enclosed_flow.png\", dpi=450)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Enclosed overdensity $\\texttt{BORG2}$ all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)\n",
    "nsims = paths.get_ics(\"borg2_all\")\n",
    "\n",
    "r, overdensity = read_enclosed_density(\"borg2_all\")\n",
    "overdensity_A = overdensity[:80]\n",
    "overdensity_B = overdensity[80:][::30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7500,  7600,  7700,  7800,  7900,  8000,  8100,  8200,  8300,\n",
       "        8400,  8500,  8600,  8700,  8800,  8900,  9000,  9100,  9200,\n",
       "        9300,  9400,  9500,  9600,  9700,  9800,  9900, 10000, 10100,\n",
       "       10200, 10300, 10400, 10500, 10600, 10700, 10800, 10900, 11000,\n",
       "       11100, 11200, 11300, 11400, 11500, 11600, 11700, 11800, 11900,\n",
       "       12000, 12100, 12200, 12300, 12400, 12500, 12600, 12700, 12800,\n",
       "       12900, 13000, 13100, 13200, 13300, 13400, 13500, 13600, 13700,\n",
       "       13800, 13900, 14000, 14100, 14200, 14300, 14400, 14500, 14600,\n",
       "       14700, 14800, 14900, 15000, 15100, 15200, 15300, 15400])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nsims[:80]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 170060 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "cols = plt.rcParamsDefault['axes.prop_cycle'].by_key()['color']\n",
    "\n",
    "# r, overdensity = read_enclosed_density(\"csiborg1\")\n",
    "c = cols[0]\n",
    "for i in range(len(overdensity_A)):\n",
    "    plt.plot(r, overdensity_A[i], c=c, lw=0.2, zorder=0, ls=\"dotted\")\n",
    "plt.plot(r, np.median(overdensity_A, axis=0), c=c, lw=2, label=r\"Chain index: $7500$ to $15000$\")\n",
    "\n",
    "c = cols[1]\n",
    "for i in range(len(overdensity_B)):\n",
    "    plt.plot(r, overdensity_B[i], c=c, lw=0.2, zorder=0, ls=\"dotted\")\n",
    "plt.plot(r, np.median(overdensity_B, axis=0), c=c, lw=2, label=r\"Chain index: $15000$ to $17000$\")\n",
    "\n",
    "\n",
    "\n",
    "plt.xlabel(r\"$R ~ [\\mathrm{Mpc} / h]$\")\n",
    "plt.ylabel(r\"$\\delta_{\\rm enc} (r \\leq R)$\")\n",
    "plt.legend()\n",
    "\n",
    "plt.xlim(0, r.max())\n",
    "plt.ylim(-0.14, -0.005)\n",
    "plt.axhline(0, c='k', ls='--', lw=1)\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"../../plots/enclosed_density_BORG2.png\", dpi=450)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv_csiborg",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
