Skip to content

Commit 1c50c5a

Browse files
authored
Merge pull request #2414 from lrtfm/load-binary-gmsh-file
Load binary gmsh file
2 parents 84d14ce + a0dcde4 commit 1c50c5a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

firedrake/mesh.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,21 @@ def _from_gmsh(filename, comm=None):
232232
COMM_WORLD).
233233
"""
234234
comm = comm or COMM_WORLD
235+
# check the filetype of the gmsh file
236+
filetype = None
237+
if comm.rank == 0:
238+
with open(filename, 'rb') as fid:
239+
header = fid.readline().rstrip(b'\n\r')
240+
version = fid.readline().rstrip(b'\n\r')
241+
assert header == b'$MeshFormat'
242+
if version.split(b' ')[1] == b'1':
243+
filetype = "binary"
244+
else:
245+
filetype = "ascii"
246+
filetype = comm.bcast(filetype, root=0)
235247
# Create a read-only PETSc.Viewer
236248
gmsh_viewer = PETSc.Viewer().create(comm=comm)
237-
gmsh_viewer.setType("ascii")
249+
gmsh_viewer.setType(filetype)
238250
gmsh_viewer.setFileMode("r")
239251
gmsh_viewer.setFileName(filename)
240252
gmsh_plex = PETSc.DMPlex().createGmsh(gmsh_viewer, comm=comm)

tests/meshes/square_binary.msh

1.29 KB
Binary file not shown.

tests/regression/test_load_mesh.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from firedrake import *
2+
import numpy as np
3+
import pytest
4+
from os.path import abspath, dirname, join
5+
6+
7+
path = join(abspath(dirname(__file__)), '..', 'meshes')
8+
9+
10+
def load_mesh(filename):
11+
m = Mesh(join(path, filename))
12+
return m
13+
14+
15+
@pytest.mark.parametrize(
16+
'filename', ['square.msh', 'square_binary.msh'])
17+
def test_load_mesh(filename):
18+
m = load_mesh(filename)
19+
v = assemble(1*dx(domain=m))
20+
assert np.allclose(v, 1)

0 commit comments

Comments
 (0)