Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating tests and material finding method to h5py #23

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ dmypy.json
tests/fusion_example_for_openmc_using_paramak-0.0.1/
*.h5m
*.json
*.vtk

# python version file
_version.py
54 changes: 38 additions & 16 deletions dagmc_h5m_file_inspector/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_groups(mbcore):

def get_materials_from_h5m(
filename: str, remove_prefix: Optional[bool] = True
) -> List[str]:
): # -> List[str]:
"""Reads in a DAGMC h5m file and uses PyMoab to find the material tags in
the file.

Expand All @@ -81,21 +81,43 @@ def get_materials_from_h5m(
A list of material tags
"""

mbcore = load_moab_file(filename)
group_ents = get_groups(mbcore)
name_tag = mbcore.tag_get_handle(mb.types.NAME_TAG_NAME)

materials_list = []
for group_ent in group_ents:

group_name = mbcore.tag_get_data(name_tag, group_ent)[0][0]
if group_name.startswith("mat:"):
if remove_prefix:
materials_list.append(group_name[4:])
else:
materials_list.append(group_name)

return sorted(set(materials_list))
import h5py
import numpy as np

dagmc_file_contents = h5py.File(filename)
material_group = dagmc_file_contents["/tstt/tags/NAME"]
material_tags_hex = material_group.get("values")
material_tags_ascii = []
for tag in material_tags_hex:
raw_tag_in_hex = np.array2string(tag)
print(raw_tag_in_hex)
tag_in_hex = raw_tag_in_hex.replace("\\x00", "")
print(tag_in_hex)
tag_in_hex = tag_in_hex.replace("\\x", "")
tag_in_hex = tag_in_hex.lstrip("b'")
tag_in_hex = tag_in_hex.rstrip("'")
print(tag_in_hex)
tag_in_asci = bytes.fromhex(tag_in_hex)
print(tag_in_asci)
material_tags_ascii.append(tag_in_asci.decode())

return sorted(set(material_tags_ascii))

# mbcore = load_moab_file(filename)
# group_ents = get_groups(mbcore)
# name_tag = mbcore.tag_get_handle(mb.types.NAME_TAG_NAME)

# materials_list = []
# for group_ent in group_ents:

# group_name = mbcore.tag_get_data(name_tag, group_ent)[0][0]
# if group_name.startswith("mat:"):
# if remove_prefix:
# materials_list.append(group_name[4:])
# else:
# materials_list.append(group_name)

# return sorted(set(materials_list))


def get_vol_mat_map(group_ents, mbcore, remove_prefix) -> dict:
Expand Down
1 change: 0 additions & 1 deletion dagmc_h5m_file_inspector/inspect-dagmc-h5m-file
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import argparse
import json
import pprint
from pathlib import Path

from dagmc_h5m_file_inspector import get_volumes_from_h5m, get_materials_from_h5m, get_volumes_and_materials_from_h5m

Expand Down
37 changes: 37 additions & 0 deletions tests/create_h5m_for_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# creates a dagmc geometry with material tags
# there is one material tag "material1", two "material2" and three "material3"
# the order is not sorted

import cadquery as cq
from cadquery import Assembly
from cad_to_dagmc import CadToDagmc


result1 = cq.Workplane().box(1, 1, 1) # material1
result2 = cq.Workplane().moveTo(1, 0).box(1, 1, 1) # material2
result3 = cq.Workplane().moveTo(2, 0).box(1, 1, 1) # material3
result4 = cq.Workplane().moveTo(3, 0).box(1, 1, 1) # material2
result5 = cq.Workplane().moveTo(4, 0).box(1, 1, 1) # material3
result6 = cq.Workplane().moveTo(5, 0).box(1, 1, 1) # material3

assembly = Assembly()
assembly.add(result1)
assembly.add(result2)
assembly.add(result3)
assembly.add(result4)
assembly.add(result5)
assembly.add(result6)

my_model = CadToDagmc()
my_model.add_cadquery_object(
assembly,
material_tags=[
"material1",
"material2",
"material3",
"material2",
"material3",
"material3",
],
)
my_model.export_dagmc_h5m_file("dagmc.h5m", min_mesh_size=0.5, max_mesh_size=1.0)
26 changes: 7 additions & 19 deletions tests/test_command_line_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,11 @@


class TestReactor(unittest.TestCase):
def setUp(self):

if not Path("tests/v0.0.1.tar.gz").is_file():
url = "https://github.com/Shimwell/fusion_example_for_openmc_using_paramak/archive/refs/tags/v0.0.1.tar.gz"
urllib.request.urlretrieve(url, "tests/v0.0.1.tar.gz")

tar = tarfile.open("tests/v0.0.1.tar.gz", "r:gz")
tar.extractall("tests")
tar.close()

def test_volume_finding(self):
"""Tests command runs and produces an output files with the correct contents"""
os.system("rm vols.json")
os.system(
"inspect-dagmc-h5m-file -i tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m -v -o vols.json"
)
assert Path("vols.json").is_file()
os.system("inspect-dagmc-h5m-file -i tests/dagmc.h5m -v -o vols.json")
# assert Path("vols.json").is_file()
with open("vols.json") as jsonFile:
jsonObject = json.load(jsonFile)
assert jsonObject == {
Expand Down Expand Up @@ -58,9 +46,9 @@ def test_material_finding(self):
"""Tests command runs and produces an output files with the correct contents"""
os.system("rm vols.json")
os.system(
"inspect-dagmc-h5m-file -i tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m -m -o mats.json"
"inspect-dagmc-h5m-file -i tests/magnetic_fusion_openmc_dagmc_paramak_example-0.0.1/dagmc.h5m -m -o mats.json"
)
assert Path("mats.json").is_file()
# assert Path("mats.json").is_file()
with open("mats.json") as jsonFile:
jsonObject = json.load(jsonFile)
assert jsonObject == {
Expand All @@ -71,9 +59,9 @@ def test_both_finding(self):
"""Tests command runs and produces an output files with the correct contents"""
os.system("rm vols.json")
os.system(
"inspect-dagmc-h5m-file -i tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m -b -o both.json"
"inspect-dagmc-h5m-file -i tests/magnetic_fusion_openmc_dagmc_paramak_example-0.0.1/dagmc.h5m -b -o both.json"
)
assert Path("both.json").is_file()
# assert Path("both.json").is_file()
with open("both.json") as jsonFile:
jsonObject = json.load(jsonFile)
assert jsonObject == {
Expand Down Expand Up @@ -107,7 +95,7 @@ def test_no_user_output(self):
"""Tests command runs and produces an output files with the correct contents"""
os.system("rm empty.json")
os.system(
"inspect-dagmc-h5m-file -i tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m -o empty.json"
"inspect-dagmc-h5m-file -i tests/magnetic_fusion_openmc_dagmc_paramak_example-0.0.1/dagmc.h5m -o empty.json"
)
assert Path("empty.json").is_file() == False

Expand Down
18 changes: 9 additions & 9 deletions tests/test_python_api_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
class TestApiUsage(unittest.TestCase):
def setUp(self):

if not Path("tests/v0.0.1.tar.gz").is_file():
url = "https://github.com/Shimwell/fusion_example_for_openmc_using_paramak/archive/refs/tags/v0.0.1.tar.gz"
urllib.request.urlretrieve(url, "tests/v0.0.1.tar.gz")
if not Path("tests/v0.2.0.tar.gz").is_file():
url = "https://github.com/fusion-energy/magnetic_fusion_openmc_dagmc_paramak_example/archive/refs/tags/v0.2.0.tar.gz"
urllib.request.urlretrieve(url, "tests/v0.2.0.tar.gz")

tar = tarfile.open("tests/v0.0.1.tar.gz", "r:gz")
tar = tarfile.open("tests/v0.2.0.tar.gz", "r:gz")
tar.extractall("tests")
tar.close()

Expand All @@ -22,7 +22,7 @@ def test_volume_and_material_extraction_without_stripped_prefix(self):
checks the contents match the expected contents"""

dict_of_vol_and_mats = di.get_volumes_and_materials_from_h5m(
filename="tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m",
filename="tests/magnetic_fusion_openmc_dagmc_paramak_example-0.2.0/dagmc.h5m",
remove_prefix=False,
)

Expand Down Expand Up @@ -56,7 +56,7 @@ def test_volume_and_material_extraction_remove_prefix(self):
checks the contents match the expected contents"""

dict_of_vol_and_mats = di.get_volumes_and_materials_from_h5m(
"tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m"
"tests/magnetic_fusion_openmc_dagmc_paramak_example-0.2.0/dagmc.h5m"
)

assert dict_of_vol_and_mats == {
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_volume_extraction(self):
match the expected contents"""

dict_of_vol_and_mats = di.get_volumes_from_h5m(
"tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m"
"tests/magnetic_fusion_openmc_dagmc_paramak_example-0.2.0/dagmc.h5m"
)

assert dict_of_vol_and_mats == [
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_material_extraction_no_remove_prefix(self):
contents match the expected contents"""

dict_of_vol_and_mats = di.get_materials_from_h5m(
filename="tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m",
filename="tests/magnetic_fusion_openmc_dagmc_paramak_example-0.2.0/dagmc.h5m",
remove_prefix=False,
)

Expand All @@ -140,7 +140,7 @@ def test_material_extraction_remove_prefix(self):
contents match the expected contents"""

dict_of_vol_and_mats = di.get_materials_from_h5m(
"tests/fusion_example_for_openmc_using_paramak-0.0.1/dagmc.h5m"
"tests/magnetic_fusion_openmc_dagmc_paramak_example-0.2.0/dagmc.h5m"
)

assert dict_of_vol_and_mats == [
Expand Down