{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "04bcf217",
   "metadata": {},
   "source": [
    "# Will a tolerance band leave assembly clearance?\n",
    "\n",
    "A replacement ring is heated while the shaft stays near room temperature. Predict the assembly clearance for the nominal diameters, then check the two tolerance extremes. The inputs represent an illustrative linear expansion calculation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4823f10",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "d=50.0                         # nominal shaft and bore scale, mm\n",
    "interference_low, interference_high = 22., 48.  # diametral interference, µm\n",
    "alpha_ring,alpha_shaft=12e-6,11e-6             # 1/K\n",
    "rise_ring,rise_shaft=100.,8.                   # K\n",
    "required=10.                                  # desired diametral clearance, µm\n",
    "assert d>0 and 0<=interference_low<=interference_high and min(alpha_ring,alpha_shaft)>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffc591b5",
   "metadata": {},
   "source": [
    "## Subtract the two freely expanded diameters\n",
    "A positive clearance permits assembly in the ideal diameter model. Compute the threshold from the worst initial interference, including thermal expansion of the mating shaft."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5be84b1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def thermal_gain(dt):\n",
    "    return 1000*d*(alpha_ring*dt-alpha_shaft*rise_shaft)\n",
    "minimum=thermal_gain(rise_ring)-interference_high\n",
    "maximum=thermal_gain(rise_ring)-interference_low\n",
    "threshold=(required+interference_high+1000*d*alpha_shaft*rise_shaft)/(1000*d*alpha_ring)\n",
    "assert np.isclose(thermal_gain(threshold)-interference_high,required)\n",
    "print(f\"Clearance range {minimum:.1f} to {maximum:.1f} µm; minimum modelled ring rise for target {threshold:.1f} K\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0eaa8c6",
   "metadata": {},
   "source": [
    "## Inspect the entire clearance band\n",
    "The first figure assumes one shaft temperature. The second varies the shaft rise independently to show how a temperature measurement affects the inference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82a58f0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "dt=np.linspace(0,150,160);shaft_rise=np.linspace(0,30,100)\n",
    "fig,ax=plt.subplots(1,2,figsize=(9,3.5),layout=\"constrained\")\n",
    "ax[0].fill_between(dt,thermal_gain(dt)-interference_high,thermal_gain(dt)-interference_low,alpha=.25,label=\"Tolerance band\")\n",
    "ax[0].plot(dt,thermal_gain(dt)-interference_high,label=\"Smallest clearance\")\n",
    "ax[0].axhline(required,ls=\"--\",color=\"#ab532c\",label=\"Target\")\n",
    "ax[0].set(xlabel=\"Ring rise (K)\",ylabel=\"Diametral clearance (µm)\",title=\"Fixed shaft temperature\")\n",
    "thresholds=(required+interference_high+1000*d*alpha_shaft*shaft_rise)/(1000*d*alpha_ring)\n",
    "ax[1].plot(shaft_rise,thresholds,label=\"Worst-case threshold\")\n",
    "ax[1].plot(rise_shaft,threshold,\"o\",label=\"Selected temperatures\")\n",
    "ax[1].set(xlabel=\"Shaft rise (K)\",ylabel=\"Required ring rise (K)\",title=\"Sensitivity to shaft heating\")\n",
    "for p in ax: p.grid(alpha=.22);p.legend(fontsize=8)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "477bfb1d",
   "metadata": {},
   "source": [
    "Change both materials to the same expansion coefficient and give them the same temperature rise. Does the first-order clearance change? Try a nominal interference while keeping the original worst-case target. Explain why checking the nominal pair alone can miss a tight assembly."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
