{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Configuring the Hoffman failure criterion by fitting to experimental data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sigmaepsilon.solid.material import HoffmanFailureCriterion\n", "import numpy as np\n", "\n", "inputs = [\n", " [-1, 0, 0, 0, 0, 0],\n", " [0, 1, 0, 0, 0, 0],\n", " [0, 0, -1, 0, 0, 0],\n", " [0, 0, 0, 1, 0, 0],\n", " [0, 0, 0, 0, -1, 0],\n", " [0, 0, 0, 0, 0, 1],\n", " [-1, 0.2, 0, 0, 0, 0],\n", " [0.2, 1, 0, 0, 0, 0],\n", " [-1, 0, 0.2, 0, 0, 0],\n", "]\n", "inputs = np.array(inputs, dtype=float)\n", "outputs = np.ones(len(inputs))\n", "\n", "failure_obj = HoffmanFailureCriterion()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parameter fitting with a Binary Genetic Algorithm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-0.9694749694749696,\n", " -3.0647130647130645,\n", " 0.808302808302809,\n", " 8.686202686202687,\n", " -4.378510378510379,\n", " -4.349206349206349,\n", " 1.2380952380952372,\n", " -2.5274725274725274,\n", " 1.0378510378510377)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params = failure_obj.fit(\n", " inputs,\n", " outputs,\n", " solver_params=dict(nPop=100, length=12),\n", " penalty=1e12,\n", " tol=0.1,\n", " n_iter=100,\n", " ranges=[[-10, 10] for _ in range(9)],\n", " method=\"bga\",\n", ")\n", "failure_obj.params = params\n", "params" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.6043478260869566, 1.9910851099808313)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prediction = failure_obj.utilization(stresses=inputs)\n", "max_error = np.max(np.abs(prediction - outputs))\n", "total_error = np.sum(np.sqrt((prediction - outputs) ** 2))\n", "max_error, total_error" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.02121238, 1.20985704, 0.40580257, 0.80769231, 0.39565217,\n", " 0.96352941, 1.20067546, 1.01013493, 0.87811824])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prediction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parameter fitting with Scipy's Nelder-Mead method" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-1.007322989181017,\n", " 2.1576353611607075,\n", " -0.1541917498510884,\n", " 0.9728903373271577,\n", " -0.9949634593134442,\n", " 0.30283382032188944,\n", " 0.35151153576340144,\n", " 0.4327118607825844,\n", " 0.44137317208707544)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params = failure_obj.fit(\n", " inputs,\n", " outputs,\n", " penalty=1e12,\n", " tol=0.1,\n", " method=\"Nelder-Mead\",\n", " x0=[-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 0.2, 0.2, 0.2],\n", ")\n", "failure_obj.params = params\n", "params" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3.311006678188673, 7.380983890058536)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prediction = failure_obj.utilization(stresses=inputs)\n", "max_error = np.max(np.abs(prediction - outputs))\n", "total_error = np.sum(np.sqrt((prediction - outputs) ** 2))\n", "max_error, total_error" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.98936093, 1.20858211, 1.02177759, 2.84485685, -2.31100668,\n", " 2.26565651, 0.92597216, 0.35965563, 1.00409288])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prediction" ] } ], "metadata": { "kernelspec": { "display_name": ".solid.material", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 2 }