{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "fundamentals": {
   "title": "What temperature does a probe in a hot gas measure?",
   "lead": "A small bead in a duct exchanges energy with both the gas and the duct walls. Calculate the indicated temperature from an energy balance, infer the gas temperature from the reading, and test how emissivity changes the measurement.",
   "unit_slug": "radiation-error-in-a-temperature-probe"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3b99683c163d",
   "metadata": {},
   "source": [
    "# What temperature does a probe in a hot gas measure?\n",
    "\n",
    "A small bead in a duct exchanges energy with both the gas and the duct walls. Calculate the indicated temperature from an energy balance, infer the gas temperature from the reading, and test how emissivity changes the measurement."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "383363ebd9b3",
   "metadata": {
    "fundamentals": {
     "section": "derive-the-model"
    }
   },
   "source": [
    "## Draw the control surface around the bead\n",
    "\n",
    "Let the gas have temperature $T_g$ and the bead a uniform temperature $T_s$. A large, isothermal enclosure at $T_w$ fills the bead's field of view. Use kelvins throughout. Convection supplies the bead with heat at $h A(T_g-T_s)$. A diffuse grey bead emits $\\epsilon\\sigma A T_s^4$ and absorbs $\\epsilon\\sigma A T_w^4$ from the enclosure. The absorptivity and emissivity are equal for the grey surface. The common area $A$ cancels from the steady energy balance when axial conduction through the support is negligible.\n",
    "\n",
    "$$F(T_s)=h(T_g-T_s)-\\epsilon\\sigma(T_s^4-T_w^4)=0.$$\n",
    "\n",
    "For a hotter gas, $F(T_w)$ is positive, $F(T_g)$ is nonpositive, and $F'(T_s)=-h-4\\epsilon\\sigma T_s^3$ is strictly negative. There is exactly one equilibrium between the two reservoir temperatures. A colder gas reverses their ordering and keeps the same argument. This physical bracket gives a bisection solver two trustworthy endpoints. With positive heat capacity, a small rise in bead temperature makes the net heat input negative, which restores equilibrium.\n",
    "\n",
    "The factorization $T_s^4-T_w^4=(T_s-T_w)(T_s+T_w)(T_s^2+T_w^2)$ gives an exact coefficient $h_r=\\epsilon\\sigma(T_s+T_w)(T_s^2+T_w^2)$. At the root,\n",
    "\n",
    "$$\n",
    "T_g-T_s=h_r(T_s-T_w)/h.\n",
    "$$\n",
    "\n",
    "A shiny surface and a fast gas stream therefore act on different terms of the same equation. A larger bead changes the transient response through its mass-to-area ratio, and may also change $h$. Area alone cannot correct the steady reading at fixed $h$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d8d79cae000",
   "metadata": {
    "fundamentals": {
     "section": "worked-prediction"
    }
   },
   "source": [
    "## Predict the indicated temperature\n",
    "\n",
    "Take $T_g=420$ K, $T_w=300$ K, $h=25$ W m$^{-2}$ K$^{-1}$ and $\\epsilon=0.8$. At 390 K, convection supplies 750 W m$^{-2}$ and the radiative loss is approximately 682 W m$^{-2}$. The bead thus warms. At 400 K the convective input has fallen to 500 W m$^{-2}$ while radiative loss reaches 794 W m$^{-2}$. The intersection gives $T_s=391.90$ K, an under-reading of 28.10 K. The two independently evaluated fluxes at equilibrium agree to round-off. Doubling $h$ gives 403.34 K for the same gas and wall. Metal lead wires connected to a cold mount could supply a third heat path, which the two-flux residual cannot independently diagnose."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ef8cef18e0",
   "metadata": {
    "fundamentals": {
     "section": "choose-inputs",
     "notebook_only": true
    }
   },
   "source": [
    "## Set the experimental inputs\n",
    "\n",
    "Edit these values, then run the calculation and both figures again."
   ]
  },
  {
   "cell_type": "code",
   "id": "8ee4b7e6473b",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "teal, orange, blue = \"#19596a\", \"#aa462a\", \"#3868a0\"\n",
    "def axes(xlabel, ylabel):\n",
    "    fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True)\n",
    "    ax.set(xlabel=xlabel, ylabel=ylabel)\n",
    "    ax.grid(alpha=0.2)\n",
    "    return fig, ax"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "32cb27fb8d40",
   "metadata": {},
   "source": [
    "gas_K = 420.0\n",
    "wall_K = 300.0\n",
    "film_W_m2K = 25.0\n",
    "emissivity = 0.8\n",
    "sigma = 5.670374419e-8"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "81e5ef96fbb9",
   "metadata": {},
   "source": [
    "assert min(gas_K, wall_K, film_W_m2K) > 0 and 0 <= emissivity <= 1\n",
    "def indicated(gas, wall, film, eps):\n",
    "    lower, upper = sorted((gas, wall))\n",
    "    for _ in range(70):\n",
    "        trial = (lower + upper) / 2\n",
    "        net_input = film*(gas - trial) - eps*sigma*(trial**4 - wall**4)\n",
    "        if net_input > 0:\n",
    "            lower = trial\n",
    "        else:\n",
    "            upper = trial\n",
    "    return (lower + upper)/2\n",
    "\n",
    "sensor_K = indicated(gas_K, wall_K, film_W_m2K, emissivity)\n",
    "convective = film_W_m2K*(gas_K-sensor_K)\n",
    "radiative = emissivity*sigma*(sensor_K**4-wall_K**4)\n",
    "assert np.isclose(convective, radiative, rtol=1e-9, atol=1e-7)\n",
    "print(f\"Indicated temperature: {sensor_K:.2f} K; gas minus reading: {gas_K-sensor_K:.2f} K\")\n",
    "print(f\"Convection in: {convective:.2f} W/m²; radiation out: {radiative:.2f} W/m²\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "a9bd3dceac13",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "engineering-thermodynamics-engineering-thermodynamics-methods-1.png",
      "alt": "Convective heat input and radiative heat loss versus assumed bead temperature, with the equilibrium at their crossing.",
      "caption": "Follow the curves to the same bead temperature. Their vertical separation is the energy available to warm or cool the bead."
     }
    }
   },
   "source": [
    "trial = np.linspace(max(1, min(wall_K, gas_K)-5), max(wall_K, gas_K)+5, 250)\n",
    "fig, ax = axes(\"Trial bead temperature (K)\", \"Heat flux (W/m²)\")\n",
    "ax.plot(trial, film_W_m2K*(gas_K-trial), color=teal, label=\"Convection into bead\")\n",
    "ax.plot(trial, emissivity*sigma*(trial**4-wall_K**4), color=orange, label=\"Radiation leaving bead\")\n",
    "ax.scatter([sensor_K], [convective], color=\"black\", zorder=3, label=f\"Equilibrium {sensor_K:.1f} K\")\n",
    "ax.legend()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "53fb67901702",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "Follow the curves to the same bead temperature. Their vertical separation is the energy available to warm or cool the bead."
   ]
  },
  {
   "cell_type": "code",
   "id": "bc6045d0b266",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "engineering-thermodynamics-engineering-thermodynamics-methods-2.png",
      "alt": "Gas temperature minus indicated temperature as convection strengthens at several surface emissivities.",
      "caption": "Track one surface along its curve to change the flow, or hold the heat-transfer coefficient fixed and compare surface finishes."
     }
    }
   },
   "source": [
    "films = np.geomspace(max(1e-3, film_W_m2K/8), film_W_m2K*8, 120)\n",
    "fig, ax = axes(\"Convection coefficient (W/m² K)\", \"Gas minus indicated temperature (K)\")\n",
    "for eps, color in zip((0.1, 0.5, 0.9), (teal, blue, orange)):\n",
    "    biases = [gas_K-indicated(gas_K, wall_K, h, eps) for h in films]\n",
    "    ax.semilogx(films, biases, color=color, label=f\"Emissivity {eps:.1f}\")\n",
    "ax.scatter([film_W_m2K], [gas_K-sensor_K], color=\"black\", label=\"Selected probe\")\n",
    "ax.legend()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "0493c5d4850a",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "Track one surface along its curve to change the flow, or hold the heat-transfer coefficient fixed and compare surface finishes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "839e26e27d63",
   "metadata": {
    "fundamentals": {
     "section": "problem-1"
    }
   },
   "source": [
    "## Problem 1 · Correct a recorded temperature\n",
    "\n",
    "The probe reads 400 K. The walls remain at 300 K, $h=25$ W m$^{-2}$ K$^{-1}$ and $\\epsilon=0.8$. Recover the gas temperature without iteration. How would a hotter enclosure affect the sign of this correction?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d00201f21441",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "Rearranging the balance gives\n",
    "\n",
    "$$\n",
    "T_g=T_s+\\epsilon\\sigma(T_s^4-T_w^4)/h.\n",
    "$$\n",
    "\n",
    "The correction is $793.85/25=31.75$ K, giving 431.75 K. If the walls were hotter than the bead, absorbed radiation would exceed emitted radiation and the correction to the reading would be negative. A Celsius temperature can be reported at the end after the fourth powers have been evaluated in kelvins."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b355bdf18ce5",
   "metadata": {
    "fundamentals": {
     "section": "problem-2"
    }
   },
   "source": [
    "## Problem 2 · Specify a minimum convection coefficient\n",
    "\n",
    "For the original gas at 420 K, find $h$ required for the under-reading to be at most 5 K. Then reduce the emissivity to 0.2 and repeat. Explain the ratio."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da31ba55071d",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "At the limiting reading $T_s=415$ K the required coefficient is $\\epsilon\\sigma(415^4-300^4)/5$. For $\\epsilon=0.8$ this is approximately 196 W m$^{-2}$ K$^{-1}$. The required coefficient scales directly with emissivity at this prescribed reading, so the lower-emissivity bead needs about 49 W m$^{-2}$ K$^{-1}$. Above those coefficients the gas transfers more heat at the limiting temperature and drives the equilibrium closer to 420 K."
   ]
  }
 ]
}
