{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Synchrotron Cooling\n",
    "==================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:22.990689Z",
     "iopub.status.busy": "2021-10-29T13:20:22.990385Z",
     "iopub.status.idle": "2021-10-29T13:20:23.291728Z",
     "shell.execute_reply": "2021-10-29T13:20:23.291443Z"
    }
   },
   "outputs": [],
   "source": [
    "import flaremodel as fm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "SAVE_PLOT = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:23.296222Z",
     "iopub.status.busy": "2021-10-29T13:20:23.295934Z",
     "iopub.status.idle": "2021-10-29T13:20:23.297356Z",
     "shell.execute_reply": "2021-10-29T13:20:23.297117Z"
    }
   },
   "outputs": [],
   "source": [
    "e_dist = \"powerlaw\"\n",
    "B = 20. # Gauss, Will be modified if there is expansion\n",
    "t_esc = 1e99 # Escape timescale (s), 1e99 (large number) to disable\n",
    "dt = 0.1 # Time resolution\n",
    "\n",
    "g_min = 50.\n",
    "g_max = 4e5\n",
    "p = 2.\n",
    "\n",
    "R = fm.SgrA().Rs # size of the sphere, will be modified if there is expansion\n",
    "V = 4/3.*np.pi*R**3\n",
    "total_inj = (10**6)*V # electrons\n",
    "t_inj = 5*60. # injection timescale in (s)\n",
    "p_inj = 15.*60.\n",
    "\n",
    "e_pl_fun = lambda gamma, ne, p: np.piecewise(gamma, [np.logical_and(gamma > p[1], gamma < p[2])],\n",
    "                                            [lambda gamma: fm.eDist(gamma, ne, p, \"powerlaw\"), 0.]) \n",
    "e_bkpl_fun = lambda gamma, ne, p: np.piecewise(gamma, [gamma > p[3]],\n",
    "                                            [lambda gamma: fm.eDist(gamma, ne, p, \"bknpowerlawexpcutoff\"), 0.])\n",
    "\n",
    "# Expected gamma max or break with fixed B field after time t\n",
    "gamma_after_t = lambda B, t: 7.7e8/(B**2*t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:23.299757Z",
     "iopub.status.busy": "2021-10-29T13:20:23.299477Z",
     "iopub.status.idle": "2021-10-29T13:20:23.300678Z",
     "shell.execute_reply": "2021-10-29T13:20:23.300907Z"
    }
   },
   "outputs": [],
   "source": [
    "ne_temp = fm.GaussianInjectionEdist(t_inj, p_inj, t_esc, g_max=1e6, g_steps=50)\n",
    "ne_temp.e_dist_inj_fun = lambda gamma, N_e: e_pl_fun(gamma, N_e, [p, g_min, g_max])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:23.313293Z",
     "iopub.status.busy": "2021-10-29T13:20:23.312936Z",
     "iopub.status.idle": "2021-10-29T13:20:23.449130Z",
     "shell.execute_reply": "2021-10-29T13:20:23.449438Z"
    }
   },
   "outputs": [],
   "source": [
    "ne_t = ne_temp.get_ngamma(np.array([5, 10, 15, 20, 25, 30])*60, total_inj, B, dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:23.451112Z",
     "iopub.status.busy": "2021-10-29T13:20:23.450744Z",
     "iopub.status.idle": "2021-10-29T13:20:24.416710Z",
     "shell.execute_reply": "2021-10-29T13:20:24.416431Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.\n",
      "The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 41188 characters]\n",
      "text/plain": [
       "<Figure size 432x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6,5))\n",
    "\n",
    "plt.loglog(ne_temp._gamma, ne_t[0,:]*ne_temp._gamma**3/V, color=\"gray\", linestyle='--', label=\"Rising\")\n",
    "plt.loglog(ne_temp._gamma, ne_t[1,:]*ne_temp._gamma**3/V, color=\"gray\", linestyle='--')\n",
    "plt.loglog(ne_temp._gamma, ne_t[3,:]*ne_temp._gamma**3/V, color=\"gray\", linestyle='-', label=\"Decaying\")\n",
    "plt.loglog(ne_temp._gamma, ne_t[4,:]*ne_temp._gamma**3/V, color=\"gray\", linestyle='-')\n",
    "\n",
    "plt.loglog(ne_temp._gamma, ne_t[2,:]*ne_temp._gamma**3/V, color=\"red\", linestyle='-', label=\"t=15 mins\")\n",
    "plt.loglog(ne_temp._gamma, ne_t[5,:]*ne_temp._gamma**3/V, color=\"blue\", linestyle='-', label=\"t=30 mins\")\n",
    "\n",
    "plt.plot(ne_temp._gamma[::10], (e_pl_fun(ne_temp._gamma, total_inj/V, \n",
    "                [p, g_min, gamma_after_t(B, t_inj*3)])*ne_temp._gamma**3)[::10], 'b.', label=\"powerlaw\")\n",
    "\n",
    "plt.plot(ne_temp._gamma[::10], (e_bkpl_fun(ne_temp._gamma, total_inj/V*0.5, \n",
    "                [p, p+1, gamma_after_t(B, t_inj*2.35/2.), g_min, g_max/1.5])*ne_temp._gamma**3)[::10], 'r.',\n",
    "        label=\"bknpowerlawexpcutoff\")\n",
    "\n",
    "plt.legend()\n",
    "plt.ylabel(r\"$\\gamma^3$ dN/dV/d$\\gamma$\")\n",
    "plt.xlabel(r\"$\\gamma$\")\n",
    "plt.xlim([2, 1e6])\n",
    "plt.ylim([1e9, 1e12])\n",
    "if SAVE_PLOT: plt.savefig(\"SyncCool.eps\", bbox_inches=\"tight\", dpi=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
