From e9ce7c7f0d7fb70933d7c42cbacfbf3f8404fb1a Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 27 Nov 2024 14:14:21 +0100 Subject: [PATCH 1/2] no need to write vtk and convert to h5 --- src/cad_to_dagmc/core.py | 43 +++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/cad_to_dagmc/core.py b/src/cad_to_dagmc/core.py index 304ac0a..d3d32fd 100644 --- a/src/cad_to_dagmc/core.py +++ b/src/cad_to_dagmc/core.py @@ -484,13 +484,45 @@ 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 unstrucutred 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. + """ assembly = cq.Assembly() for part in self.parts: @@ -510,12 +542,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() From 02c433f3046f4184d93b8dd3270b9f0216e3ae55 Mon Sep 17 00:00:00 2001 From: shimwell Date: Wed, 27 Nov 2024 15:06:09 +0100 Subject: [PATCH 2/2] added file suffix check --- .../curved_cadquery_object_to_dagmc_volume_mesh.py | 2 +- .../simulate_unstrucutred_volume_mesh_with_openmc.py | 4 ++-- src/cad_to_dagmc/core.py | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py b/examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py index d5bbfe5..d315be9 100644 --- a/examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py +++ b/examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py @@ -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) diff --git a/examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py b/examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py index b965078..195d1ff 100644 --- a/examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py +++ b/examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py @@ -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 @@ -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] diff --git a/src/cad_to_dagmc/core.py b/src/cad_to_dagmc/core.py index d3d32fd..80d130b 100644 --- a/src/cad_to_dagmc/core.py +++ b/src/cad_to_dagmc/core.py @@ -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 @@ -493,7 +494,7 @@ def export_unstructured_mesh_file( ): """ Exports an unstructured mesh file in VTK format for use with openmc.UnstructuredMesh. - Compatible with the MOAB unstrucutred mesh library. Example useage + Compatible with the MOAB unstructured mesh library. Example useage openmc.UnstructuredMesh(filename="umesh.vtk", library="moab") Parameters: @@ -524,6 +525,9 @@ def export_unstructured_mesh_file( 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: assembly.add(part)