{
 "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": [
    "# Scordelis-Lo roof\n",
    "\n",
    "The roof is a segment of a circular cylinder: radius $R=25$ m, length $L=50$ m, spanning\n",
    "$40°$ on either side of the crown, thickness $t=0.25$ m. It rests at its two curved ends on rigid\n",
    "diaphragms that hold it in their plane but let it move along the axis and rotate freely; the\n",
    "two straight edges are free. The load is the dead weight, 90 per unit area of the mid-surface,\n",
    "acting downwards. The material has $E=4.32\\cdot10^8$ and $\\nu=0$ (consistent units).\n",
    "\n",
    "| boundary | condition |\n",
    "|---|---|\n",
    "| curved ends $x=\\pm L/2$ (`diaphragm`) | $u_y=u_z=0$ |\n",
    "| straight edges (`free_edge`) | free |\n",
    "| one point on the crown at mid-length | $u_x=0$ (removes the rigid motion along the axis) |\n",
    "\n",
    "The roof comes from the CAD file `scordelis_lo_roof_shell.step` as a thin solid with the named\n",
    "faces `diaphragm` and `free_edge`; the script `scordelis_lo_roof_shell_geometry.py` builds it\n",
    "with the CAD objects of OneSimulation. `midsurface` turns the thin solid into the sheet halfway\n",
    "through its wall, which carries the thickness for the shell model.\n",
    "\n",
    "**Reference.** A. C. Scordelis and K. S. Lo, *Computer analysis of cylindrical shells*,\n",
    "J. American Concrete Institute 61 (1964) 539; R. H. MacNeal and R. L. Harder, *A proposed\n",
    "standard set of problems to test finite element accuracy*, Finite Elements in Analysis and\n",
    "Design 1 (1985) 3: the vertical displacement at the middle of the free edge is 0.3024 (deep\n",
    "shell theory)."
   ]
  },
  {
   "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.physics.solid_mechanics import ElasticShell, ShellSurface\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",
    "The mid-surface is meshed with paved quadrilaterals of three sizes."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "roof = load(\"scordelis_lo_roof_shell.step\")\n",
    "faces = roof.boundaries\n",
    "sheet = roof.midsurface()\n",
    "radius, length, half_angle = 25.0, 50.0, np.radians(40.0)\n",
    "sizes = [4.0, 2.0, 1.0]\n",
    "meshes = [SheetMesher(target_size=size, paved=True).generate(sheet) for size in sizes]\n",
    "\n",
    "view = plots.Camera(elevation=30, azimuth=-60)\n",
    "fig = plots.figure(1, 2, size=(15, 5.5))\n",
    "plots.geometry(\n",
    "    roof,\n",
    "    selection={\"diaphragm\": faces.diaphragm, \"free edge\": faces.free_edge},\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=f\"Roof, {sheet.thickness:.2f} m thick, with its named faces\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    meshes[1],\n",
    "    camera=view,\n",
    "    title=f\"Mid-surface mesh: {len(meshes[1].quadrilaterals)} quadrilaterals\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "`ElasticShell` is a shear-deformable shell with five degrees of freedom per node, three\n",
    "displacements and two rotations about the local tangent axes. The supports are given node by\n",
    "node: the nodes on the two diaphragm ends and the node on the crown at mid-length."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "shell = ElasticShell(\n",
    "    youngs_modulus=4.32e8, poisson_ratio=0.0, thickness=sheet.thickness\n",
    ")\n",
    "results = []\n",
    "for mesh in meshes:\n",
    "    nodes = np.asarray(mesh.coordinates)\n",
    "    ends = np.flatnonzero(np.isclose(np.abs(nodes[:, 0]), length / 2, atol=1e-6))\n",
    "    crown = int(np.argmin(np.linalg.norm(nodes - (0.0, 0.0, radius), axis=1)))\n",
    "    results.append(\n",
    "        shell.solve(\n",
    "            surface=ShellSurface(\n",
    "                coordinates=nodes, quadrilaterals=np.asarray(mesh.quadrilaterals)\n",
    "            ),\n",
    "            supports=[(int(node), axis, 0.0) for node in ends for axis in (\"y\", \"z\")]\n",
    "            + [(crown, \"x\", 0.0)],\n",
    "            surface_load=(0.0, 0.0, -90.0),\n",
    "        )\n",
    "    )"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "The vertical displacement at the middle of the free edge, the point\n",
    "$(0,\\ R\\sin40°,\\ R\\cos40°)$, against the reference value, and the balance of the reactions\n",
    "with the weight of the roof."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "reference = 0.3024\n",
    "edge = (0.0, radius * np.sin(half_angle), radius * np.cos(half_angle))\n",
    "sags = [\n",
    "    -result.displacement[\n",
    "        np.argmin(np.linalg.norm(np.asarray(mesh.coordinates) - edge, axis=1)), 2\n",
    "    ]\n",
    "    for mesh, result in zip(meshes, results)\n",
    "]\n",
    "print(\n",
    "    \"cell size   quadrilaterals   nodes   displacement at the free edge   deviation from 0.3024\"\n",
    ")\n",
    "for size, mesh, sag in zip(sizes, meshes, sags):\n",
    "    print(\n",
    "        f\"{size:9.1f} {len(mesh.quadrilaterals):16d} {len(mesh.coordinates):7d} {sag:31.4f} {100 * (sag / reference - 1):+19.2f} %\"\n",
    "    )\n",
    "print(\n",
    "    f\"vertical reactions {results[-1].reaction_force[:, 2].sum():.1f}, weight of the roof {90.0 * results[-1].area:.1f}\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The vertical displacement on the deformed roof (magnified 10 times), the sag along the free\n",
    "edge and near the crown, and the mesh study at the reference point."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "mesh, result = meshes[-1], results[-1]\n",
    "nodes = np.asarray(mesh.coordinates)\n",
    "on_edge = np.flatnonzero(np.isclose(nodes[:, 1], edge[1], atol=1e-3))\n",
    "on_crown = np.flatnonzero(np.abs(nodes[:, 1]) < 0.5)\n",
    "fig = plots.figure(1, 3, size=(19, 5.5))\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    values=result.displacement[:, 2],\n",
    "    displacement=result.displacement,\n",
    "    scale=10.0,\n",
    "    camera=view,\n",
    "    label=\"u_z\",\n",
    "    title=\"Vertical displacement, magnified 10 times\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.line(\n",
    "    points={\n",
    "        \"free edge\": (nodes[on_edge, 0], result.displacement[on_edge, 2]),\n",
    "        \"near the crown\": (nodes[on_crown, 0], result.displacement[on_crown, 2]),\n",
    "    },\n",
    "    hlines={\"reference at the middle of the free edge\": -reference},\n",
    "    xlabel=\"x along the roof\",\n",
    "    ylabel=\"u_z\",\n",
    "    title=\"Sag along the roof\",\n",
    "    legend=\"center\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.line(\n",
    "    [len(mesh.coordinates) for mesh in meshes],\n",
    "    {\"simulation\": sags},\n",
    "    markers=True,\n",
    "    hlines={\"reference 0.3024\": reference},\n",
    "    scale=\"logx\",\n",
    "    ylimits=(0.28, 0.32),\n",
    "    xlabel=\"nodes\",\n",
    "    ylabel=\"vertical displacement at the free edge\",\n",
    "    title=\"Mesh study\",\n",
    "    legend=\"lower right\",\n",
    "    ax=fig[2],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Change the opening angle or the thickness in `scordelis_lo_roof_shell_geometry.py` and write\n",
    "the STEP file again: a thinner roof makes locking visible in less robust shell elements. Add a\n",
    "layered `layer_stiffness` for a composite roof, or replace the dead weight by a snow load on\n",
    "the projected area."
   ]
  }
 ]
}
