{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7af16f2a",
   "metadata": {},
   "source": [
    "# Phase diagram calculations for nitrogen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32d5dd49",
   "metadata": {},
   "source": [
    "In this example, we will construct a PT phase diagram for nitrogen using empirical relations like the Antoine equation and approximations such as the Clausius-Clapeyron equation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb1887c3",
   "metadata": {},
   "source": [
    "## Antoine equation\n",
    "\n",
    "The Antoine equation is an emprical equation of the form\n",
    "\n",
    "$$ \\log P^\\mathrm{sub}(T) = A - \\frac{B}{T + C} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "83c296bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Antoine equation given parameters A, B, and C\n",
    "# Pressure is returned in Pa\n",
    "def P_antoine(A, B, C, T):\n",
    "    return 10**(A - B/(T + C))*100000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed693003",
   "metadata": {},
   "source": [
    "## Clausius-Clapeyron equation\n",
    "\n",
    "Given $\\Delta^\\mathrm{sat}\\underline{H}$, $T_1$, $P_1$, and $T_2$, return $P_2$ by the equation\n",
    "\n",
    "$$ \\ln \\frac{P^\\mathrm{sat}(T_2)}{P^\\mathrm{sat}(T_1)} = \\frac{-\\Delta^\\mathrm{sat}\\underline{H}}{R} \\left ( \\frac{1}{T_2} - \\frac{1}{T_1} \\right ) $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4a95c76a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Clausius-Clapeyron equation\n",
    "# Pressures in Pa, Temperatues in K\n",
    "# Enthalpy in J / mol\n",
    "\n",
    "import numpy as np\n",
    "from scipy import constants\n",
    "R = constants.R\n",
    "\n",
    "def P_CC(delta_sat_H, T1, P1, T2):\n",
    "    return P1*np.exp(-delta_sat_H/R*(1/T2-1/T1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93580680",
   "metadata": {},
   "source": [
    "## $P-T$ plot for nitrogen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87fec321",
   "metadata": {},
   "source": [
    "### Data for nitrogen -- sublimation, VLE, critical point, and triple point\n",
    "\n",
    "NIST Webbook data is included below:\n",
    "\n",
    "https://webbook.nist.gov/cgi/cbook.cgi?ID=C7727379&Units=SI&Mask=4#Thermo-Phase\n",
    "\n",
    "We can find other thermodynamic data in:\n",
    "\n",
    "The solid phase of nitrogen, including estimates of the melting (fusion) line are available on wikipedia with references:\n",
    "\n",
    "https://en.wikipedia.org/wiki/Solid_nitrogen\n",
    "\n",
    "Streng (Miscibility and Compatibility of Some Liquid and Solidified Gases at Low Temperature, J. Chem. Eng. Data, 1971, 16, 357) gives the melting temperature at 1 atm as $ T^\\text{fus} = 63.3 $ K.\n",
    "\n",
    "The enthalpy of sublimation is measured by NIST and reported in:\n",
    "\n",
    "H. Shakeel, H. Wei, and J. Pomeroy, Measurements of enthalpy of sublimation of Ne, N2, O2, Ar, CO2, Kr, Xe, and H2O using a double paddle oscillator, The Journal of Chemical Thermodynamics 118, 127 (2018).\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b4bdcfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data for nitrogen\n",
    "# Molecular weight\n",
    "MW = 28.0134 # g/mol\n",
    "\n",
    "# Critical parameters and acentricity\n",
    "Pc = 33.978*100000 # Critical pressure in Pa\n",
    "Tc = 126.19 # Critical temp in K\n",
    "Vc = 1/(11.18*1000) # Critical volume in m^3/mol (original is 11.18 mol/l)\n",
    "omega = 0.040 # acentric factor\n",
    "\n",
    "# Triple point\n",
    "T_triple = 63.14 # temp in K\n",
    "P_triple = 0.1252*100000 # pressure in Pa\n",
    "\n",
    "# Normal melting point\n",
    "T_melt = 63.3 # in K\n",
    "P_melt = 101325 # in Pa\n",
    "\n",
    "# Antoine equation parameters\n",
    "# Valid temperature range in K\n",
    "T_Antoine_min = 63.14\n",
    "T_Antoine_max = 126\n",
    "\n",
    "A = 3.7362\n",
    "B = 264.651\n",
    "C = -6.788\n",
    "\n",
    "# Enthalpy of sublimation is given by \n",
    "H_sub_1 = 7.34*1000 # J / mol, 23 to 27.5 K\n",
    "\n",
    "# Enthalpy of vaporization\n",
    "H_vap_1 = 6.1*1000 # J / mol, data from 63. to 126. K. (NIST)\n",
    "H_vap_2 = 5.57*1000 # J / mol (data wikipedia)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72d6aac4",
   "metadata": {},
   "source": [
    "### Solid-liquid equilibrium\n",
    "\n",
    "Data for N2 lists the heat of fusion as\n",
    "\n",
    "$$\\Delta^\\mathrm{fus}\\underline{H} = 720 \\text{ J/mol}$$\n",
    "\n",
    "By the Clapeyron equation, we can then find\n",
    "\n",
    "$$ \\left ( \\frac{\\partial P^\\mathrm{sat}}{\\partial T} \\right )_{\\underline{G}^I = \\underline{G}^{II}} = \\frac{\\Delta \\underline{H}}{T \\Delta \\underline{V} }$$\n",
    "\n",
    "As expected, this line should be quite steep. We can integrate the function to give\n",
    "\n",
    "$$ P^\\mathrm{sat}(T_2) = P^\\mathrm{sat}(T_1) + \\frac{\\Delta \\underline{H}}{\\Delta \\underline{V} } \\ln \\left ( \\frac{T_2}{T_1} \\right )$$\n",
    "\n",
    "In the case for nitrogen, we have data for the triple point and normal melting point. Therefore, we can estimate the entire melting (fusion) line.\n",
    "\n",
    "Rearranging,\n",
    "\n",
    "$$ \\Delta \\underline{V} = \\frac{\\Delta \\underline{H} \\ln (T_2/T_1)}{P^\\mathrm{sat}(T_2) - P^\\mathrm{sat}(T_1)}$$\n",
    "\n",
    "Using the triple and normal meltint point temperature and pressures, we can find the change in molar volume and use the Clapeyron equation to plot the melting line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cd670958",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Integrated Clapeyron equation\n",
    "def P_clapeyron(T1, P1, T2, delta_H, delta_V):\n",
    "    return P1 + delta_H/delta_V*np.log(T2/T1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1f0615ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculated molar volume: 2.05e-05 m^3/mol\n"
     ]
    }
   ],
   "source": [
    "# Data for nitrogen\n",
    "\n",
    "# Other data\n",
    "delta_fus_H = 0.72*1000 # Heat of fusion, J/mol (Wikipedia, 0.72 kJ/mol)\n",
    "\n",
    "# Calculate Delta V\n",
    "delta_fus_V = delta_fus_H*np.log(T_melt/T_triple)/(P_melt-P_triple)\n",
    "\n",
    "print(f\"Calculated molar volume: {delta_fus_V:.2e} m^3/mol\")\n",
    "#print(delta_fus_H/delta_fus_V)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3289cdae",
   "metadata": {},
   "source": [
    "### Building the PT diagram plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "69d95f54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'P-T diagram for nitrogen')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 48660 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# Make a blank plot\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "# Plot the Antoine equation\n",
    "T = np.arange(T_Antoine_min, T_Antoine_max+1, 1)\n",
    "ax.plot(T, P_antoine(A, B, C, T), label=r\"$P^\\mathrm{vap}$ (Antoine)\")\n",
    "\n",
    "# Use Clausius-Clapeyron to plot the sublimation curve\n",
    "T = np.linspace(24, T_triple, 300)\n",
    "P = P_CC(H_sub_1,T_triple, P_triple, T)\n",
    "ax.plot(T, P, label=r\"$P^\\mathrm{sub}$ (Clausius-Clapeyron)\")\n",
    "\n",
    "# Use Clausius-Clapeyron to plot the vaporization curve \n",
    "# starting from the critical point down\n",
    "T = np.linspace(Tc, T_Antoine_min, 300)\n",
    "P = P_CC(H_vap_1,Tc, Pc, T)\n",
    "ax.plot(T, P, label=r\"$P^\\mathrm{vap}$ (Clausius-Clapeyron)\")\n",
    "\n",
    "# Use the integrated Clapeyron equation to plot the melting line\n",
    "T = np.linspace(T_triple, T_triple+18, 100)\n",
    "P = P_clapeyron(T_triple, P_triple, T, delta_fus_H, delta_fus_V)\n",
    "ax.plot(T, P, label=r\"$P^\\mathrm{fus}$ (Clausius)\")\n",
    "\n",
    "# Plot the triple point\n",
    "plt.plot(T_triple, P_triple, \"bo\", label=\"triple point\")\n",
    "\n",
    "# Plot the critical point\n",
    "plt.plot(Tc, Pc, \"ro\", label=\"critical point\")\n",
    "\n",
    "# Plot the normal melting point (from Streng)\n",
    "plt.plot(T_melt, P_melt, \"co\", label=\"normal melting point\")\n",
    "\n",
    "# Plot the normal boiling point (from Streng)\n",
    "plt.plot(77.4, 101325, \"mo\", label=\"normal boiling point\")\n",
    "\n",
    "# Label and axes\n",
    "plt.ylim([100,Pc*2])\n",
    "plt.xlim([40,Tc*1.2])\n",
    "ax.set_yscale(\"log\")\n",
    "ax.legend(frameon=True, fancybox=True, edgecolor='none')\n",
    "plt.xlabel('T (K)')\n",
    "plt.ylabel('P (Pa)')\n",
    "plt.title('P-T diagram for nitrogen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f15f416f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
