{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eaba04c87904",
   "metadata": {},
   "source": [
    "# Why electrons and holes can share a diffusion width\n",
    "A light pulse makes equal excess electron and hole densities. A small internal electric field can make their particle fluxes agree even when their mobilities are very different. Compare this coupled motion with two independent Gaussian packets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e9010dd82e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "T_K=300.\n",
    "mu_n=.12             # m²/(V s)\n",
    "mu_p=.04\n",
    "n_over_p=1.0         # local equilibrium density ratio for the linearised coefficients\n",
    "time_us=1.\n",
    "initial_width_um=20.\n",
    "assert min(T_K,mu_n,mu_p,n_over_p,initial_width_um)>0 and time_us>=0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b811fdc4cf41",
   "metadata": {},
   "source": [
    "## Impose vanishing total charge current\n",
    "Take a slowly varying neutral excess with gradients equal for electrons and holes. Mobilities and background densities are uniform on the packet scale. The Einstein relation supplies diffusion constants for nondegenerate carriers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "040c07160db3",
   "metadata": {},
   "outputs": [],
   "source": [
    "thermal_voltage=8.617333262e-5*T_K\n",
    "Dn,Dp=mu_n*thermal_voltage,mu_p*thermal_voltage\n",
    "Da=(mu_n*n_over_p*Dp+mu_p*Dn)/(mu_n*n_over_p+mu_p)\n",
    "def width(D,t):\n",
    "    return np.sqrt(initial_width_um**2+2*D*1e6*t)\n",
    "print(\"Coupled diffusion coefficient (m²/s) and width (µm):\", Da,width(Da,time_us))\n",
    "assert min(Dn,Dp)<=Da+1e-15 and Da<=max(Dn,Dp)+1e-15\n",
    "if n_over_p==1:assert np.isclose(Da,2*Dn*Dp/(Dn+Dp))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee21242b00da",
   "metadata": {},
   "source": [
    "## Compare profiles and slope estimates\n",
    "All plotted profiles have the same integrated excess. The spatial variance, rather than its square root, grows linearly. Include a longer time interval before estimating a slope from noisy data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a25ef4965096",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.9),layout=\"constrained\")\n",
    "xx=np.linspace(-4*width(max(Dn,Dp),time_us),4*width(max(Dn,Dp),time_us),300)\n",
    "ts=np.linspace(0,max(2*time_us,.1),180)\n",
    "for D,name,col in [(Dn,\"Electrons alone\",\"#375b9a\"),(Dp,\"Holes alone\",\"#a23d21\"),(Da,\"Neutral packet\",\"#175868\")]:\n",
    "    w=width(D,time_us)\n",
    "    ax[0].plot(xx,np.exp(-.5*(xx/w)**2)/(np.sqrt(2*np.pi)*w),color=col,label=name)\n",
    "    ax[1].plot(ts,width(D,ts)**2,color=col,label=name)\n",
    "ax[0].set(xlabel=\"Position (µm)\",ylabel=\"Normalised density (1/µm)\",title=\"Profiles at the selected delay\");ax[0].legend(fontsize=8)\n",
    "ax[1].set(xlabel=\"Delay (µs)\",ylabel=\"Spatial variance (µm²)\",title=\"Extract the diffusion coefficient\");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
}
