{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Geometry Interface\n",
    "==================\n",
    "\n",
    ".. currentmodule:: flaremodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:55.838180Z",
     "iopub.status.busy": "2021-10-29T13:21:55.836720Z",
     "iopub.status.idle": "2021-10-29T13:21:56.175163Z",
     "shell.execute_reply": "2021-10-29T13:21:56.174840Z"
    }
   },
   "outputs": [],
   "source": [
    "import flaremodel as fm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:56.178402Z",
     "iopub.status.busy": "2021-10-29T13:21:56.178082Z",
     "iopub.status.idle": "2021-10-29T13:21:56.179754Z",
     "shell.execute_reply": "2021-10-29T13:21:56.179978Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R = 4.0e+15 cm\n"
     ]
    }
   ],
   "source": [
    "nus = np.logspace(10, 24, 100)\n",
    "\n",
    "B, ne, R_in, p, g_min, g_max, incang, steps = 10, 1e6, 0, 3, 10., 2e4, -1, 64\n",
    "\n",
    "edist = \"powerlaw\"\n",
    "params = [p, g_min, g_max]\n",
    "\n",
    "#This would give rough scaling with Thompson scattering approximation between synchrotron and SSC.\n",
    "R = 2/(fm.sigma_T*ne*np.log(g_max/g_min)*g_min**(p-1))\n",
    "print(\"R = %.1e cm\" % R)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "With this initialization, :class:`RadialSphere` is equivalent to :class:`HomogeneousSphere`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:56.182532Z",
     "iopub.status.busy": "2021-10-29T13:21:56.182212Z",
     "iopub.status.idle": "2021-10-29T13:21:56.183882Z",
     "shell.execute_reply": "2021-10-29T13:21:56.183630Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ydallilar/.miniconda3/envs/main/lib/python3.9/site-packages/flaremodel-1.0.0-py3.9-linux-x86_64.egg/flaremodel/geometry.py:298: UserWarning: Setting target to cpu. GPU functions not available...\n",
      "  warnings.warn(\"Setting target to cpu. GPU functions not available...\")\n"
     ]
    }
   ],
   "source": [
    "homo_sp = fm.HomogeneousSphere(edist=edist)\n",
    "rad_sp_cpu = fm.RadialSphere(edist=edist, target=\"cpu\")\n",
    "rad_sp_gpu = fm.RadialSphere(edist=edist, target=\"gpu\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Synchrotron SED can be calculated as follows,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:56.188233Z",
     "iopub.status.busy": "2021-10-29T13:21:56.187860Z",
     "iopub.status.idle": "2021-10-29T13:21:56.243586Z",
     "shell.execute_reply": "2021-10-29T13:21:56.243904Z"
    }
   },
   "outputs": [],
   "source": [
    "sync_h = homo_sp.compute_synchrotron(nus, ne, [R, incang], B, params)\n",
    "sync_r = rad_sp_cpu.compute_synchrotron(nus, ne, [R, R_in, incang], B, params)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Note that SSC calculations requires setting proper integration limits. \n",
    "Gamma limits are adapted from electron distribution parameters.\n",
    "Default range for the seed photons is :math:`\\nu=[10^8, 10^{17}]` Hz.\n",
    "Although it is excessive for this case, will work fine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:56.248222Z",
     "iopub.status.busy": "2021-10-29T13:21:56.245487Z",
     "iopub.status.idle": "2021-10-29T13:21:58.255470Z",
     "shell.execute_reply": "2021-10-29T13:21:58.255935Z"
    }
   },
   "outputs": [],
   "source": [
    "ssc_h = homo_sp.compute_SSC(nus, ne, [R, incang], B, params, gamma_min=g_min, gamma_max=g_max)\n",
    "ssc_cpu = rad_sp_cpu.compute_SSC(nus, ne, [R, R_in, incang], B, params, gamma_min=g_min, gamma_max=g_max)\n",
    "ssc_gpu = rad_sp_gpu.compute_SSC(nus, ne, [R, R_in, incang], B, params, gamma_min=g_min, gamma_max=g_max)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "This is an example for inverse Compton scattering. \n",
    "Essentially, SSC on :class:`RadialSphere` but with photon densities of :class:`HomogeneousSphere`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:58.258172Z",
     "iopub.status.busy": "2021-10-29T13:21:58.257503Z",
     "iopub.status.idle": "2021-10-29T13:21:59.929905Z",
     "shell.execute_reply": "2021-10-29T13:21:59.930373Z"
    }
   },
   "outputs": [],
   "source": [
    "n_ph_fun = lambda nu: homo_sp._compute_photon_density(nu, ne, [R, incang], B, params)\n",
    "ic_cpu = rad_sp_cpu.compute_IC(nus, ne, n_ph_fun, [R, R_in, incang], B, params, gamma_min=g_min, gamma_max=g_max)\n",
    "ic_cpu = rad_sp_gpu.compute_IC(nus, ne, n_ph_fun, [R, R_in, incang], B, params, gamma_min=g_min, gamma_max=g_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:21:59.932639Z",
     "iopub.status.busy": "2021-10-29T13:21:59.931944Z",
     "iopub.status.idle": "2021-10-29T13:22:00.347464Z",
     "shell.execute_reply": "2021-10-29T13:22:00.347710Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30640 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 8))\n",
    "plt.loglog(nus, sync_h*nus, 'k-')\n",
    "plt.loglog(nus, ssc_h*nus, \"r-\")\n",
    "plt.loglog(nus, sync_r*nus, 'k.')\n",
    "plt.loglog(nus, ssc_cpu*nus, 'ro')\n",
    "plt.loglog(nus, ssc_gpu*nus, 'y.')\n",
    "plt.loglog(nus, ic_cpu*nus, 'b.')\n",
    "plt.ylabel(r\"$\\nu L_\\nu$ [erg s-1]\")\n",
    "plt.xlabel(\"Frequency [Hz]\")\n",
    "plt.ylim([1e41, 4e43]);"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Doppler Boosting\n",
    "================"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-29T13:22:00.361551Z",
     "iopub.status.busy": "2021-10-29T13:22:00.358752Z",
     "iopub.status.idle": "2021-10-29T13:22:00.833742Z",
     "shell.execute_reply": "2021-10-29T13:22:00.833443Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30340 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "boost_p1 = [0.3, 0, -1] # Not physical but for testing purposes\n",
    "sync_h_b1 = homo_sp.compute_synchrotron(nus, ne, [R, incang], B, params, boost_p1)\n",
    "boost_p2 = [0.3, 0, 3]\n",
    "sync_h_b2 = homo_sp.compute_synchrotron(nus, ne, [R, incang], B, params, boost_p2)\n",
    "plt.figure(figsize=(10, 8))\n",
    "plt.loglog(nus, sync_h*nus, 'k-')\n",
    "plt.loglog(nus, sync_h_b1*nus, 'r-')\n",
    "plt.loglog(nus, sync_h_b2*nus, 'b-')\n",
    "plt.ylabel(r\"$\\nu L_\\nu$ [erg s-1]\")\n",
    "plt.xlabel(\"Frequency [Hz]\")\n",
    "plt.xlim([1e10, 1e18])\n",
    "plt.ylim([1e41, 4e43]);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "fc59d85fd849d6dd62931b67a61f0e57efea08950b5081af8b8b8a58e400e4b6"
  },
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit ('py3': conda)",
   "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
}
