{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "nteract": { "transient": { "deleting": false } } }, "source": [ "# OPT python exercise 2" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "nteract": { "transient": { "deleting": false } } }, "source": [ "In this exercise, we will look at a minimization problem linked to the deflection of a uniformly-loaded simple beam. The beam is made of a cylinder of length $L$ with an inner diameter $d$ and thickness $t$.\n", "\n", "
\n", "\n", "\n", "The deflection of this beam is expressed as:\n", "\n", "$$ u = \\frac{5qL^4}{284EI} $$\n", "\n", "where $q$ is the load applied to the beam, E its Young's modulus and I is the area moment of inertia.\n", "\n", "In this minimization problem, we want to minimize the mass of the beam while ensuring that the deflection $u$ is lower than 250 mm.\n", "\n", "The variables we want to optimize are therefore the diameter $d$ and the thickness $t$ of the beam. The thickness of the beam should be restricted to $2< t < 10$ and the diameter should be restricted to $30 < d < 60$\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "execution": { "iopub.execute_input": "2021-04-05T16:49:51.485Z", "iopub.status.busy": "2021-04-05T16:49:51.478Z", "iopub.status.idle": "2021-04-05T16:49:51.885Z", "shell.execute_reply": "2021-04-05T16:49:51.890Z" }, "jupyter": { "outputs_hidden": false, "source_hidden": false }, "nteract": { "transient": { "deleting": false } } }, "outputs": [], "source": [ "# We import here the python modules we need for this example\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from scipy import optimize" ] }, { "cell_type": "markdown", "metadata": { "nteract": { "transient": { "deleting": false } } }, "source": [ "We define three python functions to compute:\n", "\n", "1. the are moment of inertia $I$, as function of the diameter $d$ and thickness $t$ of the cylinder (function I(t,d))\n", "\n", "2. the mid-point deflection $u$ as function of the applied load $q$, the length of the beam $L$, its Young's modulus $E$ and the area moment of inertia $I$ (function mid_point(q,L,E,I)).\n", "\n", "3. the mass of the beam given its diameter $d$, thickness $t$ and density $\\rho$ (function mass(rho,L,d,t))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true, "execution": { "iopub.execute_input": "2021-04-05T16:49:54.126Z", "iopub.status.busy": "2021-04-05T16:49:54.121Z", "iopub.status.idle": "2021-04-05T16:49:54.135Z", "shell.execute_reply": "2021-04-05T16:49:54.139Z" }, "jupyter": { "outputs_hidden": false, "source_hidden": false }, "nteract": { "transient": { "deleting": false } } }, "outputs": [], "source": [ "def I(t,d):\n", " return np.pi*((d+2.0*t)**4.0-d**4.0)/64" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true, "execution": { "iopub.execute_input": "2021-04-05T16:49:55.098Z", "iopub.status.busy": "2021-04-05T16:49:55.093Z", "iopub.status.idle": "2021-04-05T16:49:55.108Z", "shell.execute_reply": "2021-04-05T16:49:55.112Z" }, "jupyter": { "outputs_hidden": false, "source_hidden": false }, "nteract": { "transient": { "deleting": false } } }, "outputs": [], "source": [ "def mid_point(q,L,E,t,d):\n", " q = 100.0 #N/mm\n", " L = 1000.0 #mm\n", " E = 70000.0 #MPa\n", " return (5*q*L**4.0)/(384.0*E*I(t,d))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true, "execution": { "iopub.execute_input": "2021-04-05T16:49:56.085Z", "iopub.status.busy": "2021-04-05T16:49:56.078Z", "iopub.status.idle": "2021-04-05T16:49:56.095Z", "shell.execute_reply": "2021-04-05T16:49:56.101Z" }, "jupyter": { "outputs_hidden": false, "source_hidden": false }, "nteract": { "transient": { "deleting": false } } }, "outputs": [], "source": [ "def mass(rho,L,t,d):\n", " V_tot = L*rho*np.pi*(d/2.0+t)**2.0\n", " V_in = L*rho*np.pi*(d/2.0)**2.0\n", " return (V_tot-V_in)*1000.0" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "nteract": { "transient": { "deleting": false } } }, "source": [ "We now give values to the load $q$, the length of the beam $L$ as well as the Young's modulus $E$ and the density $\\rho$ for our problem." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true, "execution": { "iopub.execute_input": "2021-04-05T16:49:57.822Z", "iopub.status.busy": "2021-04-05T16:49:57.815Z", "iopub.status.idle": "2021-04-05T16:49:57.830Z", "shell.execute_reply": "2021-04-05T16:49:57.835Z" }, "jupyter": { "outputs_hidden": false, "source_hidden": false }, "nteract": { "transient": { "deleting": false } } }, "outputs": [], "source": [ "q = 100.0 #N/mm\n", "L = 1000.0 #mm\n", "E = 70000.0 #MPa\n", "rho = 2.7e-9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise you need to:\n", "1. define the cost function of the problem\n", "2. define the bounds for the optimization of the thickness $t$ and diameter $d$ of the beam\n", "3. define the inequality constraint equation for the deflection of the beam $u$\n", "4. use the SLSQP algorithm to determine the optimal combination of thickness and diameter for the beam. Here the initial values can be chosen in the middle of the allowable range for the thickness and diameter of the beam.\n", "5. evaluate the local/global minima by carrying out two more optimization with different starting points.\n", "6. visualize the cost function of the problem using a filled contour plot\n", "7. add the starting points as well as the optimal thicknesses and diameters obtained in questions 4 and 5. What can you conclude on this problem?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernel_info": { "name": "python3" }, "kernelspec": { "argv": [ "/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9", "-m", "ipykernel_launcher", "-f", "{connection_file}" ], "display_name": "Python 3", "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.1" }, "nteract": { "version": "0.28.0" } }, "nbformat": 4, "nbformat_minor": 0 }