From 54963839235e05f5ab7d03d7cb675e05284a2a0d Mon Sep 17 00:00:00 2001 From: shimwell Date: Fri, 15 Nov 2024 16:24:38 +0100 Subject: [PATCH 1/6] adding scale_factor --- examples/surface_mesh/cadquery_assembly.py | 2 +- src/cad_to_dagmc/core.py | 32 ++++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/examples/surface_mesh/cadquery_assembly.py b/examples/surface_mesh/cadquery_assembly.py index b0204e7..c4303bd 100644 --- a/examples/surface_mesh/cadquery_assembly.py +++ b/examples/surface_mesh/cadquery_assembly.py @@ -10,4 +10,4 @@ my_model = CadToDagmc() my_model.add_cadquery_object(cadquery_object=assembly, material_tags=["mat1", "mat2"]) -my_model.export_dagmc_h5m_file(min_mesh_size=0.5, max_mesh_size=1.0) +my_model.export_dagmc_h5m_file(min_mesh_size=0.5, max_mesh_size=1.0e6) diff --git a/src/cad_to_dagmc/core.py b/src/cad_to_dagmc/core.py index d193725..c4fe133 100644 --- a/src/cad_to_dagmc/core.py +++ b/src/cad_to_dagmc/core.py @@ -190,16 +190,26 @@ def _vertices_to_h5m( return h5m_filename -def get_volumes(gmsh, assembly, method="file"): +def get_volumes(gmsh, assembly, method="file", scale_factor=1.0): if method == "in memory": volumes = gmsh.model.occ.importShapesNativePointer(assembly.wrapped._address()) - gmsh.model.occ.synchronize() + elif method == "file": with tempfile.NamedTemporaryFile(suffix=".step") as temp_file: exporters.export(assembly, temp_file.name) volumes = gmsh.model.occ.importShapes(temp_file.name) - gmsh.model.occ.synchronize() + + # updating the model to ensure the entities in the geometry are found + gmsh.model.occ.synchronize() + + if scale_factor != 1.0: + dim_tags = gmsh.model.getEntities(3) + gmsh.model.occ.dilate( + dim_tags, 0.0, 0.0, 0.0, scale_factor, scale_factor, scale_factor + ) + # update the model to ensure the scaling factor has been applied + gmsh.model.occ.synchronize() return gmsh, volumes @@ -478,6 +488,7 @@ def export_unstructured_mesh_file( max_mesh_size: float = 5, mesh_algorithm: int = 1, method: str = "file", + scale_factor: float = 1.0, ): assembly = cq.Assembly() @@ -488,7 +499,7 @@ def export_unstructured_mesh_file( gmsh = init_gmsh() - gmsh, _ = get_volumes(gmsh, imprinted_assembly, method=method) + gmsh, _ = get_volumes(gmsh, imprinted_assembly, method=method, scale_factor=scale_factor) gmsh = _mesh_brep( gmsh=gmsh, @@ -517,6 +528,7 @@ def export_gmsh_mesh_file( mesh_algorithm: int = 1, dimensions: int = 2, method: str = "file", + scale_factor: float = 1.0, ): """Saves a GMesh msh file of the geometry in either 2D surface mesh or 3D volume mesh. @@ -536,6 +548,9 @@ def export_gmsh_mesh_file( 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 """ assembly = cq.Assembly() @@ -546,7 +561,7 @@ def export_gmsh_mesh_file( gmsh = init_gmsh() - gmsh, _ = get_volumes(gmsh, imprinted_assembly, method=method) + gmsh, _ = get_volumes(gmsh, imprinted_assembly, method=method, scale_factor=scale_factor) gmsh = _mesh_brep( gmsh=gmsh, @@ -570,6 +585,7 @@ def export_dagmc_h5m_file( mesh_algorithm: int = 1, implicit_complement_material_tag: str | None = None, method: str = "file", + scale_factor: float = 1.0, ) -> str: """Saves a DAGMC h5m file of the geometry @@ -590,6 +606,10 @@ def export_dagmc_h5m_file( 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: str: the DAGMC filename saved """ @@ -618,7 +638,7 @@ def export_dagmc_h5m_file( gmsh = init_gmsh() - gmsh, volumes = get_volumes(gmsh, imprinted_assembly, method=method) + gmsh, volumes = get_volumes(gmsh, imprinted_assembly, method=method, scale_factor=scale_factor) gmsh = _mesh_brep( gmsh=gmsh, From 23a7d72c4cd3e4caab17f3f9990d55fc09f6ae12 Mon Sep 17 00:00:00 2001 From: shimwell Date: Fri, 15 Nov 2024 15:25:00 +0000 Subject: [PATCH 2/6] [skip ci] Apply formatting changes --- src/cad_to_dagmc/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cad_to_dagmc/core.py b/src/cad_to_dagmc/core.py index c4fe133..16899ff 100644 --- a/src/cad_to_dagmc/core.py +++ b/src/cad_to_dagmc/core.py @@ -205,9 +205,7 @@ def get_volumes(gmsh, assembly, method="file", scale_factor=1.0): if scale_factor != 1.0: dim_tags = gmsh.model.getEntities(3) - gmsh.model.occ.dilate( - dim_tags, 0.0, 0.0, 0.0, scale_factor, scale_factor, scale_factor - ) + gmsh.model.occ.dilate(dim_tags, 0.0, 0.0, 0.0, scale_factor, scale_factor, scale_factor) # update the model to ensure the scaling factor has been applied gmsh.model.occ.synchronize() @@ -638,7 +636,9 @@ def export_dagmc_h5m_file( gmsh = init_gmsh() - gmsh, volumes = get_volumes(gmsh, imprinted_assembly, method=method, scale_factor=scale_factor) + gmsh, volumes = get_volumes( + gmsh, imprinted_assembly, method=method, scale_factor=scale_factor + ) gmsh = _mesh_brep( gmsh=gmsh, From ef3df46461b7c7679974ea7015cfea9344dcd58a Mon Sep 17 00:00:00 2001 From: shimwell Date: Fri, 15 Nov 2024 16:45:03 +0100 Subject: [PATCH 3/6] added scaling example --- .github/workflows/ci_with_conda_install.yml | 1 + .github/workflows/ci_with_pip_install.yml | 1 + .../cadquery_assembly_with_scaled_geometry.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+) create mode 100644 examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py diff --git a/.github/workflows/ci_with_conda_install.yml b/.github/workflows/ci_with_conda_install.yml index 337d250..1de7806 100644 --- a/.github/workflows/ci_with_conda_install.yml +++ b/.github/workflows/ci_with_conda_install.yml @@ -62,3 +62,4 @@ jobs: python examples/surface_mesh/from_gmsh_mesh_file.py python examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py python examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py + python examples/cadquery_assembly_with_scaled_geometry.py diff --git a/.github/workflows/ci_with_pip_install.yml b/.github/workflows/ci_with_pip_install.yml index 8affa04..0b992a0 100644 --- a/.github/workflows/ci_with_pip_install.yml +++ b/.github/workflows/ci_with_pip_install.yml @@ -58,3 +58,4 @@ jobs: python examples/surface_mesh/from_gmsh_mesh_file.py python examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py python examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py + python examples/cadquery_assembly_with_scaled_geometry.py \ No newline at end of file diff --git a/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py b/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py new file mode 100644 index 0000000..2631b95 --- /dev/null +++ b/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py @@ -0,0 +1,14 @@ +import cadquery as cq +from cad_to_dagmc import CadToDagmc + +result = cq.Workplane().sphere(5) +result2 = cq.Workplane().moveTo(10, 0).sphere(2) + +assembly = cq.Assembly() +assembly.add(result) +assembly.add(result2) + +my_model = CadToDagmc() +my_model.add_cadquery_object(cadquery_object=assembly, material_tags=["mat1", "mat2"]) +# scales geometry to makie it 10 times bigger +my_model.export_dagmc_h5m_file(min_mesh_size=0.5, max_mesh_size=1.0e6, scaling_factor=10.0) From 774ad322a93bc3ff0ec8f005c3d4ef366c54c4e7 Mon Sep 17 00:00:00 2001 From: shimwell Date: Fri, 15 Nov 2024 16:50:55 +0100 Subject: [PATCH 4/6] added missing subfolder --- .github/workflows/ci_with_conda_install.yml | 2 +- .github/workflows/ci_with_pip_install.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_with_conda_install.yml b/.github/workflows/ci_with_conda_install.yml index 1de7806..4f1d83c 100644 --- a/.github/workflows/ci_with_conda_install.yml +++ b/.github/workflows/ci_with_conda_install.yml @@ -62,4 +62,4 @@ jobs: python examples/surface_mesh/from_gmsh_mesh_file.py python examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py python examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py - python examples/cadquery_assembly_with_scaled_geometry.py + python examples/surface/cadquery_assembly_with_scaled_geometry.py diff --git a/.github/workflows/ci_with_pip_install.yml b/.github/workflows/ci_with_pip_install.yml index 0b992a0..d2328f3 100644 --- a/.github/workflows/ci_with_pip_install.yml +++ b/.github/workflows/ci_with_pip_install.yml @@ -58,4 +58,4 @@ jobs: python examples/surface_mesh/from_gmsh_mesh_file.py python examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py python examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py - python examples/cadquery_assembly_with_scaled_geometry.py \ No newline at end of file + python examples/surface/cadquery_assembly_with_scaled_geometry.py \ No newline at end of file From d5d193f00dea8b9786bc728a0e901ec0e7f4654c Mon Sep 17 00:00:00 2001 From: shimwell Date: Fri, 15 Nov 2024 17:22:57 +0100 Subject: [PATCH 5/6] corrected folder path --- .github/workflows/ci_with_conda_install.yml | 2 +- .github/workflows/ci_with_pip_install.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_with_conda_install.yml b/.github/workflows/ci_with_conda_install.yml index 4f1d83c..14c1642 100644 --- a/.github/workflows/ci_with_conda_install.yml +++ b/.github/workflows/ci_with_conda_install.yml @@ -62,4 +62,4 @@ jobs: python examples/surface_mesh/from_gmsh_mesh_file.py python examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py python examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py - python examples/surface/cadquery_assembly_with_scaled_geometry.py + python examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py diff --git a/.github/workflows/ci_with_pip_install.yml b/.github/workflows/ci_with_pip_install.yml index d2328f3..820866f 100644 --- a/.github/workflows/ci_with_pip_install.yml +++ b/.github/workflows/ci_with_pip_install.yml @@ -58,4 +58,4 @@ jobs: python examples/surface_mesh/from_gmsh_mesh_file.py python examples/unstrucutred_volume_mesh/curved_cadquery_object_to_dagmc_volume_mesh.py python examples/unstrucutred_volume_mesh/simulate_unstrucutred_volume_mesh_with_openmc.py - python examples/surface/cadquery_assembly_with_scaled_geometry.py \ No newline at end of file + python examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py \ No newline at end of file From e9480ff033c2f66e2a27b7a88e71c424faf40d5c Mon Sep 17 00:00:00 2001 From: shimwell Date: Sat, 16 Nov 2024 23:30:46 +0100 Subject: [PATCH 6/6] corrected arg name --- examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py b/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py index 2631b95..9a260ac 100644 --- a/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py +++ b/examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py @@ -11,4 +11,4 @@ my_model = CadToDagmc() my_model.add_cadquery_object(cadquery_object=assembly, material_tags=["mat1", "mat2"]) # scales geometry to makie it 10 times bigger -my_model.export_dagmc_h5m_file(min_mesh_size=0.5, max_mesh_size=1.0e6, scaling_factor=10.0) +my_model.export_dagmc_h5m_file(min_mesh_size=0.5, max_mesh_size=1.0e6, scale_factor=10.0)