Python Script For Static Deflection of A Beam Using Finite Elements - Mechanics and Machines
Python Script For Static Deflection of A Beam Using Finite Elements - Mechanics and Machines
FEA, FUNDAMENTALS
Below we present a simple script for calculating the static de ection of a beam with a variety of
boundary conditions and load types. The nite element method is implemented using Python with the
numpy library and plot are made using matplotlib. This code can be easily modi ed for other boundary
conditions or loads.
www.mechanicsandmachines.com/?p=705 1/5
8/22/2020 Python script for static deflection of a beam using finite elements | Mechanics and Machines
K=np.zeros((2*(N+1),2*(N+1)))
# M=zeros(2*(N+1),2*(N+1))
for i in range(1,N+1):
K[(2*(i-1)+1-1):(2*(i-1)+4), (2*(i-1)+1-1):(2*(i-1)+4)]=K[(2*(i-1)+1-1):(2*(i-
1)+4)][:,(2*(i-1)+1-1):(2*(i-1)+4)]+K_e
f=np.zeros((2*Nnodes,1))
if loadcase == 'pointforce':
print('Load: concentrated force at x=L/2')
#load: concentrated force on center of beam
f[Nnodes-1]=1
elif loadcase == 'pointmoment':
print('Load: concentrated moment at x=L/2')
#load: concentrated moment on center of beam
f[Nnodes]=1
elif loadcase == 'uniformdistribmoment':
print('Load: uniformly distributed moment')
#load: uniform distributed moment on entire beam
m=1.0
m_el=np.zeros((4,1))
m_el[:,0] = np.array([-m,0,m,0])
for i in range(1,N+1):
f[2*(i-1)+1-1:2*(i-1)+4]=f[2*(i-1)+1-1:2*(i-1)+4]+m_el
elif loadcase == 'concmomenteachnode':
www.mechanicsandmachines.com/?p=705 2/5
8/22/2020 Python script for static deflection of a beam using finite elements | Mechanics and Machines
if bcs == 'clamped-clamped':
print('BCs: clamped-clamped')
#clamped-clamped beam BCs
K=np.delete(K,[2*Nnodes-2,2*Nnodes-1],0) #K[1:2,:]=[]
K=np.delete(K,[2*Nnodes-2,2*Nnodes-1],1) #K[:,1:2]=[]
f=np.delete(f,[2*Nnodes-2,2*Nnodes-1]) #f[1:2]=[]
K=np.delete(K,[0,1],0) #K[1:2,:]=[]
K=np.delete(K,[0,1],1) #K[:,1:2]=[]
f=np.delete(f,[0,1]) #f[1:2]=[]
www.mechanicsandmachines.com/?p=705 3/5
8/22/2020 Python script for static deflection of a beam using finite elements | Mechanics and Machines
dx_vec=np.linalg.solve(K,f)
if bcs == 'clamped-clamped':
print('BCs output: clamped-clamped')
#clamped-clamped
dx=np.hstack([0., dx_vec[0:2*Nnodes-5:2], 0.])
dtheta=np.hstack([0., dx_vec[1:2*Nnodes-4:2], 0.])
elif bcs == 'simplysupported':
print('BCs output: simply-supported')
#simply-supported
dx=np.hstack([0., dx_vec[1:2*Nnodes-4:2], 0.])
dtheta=np.hstack([dx_vec[0:2*Nnodes-3:2], dx_vec[2*Nnodes-3]])
elif bcs == 'cantilever':
print('BCs output: cantilever')
#cantilever
dx=np.hstack([0., dx_vec[0:2*Nnodes-2:2]])
dtheta=np.hstack([0., dx_vec[1:2*Nnodes-1:2]])
elif bcs == 'clamped-sliding':
print('BCs output: clamped-sliding')
#clamped-sliding beam BCs
dx=np.hstack([0., dx_vec[0:2*Nnodes-3:2]])
dtheta=np.hstack([0., dx_vec[1:2*Nnodes-4:2], 0.])
else:
print('Output: Unknown boundary conditions.')
plt.figure(1)
plt.subplot(211)
www.mechanicsandmachines.com/?p=705 4/5
8/22/2020 Python script for static deflection of a beam using finite elements | Mechanics and Machines
plt.plot(x,dx)
plt.ylabel('displacement [m]')
#plt.title(['BCs: ' bcs 'Load case: ' loadcase])
plt.show()
plt.subplot(212)
plt.plot(x,dtheta)
plt.ylabel('slope [radians]')
plt.xlabel('X [m]')
BEAM BENDING FEA FINITE ELEMENT MATPLOTLIB NUMPY PYTHON SCIPY SCRIPT
STATIC DEFLECTION
www.mechanicsandmachines.com/?p=705 5/5