Skip to content

Commit

Permalink
Merge pull request #99 from fusion-energy/adding-scaling-factor
Browse files Browse the repository at this point in the history
adding scale_factor that scales mesh
  • Loading branch information
shimwell authored Nov 18, 2024
2 parents 2a0596c + e9480ff commit 212ae43
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_with_conda_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/surface_mesh/cadquery_assembly_with_scaled_geometry.py
1 change: 1 addition & 0 deletions .github/workflows/ci_with_pip_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/surface_mesh/cadquery_assembly_with_scaled_geometry.py
2 changes: 1 addition & 1 deletion examples/surface_mesh/cadquery_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 14 additions & 0 deletions examples/surface_mesh/cadquery_assembly_with_scaled_geometry.py
Original file line number Diff line number Diff line change
@@ -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, scale_factor=10.0)
32 changes: 26 additions & 6 deletions src/cad_to_dagmc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,24 @@ 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

Expand Down Expand Up @@ -478,6 +486,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()
Expand All @@ -488,7 +497,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,
Expand Down Expand Up @@ -517,6 +526,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.
Expand All @@ -536,6 +546,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()
Expand All @@ -546,7 +559,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,
Expand All @@ -570,6 +583,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
Expand All @@ -590,6 +604,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
"""
Expand Down Expand Up @@ -618,7 +636,9 @@ 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,
Expand Down

0 comments on commit 212ae43

Please sign in to comment.