{
 "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": [
    "# Natural frequency of a beam clamped at both ends\n",
    "\n",
    "A prismatic beam (length $L$, square cross-section $b\\times h$) is fully clamped on\n",
    "**both** end faces and traction-free on every other face. It comes from the CAD file\n",
    "`fixed_fixed_beam_frequency.step` with the face names `x_min` to `z_max`; the script\n",
    "`fixed_fixed_beam_frequency_geometry.py` builds it with the CAD objects of OneSimulation.\n",
    "OneSimulation assembles the 3D linear-elastic stiffness and mass operators on quadratic\n",
    "hexahedra and solves the generalised eigenproblem\n",
    "$K\\boldsymbol\\phi=\\omega^2 M\\boldsymbol\\phi$ for the natural modes and frequencies.\n",
    "\n",
    "**Reference.** Euler-Bernoulli beam theory gives the bending natural frequencies\n",
    "\n",
    "$$f_n=\\frac{\\beta_n^2}{2\\pi}\\sqrt{\\frac{E I}{\\rho A L^4}},\\qquad\n",
    "  I=\\frac{b h^3}{12},\\quad A=b h,$$\n",
    "\n",
    "with $\\beta_1=4.730041$ for the first clamped-clamped mode (the first root of\n",
    "$\\cos\\beta\\cosh\\beta=1$). Beam theory neglects shear deformation and rotary inertia\n",
    "and is therefore an approximate target for a three-dimensional solid of finite\n",
    "slenderness; the deviation grows for stubby beams."
   ]
  },
  {
   "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 CadHexahedralMesher\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",
    "    FixedDisplacement,\n",
    "    LinearElasticity,\n",
    "    TractionFree,\n",
    "    structural_modes,\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"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "beam = load(\"fixed_fixed_beam_frequency.step\")\n",
    "faces = beam.boundaries\n",
    "length, side, _ = beam.size\n",
    "mesh = CadHexahedralMesher(cells=(48, 3, 3)).generate(beam)\n",
    "\n",
    "fig = plots.figure(1, 2, size=(14, 4.5))\n",
    "plots.geometry(\n",
    "    beam,\n",
    "    selection={\"clamped\": faces.x_min | faces.x_max},\n",
    "    labels=False,\n",
    "    title=\"Beam with its clamped faces\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh, groups=(\"all\",), legend=False, title=f\"{mesh.cell_count} hexahedra\", ax=fig[1]\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "youngs_modulus, density = 210e9, 7850.0\n",
    "steel = IsotropicSolid(\n",
    "    density=density,\n",
    "    specific_heat_capacity=460.0,\n",
    "    thermal_conductivity=50.0,\n",
    "    youngs_modulus=youngs_modulus,\n",
    "    poisson_ratio=0.30,\n",
    "    thermal_expansion_coefficient=12e-6,\n",
    ")\n",
    "model = LinearElasticity(domain=beam.volume, material=steel)\n",
    "problem = Problem(\n",
    "    name=\"Doubly clamped beam natural modes\",\n",
    "    geometry=beam,\n",
    "    physics=(model,),\n",
    "    boundary_conditions=(\n",
    "        FixedDisplacement(\n",
    "            boundary=faces.x_min | faces.x_max, physics=model, value=(0.0, 0.0, 0.0)\n",
    "        ),\n",
    "        TractionFree(boundary=faces.all - faces.x_min - faces.x_max, physics=model),\n",
    "    ),\n",
    ")\n",
    "numerics = Numerics(\n",
    "    spatial=FiniteElement(element=Lagrange(order=2)), linear_solver=SparseDirect()\n",
    ")\n",
    "result = Simulation(problem=problem, mesh=mesh, numerics=numerics).solve_stationary()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modal analysis and comparison with beam theory\n",
    "\n",
    "The square cross-section makes bending about the two transverse axes degenerate, so\n",
    "the first two computed frequencies form one bending pair."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "modes = structural_modes(result, model, mode_count=4)\n",
    "beam_theory = (\n",
    "    4.730041**2\n",
    "    / (2 * np.pi)\n",
    "    * np.sqrt(youngs_modulus * side**4 / 12 / (density * side**2 * length**4))\n",
    ")\n",
    "\n",
    "print(\n",
    "    f\"fundamental frequency: simulation {modes.frequencies[0]:.2f} Hz, Euler-Bernoulli {beam_theory:.2f} Hz, \"\n",
    "    f\"deviation {100 * (modes.frequencies[0] / beam_theory - 1):+.2f} %\"\n",
    ")\n",
    "print(\"first four frequencies [Hz]:\", [round(f, 2) for f in modes.frequencies])"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "plots.bars(\n",
    "    [\"Mode 1\\n(bending)\", \"Mode 2\\n(bending)\", \"Mode 3\", \"Mode 4\"],\n",
    "    {\"simulation\": modes.frequencies},\n",
    "    reference={\"Euler-Bernoulli\": [beam_theory, beam_theory, np.nan, np.nan]},\n",
    "    ylabel=\"natural frequency [Hz]\",\n",
    "    title=\"Doubly clamped beam: natural frequencies\",\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Change the slenderness in `fixed_fixed_beam_frequency_geometry.py` and write the STEP file\n",
    "again; for stubby beams shear and rotary inertia grow and beam theory increasingly\n",
    "overestimates the clamped-clamped fundamental."
   ]
  }
 ]
}
