-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh_Preprocess.py
1774 lines (1294 loc) · 56.8 KB
/
Mesh_Preprocess.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# # Importing necessary libraries:
# ******************************************************
# In[1]:
import numpy as np
# In[2]:
import pandas as pd
# In[3]:
import matplotlib.pyplot as plt
# In[4]:
import matplotlib
# In[5]:
from matplotlib.colors import Normalize
# In[6]:
import subprocess
# In[9]:
import scipy
# In[10]:
from numba import jit
# ******************************************
# # Preprocessing the mesh file:
# ****************************************************
# ## We are using the Gmsh to mesh the geometry:
# ### Gmsh website: https://gmsh.info/
# ### Reference to Gmsh file formats: https://gmsh.info/doc/texinfo/gmsh.html#Gmsh-file-formats
# ### NOTE: Enable save all nodes option while saving the mesh file
# ## Reading the contents of mesh file
# In[12]:
# Mesh_File should be the name of the Gmsh file which is to be used
Mesh_File = "mesh.msh"
# In[13]:
def Mesh_String_Extractor(mesh_file_content,Start,End):
"""
Input:
mesh_file_content: Content of the mesh file as string
Start: Start of the string to be extracted, excluding Start
End: End of the string to be extracted, excluding End
Output:
Mesh_String: Content of the mesh file between Start and End
Description:
Extracting the string between Start and End in the string mesh_file_content
"""
# Acquiring the index of Start
Mesh_Start_index = mesh_file_content.find(Start)
# Acquiring the index of End
Mesh_End_index = mesh_file_content.find(End)
# Acquiring the content between Start and End
Mesh_String = mesh_file_content[Mesh_Start_index + len(Start)+1:Mesh_End_index-1]
return Mesh_String
# In[14]:
def Mesh_Format(Mesh_File):
"""
Input:
mesh_file: Path to the mesh file
Output:
Mesh_Ascii_Format: Ascii Format of the mesh file
Mesh_File_Type: 0 for ASCII mode, 1 for binary mode
Mesh_Data_Size_Native: Data-size in the mesh_file
Description:
Extracting the mesh file format
"""
# Reading the mesh file
mesh_file = open(Mesh_File,"r")
# Creating the string of the entire mesh file
mesh_file_content = mesh_file.read()
# Closing the mesh file
mesh_file.close()
# Extracting the mesh format
Start = "$MeshFormat"
End = "$EndMeshFormat"
Mesh_Format_string = Mesh_String_Extractor(mesh_file_content,Start,End)
# Mesh_Format_string: Ascii Format of the mesh file
Mesh_Ascii_Format = float(Mesh_Format_string.split(sep=" ")[0])
# Mesh_File_Type: 0 for ASCII mode, 1 for binary mode
Mesh_File_Type = int(Mesh_Format_string.split(sep=" ")[1])
# Mesh_Data_Size_Native: Data-size in the mesh_file
Mesh_Data_Size_Native = int(Mesh_Format_string.split(sep=" ")[2])
return Mesh_Ascii_Format, Mesh_File_Type, Mesh_Data_Size_Native
# In[15]:
# Mesh file format
# Mesh_Ascii_Format, Mesh_File_Type, Mesh_Data_Size_Native = Mesh_Format(Mesh_File)
# print(f"Mesh File Format: ")
# print(f"Mesh Ascii Format: {Mesh_Ascii_Format}")
# print(f"Mesh File Type: {Mesh_File_Type}")
# print(f"Mesh Data Size_Native: {Mesh_Data_Size_Native}")
# In[16]:
def Mesh_Physical_Names(Mesh_File):
"""
Input:
Mesh_File: Path to the mesh file
Output:
Physical_Tags_Dimension_Array[Physical_Tag,Dimension]
Physical_Tags_Name[Physical_Tag_Name]
Description:
Extracting the physical names in the mesh
NOTE:
This function will be used in future
"""
# Reading the mesh file
mesh_file = open(Mesh_File,"r")
# Creating the string of the entire mesh file
mesh_file_content = mesh_file.read()
# Closing the mesh file
mesh_file.close()
# Extracting the physical names
Start = "$PhysicalNames"
End = "$EndPhysicalNames"
Mesh_String = Mesh_String_Extractor(mesh_file_content,Start,End)
# Extracting the lines
Lines = Mesh_String.split(sep="\n")
# Extracting the number of the lines
num_Physical_Names = int(Lines[0])
# Memory Allocation
# Physical_Tags_Dimension_Array:
# Physical_Tags_Dimension_Array[Physical_Tag,Dimension]
Physical_Tags_Dimension_Array = np.zeros((num_Physical_Names,2),dtype=int)
# Physical_Tags_Name[Physical_Tag_Name]
Physical_Tags_Name = np.empty(num_Physical_Names,dtype = object)
# Populating the arrays
for i in range(1,num_Physical_Names+1):
# Debugging Script:
# print(i)
# dimension = int(Lines[i].split(" ")[0])
# Physical_Tag = int(Lines[i].split(" ")[1])
# Physical_Tag_Name = Lines[i].split(" ")[2][1:-1]
# print(Physical_Tag_Name)
Physical_Tags_Dimension_Array[i-1,0] = int(Lines[i].split(" ")[0])
Physical_Tags_Dimension_Array[i-1,1] = int(Lines[i].split(" ")[1])
Physical_Tags_Name[i-1] = Lines[i].split(" ")[2][1:-1]
return Physical_Tags_Dimension_Array, Physical_Tags_Name
# In[17]:
# Physical names in the mesh file
# Mesh_Physical_Names(Mesh_File)
# In[18]:
def Mesh_Entities(Mesh_File):
"""
Input:
Mesh_File: Path to the mesh file
Output:
Point_Data: [pointTag(int),X(double),Y(double),Z(double),numPhysicalTags(size_t),physicalTag(int)]
Curve_Data: [curveTag(int),minX(double),minY(double),minZ(double),maxX(double),maxY(double),maxZ(double),numPhysicalTags(size_t),physicalTag(int),numBoundingPoints(size_t),pointTag]
Surface_Data: [surfaceTag(int),minX(double),minY(double),minZ(double),maxX(double),maxY(double),maxZ(double),numPhysicalTags(size_t),physicalTag(int),numBoundingCurves(size_t),curveTag]
Volume_Data: [volumeTag(int),minX(double),minY(double),minZ(double),maxX(double),maxY(double),maxZ(double),numPhysicalTags(size_t),physicalTag(int),numBoundngSurfaces(size_t),surfaceTag]
Description:
Extracting the data about different types of entities in the mesh
"""
# Reading the mesh file
mesh_file = open(Mesh_File,"r")
# Creating the string of the entire mesh file
mesh_file_content = mesh_file.read()
# Closing the mesh file
mesh_file.close()
# Extracting the Entities
Start = "$Entities"
End = "$EndEntities"
Mesh_String = Mesh_String_Extractor(mesh_file_content,Start,End)
# Extracting the lines
Lines = Mesh_String.split(sep="\n")
num_Mesh_Points = int(Lines[0].split(" ")[0])
num_Mesh_Curves = int(Lines[0].split(" ")[1])
num_Mesh_Surfaces = int(Lines[0].split(" ")[2])
num_Mesh_Volumes = int(Lines[0].split(" ")[3])
# Memory allocation
Point_Attributes = len(Lines[1:num_Mesh_Points+1][0].split(" "))
Point_Data = np.zeros((num_Mesh_Points,Point_Attributes))
# Populating the Points_Data
for i in range(1,num_Mesh_Points+1):
# Make this True if you are using Physical Tags for the points
Point_Pysical_Tags = False
if Point_Pysical_Tags == False:
# print(Lines[1:num_Mesh_Points+1][i-1])
Point_Data[i-1][:Point_Attributes-1] = Lines[1:num_Mesh_Points+1][i-1].split(" ")[:Point_Attributes-1]
else:
Point_Data[i-1] = Lines[1:num_Mesh_Points+1][i-1].split(" ")
# Memory allocation
Curve_Attributes = len(Lines[num_Mesh_Points+1:num_Mesh_Points+num_Mesh_Curves+1][0].split(" "))
Curve_Data = np.zeros((num_Mesh_Curves,Curve_Attributes))
# Populating the Curve_Data
for i in range(1,num_Mesh_Curves+1):
# Make this True if you are using Physical Tags for the points
Point_Pysical_Tags = False
if Point_Pysical_Tags == False:
Curve_Data[i-1][:Curve_Attributes-1] = Lines[num_Mesh_Points+1:num_Mesh_Points+num_Mesh_Curves+1][i-1].split(" ")[:Curve_Attributes-1]
else:
Curve_Data[i-1] = Lines[num_Mesh_Points+1:num_Mesh_Points+num_Mesh_Curves+1][i-1].split(" ")
# Memory allocation
Surface_Attributes = len(Lines[num_Mesh_Points+num_Mesh_Curves+1:num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+1][0].split(" "))
Surface_Data = np.zeros((num_Mesh_Surfaces,Surface_Attributes))
# Populating the Surface_Data
for i in range(1,num_Mesh_Surfaces+1):
# Make this True if you are using Physical Tags for the points
Point_Pysical_Tags = False
if Point_Pysical_Tags == False:
Surface_Data[i-1][:Surface_Attributes-1] = Lines[num_Mesh_Points+num_Mesh_Curves+1:num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+1][i-1].split(" ")[:Surface_Attributes-1]
else:
Surface_Data[i-1] = Lines[num_Mesh_Points+num_Mesh_Curves+1:num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+1][i-1].split(" ")
if num_Mesh_Volumes == 0:
Volume_Data = 0
else:
# Memory Allocation
Volume_Attributes = len(Lines[num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+1:num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+num_Mesh_Volumes+1][0].split(" "))
Volume_Data = np.zeros((num_Mesh_Volumes,Volume_Attributes))
# Populating the Curve_Data
for i in range(1,num_Mesh_Volumes+1):
# Make this True if you are using Physical Tags for the points
Point_Pysical_Tags = False
if Point_Pysical_Tags == False:
Volume_Data[i-1][:Volume_Attributes-1] = Lines[num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+1:num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+num_Mesh_Volumes+1][i-1].split(" ")[:Volume_Attributes-1]
else:
Volume_Data[i-1] = Lines[num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+1:num_Mesh_Points+num_Mesh_Curves+num_Mesh_Surfaces+num_Mesh_Volumes+1][i-1].split(" ")
return Point_Data,Curve_Data,Surface_Data,Volume_Data
# In[19]:
def Mesh_Nodes(Mesh_File):
"""
Input:
Mesh_File: Path to the mesh file
Output:
Point_Data: [pointTag(int),X(double),Y(double),Z(double),numPhysicalTags(size_t),physicalTag(int)]
Curve_Data: [curveTag(int),minX(double),minY(double),minZ(double),maxX(double),maxY(double),maxZ(double),numPhysicalTags(size_t),physicalTag(int),numBoundingPoints(size_t),pointTag]
Surface_Data: [surfaceTag(int),minX(double),minY(double),minZ(double),maxX(double),maxY(double),maxZ(double),numPhysicalTags(size_t),physicalTag(int),numBoundingCurves(size_t),curveTag]
Volume_Data: [volumeTag(int),minX(double),minY(double),minZ(double),maxX(double),maxY(double),maxZ(double),numPhysicalTags(size_t),physicalTag(int),numBoundngSurfaces(size_t),surfaceTag]
Description:
Extracting different types of mesh nodes in the mesh
"""
# Reading the mesh file
mesh_file = open(Mesh_File,"r")
# Creating the string of the entire mesh file
mesh_file_content = mesh_file.read()
# Closing the mesh file
mesh_file.close()
# Extracting the Entities
Start = "$Entities"
End = "$EndEntities"
Mesh_String = Mesh_String_Extractor(mesh_file_content,Start,End)
# Extracting the lines
Lines = Mesh_String.split(sep="\n")
num_Mesh_Points = int(Lines[0].split(" ")[0])
num_Mesh_Curves = int(Lines[0].split(" ")[1])
num_Mesh_Surfaces = int(Lines[0].split(" ")[2])
num_Mesh_Volumes = int(Lines[0].split(" ")[3])
# Extracting the Nodes
Start = "$Nodes"
End = "$EndNodes"
Mesh_String = Mesh_String_Extractor(mesh_file_content,Start,End)
# Extracting the lines
Lines = Mesh_String.split(sep="\n")
num_Entity_Blocks = int(Lines[0].split(" ")[0])
num_Nodes = int(Lines[0].split(" ")[1])
min_Node_Tag = int(Lines[0].split(" ")[2])
max_Node_Tag = int(Lines[0].split(" ")[3])
# Memory allocation
Node_Coordinates = np.zeros((num_Nodes,3))
# Storing the coordinates of all the nodes
# The index of the array is the (Node_Tag - 1)
for i in range(1,len(Lines[1:]) - 1):
# Description Line
# entityDim(int),entityTag(int),parametric(int; 0 or 1),numNodesInBlock(size_t),nodeTag(size_t)
if (len(Lines[i].split()) == 4) and (len(Lines[i+1].split()) != 4):
# print(Lines[i].split(" "))
temp = int(Lines[i].split(" ")[-1])
for j in range(i+1,temp+i+1):
Node_Tag = int(Lines[j])
# print(Lines[temp + j].split(" "))
Node_Coordinates[Node_Tag-1][:] = Lines[temp + j].split(" ")[:3]
i = i+(2*temp)
# Memory allocation
Point_Nodes = np.zeros((num_Mesh_Points,1))
Curve_Nodes = np.empty(num_Mesh_Curves,dtype = object)
Surface_Nodes = np.empty(num_Mesh_Surfaces,dtype = object)
# Storing the nodes in the different entities (Points, Curves and Surfaces)
# The index of the arrays are the (Tag - 1) and the list output is the list of the node in that entity
for i in range(1,len(Lines[1:]) - 1):
# Description Line
# entityDim(int),entityTag(int),parametric(int; 0 or 1),numNodesInBlock(size_t),nodeTag(size_t)
if (len(Lines[i].split()) == 4) and (len(Lines[i+1].split()) != 4):
# print(Lines[i].split(" "))
temp = int(Lines[i].split(" ")[0])
if temp == 0:
temp_1 = int(Lines[i].split(" ")[1])
Point_Nodes[temp_1 - 1] = int(Lines[i+1].split(" ")[0]) - 1
if temp == 1:
temp_2 = int(Lines[i].split(" ")[-1])
Curve_List = []
for j in range(0,temp_2):
Curve_List.append(int(Lines[i+j+1].split(" ")[0])-1)
temp_1 = int(Lines[i].split(" ")[1])
Curve_Nodes[temp_1 - 1] = Curve_List
if temp == 2:
temp_2 = int(Lines[i].split(" ")[-1])
Surface_List = []
for j in range(0,temp_2):
Surface_List.append(int(Lines[i+j+1].split(" ")[0])-1)
temp_1 = int(Lines[i].split(" ")[1])
Surface_Nodes[temp_1 - 1] = Surface_List
i = i+(2*temp)
return Node_Coordinates,Point_Nodes,Curve_Nodes,Surface_Nodes
# In[20]:
def Mesh_Elements(Mesh_File):
"""
Input:
Mesh_File: Path to the mesh file
Output:
Element_Nodes: [X,Y,Z,Element Type]
Index is the (Element_Tag -1)
Description:
This will contain different types of elements in 0D, 1D, 2D and 3D
"""
# Reading the mesh file
mesh_file = open(Mesh_File,"r")
# Creating the string of the entire mesh file
mesh_file_content = mesh_file.read()
# Closing the mesh file
mesh_file.close()
# Extracting the Elements
Start = "$Elements"
End = "$EndElements"
Mesh_String = Mesh_String_Extractor(mesh_file_content,Start,End)
# Extracting the lines
Lines = Mesh_String.split(sep="\n")
num_Entity_Blocks = int(Lines[0].split(" ")[0])
num_Elements = int(Lines[0].split(" ")[1])
min_Element_Tag = int(Lines[0].split(" ")[2])
max_Element_Tag = int(Lines[0].split(" ")[3])
# Memory allocation
Element_Nodes = np.zeros((num_Elements,4),dtype = int)
for i in range(1,len(Lines[1:])):
# print(Lines[i].split())
if len(Lines[i].split()) == 4:
if int(Lines[i].split()[0]) <= 3:
# print(Lines[i])
for j in range(i+1,i+1+int(Lines[i].split()[-1])):
# print(Lines[j])
index = int(Lines[j].split()[0])
if len(Lines[j].split()) == 2:
Element_Nodes[index-1,-1] = Lines[i].split()[0]
Element_Nodes[index-1,0] = Lines[j].split()[1]
Element_Nodes[index-1,0] = Element_Nodes[index-1,0] - 1
if len(Lines[j].split()) == 3:
Element_Nodes[index-1,-1] = Lines[i].split()[0]
Element_Nodes[index-1,0:2] = Lines[j].split()[1:]
Element_Nodes[index-1,0:2] = Element_Nodes[index-1,0:2] - 1
if len(Lines[j].split()) == 4:
Element_Nodes[index-1,-1] = Lines[i].split()[0]
Element_Nodes[index-1,0:3] = Lines[j].split()[1:]
Element_Nodes[index-1,0:3] = Element_Nodes[index-1,0:3] - 1
return Element_Nodes
# In[21]:
# Mesh Elements
Mesh_Elements_Data = Mesh_Elements(Mesh_File)
# In[22]:
# This will count the number of triangles in the mesh
Num_Triangles = np.count_nonzero(Mesh_Elements_Data[:,-1] == 2)
# In[23]:
def Element_Node_Connectivity_Calculate(Num_Triangles,Mesh_Elements_Data):
"""
Input:
Num_Triangles: Number of triangles
Mesh_Elements_Data: Mesh Elements Data (Previously Extracted)
Output:
Element Node Connectivity: Format: [Element, Node1, Node2, Node3]
"""
# Memory allocation
# Format: [Element, Node1, Node2, Node3]
Element_Node_Connectivity = np.zeros((Num_Triangles,4))
# This a counter
j = 0
# Looping over all type of mesh elements in the mesh
for i in range(Mesh_Elements_Data.shape[0]):
# Only triangle mesh elements will be considered
if Mesh_Elements_Data[i,-1] == 2:
# Element is stored
Element_Node_Connectivity[j,0] = i
# Nodes are stored
Element_Node_Connectivity[j,1:] = Mesh_Elements_Data[i,:3]
j = j+1
return Element_Node_Connectivity
# In[24]:
Element_Node_Connectivity = Element_Node_Connectivity_Calculate(Num_Triangles,Mesh_Elements_Data)
# In[25]:
@jit(nopython=True)
def count_matching_elements(arr1,arr2):
count = 0
for elm in arr1:
if elm in arr2:
count = count + 1
return count
# In[26]:
@jit(nopython=True)
def intersect1d_numba(arr1, arr2):
# Sort the input arrays
arr1_sorted = np.sort(arr1)
arr2_sorted = np.sort(arr2)
# Initialize variables to track unique elements and intersection
intersection = []
i, j = 0, 0
# Find the intersection of the sorted arrays
while i < len(arr1_sorted) and j < len(arr2_sorted):
if arr1_sorted[i] < arr2_sorted[j]:
i += 1
elif arr1_sorted[i] > arr2_sorted[j]:
j += 1
else:
intersection.append(arr1_sorted[i])
i += 1
j += 1
return np.array(intersection)
# In[27]:
def Edge_Node_Connectivity_Calculate(Element_Node_Connectivity,Mesh_Elements_Data):
"""
Input:
Element_Node_Connectivity: Element Node Connectivity (Previously Extracted)
Mesh_Elements_Data: Mesh Elements Data (Previously Extracted)
Output:
Edge_Node_Connectivity: Format: [Edge, Node1, Node2]
Boundary_Edges: [Edge]
Description:
This function calculates edge node connectivity
NOTE:
This function also returns boundary edges
"""
# List to store edge node connectivity
Edge_Node_Connectivity = []
# List to store boundary edges
Boundary_Edges = []
# Edges inside the domain
Edge_num = 0
for i in range(Element_Node_Connectivity.shape[0]):
Element = Element_Node_Connectivity[i][0]
Nodes = Element_Node_Connectivity[i][1:]
for j in range(i+1,Element_Node_Connectivity.shape[0]):
element = Element_Node_Connectivity[j][0]
nodes = Element_Node_Connectivity[j][1:]
# Ordinary
# value = np.isin(Nodes,nodes).sum()
# Numba
value = count_matching_elements(Nodes,nodes)
temp = []
if value == 2:
if Element != element:
#Ordinary
temp_node = np.intersect1d(Nodes,nodes)
# Numba
# temp_node = intersect1d_numba(Nodes,nodes)
temp_node = np.sort(temp_node)
# Debugging Script:
# print(Nodes)
# print(nodes)
# print(element)
# print(Element)
temp.append(Edge_num)
temp.append(temp_node[0])
temp.append(temp_node[1])
Edge_Node_Connectivity.append(temp)
Edge_num = Edge_num + 1
# Edges at the boundary
for i in range(Mesh_Elements_Data.shape[0]):
if Mesh_Elements_Data[i,-1] == 1:
temp = []
temp.append(Edge_num)
temp_node = Mesh_Elements_Data[i][0:2]
temp_node = np.sort(temp_node)
temp.append(temp_node[0])
temp.append(temp_node[1])
Edge_Node_Connectivity.append(temp)
Boundary_Edges.append(Edge_num)
Edge_num = Edge_num + 1
# Debugging Script:
# print(temp)
# print(np.isin(Nodes,nodes).sum())
Edge_Node_Connectivity = np.array(Edge_Node_Connectivity)
Boundary_Edges = np.array(Boundary_Edges)
return Edge_Node_Connectivity,Boundary_Edges
# In[28]:
Edge_Node_Connectivity,Boundary_Edges = Edge_Node_Connectivity_Calculate(Element_Node_Connectivity,Mesh_Elements_Data)
# In[29]:
@jit(nopython=True)
def count_matching_elements(arr1,arr2):
count = 0
for elm in arr1:
if elm in arr2:
count = count + 1
return count
# In[30]:
@jit(nopython = True)
def Element_Edge_Connectivity_Calculate(Num_Triangles,Element_Node_Connectivity,Edge_Node_Connectivity):
"""
Input:
Num_Triangles: Number of triangles
Element_Node_Connectivity: Element Node Connectivity (Previously Calculated)
Edge_Node_Connectivity: Edge Node Connectivity (Previously Calculated)
Output:
Element_Edge_Connectivity: Element Edge Connectivity
"""
# Memory allocation
Element_Edge_Connectivity = np.zeros((Num_Triangles,4))
for i in range(Element_Node_Connectivity.shape[0]):
Element = Element_Node_Connectivity[i][0]
Nodes = Element_Node_Connectivity[i][1:]
Element_Edge_Connectivity[i,0] = Element
# print(Element)
ctr = 1
for j in range(Edge_Node_Connectivity.shape[0]):
edge = Edge_Node_Connectivity[j][0]
# node_1 = Edge_Node_Connectivity[j][1]
# node_2 = Edge_Node_Connectivity[j][2]
nodes = Edge_Node_Connectivity[j][1:]
# Ordinary
# value = np.isin(Nodes,nodes).sum()
# Numba
value = count_matching_elements(Nodes,nodes)
# temp = []
if value == 2:
Element_Edge_Connectivity[i,ctr] = edge
ctr = ctr + 1
if ctr == 4:
i = i + 1
j = Edge_Node_Connectivity.shape[0]
return Element_Edge_Connectivity
# In[31]:
Element_Edge_Connectivity = Element_Edge_Connectivity_Calculate(Num_Triangles,Element_Node_Connectivity,Edge_Node_Connectivity)
# In[32]:
Node_Coordinates,Point_Nodes,Curve_Nodes,Surface_Nodes = Mesh_Nodes(Mesh_File)
# In[33]:
def Plot_Nodes(Node_Coordinates,Marker):
"""
Input:
Node_Coordinates: Node Coordinates (Previously Calculated)
Marker: Marker for each of the nodes
Output:
Plots the nodes of the elements
"""
x = Node_Coordinates[:,0]
y = Node_Coordinates[:,1]
plt.plot(x,y,Marker)
plt.axis('scaled')
plt.show()
# Plot_Nodes(Node_Coordinates,"g*")
# In[34]:
def Plot_Edges(Element_Node_Connectivity,Node_Coordinates):
"""
Input:
Element_Node_Connectivity: Element Node Connectivity (Previously Calculated)
Node_Coordinates: Node Coordinates (Previously Calculated)
Output:
Plots the edges in the domain
"""
temp = np.zeros((4,2))
x = Node_Coordinates[:,0]
y = Node_Coordinates[:,1]
for i in range(Element_Node_Connectivity.shape[0]):
Nodes = Element_Node_Connectivity[i,1:]
Nodes = np.array(Nodes,dtype = int)
temp[:3,0] = x[Nodes]
temp[:3,1] = y[Nodes]
temp[-1,0] = x[Nodes[0]]
temp[-1,1] = y[Nodes[0]]
X = temp[:,0]
Y = temp[:,1]
plt.plot(X,Y)
# plt.plot(x[Nodes[0]],y[Nodes[0]],"-b*")
plt.axis('scaled')
plt.show()
# Plot_Edges(Element_Node_Connectivity,Node_Coordinates)
# In[35]:
def Element_Element_Connectivity_Calculate(Num_Triangles,Element_Node_Connectivity):
"""
Input:
Num_Triangles: Number of Triangles
Element_Node_Connectivity: Element Node Connectivity (Previously Calculated)
Output:
Element_Element_Connectivity: Format [Element, Nb_Element_1, Nb_Element_2, Nb_Element_3]
"""
Element_Element_Connectivity = np.zeros((Num_Triangles,4),dtype = int)
Edge_num = 0
for i in range(Element_Node_Connectivity.shape[0]):
# print(i)
Element = Element_Node_Connectivity[i][0]
# This is where all the entries are made equal to the original element
Element_Element_Connectivity[i] = Element
Nodes = Element_Node_Connectivity[i][1:]
ctr = 1
for j in range(Element_Node_Connectivity.shape[0]):
element = Element_Node_Connectivity[j][0]
nodes = Element_Node_Connectivity[j][1:]
value = np.isin(Nodes,nodes).sum()
if value == 2:
if Element != element:
# print(Element)
# print(element)
Element_Element_Connectivity[i][ctr] = element
ctr = ctr + 1
if ctr == 4:
# print(ctr)
i = i + 1
j = Element_Node_Connectivity.shape[0]
# print(i)
# Debug Script
# print(ctr)
# if ctr == 3:
# print(Element_Element_Connectivity[i][:])
return Element_Element_Connectivity
# In[36]:
Node_Coordinates.shape[0]
# In[37]:
Element_Node_Connectivity
# In[38]:
Element_Node_Connectivity[:,0].min()
# In[39]:
# Fast
def Element_Element_Connectivity_Calculate_fast(Num_Triangles,Num_Nodes,Element_Node_Connectivity):
Element_Elements_Array = np.zeros((Num_Triangles,4),dtype = int)
# Node loop
min_element = int(Element_Node_Connectivity[:,0].min())
for i in range(Num_Nodes):
Node_Elements = []
for k in range(1,4,1):
for j in range(Element_Node_Connectivity[Element_Node_Connectivity[:,k] == i].shape[0]):
# print(Mesh_Element_Points_dict[key][Mesh_Element_Points_dict[key][:,k] == i][j])
Node_Elements.append(Element_Node_Connectivity[Element_Node_Connectivity[:,k] == i][j])
Node_Elements = np.array(Node_Elements)
Num_Elements_in_Node = Node_Elements.shape[0]
# print(Num_Elements_in_Node)
# print(Node_Elements)
# Cell loop
for j in range(Num_Elements_in_Node):
Element = int(Node_Elements[j,0])
Points = Node_Elements[j,1:]
# print(Element)
Element_Elements_Array[Element-min_element,0] = Element
for k in range(Num_Elements_in_Node):
element = Node_Elements[k,0]
points = Node_Elements[k,1:]
# Avoid the cell in consideration
if element != Element:
value = np.isin(Points,points).sum()
# Share 2 points
if value == 2:
temp_array = np.where(Element_Elements_Array[Element -min_element][1:] == 0)[0]
# Still entries are empty
if temp_array.shape[0] != 0:
# No duplicate entries
if np.isin(Element_Elements_Array[Element-min_element,1:],element).sum() == 0:
zero_index = temp_array[0]
Element_Elements_Array[Element-min_element,zero_index+1] = element
# Debug
# print(f"{Element}\t{element}")
# print(zero_index)
# print(Element_Elements_Array[Element-1,:])
# Element_Elements_Array[Element_Elements_Array == 0] = -1
# Element_Elements_Array = Element_Elements_Array - 1
for i in range(1,4,1):
Element_Elements_Array[Element_Elements_Array[:,i] == 0,i] = Element_Elements_Array[Element_Elements_Array[:,i] == 0,0]
return Element_Elements_Array
# In[40]:
Num_Nodes = Node_Coordinates.shape[0]
# In[41]:
Num_Triangles
# In[42]:
Element_Element_Connectivity = Element_Element_Connectivity_Calculate_fast(Num_Triangles,Num_Nodes,Element_Node_Connectivity)
# Element_Element_Connectivity = Element_Element_Connectivity_Calculate(Num_Triangles,Element_Node_Connectivity)
# ### We need to renumber the elements
# In[43]:
def Renumbering(Element_Element_Connectivity,Element_Edge_Connectivity,Element_Node_Connectivity,Edge_Node_Connectivity):
# Renumbering
Element_Element_Connectivity_new = Element_Element_Connectivity - np.min(Element_Element_Connectivity)
# Renumbering
Element_Edge_Connectivity_new = Element_Edge_Connectivity
Element_Edge_Connectivity_new[:,0] = Element_Edge_Connectivity_new[:,0] - np.min(Element_Edge_Connectivity_new[:,0])
# Renumbering
Element_Node_Connectivity_new = Element_Node_Connectivity
# Renumbering
Element_Node_Connectivity_new[:,0] = Element_Node_Connectivity_new[:,0] - np.min(Element_Node_Connectivity_new[:,0])
# Renumbering
Edge_Node_Connectivity_new = Edge_Node_Connectivity
return Element_Element_Connectivity_new,Element_Edge_Connectivity_new,Element_Node_Connectivity_new,Edge_Node_Connectivity_new
# In[44]:
Element_Element_Connectivity_new,Element_Edge_Connectivity_new,Element_Node_Connectivity_new,Edge_Node_Connectivity_new = Renumbering(Element_Element_Connectivity,Element_Edge_Connectivity,Element_Node_Connectivity,Edge_Node_Connectivity)
# In[45]:
def Face_Centroid_Calculate(Edge_Node_Connectivity_new,Node_Coordinates):
"""
Input:
Edge_Node_Connectivity_new: Edge Node Connectivity renumbered
Node_Coordinates: Node Coordinates (Previously Calculated)
Output:
Face_Centroid: Format: [Edge,centroid x, centroid y]
"""
# Memory allocation
Face_Centroid = np.zeros((Edge_Node_Connectivity_new.shape[0],3))
# Converting the 3D coordinates to 2D coordinates
Node_Coordinates_2D = Node_Coordinates[:,:2]
for i in range(Edge_Node_Connectivity_new.shape[0]):
Face_Centroid[i,0] = Edge_Node_Connectivity_new[i,0]
Nodes = Edge_Node_Connectivity_new[i,1:]
# print(Nodes)
temp = 0
for node in Nodes:
temp = temp + Node_Coordinates_2D[int(node)]