{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Frame Analysis Example\n\nAnalyse a cross-section to be used in frame analysis.\n\nThe following example demonstrates how *sectionproperties* can be used to\ncalculate the cross-section properties required for a frame analysis. Using this\nmethod is preferred over executing a geometric and warping analysis as only variables\nrequired for a frame analysis are computed. In this example the torsion constant of\na rectangular section is calculated for a number of different mesh sizes and the\naccuracy of the result compared with the time taken to obtain the solution.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_number = 1\n\nimport time\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport sectionproperties.pre.library.primitive_sections as sections\nfrom sectionproperties.analysis.section import Section"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a rectangular section\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "geometry = sections.rectangular_section(d=100, b=50)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a list of mesh sizes to analyse\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh_sizes = [3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 200]\nj_calc = []  # list to store torsion constants\nt_calc = []  # list to store computation times"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Loop through mesh sizes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for mesh_size in mesh_sizes:\n    geometry.create_mesh(mesh_sizes=[mesh_size])  # create mesh\n    section = Section(geometry)  # create a Section object\n    start_time = time.time()  # start timing\n    # calculate the frame properties\n    (_, _, _, _, j, _) = section.calculate_frame_properties()\n    t = time.time() - start_time  # stop timing\n    t_calc.append(t)  # save the time\n    j_calc.append(j)  # save the torsion constant\n    # print the result\n    str = \"Mesh Size: {0}; \".format(mesh_size)\n    str += \"Solution Time {0:.5f} s; \".format(t)\n    str += \"Torsion Constant: {0:.12e}\".format(j)\n    print(str)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compute the error, assuming that the finest mesh (index 0) gives the 'correct' value\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "correct_val = j_calc[0]\nj_np = np.array(j_calc)\nerror_vals = (j_calc - correct_val) / j_calc * 100"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Produce a plot of the accuracy of the torsion constant with computation time\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.loglog(t_calc[1:], error_vals[1:], \"kx-\")\nplt.xlabel(\"Solver Time [s]\")\nplt.ylabel(\"Torsion Constant Error [%]\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "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.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}