Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #59 from fusion-energy/develop
Browse files Browse the repository at this point in the history
adding stl import and tests
  • Loading branch information
shimwell authored Jun 7, 2022
2 parents a6fa763 + 15097b4 commit d4a9972
Show file tree
Hide file tree
Showing 8 changed files with 6,782 additions and 142 deletions.
113 changes: 61 additions & 52 deletions cad_to_h5m/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def cad_to_h5m(
surface_reflectivity_name: str = "reflective",
exo_filename: Optional[str] = None,
implicit_complement_material_tag: Optional[str] = None,
verbose: bool = True
verbose: bool = True,
autoheal: bool = False,
):
"""Converts a CAD files in STP or SAT format into a h5m file for use in
DAGMC simulations. The h5m file contains material tags associated with the
Expand Down Expand Up @@ -74,25 +75,27 @@ def cad_to_h5m(
else:
msg = (
'The h5m_filename argument should end with ".h5m". The provided '
f'h5m_filename "{h5m_filename}" does not end with .h5m')
f'h5m_filename "{h5m_filename}" does not end with .h5m'
)
raise ValueError(msg)

if exo_filename is None or Path(exo_filename).suffix == ".exo":
pass
else:
msg = (
'The exo_filename argument should end with ".exo". The provided '
f'exo_filename "{exo_filename}" does not end with .exo')
f'exo_filename "{exo_filename}" does not end with .exo'
)
raise ValueError(msg)

if cubit_filename is None or Path(cubit_filename).suffix in [
".cub", ".cub5"]:
if cubit_filename is None or Path(cubit_filename).suffix in [".cub", ".cub5"]:
pass
else:
msg = (
'The cubit_filename argument should end with ".cub" or ".cub5". '
f'The provided cubit_filename "{cubit_filename}" does not end '
' with either')
" with either"
)
raise ValueError(msg)

sys.path.append(cubit_path)
Expand All @@ -108,19 +111,21 @@ def cad_to_h5m(

cubit.init([])
if not verbose:
cubit.cmd('set echo off')
cubit.cmd('set info off')
cubit.cmd('set journal off')
cubit.cmd('set warning off')
cubit.cmd("set echo off")
cubit.cmd("set info off")
cubit.cmd("set journal off")
cubit.cmd("set warning off")

geometry_details, total_number_of_volumes = find_number_of_volumes_in_each_step_file(
files_with_tags, cubit, verbose)
(
geometry_details,
total_number_of_volumes,
) = find_number_of_volumes_in_each_step_file(
files_with_tags, cubit, verbose, autoheal
)

scale_geometry(geometry_details, cubit)
scale_geometry(geometry_details, cubit, autoheal)

tag_geometry_with_mats(
geometry_details, implicit_complement_material_tag, cubit
)
tag_geometry_with_mats(geometry_details, implicit_complement_material_tag, cubit)

if imprint and total_number_of_volumes > 1:
imprint_geometry(cubit)
Expand All @@ -145,7 +150,7 @@ def cad_to_h5m(
)

# resets cubit workspace
cubit.cmd('reset')
cubit.cmd("reset")

return h5m_filename

Expand All @@ -166,11 +171,15 @@ def create_tet_mesh(geometry_details, cubit):
cubit.cmd("mesh volume " + str(volume))


def scale_geometry(geometry_details: dict, cubit):
def scale_geometry(geometry_details: dict, cubit, autoheal):
for entry in geometry_details:
if 'scale' in entry.keys():
cubit.cmd(
f'volume {" ".join(entry["volumes"])} scale {entry["scale"]}')
if "scale" in entry.keys():
cubit.cmd(f'volume {" ".join(entry["volumes"])} scale {entry["scale"]}')

# autoheal geometry issues
if autoheal:
cubit.cmd("healer autoheal vol all")


# TODO implent a flag to allow tet file info to be saved
# def save_tet_details_to_json_file(
Expand All @@ -196,7 +205,7 @@ def save_output_files(
faceting_tolerance: float,
exo_filename: str,
cubit,
verbose: bool
verbose: bool,
):
"""This saves the output files"""
cubit.cmd("set attribute on")
Expand Down Expand Up @@ -316,16 +325,16 @@ def find_reflecting_surfaces_of_reflecting_wedge(
return geometry_details, wedge_volume


def tag_geometry_with_mats(
geometry_details, implicit_complement_material_tag, cubit
):
def tag_geometry_with_mats(geometry_details, implicit_complement_material_tag, cubit):
for entry in geometry_details:
if "material_tag" in entry.keys():

if len(entry['material_tag']) > 27:
msg = ("material_tag > 28 characters. Material tags "
"must be less than 28 characters use in DAGMC. "
f"{entry['material_tag']} is too long.")
if len(entry["material_tag"]) > 27:
msg = (
"material_tag > 28 characters. Material tags "
"must be less than 28 characters use in DAGMC. "
f"{entry['material_tag']} is too long."
)
raise ValueError(msg)

cubit.cmd(
Expand All @@ -334,7 +343,7 @@ def tag_geometry_with_mats(
+ '" add volume '
+ " ".join(entry["volumes"])
)
if entry['material_tag'].lower() == 'graveyard':
if entry["material_tag"].lower() == "graveyard":
if implicit_complement_material_tag is not None:
graveyard_volume_number = entry["volumes"][0]
cubit.cmd(
Expand All @@ -345,20 +354,25 @@ def tag_geometry_with_mats(
raise ValueError(msg)


def find_number_of_volumes_in_each_step_file(files_with_tags, cubit, verbose):
def find_number_of_volumes_in_each_step_file(files_with_tags, cubit, verbose, autoheal):
""" """
for entry in files_with_tags:
if verbose:
print(f'loading {entry["cad_filename"]}')
current_vols = cubit.parse_cubit_list("volume", "all")
if entry["cad_filename"].endswith(
".stp") or entry["cad_filename"].endswith(".step"):
if entry["cad_filename"].endswith(".stp") or entry["cad_filename"].endswith(
".step"
):
import_type = "step"
elif entry["cad_filename"].endswith(".sat"):
import_type = "acis"
elif entry["cad_filename"].endswith(".stl"):
import_type = "stl"
else:
msg = (f'File format for {entry["cad_filename"]} is not supported.'
'Try step files or sat files')
msg = (
f'File format for {entry["cad_filename"]} is not supported.'
"Try step files or sat files"
)
raise ValueError(msg)
if not Path(entry["cad_filename"]).is_file():
msg = f'File with filename {entry["cad_filename"]} could not be found'
Expand All @@ -376,33 +390,28 @@ def find_number_of_volumes_in_each_step_file(files_with_tags, cubit, verbose):
new_vols = list(map(str, new_vols))
if len(new_vols) > 1:
cubit.cmd(
"unite vol " +
" ".join(new_vols) +
" with vol " +
" ".join(new_vols))
"unite vol " + " ".join(new_vols) + " with vol " + " ".join(new_vols)
)
all_vols = cubit.parse_cubit_list("volume", "all")
new_vols_after_unite = set(
current_vols).symmetric_difference(set(all_vols))
new_vols_after_unite = set(current_vols).symmetric_difference(set(all_vols))
new_vols_after_unite = list(map(str, new_vols_after_unite))
entry["volumes"] = new_vols_after_unite
cubit.cmd(
'group "' +
short_file_name +
'" add volume ' +
" ".join(
entry["volumes"]))
'group "' + short_file_name + '" add volume ' + " ".join(entry["volumes"])
)
if "surface_reflectivity" in entry.keys():
entry["surface_reflectivity"] = find_all_surfaces_of_reflecting_wedge(
new_vols_after_unite, cubit)
new_vols_after_unite, cubit
)
if verbose:
print(
"entry['surface_reflectivity']",
entry["surface_reflectivity"])
print("entry['surface_reflectivity']", entry["surface_reflectivity"])
cubit.cmd("separate body all")

# checks the cad is clean and catches some errors with the geometry early
cubit.cmd("validate vol all")
# commented out as cmd not known see issue #3
# cubit.cmd("autoheal analyze vol all")

# autoheal geometry issues
if autoheal:
cubit.cmd("healer autoheal vol all")

return files_with_tags, sum(all_vols)
89 changes: 51 additions & 38 deletions examples/create_h5m_from_download_stp_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,54 @@
tar.extractall()
tar.close()

cad_to_h5m(h5m_filename="dagmc.h5m",
verbose=False,
cubit_path="/opt/Coreform-Cubit-2021.5/bin/",
files_with_tags=[{"material_tag": "pf_coil_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/pf_coils.stp",
},
{"material_tag": "pf_coil_case_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/pf_coil_cases.stp",
},
{"material_tag": "center_column_shield_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/center_column_shield.stp",
},
{"material_tag": "firstwall_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/outboard_firstwall.stp",
},
{"material_tag": "blanket_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/blanket.stp",
},
{"material_tag": "divertor_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/divertor.stp",
},
{"material_tag": "supports_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/supports.stp",
},
{"material_tag": "blanket_rear_wall_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/outboard_rear_blanket_wall.stp",
},
{"material_tag": "inboard_tf_coils_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/inboard_tf_coils.stp",
},
{"material_tag": "outer_tf_coil_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/outboard_tf_coil.stp",
},
{"material_tag": "graveyard",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/graveyard.stp",
},
],
)
cad_to_h5m(
h5m_filename="dagmc.h5m",
verbose=False,
cubit_path="/opt/Coreform-Cubit-2021.5/bin/",
files_with_tags=[
{
"material_tag": "pf_coil_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/pf_coils.stp",
},
{
"material_tag": "pf_coil_case_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/pf_coil_cases.stp",
},
{
"material_tag": "center_column_shield_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/center_column_shield.stp",
},
{
"material_tag": "firstwall_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/outboard_firstwall.stp",
},
{
"material_tag": "blanket_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/blanket.stp",
},
{
"material_tag": "divertor_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/divertor.stp",
},
{
"material_tag": "supports_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/supports.stp",
},
{
"material_tag": "blanket_rear_wall_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/outboard_rear_blanket_wall.stp",
},
{
"material_tag": "inboard_tf_coils_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/inboard_tf_coils.stp",
},
{
"material_tag": "outer_tf_coil_mat",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/outboard_tf_coil.stp",
},
{
"material_tag": "graveyard",
"cad_filename": "fusion_example_for_openmc_using_paramak-0.0.1/stp_files/graveyard.stp",
},
],
)
20 changes: 10 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
url="https://github.com/fusion-energy/cad_to_h5m",
packages=setuptools.find_packages(),
classifiers=[
'Natural Language :: English',
'Topic :: Scientific/Engineering',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
"Natural Language :: English",
"Topic :: Scientific/Engineering",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
python_requires=">=3.6",
)
Loading

0 comments on commit d4a9972

Please sign in to comment.