{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Photon Densities on Homogeneous Sphere\n",
    "======================================"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:16.657406Z",
     "iopub.status.busy": "2021-10-29T13:20:16.657123Z",
     "iopub.status.idle": "2021-10-29T13:20:16.962037Z",
     "shell.execute_reply": "2021-10-29T13:20:16.961768Z"
    }
   },
   "outputs": [],
   "source": [
    "import flaremodel as fm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "SAVE_PLOT = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:16.964720Z",
     "iopub.status.busy": "2021-10-29T13:20:16.964370Z",
     "iopub.status.idle": "2021-10-29T13:20:16.966022Z",
     "shell.execute_reply": "2021-10-29T13:20:16.965786Z"
    }
   },
   "outputs": [],
   "source": [
    "SgrA = fm.SgrA()\n",
    "\n",
    "B, ne, R, R_in, p, g_min, g_max, rsteps = 10, 3e8, SgrA.Rs, 0., 3, 10., 500., 64\n",
    "\n",
    "edist = \"powerlaw\"\n",
    "\n",
    "params = [p, g_min, g_max]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:16.970371Z",
     "iopub.status.busy": "2021-10-29T13:20:16.970003Z",
     "iopub.status.idle": "2021-10-29T13:20:17.019125Z",
     "shell.execute_reply": "2021-10-29T13:20:17.019435Z"
    }
   },
   "outputs": [],
   "source": [
    "sh_sp = fm.RadialSphere(edist=edist, rsteps=rsteps, target=\"cpu\")\n",
    "h_sp = fm.HomogeneousSphere(edist)\n",
    "\n",
    "nu_ph = np.array([3e10, 1e11, 1.5e11, 1e12])\n",
    "\n",
    "nus = np.logspace(10, 15, 100)\n",
    "\n",
    "lum_sync_sh = sh_sp.compute_synchrotron(nus, ne, [R, R_in, -1], B, params)\n",
    "r, phd_sh = sh_sp._compute_photon_density(nu_ph, ne, [R, R_in, -1], B, params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:17.021109Z",
     "iopub.status.busy": "2021-10-29T13:20:17.020735Z",
     "iopub.status.idle": "2021-10-29T13:20:17.604556Z",
     "shell.execute_reply": "2021-10-29T13:20:17.604225Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_8357/538814947.py:6: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  fun = lambda x: 2./3*((1-x**2)/(2*x)*np.log((1+x)/(1-x))+1)\n",
      "/tmp/ipykernel_8357/538814947.py:6: RuntimeWarning: invalid value encountered in multiply\n",
      "  fun = lambda x: 2./3*((1-x**2)/(2*x)*np.log((1+x)/(1-x))+1)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 42244 characters]\n",
      "text/plain": [
       "<Figure size 432x720 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axs = plt.subplots(2, 1, figsize=(6, 10))\n",
    "cs = [\"g\", \"y\", \"b\", \"m\"]\n",
    "r = r/r[-1]\n",
    "\n",
    "# BandGrindlay85 Eq. III.4\n",
    "fun = lambda x: 2./3*((1-x**2)/(2*x)*np.log((1+x)/(1-x))+1)\n",
    "\n",
    "for i in range(4):\n",
    "    norm = phd_sh[0,i]\n",
    "    axs[1].plot(r, phd_sh[:,i]/norm, cs[i]+\"-\", \n",
    "                drawstyle='steps-mid', label=r\"$\\nu$ = %.1e Hz\" % (nu_ph[i]))\n",
    "\n",
    "axs[1].plot([0,1],[1,1], 'r-.', label=\"Opt. Thin Limit\")\n",
    "axs[1].plot(r, fun(r)/fun(1e-2), 'k-.', label=\"Opt. Thick Limit\")\n",
    "axs[1].legend()\n",
    "\n",
    "axs[1].set_ylabel(\"Photon density [Norm.]\")\n",
    "axs[1].set_xlabel(\"Radius [r/R]\")\n",
    "\n",
    "axs[0].loglog(nus, lum_sync_sh/np.max(lum_sync_sh), 'k')\n",
    "for i in range(4):\n",
    "    axs[0].plot([nu_ph[i], nu_ph[i]], [1e-2, 1.2], cs[i]+\"--\")\n",
    "\n",
    "axs[0].set_ylabel(r\"$\\nu L_\\nu$ [Norm.]\")\n",
    "axs[0].set_xlabel(r\"Frequency [$\\nu$]\")\n",
    "axs[0].set_ylim([1e-2, 1.2])\n",
    "axs[0].set_xlim([1e10, 1e14]);\n",
    "\n",
    "if SAVE_PLOT: fig.savefig(\"Ph_HomoSp.eps\", bbox_inches=\"tight\", dpi=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:17.618269Z",
     "iopub.status.busy": "2021-10-29T13:20:17.617799Z",
     "iopub.status.idle": "2021-10-29T13:20:19.641952Z",
     "shell.execute_reply": "2021-10-29T13:20:19.641650Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33936 characters]\n",
      "text/plain": [
       "<Figure size 432x360 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axs = plt.subplots(2, figsize=(6, 5), gridspec_kw={\"height_ratios\": [2,1], \"hspace\": 0})\n",
    "params[2] = 500\n",
    "cs = [\"k\", \"r\", \"b\"]\n",
    "nus = np.logspace(11, 20, 100)\n",
    "ps = [2., 2.5, 3.]\n",
    "for i in range(3):\n",
    "    params[0] = ps[i]\n",
    "    lum_ssc_h = h_sp.compute_SSC(nus, ne, [R, -1], B, params, gamma_min=g_min, gamma_max=g_max)\n",
    "    lum_ssc_sh = sh_sp.compute_SSC(nus, ne, [R, R_in, -1], B, params, gamma_min=g_min, gamma_max=g_max)\n",
    "    if i == 0: norm = np.max(lum_ssc_h*nus)\n",
    "    axs[0].loglog(nus, lum_ssc_sh*nus/norm, cs[i]+'-.')\n",
    "    axs[0].loglog(nus, lum_ssc_h*nus/norm, cs[i]+'-')\n",
    "    axs[1].semilogx(nus, 100*(lum_ssc_sh/lum_ssc_h-1), cs[i]+'-')\n",
    "axs[0].set_ylim([2e-3, 2])\n",
    "axs[1].set_ylim([0, 65])\n",
    "axs[0].set_ylabel(r\"$\\nu L_\\nu$ [norm.]\")\n",
    "axs[1].set_xlabel(r\"Frequency [$\\nu$]\")\n",
    "axs[1].set_ylabel(r\"1-Ratio [%]\")\n",
    "if SAVE_PLOT: fig.savefig(\"SSC_Comp.eps\", bbox_inches=\"tight\", dpi=300)"
   ]
  }
 ],
 "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
}
