-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocessing.py
679 lines (473 loc) · 19.9 KB
/
preprocessing.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import scipy.io
import numpy as np
import pandas as pd
import csv
from csv import reader
import itertools
from numpy import genfromtxt
def adj_matrix_builder(elements, num_nodes):
'''
:param elements: a .csv file which contains the elements of the FE model.
:param num_nodes: number of nodes.
:return: the adjacency matrix.
'''
A = np.zeros((num_nodes, num_nodes))
with open(elements, 'r') as read_obj: # pass the file object to reader() to get the reader object
csv_reader = reader(read_obj) # Iterate over each row in the csv using reader object
for row in csv_reader: # row variable is a list that represents a row in csv
node_1 = int(row[0]) - 1
node_2 = int(row[1]) - 1
node_3 = int(row[2]) - 1
node_4 = int(row[3]) - 1
A[node_1, node_2] = 1
A[node_2, node_1] = 1
A[node_1, node_3] = 1
A[node_3, node_1] = 1
A[node_1, node_4] = 1
A[node_4, node_1] = 1
A[node_2, node_3] = 1
A[node_3, node_2] = 1
A[node_2, node_4] = 1
A[node_4, node_2] = 1
A[node_3, node_4] = 1
A[node_4, node_3] = 1
return A
# --------------------------------------------------------------------------------------------------
def adj_matrix_format(A, filename):
'''
:param A: an adjacency matrix
:param filename: the name of the file where the formatted adjacency matrix will be stored.
:return: -
'''
num_rows = np.size(A,0)
num_cols = np.size(A,1)
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
for i in range(num_rows):
for j in range(num_cols):
list = []
if A[i,j]!=0:
list.append(j+1)
list.append(i+1)
writer.writerow(list)
# --------------------------------------------------------------------------------------------------
def adj_matrix_full_format(A, num_nodes, num_dir, t_steps, filename):
'''
:param A: an adjacency matrix
:param filename: the name of the file where the formatted adjacency matrix will be stored.
:return: -
'''
num_rows = np.size(A,0)
num_cols = np.size(A,1)
counter = 0
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
for k in range(num_dir*t_steps):
for i in range(num_rows):
for j in range(num_cols):
list = []
if A[i,j]!=0:
list.append(j+1 + (k * num_nodes))
list.append(i+1 + (k * num_nodes))
writer.writerow(list)
counter = counter + 1
print(counter)
# --------------------------------------------------------------------------------------------------
def xyz(filename_xyz):
'''
:param filename_xyz: a .csv file containing the x y and z coordinates of the nodes within the FE model
:return: a panda data frame holding the x y z coordinate information.
'''
data = pd.read_csv(filename_xyz, header=None)
df = pd.DataFrame(data)
df.columns = ['x', 'y', 'z']
return df
# --------------------------------------------------------------------------------------------------
def node_material_assign(elements, elements_ID, face_boundary, boundary_marker, num_nodes, filename):
'''
:param elements: csv file which contains the elements of the FE model.
:param elements_ID: a .csv file which assigns every element to a material.
:param num_nodes: number of nodes within the FE model.
:param filename: a filename where every NODE will be assigned a material property (as opposed to element).
:return: a panda data frame holding node material information.
'''
node_ID = np.zeros((num_nodes, 1), dtype=int)
with open(elements, 'r') as read_obj_1:
with open(elements_ID, 'r') as read_obj_2:
csv_reader_1 = reader(read_obj_1) # elements
csv_reader_2 = reader(read_obj_2) # element id
for row_1, row_2 in itertools.zip_longest(csv_reader_1, csv_reader_2):
val = int(row_2[0])
for n in row_1:
n = int(n)-1
node_ID[n, 0] = val
with open(face_boundary, 'r') as read_obj_3:
with open(boundary_marker, 'r') as read_obj_4:
csv_reader_3 = reader(read_obj_3) # face boundaries
csv_reader_4 = reader(read_obj_4) # boundary id
for row_3, row_4 in itertools.zip_longest(csv_reader_3, csv_reader_4):
val_b = int(row_4[0])
print(val_b)
for n in row_3:
n = int(n) - 1
node_ID[n, 0] = val_b
np.savetxt(filename, node_ID, fmt='%i')
df = pd.DataFrame(list(node_ID), columns=['Mat_ID'])
print(df)
return df
# --------------------------------------------------------------------------------------------------
def node_support_assign(support_nodes, num_nodes, filename):
'''
:param support_nodes: a .csv file which indicates the node ID of rigid nodes in the FE model.
:param num_nodes: number of nodes within the FE model.
:param filename: the place to store a column of size( num_nodes x 1) where rigid nodes have a value of 1.
:return: a panda data frame marking rigid nodes.
'''
support_list = np.zeros((num_nodes, 1), dtype=np.int)
with open(support_nodes, 'r') as read_obj:
csv_reader = reader(read_obj)
for row in csv_reader:
n = int(row[0])-1
support_list[n,0]=1
np.savetxt(filename, support_list, fmt='%i')
df = pd.DataFrame(list(support_list), columns=['Rigid_ID'])
return df
# --------------------------------------------------------------------------------------------------
"""
The format of the feature matrix is:
{Node ID} {x} {y} {z} {material_ID} {Rigid} {Force magnitude} {F_x} {F_y} {F_z}
"""
#
def feature_constructor(num_nodes, df_xyz, df_mat_id, df_rigid_id, filename_pres_nodes, filename_force_dir, magnitude, t_steps):
'''
:param num_nodes: Number of nodes within the FE mesh
:param df_xyz: x y z data frame
:param df_mat_id: material id data frame
:param df_rigid_id: support nodes data frame
:param filename_pres_nodes: prescribed load nodes csv file
:param filename_force_dir: direction of forces (normal vectors) csv file
:param magnitude: magnitude of the force
:param t_steps: number of time steps in the FEA
:return: features data frame
'''
pres_nodes = genfromtxt(filename_pres_nodes, delimiter=',', dtype=int)
force_dirs = genfromtxt(filename_force_dir, delimiter=',')
idx_repeat = int(len(force_dirs) / len(pres_nodes))
pres_nodes_full = []
for p_node in pres_nodes:
for j in range(idx_repeat):
pres_nodes_full.append(int(p_node))
# print(pres_nodes_full)
df_f_1 = pd.concat([df_xyz, df_mat_id, df_rigid_id], axis=1)
df_f_copy = pd.concat([df_xyz, df_mat_id, df_rigid_id], axis=1)
for i in range(len(pres_nodes_full) * t_steps - 1):
frame = [df_f_1,df_f_copy]
df_f_1 = pd.concat(frame)
df_f_1.reset_index(drop=True, inplace=True)
step_mag = magnitude/t_steps
force = []
pres_nodes_idx = []
m = 0
for i in range(len(pres_nodes_full)):
p_node_force_direction = force_dirs[i, :]
for j in range(t_steps):
updated_mag = step_mag * (j+1)
force.append(updated_mag)
force.append(p_node_force_direction[0])
force.append(p_node_force_direction[1])
force.append(p_node_force_direction[2])
# ----------------------------------------
idx = (pres_nodes_full[i] - 1) + (m * num_nodes)
pres_nodes_idx.append(idx)
m = m + 1
force = np.reshape(force, (-1, 4)) # columns of this matrix are: magnitude, F_x, F_y, F_z
# print(force)
force_length = t_steps * len(pres_nodes_full) * num_nodes
M = np.zeros((force_length, 4))
# print(pres_nodes_idx)
for k in range(len(pres_nodes_idx)):
# print('------------')
# print(pres_nodes_idx[k])
# print(force[k, 0])
# print(force[k, 1])
# print(force[k, 2])
# print(force[k, 3])
# print('------------')
M[pres_nodes_idx[k], 0] = force[k, 0]
M[pres_nodes_idx[k], 1] = force[k, 1]
M[pres_nodes_idx[k], 2] = force[k, 2]
M[pres_nodes_idx[k], 3] = force[k, 3]
# print('Printing M')
# print(M)
df_f_2 = pd.DataFrame(data=M, columns=["Magnitude", "F_x", "F_y", "F_z"])
# print(df_f_1)
# print(df_f_2)
frame_2 = [df_f_1, df_f_2]
df_features = pd.concat(frame_2, axis=1)
# print(df_features)
return df_features
# --------------------------------------------------------------------------------------------------
def feature_constructor_2(num_nodes, df_xyz, df_mat_id, df_rigid_id, filename_pres_nodes,
filename_force_dir_x, filename_force_dir_y, filename_force_dir_z, magnitude, t_steps):
'''
This function is used for when we want to construct the feature matrix when we are applying prescribe forces to
multiple nodes at the same time.
:param num_nodes: Number of nodes within the FE mesh
:param df_xyz: x y z data frame
:param df_mat_id: material id data frame
:param df_rigid_id: support nodes data frame
:param filename_pres_nodes: prescribed load nodes csv file
:param filename_force_dir: direction of forces (normal vectors) csv file
:param magnitude: magnitude of the force
:param t_steps: number of time steps in the FEA
:return: features data frame
'''
pres_nodes = genfromtxt(filename_pres_nodes, delimiter=',', dtype=int)
force_dir_x = genfromtxt(filename_force_dir_x, delimiter=',')
force_dir_y = genfromtxt(filename_force_dir_y, delimiter=',')
force_dir_z = genfromtxt(filename_force_dir_z, delimiter=',')
idx_repeat = force_dir_x.shape[1]
print(idx_repeat)
df_f_1 = pd.concat([df_xyz, df_mat_id, df_rigid_id], axis=1)
df_f_copy = pd.concat([df_xyz, df_mat_id, df_rigid_id], axis=1)
for i in range(idx_repeat * t_steps - 1):
frame = [df_f_1,df_f_copy]
df_f_1 = pd.concat(frame)
df_f_1.reset_index(drop=True, inplace=True)
step_mag = magnitude/(t_steps * len(pres_nodes))
force_dir_x_full = []
force_dir_y_full = []
force_dir_z_full = []
force_magnitude = []
pres_nodes_idx = []
m = 0
for i in range(idx_repeat):
for j in range(t_steps):
for k in range(len(pres_nodes)):
force_dir_x_full.append(force_dir_x[k, i])
force_dir_y_full.append(force_dir_y[k, i])
force_dir_z_full.append(force_dir_z[k, i])
updated_mag = step_mag * (j + 1)
force_magnitude.append(updated_mag)
idx = int(pres_nodes[k]) - 1 + (num_nodes*m)
pres_nodes_idx.append(idx)
m = m + 1
force_length = t_steps * idx_repeat * num_nodes
M = np.zeros((force_length, 4))
# print(force_dir_x_full)
# print(force_dir_y_full)
# print(force_dir_z_full)
# print(pres_nodes_idx)
for k in range(len(pres_nodes_idx)):
# print('------------')
# print(pres_nodes_idx[k])
# print(force[k, 0])
# print(force[k, 1])
# print(force[k, 2])
# print(force[k, 3])
# print('------------')
M[pres_nodes_idx[k], 0] = force_magnitude[k]
M[pres_nodes_idx[k], 1] = force_dir_x_full[k]
M[pres_nodes_idx[k], 2] = force_dir_y_full[k]
M[pres_nodes_idx[k], 3] = force_dir_z_full[k]
# print('Printing M')
# print(M)
df_f_2 = pd.DataFrame(data=M, columns=["Magnitude", "F_x", "F_y", "F_z"])
# print(df_f_1)
# print(df_f_2)
frame_2 = [df_f_1, df_f_2]
df_features = pd.concat(frame_2, axis=1)
print(df_features)
return df_features
# --------------------------------------------------------------------------------------------------
def feature_constructor3(num_nodes, df_xyz, df_mat_id, df_rigid_id, filename_pres_nodes, node_idx, filename_force_dir, magnitude, t_steps):
'''
:param num_nodes: Number of nodes within the FE mesh
:param df_xyz: x y z data frame
:param df_mat_id: material id data frame
:param df_rigid_id: support nodes data frame
:param filename_pres_nodes: prescribed load nodes csv file
:param filename_force_dir: direction of forces (normal vectors) csv file
:param magnitude: magnitude of the force
:param t_steps: number of time steps in the FEA
:return: features data frame
'''
pres_nodes = genfromtxt(filename_pres_nodes, delimiter=',', dtype=int)
print(pres_nodes)
p_node = pres_nodes[node_idx-1]
print(p_node)
force_dirs = genfromtxt(filename_force_dir, delimiter=',')
idx_repeat = len(force_dirs)
pres_nodes_full = []
for j in range(idx_repeat):
pres_nodes_full.append(int(p_node))
print(pres_nodes_full)
df_f_1 = pd.concat([df_xyz, df_mat_id, df_rigid_id], axis=1)
df_f_copy = pd.concat([df_xyz, df_mat_id, df_rigid_id], axis=1)
for i in range(len(pres_nodes_full) * t_steps - 1):
frame = [df_f_1,df_f_copy]
df_f_1 = pd.concat(frame)
df_f_1.reset_index(drop=True, inplace=True)
step_mag = magnitude/t_steps
force = []
pres_nodes_idx = []
m = 0
for i in range(len(pres_nodes_full)):
p_node_force_direction = force_dirs[i, :]
for j in range(t_steps):
updated_mag = step_mag * (j+1)
force.append(updated_mag)
force.append(p_node_force_direction[0])
force.append(p_node_force_direction[1])
force.append(p_node_force_direction[2])
# ----------------------------------------
idx = (pres_nodes_full[i] - 1) + (m * num_nodes)
pres_nodes_idx.append(idx)
m = m + 1
force = np.reshape(force, (-1, 4)) # columns of this matrix are: magnitude, F_x, F_y, F_z
# print(pres_nodes_idx)
# print(force)
force_length = t_steps * len(pres_nodes_full) * num_nodes
M = np.zeros((force_length, 4))
# print(pres_nodes_idx)
for k in range(len(pres_nodes_idx)):
# print('------------')
# print(pres_nodes_idx[k])
# print(force[k, 0])
# print(force[k, 1])
# print(force[k, 2])
# print(force[k, 3])
# print('------------')
M[pres_nodes_idx[k], 0] = force[k, 0]
M[pres_nodes_idx[k], 1] = force[k, 1]
M[pres_nodes_idx[k], 2] = force[k, 2]
M[pres_nodes_idx[k], 3] = force[k, 3]
# print('Printing M')
# print(M)
df_f_2 = pd.DataFrame(data=M, columns=["Magnitude", "F_x", "F_y", "F_z"])
# print(df_f_1)
# print(df_f_2)
frame_2 = [df_f_1, df_f_2]
df_features = pd.concat(frame_2, axis=1)
# print(df_features)
return df_features
# --------------------------------------------------------------------------------------------------
def graph_indicator(num_nodes, num_p_nodes, num_dirs, t_steps):
num_graphs = num_p_nodes * num_dirs * t_steps
graph_labels = []
for i in range(num_graphs):
for j in range(num_nodes):
graph_labels.append(i+1)
df = pd.DataFrame(graph_labels)
return df
# --------------------------------------------------------------------------------------------------
def graph_indicator_2(num_nodes, num_dirs, t_steps):
num_graphs = num_dirs * t_steps
graph_labels = []
for i in range(num_graphs):
for j in range(num_nodes):
graph_labels.append(i+1)
df = pd.DataFrame(graph_labels)
return df
# --------------------------------------------------------------------------------------------------
def graph_label(num_p_nodes, num_dirs, t_steps):
num_graphs = num_p_nodes * num_dirs * t_steps
graph_labels = []
for i in range(num_graphs):
graph_labels.append(i+1)
df = pd.DataFrame(graph_labels)
return df
# --------------------------------------------------------------------------------------------------
def graph_label_2(num_dirs, t_steps):
num_graphs = num_dirs * t_steps
graph_labels = []
for i in range(num_graphs):
graph_labels.append(i+1)
df = pd.DataFrame(graph_labels)
return df
# --------------------------------------------------------------------------------------------------
def output_format(filename_output, t_steps): # this part needs to be hardcoded. This code is for when we have 5 time steps
'''
This function requires to be hardcoded.
:param filename_output: an output file holding displacement values at x y z for t_steps
:param t_steps: number of time steps in the FEA
:return: a panda data frame
'''
output_v1 = genfromtxt(filename_output, delimiter=',')
start = []
end = []
for i in range(t_steps):
x = 3 * (i + 1)
start.append(x)
end.append(x + 3)
out_1 = output_v1[:, start[0]: end[0]]
out_2 = output_v1[:, start[1]: end[1]]
out_3 = output_v1[:, start[2]: end[2]]
out_4 = output_v1[:, start[3]: end[3]]
out_5 = output_v1[:, start[4]: end[4]]
out_6 = output_v1[:, start[5]: end[5]]
out_7 = output_v1[:, start[6]: end[6]]
out_8 = output_v1[:, start[7]: end[7]]
out_9 = output_v1[:, start[8]: end[8]]
out_10 = output_v1[:, start[9]: end[9]]
out_11 = output_v1[:, start[10]: end[10]]
out_12 = output_v1[:, start[11]: end[11]]
out_13 = output_v1[:, start[12]: end[12]]
out_14 = output_v1[:, start[13]: end[13]]
out_15 = output_v1[:, start[14]: end[14]]
out_16 = output_v1[:, start[15]: end[15]]
out_17 = output_v1[:, start[16]: end[16]]
out_18 = output_v1[:, start[17]: end[17]]
out_19 = output_v1[:, start[18]: end[18]]
out_20 = output_v1[:, start[19]: end[19]]
out_21 = output_v1[:, start[20]: end[20]]
out_22 = output_v1[:, start[21]: end[21]]
out_23 = output_v1[:, start[22]: end[22]]
out_24 = output_v1[:, start[23]: end[23]]
out_25 = output_v1[:, start[24]: end[24]]
out_26 = output_v1[:, start[25]: end[25]]
out_27 = output_v1[:, start[26]: end[26]]
out_28 = output_v1[:, start[27]: end[27]]
out_29 = output_v1[:, start[28]: end[28]]
out_30 = output_v1[:, start[29]: end[29]]
df_1 = pd.DataFrame(data=out_1)
df_2 = pd.DataFrame(data=out_2)
df_3 = pd.DataFrame(data=out_3)
df_4 = pd.DataFrame(data=out_4)
df_5 = pd.DataFrame(data=out_5)
df_6 = pd.DataFrame(data=out_6)
df_7 = pd.DataFrame(data=out_7)
df_8 = pd.DataFrame(data=out_8)
df_9 = pd.DataFrame(data=out_9)
df_10 = pd.DataFrame(data=out_10)
df_11 = pd.DataFrame(data=out_11)
df_12 = pd.DataFrame(data=out_12)
df_13 = pd.DataFrame(data=out_13)
df_14 = pd.DataFrame(data=out_14)
df_15 = pd.DataFrame(data=out_15)
df_16 = pd.DataFrame(data=out_16)
df_17 = pd.DataFrame(data=out_17)
df_18 = pd.DataFrame(data=out_18)
df_19 = pd.DataFrame(data=out_19)
df_20 = pd.DataFrame(data=out_20)
df_21 = pd.DataFrame(data=out_21)
df_22 = pd.DataFrame(data=out_22)
df_23 = pd.DataFrame(data=out_23)
df_24 = pd.DataFrame(data=out_24)
df_25 = pd.DataFrame(data=out_25)
df_26 = pd.DataFrame(data=out_26)
df_27 = pd.DataFrame(data=out_27)
df_28 = pd.DataFrame(data=out_28)
df_29 = pd.DataFrame(data=out_29)
df_30 = pd.DataFrame(data=out_30)
frame = [df_1, df_2, df_3, df_4,
df_5, df_6, df_7, df_8,
df_9, df_10, df_11, df_12,
df_13, df_14, df_15, df_16,
df_17, df_18, df_19, df_20,
df_21, df_22, df_23, df_24,
df_25, df_26, df_27, df_28,
df_29, df_30]
df_output = pd.concat(frame)
df_output.reset_index(drop=True, inplace=True)
return df_output