{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d77ea758-4368-4ffc-86c7-2980c84ceaa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.optimize import curve_fit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1c0385c-84ab-479f-a1ad-c2fc34e59e8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def line(x, k, b):\n",
    "    return x*k + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "805a285a-8aaf-4e25-8c23-4503c8cca594",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(r\"raw-data/discharge.txt\")\n",
    "U = df['U']*10\n",
    "I = df['I']*6/155"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "141d5962-2dcc-4795-ac71-5d497f89418b",
   "metadata": {},
   "outputs": [],
   "source": [
    "I_line = I[:8]\n",
    "U_line = U[:8]\n",
    "opt, cov = curve_fit(line, U_line, I_line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e0b87229-443c-4642-b053-7ce974af2817",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 88044 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6, 4), dpi=300)\n",
    "\n",
    "# plt.minorticks_on()\n",
    "plt.grid(which='minor', linestyle=':', color='0.9')\n",
    "plt.grid(which='major', linestyle='-', color='0.8', linewidth=0.3)\n",
    "\n",
    "plt.errorbar(U, I, yerr=6/155, xerr=0.003*U, linestyle='', marker='.', markersize=0, label=\"Experiment\", color=\"tab:red\", alpha=0.5)\n",
    "plt.plot(U_line, line(U_line, opt[0], opt[1]), label=r\"Fitting: $R_{diff}=$ \" + f\"{-1/opt[0]:.1f}\" + r\"$\\pm 1.48$ kOhm\", color=\"tab:red\", linestyle='-')\n",
    "\n",
    "# plt.title(\"ВАХ разряда\", fontsize=16)\n",
    "plt.ylabel(\"Amperage, mA\")\n",
    "plt.xlabel(\"Voltage, V\")\n",
    "\n",
    "plt.legend()\n",
    "plt.savefig(r\"images\\vah-discharge.png\", facecolor=\"white\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02c19433-6f3d-4376-a176-883abf2da51e",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "**Кривая соответствует участку ГД** -- поднормальный тлеющий разряд"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "43c138ed-008c-46e0-b45c-8ab96cf134d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "pr = []\n",
    "pr.append(pd.read_csv(r\"raw-data\\probe5.txt\"))\n",
    "pr.append(pd.read_csv(r\"raw-data\\probe3.txt\"))\n",
    "pr.append(pd.read_csv(r\"raw-data\\probe1-5.txt\"))\n",
    "\n",
    "I_name = [5, 3, 1.5]\n",
    "colors = [\"orange\", \"green\", \"blue\"]\n",
    "I_corr = [5.65, 4.16, 2.25]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "404f94e8-091e-4186-8d13-09452313493e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 140212 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6, 4), dpi=300)\n",
    "\n",
    "# plt.minorticks_on()\n",
    "plt.grid(which='minor', linestyle=':', color='0.9')\n",
    "plt.grid(which='major', linestyle='-', color='0.8', linewidth=0.3)\n",
    "\n",
    "for index, probe in enumerate(pr):\n",
    "    U = probe['U']\n",
    "    I = probe['I'] + I_corr[index]\n",
    "    plt.errorbar(U, I, yerr=0.003*I, xerr=0.003*U, linestyle='-', marker='.', markersize=5, label=r\"Experiment $I_d = $\" + f\"{I_name[index]}\" +\" mA\", color=f\"tab:{colors[index]}\", alpha=1)\n",
    "# plt.plot(U_line, line(U_line, opt[0], opt[1]), label=r\"Fitting: $R_{diff}=$ \" + f\"{-1/opt[0]:.1f}\" + r\"$\\pm 1.48$ kOhm\", color=\"tab:red\", linestyle='-')\n",
    "\n",
    "# plt.title(\"ВАХ разряда\", fontsize=16)\n",
    "plt.ylabel(\"Amperage, $\\mu A$\")\n",
    "plt.xlabel(\"Voltage, V\")\n",
    "\n",
    "plt.legend()\n",
    "plt.savefig(r\"images\\vah-probe.png\", facecolor=\"white\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "54664dff-391c-4b6f-886e-e1bd4654c809",
   "metadata": {},
   "outputs": [],
   "source": [
    "I_in = []\n",
    "dIdU = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bfaa33ac-4c2a-4f2e-9060-4a5cec648b1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I_plus_5 = 95.8328\t I_minus_5 = -95.9134\n",
      "I_plus_3 = 52.4716\t I_minus_3 = -52.5127\n",
      "I_plus_1.5 = 25.8552\t I_minus_1.5 = -25.8095\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 157744 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6, 4), dpi=300)\n",
    "\n",
    "# plt.minorticks_on()\n",
    "plt.grid(which='minor', linestyle=':', color='0.9')\n",
    "plt.grid(which='major', linestyle='-', color='0.8', linewidth=0.3)\n",
    "\n",
    "for index, probe in enumerate(pr):\n",
    "    U = probe['U']\n",
    "    I = probe['I'] + I_corr[index]\n",
    "    plt.errorbar(U, I, yerr=0.003*I, xerr=0.003*U, linestyle='-', marker='.', markersize=5, alpha=0.1, color=f\"tab:{colors[index]}\")\n",
    "    \n",
    "    U_plus = np.concatenate([U[:5], [0]])\n",
    "    U_minus = np.concatenate([U[-5:], [0]])\n",
    "    \n",
    "    ptr_plus, _ = curve_fit(line, U[:5], I[:5])\n",
    "    ptr_minus, _ = curve_fit(line, U[-5:], I[-5:])\n",
    "    \n",
    "    plt.plot(U_plus, line(U_plus, ptr_plus[0], ptr_plus[1]), color=f\"tab:{colors[index]}\", linestyle='--', label=r\"Fitting $I_d = $\" + f\"{I_name[index]}\" +\" mA\")\n",
    "    plt.plot(U_minus, line(U_minus, ptr_minus[0], ptr_minus[1]), color=f\"tab:{colors[index]}\", linestyle='--')\n",
    "    \n",
    "    print(f\"I_plus_{I_name[index]} = {line(0, ptr_plus[0], ptr_plus[1]):.4f}\\t I_minus_{I_name[index]} = {line(0, ptr_minus[0], ptr_minus[1]):.4f}\")\n",
    "\n",
    "    I_in.append(line(0, ptr_plus[0], ptr_plus[1]) * 1e-6)\n",
    "    dIdU.append((I[11] - I[10]) / (U[11] - U[10]) * 1e-6)\n",
    "    \n",
    "# plt.plot(U_line, line(U_line, opt[0], opt[1]), label=r\"Fitting: $R_{diff}=$ \" + f\"{-1/opt[0]:.1f}\" + r\"$\\pm 1.48$ kOhm\", color=\"tab:red\", linestyle='-')\n",
    "\n",
    "# plt.title(\"ВАХ разряда\", fontsize=16)\n",
    "plt.ylabel(\"Amperage, $\\mu A$\")\n",
    "plt.xlabel(\"Voltage, V\")\n",
    "\n",
    "plt.legend()\n",
    "plt.savefig(r\"images\\vah-probe-fit.png\", facecolor=\"white\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f8ecf512-050e-4b8d-b02b-00dadf17dadb",
   "metadata": {},
   "outputs": [],
   "source": [
    "I_in = np.array(I_in)\n",
    "dIdU = np.array(dIdU)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5f36f744-de49-4724-a855-8fd7956dc46f",
   "metadata": {},
   "outputs": [],
   "source": [
    "e = 1.6e-19\n",
    "me = 9.1e-31\n",
    "k = 1.38e-23\n",
    "kTe = 0.5 * e * I_in / dIdU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2aaffe8e-6be3-47cd-aecd-099871fd5417",
   "metadata": {},
   "outputs": [],
   "source": [
    "dU = 2 * kTe / e\n",
    "T = 11800 * dU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6ed2bd62-f4bd-4c2e-b9f2-64f8531ff7b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([42973.01960467, 62783.49451912, 65561.29135539])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a1bc3c32-7e4c-4287-ad8e-986d0976f4e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.82089066, 2.66031756, 2.77802082])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kTe / e"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30a91e74-973a-413e-964a-415419601631",
   "metadata": {},
   "source": [
    "11. Концентрация электронов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a93eced5-57d8-4f5d-9a51-f67347be01db",
   "metadata": {},
   "outputs": [],
   "source": [
    "d=0.2e-3\n",
    "l=5.2e-3\n",
    "n_e = 2.5 * I_in / (e * 3.14 * l * d * np.sqrt((2 * kTe) / (22*1.66e-27)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "907c9a45-f6a8-463d-8d71-db964f1d45e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.14794130e+17, 5.20001953e+16, 2.50742641e+16])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_e"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e916a94-a15c-42fb-a371-73924e5b30cf",
   "metadata": {},
   "source": [
    "12. Плазменная частота колебаний электронов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e89bdcb2-4672-4f12-a3c1-5ad13837fc91",
   "metadata": {},
   "outputs": [],
   "source": [
    "w_p = np.sqrt((4*np.pi * e**2) / me) * np.sqrt(n_e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "be2c9578-5b5f-49c2-aef7-aeec63f63f1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([201448.50999052, 135583.45788038,  94149.5646552 ])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "af2cc93e-e468-4554-b2bb-6125293856dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "rDe = np.sqrt(kTe / (4*np.pi*(e**2)*n_e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cc90fb89-62a5-4885-8d77-bd801c02c60a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.80877642, 5.04428168, 7.42315839])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rDe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c75a366e-32fe-46e8-8247-8f0124d8ac43",
   "metadata": {},
   "outputs": [],
   "source": [
    "rD = np.sqrt(k * 300 / (4*np.pi*(e**2)*n_e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6f577488-e13e-462c-9be5-35154a18c99a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.3348231 , 0.49747673, 0.71640921])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "986fdf69-d8c0-46b0-915c-c3d1f286e3fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "ND = 4/3 * np.pi * (rD)**3 * n_e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "84cf5b5c-1e1c-4bf7-b8ae-c5422056c777",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.80490578e+16, 2.68171048e+16, 3.86189338e+16])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ND"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "728229fc-6e84-4264-98e6-94e31ac6dd92",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = n_e / (266.6 / (300 * k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c9f63713-eaed-472e-9526-08d915c9d245",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.78262453e-06, 8.07504908e-07, 3.89375294e-07])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
