-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
232 lines (171 loc) · 6.1 KB
/
main.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
import numpy as np
from scipy.linalg import eigh
import plotly.graph_objects as go
# PROGRAM DE UMA VIGA EULER-BERNOULLI
class BeamEB:
"""Creates a beam element using Euler-Bernoulli theory, considering 2 degrees of freedom per node.
Parameters
----------
L : float
Length of the beam.
h : float
Height of the beam
b : float
Width of the beam.
E : float
Young's modulus.
rho : float
Density.
n_ef : int
Number of finite elements.
Attributes
----------
matrices : tuple
Contains the stiffness and mass global matrices, respectively.
"""
def __init__(self,L,h,b,E,rho,n_ef):
self.L = L
self.h = h
self.b = b
self.n_ef = n_ef
self.n_gdl = 2
self.E = E
self.rho = rho
self.A = b*h
self.I = (b*(h**3))/12
self.L_ef = L / n_ef
self.n_nos = n_ef + 1
self.n_DOF = self.n_gdl * self.n_nos
self.done = False
self.matrices = self.mountMatrices()
def mountMatrices(self):
"""Mounts the Stiffness and Mass global matrices.
Returns
-------
KK : numpy.ndarray
Stiffness global matrix.
MM : numpy.ndarray
Mass global matrix.
"""
mat_connect = np.zeros((self.n_ef,2))
for ii in range(self.n_ef):
mat_connect[ii,:] = [ii,ii+1]
gdl = np.arange(0,self.n_DOF,1) # gdl = 0:1:n_DOF
self.mat_gdl = np.zeros((self.n_nos,self.n_gdl))
for ii in range(self.n_nos):
self.mat_gdl[ii,:] = gdl[:self.n_gdl]
gdl = np.delete(gdl,range(self.n_gdl))
MC = np.zeros((self.n_ef,2*self.n_gdl))
for ii in range(self.n_ef):
MC[ii,:self.n_gdl] = self.mat_gdl[int(mat_connect[ii,0])]
MC[ii,-self.n_gdl:] = self.mat_gdl[int(mat_connect[ii,1])]
KK = np.zeros((self.n_DOF,self.n_DOF))
MM = np.zeros((self.n_DOF,self.n_DOF))
II = np.identity(self.n_DOF)
K = self.K(self.E,self.L_ef,self.I)
M = self.M(self.L_ef)
for ii in range(self.n_ef):
aux = II[MC.astype(int)[ii,:],:]
KK = KK + (aux.T.dot(K)).dot(aux)
MM = MM + (aux.T.dot(M)).dot(aux)
return KK,MM
def K(self,E, L_ef, I):
"""Elemental stiffness matrix
Parameters
----------
E : float
Young's modulus.
L_ef : float
Length of each element.
I : float
Moment of inertia.
Returns
-------
K : numpy.ndarray
Stiffness elemental matrix.
"""
K = np.array([
[ (12*E*I)/(L_ef**3), (6*E*I)/(L_ef**2), -(12*E*I)/(L_ef**3), (6*E*I)/(L_ef**2)],
[ (6*E*I)/(L_ef**2), (4*E*I)/L_ef, -(6*E*I)/(L_ef**2), (2*E*I)/L_ef],
[ -(12*E*I)/(L_ef**3), -(6*E*I)/(L_ef**2), (12*E*I)/(L_ef**3), -(6*E*I)/(L_ef**2)],
[ (6*E*I)/(L_ef**2), (2*E*I)/L_ef, -(6*E*I)/(L_ef**2), (4*E*I)/L_ef],
]
)
return K
def M(self,L_ef):
"""Elemental Mass matrix
Parameters
----------
L_ef : float
Length of each element.
Returns
-------
M : numpy.ndarray
Mass elemental matrix.
"""
M = np.array([
[ (13*L_ef)/35, (11*L_ef**2)/210, (9*L_ef)/70, -(13*(L_ef**2))/420],
[ (11*(L_ef**2))/210, (L_ef**3)/105, (13*(L_ef**2))/420, -(L_ef**3)/140],
[ (9*L_ef)/70, (13*(L_ef**2))/420, (13*L_ef)/35, -(11*(L_ef**2))/210],
[ -(13*(L_ef**2))/420, -(L_ef**3)/140, -(11*(L_ef**2))/210, (L_ef**3)/105],
]
)
return self.rho*self.A*M
def runVibrationModes(self):
"""Executes the calculus to find the vibration modes of the beam.
Paramters
---------
plot : bool, optional
Set it False not to plot the vibration modes, and True to do it. Defaults is True.
Returns
-------
modo : numpy.ndarray
The matrix with the vibration modes of the beam. Shape is number of elements per vibration modes.
"""
KK = self.matrices[0]
MM = self.matrices[1]
noE = 0
ddlE = np.array([np.arange(0,self.n_gdl)],dtype=int)
posE = self.mat_gdl[noE,ddlE].astype(int)
MM = np.delete(MM,posE,0)
MM = np.delete(MM,posE,1)
KK = np.delete(KK,posE,0)
KK = np.delete(KK,posE,1)
# Análise dos modos
modo = self.solveProblem(KK,MM)
self.done = True
self.modo = modo
print("\n Modos de Vibrar calculados! \n")
return modo
def solveProblem(self,KK,MM):
"""Executes the solution the eigenvalues and eigenvector's problem.
Parameters
----------
KK : numpy.ndarray
Stiffness global matrix.
MM : numpy.ndarray
Mass global matrix.
Returns
-------
modo : numpy.ndarray
Eigenvectors matrix.
"""
U, D = eigh(KK,MM)
v = np.arange(0,len(D[:,0]),self.n_gdl)
modo = D[v.astype(int),:]
modo = modo/modo[-1]
return modo
def plot(self, qtd_modos=5):
if self.done:
fig = go.Figure()
Lo = np.arange(0,self.L,self.L_ef)
for mode in range(qtd_modos):
fig.add_trace(go.Scatter(x=Lo,y=self.modo[:,mode],name=f"{mode + 1} modo de vibrar", showlegend=True))
fig.update_layout(title="Viga de Euler-Bernoulli e seus modos de vibrar")
fig.show()
else:
raise Exception("You need to run the vibration modes before plotting!")
if __name__ == '__main__':
viga1 = BeamEB(0.35,0.02,0.06,7e10,2780,100)
modo = viga1.runVibrationModes()
viga1.plot()