-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleBeamSquareTE3.py
562 lines (420 loc) · 15.9 KB
/
SimpleBeamSquareTE3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 28 09:29:38 2019
@author: NAOMI
"""
import numpy as np
from numpy import linalg as LA
import pylab as pl
import math as Ma
import scipy.integrate as integ
import scipy.linalg as LAS
from sympy import Symbol
from numpy import matrix
import time
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
from scipy import sparse
from scipy.linalg import eig, eigh
from scipy.sparse.linalg import eigs,eigsh
"""Datas"""
bnod=3 #bnod is what in the previous sections has been referred as Nn, that is to say the number of nodes for each beam element
nF=10 #nF is what in the previous sections has been referres as M, that is to say the number of terms for each cross section expansion F_\tau
ne_x=20 #number of longitudinal elements
nn_x=(bnod-1)*ne_x+1 #number of longitudinal nodes
h=1 #edge of the cross-section beam. I suppose a square cross-section
L=20 #longitudinal length of the beam
E=75*(10**9) #Young's Module
tau=0.33 #Poisson ratio
G=E/(2*(1+tau)) #Shear Elastic Constant
rho=2700 #density of material, I have supposed Alluminium
le=L/ne_x #length of each beam element--> I have used a uniform longitudinal mesh
"""Cx= matrix where the i-th raw has the global index of i-th beam element's longitudinal nodes"""
Cx=[]
for i in range(0,ne_x):
Cx.append([])
Cx[i].append(i*2)
Cx[i].append(i*2+1)
Cx[i].append(i*2+2)
# If, for example, bnod was equal to 2-->
#Cx=[]
#for i in range(0,ne_x):
# Cx.append([])
# Cx[i].append(i)
# Cx[i].append(i+1)
"""Matrix of elasticity for an isotropic material"""
S=np.array([[1/E,-tau/E,-tau/E,0,0,0],[-tau/E,1/E,-tau/E,0,0,0],[-tau/E,-tau/E,1/E,0,0,0],[0,0,0,1/G,0,0],[0,0,0,0,1/G,0],[0,0,0,0,0,1/G]])
C=np.linalg.inv(S)
F=[] #will contain the functions Ftau and its partial derivatives
Kel=[] #will be the stiffness matrix of a beam element
N=[] #will contain the interpolatin function along the longitudinal axis
"""Let's build the nucleus stiffeness matrix"""
#This funtion will provide the function to be integrated over the beam element volume
def integrand(z,y,x,c,b,n,a,k,p):
global F
F.clear()
#Functions Fi
F1=1
F2=y
F3=z
F4=y**2
F5=y*z
F6=z**2
F7=y**3
F8=(y**2)*z
F9=y*(z**2)
F10=z**3
#Partial derivatives of Fi
F1y=0
F1z=0
F2y=1
F2z=0
F3y=0
F3z=1
F4y=2*y
F4z=0
F5y=z
F5z=y
F6y=0
F6z=2*z
F7y=3*(y**2)
F7z=0
F8y=2*y*z
F8z=y**2
F9y=z**2
F9z=2*y*z
F10y=0
F10z=3*(z**2)
F.append(F1)
F.append(F1y)
F.append(F1z)
F.append(F2)
F.append(F2y)
F.append(F2z)
F.append(F3)
F.append(F3y)
F.append(F3z)
F.append(F4)
F.append(F4y)
F.append(F4z)
F.append(F5)
F.append(F5y)
F.append(F5z)
F.append(F6)
F.append(F6y)
F.append(F6z)
F.append(F7)
F.append(F7y)
F.append(F7z)
F.append(F8)
F.append(F8y)
F.append(F8z)
F.append(F9)
F.append(F9y)
F.append(F9z)
F.append(F10)
F.append(F10y)
F.append(F10z)
N.clear()
# if bnod=2--> decomment the following 4 raws
# N.append((1/2)*(1-(x/(le/2))))
# N.append(-1/(le))
# N.append((1/2)*(1+(x/(le/2))))
# N.append(1/(le))
# if bnod=3--> decomment the following 7 raws
eta=x/(le/2)
N.append((1/2)*eta*(eta-1))
N.append((1/le)*(2*eta-1))
N.append(+(1+eta)*(1-eta))
N.append(-(4/le)*eta)
N.append((1/2)*eta*(eta+1))
N.append((1/le)*(2*eta+1))
# if bnod=4--> decomment the following 9 raws
# eta=x/(le/2)
# N.append((-9/16)*(eta+1/3)*(eta-1/3)*(eta-1))
# N.append((-9*2/(16*le))*((1)*(eta-1/3)*(eta-1)+(eta+1/3)*(1)*(eta-1)+(eta+1/3)*(eta-1/3)*(1)))
#
# N.append((27/16)*(eta+1)*(eta-1/3)*(eta-1))
# N.append((27*2/(16*le))*((1)*(eta-1/3)*(eta-1)+(eta+1)*(1)*(eta-1)+(eta+1)*(eta-1/3)*(1)))
#
# N.append((-27/16)*(eta+1)*(eta+1/3)*(eta-1))
# N.append((-27*2/(16*le))*((1)*(eta+1/3)*(eta-1)+(eta+1)*(1)*(eta-1)+(eta+1)*(eta+1/3)*(1)))
#
# N.append((9/16)*(eta+1/3)*(eta-1/3)*(eta+1))
# N.append((9*2/(16*le))*((1)*(eta-1/3)*(eta+1)+(eta+1/3)*(1)*(eta+1)+(eta+1/3)*(eta-1/3)*(1)))
#
##
return N[c]*F[b]*C[n][a]*F[k]*N[p]
#this function will perform the integral of the function integrand over the beam element
def integral(c,b,n,a,k,p):
return (integ.tplquad(integrand,-le/2,le/2,lambda x: -h/2, lambda x: h/2,lambda x,y: -h/2, lambda x,y:h/2, args=(c,b,n,a,k,p),epsabs=1, epsrel=1)[0])
def integraltot():
global Kel
Kel=np.zeros((bnod*nF*3,bnod*nF*3))
for q in range(0,bnod): #loop over the nodes of a longitudinal element
for o in range(0,bnod): #loop over the nodes of a longitudinal element
for w in range(0,nF): #loop over the terms of transverse expansion over the cross-section
for m in range(0,nF): #loop over the terms of transverse expansion over the cross-section
Kel[q*nF*3+w*3][o*nF*3+m*3]=integral(q*2+1,w*3,0,0,m*3,o*2+1)+integral(q*2,w*3+1,5,5,m*3+1,o*2)+integral(q*2,w*3+2,3,3,m*3+2,o*2)
Kel[q*nF*3+w*3][o*nF*3+m*3+1]=integral(q*2+1,w*3,0,1,m*3+1,o*2)+integral(q*2,w*3+1,5,5,m*3,o*2+1)
Kel[q*nF*3+w*3][o*nF*3+m*3+2]=integral(q*2+1,w*3,0,2,m*3+2,o*2)+integral(q*2,w*3+2,3,3,m*3,o*2+1)
Kel[q*nF*3+w*3+1][o*nF*3+m*3]=integral(q*2,w*3+1,1,0,m*3,o*2+1)+integral(q*2+1,w*3,5,5,m*3+1,o*2)
Kel[q*nF*3+w*3+1][o*nF*3+m*3+1]=integral(q*2+1,w*3,5,5,m*3,o*2+1)+integral(q*2,w*3+1,1,1,m*3+1,o*2)+integral(q*2,w*3+2,4,4,m*3+2,o*2)
Kel[q*nF*3+w*3+1][o*nF*3+m*3+2]=integral(q*2,w*3+1,1,2,m*3+2,o*2)+integral(q*2,w*3+2,4,4,m*3+1,o*2)
Kel[q*nF*3+w*3+2][o*nF*3+m*3]=integral(q*2,w*3+2,2,0,m*3,o*2+1)+integral(q*2+1,w*3,3,3,m*3+2,o*2)
Kel[q*nF*3+w*3+2][o*nF*3+m*3+1]=integral(q*2,w*3+2,2,1,m*3+1,o*2)+integral(q*2,w*3+1,4,4,m*3+2,o*2)
Kel[q*nF*3+w*3+2][o*nF*3+m*3+2]=integral(q*2+1,w*3,3,3,m*3,o*2+1)+integral(q*2,w*3+1,4,4,m*3+1,o*2)+integral(q*2,w*3+2,2,2,m*3+2,o*2)
return
"""Finally I will call function integraltot() to build up matrix Kel"""
integraltot()
""" Now I build the mass matrix """
Mel=[]
def Mintegrand(z,y,x,c,b,k,p):
global F
global N
F.clear()
F1=1
F2=y
F3=z
F4=y**2
F5=y*z
F6=z**2
F6=z**2
F7=y**3
F8=(y**2)*z
F9=y*(z**2)
F10=z**3
F.append(F1)
F.append(F2)
F.append(F3)
F.append(F4)
F.append(F5)
F.append(F6)
F.append(F7)
F.append(F8)
F.append(F9)
F.append(F10)
N.clear()
#decomment the following two raws if bnod=2
# N.append((1/2)*(1-(x/(le/2))))
# N.append((1/2)*(1+(x/(le/2))))
#
#decomment the following 5 raws if bnod=4
# eta=x/(le/2)
# N.append((-9/16)*(eta+1/3)*(eta-1/3)*(eta-1))
# N.append((27/16)*(eta+1)*(eta-1/3)*(eta-1))
# N.append((-27/16)*(eta+1)*(eta+1/3)*(eta-1))
# N.append((9/16)*(eta+1/3)*(eta-1/3)*(eta+1))
#
#decomment the following 4 raws if bnod=3
eta=x/(le/2)
N.append((1/2)*eta*(eta-1))
N.append(+(1+eta)*(1-eta))
N.append((1/2)*eta*(eta+1))
return N[c]*F[b]*F[k]*N[p]*rho
def Mintegral(dec,c,b,k,p):
return (integ.tplquad(Mintegrand,-le/2,le/2,lambda x: -h/2, lambda x: h/2,lambda x,y: -h/2, lambda x,y:h/2, args=(c,b,k,p),epsabs=1.49, epsrel=1.49)[0])
def Mintegraltot():
global Mel
Mel=np.zeros((bnod*nF*3,bnod*nF*3))
for q in range(0,bnod):
for o in range(0,bnod):
for w in range(0,nF):
for m in range(0,nF):
Mel[q*nF*3+w*3][o*nF*3+m*3]=Mintegral(0,q,w,m,o)
Mel[q*nF*3+w*3+1][o*nF*3+m*3+1]=Mintegral(0,q,w,m,o)
Mel[q*nF*3+w*3+2][o*nF*3+m*3+2]=Mintegral(0,q,w,m,o)
Mintegraltot()
np.array(Mel)
np.array(Kel)
""" Now I have to build the final stiffness and Mass matrix """
K=np.zeros((nn_x*nF*3,nn_x*nF*3))
M=np.zeros((nn_x*nF*3,nn_x*nF*3))
""" Now I will assembly the element stiffness matrix Kel """
for el in range(0,ne_x):
for k in range (0,bnod):
for s in range(0,bnod):
for i in range(0,nF):
for j in range(0,nF):
for m in range(0,3):
for r in range(0,3):
K[(Cx[el][k])*nF*3+i*3+m][(Cx[el][s])*nF*3+j*3+r]=K[(Cx[el][k])*nF*3+i*3+m][(Cx[el][s])*nF*3+j*3+r]+Kel[k*nF*3+i*3+m][s*nF*3+j*3+r]
""" Now I will assembly the element mass matrix Mel """
for el in range(0,ne_x):
for k in range (0,bnod):
for s in range(0,bnod):
for i in range(0,nF):
for j in range(0,nF):
for m in range(0,3):
for r in range(0,3):
M[(Cx[el][k])*nF*3+i*3+m][(Cx[el][s])*nF*3+j*3+r]=M[(Cx[el][k])*nF*3+i*3+m][(Cx[el][s])*nF*3+j*3+r]+Mel[k*nF*3+i*3+m][s*nF*3+j*3+r]
eigv=[]
omega2=[]
omega=[]
sK=sparse.csr_matrix(K)
sM=sparse.csr_matrix(M)
[omega2,eigv]=eigsh(sK,50,sM,which='SM') #to obrain the first 50 omega_i^2
for i in range(0,len(omega2)):
if -0.001<omega2[i]<0.001:
omega.append(Ma.sqrt(np.abs(np.real(omega2[i]))))
else:
if omega2[i]>=0:
omega.append(Ma.sqrt(np.real(omega2[i])))
freq=[]
for i in range(0,len(omega)):
freq.append(omega[i]/(2*Ma.pi)) #vector with the fist 50 frequencies
"""The following functions have been created to represent the eigenvectors"""
def Func(w,yq,zq):
F.clear()
F1=1
F2=yq
F3=zq
F4=yq**2
F5=yq*zq
F6=zq**2
F7=yq**3
F8=(yq**2)*zq
F9=yq*(zq**2)
F10=zq**3
F.append(F1)
F.append(F2)
F.append(F3)
F.append(F4)
F.append(F5)
F.append(F6)
F.append(F7)
F.append(F8)
F.append(F9)
F.append(F10)
return F[w]
def Nfunc(q,xq):
N.clear()
#if bnod=2--> decomment the following two raws
# N.append((1/2)*(1-(xq/(le/2))))
# N.append((1/2)*(1+(xq/(le/2))))
#if bnod=4--> decomment the following 5 raws
# etaq=xq/(le/2)
# N.append((-9/16)*(etaq+1/3)*(etaq-1/3)*(etaq-1))
# N.append((27/16)*(etaq+1)*(etaq-1/3)*(etaq-1))
# N.append((-27/16)*(etaq+1)*(etaq+1/3)*(etaq-1))
# N.append((9/16)*(etaq+1/3)*(etaq-1/3)*(etaq+1))
#if bnod=3--> decomment the following 4 raws
etaq=xq/(le/2)
N.append((1/2)*etaq*(etaq-1))
N.append(+(1+etaq)*(1-etaq))
N.append((1/2)*etaq*(etaq+1))
return N[q]
# this function will provide the index in eigv of the nmin-th frequency
def findindex(nmin):
minfreq=[]
for i in range(0,len(freq)):
minfreq.append(freq[i])
for i in range(0,nmin-1):
del minfreq[minfreq.index(min(minfreq))]
print(freq[freq.index(min(minfreq))])
return freq.index(min(minfreq))
"""This function will represent the nm-th first eigenvector with a factor scale equal to scale"""
def plot3dmode(nm,scale):
yk=np.array(list(np.arange(-h/2,h/2+0.05,0.05)))
zk=np.array(list(np.arange(-h/2,h/2+0.05,0.05)))
xk=np.array(list(np.arange(0,L+0.1,0.1)))
X=[]
Y=[]
Z=[]
um=[]
for i in range(0,nn_x*nF*3):
um.append(eigv[i][nm])
for i in range(0,len(yk)):
for j in range(0,len(zk)):
for k in range(0,len(xk)-1):
if -h/2<=yk[i]<=h/2 and -h/2<=zk[j]<=h/2:
uy=0
uz=0
ux=0
q=int(xk[k]/le)
for n in range(0,bnod):
for f in range(0,nF):
uy=uy+Nfunc(n,xk[k]-q*le-le/2)*Func(f,yk[i],zk[j])*um[q*(bnod-1)*(nF)*3+n*(nF)*3+f*3+1]
uz=uz+Nfunc(n,xk[k]-q*le-le/2)*Func(f,yk[i],zk[j])*um[q*(bnod-1)*(nF)*3+n*(nF)*3+f*3+2]
ux=ux+Nfunc(n,xk[k]-q*le-le/2)*Func(f,yk[i],zk[j])*um[q*(bnod-1)*(nF)*3+n*(nF)*3+f*3+0]
X.append((xk[k]+ux*scale))
Y.append((yk[i]+uy*scale))
Z.append((zk[j]+uz*scale))
uy=0
uz=0
ux=0
for n in range(0,bnod):
for f in range(0,nF):
uy=uy+Nfunc(n,xk[len(xk)-1]-q*le-le/2)*Func(f,yk[i],zk[j])*um[q*(bnod-1)*(nF)*3+n*(nF)*3+f*3+1]
uz=uz+Nfunc(n,xk[len(xk)-1]-q*le-le/2)*Func(f,yk[i],zk[j])*um[q*(bnod-1)*(nF)*3+n*(nF)*3+f*3+2]
ux=ux+Nfunc(n,xk[len(xk)-1]-q*le-le/2)*Func(f,yk[i],zk[j])*um[q*(bnod-1)*(nF)*3+n*(nF)*3+f*3+0]
X.append((xk[k]+ux*scale))
Y.append((yk[i]+uy*scale))
Z.append((zk[j]+uz*scale))
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(Y,X,Z,',',c=X)
ax.set_title('Deformation');
ax.set_xlabel('Y Label')
ax.set_ylabel('X Label')
ax.set_zlabel('Z Label')
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(X, Y, Z,',',c=X)
ax.set_title('Deformation');
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(Y,Z,X,',',c=X)
ax.set_title('Deformation');
ax.set_xlabel('Y Label')
ax.set_ylabel('Z Label')
ax.set_zlabel('X Label')
plt.show()
return
def plotundeformed():
yk=np.array(list(np.arange(-h/2,h/2+0.05,0.05)))
zk=np.array(list(np.arange(-h/2,h/2+0.05,0.05)))
xk=np.array(list(np.arange(0,L,0.1)))
X=[]
Y=[]
Z=[]
for i in range(0,len(yk)):
for j in range(0,len(zk)):
for k in range(0,len(xk)):
if -h/2<=yk[i]<=h/2 and -h/2<=zk[j]<=h/2:
uy=0
uz=0
ux=0
X.append((xk[k]+ux))
Y.append((yk[i]+uy))
Z.append((zk[j]+uz))
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(Y,X,Z,',',c=X)
ax.set_title('Deformation');
ax.set_xlabel('Y Label')
ax.set_ylabel('X Label')
ax.set_zlabel('Z Label')
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(X, Y, Z,',',c=X)
ax.set_title('Deformation');
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(Y,Z,X,',',c=X)
ax.set_title('Deformation');
ax.set_xlabel('Y Label')
ax.set_ylabel('Z Label')
ax.set_zlabel('X Label')
plt.show()
return