{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python problem for TKT4140 re-sit exam\n", "## 18 August 2023" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this problem, you need to analyse the following python notebook.\n", "\n", "In the first cell you need to write what is the overall goal of this python notebook.\n", "\n", "You need to comment in the cell marked with `insert comment here` what is the purpose of the following cell.\n", "\n", "In the final cell you can comment upon the overall optimization problem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert the overall goal of this python notebook`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from scipy.optimize import minimize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def cost_function(cm,data):\n", " error = 0.0\n", " peeq = data[0]\n", " seq = data[1]\n", " model = voce(peeq,cm)\n", " for t,m in zip(seq,model):\n", " error += np.abs(t-m)**2.0\n", " return np.sqrt(error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def voce(p,cm):\n", " return cm[0]+cm[2]*(1.0-np.exp(-cm[1]/cm[2]*p))+cm[4]*(1.0-np.exp(-cm[3]/cm[4]*p))+cm[6]*(1.0-np.exp(-cm[5]/cm[6]*p))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = np.loadtxt('UT_data.csv',skiprows=1,delimiter=',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following cell is meant to prepare data and is not supposed to be discussed during the examination" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#-------------------------------------------------------------------------------\n", "# Extract stress and strain arrays\n", "#-------------------------------------------------------------------------------\n", "strain = data[:,1]\n", "stress = data[:,2]\n", "#-------------------------------------------------------------------------------\n", "# Find necking point\n", "#-------------------------------------------------------------------------------\n", "neck = np.where(stress == np.max(stress))[0][0]\n", "#-------------------------------------------------------------------------------\n", "# Compute true stress and true strain\n", "#-------------------------------------------------------------------------------\n", "true_strain = np.log(1.0+strain[0:neck])\n", "true_stress = stress[0:neck]*(1.0+strain[0:neck])\n", "#-------------------------------------------------------------------------------\n", "# Find yield stress (here fixed to 200 MPa)\n", "#-------------------------------------------------------------------------------\n", "yielding = np.where(true_stress >= 200.0)[0][0]\n", "#-------------------------------------------------------------------------------\n", "# Compute equivalent plastic strain\n", "#-------------------------------------------------------------------------------\n", "plastic_strain = true_strain[yielding:]-true_strain[yielding]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (plastic_strain,true_stress[yielding:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm0 = [200.0,2310.42, 5.26, 993.48, 89.26, 19.08, 2.10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = []\n", "b.append((199.0,201.0))\n", "b.append((1000.0,5000.0))\n", "b.append((1.0,100.0))\n", "b.append((500.0,2000.0))\n", "b.append((750.0,1500.0))\n", "b.append((0.01,500.0))\n", "b.append((0.1,100.0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = []\n", "c.append({'type': 'ineq', 'fun': lambda x: x[1]/x[2]-x[3]/x[4]})\n", "c.append({'type': 'ineq', 'fun': lambda x: x[3]/x[4]-x[5]/x[6]})\n", "c.append({'type': 'eq', 'fun': lambda x: voce(plastic_strain[-1],x)-true_stress[-1]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "res = minimize(cost_function,cm0,constraints=c,bounds=b,method='SLSQP',args=(data,))\n", "fit_1 = res.x\n", "print(res.x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(1,1,figsize=(7,5))\n", "axs.plot(plastic_strain,true_stress[yielding:],label='Experiments')\n", "axs.plot(plastic_strain,voce(plastic_strain,fit_1),label='Optimized')\n", "axs.plot(plastic_strain,voce(plastic_strain,cm0),label='Initial')\n", "axs.grid()\n", "axs.set_xlim([0.0,0.14])\n", "axs.set_ylim([150.0,350.0])\n", "axs.set_xlabel('Equivalent plastic strain (-)')\n", "axs.set_ylabel('Equivalent stress (MPa)')\n", "axs.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm0 = [199.0, 1000.0, 1.0, 500.0, 750.0, 0.01, 0.1]\n", "res = minimize(cost_function,cm0,constraints=c,bounds=b,method='SLSQP',args=(data,))\n", "fit_2 = res.x\n", "print(res.x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm0 = [201.0, 5000.0, 100.0, 2000.0, 1500.0, 500.0, 100.0]\n", "res = minimize(cost_function,cm0,constraints=c,bounds=b,method='SLSQP',args=(data,))\n", "fit_3 = res.x\n", "print(res.x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`insert comment here`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(1,1,figsize=(7,5))\n", "axs.plot(plastic_strain,true_stress[yielding:],label='Experiments')\n", "axs.plot(plastic_strain,voce(plastic_strain,fit_1),label='1st fitting')\n", "axs.plot(plastic_strain,voce(plastic_strain,fit_2),label='2nd fitting')\n", "axs.plot(plastic_strain,voce(plastic_strain,fit_3),label='3rd fitting')\n", "axs.grid()\n", "axs.set_xlim([0.0,0.14])\n", "axs.set_ylim([150.0,350.0])\n", "axs.set_xlabel('Equivalent plastic strain (-)')\n", "axs.set_ylabel('Equivalent stress (MPa)')\n", "axs.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`final comments`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "env_tf", "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.9.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }