Skip to content

Commit

Permalink
Merge pull request #101 from fusion-energy/faster-unstrucutred-mesh-p…
Browse files Browse the repository at this point in the history
…roduction

Faster unstrucutred mesh file production by writing vtk file
  • Loading branch information
shimwell authored Nov 27, 2024
2 parents 61ddaf2 + 02c433f commit 47af923
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def gear(t, r1=4, r2=1):
my_model.add_cadquery_object(result, material_tags=["mat1"])
my_model.add_cadquery_object(result2, material_tags=["mat2"])

my_model.export_unstructured_mesh_file(filename="umesh.h5m", max_mesh_size=1, min_mesh_size=0.1)
my_model.export_unstructured_mesh_file(filename="umesh.vtk", max_mesh_size=1, min_mesh_size=0.1)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# script assumes that "umesh.h5m" has been created by
# script assumes that "umesh.vtk" has been created by
# curved_cadquery_object_to_dagmc_volume_mesh.py has been

import openmc
Expand All @@ -14,7 +14,7 @@
)
openmc.config["cross_sections"] = "cross_sections.xml"

umesh = openmc.UnstructuredMesh("umesh.h5m", library="moab")
umesh = openmc.UnstructuredMesh("umesh.vtk", library="moab")
mesh_filter = openmc.MeshFilter(umesh)
tally = openmc.Tally(name="unstrucutred_mesh_tally")
tally.filters = [mesh_filter]
Expand Down
49 changes: 41 additions & 8 deletions src/cad_to_dagmc/core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
import cadquery as cq
import gmsh
import numpy as np
from cadquery import importers, exporters
from cadquery import importers
from pymoab import core, types
import tempfile

Expand Down Expand Up @@ -484,13 +485,48 @@ def add_cadquery_object(

def export_unstructured_mesh_file(
self,
filename: str = "umesh.h5m",
filename: str = "umesh.vtk",
min_mesh_size: float = 1,
max_mesh_size: float = 5,
mesh_algorithm: int = 1,
method: str = "file",
scale_factor: float = 1.0,
):
"""
Exports an unstructured mesh file in VTK format for use with openmc.UnstructuredMesh.
Compatible with the MOAB unstructured mesh library. Example useage
openmc.UnstructuredMesh(filename="umesh.vtk", library="moab")
Parameters:
-----------
filename : str, optional
The name of the output file. Default is "umesh.vtk".
min_mesh_size: the minimum mesh element size to use in Gmsh. Passed
into gmsh.option.setNumber("Mesh.MeshSizeMin", min_mesh_size)
max_mesh_size: the maximum mesh element size to use in Gmsh. Passed
into gmsh.option.setNumber("Mesh.MeshSizeMax", max_mesh_size)
mesh_algorithm: The Gmsh mesh algorithm number to use. Passed into
gmsh.option.setNumber("Mesh.Algorithm", mesh_algorithm)
method: the method to use to import the geometry into gmsh. Options
are 'file' or 'in memory'. 'file' is the default and will write
the geometry to a temporary file before importing it into gmsh.
'in memory' will import the geometry directly into gmsh but
requires the version of OpenCASCADE used to build gmsh to be
the same as the version used by CadQuery. This is possible to
ensure when installing the package with Conda but harder when
installing from PyPI.
scale_factor: a scaling factor to apply to the geometry that can be
used to enlarge or shrink the geometry. Useful when converting
Useful when converting the geometry to cm for use in neutronics
Returns:
--------
gmsh : gmsh
The gmsh object after finalizing the mesh.
"""

if Path(filename).suffix != ".vtk":
raise ValueError("Unstructured mesh filename must have a .vtk extension")

assembly = cq.Assembly()
for part in self.parts:
Expand All @@ -510,12 +546,9 @@ def export_unstructured_mesh_file(
dimensions=3,
)

# gmesh writes out a vtk file that is converted by pymoab into a h5 file
gmsh.write(filename + ".vtk")

moab_core = core.Core()
moab_core.load_file(filename + ".vtk")
moab_core.write_file(filename)
# gmesh writes out a vtk file that is accepted by openmc.UnstructuredMesh
# The library argument must be set to "moab"
gmsh.write(filename)

gmsh.finalize()

Expand Down

0 comments on commit 47af923

Please sign in to comment.