Skip to content

Commit

Permalink
Created using Colab
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoCalcul committed Oct 1, 2024
1 parent c97610a commit 88a037d
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions Truss_Element.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"private_outputs": true,
"provenance": [],
"authorship_tag": "ABX9TyMxahuOixvTTe6EdHJBA9EE",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/GEORMC/Nnumerical_Methods_Course/blob/main/Truss_Element.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7_YgV2riW2t1"
},
"outputs": [],
"source": [
"import numpy as np\n",
" # Input data\n",
"coordinates = np.array([\n",
" [0, 0],\n",
" [4, 0],\n",
" [8, 0],\n",
" [4, -6]\n",
"])\n",
"connectivity = np.array([\n",
" [0, 3],\n",
" [1, 3],\n",
" [2, 3]\n",
"])\n",
"E = 1 # Young's Modulus (Pa)\n",
"A = 1 # Cross-sectional area (m^2)\n",
" # Define supports (0 for not supported, 1 for supported)\n",
"supports = np.array([\n",
" [1, 1],\n",
" [1, 1],\n",
" [1, 1],\n",
" [0, 0]\n",
"])\n",
" # Define applied loads (0 for no load, specify direction and value)\n",
"applied_loads = np.array([\n",
" [0, 0],\n",
" [0, 0], # Node 1, applied horizontal load\n",
" [0, 0], # Node 2, applied vertical load\n",
" [100, -100]\n",
"])\n",
" # Create freedom matrix\n",
"num_nodes = len(coordinates)\n",
"num_dofs = 2 * num_nodes\n",
"num_elements=len(connectivity)\n",
"dofs = np.zeros((num_elements, 4), dtype=int)\n",
"NodeDof=np.zeros((num_nodes, 2) , dtype=int)\n",
" # Initialize global stiffness matrix and force vector\n",
"KG = np.zeros((num_dofs, num_dofs))\n",
"kt_global = np.zeros((num_dofs, num_dofs))\n",
"F_global = np.zeros(num_dofs)\n",
" # Calculate Dof Matrix\n",
"for i, (node1, node2) in enumerate(connectivity):\n",
" dofs[i,:] = np.array([2 * node1, 2 * node1 + 1, 2 * node2, 2 * node2 + 1])\n",
" NodeDof[node1,0]=np.array([2 * node1])\n",
" NodeDof[node1,1]=np.array([2 * node1 + 1])\n",
" NodeDof[node2,0]=np.array([ 2 * node2])\n",
" NodeDof[node2,1]=np.array([2* node2+1])\n",
" print(NodeDof)\n",
"# Calculate element lengths and stiffness matrices\n",
"for i, (node1, node2) in enumerate(connectivity):\n",
" x1, y1 = coordinates[node1]\n",
" x2, y2 = coordinates[node2]\n",
" L = np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)\n",
" c = (x2 - x1) / L\n",
" s = (y2 - y1) / L\n",
" k_local = (E * A / L) * np.array([\n",
" [c ** 2, c * s, -c ** 2, -c * s],\n",
" [c * s, s ** 2, -c * s, -s ** 2],\n",
" [-c ** 2, -c * s, c ** 2, c * s],\n",
" [-c * s, -s ** 2, c * s, s ** 2]\n",
" ])\n",
" # Assemble local stiffness matrix into global stiffness matrix\n",
"\n",
" KG[np.ix_(dofs[i,:], dofs[i,:])] += k_local\n",
" # print(k_local)\n",
" # print(np.ix_(dofs[i,:], dofs[i,:]) )\n",
" # print(K_global)\n",
" print(KG)\n",
"kt_global=KG*1\n",
"# Apply boundary conditions and applied loads\n",
"for node in range(num_nodes):\n",
" for i in range(2):\n",
" if supports[node, i] == 1:\n",
" fixed_dof = NodeDof[node,i]\n",
" kt_global[fixed_dof, :] = 0\n",
" kt_global[:, fixed_dof] = 0\n",
" kt_global[fixed_dof, fixed_dof] = 1\n",
" F_global[fixed_dof] = 0\n",
" else:\n",
" applied_force = applied_loads[node, i]\n",
" F_global[2 * node + i] = applied_force\n",
" print(KG)\n",
"# Solve for displacements\n",
"displacement = np.linalg.solve(kt_global, F_global)\n",
"print(KG)\n",
"# Calculate reaction forces\n",
"reaction_forces = np.dot(KG, displacement)\n",
" print(KG)\n",
"print(\"Displacements (mm):\")\n",
"print(displacement * 1000)\n",
"print(\"Reaction Forces (N):\")\n",
"print(reaction_forces)\n",
"\n",
"\n"
]
}
]
}

0 comments on commit 88a037d

Please sign in to comment.