{
 "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": [
    "# Bending of a simply supported cross-ply laminate\n",
    "\n",
    "A square plate of side $a$ and thickness $h=a/4$ is made of three plies of equal thickness with\n",
    "the fibres along $x$, $y$ and $x$ ([0/90/0]). Each ply is a transversely isotropic composite,\n",
    "\n",
    "| $E_1$ | $E_2=E_3$ | $G_{12}=G_{13}$ | $G_{23}$ | $\\nu_{12}=\\nu_{13}=\\nu_{23}$ |\n",
    "|---|---|---|---|---|\n",
    "| $25E_2$ | 6.9 GPa | $0.5E_2$ | $0.2E_2$ | 0.25 |\n",
    "\n",
    "The plate is simply supported on its four edges and loaded on its upper face by\n",
    "$q=q_0\\sin(\\pi x/a)\\sin(\\pi y/a)$. One quarter is modelled:\n",
    "\n",
    "| face | condition |\n",
    "|---|---|\n",
    "| `x_min` (edge $x=0$) | $u_y=u_z=0$ |\n",
    "| `y_min` (edge $y=0$) | $u_x=u_z=0$ |\n",
    "| `x_max`, `y_max` (symmetry planes $x=a/2$, $y=a/2$) | $u_x=0$, $u_y=0$ |\n",
    "| `z_max` | pressure $q$ |\n",
    "\n",
    "The quarter plates come from the CAD files `composite_laminate_bending.step` ($h=a/4$) and\n",
    "`..._s10`, `_s20`, `_s50.step` (thinner plates for the comparison with plate theory); the\n",
    "script `composite_laminate_bending_geometry.py` builds them with the CAD objects of\n",
    "OneSimulation.\n",
    "\n",
    "**Reference.** J. N. Reddy, *Mechanics of Laminated Composite Plates and Shells*, 2nd ed., CRC\n",
    "Press (2004), Sec. 5.2: the Navier solution of classical laminated plate theory for this load\n",
    "is a single term, with the bending stiffnesses $D_{ij}$ of the laminate,\n",
    "\n",
    "$$w_{max}=\\frac{q_0a^4}{\\pi^4\\,\\big(D_{11}+2(D_{12}+2D_{66})+D_{22}\\big)},\\qquad\n",
    "  \\bar w=\\frac{100\\,E_2h^3}{q_0a^4}\\,w_{max}.$$\n",
    "\n",
    "Plate theory neglects transverse shear deformation, so it holds for thin plates; for $h=a/4$\n",
    "the 3D solution is expected to be far more flexible."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.meshing import StructuredHexahedralMesher\n",
    "from onesim.physics.solid_mechanics import SmallStrainSolid\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",
    "Quadratic hexahedra, 10 x 10 across the quarter and two per ply through the thickness, so that\n",
    "the ply interfaces lie on element faces."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "ratios = (4, 10, 20, 50)\n",
    "plates = {\n",
    "    s: load(f\"composite_laminate_bending{'' if s == 4 else f'_s{s}'}.step\")\n",
    "    for s in ratios\n",
    "}\n",
    "side = 2 * plates[4].size[0]\n",
    "meshes = {\n",
    "    s: StructuredHexahedralMesher(cells=(10, 10, 6)).generate(plate)\n",
    "    for s, plate in plates.items()\n",
    "}\n",
    "\n",
    "view = plots.Camera(elevation=30, azimuth=-55)\n",
    "faces = plates[4].boundaries\n",
    "fig = plots.figure(1, 2, size=(15, 5.5))\n",
    "plots.geometry(\n",
    "    plates[4],\n",
    "    selection={\n",
    "        \"simply supported\": faces.x_min | faces.y_min,\n",
    "        \"symmetry\": faces.x_max | faces.y_max,\n",
    "        \"loaded face\": faces.z_max,\n",
    "    },\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=\"Quarter of the plate with h = a/4\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    meshes[4],\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    camera=view,\n",
    "    title=f\"{meshes[4].cell_count} quadratic hexahedra\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "`SmallStrainSolid` with volume kinematics takes a selected elastic stiffness and a material\n",
    "orientation per point. The ply stiffness is the inverse of the orthotropic compliance in the\n",
    "order xx, yy, zz, xy, yz, xz; the middle third of the thickness is turned by 90° about $z$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "E2 = 6.9e9\n",
    "E1, G12, G23, nu, load_amplitude = 25 * E2, 0.5 * E2, 0.2 * E2, 0.25, 1.0e6\n",
    "compliance = np.diag([1 / E1, 1 / E2, 1 / E2, 1 / G12, 1 / G23, 1 / G12])\n",
    "compliance[0, 1] = compliance[1, 0] = compliance[0, 2] = compliance[2, 0] = -nu / E1\n",
    "compliance[1, 2] = compliance[2, 1] = -nu / E2\n",
    "ply = np.linalg.inv(compliance)\n",
    "turned = np.array([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])\n",
    "results = {}\n",
    "for s, plate in plates.items():\n",
    "    h, sides = side / s, plate.boundaries\n",
    "    results[s] = SmallStrainSolid(\n",
    "        youngs_modulus=E2, poisson_ratio=nu, kinematics=\"volume\"\n",
    "    ).solve(\n",
    "        mesh=meshes[s],\n",
    "        times=(0.0,),\n",
    "        order=2,\n",
    "        elastic_stiffness=ply,\n",
    "        material_orientation=lambda points, time, h=h: np.where(\n",
    "            (np.abs(np.asarray(points)[:, 2] - h / 2) < h / 6)[:, None, None],\n",
    "            turned,\n",
    "            np.eye(3),\n",
    "        ),\n",
    "        displacements=(\n",
    "            (sides.x_min, \"y\", 0.0),\n",
    "            (sides.x_min, \"z\", 0.0),\n",
    "            (sides.y_min, \"x\", 0.0),\n",
    "            (sides.y_min, \"z\", 0.0),\n",
    "            (sides.x_max, \"x\", 0.0),\n",
    "            (sides.y_max, \"y\", 0.0),\n",
    "        ),\n",
    "        tractions=(\n",
    "            (\n",
    "                sides.z_max,\n",
    "                lambda x, y, z: np.stack(\n",
    "                    (\n",
    "                        0 * x,\n",
    "                        0 * x,\n",
    "                        -load_amplitude\n",
    "                        * np.sin(np.pi * x / side)\n",
    "                        * np.sin(np.pi * y / side),\n",
    "                    ),\n",
    "                    axis=-1,\n",
    "                ),\n",
    "            ),\n",
    "        ),\n",
    "    )"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "The normalised deflection at the centre of the mid-plane for the four thickness ratios against\n",
    "classical laminated plate theory. With equal ply thicknesses the bending stiffnesses are\n",
    "$D_{11}=h^3(26Q_{11}+Q_{22})/324$, $D_{22}=h^3(26Q_{22}+Q_{11})/324$, $D_{12}=Q_{12}h^3/12$ and\n",
    "$D_{66}=G_{12}h^3/12$, with the reduced ply stiffnesses $Q_{ij}$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "q11, q22, q12 = (E1, E2, nu * E2) / np.array(1 - nu**2 * E2 / E1)\n",
    "plate_theory = (\n",
    "    100\n",
    "    * E2\n",
    "    / (\n",
    "        np.pi**4\n",
    "        * ((26 * q11 + q22) / 324 + 2 * (q12 + 2 * G12) / 12 + (26 * q22 + q11) / 324)\n",
    "    )\n",
    ")\n",
    "deflection = {\n",
    "    s: -results[s].evaluate_displacement(\n",
    "        np.array([[side / 2, side / 2, side / s / 2]])\n",
    "    )[0, 2]\n",
    "    * 100\n",
    "    * E2\n",
    "    * (side / s) ** 3\n",
    "    / (load_amplitude * side**4)\n",
    "    for s in ratios\n",
    "}\n",
    "\n",
    "print(f\"classical laminated plate theory: w̄ = {plate_theory:.4f} for every thickness\")\n",
    "print(\"a/h    3D solution w̄   ratio to plate theory\")\n",
    "for s, value in deflection.items():\n",
    "    print(f\"{s:3d} {value:15.4f} {value / plate_theory:21.3f}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The deflection of the thick plate, the normalised deflection over the slenderness against plate\n",
    "theory, and the in-plane stress $\\sigma_{xx}$ at the integration points of the centre column of\n",
    "cells for the thick and the thinnest plate: the thick plate is no longer linear in each ply."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "centre = {\n",
    "    s: (results[s].quadrature_coordinates[:, 0] > side / 2 - side / 20)\n",
    "    & (results[s].quadrature_coordinates[:, 1] > side / 2 - side / 20)\n",
    "    for s in (4, 50)\n",
    "}\n",
    "profiles = {\n",
    "    f\"a/h = {s}\": (\n",
    "        results[s].stress[0][centre[s], 0, 0]\n",
    "        * (side / s) ** 2\n",
    "        / (load_amplitude * side**2),\n",
    "        results[s].quadrature_coordinates[centre[s], 2] / (side / s),\n",
    "    )\n",
    "    for s in (4, 50)\n",
    "}\n",
    "fig = plots.figure(1, 3, size=(19, 5.5))\n",
    "plots.mesh(\n",
    "    meshes[4],\n",
    "    cell_values=-1e6\n",
    "    * results[4].evaluate_displacement(\n",
    "        np.asarray(meshes[4].coordinates)[np.asarray(meshes[4].cells)].mean(axis=1)\n",
    "    )[:, 2],\n",
    "    camera=view,\n",
    "    label=\"deflection [µm]\",\n",
    "    title=\"Thick plate a/h = 4: deflection per cell\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.line(\n",
    "    list(ratios),\n",
    "    {\"3D solution\": list(deflection.values())},\n",
    "    markers=True,\n",
    "    hlines={\"classical plate theory\": plate_theory},\n",
    "    scale=\"logx\",\n",
    "    xlabel=\"slenderness a/h\",\n",
    "    ylabel=\"normalised centre deflection w̄\",\n",
    "    title=\"From thick to thin laminate\",\n",
    "    legend=\"upper right\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.line(\n",
    "    points=profiles,\n",
    "    hlines=[1 / 3, 2 / 3],\n",
    "    xlabel=\"σ_xx h² / (q0 a²) at the centre\",\n",
    "    ylabel=\"height z / h\",\n",
    "    title=\"In-plane stress through the plies\",\n",
    "    legend=\"lower left\",\n",
    "    ax=fig[2],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Change the stacking to [0/90/90/0] or an angle ply by changing the orientation per ply, or\n",
    "load the plate with a uniform pressure: the Navier series then needs more terms, the 3D model\n",
    "does not change."
   ]
  }
 ]
}
