1
+ # Support for Gmsh meshes
2
+
3
+ module Gmsh_utils
4
+
5
+ using GridapGmsh
6
+
7
+ import GeophysicalModelGenerator: import_Gmsh, FEData
8
+
9
+ """
10
+ fe_data, tag_names = import_Gmsh(fname::String)
11
+
12
+ Reads a Gmsh file and returns a `FEData` object with info about the mesh. `tag_names` contains the names of the regions of the FE mesh
13
+ """
14
+ function import_Gmsh (fname:: String )
15
+
16
+ mesh = GmshDiscreteModel (fname, renumber= false )
17
+
18
+ # Extract vertices
19
+ nverts = length (mesh. grid. node_coordinates);
20
+ dims = length (mesh. grid. node_coordinates[1 ])
21
+ vertices= [mesh. grid. node_coordinates[n][i] for i= 1 : dims,n= 1 : nverts]
22
+
23
+ # write coords as 1D double array
24
+ nvertices_cell = length (mesh. grid. cell_node_ids[1 ])
25
+ connectivity = [c[i] for i= 1 : nvertices_cell, c in mesh. grid. cell_node_ids]
26
+
27
+
28
+ regions, tag_names = cell_tags_from_gmsh (mesh) # extract number of each of the tetrahedrons
29
+
30
+ cellfields = (regions= regions,)
31
+ fields= nothing
32
+
33
+ return FEData (vertices,connectivity, fields, cellfields), tag_names
34
+ end
35
+
36
+
37
+ """
38
+ tags, tag_names = cell_tags_from_gmsh(mesh::GmshDiscreteModel)
39
+
40
+ Returns a list with integers that are the tags for each of the cells
41
+ """
42
+ function cell_tags_from_gmsh (mesh)
43
+ cell_entities = mesh. face_labeling. d_to_dface_to_entity[4 ] # volumetric entities
44
+ cell_entities_unique = unique (cell_entities)
45
+ tag_unique = zeros (Int64,size (cell_entities_unique))
46
+
47
+ for i= 1 : length (cell_entities_unique)
48
+ for (n,tag) in enumerate (mesh. face_labeling. tag_to_entities)
49
+ if any (tag .== cell_entities_unique[i])
50
+ tag_unique[i] = n
51
+ end
52
+ end
53
+ end
54
+
55
+ # create tags for cells
56
+ tags = zeros (Int64,length (cell_entities))
57
+ for (i,entity) in enumerate (cell_entities_unique)
58
+ id = findall (cell_entities.== entity)
59
+ tags[id] .= tag_unique[i]
60
+ end
61
+
62
+ return tags, mesh. face_labeling. tag_to_name
63
+ end
64
+
65
+
66
+ end
0 commit comments