Skip to content

Commit

Permalink
Created using Colab
Browse files Browse the repository at this point in the history
  • Loading branch information
GEORMC committed Oct 9, 2024
1 parent 6ef74dd commit 310adfc
Showing 1 changed file with 195 additions and 0 deletions.
195 changes: 195 additions & 0 deletions Truss_with_plot.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"private_outputs": true,
"provenance": [],
"authorship_tag": "ABX9TyNiFdeHqTKjq6Y2q7yQmHLE",
"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_with_plot.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": "YUsOFx4ipiY3"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\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 = 100 # Young's Modulus (Pa)\n",
"A = 100 # 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",
"\n",
"\n",
"\n",
"\n",
"\n",
"# Extract coordinates of nodes\n",
"node_x = coordinates[:, 0]\n",
"node_y = coordinates[:, 1]\n",
"\n",
"# Extract connectivity of elements\n",
"fig, ax = plt.subplots()\n",
"\n",
"for i, (node1, node2) in enumerate(connectivity):\n",
" x1, y1 = coordinates[node1]\n",
" x2, y2 = coordinates[node2]\n",
" ax.plot([x1, x2], [y1, y2], 'k-')\n",
"\n",
"# Plot nodes\n",
"ax.plot(node_x, node_y, 'ro')\n",
"\n",
"# Add labels to nodes\n",
"for i in range(len(coordinates)):\n",
" ax.text(coordinates[i][0], coordinates[i][1], str(i))\n",
"\n",
"# Set axis labels and title\n",
"ax.set_xlabel(\"X-axis\")\n",
"ax.set_ylabel(\"Y-axis\")\n",
"ax.set_title(\"Truss Structure\")\n",
"\n",
"# Show the plot\n",
"ax.grid(True)\n",
"ax.axis('equal')\n",
"\n",
"\n",
"# Plot deformed truss\n",
"scale_factor = 10 # Adjust the scale of deformation as needed\n",
"\n",
"deformed_node_x = node_x + displacement[::2] * scale_factor\n",
"deformed_node_y = node_y + displacement[1::2] * scale_factor\n",
"\n",
"#plt.figure() # Create a new figure for the deformed truss\n",
"\n",
"for i, (node1, node2) in enumerate(connectivity):\n",
" x1, y1 = deformed_node_x[node1], deformed_node_y[node1]\n",
" x2, y2 = deformed_node_x[node2], deformed_node_y[node2]\n",
" ax.plot([x1, x2], [y1, y2], 'b-') # Plot deformed elements in blue\n",
"\n",
"ax.plot(deformed_node_x, deformed_node_y, 'ro') # Plot deformed nodes\n",
"\n",
"# Add labels to nodes\n",
"for i in range(len(coordinates)):\n",
" ax.text(deformed_node_x[i], deformed_node_y[i], str(i))\n",
"\n",
"ax.set_xlabel(\"X-axis\")\n",
"ax.set_ylabel(\"Y-axis\")\n",
"ax.set_title(\"Deformed Truss Structure\")\n",
"ax.grid(True)\n",
"ax.axis('equal')\n",
"\n",
"plt.show(block=True)\n"
]
}
]
}

0 comments on commit 310adfc

Please sign in to comment.