{
 "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": [
    "# Submodel of a vent hole in a steel wheel\n",
    "\n",
    "The vent holes in the disc of a steel wheel are its fatigue-critical spots, but the wheel load\n",
    "enters through the rim and leaves through the bolts. **Submodelling** resolves the hole without\n",
    "meshing the whole wheel finely: a coarse model of the wheel carries loads and supports, and a\n",
    "fine model of the region around the hole is driven by the coarse displacement on its cut faces.\n",
    "\n",
    "The wheel has a 6 mm disc of 336 mm diameter with a 60 mm bore, five bolt holes and five vent\n",
    "holes of 50 mm, and a rim 340 mm in diameter, 5 mm thick and 140 mm wide. The bolt holes are held\n",
    "fixed; the wheel load $F=5$ kN acts upwards on the bottom of the rim, spread evenly over the\n",
    "contact patch of $\\pm20°$ around the bottom. One vent hole sits right above the contact patch.\n",
    "\n",
    "| face | global model | submodel |\n",
    "|---|---|---|\n",
    "| `bolts` | $\\mathbf u=0$ | - |\n",
    "| `rim`, within $\\pm20°$ of the bottom | traction $F/A_{patch}\\ \\mathbf e_y$ | - |\n",
    "| `cut` (four planes around the bottom vent hole) | - | displacement of the global model |\n",
    "| all other faces | free | free |\n",
    "\n",
    "The wheel comes from the CAD file `wheel_rim_submodel.step`, the submodel region from\n",
    "`wheel_rim_submodel_local.step`; the script `wheel_rim_submodel_geometry.py` builds both with the\n",
    "CAD objects of OneSimulation.\n",
    "\n",
    "**Reference.** A finely meshed model of the whole wheel gives the hole stress the submodel must\n",
    "reproduce, and the reactions at the bolts must balance the wheel load (R. D. Cook, D. S. Malkus,\n",
    "M. E. Plesha and R. J. Witt, *Concepts and Applications of Finite Element Analysis*, 4th ed.,\n",
    "Wiley (2002), Sec. 9.12, submodelling)."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.d2 import Plane\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.materials import IsotropicSolid\n",
    "from onesim.meshing import LocalRefinement, TetrahedralMesher\n",
    "from onesim.numerics import Numerics\n",
    "from onesim.numerics.fem import FiniteElement, Lagrange\n",
    "from onesim.numerics.linear import SparseDirect\n",
    "from onesim.physics.solid_mechanics import (\n",
    "    FixedDisplacement,\n",
    "    LinearElasticity,\n",
    "    Traction,\n",
    "    TractionFree,\n",
    ")\n",
    "from onesim.simulation import Problem, Simulation\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geometry and meshes\n",
    "\n",
    "Quadratic tetrahedra: the coarse wheel mesh has 8 mm cells everywhere, also on the vent hole;\n",
    "the submodel and the fine reference model have 1 mm cells on the bottom vent hole."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "wheel, local = load(\"wheel_rim_submodel.step\"), load(\"wheel_rim_submodel_local.step\")\n",
    "faces, cut_faces = wheel.boundaries, local.boundaries\n",
    "mesh_on = lambda part, vent, size: TetrahedralMesher(\n",
    "    target_size=0.008, refinements=(LocalRefinement(region=vent, target_size=size),)\n",
    ").generate(part)\n",
    "coarse, fine, detail = (\n",
    "    mesh_on(wheel, faces.vent, 0.008),\n",
    "    mesh_on(wheel, faces.vent, 0.001),\n",
    "    mesh_on(local, cut_faces.vent, 0.001),\n",
    ")\n",
    "\n",
    "view = plots.Camera(elevation=60, azimuth=-110)\n",
    "fig = plots.figure(1, 3, size=(19, 6))\n",
    "plots.geometry(\n",
    "    wheel,\n",
    "    selection={\"bolts\": faces.bolts, \"rim\": faces.rim, \"bottom vent hole\": faces.vent},\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=\"Wheel with its named faces\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    coarse,\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    camera=view,\n",
    "    title=f\"Global model: {coarse.cell_count} tetrahedra\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.mesh(\n",
    "    detail,\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    camera=plots.Camera(elevation=60, azimuth=-90),\n",
    "    title=f\"Submodel: {detail.cell_count} tetrahedra\",\n",
    "    ax=fig[2],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Global model and reference"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "force, rim_radius, half_patch, width = 5000.0, 0.170, np.radians(20.0), 0.140\n",
    "pressure = force / (2 * half_patch * rim_radius * width)\n",
    "steel = IsotropicSolid(\n",
    "    density=7850.0,\n",
    "    specific_heat_capacity=460.0,\n",
    "    thermal_conductivity=50.0,\n",
    "    youngs_modulus=210e9,\n",
    "    poisson_ratio=0.3,\n",
    "    thermal_expansion_coefficient=12e-6,\n",
    ")\n",
    "numerics = Numerics(\n",
    "    spatial=FiniteElement(element=Lagrange(order=2)), linear_solver=SparseDirect()\n",
    ")\n",
    "model = LinearElasticity(domain=wheel.volume, material=steel)\n",
    "problem = Problem(\n",
    "    name=\"Steel wheel\",\n",
    "    geometry=wheel,\n",
    "    physics=(model,),\n",
    "    boundary_conditions=(\n",
    "        FixedDisplacement(boundary=faces.bolts, physics=model, value=(0.0, 0.0, 0.0)),\n",
    "        Traction(\n",
    "            boundary=faces.rim,\n",
    "            physics=model,\n",
    "            value=lambda x, y, z: np.stack(\n",
    "                (\n",
    "                    0 * y,\n",
    "                    np.where(y < -rim_radius * np.cos(half_patch), pressure, 0.0),\n",
    "                    0 * y,\n",
    "                ),\n",
    "                axis=-1,\n",
    "            ),\n",
    "        ),\n",
    "        TractionFree(boundary=faces.all - faces.bolts - faces.rim, physics=model),\n",
    "    ),\n",
    ")\n",
    "global_result, reference_result = (\n",
    "    Simulation(problem=problem, mesh=mesh, numerics=numerics).solve_stationary()\n",
    "    for mesh in (coarse, fine)\n",
    ")\n",
    "global_displacement = global_result.field(model.displacement)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Submodel\n",
    "\n",
    "The four cut faces of the submodel take the displacement of the coarse wheel model, evaluated at\n",
    "their own nodes."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "sub = LinearElasticity(domain=local.volume, material=steel)\n",
    "submodel = Simulation(\n",
    "    problem=Problem(\n",
    "        name=\"Vent hole submodel\",\n",
    "        geometry=local,\n",
    "        physics=(sub,),\n",
    "        boundary_conditions=(\n",
    "            FixedDisplacement(\n",
    "                boundary=cut_faces.cut,\n",
    "                physics=sub,\n",
    "                value=lambda x, y, z: global_displacement.evaluate(\n",
    "                    np.column_stack((x, y, z))\n",
    "                ),\n",
    "            ),\n",
    "            TractionFree(boundary=cut_faces.all - cut_faces.cut, physics=sub),\n",
    "        ),\n",
    "    ),\n",
    "    mesh=detail,\n",
    "    numerics=numerics,\n",
    ").solve_stationary()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "The peak von Mises stress on the bottom vent hole from the three models and the balance of the\n",
    "bolt reactions with the wheel load."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "peaks = {\n",
    "    name: result.field(physics.von_mises_stress)\n",
    "    .sample(along=vent, subdivisions=4)\n",
    "    .values.max()\n",
    "    for name, result, physics, vent in (\n",
    "        (\"coarse global model\", global_result, model, faces.vent),\n",
    "        (\"submodel\", submodel, sub, cut_faces.vent),\n",
    "        (\"fine model of the whole wheel\", reference_result, model, faces.vent),\n",
    "    )\n",
    "}\n",
    "for name, peak in peaks.items():\n",
    "    print(f\"{name:30s} peak von Mises stress on the vent hole {peak / 1e6:7.2f} MPa\")\n",
    "print(\n",
    "    f\"submodel against the fine model of the whole wheel: {100 * (peaks['submodel'] / peaks['fine model of the whole wheel'] - 1):+.2f} %, \"\n",
    "    f\"coarse model {100 * (peaks['coarse global model'] / peaks['fine model of the whole wheel'] - 1):+.2f} %\"\n",
    ")\n",
    "print(\n",
    "    f\"bolt reactions: {abs(global_displacement.reaction(faces.bolts)[1]):.1f} N against the wheel load {force:.0f} N \"\n",
    "    f\"(the contact patch is resolved by the facets of the rim)\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The von Mises stress on the deformed wheel, in the disc around the bottom vent hole from the\n",
    "submodel (just below the outer face, where the bending stress peaks), and the three hole peaks."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "megapascal = lambda s, position: s / 1e6\n",
    "fig = plots.figure(1, 3, size=(19, 6))\n",
    "plots.surface(\n",
    "    global_result.field(model.von_mises_stress),\n",
    "    component=megapascal,\n",
    "    deformation=global_displacement,\n",
    "    deformation_scale=50.0,\n",
    "    limits=(0, 70),\n",
    "    camera=view,\n",
    "    label=\"von Mises stress [MPa]\",\n",
    "    title=\"Global model, deformation magnified 50 times\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.section(\n",
    "    submodel.field(sub.von_mises_stress),\n",
    "    plane=Plane(origin=(0, 0, 0.0055), normal=(0, 0, 1)),\n",
    "    component=megapascal,\n",
    "    limits=(0, 70),\n",
    "    subdivisions=3,\n",
    "    label=\"von Mises stress [MPa]\",\n",
    "    title=\"Submodel, just below the outer face of the disc\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.bars(\n",
    "    list(peaks),\n",
    "    {\"peak von Mises stress\": [peak / 1e6 for peak in peaks.values()]},\n",
    "    ylabel=\"peak von Mises stress on the vent hole [MPa]\",\n",
    "    title=\"Vent hole peak from the three models\",\n",
    "    ax=fig[2],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Add a lateral cornering force on the rim, round the vent hole edges in the submodel only, or\n",
    "feed the submodel stress into `fatigue_cycles` for a rotating wheel, where each vent hole passes\n",
    "the contact patch once per revolution."
   ]
  }
 ]
}
