Question: The following python code is meant to take any truss, inputting information about its nodes, members, and external loads and the code is meant to
The following python code is meant to take any truss, inputting information about its nodes, members, and external loads and the code is meant to output all internal member forces and reactions of the truss.
Task: Add a virtual work method where the horizontal and vertical deflection can be calculated and outputted. The code should be able to output the horizontal and vertical deflection of any node in the truss.
Fix any errors in this code. Once you finish, submit the finished python code and ensure it works. I will check the code to see if it is right. If everything is good, I will give you a like immediately.
If the submitted code cant run, I will give you a thumbs down, until it is fixed.
Python Code
import numpy as np
class Truss:
def initself nodes, members, loads:
self.nodes nodes
self.members members
self.loads loads
self.numnodes lennodes
self.nummembers lenmembers
self.numunknowns self.nummembers # Each member has two unknown forces
def solvemethodofjointsself:
# Initialize the equations matrix and the forces vector
numequations self.numnodes # Number of equations
A npzerosnumequations, self.numunknowns
forces npzerosnumequations,
# Fill the equations matrix and forces vector
eqindex # Equation index
for node in self.nodes:
if node 'fixed': # Skip the fixed node
# Gather member indices connected to the current node
memberindices i for i member in enumerateselfmembers if node in membernodes
# Sum forces in x and y directions for each connected member
for memberindex in memberindices:
dx self.nodesselfmembersmemberindexnodesx self.nodesselfmembersmemberindexnodesx
dy self.nodesselfmembersmemberindexnodesy self.nodesselfmembersmemberindexnodesy
length npsqrtdx dy
costheta dx length
sintheta dy length
Aeqindex, memberindex costheta
Aeqindex, memberindex sintheta
# Add external forces to the forces vector
for load in self.loads:
if loadjoint node:
forceseqindex loadforce
eqindex
try:
# Solve for the unknown forces
unknownforces nplinalg.lstsqA forces, rcondNone
except nplinalg.LinAlgError:
printError: Incompatible dimensions in the linear equations."
return None
# Store the results in a dictionary
results
for i member in enumerateselfmembers:
resultsmembername
'forcex: unknownforces i
'forcey: unknownforces i
return results
# Example usage:
nodesA: x: y: B: x: y: C: x: y:
membersname: AB 'nodes': ABname: BC 'nodes': BC
loadsforce: 'deltax: 'deltay: 'joint': B
truss Trussnodes members loads
# Solve for unknown forces using Method of Joints
results truss.solvemethodofjoints
if results is not None:
printUnknown forces:"
for member, forces in results.items:
printfmember: forces
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
