{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ladder frame geometry\n",
    "\n",
    "Truck and trailer chassis are ladder frames: two long side rails of channel section and a row of\n",
    "cross members between them. The walls are thin against every other dimension, so the frame is\n",
    "analysed with shell elements on the mid-surface of its walls rather than with solid elements.\n",
    "\n",
    "`part.midsurfaces()` finds the pairs of opposite wall faces of a solid and returns one sheet that\n",
    "lies halfway between them, joined at corners and T-joints and carrying the wall thickness.\n",
    "`SheetMesher` lays quadrilaterals on it.\n",
    "\n",
    "| member | section | count |\n",
    "|---|---|---|\n",
    "| side rail | channel $h\\times f\\times t$ = 160 x 60 x 6 mm, flanges pointing inwards, 2.4 m long | 2 |\n",
    "| cross member | flat web 100 x 6 mm between the rail webs, frame width 0.8 m | 4 |\n",
    "\n",
    "**Reference.** A wall of thickness $t$ meeting another wall at a corner or a T-joint has its\n",
    "mid-surface run to the mid-plane of the other wall. With rail length $L$, frame width $W$ and\n",
    "cross member height $h_c$:\n",
    "\n",
    "$$A_\\text{mid} = 2\\big[L(h-t) + 2L(f-\\tfrac t2)\\big] + 4(W-t)\\,h_c,\\qquad\n",
    "  V = 2\\big[Lht + 2L(f-t)t\\big] + 4(W-2t)\\,h_c t .$$"
   ],
   "id": "97f802656c90"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.meshing import SheetMesher\n",
    "from onesim.meshing.quality import quality\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "254b32b20426"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inspect the imported solid\n",
    "\n",
    "Read the demonstration chassis from its STEP file. Two channels and four flat\n",
    "cross members define the envelope. The downloadable CAD script sets their\n",
    "nominal section sizes; the imported solid determines the envelope dimensions."
   ],
   "id": "e1fb49c101cd"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "frame = load(\"ladder_frame_geometry.step\")\n",
    "rail, width, height = frame.size\n",
    "flange, wall, cross = 0.06, 0.006, 0.10\n",
    "volume = (\n",
    "    2 * (rail * height * wall + 2 * rail * (flange - wall) * wall)\n",
    "    + 4 * (width - 2 * wall) * cross * wall\n",
    ")\n",
    "print(f\"Imported frame: {rail:.2f} × {width:.2f} × {height:.2f} m, {len(frame.faces)} CAD faces.\")\n",
    "print(f\"Solid volume {frame.measure * 1e6:.3f} cm³; plate-layout reference {volume * 1e6:.3f} cm³.\")"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "dd1d37cc45fe"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mid-surface and shell mesh\n",
    "\n",
    "The mid-surface has one face per wall; the cross members end on the mid-planes of the rail webs."
   ],
   "id": "c190e9c16ec7"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "middle = frame.midsurfaces()\n",
    "area = 2 * (rail * (height - wall) + 2 * rail * (flange - wall / 2)) + 4 * (width - wall) * cross\n",
    "coarse = SheetMesher(target_size=0.020).generate(middle)\n",
    "mesh = SheetMesher(target_size=0.010).generate(middle)\n",
    "for spacing, level in ((20, coarse), (10, mesh)):\n",
    "    print(f\"{spacing:g} mm target: {len(level.coordinates)} shell nodes, {len(level.quadrilaterals)} quads, area {level.area:.6f} m²\")\n",
    "print(\n",
    "    f\"mid-surface: {len(middle.faces)} faces for 10 walls, thickness {middle.thickness * 1e3:.3f} mm\"\n",
    ")\n",
    "print(\n",
    "    f\"mid-surface area {middle.area:.6f} m2, closed form {area:.6f} m2, deviation {middle.area / area - 1:+.1e}\"\n",
    ")\n",
    "print(\n",
    "    f\"shell mesh: {len(mesh.quadrilaterals)} quadrilaterals, area {mesh.area:.6f} m2; \"\n",
    "    f\"shell volume area x thickness {mesh.area * middle.thickness * 1e6:.3f} cm3\"\n",
    ")\n",
    "report = quality(mesh)\n",
    "print(\n",
    "    f\"quadrilaterals: scaled Jacobian min {report['scaled_jacobian'].min():.3f}, warpage max {report['warpage'].max():.2e}\"\n",
    ")\n",
    "\n",
    "view = plots.Camera(elevation=30, azimuth=-55)\n",
    "fig = plots.figure(1, 2, size=(17, 7))\n",
    "plots.geometry(\n",
    "    frame,\n",
    "    selection={\"walls\": frame.boundaries.all},\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=\"Ladder frame: two channel rails, four cross members\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    camera=view,\n",
    "    title=f\"Shell mesh on the mid-surface: {len(mesh.quadrilaterals)} quadrilaterals\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "e3951f3a3566"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The mid-surface of all walls, and the shell mesh seen from the rear end, where the cross member\n",
    "meets the rail webs in T-joints."
   ],
   "id": "d8a3378ed1d7"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "joint = plots.Camera(elevation=35, azimuth=-130)\n",
    "fig = plots.figure(1, 2, size=(17, 7))\n",
    "plots.geometry(middle, labels=False, camera=view, title=\"Mid-surface of all walls\", ax=fig[0])\n",
    "plots.mesh(mesh, camera=joint, title=\"Shell mesh at the rear cross member\", ax=fig[1])\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "76c33b48e80e"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Load the frame in torsion with `ElasticShell` on `ShellSurface.from_mesh(mesh)`\n",
    "(`onesim.physics.solid_mechanics`), replace the flat cross members by tubes, or add gusset plates\n",
    "at the joints with `frame.gusset(...)`."
   ],
   "id": "64d4377e3eb4"
  }
 ]
}
