{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-01-26T20:40:21.257934Z",
     "iopub.status.busy": "2026-01-26T20:40:21.257721Z",
     "iopub.status.idle": "2026-01-26T20:40:22.046745Z",
     "shell.execute_reply": "2026-01-26T20:40:22.046265Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-01-26T20:40:22.047950Z",
     "iopub.status.busy": "2026-01-26T20:40:22.047844Z",
     "iopub.status.idle": "2026-01-26T20:40:22.059004Z",
     "shell.execute_reply": "2026-01-26T20:40:22.058667Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spin Up Data Points: 4301\n",
      "Spin Down Data Points: 4301\n"
     ]
    }
   ],
   "source": [
    "# File path\n",
    "filename = 'tspin_1o_DS2_DOS'\n",
    "\n",
    "# Read all lines to handle the split structure\n",
    "with open(filename, 'r') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "spin_up_data = []\n",
    "spin_dn_data = []\n",
    "\n",
    "reading_up = False\n",
    "reading_dn = False\n",
    "\n",
    "for line in lines:\n",
    "    if \"Spin-up DOS\" in line:\n",
    "        reading_up = True\n",
    "        reading_dn = False\n",
    "        continue\n",
    "    elif \"Spin-dn DOS\" in line:\n",
    "        reading_up = False\n",
    "        reading_dn = True\n",
    "        continue\n",
    "    \n",
    "    # Skip comments or empty lines\n",
    "    if line.strip().startswith('#') or not line.strip():\n",
    "        continue\n",
    "        \n",
    "    # Parse data lines\n",
    "    parts = line.split()\n",
    "    if len(parts) >= 2:\n",
    "        try:\n",
    "            # Columns: energy, DOS, Integr. DOS, DOS(tsmear/2), DOS(tsmear*2)\n",
    "            # We need energy (col 0) and DOS (col 1)\n",
    "            energy = float(parts[0])\n",
    "            dos = float(parts[1])\n",
    "            \n",
    "            if reading_up:\n",
    "                spin_up_data.append([energy, dos])\n",
    "            elif reading_dn:\n",
    "                spin_dn_data.append([energy, dos])\n",
    "        except ValueError:\n",
    "            continue\n",
    "\n",
    "df_up = pd.DataFrame(spin_up_data, columns=['Energy', 'DOS'])\n",
    "df_dn = pd.DataFrame(spin_dn_data, columns=['Energy', 'DOS'])\n",
    "\n",
    "print(f\"Spin Up Data Points: {len(df_up)}\")\n",
    "print(f\"Spin Down Data Points: {len(df_dn)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-01-26T20:40:22.074101Z",
     "iopub.status.busy": "2026-01-26T20:40:22.074006Z",
     "iopub.status.idle": "2026-01-26T20:40:22.306786Z",
     "shell.execute_reply": "2026-01-26T20:40:22.306367Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 65624 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 6))\n",
    "\n",
    "# Plot Spin Up as positive\n",
    "plt.plot(df_up['Energy'], df_up['DOS'], label='Spin Up (Majority)', color='red')\n",
    "\n",
    "# Plot Spin Down as negative\n",
    "plt.plot(df_dn['Energy'], -df_dn['DOS'], label='Spin Down (Minority)', color='blue')\n",
    "\n",
    "plt.axhline(0, color='black', linewidth=0.8, linestyle='--')\n",
    "plt.xlabel('Energy (Hartree)')\n",
    "plt.ylabel('Density of States (states/Hartree/cell)')\n",
    "plt.title('Spin-Polarized Density of States')\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "\n",
    "# Adjust x limits if needed (based on Fermi energy usually, but using data range here)\n",
    "plt.xlim(df_up['Energy'].min(), df_up['Energy'].max())\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.savefig('dos_plot.png', dpi=300)\n",
    "plt.show()"
   ]
  }
 ],
 "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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
