Skip to content

Commit

Permalink
updated tests for mat tag location
Browse files Browse the repository at this point in the history
  • Loading branch information
shimwell committed Apr 13, 2024
1 parent 5b82cd2 commit ccb94ce
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
7 changes: 5 additions & 2 deletions examples/surface_mesh/cadquery_object_and_stp_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

my_model = CadToDagmc()

my_model.add_cadquery_object(cadquery_object=result)
my_model.add_cadquery_object(
cadquery_object=result,
material_tags=["mat1"],
)

my_model.add_stp_file(
filename="single_cube.stp",
scale_factor=0.1,
material_tags=["mat1", "mat2"],
material_tags=["mat2"],
)

my_model.export_dagmc_h5m_file(
Expand Down
14 changes: 8 additions & 6 deletions examples/surface_mesh/cadquery_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@


my_model = CadToDagmc()
my_model.add_cadquery_object(cadquery_object=text)

my_model.export_dagmc_h5m_file(
filename="cadquery_text.h5m",
max_mesh_size=0.2,
min_mesh_size=0.1,
my_model.add_cadquery_object(
cadquery_object=text,
material_tags=[
"mat1",
"mat2",
Expand All @@ -19,3 +15,9 @@
"mat5",
], # 5 volumes one for each letter
)

my_model.export_dagmc_h5m_file(
filename="cadquery_text.h5m",
max_mesh_size=0.2,
min_mesh_size=0.1,
)
11 changes: 7 additions & 4 deletions src/cad_to_dagmc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ def _check_material_tags(material_tags, iterable_solids):
f"and {len(material_tags)} material_tags found"
)
raise ValueError(msg)
for material_tag in material_tags:
if not isinstance(material_tag, str):
msg = f"material_tags should be an iterable of strings."
raise ValueError(msg)
for material_tag in material_tags:
if not isinstance(material_tag, str):
msg = f"material_tags should be an iterable of strings."
raise ValueError(msg)


def order_material_ids_by_brep_order(original_ids, scrambled_id, material_tags):
Expand All @@ -331,6 +331,9 @@ class MeshToDagmc:
def __init__(self, filename: str):
self.filename = filename

# TODO add export_unstructured_mesh_file
# TODO add add_gmsh_msh_file

def export_dagmc_h5m_file(
self,
material_tags: typing.Iterable[str],
Expand Down
26 changes: 14 additions & 12 deletions tests/test_file_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def test_h5m_with_single_volume_list():
h5m_file = "tests/single_cube.h5m"
mesh_file = "test.msh"
my_model = CadToDagmc()
my_model.add_stp_file(filename="tests/single_cube.stp")
my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=["mat1"])
my_model.add_stp_file(filename="tests/single_cube.stp", material_tags=["mat1"])
my_model.export_dagmc_h5m_file(filename=h5m_file)
my_model.export_gmsh_mesh_file(filename=mesh_file)
assert Path(mesh_file).is_file()
my_model.export_gmsh_mesh_file(filename="test3d.msh", dimensions=3)
Expand All @@ -71,8 +71,8 @@ def test_h5m_with_single_volume_2():
h5m_file = "tests/curved_extrude.h5m"

my_model = CadToDagmc()
my_model.add_stp_file(filename="tests/curved_extrude.stp")
my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=["mat1"])
my_model.add_stp_file(filename="tests/curved_extrude.stp", material_tags=["mat1"])
my_model.export_dagmc_h5m_file(filename=h5m_file)

assert get_volumes_and_materials_from_h5m(h5m_file) == {1: "mat:mat1"}

Expand All @@ -89,9 +89,9 @@ def test_h5m_with_multi_volume_not_touching():
]
for stp_file, mat_tags, h5m_file in zip(stp_files, material_tags, h5m_files):
my_model = CadToDagmc()
my_model.add_stp_file(filename=stp_file)
my_model.add_stp_file(filename=stp_file, material_tags=mat_tags)

my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=mat_tags)
my_model.export_dagmc_h5m_file(filename=h5m_file)

tags_dict = {}
for counter, loop_mat_tag in enumerate(mat_tags, 1):
Expand All @@ -114,9 +114,9 @@ def test_h5m_with_multi_volume_touching():
]
for stp_file, mat_tags, h5m_file in zip(stp_files, material_tags, h5m_files):
my_model = CadToDagmc()
my_model.add_stp_file(stp_file)
my_model.add_stp_file(stp_file, material_tags=mat_tags)

my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=mat_tags)
my_model.export_dagmc_h5m_file(filename=h5m_file)
my_model.export_gmsh_mesh_file(filename=h5m_file + ".msh")

tags_dict = {}
Expand Down Expand Up @@ -152,12 +152,14 @@ def test_cq_compound():
compound_of_workplanes = cq.Compound.makeCompound([cq_shape_1.val(), cq_shape_2.val()])

my_model = CadToDagmc()
my_model.add_cadquery_object(object=compound_of_workplanes)
my_model.add_cadquery_object(
cadquery_object=compound_of_workplanes,
material_tags=["mat1", "mat2"],
)
my_model.export_dagmc_h5m_file(
filename="compound_dagmc.h5m",
max_mesh_size=0.2,
min_mesh_size=0.1,
material_tags=["mat1", "mat2"],
)

assert Path("compound_dagmc.h5m").is_file()
Expand Down Expand Up @@ -204,7 +206,7 @@ def test_gmsh_mesh_with_multi_volume_not_touching():
]
for stp_file, mat_tags, gmsh_mesh_file in zip(stp_files, material_tags, gmsh_mesh_files):
my_model = CadToDagmc()
my_model.add_stp_file(filename=stp_file)
my_model.add_stp_file(filename=stp_file, material_tags=mat_tags)

my_model.export_gmsh_mesh_file(filename=gmsh_mesh_file)

Expand All @@ -224,7 +226,7 @@ def test_gmsh_mesh_with_multi_volume_touching():
]
for stp_file, mat_tags, gmsh_mesh_file in zip(stp_files, material_tags, gmsh_mesh_files):
my_model = CadToDagmc()
my_model.add_stp_file(stp_file)
my_model.add_stp_file(stp_file, material_tags=mat_tags)

my_model.export_gmsh_mesh_file(filename=gmsh_mesh_file)

Expand Down
31 changes: 17 additions & 14 deletions tests/test_h5m_in_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ def test_transport_result_h5m_with_2_sep_volumes():
# cq.Assembly().add(workplane1).add(workplane2)

my_model = CadToDagmc()
my_model.add_cadquery_object(workplane1)
my_model.add_cadquery_object(workplane2)
my_model.add_cadquery_object(workplane1, material_tags=[material_tags[0]])
my_model.add_cadquery_object(workplane2, material_tags=[material_tags[1]])
my_model.export_dagmc_h5m_file(
filename=h5m_filename, material_tags=[material_tags[0], material_tags[1]]
filename=h5m_filename
)

transport_particles_on_h5m_geometry(
Expand All @@ -155,8 +155,8 @@ def test_transport_result_h5m_with_1_curved_volumes():
workplane1 = cq.Workplane("XY").cylinder(height=10, radius=4)

my_model = CadToDagmc()
my_model.add_cadquery_object(workplane1)
my_model.export_dagmc_h5m_file(filename=h5m_filename, material_tags=[material_tags[0]])
my_model.add_cadquery_object(workplane1, material_tags=[material_tags[0]])
my_model.export_dagmc_h5m_file(filename=h5m_filename)

transport_particles_on_h5m_geometry(
h5m_filename=h5m_filename,
Expand All @@ -175,10 +175,10 @@ def test_transport_result_h5m_with_2_joined_curved_volumes():
workplane2 = cq.Workplane("XY").cylinder(height=10, radius=5).cut(workplane1)

my_model = CadToDagmc()
my_model.add_cadquery_object(workplane1)
my_model.add_cadquery_object(workplane2)
my_model.add_cadquery_object(workplane1,material_tags=[material_tags[0]])
my_model.add_cadquery_object(workplane2, material_tags=[material_tags[1]])
my_model.export_dagmc_h5m_file(
filename=h5m_filename, material_tags=[material_tags[0], material_tags[1]]
filename=h5m_filename,
)

transport_particles_on_h5m_geometry(
Expand All @@ -195,8 +195,8 @@ def test_h5m_with_single_volume_list():
h5m_file = "tests/single_cube.h5m"

my_model = CadToDagmc()
my_model.add_stp_file(filename="tests/single_cube.stp")
my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=["mat1"])
my_model.add_stp_file(filename="tests/single_cube.stp", material_tags=["mat1"])
my_model.export_dagmc_h5m_file(filename=h5m_file)

h5m_files = [
"tests/single_cube.h5m",
Expand All @@ -216,8 +216,8 @@ def test_h5m_with_multi_volume_not_touching():
h5m_file = "tests/two_disconnected_cubes.h5m"

my_model = CadToDagmc()
my_model.add_stp_file(filename="tests/two_disconnected_cubes.stp")
my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=["mat1", "mat2"])
my_model.add_stp_file(filename="tests/two_disconnected_cubes.stp", material_tags=["mat1", "mat2"])
my_model.export_dagmc_h5m_file(filename=h5m_file)

transport_particles_on_h5m_geometry(
h5m_filename="tests/two_disconnected_cubes.h5m",
Expand All @@ -243,9 +243,12 @@ def test_h5m_with_multi_volume_touching():
for stp_file, mat_tags, h5m_file in zip(stp_files, material_tags, h5m_files):

my_model = CadToDagmc()
my_model.add_stp_file(stp_file)
my_model.add_stp_file(
filename=stp_file,
material_tags=mat_tags,
)

my_model.export_dagmc_h5m_file(filename=h5m_file, material_tags=mat_tags)
my_model.export_dagmc_h5m_file(filename=h5m_file)

transport_particles_on_h5m_geometry(
h5m_filename=h5m_file,
Expand Down
15 changes: 6 additions & 9 deletions tests/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,25 @@ def test_max_mesh_size_impacts_file_size():
sphere = cq.Workplane().sphere(100)

c2d = CadToDagmc()
c2d.add_cadquery_object(sphere)
c2d.add_cadquery_object(sphere, material_tags=["m1"])
os.system("rm *.h5m")
c2d.export_dagmc_h5m_file(
min_mesh_size=10,
max_mesh_size=20,
mesh_algorithm=1,
filename="test_10_30.h5m",
material_tags=["m1"],
filename="test_10_30.h5m",
)
c2d.export_dagmc_h5m_file(
min_mesh_size=20,
max_mesh_size=30,
mesh_algorithm=1,
filename="test_20_30.h5m",
material_tags=["m1"],
)
c2d.export_dagmc_h5m_file(
min_mesh_size=20,
max_mesh_size=25,
mesh_algorithm=1,
filename="test_20_25.h5m",
material_tags=["m1"],
)

assert Path("test_10_30.h5m").is_file()
Expand All @@ -91,15 +88,15 @@ def test_h5m_file_tags():
sphere3 = cq.Workplane().moveTo(-100, -100).sphere(20)

c2d = CadToDagmc()
c2d.add_cadquery_object(sphere1)
c2d.add_cadquery_object(sphere2)
c2d.add_cadquery_object(sphere3)
c2d.add_cadquery_object(sphere1, material_tags=["mat1"])
c2d.add_cadquery_object(sphere2, material_tags=["mat2"])
c2d.add_cadquery_object(sphere3, material_tags=["mat3"])

test_h5m_filename = "test_dagmc.h5m"
os.system(f"rm {test_h5m_filename}")

returned_filename = c2d.export_dagmc_h5m_file(
filename=test_h5m_filename, material_tags=["mat1", "mat2", "mat3"]
filename=test_h5m_filename
)

assert Path(test_h5m_filename).is_file()
Expand Down

0 comments on commit ccb94ce

Please sign in to comment.