{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a994e0c7-d5ad-44c6-b623-0bf8f614d4b1",
   "metadata": {},
   "source": [
    "# Wall bracket: 200 N static load\n",
    "\n",
    "This check uses the current 120 mm-wide S235 bracket. The CAD study distributes 200 N downward over the flat top of the flange and fixes the walls of both slots. It does not model wall anchors or bolt contact.\n",
    "\n",
    "![CAD geometry before the load study](Bracket%20Geometry.png)\n",
    "\n",
    "The hand calculation treats just the straight portion of the flange as a uniformly loaded cantilever. It checks the scale of the finite-element results, but does not include flex of the bend or back plate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "367045a8-cef5-4a3c-ac33-92e6404a5083",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nominal flange root stress: 18.9 MPa\n",
      "Flange-only free-edge deflection: 0.0173 mm\n"
     ]
    }
   ],
   "source": [
    "force, width, thickness, span, modulus = 200, 120, 3, 34, 210_000  # N, mm, MPa\n",
    "second_moment = width * thickness**3 / 12\n",
    "section_modulus = second_moment / (thickness / 2)\n",
    "print(f\"Nominal flange root stress: {force * span / (2 * section_modulus):.1f} MPa\")\n",
    "print(f\"Flange-only free-edge deflection: {force * span**3 / (8 * modulus * second_moment):.4f} mm\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85f65aac-9eef-423e-9b11-6832a824abd2",
   "metadata": {},
   "source": [
    "The estimate is a baseline for the straight flange; it is not a bound on local stress at the fixed slot edges."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "057d1118-f729-41c6-a715-7588e09fd2a4",
   "metadata": {},
   "source": [
    "## Finite-element results\n",
    "\n",
    "The table reads the CalculiX output and compares mesh sizes. The stress reported away from the slots excludes points within 3 mm (one plate thickness) of either slot wall, where perfectly fixing the surface can create a mesh-sensitive stress peak."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ec8ac550-4138-4ba3-8500-61637fd34746",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Nodes</th>\n",
       "      <th>Peak stress (MPa)</th>\n",
       "      <th>Stress away from slots (MPa)</th>\n",
       "      <th>Peak at bend (MPa)</th>\n",
       "      <th>Max. displacement (mm)</th>\n",
       "      <th>Max. downward movement (mm)</th>\n",
       "      <th>Peak position (X, Y, Z in mm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2.0 mm</th>\n",
       "      <td>55700</td>\n",
       "      <td>50.511392</td>\n",
       "      <td>37.466475</td>\n",
       "      <td>28.190908</td>\n",
       "      <td>0.142621</td>\n",
       "      <td>0.128956</td>\n",
       "      <td>(3.0, -84.5541, 36.8124)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.2 mm</th>\n",
       "      <td>179717</td>\n",
       "      <td>55.422856</td>\n",
       "      <td>37.597605</td>\n",
       "      <td>28.732103</td>\n",
       "      <td>0.144107</td>\n",
       "      <td>0.130458</td>\n",
       "      <td>(3.0, -84.273, 36.7501)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Nodes Peak stress (MPa) Stress away from slots (MPa)  \\\n",
       "2.0 mm   55700         50.511392                    37.466475   \n",
       "1.2 mm  179717         55.422856                    37.597605   \n",
       "\n",
       "       Peak at bend (MPa) Max. displacement (mm) Max. downward movement (mm)  \\\n",
       "2.0 mm          28.190908               0.142621                    0.128956   \n",
       "1.2 mm          28.732103               0.144107                    0.130458   \n",
       "\n",
       "       Peak position (X, Y, Z in mm)  \n",
       "2.0 mm      (3.0, -84.5541, 36.8124)  \n",
       "1.2 mm       (3.0, -84.273, 36.7501)  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "import gzip\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from IPython.display import display\n",
    "\n",
    "def summarize(file):\n",
    "    nodes, stresses, displacements = {}, {}, {}\n",
    "    state = None\n",
    "    with gzip.open(file, 'rt') as frd:\n",
    "        for line in frd:\n",
    "            if line.startswith('    2C'):\n",
    "                state = nodes\n",
    "            elif line.startswith(' -4  DISP'):\n",
    "                state = displacements\n",
    "            elif line.startswith(' -4  STRESS'):\n",
    "                state = stresses\n",
    "            elif line.startswith(' -3'):\n",
    "                state = None\n",
    "            elif state is not None and line.startswith(' -1'):\n",
    "                state[int(line[3:13])] = [float(line[i:i+12]) for i in range(13, len(line.rstrip()), 12)]\n",
    "    ids = list(stresses)\n",
    "    x, y, z = np.array([nodes[i] for i in ids]).T\n",
    "    xx, yy, zz, xy, yz, zx = np.array([stresses[i] for i in ids]).T\n",
    "    mises = np.sqrt(((xx-yy)**2 + (yy-zz)**2 + (zz-xx)**2)/2 + 3*(xy**2 + yz**2 + zx**2))\n",
    "    # The slot is the outline of a capsule about a 7.4 mm line segment in the YZ plane.\n",
    "    slot_distances = [np.abs(np.hypot(np.maximum(np.abs(y-center)-3.7, 0), z-40)-3.3)\n",
    "                      for center in (-40, -80)]\n",
    "    away = np.minimum(*slot_distances) >= 3\n",
    "    k = np.argmax(mises)\n",
    "    u = np.array(list(displacements.values()))\n",
    "    return dict(nodes=len(nodes), peak=mises[k], away=mises[away].max(),\n",
    "                bend=mises[(x<=6.1)&(z<=6.1)].max(),\n",
    "                displacement=np.linalg.norm(u,axis=1).max(), down=-u[:,2].min(),\n",
    "                peak_xyz=(x[k],y[k],z[k]))\n",
    "\n",
    "files = {'2.0 mm': Path('Solver Data/Coarse Mesh.frd.gz'),\n",
    "         '1.2 mm': Path('Solver Data/Fine Mesh.frd.gz')}\n",
    "results = pd.DataFrame({label: summarize(file) for label,file in files.items() if file.exists()}).T\n",
    "display(results.rename(columns={'nodes':'Nodes', 'peak':'Peak stress (MPa)',\n",
    "    'away':'Stress away from slots (MPa)', 'bend':'Peak at bend (MPa)',\n",
    "    'displacement':'Max. displacement (mm)', 'down':'Max. downward movement (mm)',\n",
    "    'peak_xyz':'Peak position (X, Y, Z in mm)'}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3afdc405-2a1e-49ff-82be-c6c469fc838c",
   "metadata": {},
   "source": [
    "Von Mises stress is the scalar compared with the 235 MPa minimum yield strength for 3 mm S235. [Tata Steel lists this minimum for S235 through 16 mm.](https://products.tatasteelnederland.com/products/engineering/hot-rolled/structural-steel) Displacements describe the bracket with perfectly rigid supports, not the motion of a bolted assembly."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90618e8-ac3c-4a55-b698-c4e3c6df682e",
   "metadata": {},
   "source": [
    "The finer mesh gives 55.4 MPa at a fixed slot edge. The ratio of yield strength to that peak stress is 4.24. The peak rises by 9.7% as the mesh is refined, while the maximum at least 3 mm from the slots changes by only 0.35%, to 37.6 MPa. The predicted downward movement is 0.130 mm. The bend reaches 28.7 MPa, above the straight-flange estimate of 18.9 MPa; the simple estimate omits bend geometry and back-plate flexibility. The result indicates no yielding under the modeled load and restraints, not a rating for the mounted shelf."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffb6c9eb-120b-4859-aac0-80211d4e5bdc",
   "metadata": {},
   "source": [
    "The stress plot shows the two hot spots at the lower edges of the restrained slots.\n",
    "\n",
    "![Von Mises stress on the bracket under the 200 N load](Stress%20Plot.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  },
  "vicena": {
   "changed_at": 1796327360568,
   "changed_cells": [
    "ffb6c9eb-120b-4859-aac0-80211d4e5bdc"
   ]
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
