forked from Modelizacion-de-Materiales/finel3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinel.py
344 lines (293 loc) · 11.4 KB
/
finel.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
import gmesh_handler as gmesh
import finite_elements as fe
import numpy as np
from scipy.linalg import eigh
import boundaries_handler as boundaries
from pyBST import BST
import os
import subprocess
import sys
import platform
from consolemenu import *
from consolemenu.items import *
import pdb
import matplotlib.pyplot as plt
import time
with open('gmsh_path', 'r') as file:
gmsh_path = file.readline().rstrip()
if 'Linux' in platform.system():
os.system('clear')
elif 'Darwin' in platform.system():
os.system('clear')
else:
os.system('cls')
if sys.argv[1] == "-f":
print("Opening {}".format(sys.argv[2]))
file_name = sys.argv[2]
elif sys.argv[1] == "-v":
#file = gmesh.read(sys.argv[2])
#file.read_nodes()
#elem = file.find_elements(ElementType=2)
#file.BST_config()
#subprocess.Popen('./bst')
print('Feature not available')
exit()
elif sys.argv[1] == "-g":
if 'Linux' in platform.system():
os.system('/usr/local/bin/gmsh -open ./mesh/'+sys.argv[2])
elif 'Darwin' in platform.system():
os.system('/Applications/Gmsh.app/Contents/MacOS/gmsh -open ./mesh/'+sys.argv[2])
else:
subprocess.Popen(gmsh_path + "/gmsh.exe -open ./mesh/"+sys.argv[2])
exit()
elif sys.argv[1] == "-c":
file_name = sys.argv[2]
while True:
answer = input("Clean file? (y/n): ")
if answer == 'y':
file = gmesh.read(sys.argv[2])
file.clean()
print("Done.")
exit()
if answer =='n':
exit()
else:
file_name = "mesh"
menu_list=["1D Element (Line)", "2D Element (Triangle)", "3D Element (Tetrahedron)", "Beams"]
menu = SelectionMenu(menu_list,"Finite Elements 1.0 - Select a mesh option")
menu.show()
menu.join()
selection = menu.selected_option
if selection == 0:
#DOF_nodes = 1
ELEM_type = 1
menu_list=["1D Solution (1 DOF)", "2D Solution (2 DOF)", "3D Solution (3 DOF)", " - "]
elif selection == 1:
#DOF_nodes = 2
ELEM_type = 2
menu_list=[" - ", "2D Solution (2 DOF)", "3D Solution (3 DOF)", " - "]
elif selection == 2:
ELEM_type = 4
menu_list=[" - ", " - ", "3D Solution (3 DOF)", "Normal mode"]
elif selection == 3:
#DOF_nodes = 2
ELEM_type = 5
menu_list=[" - ", "2D Solution (2 DOF)", "3D Solution (3 DOF)", "Normal mode"]
elif selection == 4:
exit()
#menu_list=["1D Solution (1 DOF)", "2D Solution (2 DOF)", "3D Solution (3 DOF)"]
menu = SelectionMenu(menu_list,"Finite Elements 1.0 - Select the degree of fredoom per node")
menu.show()
menu.join()
selection2 = menu.selected_option
if selection2 == 0:
DOF_nodes = 1
#ELEM_type = 1
solve = 'Stress-Strain'
elif selection2 == 1:
DOF_nodes = 2
#ELEM_type = 2
solve = 'Stress-Strain'
elif selection2 == 2:
DOF_nodes = 3
#ELEM_type = 4
solve = 'Stress-Strain'
elif selection2 == 3:
if selection == 2:
DOF_nodes = 3
if selection == 3:
DOF_nodes = 2
solve = 'Normal-Modes'
if solve == 'Stress-Strain':
##########################################################################################################
# Solucion tension-deformacion
##########################################################################################################
file = gmesh.read(file_name)
nodes_mat = file.read_nodes()
if ELEM_type == 5:
# Las vigas se buscan como elementos tipo 1 en gmsh
elem = file.find_elements(ElementType=1)
else:
# El resto de los elementos se buscan de manera normal
elem = file.find_elements(ElementType=ELEM_type)
#file.BST_config()
#print('Opening Boundaries Selection Tool...')
#print('BST disabled!')
#subprocess.call('./bst')
print('Opening pyBST...')
print("Please generate boundary condition file!")
print()
print('physical type, to set boundaries conditions')
print('1: line 2: surf 4: volume')
PHY_type = input('> ')
BST(file_name=file_name, num_of_nodes = len(nodes_mat), elem_type=int(PHY_type))
while True:
answer = input("Continue? (q to quit)")
if answer == 'y':
break
if answer =='q':
exit()
else:
break
print('Reading boundaries file')
boundaries = boundaries.read('nodes_selection_file.dat')
f_elem = fe.finite_elements(nodes=nodes_mat, elements=elem, dof_per_node=DOF_nodes)
if ELEM_type == 5:
print('Getting beams areas')
a = f_elem.get_areas(ElementType=1, areas=1e-3)
else:
pass
#boundaries = boundaries.read('nodes_selection_file.dat')
#lines = file.find_elements(ElementType=1)
#Lineas con el physical group
physical_lines = file.find_physical_elements(ElementType=int(PHY_type))
nodes = boundaries.get_boundaries()
r, s = boundaries.get_r_s(nodes, dof_per_node=DOF_nodes)
print()
forces = {}
PS = physical_lines[0][0]
new_force = input('Forces in Physical Group {}: '.format(physical_lines[0][0]))
forces[physical_lines[0][0]] = float(new_force)
for i in range(1,len(physical_lines)):
if physical_lines[i][0] in forces:
pass
else:
new_force = input('Forces in Physical Group {}: '.format(physical_lines[i][0]))
forces[physical_lines[i][0]] = float(new_force)
PS = physical_lines[i][0]
print()
print('Physical Group : |Force|')
print(forces)
print()
print('Solving...')
F = boundaries.get_stress(nodes_mat, physical_lines, forces, nodes, ElementType=ELEM_type, dof_per_node=DOF_nodes)
f_elem = fe.finite_elements(nodes=nodes_mat, elements=elem, dof_per_node=DOF_nodes)
a = f_elem.get_areas(ElementType=ELEM_type, areas=1)
mat_rig = f_elem.get_stiff_mat(ElementType=ELEM_type)
K = f_elem.get_global_mat(ElementType=ELEM_type, matrix_to_assemble=mat_rig)
D = np.zeros(f_elem.dof_per_node*len(nodes)).reshape(-1,1)
F, D = f_elem.solve_for(K, F, D, r, s)
file.clean()
if ELEM_type == 2:
#f_elem.get_stress(ELEM_type)
tensiones = f_elem.get_principal_stress('max', ElementType=2)
#Cuenta los elementos nuevamente para que las tensiones se escriban correctamente
countElements = file.find_elements(ElementType=2)
file.write(D,'Desplazamientos(m)',F,'Fuerzas(N)', tensiones, 'Tensiones(psi)', dof_per_node=2)
elif ELEM_type == 4:
#f_elem.get_stress()
tensiones = f_elem.get_principal_stress('max', ElementType=4)
#Cuenta los elementos nuevamente para que las tensiones se escriban correctamente
countElements = file.find_elements(ElementType=4)
file.write(D,'Desplazamientos(m)',F,'Fuerzas(N)', tensiones, 'Tensiones(psi)', dof_per_node=3)
elif ELEM_type == 1:
file.write(D,'Desplazamientos(m)',F,'Fuerzas(N)', F, 'Fuerzas(N)', DOF_nodes)
elif ELEM_type == 5:
file.write(D,'Desplazamientos(m)',F,'Fuerzas(N)', F, 'Fuerzas(N)', DOF_nodes)
while True:
answer = input("Open gmsh? (y/n): ")
if answer == 'y':
if 'Linux' in platform.system():
os.system('/usr/local/bin/gmsh -open ./mesh/'+ sys.argv[2])
elif 'Darwin' in platform.system():
os.system('/Applications/Gmsh.app/Contents/MacOS/gmsh -open ./mesh/'+sys.argv[2])
else:
subprocess.Popen(gmsh_path + "/gmsh.exe -open ./mesh/"+sys.argv[2])
break
if answer =='n':
exit()
##########################################################################################################
# Solucion Modos Normales de vibracion
##########################################################################################################
elif solve == 'Normal-Modes':
# ElementType == 5 ! ojo que para gmsh son elementos tipo 1
file = gmesh.read(file_name)
nodes_mat = file.read_nodes()
if ELEM_type == 5:
print('Using element 1 as Beam')
elem = file.find_elements(ElementType=1)
else:
elem = file.find_elements(ElementType=ELEM_type)
# BST! para los gl empotrados
#file.BST_config()
#print('Opening Boundaries Selection Tool...')
#subprocess.call('./bst')
print('Opening pyBST...')
print("Please generate boundary condition file!")
print()
print('physical type, to set boundaries conditions')
print('1: line 2: surf 4: volume')
PHY_type = input('> ')
BST(file_name=file_name, num_of_nodes = len(nodes_mat), elem_type=int(PHY_type))
while True:
answer = input("Continue? (q to quit)")
if answer == 'y':
break
if answer =='q':
exit()
else:
break
print('Reading boundaries file')
boundaries = boundaries.read('nodes_selection_file.dat')
f_elem = fe.finite_elements(nodes=nodes_mat, elements=elem, dof_per_node=DOF_nodes)
if ELEM_type == 5:
print('Getting beams areas')
a = f_elem.get_areas(ElementType=1, areas=1e-3)
else:
pass
# matriz de rigidez
mat_rig = f_elem.get_stiff_mat(ElementType=ELEM_type)
K_glob = f_elem.get_global_mat(ElementType=ELEM_type, matrix_to_assemble=mat_rig)
#matriz de masa reducida
#np.savetxt('rigidez_global.txt',K_glob,fmt='%.2f')
if ELEM_type == 5:
red_mass = f_elem.mass_matrix(ElementType=ELEM_type, matrix_type='consistent', density=7850)
m_glob = f_elem.get_global_mat(ElementType=ELEM_type, matrix_to_assemble=red_mass)
if ELEM_type == 4:
mass_mat = f_elem.mass_matrix(ElementType=ELEM_type, matrix_type='consistent', density=7850/1e9)
m_glob = f_elem.get_global_mat(ElementType=ELEM_type, matrix_to_assemble=mass_mat)
#condiciones de contorno del problema
nodes = boundaries.get_boundaries()
r, s = boundaries.get_r_s(nodes, dof_per_node=DOF_nodes)
#calculo de autovalores
print('solving...')
start_time = time.time()
autoval, autov = eigh(K_glob[np.ix_(r,r)], m_glob[np.ix_(r,r)])
print("Solved in %s seconds" % (time.time() - start_time))
print('Done')
#ans= input('Modes to graph: ')
freq = np.sqrt(autoval) / (2*np.pi)
disp = np.zeros(DOF_nodes*len(nodes))
# Imprime las frecuencias de los modos normales
#MDF-COMMENT supongo que aca elegis el modo que escribis
#MDF-COMMENT num_freq = 2
max_frec = min(4, autov.shape[1])
print("Saving {:d} first modes...".format(max_frec))
#MDF-COMMENT print('Frequency: {}'.format(freq[0:6]))
print('Frequency: {}'.format(freq[0:max_frec]))
#print(str(np.round(freq[num_freq], 1)) + ' Hz')
#print()
#pdb.set_trace()
file.clean()
for num_freq in range(max_frec):
print('saving mode {:d}'.format(num_freq))
for i in range(len(r)):
disp[r[i]] = autov[i, num_freq]
if ELEM_type == 4:
file.clip_write(
disp,
'Desplazam. MAX {:d}'.format(num_freq),
dof_per_node=DOF_nodes
)
while True:
answer = input("Open gmsh? (y/n): ")
if answer == 'y':
if 'Linux' in platform.system():
os.system('/usr/local/bin/gmsh -open ./mesh/'+ sys.argv[2])
elif 'Darwin' in platform.system():
os.system('/Applications/Gmsh.app/Contents/MacOS/gmsh -open ./mesh/'+sys.argv[2])
else:
subprocess.Popen(gmsh_path + "/gmsh.exe -open ./mesh/"+sys.argv[2])
break
if answer =='n':
exit()