{ "cells": [ { "cell_type": "markdown", "id": "026a49a0-a0a2-41c5-95ec-0c6d8a7077d7", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": 3, "height": 6, "hidden": true, "locked": true, "row": 358, "width": 9 } } } }, "tags": [] }, "source": [ "

Simple beam modeling

\n", "
\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "b132a882-6f77-46ae-a371-1acaa75d8dda", "metadata": {}, "outputs": [], "source": [ "try:\n", " import piplite\n", " await piplite.install(['ipywidgets==7.7.0', 'ipyflex', 'plotly', 'cosapp'])\n", "except:\n", " pass" ] }, { "cell_type": "code", "execution_count": 1, "id": "01ddcc7d-6223-4296-9884-3a9bcb19bd7a", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "from cosapp.ports import Port\n", "from cosapp.systems import System\n", "import numpy as np\n", "\n", "class GeometryPort(Port):\n", " def setup(self):\n", " self.add_variable(\"visible\", True, desc=\"Should this geometry be shown?\")\n", " self.add_variable(\"shape\", None, desc=\"Geometrical object\")\n", "class BeamGeo(System):\n", " \n", " def setup(self):\n", " self.add_inward(\"file\",\"path\")\n", " self.add_inward(\"mesh_size\",100)\n", " self.add_inward('width', 0.5, unit = 'm')\n", " self.add_inward('height',0.5, unit = 'm')\n", " self.add_inward('length', 5., unit = 'm')\n", " self.add_outward('I', desc='Second area moment')\n", " self.add_outward('grid', np.zeros(1))\n", " self.add_outward(\"section\", [])\n", " self.add_output(GeometryPort, 'geom')\n", " def compute(self):\n", " self.I = self.width*self.height**3/12.\n", " self.grid = np.array([i*self.length/self.mesh_size for i in range(0,int(self.mesh_size)+1)])\n", " section = [[],[]]\n", " for i in range(0,100):\n", " section[0].append((- 0.5 + i/100.)*self.width)\n", " section[1].append(-0.5*self.height)\n", " for i in range(0,100):\n", " section[0].append(0.5*self.width)\n", " section[1].append((- 0.5 + i/100.)*self.height) \n", " for i in range(0,100):\n", " section[0].append((0.5 - i/100.)*self.width)\n", " section[1].append(0.5*self.height) \n", " for i in range(0,100):\n", " section[0].append( -0.5*self.width)\n", " section[1].append((0.5 - i/100.)*self.height) \n", " self.section = section\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "5cc948e4-115b-459b-80fc-b24d8546b426", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "class BeamMeca(System):\n", " def setup(self):\n", " \n", " self.add_inward('E', 270, desc = \"Young modulus\", valid_range = [100,150],limits = [50,200])\n", " self.add_inward(\"grid\", np.zeros(101), valid_range = [2,4],limits = [-np.inf,10] )\n", " self.add_inward(\"I\", 1., desc='Second area moment' )\n", " self.add_inward(\"force\", -1., desc='Force value', unit = 'N' )\n", " self.add_inward(\"position\", 0.5, desc=\"force relative position\",limits= [0.2,0.3], valid_range = [0.1,0.8] )\n", " self.add_outward(\"M\", np.zeros(1), desc = \"Bending moments\" )\n", " self.add_outward(\"Q\", np.zeros(1), desc = \"Shear forces\" )\n", " self.add_outward(\"W\", np.zeros(1), desc = \"Deflections\" )\n", " self.add_outward(\"maxW\", 0, desc = \"Max deflections\" )\n", " self.add_outward(\"f_position\", 0.5, desc=\"force position\")\n", " self.add_outward(\"maxW_loc\", 0, desc = \"Max deflections location\" )\n", " def compute(self):\n", " if self.position > 1.:\n", " position = 0.99\n", " elif self.position < 0:\n", " position = 0.\n", " else:\n", " position = self.position\n", " mesh_size = len(self.grid)\n", " L = self.grid[-1]\n", " b = (1.- position)*L\n", " a = position*L\n", " M =[]\n", " Q = []\n", " W = []\n", " for i in range(0, int(mesh_size*position)):\n", " x = self.grid[i]\n", " M.append(self.force*b*x/L)\n", " Q.append(self.force*b/L)\n", " W.append(self.force*b*x*(L**2 - b**2 - x**2)/(6*L*self.E*self.I)) \n", " for j in range(int(mesh_size*position), mesh_size):\n", " x = self.grid[j]\n", " M.append(self.force*a*(L - x)/L)\n", " Q.append(self.force*(b/L-1))\n", " W.append(self.force*b*x*(L**2 - b**2 - x**2)/(6*L*self.E*self.I)+ self.force*(x-a)**3/(6*self.E*self.I))\n", " self.M = np.array(M)\n", " self.Q = np.array(Q) \n", " self.W = np.array(W)\n", " self.maxW_loc = int(np.argmax(np.absolute(self.W)))-1\n", " self.maxW = self.W[self.maxW_loc]\n", " self.f_position = mesh_size*position" ] }, { "cell_type": "code", "execution_count": 3, "id": "3f5f7ee7-819e-4690-b6ec-34f57ce9a3fb", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "class Main(System):\n", " def setup(self):\n", " self.add_inward(\"maintest\",0)\n", " self.add_child(BeamGeo(\"geo\"))\n", " self.add_child(BeamMeca(\"meca\"))\n", " self.connect(self.geo.outwards, self.meca.inwards, [\"I\", \"grid\"])" ] }, { "cell_type": "code", "execution_count": 4, "id": "20ec9af0-99e4-40dc-8ccd-cdade154b36b", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "main = Main(\"main\")\n", "main.run_drivers()" ] }, { "cell_type": "code", "execution_count": 5, "id": "9cad584d-3a0e-4278-95fd-90b216d458a7", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "import ipywidgets\n", "import ipyflex\n", "import plotly.graph_objects as go" ] }, { "cell_type": "code", "execution_count": 6, "id": "d2247985-b5fb-474c-a31c-8cdd8c7178bd", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "widget_M = go.FigureWidget(go.Bar(\n", " y=main.meca.M,\n", " # marker_color='rgba(91, 91, 91, 0.73)',\n", " name='M',\n", "))\n", "widget_Q = go.FigureWidget(go.Scatter(\n", " y=main.meca.Q ,\n", " # marker_color='rgba(91, 91, 91, 0.73)',\n", " name='M',\n", "))\n", "widget_W = go.FigureWidget(go.Bar(\n", " y=main.meca.W,\n", " # marker_color='rgba(91, 91, 91, 0.73)',\n", " name='M',\n", "), layout_yaxis_range=[-10,0])" ] }, { "cell_type": "code", "execution_count": 6, "id": "ce89045a-45c3-4409-b1d5-75896ace81db", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "id": "87ad8d18-1628-4a58-89b8-cdde8bae95a1", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "E_slider = ipywidgets.FloatSlider( value=150,\n", " min=50,\n", " max=250,description='Young modulus:',\n", " continuous_update=False,)\n", "def on_E_change(change):\n", " main.meca.E = change['new']\n", " main.run_drivers()\n", " widget_M.data[0].y = main.meca.M\n", " widget_Q.data[0].y = main.meca.Q\n", " widget_W.data[0].y = main.meca.W\n", "E_slider.observe(on_E_change, names='value')\n", "\n", "position_slider = ipywidgets.FloatSlider( value=0.5,\n", " min=0.1,\n", " max=0.8,\n", " continuous_update=False,\n", " description='Force position',)\n", "def on_position_change(change):\n", " main.meca.position = change['new']\n", " main.run_drivers()\n", " widget_M.data[0].y = main.meca.M\n", " widget_Q.data[0].y = main.meca.Q\n", " widget_W.data[0].y = main.meca.W\n", "position_slider.observe(on_position_change, names='value')\n", "control = ipywidgets.VBox([E_slider, position_slider])\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "ad86a098-ef40-48eb-b097-27b529ead5be", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [ "widget_dict = {'Moment': widget_M, 'Deflection': widget_W, 'Shear forces':widget_Q, 'Control': control}\n", "w = ipyflex.FlexLayout(widget_dict, \n", " template='beam.json',\n", " editable=False,\n", " header={'title':'Simply supported beam', 'buttons':[]},\n", " )" ] }, { "cell_type": "code", "execution_count": 9, "id": "8c9fd04f-04bc-4cb8-9238-5d343f9be7cf", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": 0, "height": 19, "hidden": false, "locked": true, "row": 0, "width": 12 } } } }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f346a26e591b4efe8ec5db4d26f7d282", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FlexLayout(children={'Moment': FigureWidget({\n", " 'data': [{'name': 'M',\n", " 'type': 'bar',\n", " …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "w" ] }, { "cell_type": "code", "execution_count": 9, "id": "8003d3d4-6513-4480-a379-4e17424118bc", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } }, "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "0f248175-273b-4031-8d59-6536913ac5fb", "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "views": { "grid_default": { "col": null, "height": 2, "hidden": true, "locked": true, "row": null, "width": 2 } } } } }, "outputs": [], "source": [] } ], "metadata": { "extensions": { "jupyter_dashboards": { "activeView": "grid_default", "version": 1, "views": { "grid_default": { "cellMargin": 2, "defaultCellHeight": 40, "maxColumns": 12, "name": "grid", "type": "grid" } } } }, "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }