Skip to content

Commit

Permalink
workaround for xdmf export
Browse files Browse the repository at this point in the history
  • Loading branch information
aradermacher committed Oct 18, 2023
1 parent 5b0bd20 commit 63e425a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
50 changes: 39 additions & 11 deletions amworkflow/meshing/meshing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import typing
from pathlib import Path

import meshio

import gmsh
import multiprocessing
from OCC.Core.TopoDS import TopoDS_Solid
Expand Down Expand Up @@ -101,20 +103,46 @@ def create(
phy_gp = model.getPhysicalGroups()
model_name = model.get_current()

# save
msh, cell_markers, facet_markers = gmshio.model_to_mesh(model, MPI.COMM_SELF, 0)
msh.name = model_name
cell_markers.name = f"{msh.name}_cells"
facet_markers.name = f"{msh.name}_facets"
with XDMFFile(msh.comm, out_xdmf, "w") as file:
file.write_mesh(msh)
file.write_meshtags(cell_markers)
msh.topology.create_connectivity(msh.topology.dim - 1, msh.topology.dim)
file.write_meshtags(facet_markers)
# # save
# msh, cell_markers, facet_markers = gmshio.model_to_mesh(model, MPI.COMM_SELF, 0)
# msh.name = model_name
# cell_markers.name = f"{msh.name}_cells"
# facet_markers.name = f"{msh.name}_facets"
# with XDMFFile(msh.comm, out_xdmf, "w") as file:
# file.write_mesh(msh)
# file.write_meshtags(cell_markers)
# msh.topology.create_connectivity(msh.topology.dim - 1, msh.topology.dim)
# file.write_meshtags(facet_markers)

#workaround for since gmshio.model_to_mesh is not working
out_msh = out_xdmf.with_suffix(".msh")
gmsh.write(str(out_msh))
msh = meshio.read(out_msh)
mesh = self.create_mesh(msh, "tetra")
meshio.write(out_xdmf, mesh)

if out_vtk:
gmsh.write(str(out_vtk))
gmsh.write(str(out_vtk).replace('.vtk', '.msh')) # additional msh output for debugging


return

def create_mesh(self, mesh, cell_type: str, prune_z: bool = False) -> meshio.Mesh:
"""Convert msh file to xdmf file for fenic
based on https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html?highlight=read_mesh
Args:
mesh:
cell_type:
prune_z:
Returns:
"""
cells = mesh.get_cells_type(cell_type)
cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
points = mesh.points[:, :2] if prune_z else mesh.points
out_mesh = meshio.Mesh(
points=points, cells={cell_type: cells}, cell_data={"name_to_read": [cell_data]}
)
return out_mesh
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies:
- sqlite
- numpy
- fenics-dolfinx=0.6.0
- meshio
- mpich
- pyvista
- pip
Expand Down

0 comments on commit 63e425a

Please sign in to comment.