{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "OneSimulation (venv)",
   "language": "python",
   "name": "onesim"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Steam reformer geometry\n",
    "\n",
    "In a tubular steam reformer methane and steam react in tubes filled with catalyst while hot flue\n",
    "gas flows around the tubes in the shell and supplies the heat of the endothermic reaction. A model\n",
    "needs two kinds of domains: the catalyst beds inside the tubes and the shell space around them,\n",
    "with the tube walls as the faces through which the heat passes.\n",
    "\n",
    "| part | built from | count |\n",
    "|---|---|---|\n",
    "| `tube_0` ... `tube_18` | `Cylinder`, 24 mm diameter, 0.4 m long; centre tube, ring of 6 at 60 mm, ring of 12 at 115 mm | 19 |\n",
    "| `shell` | `Cylinder` of 300 mm diameter and 0.5 m height less all tubes, with 50 mm plenums above and below the tubes | 1 |\n",
    "\n",
    "The positions of a ring come from rotating one tube about the axis (`rotated`); the shell space\n",
    "is the shell cylinder with every tube subtracted.\n",
    "\n",
    "**Reference.** With shell radius $R_s$ and height $H_s = H + 2p$, tube radius $r$, length $H$ and\n",
    "$N$ tubes: $V_\\text{tubes} = N\\pi r^2 H$, $V_\\text{shell} = \\pi R_s^2 H_s - N\\pi r^2 H$, the\n",
    "heat-transfer area over walls and tube ends is $A = N(2\\pi r H + 2\\pi r^2)$, and a cross-section\n",
    "through the tubes has $\\pi(R_s^2 - N r^2)$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from functools import reduce\n",
    "\n",
    "import numpy as np\n",
    "from onesim.cad.d3 import Assembly, Cylinder\n",
    "from onesim.meshing import TetrahedralMesher\n",
    "from onesim.meshing.quality import quality\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tubes and shell\n",
    "\n",
    "A tube on the x axis at the ring radius, turned to its place on the ring."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "shell_radius, tube_radius, height, plenum = 0.15, 0.012, 0.4, 0.05\n",
    "rings = ((0.0, 1), (0.06, 6), (0.115, 12))\n",
    "tubes = [\n",
    "    Cylinder(radius=tube_radius, length=height, origin=(ring, 0.0, plenum)).rotated(\n",
    "        (0, 0, 1), 2 * np.pi * k / count + np.pi / count\n",
    "    )\n",
    "    for ring, count in rings\n",
    "    for k in range(count)\n",
    "]\n",
    "tubes = [tube.named(f\"tube_{index}\") for index, tube in enumerate(tubes)]\n",
    "shell = reduce(\n",
    "    lambda body, tube: body - tube,\n",
    "    tubes,\n",
    "    Cylinder(radius=shell_radius, length=height + 2 * plenum),\n",
    ").named(\"shell\")\n",
    "reformer = Assembly(parts=[shell, *tubes])\n",
    "print(f\"{len(tubes)} tubes; shell space: {len(shell.faces)} faces\")\n",
    "\n",
    "mesh = TetrahedralMesher(\n",
    "    target_size=0.03, curvature_angle=0.5, max_candidate_cells=400_000\n",
    ").generate(reformer)\n",
    "view = plots.Camera(elevation=30, azimuth=-60)\n",
    "fig = plots.figure(1, 2, size=(16, 8))\n",
    "plots.render(\n",
    "    tubes,\n",
    "    camera=view,\n",
    "    material=\"steel\",\n",
    "    shadows=False,\n",
    "    title=\"Steam reformer: the 19 catalyst tubes\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    cell_values=quality(mesh)[\"shape\"],\n",
    "    limits=(0, 1),\n",
    "    plane=((0.0, 0.0, plenum + height / 2), (1.0, 0.0, 0.0)),\n",
    "    label=\"shape measure\",\n",
    "    camera=plots.Camera(elevation=10, azimuth=0),\n",
    "    title=f\"Cut through three tubes: {mesh.cell_count} cells\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "Volumes of the shell space and the tubes in the CAD model and the mesh, the contact area between\n",
    "them, and the area of an end face of the shell space."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "count = len(tubes)\n",
    "cells = quality(mesh)[\"size\"]\n",
    "meshed = {\n",
    "    part.id: cells[list(mesh.region_cells(reformer.volume_of(part.id)))].sum()\n",
    "    for part in reformer.volumes\n",
    "}\n",
    "print(\n",
    "    f\"shell space: CAD {shell.measure * 1e3:.5f} l, mesh {meshed['shell'] * 1e3:.5f} l, \"\n",
    "    f\"closed form {(np.pi * shell_radius**2 * (height + 2 * plenum) - count * np.pi * tube_radius**2 * height) * 1e3:.5f} l\"\n",
    ")\n",
    "print(\n",
    "    f\"tubes:       CAD {sum(tube.measure for tube in tubes) * 1e3:.5f} l, mesh {sum(meshed[tube.name] for tube in tubes) * 1e3:.5f} l, \"\n",
    "    f\"closed form {count * np.pi * tube_radius**2 * height * 1e3:.5f} l\"\n",
    ")\n",
    "print(\n",
    "    f\"heat-transfer area: {sum(contact.area for contact in reformer.interfaces):.6f} m2 in {len(reformer.interfaces)} contacts, \"\n",
    "    f\"closed form N (2 pi r H + 2 pi r^2) {count * (2 * np.pi * tube_radius * height + 2 * np.pi * tube_radius**2):.6f} m2\"\n",
    ")\n",
    "section = shell.section((0, 0, 1), (0.0, 0.0, plenum + height / 2))\n",
    "print(\n",
    "    f\"cross-section of the shell space: {section.measure * 1e4:.4f} cm2, closed form {np.pi * (shell_radius**2 - count * tube_radius**2) * 1e4:.4f} cm2\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The cross-section of the shell space with the nineteen tube openings, and the mesh cut across the\n",
    "tubes at half height."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "fig = plots.figure(1, 2, size=(16, 7))\n",
    "plots.geometry(\n",
    "    section,\n",
    "    selection={\"shell space\": section.boundaries.all},\n",
    "    labels=False,\n",
    "    title=\"Cross-section at half height\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    cell_values=quality(mesh)[\"shape\"],\n",
    "    limits=(0, 1),\n",
    "    plane=((0.0, 0.0, plenum + height / 2), (0.0, 0.0, 1.0)),\n",
    "    label=\"shape measure\",\n",
    "    camera=plots.Camera(elevation=60, azimuth=-60),\n",
    "    title=\"Mesh cut at half height\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Add baffles as thin discs subtracted from the shell, give the tube walls their own thickness with\n",
    "`AnnularCylinder`, or solve the heat flow from the hot shell space into the catalyst beds."
   ]
  }
 ]
}
