Skip to content

Commit 247d445

Browse files
committed
add Gmsh import routines
1 parent 759c901 commit 247d445

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ GeoParams = "e018b62d-d9de-4a26-8697-af89c310ae38"
1313
Geodesy = "0ef565a4-170c-5f04-8de2-149903a85f3d"
1414
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
1515
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
16+
GridapGmsh = "3025c34a-b394-11e9-2a55-3fee550c04c8"
1617
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
1718
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
1819
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
@@ -31,10 +32,12 @@ WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"
3132
[weakdeps]
3233
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
3334
GMT = "5752ebe1-31b9-557e-87aa-f909b540aa54"
35+
GridapGmsh = "3025c34a-b394-11e9-2a55-3fee550c04c8"
3436

3537
[extensions]
3638
GLMakie_Visualisation = "GLMakie"
3739
GMT_utils = "GMT"
40+
Gmsh_utils = "GridapGmsh"
3841

3942
[compat]
4043
Colors = "0.9 - 0.12"
@@ -47,10 +50,11 @@ GeoParams = "0.2 - 0.5"
4750
Geodesy = "1"
4851
GeometryBasics = "0.1 - 0.4"
4952
Glob = "1.2 - 1.3"
53+
GridapGmsh = "0.5 - 0.7"
5054
ImageIO = "0.1 - 0.6"
5155
Interpolations = "0.14, 0.15"
52-
LightXML = "0.8, 0.9"
5356
JLD2 = "0.4"
57+
LightXML = "0.8, 0.9"
5458
MeshIO = "0.1 - 0.4"
5559
NearestNeighbors = "0.2 - 0.4"
5660
Parameters = "0.9 - 0.12"

ext/Gmsh_utils.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

src/GeophysicalModelGenerator.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ export import_topo, import_GeoTIFF
7171
visualise
7272
Interactive widget that allows you to explore a 3D data set `DataSet` in an interactive manner.
7373
It requires you to load `GLMakie`.
74-
"""
74+
""";
7575
function visualise end
7676
export visualise
7777

7878

79+
"""
80+
import_Gmsh(fname::String)
81+
82+
Reads a Gmsh file. Requires loading `GridapGmsh`.
83+
"""
84+
function import_Gmsh end
85+
export import_Gmsh
86+
7987

8088
end

0 commit comments

Comments
 (0)