Skip to content

Instantly share code, notes, and snippets.

@bagustris
Created May 26, 2021 05:08
Show Gist options
  • Select an option

  • Save bagustris/3c0d97cdbc2944c64d527c7948c0c03a to your computer and use it in GitHub Desktop.

Select an option

Save bagustris/3c0d97cdbc2944c64d527c7948c0c03a to your computer and use it in GitHub Desktop.

Revisions

  1. bagustris revised this gist May 26, 2021. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion calfem.ipynb
    Original file line number Diff line number Diff line change
    @@ -24,10 +24,21 @@
    "name": "Calfem.ipynb",
    "provenance": [],
    "collapsed_sections": [],
    "toc_visible": true
    "toc_visible": true,
    "include_colab_link": true
    }
    },
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "view-in-github",
    "colab_type": "text"
    },
    "source": [
    "<a href=\"https://colab.research.google.com/gist/bagustris/3c0d97cdbc2944c64d527c7948c0c03a/calfem.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
  2. bagustris created this gist May 26, 2021.
    1,063 changes: 1,063 additions & 0 deletions calfem.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,1063 @@
    {
    "nbformat": 4,
    "nbformat_minor": 0,
    "metadata": {
    "anaconda-cloud": {},
    "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.6.4"
    },
    "colab": {
    "name": "Calfem.ipynb",
    "provenance": [],
    "collapsed_sections": [],
    "toc_visible": true
    }
    },
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "gFSDmEZytWMX"
    },
    "source": [
    "# What is CALFEM for Python"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "nWqS94s4tWMa"
    },
    "source": [
    "* Subset of CALFEM routines implemented in Python\n",
    "* Using NumPy for matrices\n",
    "* Additional mesh generation routines supporting Triangle and GMSH\n",
    "* Plotting with Matplotlib"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "H0G9jlwKtWMb"
    },
    "source": [
    "# CALFEM Python modules"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "lDlZ3786tWMc"
    },
    "source": [
    "* **calfem.core**\n",
    " * Element routines\n",
    " * System routines\n",
    "* **calfem.utils**\n",
    " * I/O routines\n",
    " * Misc. routines\n",
    "* **calfem.geometry**\n",
    " * Routines for defining problem geometry used for input in mesh generation\n",
    "* **calfem.mesh**\n",
    " * Mesh generation routines\n",
    "* **calfem.vis/calfem.vis_mpl**\n",
    " * Routines for visualising geometry, meshes and results."
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "TmEFTEGmzvOA"
    },
    "source": [
    "# Installing CALFEM for Pyton"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "bUpWJT0Zzr2q",
    "colab": {
    "base_uri": "https://localhost:8080/"
    },
    "outputId": "ae809070-721f-452e-de43-96d0e618ea43"
    },
    "source": [
    "!pip install calfem-python\n",
    "!pip install pyqt5"
    ],
    "execution_count": null,
    "outputs": [
    {
    "output_type": "stream",
    "text": [
    "Collecting calfem-python\n",
    "\u001b[?25l Downloading https://files.pythonhosted.org/packages/b1/89/75feb11d12f8ac88feb6c745a7e37621caea6750a1242dd16f0bc5a91e70/calfem_python-3.5.3-py3-none-any.whl (70kB)\n",
    "\u001b[K |████████████████████████████████| 71kB 3.0MB/s \n",
    "\u001b[?25hCollecting visvis\n",
    "\u001b[?25l Downloading https://files.pythonhosted.org/packages/02/4e/e175ada34cb13967ed8b6597ba39ac737e191d7c5a64a1d0fd622c334a24/visvis-1.13.0.tar.gz (5.1MB)\n",
    "\u001b[K |████████████████████████████████| 5.1MB 7.1MB/s \n",
    "\u001b[?25hCollecting pyvtk\n",
    " Downloading https://files.pythonhosted.org/packages/6a/dc/d5ffc2cc50bdd7a2e7b435655ee5931d614f7c118624dd20c51440c79337/PyVTK-0.5.18.tar.gz\n",
    "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from calfem-python) (3.2.2)\n",
    "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from calfem-python) (1.19.5)\n",
    "Requirement already satisfied: pyOpenGl in /usr/local/lib/python3.7/dist-packages (from visvis->calfem-python) (3.1.5)\n",
    "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from pyvtk->calfem-python) (1.15.0)\n",
    "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->calfem-python) (2.8.1)\n",
    "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->calfem-python) (1.3.1)\n",
    "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->calfem-python) (2.4.7)\n",
    "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->calfem-python) (0.10.0)\n",
    "Building wheels for collected packages: visvis, pyvtk\n",
    " Building wheel for visvis (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
    " Created wheel for visvis: filename=visvis-1.13.0-cp37-none-any.whl size=4905513 sha256=8862b98830b4158cef834b2e7877c917651d8a08d4b66730ca1b0950a75ecced\n",
    " Stored in directory: /root/.cache/pip/wheels/30/c2/3e/d6ecf9ef400e03b84f3533211fa1a9cb21f828c12658508b44\n",
    " Building wheel for pyvtk (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
    " Created wheel for pyvtk: filename=PyVTK-0.5.18-cp37-none-any.whl size=24735 sha256=39ad91270bf76f32d1ce69c27bb33c30b0a0dc0a704301978308779f2cae56ca\n",
    " Stored in directory: /root/.cache/pip/wheels/7d/73/d6/4c3ce104624b4459b6dbc3329235f68815d35e5d6c0f3aac83\n",
    "Successfully built visvis pyvtk\n",
    "Installing collected packages: visvis, pyvtk, calfem-python\n",
    "Successfully installed calfem-python-3.5.3 pyvtk-0.5.18 visvis-1.13.0\n",
    "Collecting pyqt5\n",
    "\u001b[?25l Downloading https://files.pythonhosted.org/packages/ed/62/cd9f10702c75b242f82da858668fba0cda04cda92133244d3d1555e530b4/PyQt5-5.15.4-cp36.cp37.cp38.cp39-abi3-manylinux2014_x86_64.whl (8.3MB)\n",
    "\u001b[K |████████████████████████████████| 8.3MB 4.1MB/s \n",
    "\u001b[?25hCollecting PyQt5-Qt5>=5.15\n",
    "\u001b[?25l Downloading https://files.pythonhosted.org/packages/83/d4/241a6a518d0bcf0a9fcdcbad5edfed18d43e884317eab8d5230a2b27e206/PyQt5_Qt5-5.15.2-py3-none-manylinux2014_x86_64.whl (59.9MB)\n",
    "\u001b[K |████████████████████████████████| 59.9MB 82kB/s \n",
    "\u001b[?25hCollecting PyQt5-sip<13,>=12.8\n",
    "\u001b[?25l Downloading https://files.pythonhosted.org/packages/31/24/f887203677955ba4d5d4efe9176ac7ed2bf84efce8c243ab91e63183ad9e/PyQt5_sip-12.8.1-cp37-cp37m-manylinux1_x86_64.whl (283kB)\n",
    "\u001b[K |████████████████████████████████| 286kB 27.1MB/s \n",
    "\u001b[?25hInstalling collected packages: PyQt5-Qt5, PyQt5-sip, pyqt5\n",
    "Successfully installed PyQt5-Qt5-5.15.2 PyQt5-sip-12.8.1 pyqt5-5.15.4\n"
    ],
    "name": "stdout"
    }
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "QSmht_QotWMe"
    },
    "source": [
    "# General procedure for FE calculation"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "hOcQ1h_YtWMf"
    },
    "source": [
    "* Define the model\n",
    "* Generate element matrices\n",
    "* Assemble element matrices in the global stiffness matrix\n",
    "* Solve the system of equations\n",
    "* Calculate element forces"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "xWdbZzY9tWMg"
    },
    "source": [
    "# Example 1 - System of springs"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "JmL12HGVtWMg"
    },
    "source": [
    "![alt text](https://drive.google.com/uc?id=1nVxbglBfoJMvjOB_bGzFSxT6XLuvoQvg)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "bOzQsbQftWMh"
    },
    "source": [
    "First we import the required Python-modules:"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "vuiqm6cFtWMi"
    },
    "source": [
    "import numpy as np\n",
    "import calfem.core as cfc\n",
    "import calfem.vis_mpl as cfv\n",
    "import math"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "Jc13lKa4tWMm"
    },
    "source": [
    "Create the topology matrix. All input to CALFEM is NumPy arrays. Topology is defined by index 1."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "UsHJRBIVtWMn",
    "colab": {
    "base_uri": "https://localhost:8080/"
    },
    "outputId": "9008bf91-4654-47dc-d8c5-7750f0ad501d"
    },
    "source": [
    "Edof = np.array([\n",
    " [1,2],\n",
    " [2,3],\n",
    " [2,3]\n",
    " ])\n",
    "print(Edof)"
    ],
    "execution_count": null,
    "outputs": [
    {
    "output_type": "stream",
    "text": [
    "[[1 2]\n",
    " [2 3]\n",
    " [2 3]]\n"
    ],
    "name": "stdout"
    }
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "cbxmq_WdtWMs"
    },
    "source": [
    "Stiffness matrix and force vector."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "k2eVblPhtWMt",
    "colab": {
    "base_uri": "https://localhost:8080/"
    },
    "outputId": "21cc4090-e3cc-47eb-9dde-c396cef452bb"
    },
    "source": [
    "K = np.zeros((3,3))\n",
    "f = np.zeros((3,1))\n",
    "print(K)\n",
    "print(f)"
    ],
    "execution_count": null,
    "outputs": [
    {
    "output_type": "stream",
    "text": [
    "[[0. 0. 0.]\n",
    " [0. 0. 0.]\n",
    " [0. 0. 0.]]\n",
    "[[0.]\n",
    " [0.]\n",
    " [0.]]\n"
    ],
    "name": "stdout"
    }
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "W1WAd9vxtWMx"
    },
    "source": [
    "Element matrices."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "XaB3JJG2tWMy",
    "colab": {
    "base_uri": "https://localhost:8080/"
    },
    "outputId": "e1c9fccf-b15a-437e-a48f-9561619fa588"
    },
    "source": [
    "k = 1500.0\n",
    "ep1 = k\n",
    "ep2 = 2.0*k\n",
    "Ke1 = cfc.spring1e(ep1)\n",
    "Ke2 = cfc.spring1e(ep2)\n",
    "print(Ke1)\n",
    "print(Ke2)"
    ],
    "execution_count": null,
    "outputs": [
    {
    "output_type": "stream",
    "text": [
    "[[ 1500. -1500.]\n",
    " [-1500. 1500.]]\n",
    "[[ 3000. -3000.]\n",
    " [-3000. 3000.]]\n"
    ],
    "name": "stdout"
    }
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "EosogK5itWM2"
    },
    "source": [
    "Assemble element matrices in global stiffness matrix."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "PnRJk8YxtWM2",
    "colab": {
    "base_uri": "https://localhost:8080/"
    },
    "outputId": "52ebaf8d-e5e7-4fa8-c8e4-5cec55b246fe"
    },
    "source": [
    "cfc.assem(Edof[0,:], K, Ke2)\n",
    "cfc.assem(Edof[1,:], K, Ke1)\n",
    "cfc.assem(Edof[2,:], K, Ke2)\n",
    "\n",
    "print(K)"
    ],
    "execution_count": null,
    "outputs": [
    {
    "output_type": "stream",
    "text": [
    "[[ 3000. -3000. 0.]\n",
    " [-3000. 7500. -4500.]\n",
    " [ 0. -4500. 4500.]]\n"
    ],
    "name": "stdout"
    }
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "D_qDeNR0tWM5"
    },
    "source": [
    "Solving the equation system.\n",
    "\n",
    "bc is an array of prescribed degrees of freedom. Values to be specified are specified in a separate array. If all values are 0, they don't have to be specified. \n",
    "\n",
    "Note: NumPy index from 0 ie 1 here corresponds to degree of freedom 2"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "CS9_3q1OtWM6",
    "colab": {
    "base_uri": "https://localhost:8080/"
    },
    "outputId": "0748a78d-35f0-4aed-ee4b-e8a08342aead"
    },
    "source": [
    "bc = np.array([1,3])\n",
    "\n",
    "f[1] = 100.0\n",
    "\n",
    "# Solve equation system \n",
    "\n",
    "a, r = cfc.solveq(K, f, bc)\n",
    "\n",
    "print(\"Displacements:\")\n",
    "print(a)\n",
    "print(\"Reaction forces:\")\n",
    "print(r)"
    ],
    "execution_count": null,
    "outputs": [
    {
    "output_type": "stream",
    "text": [
    "Displacements:\n",
    "[[0. ]\n",
    " [0.01333333]\n",
    " [0. ]]\n",
    "Reaction forces:\n",
    "[[-40.]\n",
    " [ 0.]\n",
    " [-60.]]\n"
    ],
    "name": "stdout"
    }
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "fkayC2m3tWM9"
    },
    "source": [
    "Calculating element forces."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "RkzQgzz8tWM-"
    },
    "source": [
    "ed1 = cfc.extractEldisp(Edof[0,:], a)\n",
    "ed2 = cfc.extractEldisp(Edof[1,:], a)\n",
    "ed3 = cfc.extractEldisp(Edof[2,:], a)\n",
    " \n",
    "es1 = cfc.spring1s(ep2, ed1)\n",
    "es2 = cfc.spring1s(ep1, ed2)\n",
    "es3 = cfc.spring1s(ep2, ed3)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "D-eJKUm9tWNB"
    },
    "source": [
    "print(\"N1 =\", es1)\n",
    "print(\"N2 =\", es2)\n",
    "print(\"N3 =\", es3)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "GmhChjXUtWNI"
    },
    "source": [
    "# Example 2 - Bars"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "_2kdcB0ItWNJ"
    },
    "source": [
    "![alt text](https://drive.google.com/uc?id=1VBNcsu0MKWP0donOiC7CeSJeAoYZQS68)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "ceRrvOKwtWNK"
    },
    "source": [
    "Define element topology. Element number are not used in the **Edof** matrix in CALFEM for Python."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "558bhYPmtWNK"
    },
    "source": [
    "Edof = np.array([\n",
    " [1,2,5,6],\n",
    " [5,6,7,8],\n",
    " [3,4,5,6]\n",
    "])"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "I70w6CUmtWNN"
    },
    "source": [
    "Stiffness matrix and load vector."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "tBslPdwutWNP"
    },
    "source": [
    "K = np.matrix(np.zeros((8,8)))\n",
    "f = np.matrix(np.zeros((8,1)))"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "3Qr3rjgqtWNS"
    },
    "source": [
    "Element properties."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "TpnMqyK5tWNT"
    },
    "source": [
    "E = 2.0e11\n",
    "A1 = 6.0e-4\n",
    "A2 = 3.0e-4\n",
    "A3 = 10.0e-4\n",
    "ep1 = [E, A1] # Vanliga listor kan användas för att definiera element-\n",
    "ep2 = [E, A2] # egenskaper\n",
    "ep3 = [E, A3]"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "IXOMKpPltWNV"
    },
    "source": [
    "Element coordinates."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "F_StF3g2tWNW"
    },
    "source": [
    "ex1=np.array([0.,1.6])\n",
    "ex2=np.array([1.6,1.6])\n",
    "ex3=np.array([0.,1.6])\n",
    "\n",
    "ey1=np.array([0.,0.])\n",
    "ey2=np.array([0.,1.2])\n",
    "ey3=np.array([1.2,0.])"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "2tvMp03ftWNZ"
    },
    "source": [
    "Element matrices."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "g2a2lkAstWNa"
    },
    "source": [
    "Ke1 = cfc.bar2e(ex1, ey1, ep1)\n",
    "Ke2 = cfc.bar2e(ex2, ey2, ep2)\n",
    "Ke3 = cfc.bar2e(ex3, ey3, ep3)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "NITmOQq_tWNd"
    },
    "source": [
    "Assemble stiffness matrix."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "A_0HwLpstWNd"
    },
    "source": [
    "cfc.assem(Edof[0,:],K,Ke1)\n",
    "cfc.assem(Edof[1,:],K,Ke2)\n",
    "cfc.assem(Edof[2,:],K,Ke3)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "d9Y6ne2MtWNh"
    },
    "source": [
    "Solve the equation system."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "51LIp0LetWNi"
    },
    "source": [
    "bc = np.array([1,2,3,4,7,8])\n",
    "f[5] = -80e3\n",
    "\n",
    "a, r = cfc.solveq(K, f, bc)\n",
    "\n",
    "print(\"Displacements:\")\n",
    "print(a)\n",
    "print(\"Reaction forces:\")\n",
    "print(r)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "CR94WSfgtWNk"
    },
    "source": [
    "Calculate element forces."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "2PKnGVP8tWNl"
    },
    "source": [
    "ed1=cfc.extractEldisp(Edof[0,:],a);\n",
    "N1=cfc.bar2s(ex1,ey1,ep1,ed1)\n",
    "ed2=cfc.extractEldisp(Edof[1,:],a);\n",
    "N2=cfc.bar2s(ex2,ey2,ep2,ed2)\n",
    "ed3=cfc.extractEldisp(Edof[2,:],a);\n",
    "N3=cfc.bar2s(ex3,ey3,ep3,ed3)\n",
    "print(\"N1=\",N1)\n",
    "print(\"N2=\",N2)\n",
    "print(\"N3=\",N3)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "50NnInZYtWNn"
    },
    "source": [
    "# Example 3 - More bars"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "6IInWnYItWNo"
    },
    "source": [
    "![alt text](https://drive.google.com/uc?id=1vKAnxWXDASnXs254hggY6xMGhGU8T4b9)\n",
    "![alt text](https://drive.google.com/uc?id=1fx0_yPlDvu6kmISfBJEiY73AtYocSRTc)\n"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "NPABtMlktWNo"
    },
    "source": [
    "Element topology."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "SI9pBAoEtWNp"
    },
    "source": [
    "Edof = np.array([\n",
    " [1, 2, 5, 6],\n",
    " [3, 4, 7, 8],\n",
    " [5, 6, 9, 10],\n",
    " [7, 8, 11, 12],\n",
    " [7, 8, 5, 6],\n",
    " [11, 12, 9, 10],\n",
    " [3, 4, 5, 6],\n",
    " [7, 8, 9, 10],\n",
    " [1, 2, 7, 8],\n",
    " [5, 6, 11, 12]\n",
    "])"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "heukZGRHtWNr"
    },
    "source": [
    "Stiffness matrix and element properties."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "U4mPbBxUtWNs"
    },
    "source": [
    "K=np.zeros([12,12])\n",
    "f=np.zeros([12,1])\n",
    "\n",
    "A = 25.0e-4\n",
    "E = 2.1e11\n",
    "ep = [E,A]"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "1cIl24vRtWNu"
    },
    "source": [
    "Element coordinates."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "bDoWJp60tWNv"
    },
    "source": [
    "ex = np.array([\n",
    " [0., 2.],\n",
    " [0., 2.],\n",
    " [2., 4.],\n",
    " [2., 4.],\n",
    " [2., 2.],\n",
    " [4., 4.],\n",
    " [0., 2.],\n",
    " [2., 4.],\n",
    " [0., 2.],\n",
    " [2., 4.]\n",
    "])\n",
    "\n",
    "ey = np.array([\n",
    " [2., 2.],\n",
    " [0., 0.],\n",
    " [2., 2.],\n",
    " [0., 0.],\n",
    " [0., 2.],\n",
    " [0., 2.],\n",
    " [0., 2.],\n",
    " [0., 2.],\n",
    " [2., 0.],\n",
    " [2., 0.]\n",
    "])\n"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "0P_ELf50tWNy"
    },
    "source": [
    "Assemble elements using a loop."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "L3rEx4AgtWNz"
    },
    "source": [
    "for elx, ely, eltopo in zip(ex, ey, Edof):\n",
    " Ke = cfc.bar2e(elx, ely, ep)\n",
    " cfc.assem(eltopo, K, Ke)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "5FVk7ApvtWN1"
    },
    "source": [
    "Solve the equation system."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "o0GxDoYvtWN2"
    },
    "source": [
    "bc = np.array([1,2,3,4])\n",
    "\n",
    "f[10]=0.5e6*math.sin(math.pi/6)\n",
    "f[11]=-0.5e6*math.cos(math.pi/6)\n",
    "\n",
    "a, r = cfc.solveq(K,f,bc)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "-_08WXh0tWN5"
    },
    "source": [
    "Calculating element forces."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "BUHPtrhVtWN6"
    },
    "source": [
    "ed=cfc.extractEldisp(Edof,a) # Extraherar alla elementförskjutningar\n",
    "\n",
    "N=np.zeros([Edof.shape[0]]) # Array för att lagra elementkrafter\n",
    "\n",
    "print(\"Element forces:\")\n",
    "\n",
    "i = 0\n",
    "for elx, ely, eld in zip(ex, ey, ed):\n",
    " N[i] = cfc.bar2s(elx, ely, ep, eld)\n",
    " print(\"N%d = %g\" % (i+1,N[i]))\n",
    " i+=1"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "vJl2zY8TxK4F"
    },
    "source": [
    "# Example 6 - Frame and bars"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "V9BJFzhhx1KC"
    },
    "source": [
    "![alt text](https://drive.google.com/uc?id=1RnDUtwd0xr7yhDVxtsXfWMlFPgbAcTUy)"
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "dywtvOaOyKhQ"
    },
    "source": [
    "System matrices"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "Od0OlgUXsSVy"
    },
    "source": [
    "K = np.zeros([18,18])\n",
    "f = np.zeros([18,1])\n",
    "f[12,0] = 1.0\n",
    "\n",
    "coord = np.array([\n",
    " [0.0, 0.0],\n",
    " [1.0, 0.0],\n",
    " [0.0, 1.0],\n",
    " [1.0, 1.0],\n",
    " [0.0, 2.0],\n",
    " [1.0, 2.0]\n",
    "])\n",
    "\n",
    "dof1 = np.array([\n",
    " [1, 2, 3],\n",
    " [4, 5, 6],\n",
    " [7, 8, 9],\n",
    " [10, 11, 12],\n",
    " [13, 14, 15],\n",
    " [16, 17, 18]\n",
    "])\n",
    "\n",
    "dof2 = np.array([\n",
    " [1, 2],\n",
    " [4, 5],\n",
    " [7, 8],\n",
    " [10, 11],\n",
    " [13, 14],\n",
    " [16, 17]\n",
    "])"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "uAIQvIuB0KmG"
    },
    "source": [
    "Element properties, topology and coordinates"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "7HdJM9lxytXS"
    },
    "source": [
    "ep1 = [1.0, 1.0, 1.0]\n",
    "\n",
    "edof1 = np.array([\n",
    " [1, 2, 3, 7, 8, 9],\n",
    " [7, 8, 9, 13, 14, 15],\n",
    " [4, 5, 6, 10, 11, 12],\n",
    " [10, 11, 12, 16, 17, 18],\n",
    " [7, 8, 9, 10, 11, 12],\n",
    " [13, 14, 15, 16, 17, 18]\n",
    "])\n",
    "\n",
    "ex1, ey1 = cfc.coordxtr(edof1, coord, dof1);\n",
    "\n",
    "ep2 = [1.0, 1.0]\n",
    "\n",
    "edof2 = np.array([\n",
    " [1, 2, 10, 11],\n",
    " [7, 8, 16, 17],\n",
    " [7, 8, 4, 5],\n",
    " [13, 14, 10, 11]\n",
    "])\n",
    "\n",
    "ex2, ey2 = cfc.coordxtr(edof2, coord, dof2);"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "_b_ofF4l2EOW"
    },
    "source": [
    " Create and assemble element matrices"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "uL8CUlIM1NrM"
    },
    "source": [
    "for elx, ely, eltopo in zip(ex1, ey1, edof1):\n",
    " Ke = cfc.beam2e(elx, ely, ep1)\n",
    " cfc.assem(eltopo, K, Ke)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "e7VVWmw42RZU"
    },
    "source": [
    "for elx, ely, eltopo in zip(ex2, ey2, edof2):\n",
    " Ke = cfc.bar2e(elx, ely, ep2)\n",
    " cfc.assem(eltopo,K,Ke)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "F0hFDqb32scN"
    },
    "source": [
    "Solve equation system"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "-GdjFV6P2o7r"
    },
    "source": [
    "bc_prescr = np.array([1, 2, 3, 4, 5, 6])\n",
    "a, r = cfc.solveq(K, f, bc_prescr)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "Axe1ELYJ272x"
    },
    "source": [
    "print(a)"
    ],
    "execution_count": null,
    "outputs": []
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "wJ1U3vF-2-oA"
    },
    "source": [
    ""
    ],
    "execution_count": null,
    "outputs": []
    }
    ]
    }