{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Torsion Constant of a Rectangle\n\nPlot the variation of the torsion constant with aspect ratio of a rectangle section.\n\nIn this example, the aspect ratio of a rectangular section is varied whilst keeping a constant\ncross-sectional area and the torsion constant calculated. The variation of the torsion constant\nwith the aspect ratio is then plotted.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_number = 1\n\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": [
        "Rectangle dimensions\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "d_list = []\nb_list = np.linspace(0.2, 1, 20)\nj_list = []  # list holding torsion constant results"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Number of elements for each analysis\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n = 100"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Loop through all the widths\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for b in b_list:\n    # calculate d assuming area = 1\n    d = 1 / b\n    d_list.append(d)\n\n    # compute mesh size\n    ms = d * b / n\n\n    # perform a warping analysis on rectangle\n    geometry = sections.rectangular_section(d=d, b=b)\n    geometry.create_mesh(mesh_sizes=[ms])\n    section = Section(geometry)\n    section.calculate_geometric_properties()\n    section.calculate_warping_properties()\n\n    # get the torsion constant\n    j = section.get_j()\n    print(\"d/b = {0:.3f}; J = {1:.5e}\".format(d / b, j))\n    j_list.append(j)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the torsion constant as a function of the aspect ratio\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "(fig, ax) = plt.subplots()\nax.plot(np.array(d_list) / b_list, j_list, \"kx-\")\nax.set_xlabel(\"Aspect Ratio [d/b]\")\nax.set_ylabel(\"Torsion Constant [J]\")\nax.set_title(\"Rectangular Section Torsion Constant\")\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
}