{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1324fa95",
   "metadata": {},
   "source": [
    "# How a beam spreads a point load through an elastic support\n",
    "\n",
    "Imagine looking under a long workbench rail carried on an elastic strip. Guess which part of the strip supports half the load. Compute the deflection and the cumulative reaction below. The default parameters describe a bilateral linear support. Change the four numbers in the first cell and run the cells in order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "812d43c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "E = 70e9             # Young modulus, Pa\n",
    "width, depth = 0.04, 0.02  # rectangular rail, m\n",
    "k = 1.5e6            # foundation force per length per displacement, N/m²\n",
    "P = 150.0            # downward point load, N\n",
    "assert min(E, width, depth, k, P) > 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d0a8692",
   "metadata": {},
   "source": [
    "## Construct the field from the half-line solution\n",
    "The support reaction per unit length is $k w$. Symmetry makes the slope zero at the load. The jump in the third derivative supplies the concentrated force. The cumulative signed reaction within a symmetric interval follows by integrating the resulting field."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e197bd57",
   "metadata": {},
   "outputs": [],
   "source": [
    "I = width * depth**3 / 12\n",
    "EI = E * I\n",
    "beta = (k / (4*EI))**0.25\n",
    "length_scale = 1/beta\n",
    "w0 = P*beta/(2*k)\n",
    "\n",
    "def deflection(x):\n",
    "    s = beta * np.abs(x)\n",
    "    return w0*np.exp(-s)*(np.cos(s) + np.sin(s))\n",
    "\n",
    "def captured(distance):\n",
    "    s = beta * np.abs(distance)\n",
    "    return 1 - np.exp(-s)*np.cos(s)\n",
    "\n",
    "print(f\"EI = {EI:.3f} N m²; decay length = {length_scale:.4f} m\")\n",
    "print(f\"Centre deflection = {w0*1e3:.4f} mm\")\n",
    "print(f\"Fraction within one decay length = {captured(length_scale):.5f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dd20e5d",
   "metadata": {},
   "source": [
    "## Solve an independently discretised beam\n",
    "Represent the curvature by second differences on a long simply supported interval. Form the energy Hessian and solve for the nodal displacement. The middle force is divided by the grid spacing to express it as a load per length. The cells below run with NumPy; a local OneSimulation installation can supply the same linear solve."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b16e74e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    from onesim.numerics.linear.matrices.dense_linear_algebra import solve_dense\n",
    "    backend = \"OneSimulation dense linear solver\"\n",
    "except ImportError:\n",
    "    solve_dense = np.linalg.solve\n",
    "    backend = \"NumPy linear solver\"\n",
    "\n",
    "def discrete_beam(points):\n",
    "    edge = 8*length_scale\n",
    "    x = np.linspace(-edge, edge, points+2)[1:-1]\n",
    "    h = x[1]-x[0]\n",
    "    D2 = (-2*np.eye(points) + np.eye(points,k=1) + np.eye(points,k=-1))/h**2\n",
    "    force = np.zeros(points)\n",
    "    force[points//2] = P/h\n",
    "    w = solve_dense(EI*(D2.T @ D2) + k*np.eye(points), force)\n",
    "    return x, w\n",
    "\n",
    "for count in (65,129):\n",
    "    x_grid, w_grid = discrete_beam(count)\n",
    "    discrepancy = abs(w_grid[count//2]/w0-1)\n",
    "    print(f\"{backend}, {count} unknowns: centre error {100*discrepancy:.3f}%\")\n",
    "assert discrepancy < 0.015\n",
    "assert abs(captured(12*length_scale)-1) < 1e-5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20a0cba2",
   "metadata": {},
   "source": [
    "## Observe the field and the signed support force\n",
    "The bending moment has a different shape from the displacement. The right panel integrates reactions outward from the load. The reference at one marks the full applied force."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b2f069d",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(-4*length_scale,4*length_scale,501)\n",
    "d = np.linspace(0,5*length_scale,320)\n",
    "fig, ax = plt.subplots(1,2,figsize=(9,3.5),layout=\"constrained\")\n",
    "ax[0].plot(x,1e3*deflection(x),label=\"Infinite-beam solution\")\n",
    "mask = np.abs(x_grid) <= 4*length_scale\n",
    "ax[0].plot(x_grid[mask][::2],1e3*w_grid[mask][::2],\"o\",ms=3,label=\"Discrete energy solution\")\n",
    "ax[0].axhline(0,color=\"0.5\",lw=0.8)\n",
    "ax[0].set(xlabel=\"Rail coordinate (m)\",ylabel=\"Downward deflection (mm)\",title=\"Local rail displacement\")\n",
    "ax[1].plot(d,captured(d),label=\"Signed reaction / load\")\n",
    "ax[1].axhline(1,color=\"0.45\",ls=\":\",label=\"Applied load\")\n",
    "ax[1].set(xlabel=\"Distance on each side (m)\",ylabel=\"Fraction of load\",title=\"Reaction collected under the rail\")\n",
    "for a in ax:\n",
    "    a.grid(alpha=.22); a.legend(fontsize=8)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec0c8ff9",
   "metadata": {},
   "source": [
    "## Change the experiment\n",
    "Double the rail depth before rerunning. Compare the ratio of decay lengths to $2^{3/4}$. Hold the section fixed and increase the foundation stiffness sixteenfold. Explain any values above one in the cumulative reaction. Which part of the plotted deflection would make a support that can only push lose contact?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
