{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2686f52050e8",
   "metadata": {},
   "source": [
    "# A leaky vessel changes its mixture\n",
    "Two nonreacting species share a vessel at fixed temperature. Their pressures initially add according to the ideal-gas law, but the molecules hitting a small hole have a speed bias. Predict whether the gas left behind or the gas escaping initially is richer in the light species."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cdcfae2dd9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "light_mass_u=20.\n",
    "heavy_mass_u=40.\n",
    "light_initial=.50\n",
    "heavy_time_s=100.      # heavy-species depletion time V/(A <vz positive>)\n",
    "assert 0<light_mass_u<heavy_mass_u and 0<light_initial<1 and heavy_time_s>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bfa233e058c",
   "metadata": {},
   "source": [
    "## Integrate the separate number balances\n",
    "The ratio of depletion rates follows the inverse square root of mass. Use two time constants to compute the composition inside the vessel, in the instantaneous beam, and in the collection integrated since the start."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b56b89616df2",
   "metadata": {},
   "outputs": [],
   "source": [
    "rate_ratio=np.sqrt(heavy_mass_u/light_mass_u)\n",
    "t=np.linspace(0,3*heavy_time_s,241)\n",
    "a=light_initial*np.exp(-rate_ratio*t/heavy_time_s)\n",
    "b=(1-light_initial)*np.exp(-t/heavy_time_s)\n",
    "remaining=a/(a+b); beam=rate_ratio*a/(rate_ratio*a+b)\n",
    "escaped_a=light_initial*(-np.expm1(-rate_ratio*t/heavy_time_s))\n",
    "escaped_b=(1-light_initial)*(-np.expm1(-t/heavy_time_s))\n",
    "collected=np.divide(escaped_a,escaped_a+escaped_b,out=np.full_like(a,beam[0]),where=t>0)\n",
    "print(\"Initial light fraction in vessel and escape flux:\", remaining[0],beam[0])\n",
    "assert np.allclose(a+escaped_a,light_initial) and np.allclose(b+escaped_b,1-light_initial)\n",
    "assert np.all(beam>=remaining)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8d031dc723a",
   "metadata": {},
   "source": [
    "## Compare count rates with collected composition\n",
    "At very late times the instantaneous beam becomes mostly heavy even though the first collected atoms were enriched in the light species. Collecting nearly every original atom must eventually reproduce the initial composition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5269a2888d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.9),layout=\"constrained\")\n",
    "ax[0].plot(t,a,color=\"#375b9a\",label=\"Light species\");ax[0].plot(t,b,color=\"#a23d21\",label=\"Heavy species\")\n",
    "ax[0].set(xlabel=\"Time (s)\",ylabel=\"Number / initial total\",title=\"Separate reservoir balances\");ax[0].legend(fontsize=8)\n",
    "for y,label,c in [(remaining,\"In vessel\",\"#375b9a\"),(beam,\"Instantaneous flux\",\"#a23d21\"),(collected,\"Collected since start\",\"#175868\")]:ax[1].plot(t,y,label=label,color=c)\n",
    "ax[1].set(xlabel=\"Time (s)\",ylabel=\"Light-species fraction\",title=\"Three different measurements\");ax[1].legend(fontsize=8)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
