{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Accuracy - Synchrotron emission/absorption coefficients\n",
    "======================================================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:32:20.708061Z",
     "iopub.status.busy": "2021-10-15T09:32:20.707758Z",
     "iopub.status.idle": "2021-10-15T09:32:25.758013Z",
     "shell.execute_reply": "2021-10-15T09:32:25.757729Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys; sys.path.append(\"/home/ydallilar/.software/source/symphony/build/\")\n",
    "from joblib import Parallel, delayed\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import flaremodel as fm\n",
    "import symphonyPy\n",
    "\n",
    "SAVE_PLOT = True\n",
    "symphony_njobs = 12\n",
    "\n",
    "# Setup parameters used in comparison\n",
    "ne = 1\n",
    "theta_E = 30.\n",
    "p = 3.\n",
    "gamma_min = 1\n",
    "gamma_max = 1e3\n",
    "p_angle = np.pi/4\n",
    "B = 10\n",
    "kappa = p+1 # p = 3.5\n",
    "kappa_w = theta_E\n",
    "\n",
    "nu = np.logspace(9, 16, 50)\n",
    "nu_c = fm.nu_crit(1, B, p_angle)\n",
    "\n",
    "# We use basic grid integration scheme, steps is grid points per factor of 10 in gamma, \n",
    "# g_min_br/g_max_br are gamma limits used in integration. These are the defaults and for most\n",
    "# purposes should be enough. Powerlaw distribution overwrites these so that integration is limited\n",
    "# to given gamma_min/gamma_max parameters of the distribution.\n",
    "steps = 50\n",
    "g_min_br = 1.1\n",
    "g_max_br = 1e7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:32:25.763922Z",
     "iopub.status.busy": "2021-10-15T09:32:25.763626Z",
     "iopub.status.idle": "2021-10-15T09:32:25.765321Z",
     "shell.execute_reply": "2021-10-15T09:32:25.765015Z"
    }
   },
   "outputs": [],
   "source": [
    "def j_nu_sym(nu, ne, B, params, edist, incang):\n",
    "    \n",
    "    if edist == \"thermal\":\n",
    "        fun = lambda nu_t: symphonyPy.j_nu_py(nu_t, B, ne, incang, \n",
    "                        symphonyPy.MAXWELL_JUETTNER, symphonyPy.STOKES_I, \n",
    "                         params[0], 0, 0, 0, 0, 0, 0)  \n",
    "    elif edist == \"powerlaw\":\n",
    "        fun = lambda nu_t: symphonyPy.j_nu_py(nu_t, B, ne, incang, \n",
    "                        symphonyPy.POWER_LAW, symphonyPy.STOKES_I, \n",
    "                         0, params[0], params[1], params[2], params[2], 0, 0)\n",
    "    elif edist == \"kappa\":\n",
    "        fun = lambda nu_t: symphonyPy.j_nu_fit_py(nu_t, B, ne, incang, \n",
    "                        symphonyPy.KAPPA_DIST, symphonyPy.STOKES_I, \n",
    "                         0, 0, 0, 0, 0, params[0], params[1])\n",
    "    \n",
    "    return np.array(Parallel(n_jobs=symphony_njobs)(delayed(fun)(nu_t) for nu_t in nu))\n",
    "\n",
    "def a_nu_sym(nu, ne, B, params, edist, incang):\n",
    "    \n",
    "    if edist == \"thermal\":\n",
    "        fun = lambda nu_t: symphonyPy.alpha_nu_py(nu_t, B, ne, incang, \n",
    "                        symphonyPy.MAXWELL_JUETTNER, symphonyPy.STOKES_I, \n",
    "                         params[0], 0, 0, 0, 0, 0, 0, symphonyPy.SYMPHONY_METHOD)  \n",
    "    elif edist == \"powerlaw\":\n",
    "        fun = lambda nu_t: symphonyPy.alpha_nu_py(nu_t, B, ne, incang, \n",
    "                        symphonyPy.POWER_LAW, symphonyPy.STOKES_I, \n",
    "                         0, params[0], params[1], params[2], params[2], 0, 0, symphonyPy.SYMPHONY_METHOD)\n",
    "    elif edist == \"kappa\":\n",
    "        fun = lambda nu_t: symphonyPy.alpha_nu_fit_py(nu_t, B, ne, incang, \n",
    "                        symphonyPy.KAPPA_DIST, symphonyPy.STOKES_I, \n",
    "                         0, 0, 0, 0, 0, params[0], params[1])\n",
    "    \n",
    "    return np.array(Parallel(n_jobs=symphony_njobs)(delayed(fun)(nu_t) for nu_t in nu))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Thermal Distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:32:25.768837Z",
     "iopub.status.busy": "2021-10-15T09:32:25.768535Z",
     "iopub.status.idle": "2021-10-15T09:35:05.995499Z",
     "shell.execute_reply": "2021-10-15T09:35:05.995816Z"
    }
   },
   "outputs": [],
   "source": [
    "gamma = np.logspace(np.log10(g_min_br), np.log10(g_max_br), 350)\n",
    "ne_fun = lambda gamma : fm.eDist(gamma, ne, [theta_E], \"thermal\")\n",
    "\n",
    "j_nu_th_sym = j_nu_sym(nu, ne, B, [theta_E], \"thermal\", p_angle)\n",
    "a_nu_th_sym = a_nu_sym(nu, ne, B, [theta_E], \"thermal\", p_angle)\n",
    "\n",
    "j_nu_th_br = fm.j_nu_brute(nu, ne, B, [theta_E], \"thermal\", incang=p_angle, \\\n",
    "                           steps=steps, gamma_min=g_min_br, gamma_max=g_max_br)\n",
    "j_nu_th_usr = fm.j_nu_userdist(nu, B, gamma, ne_fun(gamma), incang=p_angle)\n",
    "a_nu_th_br = fm.a_nu_brute(nu, ne, B, [theta_E], \"thermal\", incang=p_angle, \\\n",
    "                           steps=steps, gamma_min=g_min_br, gamma_max=g_max_br)\n",
    "a_nu_th_usr = fm.a_nu_userdist(nu, B, gamma, ne_fun(gamma), incang=p_angle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:35:06.005627Z",
     "iopub.status.busy": "2021-10-15T09:35:06.005238Z",
     "iopub.status.idle": "2021-10-15T09:35:07.713715Z",
     "shell.execute_reply": "2021-10-15T09:35:07.713403Z"
    }
   },
   "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": {
      "text/plain": [
       "(0.0001, 1)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 23812 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 13108 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6,4))\n",
    "\n",
    "plt.loglog(nu/nu_c, j_nu_th_sym*nu, 'k-', label=\"symphony\")\n",
    "plt.loglog(nu/nu_c, j_nu_th_br*nu, 'ko', label=\"brute\")\n",
    "plt.loglog(nu/nu_c, j_nu_th_usr*nu, 'y.', label=\"userdist\")\n",
    "\n",
    "plt.ylim([1e-20, 1e-10])\n",
    "plt.ylabel(r\"$\\nu j_{\\nu}/n_e$\")\n",
    "plt.xlabel(r\"$\\nu/\\nu_c$\")\n",
    "plt.legend()\n",
    "\n",
    "plt.gca().twinx()\n",
    "plt.loglog(nu/nu_c, a_nu_th_sym, 'r-')\n",
    "plt.loglog(nu/nu_c, a_nu_th_br, 'ro')\n",
    "plt.loglog(nu/nu_c, a_nu_th_usr, 'y.')\n",
    "\n",
    "plt.ylim([1e-50, 1e-14])\n",
    "plt.xlim([1e2, 1e8])\n",
    "plt.ylabel(r\"$\\alpha_{\\nu}/n_e$\")\n",
    "\n",
    "if SAVE_PLOT: plt.savefig(\"CompThermal.eps\", bbox_inches=\"tight\", dpi=300);\n",
    "\n",
    "plt.figure(figsize=(6,4))\n",
    "\n",
    "plt.loglog(nu/nu_c, np.abs(1-j_nu_th_br/j_nu_th_sym), 'r-')\n",
    "plt.loglog(nu/nu_c, np.abs(1-a_nu_th_br/a_nu_th_sym), 'k-')\n",
    "plt.loglog(nu/nu_c, np.abs(1-j_nu_th_usr/j_nu_th_sym), 'ro')\n",
    "plt.loglog(nu/nu_c, np.abs(1-a_nu_th_usr/a_nu_th_sym), 'ko')\n",
    "plt.ylabel(\"|1-abs(ratio)|\")\n",
    "plt.xlabel(r\"$\\nu/\\nu_c$\")\n",
    "plt.xlim([1e2, 1e8])\n",
    "plt.ylim([1e-4, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Powerlaw Distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:35:07.717369Z",
     "iopub.status.busy": "2021-10-15T09:35:07.717071Z",
     "iopub.status.idle": "2021-10-15T09:44:34.017293Z",
     "shell.execute_reply": "2021-10-15T09:44:34.016877Z"
    }
   },
   "outputs": [],
   "source": [
    "gamma = np.logspace(np.log10(g_min_br), np.log10(g_max_br), 350)\n",
    "ne_fun = lambda gamma : fm.eDist(gamma, ne, [p, gamma_min, gamma_max], \"powerlawexpcutoff\")\n",
    "\n",
    "j_nu_pl_sym = j_nu_sym(nu, ne, B, [p, gamma_min, gamma_max], \"powerlaw\", p_angle)\n",
    "a_nu_pl_sym = a_nu_sym(nu, ne, B, [p, gamma_min, gamma_max], \"powerlaw\", p_angle)\n",
    "\n",
    "j_nu_pl_br = fm.j_nu_brute(nu, ne, B, [p, gamma_min, gamma_max], \"powerlawexpcutoff\", incang=p_angle, steps=steps)\n",
    "j_nu_pl_usr = fm.j_nu_userdist(nu, B, gamma, ne_fun(gamma), incang=p_angle)\n",
    "a_nu_pl_br = fm.a_nu_brute(nu, ne, B, [p, gamma_min, gamma_max], \"powerlawexpcutoff\", incang=p_angle, steps=steps)\n",
    "a_nu_pl_usr = fm.a_nu_userdist(nu, B, gamma, ne_fun(gamma), incang=p_angle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:44:34.025460Z",
     "iopub.status.busy": "2021-10-15T09:44:34.025084Z",
     "iopub.status.idle": "2021-10-15T09:44:35.692926Z",
     "shell.execute_reply": "2021-10-15T09:44:35.692603Z"
    }
   },
   "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": {
      "text/plain": [
       "(0.0001, 1)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22736 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 16012 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6,4))\n",
    "\n",
    "plt.loglog(nu/nu_c, j_nu_pl_sym*nu, 'k-', label=\"symphony\")\n",
    "plt.loglog(nu/nu_c, j_nu_pl_br*nu, 'ko', label=\"brute\")\n",
    "plt.loglog(nu/nu_c, j_nu_pl_usr*nu, 'y.', label=\"userdist\")\n",
    "\n",
    "plt.ylim([1e-17, 1e-14])\n",
    "\n",
    "plt.ylabel(r\"$\\nu j_{\\nu}/n_e$\")\n",
    "plt.xlabel(r\"$\\nu/\\nu_c$\")\n",
    "plt.legend()\n",
    "\n",
    "plt.gca().twinx()\n",
    "plt.loglog(nu/nu_c, a_nu_pl_sym, 'r-')\n",
    "plt.loglog(nu/nu_c, a_nu_pl_br, 'ro')\n",
    "plt.loglog(nu/nu_c, a_nu_pl_usr, 'y.')\n",
    "\n",
    "plt.ylim([1e-41, 1e-13])\n",
    "plt.xlim([1e2, 1e8])\n",
    "plt.ylabel(r\"$\\alpha_{\\nu}/n_e$\")\n",
    "\n",
    "if SAVE_PLOT: plt.savefig(\"CompPl.eps\", bbox_inches=\"tight\", dpi=300);\n",
    "    \n",
    "plt.figure(figsize=(6, 4))\n",
    "\n",
    "plt.loglog(nu/nu_c, np.abs(1-j_nu_pl_br/j_nu_pl_sym), 'r-')\n",
    "plt.loglog(nu/nu_c, np.abs(1-a_nu_pl_br/a_nu_pl_sym), 'k-')\n",
    "plt.loglog(nu/nu_c, np.abs(1-j_nu_pl_usr/j_nu_pl_sym), 'ro')\n",
    "plt.loglog(nu/nu_c, np.abs(1-a_nu_pl_usr/a_nu_pl_sym), 'ko')\n",
    "plt.ylabel(\"|1-abs(ratio)|\")\n",
    "plt.xlabel(r\"$\\nu/\\nu_c$\")\n",
    "plt.xlim([1e2, 1e8])\n",
    "plt.ylim([1e-4, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:44:35.696809Z",
     "iopub.status.busy": "2021-10-15T09:44:35.696478Z",
     "iopub.status.idle": "2021-10-15T09:44:35.745585Z",
     "shell.execute_reply": "2021-10-15T09:44:35.745972Z"
    }
   },
   "outputs": [],
   "source": [
    "gamma = np.logspace(np.log10(g_min_br), np.log10(g_max_br), 350)\n",
    "ne_fun = lambda gamma : fm.eDist(gamma, ne, [kappa, kappa_w], \"kappa\")\n",
    "\n",
    "j_nu_kp_sym = j_nu_sym(nu, ne, B, [kappa, kappa_w], \"kappa\", p_angle)\n",
    "a_nu_kp_sym = a_nu_sym(nu, ne, B, [kappa, kappa_w], \"kappa\", p_angle)\n",
    "\n",
    "j_nu_kp_br = fm.j_nu_brute(nu, ne, B, [kappa, kappa_w], \"kappa\", incang=p_angle, \\\n",
    "                           steps=steps, gamma_min=g_min_br, gamma_max=g_max_br)\n",
    "j_nu_kp_usr = fm.j_nu_userdist(nu, B, gamma, ne_fun(gamma), incang=p_angle)\n",
    "a_nu_kp_br = fm.a_nu_brute(nu, ne, B, [kappa, kappa_w], \"kappa\", incang=p_angle, \\\n",
    "                           steps=steps, gamma_min=g_min_br, gamma_max=g_max_br)\n",
    "a_nu_kp_usr = fm.a_nu_userdist(nu, B, gamma, ne_fun(gamma), incang=p_angle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-15T09:44:35.757101Z",
     "iopub.status.busy": "2021-10-15T09:44:35.754036Z",
     "iopub.status.idle": "2021-10-15T09:44:37.364125Z",
     "shell.execute_reply": "2021-10-15T09:44:37.364416Z"
    }
   },
   "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": {
      "text/plain": [
       "(0.0001, 1)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19968 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 15584 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6,4))\n",
    "\n",
    "plt.loglog(nu/nu_c, j_nu_kp_sym*nu, 'k-', label=\"symphony\")\n",
    "plt.loglog(nu/nu_c, j_nu_kp_br*nu, 'ko', label=\"brute\")\n",
    "plt.loglog(nu/nu_c, j_nu_kp_usr*nu, 'y.', label=\"userdist\")\n",
    "\n",
    "plt.ylim([1e-14, 1e-6])\n",
    "plt.ylabel(r\"$\\nu j_{\\nu}/n_e$\")\n",
    "plt.xlabel(r\"$\\nu/\\nu_c$\")\n",
    "plt.legend()\n",
    "\n",
    "plt.gca().twinx()\n",
    "plt.loglog(nu/nu_c, a_nu_kp_sym, 'r-')\n",
    "plt.loglog(nu/nu_c, a_nu_kp_br, 'ro')\n",
    "plt.loglog(nu/nu_c, a_nu_kp_usr, 'y.')\n",
    "\n",
    "plt.ylim([1e-41, 1e-13])\n",
    "plt.xlim([1e2, 1e8])\n",
    "plt.ylabel(r\"$\\alpha_{\\nu}$\")\n",
    "\n",
    "if SAVE_PLOT: plt.savefig(\"CompKappa.eps\", bbox_inches=\"tight\", dpi=300);\n",
    "\n",
    "plt.figure(figsize=(6,4))\n",
    "\n",
    "plt.loglog(nu/nu_c, np.abs(1-j_nu_kp_br/j_nu_kp_sym), 'r-')\n",
    "plt.loglog(nu/nu_c, np.abs(1-a_nu_kp_br/a_nu_kp_sym), 'k-')\n",
    "plt.loglog(nu/nu_c, np.abs(1-j_nu_kp_usr/j_nu_kp_sym), 'ro')\n",
    "plt.loglog(nu/nu_c, np.abs(1-a_nu_kp_usr/a_nu_kp_sym), 'ko')\n",
    "plt.ylabel(\"|1-abs(ratio)|\")\n",
    "plt.xlabel(r\"$\\nu/\\nu_c$\")\n",
    "plt.xlim([1e2, 1e8])\n",
    "plt.ylim([1e-4, 1])"
   ]
  },
  {
   "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
