{
 "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": [
    "# A wheel load moving across a simply supported deck\n",
    "\n",
    "Bridges, crane runways and rails carry loads that move. As long as the load moves slowly compared\n",
    "with the natural vibrations of the structure, each position is a static load case and the response\n",
    "follows the position of the load. Plotting a response quantity against the load position gives its\n",
    "*influence line*, the basic design tool for moving loads.\n",
    "\n",
    "**Setup.** A deck slab of length $L=10$ m, width $b=1$ m and depth $h=0.5$ m, concrete with\n",
    "$E=30$ GPa and $\\nu=0.2$. Both end faces rest on supports that hold them vertically and leave them\n",
    "free to rotate; one end is also held horizontally at mid-depth. A wheel load $P=100$ kN, spread\n",
    "smoothly over 0.4 m of the top face, crosses the deck at $v=20$ m/s, so its position is\n",
    "$a(t)=0.5\\text{ m}+v\\,t$. The slab comes from the CAD file `moving_load_bridge_deck.step`,\n",
    "written by `moving_load_bridge_deck_geometry.py` with the CAD objects of OneSimulation.\n",
    "\n",
    "| face | condition |\n",
    "|---|---|\n",
    "| `x_min` | $u_z=0$; $u_x=0$ on the mid-depth line, $u_y=0$ on the centre line |\n",
    "| `x_max` | $u_z=0$ |\n",
    "| `z_max` | moving traction $-q(x-a(t))$ |\n",
    "| other faces | free |\n",
    "\n",
    "**Reference.** For a simply supported Timoshenko beam with a load $P$ at $x=a\\le L/2$ the midspan\n",
    "deflection is (Timoshenko and Gere, *Mechanics of Materials*)\n",
    "\n",
    "$$w\\!\\left(\\tfrac L2\\right)=\\frac{P\\,a\\,(3L^2-4a^2)}{48\\,EI}+\\frac{P\\,a}{2\\,\\kappa GA},\\qquad \\kappa=\\tfrac56 ,$$\n",
    "\n",
    "and symmetric for $a>L/2$. Independently of any beam theory, Maxwell-Betti reciprocity says that the\n",
    "midspan deflection under a load at $a$ equals the deflection at $a$ under the same load at midspan,\n",
    "so the influence line must coincide with the deflection line of the midspan load case."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.materials import IsotropicSolid\n",
    "from onesim.meshing import TetrahedralMesher, with_order\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",
    "    FixedDisplacementComponents,\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 mesh\n",
    "\n",
    "`load` returns the slab with its named faces; quadratic tetrahedra of 0.2 m."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "deck = load(\"moving_load_bridge_deck.step\")\n",
    "faces = deck.boundaries\n",
    "length, width, depth = deck.size\n",
    "mesh = with_order(TetrahedralMesher(target_size=0.2).generate(deck), 2)\n",
    "\n",
    "view = plots.Camera(elevation=25, azimuth=-70)\n",
    "fig = plots.figure(1, 2, size=(15, 5.5))\n",
    "plots.geometry(\n",
    "    deck,\n",
    "    selection={\n",
    "        \"supported ends\": faces.x_min | faces.x_max,\n",
    "        \"deck surface\": faces.z_max,\n",
    "    },\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=\"Deck slab with its named faces\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    camera=view,\n",
    "    title=f\"{mesh.cell_count} quadratic tetrahedra\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "The wheel pressure is a smooth bump of width $w=0.4$ m, $q=\\frac{2P}{bw}\\cos^2\\!\\big(\\pi(x-a)/w\\big)$\n",
    "for $|x-a|<w/2$, whose resultant is exactly $P$. The crossing is sampled every 25 ms, i.e. every\n",
    "0.5 m of travel; each sample is one static solve on the same mesh."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "concrete = IsotropicSolid(\n",
    "    density=2400.0,\n",
    "    specific_heat_capacity=880.0,\n",
    "    thermal_conductivity=1.8,\n",
    "    youngs_modulus=30e9,\n",
    "    poisson_ratio=0.2,\n",
    "    thermal_expansion_coefficient=10e-6,\n",
    ")\n",
    "model = LinearElasticity(domain=deck.volume, material=concrete)\n",
    "force, patch, speed = 100e3, 0.4, 20.0\n",
    "times = np.arange(0.0, 0.4501, 0.025)\n",
    "position = lambda t: 0.5 + speed * t\n",
    "wheel = lambda a: (\n",
    "    lambda x, y, z: np.column_stack(\n",
    "        (\n",
    "            0 * x,\n",
    "            0 * x,\n",
    "            -2\n",
    "            * force\n",
    "            / (width * patch)\n",
    "            * np.cos(np.pi * (x - a) / patch) ** 2\n",
    "            * (np.abs(x - a) < patch / 2),\n",
    "        )\n",
    "    )\n",
    ")\n",
    "supports = (\n",
    "    FixedDisplacementComponents(\n",
    "        boundary=faces.x_min,\n",
    "        physics=model,\n",
    "        components=(\"x\", \"y\", \"z\"),\n",
    "        where={\n",
    "            \"x\": lambda x, y, z: np.abs(z) < 1e-6,\n",
    "            \"y\": lambda x, y, z: np.abs(y) < 1e-6,\n",
    "        },\n",
    "    ),\n",
    "    FixedDisplacementComponents(boundary=faces.x_max, physics=model, components=(\"z\",)),\n",
    ")\n",
    "numerics = Numerics(\n",
    "    spatial=FiniteElement(element=Lagrange(order=2)), linear_solver=SparseDirect()\n",
    ")\n",
    "results = [\n",
    "    Simulation(\n",
    "        problem=Problem(\n",
    "            name=f\"Wheel at t = {t:.3f} s\",\n",
    "            geometry=deck,\n",
    "            physics=(model,),\n",
    "            boundary_conditions=(\n",
    "                *supports,\n",
    "                Traction(boundary=faces.z_max, physics=model, value=wheel(position(t))),\n",
    "                TractionFree(\n",
    "                    boundary=faces.all - faces.x_min - faces.x_max - faces.z_max,\n",
    "                    physics=model,\n",
    "                ),\n",
    "            ),\n",
    "        ),\n",
    "        mesh=mesh,\n",
    "        numerics=numerics,\n",
    "    ).solve_stationary()\n",
    "    for t in times\n",
    "]\n",
    "displacements = [result.field(model.displacement) for result in results]\n",
    "centre_line = lambda x: np.column_stack((x, 0 * x, 0 * x))\n",
    "midspan = np.array(\n",
    "    [\n",
    "        field.evaluate(centre_line(np.array([length / 2])))[0, 2]\n",
    "        for field in displacements\n",
    "    ]\n",
    ")\n",
    "middle = int(np.argmin(np.abs(position(times) - length / 2)))\n",
    "line = displacements[middle].evaluate(centre_line(position(times)))[:, 2]"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparison with beam theory and reciprocity\n",
    "\n",
    "The midspan deflection on the axis for every load position against the Timoshenko influence line,\n",
    "and against the deflection line of the midspan load case at the same positions."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "inertia, area, shear = width * depth**3 / 12, width * depth, 30e9 / (2 * 1.2)\n",
    "near = lambda a: np.minimum(a, length - a)\n",
    "timoshenko = lambda a: -(\n",
    "    force * near(a) * (3 * length**2 - 4 * near(a) ** 2) / (48 * 30e9 * inertia)\n",
    "    + force * near(a) / (2 * 5 / 6 * shear * area)\n",
    ")\n",
    "print(\n",
    "    f\"{'t [s]':>6s} {'a [m]':>6s} {'w(L/2) [mm]':>12s} {'Timoshenko [mm]':>16s} {'deviation':>10s} {'w(a), load at L/2 [mm]':>23s} {'Betti gap':>10s}\"\n",
    ")\n",
    "for t, a, w, w_line in zip(times, position(times), midspan, line):\n",
    "    print(\n",
    "        f\"{t:6.3f} {a:6.2f} {1e3 * w:12.4f} {1e3 * timoshenko(a):16.4f} {100 * (w / timoshenko(a) - 1):+9.2f} % {1e3 * w_line:23.4f} {100 * (w - w_line) / abs(midspan).max():+9.3f} %\"\n",
    "    )\n",
    "print(\n",
    "    f\"largest midspan deflection {1e3 * abs(midspan).max():.3f} mm with the load at a = {position(times[np.argmax(abs(midspan))]):.2f} m; \"\n",
    "    f\"of it {100 * force * length / 2 / (2 * 5 / 6 * shear * area) / abs(timoshenko(length / 2)):.1f} % from shear\"\n",
    ")\n",
    "print(\n",
    "    f\"reaction at the far support with the load at midspan: {abs(displacements[middle].reaction(faces.x_max)[2]) / 1e3:.2f} kN (statics: {force / 2e3:.2f} kN)\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "Left the influence line of the midspan deflection with the Timoshenko curve and the deflection line\n",
    "of the midspan load case; right the deflected deck with the wheel at midspan, 100 times enlarged."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "axis = np.linspace(0.0, length, 201)\n",
    "fig = plots.figure(1, 2, size=(16, 6))\n",
    "plots.line(\n",
    "    points={\n",
    "        \"midspan deflection under the moving load\": (position(times), 1e3 * midspan),\n",
    "        \"deflection line, load at midspan (Betti)\": (position(times), 1e3 * line),\n",
    "    },\n",
    "    reference={\"Timoshenko beam\": (axis, 1e3 * timoshenko(axis))},\n",
    "    xlabel=\"load position a [m]\",\n",
    "    ylabel=\"deflection [mm]\",\n",
    "    title=\"Influence line of the midspan deflection\",\n",
    "    legend=\"upper center\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.surface(\n",
    "    displacements[middle],\n",
    "    component=lambda u, position: 1e3 * u[:, 2],\n",
    "    deformation=displacements[middle],\n",
    "    deformation_scale=100.0,\n",
    "    camera=view,\n",
    "    label=\"u_z [mm]\",\n",
    "    title=\"Deck with the wheel at midspan (×100)\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Add a second axle at a fixed distance behind the first and superpose, trace the bending stress at\n",
    "the bottom fibre instead of the deflection, or replace the slab by a box girder read from its own\n",
    "STEP file. At speeds that approach the natural frequencies of the deck, inertia adds a dynamic\n",
    "amplification that this quasi-static crossing leaves out."
   ]
  }
 ]
}
