{
 "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": [
    "# Stresses in a gantry crane with internal hinges\n",
    "\n",
    "A gantry crane straddles a yard on two rails. On one rail it stands on stiff \"fixed\" legs, on the\n",
    "other on slender \"pendulum\" legs with a moment hinge at the top. The hinges let the main girders\n",
    "grow and shrink with temperature without bending the legs; the fixed legs, clamped to their\n",
    "bogies, keep the crane upright and make it statically indeterminate.\n",
    "\n",
    "**Setup.** Span $L=12$ m along $x$, girder height $H=8$ m, girder centre lines at $y=\\pm1.5$ m. Main\n",
    "girders and fixed legs are welded box sections ($A=1.98\\cdot10^{-2}$ m², $I=5.77\\cdot10^{-4}$ m⁴ about the\n",
    "axis normal to the girder plane, $1.08\\cdot10^{-4}$ m⁴ about the other, $J=1.5\\cdot10^{-4}$ m⁴), the end girders\n",
    "smaller boxes ($A=1.2\\cdot10^{-2}$ m², $I=2\\cdot10^{-4}$ / $0.6\\cdot10^{-4}$ m⁴), the pendulum legs tubes of 300 mm\n",
    "diameter and 10 mm wall. Steel: $E=210$ GPa, $G=81$ GPa, $\\rho=7850$ kg/m³, $\\alpha=12\\cdot10^{-6}$ 1/K. Load\n",
    "cases: (1) self-weight; (2) a payload of 15 t, carried by the trolley on both girders 4 m from\n",
    "the fixed legs; (3) a uniform temperature rise of 30 K of the whole steel structure. The solid model\n",
    "comes from the CAD file `portal_crane_frame_stress.step`, written by\n",
    "`portal_crane_frame_stress_geometry.py` with the CAD objects of OneSimulation; the frame follows\n",
    "the centre lines of its members.\n",
    "\n",
    "| joint | condition |\n",
    "|---|---|\n",
    "| feet of the fixed legs | clamped (all six degrees of freedom) |\n",
    "| feet of the pendulum legs | pinned (translations held) |\n",
    "| tops of the pendulum legs | moment hinges (member start released about both bending axes) |\n",
    "| girders at the trolley | half the payload on each girder |\n",
    "\n",
    "**Reference.** (1) *Equilibrium:* reactions balance the applied forces and their moments about the\n",
    "origin. (2) *Symmetry:* the crane and its loads are mirror-symmetric to the plane $y=0$. (3) *Hand\n",
    "calculations of subsystems.* Under symmetric vertical load each girder with its fixed leg and\n",
    "pendulum leg forms a plane portal. The pendulum leg carries only an axial force, so the fixed leg is\n",
    "a cantilever loaded by the girder end moment $M_0$ alone, and $M_0$ follows from one compatibility\n",
    "condition (force method): the end rotation of the simply supported girder $\\theta_0$ plus the tilt\n",
    "from the axial shortening of the legs equals the rotation of the fixed leg top,\n",
    "\n",
    "$$M_0=\\frac{\\theta_0+\\frac HL\\Big(\\frac{\\bar N_P}{EA_P}-\\frac{\\bar N_F}{EA_F}\\Big)}\n",
    "  {\\frac H{EI_F}+\\frac L{3EI_G}+\\frac H{L^2}\\Big(\\frac1{EA_P}+\\frac1{EA_F}\\Big)},\\qquad\n",
    "  \\theta_0=\\frac{Qab(L+b)}{6LEI_G}\\ \\text{(payload } Q \\text{ at } a\\text{)},\\quad \\theta_0=\\frac{wL^3}{24EI_G}\\ \\text{(weight } w\\text{)},$$\n",
    "\n",
    "with the mean axial forces $\\bar N$ of the legs at $M_0=0$. Temperature: the pendulum legs cannot\n",
    "hold the girders back, so the girders carry no axial force and no bending, and the girder ends at\n",
    "the pendulum legs move $\\alpha\\Delta T L$ further than at the fixed legs. Across the rails the end\n",
    "girder at the fixed legs is restrained by the clamped legs: a symmetric fixed-base frame whose\n",
    "beam grows by $\\alpha\\Delta T\\,2c$ (slope-deflection method, legs bent about their weak axis). The\n",
    "main girders, which twist when the leg tops turn, stiffen this frame a little; the hand calculation\n",
    "leaves them out."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.physics.solid_mechanics import (\n",
    "    FrameMember,\n",
    "    SpatialFrame,\n",
    "    ThermalFrameMemberLoads,\n",
    ")\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geometry\n",
    "\n",
    "The solid model shows the members; the frame model uses their centre lines."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "crane_solid = load(\"portal_crane_frame_stress.step\")\n",
    "plots.geometry(\n",
    "    crane_solid,\n",
    "    selection={\"steel structure\": crane_solid.boundaries.all},\n",
    "    labels=False,\n",
    "    camera=plots.Camera(elevation=22, azimuth=-60),\n",
    "    title=\"Gantry crane: fixed legs (x = 0), pendulum legs (x = 12 m)\",\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Frame model\n",
    "\n",
    "Each girder is divided into 1 m members so that the trolley sits on a joint. The joints of one rail\n",
    "side are numbered from the foot of the fixed leg along the girder to the foot of the pendulum leg.\n",
    "The pendulum legs run from the girder down to their feet and are released at their start."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "span, height, half_gauge, trolley, n = 12.0, 8.0, 1.5, 4.0, 12\n",
    "payload, density, steel, shear, alpha, warming = (\n",
    "    15e3 * 9.81,\n",
    "    7850.0,\n",
    "    210e9,\n",
    "    81e9,\n",
    "    12e-6,\n",
    "    30.0,\n",
    ")\n",
    "box = dict(\n",
    "    youngs_modulus=steel,\n",
    "    shear_modulus=shear,\n",
    "    area=1.98e-2,\n",
    "    bending_inertia_y=5.77e-4,\n",
    "    bending_inertia_z=1.08e-4,\n",
    "    torsion_constant=1.5e-4,\n",
    ")\n",
    "end_box = dict(\n",
    "    youngs_modulus=steel,\n",
    "    shear_modulus=shear,\n",
    "    area=1.2e-2,\n",
    "    bending_inertia_y=2.0e-4,\n",
    "    bending_inertia_z=0.6e-4,\n",
    "    torsion_constant=0.8e-4,\n",
    ")\n",
    "tube = dict(\n",
    "    youngs_modulus=steel,\n",
    "    shear_modulus=shear,\n",
    "    area=np.pi * (0.15**2 - 0.14**2),\n",
    "    bending_inertia_y=np.pi * (0.15**4 - 0.14**4) / 4,\n",
    "    bending_inertia_z=np.pi * (0.15**4 - 0.14**4) / 4,\n",
    "    torsion_constant=np.pi * (0.15**4 - 0.14**4) / 2,\n",
    ")\n",
    "sides = {\"front\": -half_gauge, \"rear\": half_gauge}\n",
    "joints, foot, top, prop = [], {}, {}, {}\n",
    "for side, y in sides.items():\n",
    "    foot[side] = len(joints)\n",
    "    joints.append((0.0, y, 0.0))\n",
    "    for k in range(n + 1):\n",
    "        top[side, k] = len(joints)\n",
    "        joints.append((span * k / n, y, height))\n",
    "    prop[side] = len(joints)\n",
    "    joints.append((span, y, 0.0))\n",
    "members = [\n",
    "    member\n",
    "    for side in sides\n",
    "    for member in (\n",
    "        FrameMember(\n",
    "            name=f\"fixed leg {side}\", start=foot[side], end=top[side, 0], **box\n",
    "        ),\n",
    "        *(\n",
    "            FrameMember(\n",
    "                name=f\"girder {side} {k}\",\n",
    "                start=top[side, k],\n",
    "                end=top[side, k + 1],\n",
    "                **box,\n",
    "            )\n",
    "            for k in range(n)\n",
    "        ),\n",
    "        FrameMember(\n",
    "            name=f\"pendulum leg {side}\",\n",
    "            start=top[side, n],\n",
    "            end=prop[side],\n",
    "            releases=(\"start_ry\", \"start_rz\"),\n",
    "            **tube,\n",
    "        ),\n",
    "    )\n",
    "]\n",
    "members += [\n",
    "    FrameMember(\n",
    "        name=\"end girder fixed side\",\n",
    "        start=top[\"front\", 0],\n",
    "        end=top[\"rear\", 0],\n",
    "        local_y=(-1.0, 0.0, 0.0),\n",
    "        **end_box,\n",
    "    ),\n",
    "    FrameMember(\n",
    "        name=\"end girder pendulum side\",\n",
    "        start=top[\"front\", n],\n",
    "        end=top[\"rear\", n],\n",
    "        local_y=(-1.0, 0.0, 0.0),\n",
    "        **end_box,\n",
    "    ),\n",
    "]\n",
    "crane = SpatialFrame(joints=np.array(joints), members=tuple(members))\n",
    "supports = [\n",
    "    (foot[side], dof, 0.0)\n",
    "    for side in sides\n",
    "    for dof in (\"x\", \"y\", \"z\", \"rx\", \"ry\", \"rz\")\n",
    "] + [(prop[side], dof, 0.0) for side in sides for dof in (\"x\", \"y\", \"z\")]\n",
    "print(\n",
    "    f\"{len(crane.joints)} joints, {len(crane.members)} members, {len(supports)} support conditions\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load cases\n",
    "\n",
    "`self_weight` turns density and gravity into distributed loads in the axes of every member. The\n",
    "payload is split onto the two girder joints under the trolley. The temperature rise acts on all\n",
    "members."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "k_trolley = int(round(trolley / span * n))\n",
    "cases = {\n",
    "    \"self-weight\": crane.solve(\n",
    "        supports=supports, line_loads=crane.self_weight(density=density)\n",
    "    ),\n",
    "    \"payload\": crane.solve(\n",
    "        supports=supports,\n",
    "        forces={top[side, k_trolley]: (0.0, 0.0, -payload / 2) for side in sides},\n",
    "    ),\n",
    "    \"hot day\": crane.solve(\n",
    "        supports=supports,\n",
    "        member_loads=ThermalFrameMemberLoads.linear_temperature(\n",
    "            temperature=293.15 + warming,\n",
    "            reference_temperature=293.15,\n",
    "            expansion_coefficient=alpha,\n",
    "        ),\n",
    "    ),\n",
    "}\n",
    "girder_moment = {\n",
    "    name: np.array(\n",
    "        [result.members[f\"girder front {k}\"].local_end_actions[4] for k in range(n)]\n",
    "        + [-result.members[f\"girder front {n - 1}\"].local_end_actions[10]]\n",
    "    )\n",
    "    for name, result in cases.items()\n",
    "}"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Equilibrium and symmetry\n",
    "\n",
    "Applied loads: the weight of every member at its midpoint, and the payload. The mirror image of a\n",
    "reaction at $y<0$ has the force components $(F_x,-F_y,F_z)$ and the moment components\n",
    "$(-M_x,M_y,-M_z)$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "lengths = np.array(\n",
    "    [np.linalg.norm(crane.joints[m.end] - crane.joints[m.start]) for m in crane.members]\n",
    ")\n",
    "centres = np.array(\n",
    "    [(crane.joints[m.start] + crane.joints[m.end]) / 2 for m in crane.members]\n",
    ")\n",
    "weights = np.array([density * 9.81 * m.area for m in crane.members]) * lengths\n",
    "applied = {\n",
    "    \"self-weight\": (\n",
    "        np.array([0.0, 0.0, -weights.sum()]),\n",
    "        np.cross(centres, np.column_stack((0 * weights, 0 * weights, -weights))).sum(\n",
    "            axis=0\n",
    "        ),\n",
    "    ),\n",
    "    \"payload\": (\n",
    "        np.array([0.0, 0.0, -payload]),\n",
    "        sum(\n",
    "            np.cross(crane.joints[top[side, k_trolley]], (0.0, 0.0, -payload / 2))\n",
    "            for side in sides\n",
    "        ),\n",
    "    ),\n",
    "    \"hot day\": (np.zeros(3), np.zeros(3)),\n",
    "}\n",
    "mirror_force, mirror_moment = np.array([1.0, -1.0, 1.0]), np.array([-1.0, 1.0, -1.0])\n",
    "for name, result in cases.items():\n",
    "    force = result.reaction_force.sum(axis=0) + applied[name][0]\n",
    "    moment = (\n",
    "        result.reaction_moment + np.cross(crane.joints, result.reaction_force)\n",
    "    ).sum(axis=0) + applied[name][1]\n",
    "    scale = np.abs(result.reaction_force).max()\n",
    "    asymmetry = max(\n",
    "        np.abs(\n",
    "            result.reaction_force[[foot[\"rear\"], prop[\"rear\"]]]\n",
    "            - mirror_force * result.reaction_force[[foot[\"front\"], prop[\"front\"]]]\n",
    "        ).max()\n",
    "        / scale,\n",
    "        np.abs(\n",
    "            result.reaction_moment[foot[\"rear\"]]\n",
    "            - mirror_moment * result.reaction_moment[foot[\"front\"]]\n",
    "        ).max()\n",
    "        / (scale * span),\n",
    "    )\n",
    "    print(\n",
    "        f\"{name:12s}: force balance {np.abs(force).max():.1e} N, moment balance {np.abs(moment).max():.1e} N m \"\n",
    "        f\"(largest reaction {scale / 1e3:.2f} kN); mirror asymmetry {asymmetry:.1e}\"\n",
    "    )"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hand calculations of the subsystems\n",
    "\n",
    "The vertical load cases against the force method for one portal, the hot day against the free\n",
    "expansion along the girders and the slope-deflection solution of the fixed-side end frame."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "EI_leg, EI_girder, EA_leg, EA_prop = (\n",
    "    steel * box[\"bending_inertia_y\"],\n",
    "    steel * box[\"bending_inertia_y\"],\n",
    "    steel * box[\"area\"],\n",
    "    steel * tube[\"area\"],\n",
    ")\n",
    "restraint = lambda theta, fixed_force, prop_force: (\n",
    "    theta + height / span * (prop_force / EA_prop - fixed_force / EA_leg)\n",
    ") / (\n",
    "    height / EI_leg\n",
    "    + span / (3 * EI_girder)\n",
    "    + height / span**2 * (1 / EA_prop + 1 / EA_leg)\n",
    ")\n",
    "q, b = payload / 2, span - trolley\n",
    "m_payload = restraint(\n",
    "    q * trolley * b * (span + b) / (6 * span * EI_girder),\n",
    "    q * b / span,\n",
    "    q * trolley / span,\n",
    ")\n",
    "prop_payload, trolley_moment = (q * trolley - m_payload) / span, (\n",
    "    q * trolley - m_payload\n",
    ") / span * b\n",
    "w, w_end, w_leg, w_prop = (\n",
    "    density * 9.81 * box[\"area\"],\n",
    "    density * 9.81 * end_box[\"area\"] * 2 * half_gauge,\n",
    "    density * 9.81 * box[\"area\"] * height,\n",
    "    density * 9.81 * tube[\"area\"] * height,\n",
    ")\n",
    "m_weight = restraint(\n",
    "    w * span**3 / (24 * EI_girder),\n",
    "    w * span / 2 + w_end / 2 + w_leg / 2,\n",
    "    w * span / 2 + w_end / 2 + w_prop / 2,\n",
    ")\n",
    "prop_weight, middle_moment = (\n",
    "    w * span / 2 - m_weight / span + w_end / 2 + w_prop,\n",
    "    w * span**2 / 8 - m_weight / 2,\n",
    ")\n",
    "rows = (\n",
    "    (\n",
    "        \"payload\",\n",
    "        \"pendulum foot force\",\n",
    "        cases[\"payload\"].reaction_force[prop[\"front\"], 2],\n",
    "        prop_payload,\n",
    "        \"kN\",\n",
    "    ),\n",
    "    (\n",
    "        \"payload\",\n",
    "        \"fixed leg foot moment\",\n",
    "        -cases[\"payload\"].reaction_moment[foot[\"front\"], 1],\n",
    "        m_payload,\n",
    "        \"kN m\",\n",
    "    ),\n",
    "    (\n",
    "        \"payload\",\n",
    "        \"girder moment under the trolley\",\n",
    "        girder_moment[\"payload\"][k_trolley],\n",
    "        trolley_moment,\n",
    "        \"kN m\",\n",
    "    ),\n",
    "    (\n",
    "        \"self-weight\",\n",
    "        \"pendulum foot force\",\n",
    "        cases[\"self-weight\"].reaction_force[prop[\"front\"], 2],\n",
    "        prop_weight,\n",
    "        \"kN\",\n",
    "    ),\n",
    "    (\n",
    "        \"self-weight\",\n",
    "        \"fixed leg foot moment\",\n",
    "        -cases[\"self-weight\"].reaction_moment[foot[\"front\"], 1],\n",
    "        m_weight,\n",
    "        \"kN m\",\n",
    "    ),\n",
    "    (\n",
    "        \"self-weight\",\n",
    "        \"girder moment at midspan\",\n",
    "        girder_moment[\"self-weight\"][n // 2],\n",
    "        middle_moment,\n",
    "        \"kN m\",\n",
    "    ),\n",
    ")\n",
    "for case, quantity, frame_value, hand, unit in rows:\n",
    "    print(\n",
    "        f\"{case:12s} {quantity:32s}: frame {frame_value / 1e3:9.4f} {unit:4s}, hand calculation {hand / 1e3:9.4f}, deviation {100 * (frame_value / hand - 1):+.1e} %\"\n",
    "    )\n",
    "hot = cases[\"hot day\"]\n",
    "girder_actions = np.array(\n",
    "    [\n",
    "        hot.members[f\"girder {side} {k}\"].local_end_actions\n",
    "        for side in sides\n",
    "        for k in range(n)\n",
    "    ]\n",
    ")\n",
    "print(\n",
    "    f\"hot day     girders: largest axial force {np.abs(girder_actions[:, [0, 6]]).max():.1e} N, largest vertical bending moment \"\n",
    "    f\"{np.abs(girder_actions[:, [4, 10]]).max():.1e} N m (both zero by the hinges)\"\n",
    ")\n",
    "print(\n",
    "    f\"hot day     girder end at the pendulum legs moves {1e3 * (hot.displacement[top['front', n], 0] - hot.displacement[top['front', 0], 0]):.4f} mm \"\n",
    "    f\"further than at the fixed legs (α ΔT L = {1e3 * alpha * warming * span:.4f} mm)\"\n",
    ")\n",
    "EI_weak, EI_end, EA_end, spread = (\n",
    "    steel * box[\"bending_inertia_z\"],\n",
    "    steel * end_box[\"bending_inertia_y\"],\n",
    "    steel * end_box[\"area\"],\n",
    "    alpha * warming * 2 * half_gauge,\n",
    ")\n",
    "sway, turn, thrust = np.linalg.solve(\n",
    "    [\n",
    "        [\n",
    "            -6 * EI_weak / height**2,\n",
    "            4 * EI_weak / height + 2 * EI_end / (2 * half_gauge),\n",
    "            0.0,\n",
    "        ],\n",
    "        [12 * EI_weak / height**3, -6 * EI_weak / height**2, -1.0],\n",
    "        [1.0, 0.0, 2 * half_gauge / (2 * EA_end)],\n",
    "    ],\n",
    "    [0.0, 0.0, spread / 2],\n",
    ")\n",
    "foot_moment = EI_weak / height * (2 * turn - 6 * sway / height)\n",
    "print(\n",
    "    f\"hot day     fixed-side end frame: foot shear {abs(hot.reaction_force[foot['front'], 1]) / 1e3:.4f} kN (slope-deflection {thrust / 1e3:.4f}), \"\n",
    "    f\"foot moment {abs(hot.reaction_moment[foot['front'], 0]) / 1e3:.4f} kN m ({abs(foot_moment) / 1e3:.4f}), \"\n",
    "    f\"leg top moves out {1e3 * abs(hot.displacement[top['front', 0], 1]):.5f} mm ({1e3 * sway:.5f}; free {1e3 * spread / 2:.5f})\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "Left the bending moment along a main girder for the three load cases with the hand-calculated\n",
    "values; middle the side view of the front portal under self-weight and payload, 50 times enlarged\n",
    "and drawn along the bent members: the girder end turns the fixed leg and the crane leans towards\n",
    "the pendulum legs; right the outward displacement of the legs on the hot day along their height:\n",
    "the clamped fixed leg bends as the slope-deflection solution says, the pendulum leg tilts freely."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "x = np.linspace(0.0, span, n + 1)\n",
    "chain = [\n",
    "    f\"fixed leg front\",\n",
    "    *(f\"girder front {k}\" for k in range(n)),\n",
    "    \"pendulum leg front\",\n",
    "]\n",
    "s = np.linspace(0.0, 1.0, 21)\n",
    "loaded = {\n",
    "    name: cases[\"self-weight\"]\n",
    "    .members[name]\n",
    "    .evaluate(s * cases[\"self-weight\"].members[name].length)[0]\n",
    "    + cases[\"payload\"]\n",
    "    .members[name]\n",
    "    .evaluate(s * cases[\"payload\"].members[name].length)[0]\n",
    "    for name in chain\n",
    "}\n",
    "path = np.vstack(\n",
    "    [\n",
    "        crane.joints[m.start] + np.outer(s, crane.joints[m.end] - crane.joints[m.start])\n",
    "        for m in crane.members\n",
    "        if m.name in chain\n",
    "    ]\n",
    ")\n",
    "bent = path + 50 * np.vstack([loaded[m.name] for m in crane.members if m.name in chain])\n",
    "leg = hot.members[\"fixed leg front\"].evaluate(s * height)[0]\n",
    "pendulum = hot.members[\"pendulum leg front\"].evaluate(s * height)[0]\n",
    "fig = plots.figure(1, 3, size=(19, 6))\n",
    "plots.line(\n",
    "    y={name: (x, values / 1e3) for name, values in girder_moment.items()},\n",
    "    markers=True,\n",
    "    points={\n",
    "        \"hand calculation, payload\": (\n",
    "            np.array([0.0, trolley]),\n",
    "            np.array([-m_payload, trolley_moment]) / 1e3,\n",
    "        ),\n",
    "        \"hand calculation, self-weight\": (\n",
    "            np.array([0.0, span / 2]),\n",
    "            np.array([-m_weight, middle_moment]) / 1e3,\n",
    "        ),\n",
    "    },\n",
    "    xlabel=\"position along the girder x [m]\",\n",
    "    ylabel=\"bending moment [kN m]\",\n",
    "    title=\"Main girder bending moment\",\n",
    "    legend=\"upper right\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.line(\n",
    "    y={\n",
    "        \"undeformed\": (path[:, 0], path[:, 2]),\n",
    "        \"self-weight + payload (×50)\": (bent[:, 0], bent[:, 2]),\n",
    "    },\n",
    "    styles={\"undeformed\": \"dashed\"},\n",
    "    xlimits=(-2, 14),\n",
    "    ylimits=(-1, 10),\n",
    "    xlabel=\"x [m]\",\n",
    "    ylabel=\"z [m]\",\n",
    "    title=\"Side view of the front portal\",\n",
    "    legend=\"lower center\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.line(\n",
    "    y={\n",
    "        \"fixed leg (frame)\": (-1e3 * leg[:, 1], s * height),\n",
    "        \"pendulum leg (frame)\": (-1e3 * pendulum[::-1, 1], s * height),\n",
    "    },\n",
    "    reference={\n",
    "        \"slope-deflection, fixed leg\": (\n",
    "            1e3 * (sway * (3 * s**2 - 2 * s**3) + height * turn * (s**3 - s**2)),\n",
    "            s * height,\n",
    "        )\n",
    "    },\n",
    "    xlabel=\"outward displacement across the rails [mm]\",\n",
    "    ylabel=\"height z [m]\",\n",
    "    title=\"Legs on the hot day\",\n",
    "    legend=\"upper left\",\n",
    "    ax=fig[2],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Move the trolley along the girders, or to one girder only, to find the governing load position;\n",
    "replace the moment hinges by rigid joints and see the thermal moments that the hinges avoid; or\n",
    "combine the cases with partial safety factors and check the stresses against the yield strength."
   ]
  }
 ]
}
