{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22d0dd55",
   "metadata": {},
   "source": [
    "# Extract work as two finite bodies equilibrate\n",
    "\n",
    "Two bodies in an insulated enclosure begin at different temperatures. A cyclic reversible device could extract work before their temperatures equalise. Compare this with simply putting the bodies in thermal contact. Change the heat capacities and initial temperatures, then rerun."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8007ba4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "Th,Tc=360.,280.         # initial temperatures, K\n",
    "Ch,Cc=1.2,1.2           # total heat capacities, kJ/K\n",
    "assert Th>Tc>0 and Ch>0 and Cc>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91835b94",
   "metadata": {},
   "source": [
    "## Close separate entropy and energy balances\n",
    "A reversible final state has the initial total entropy. Direct contact preserves the initial total energy. Use these two balances separately to obtain the two different final temperatures."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "830ae166",
   "metadata": {},
   "outputs": [],
   "source": [
    "Trev=np.exp((Ch*np.log(Th)+Cc*np.log(Tc))/(Ch+Cc))\n",
    "Tmix=(Ch*Th+Cc*Tc)/(Ch+Cc)\n",
    "W=(Ch+Cc)*(Tmix-Trev)\n",
    "deltaS=Ch*np.log(Tmix/Th)+Cc*np.log(Tmix/Tc)\n",
    "assert W>0 and deltaS>0\n",
    "assert abs(Ch*np.log(Trev/Th)+Cc*np.log(Trev/Tc))<1e-11\n",
    "print(f\"Direct contact {Tmix:.3f} K; reversible end state {Trev:.3f} K; maximum work {W:.3f} kJ\")\n",
    "print(f\"Entropy created in direct contact {deltaS:.5f} kJ/K.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d545c60",
   "metadata": {},
   "source": [
    "## Follow the reversible path\n",
    "Transfer equal entropy increments out of the hot body and into the cold one. Work is the difference between the released and absorbed energies. At equal temperatures the marginal work vanishes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81bd3c42",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds=np.linspace(0,Ch*np.log(Th/Trev),250)\n",
    "hot=Th*np.exp(-ds/Ch);cold=Tc*np.exp(ds/Cc)\n",
    "work=Ch*(Th-hot)-Cc*(cold-Tc)\n",
    "assert np.isclose(work[-1],W) and np.isclose(hot[-1],cold[-1])\n",
    "fig,ax=plt.subplots(1,2,figsize=(9,3.5),layout=\"constrained\")\n",
    "ax[0].plot(ds,hot,label=\"Hot body\");ax[0].plot(ds,cold,label=\"Cold body\")\n",
    "ax[0].set(xlabel=\"Entropy transferred (kJ/K)\",ylabel=\"Temperature (K)\",title=\"Equal entropy exchanged\")\n",
    "ax[1].plot(ds,work,label=\"Work delivered\")\n",
    "ax[1].axhline(W,ls=\":\",color=\"0.45\",label=\"Maximum\")\n",
    "ax[1].set(xlabel=\"Entropy transferred (kJ/K)\",ylabel=\"Accumulated work (kJ)\",title=\"End of the work-producing path\")\n",
    "for p in ax:p.grid(alpha=.22);p.legend(fontsize=8)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46d5cf56",
   "metadata": {},
   "source": [
    "Double the cold body's heat capacity without changing either temperature. Explain why the reversible end temperature approaches the cold initial temperature as the cold capacity becomes very large. Compare the area under the temperature-gap curve with the total delivered work."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
