Skip to content

Commit

Permalink
Add automatic conversion to Exodus via executable
Browse files Browse the repository at this point in the history
  • Loading branch information
aeslaughter committed Sep 10, 2021
1 parent bd4d240 commit 7f57ea2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
37 changes: 36 additions & 1 deletion python/terrain/terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import dataclasses
import pandas
import numpy
import tempfile
import subprocess
import scipy.interpolate
import gmsh


@dataclasses.dataclass
class TerrainData:
"""
Expand All @@ -29,6 +32,9 @@ def get_arguments():
parser.add_argument("-o", "--output", type=str,
help="The name of the Gmsh mesh file to create.")

parser.add_argument("--exodus", action='store_true',
help="Execute falcon with --mesh-only option to convert gmsh format to Exodus.")

parser.add_argument("--show", action='store_true',
help="Open the interactive Gmsh window.")

Expand Down Expand Up @@ -164,7 +170,13 @@ def main():

gmsh.model.mesh.generate()

gmsh.write(args.output)
# Output the msh to .msh format and Exodus format via falcon executable
if args.output is not None:
gmsh.write(args.output)

if args.exodus is not None:
convert_to_exodus(args.output)

if args.show:
gmsh.fltk.run()

Expand Down Expand Up @@ -280,3 +292,26 @@ def build_side_surfaces(bot_lines: list, bot_points: numpy.ndarray,

gmsh.model.occ.synchronize()
return surfaces

def convert_to_exodus(mesh_name):
"""
Use falcon executable to convert supplied gmsh output in *mesh_name* to Exodus.
"""
exe = os.path.join(os.path.dirname(__file__), '..', '..', 'falcon-{}'.format(os.getenv("METHOD", 'opt')))
if not os.path.isfile(exe):
msg = "The falcon executable does not exist, cannot convert to Exodus format."
raise IOError(msg)

template_file = os.path.join(os.path.dirname(__file__), 'terrain_to_exodus.i.template')
with open(template_file, 'r') as fid:
template = fid.read()

template = template.replace('GMSH_FILENAME', mesh_name)

input_file = 'terrain_to_exodus.i'
with open(input_file, 'w') as fid:
fid.write(template)

subprocess.run([exe, '-i', input_file, '--mesh-only'], check=True)

os.rename('terrain_to_exodus_in.e', '{}.e'.format(os.path.splitext(mesh_name)[0]))
4 changes: 4 additions & 0 deletions python/terrain/terrain_to_exodus.i.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Mesh/gmsh]
type = FileMeshGenerator
file = GMSH_FILENAME
[]
1 change: 1 addition & 0 deletions test/tests/terrain/gold/terrain.e
4 changes: 4 additions & 0 deletions test/tests/terrain/terrain_to_exodus.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Mesh/gmsh]
type = FileMeshGenerator
file = terrain.msh
[]
6 changes: 6 additions & 0 deletions test/tests/terrain/tests
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
command = '../../../scripts/mesh_terrain.py -o terrain.msh bottom_40m.csv granitoid_40m.csv surface_40m.csv --mesh_size 500 100 200'
required_python_packages = 'pandas numpy scipy gmsh'
[]
[mesh_to_exodus]
type = Exodiff
command = '../../../scripts/mesh_terrain.py -o terrain.msh --exodus bottom_40m.csv granitoid_40m.csv surface_40m.csv --mesh_size 500 100 200'
exodiff = terrain.e
required_python_packages = 'pandas numpy scipy gmsh'
[]
[sim]
type = RunApp
input = terrain.i
Expand Down
Binary file added tests/terrain/gold/terrain.e
Binary file not shown.

0 comments on commit 7f57ea2

Please sign in to comment.