{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Adiabatic Expansion\n",
    "==================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:50.818898Z",
     "iopub.status.busy": "2021-10-29T13:21:50.818537Z",
     "iopub.status.idle": "2021-10-29T13:21:51.139174Z",
     "shell.execute_reply": "2021-10-29T13:21:51.139422Z"
    }
   },
   "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:21:51.143774Z",
     "iopub.status.busy": "2021-10-29T13:21:51.143492Z",
     "iopub.status.idle": "2021-10-29T13:21:51.145090Z",
     "shell.execute_reply": "2021-10-29T13:21:51.144858Z"
    }
   },
   "outputs": [],
   "source": [
    "R = 2*fm.SgrA().Rs # size of the sphere, will be modified if there is expansion\n",
    "V = 4/3*np.pi*R**3\n",
    "n_e = 6e10\n",
    "B = 10.\n",
    "exp_b = 0.02\n",
    "ts = R/fm.c/exp_b*np.array([0, 1.5, 3., 4.5])\n",
    "\n",
    "g_min = 20.\n",
    "g_max = 1e5\n",
    "p = 2.\n",
    "edist = \"powerlaw\"\n",
    "\n",
    "nus = np.logspace(9, 20, 100)\n",
    "\n",
    "params = [p, g_min, g_max]\n",
    "\n",
    "e_pl_fun = lambda gamma, ne: np.piecewise(gamma, [np.logical_and(gamma < g_max, gamma > g_min)],\n",
    "                                            [lambda gamma: fm.eDist(gamma, ne, params, edist), 0.]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:51.147777Z",
     "iopub.status.busy": "2021-10-29T13:21:51.147494Z",
     "iopub.status.idle": "2021-10-29T13:21:51.148807Z",
     "shell.execute_reply": "2021-10-29T13:21:51.149036Z"
    }
   },
   "outputs": [],
   "source": [
    "nu_m_fun = lambda R, nu_m, R0: nu_m*(R/R0)**(-(4*p+6)/(p+4))\n",
    "S_m_fun = lambda R, S_m, R0: S_m*(R/R0)**(-(7*p+3)/(p+4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:51.151545Z",
     "iopub.status.busy": "2021-10-29T13:21:51.151262Z",
     "iopub.status.idle": "2021-10-29T13:21:51.153029Z",
     "shell.execute_reply": "2021-10-29T13:21:51.152740Z"
    }
   },
   "outputs": [],
   "source": [
    "t_edist = fm.vdLEdist(sync_cool=True)\n",
    "t_edist_off = fm.vdLEdist(sync_cool=False)\n",
    "t_edist.e_dist_fun = e_pl_fun\n",
    "t_edist_off.e_dist_fun = e_pl_fun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:51.172340Z",
     "iopub.status.busy": "2021-10-29T13:21:51.171984Z",
     "iopub.status.idle": "2021-10-29T13:21:51.270951Z",
     "shell.execute_reply": "2021-10-29T13:21:51.271262Z"
    }
   },
   "outputs": [],
   "source": [
    "Rs, Bs, e_dist = t_edist.get_ngamma(ts, n_e*V, B, R, exp_b, 1)\n",
    "Rs, Bs, e_dist_off = t_edist_off.get_ngamma(ts, n_e*V, B, R, exp_b, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:51.272801Z",
     "iopub.status.busy": "2021-10-29T13:21:51.272436Z",
     "iopub.status.idle": "2021-10-29T13:21:52.302765Z",
     "shell.execute_reply": "2021-10-29T13:21:52.302436Z"
    }
   },
   "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, 36472 characters]\n",
      "text/plain": [
       "<Figure size 432x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "geo_user = fm.HomogeneousSphereUserdist()\n",
    "cs = [\"green\", \"red\", \"blue\", \"magenta\"]\n",
    "plt.figure(figsize=(6,5))\n",
    "\n",
    "for i in range(ts.shape[0]):\n",
    "    syn = geo_user.compute_synchrotron(nus, e_dist[i,:], [Rs[i], -1], Bs[i], t_edist._gamma)\n",
    "    plt.loglog(nus, syn, linestyle=\"-\", color=cs[i], label=r\"%.1f$R_0$\" % (Rs[i]/R))\n",
    "    if i == 0:\n",
    "        nu_m_0 = nus[np.argmax(syn)] \n",
    "        S_m_0 = np.max(syn)\n",
    "    \n",
    "for i in range(ts.shape[0]):\n",
    "    syn = geo_user.compute_synchrotron(nus, e_dist_off[i,:], [Rs[i], -1], Bs[i], t_edist._gamma)\n",
    "    plt.loglog(nus, syn, linestyle=\"-.\", color=cs[i])\n",
    "        \n",
    "plt.plot(nu_m_fun(Rs, nu_m_0, R), S_m_fun(Rs, S_m_0, R), 'ko-')\n",
    "\n",
    "plt.legend(loc=2)\n",
    "plt.ylabel(r\"$L_\\nu$ [erg / s / Hz]\")\n",
    "plt.xlabel(r\"Frequency [Hz]\")\n",
    "plt.ylim([1e22, 1e28])\n",
    "plt.xlim([2e9, 1e14])\n",
    "if SAVE_PLOT: plt.savefig(\"vdL_single.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
}
