{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Powerlaw Sphere\n",
    "==============="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:20.531907Z",
     "iopub.status.busy": "2021-10-29T13:20:20.531613Z",
     "iopub.status.idle": "2021-10-29T13:20:20.834075Z",
     "shell.execute_reply": "2021-10-29T13:20:20.833781Z"
    }
   },
   "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:20.837010Z",
     "iopub.status.busy": "2021-10-29T13:20:20.836729Z",
     "iopub.status.idle": "2021-10-29T13:20:20.838437Z",
     "shell.execute_reply": "2021-10-29T13:20:20.838149Z"
    }
   },
   "outputs": [],
   "source": [
    "nus = np.logspace(7, 13, 100)\n",
    "SgrA = fm.SgrA()\n",
    "\n",
    "B, ne, R, x, p, g_min, g_max, incang, rsteps = 10, 1e7, SgrA.Rs, 50., 1, 3., 1e4, -1, 50\n",
    "\n",
    "edist = \"powerlaw\"\n",
    "R2 = R\n",
    "R1 = R2/x\n",
    "\n",
    "params = [p, g_min, g_max]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:20.840936Z",
     "iopub.status.busy": "2021-10-29T13:20:20.840656Z",
     "iopub.status.idle": "2021-10-29T13:20:20.841973Z",
     "shell.execute_reply": "2021-10-29T13:20:20.842193Z"
    }
   },
   "outputs": [],
   "source": [
    "n = 2.\n",
    "m = 1.\n",
    "p = 3. # equivalent to a=1\n",
    "\n",
    "params = [p, g_min, g_max]\n",
    "\n",
    "r_p_fun = lambda r, n: (r/R1)**-n\n",
    "r_pm_fun = lambda r: r_p_fun(r, m) # Radial B field\n",
    "r_pn_fun = lambda r: r_p_fun(r, n) # Radial edist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:20.846959Z",
     "iopub.status.busy": "2021-10-29T13:20:20.846599Z",
     "iopub.status.idle": "2021-10-29T13:20:20.847770Z",
     "shell.execute_reply": "2021-10-29T13:20:20.848108Z"
    }
   },
   "outputs": [],
   "source": [
    "def L_nu_bg_fun(nus):\n",
    "    s = n + m*((p-1)/2.+1)\n",
    "    j_nu_pl_br = fm.j_nu_brute(nus, ne, B, params, \"powerlaw\", incang=-1)\n",
    "    return 4*np.pi*4*np.pi*R1**3*j_nu_pl_br*(x**(3-s)-1)/(3-s)\n",
    "\n",
    "L_nu_bg = L_nu_bg_fun(nus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:20:20.849659Z",
     "iopub.status.busy": "2021-10-29T13:20:20.849299Z",
     "iopub.status.idle": "2021-10-29T13:20:22.015890Z",
     "shell.execute_reply": "2021-10-29T13:20:22.015586Z"
    }
   },
   "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, 23488 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",
    "for i in [4, 2, 1]:\n",
    "    sr_sp = fm.RadialSphere(n_r_fun=r_pn_fun, B_r_fun=r_pm_fun, \n",
    "                            edist=edist, rsteps=int((i)*x)) \n",
    "    sr_sp_syn = sr_sp.compute_synchrotron(nus, ne, [R2, R1, -1], B, params)\n",
    "    if i == 4:\n",
    "        nu_t = nus[np.argmax(sr_sp_syn)]\n",
    "        syn_p = np.max(sr_sp_syn)\n",
    "    plt.loglog(nus/nu_t, sr_sp_syn/syn_p/2, label=r\"$R/\\Delta R$=%d\" % (50*i))\n",
    "plt.loglog(nus/nu_t, L_nu_bg/syn_p/2, 'k--')\n",
    "plt.legend(loc=2)\n",
    "plt.ylim([1e-2, 1])\n",
    "plt.xlim([1e-2, 10])\n",
    "plt.ylabel(r\"$L_\\nu$ [arb.]\")\n",
    "plt.xlabel(r\"Frequency $[\\nu/\\nu_T]$\")\n",
    "if SAVE_PLOT: plt.savefig(\"BG85_pl.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
}
