diff --git a/examples/one_volume_cadquery_tesselation.py b/examples/one_volume_cadquery_tesselation.py new file mode 100644 index 0000000..bbe631b --- /dev/null +++ b/examples/one_volume_cadquery_tesselation.py @@ -0,0 +1,16 @@ +import cadquery as cq + +result = cq.Workplane("front").box(2.0, 2.0, 0.5) + +vertices, triangles = result.val().tessellate(tolerance=0.1) + +vertices_to_h5m( + vertices=vertices, + triangles=triangles, + material_tags=["mat1"], + h5m_filename="one_cadquery_volume.h5m", +) + +import os + +os.system("mbconvert one_cadquery_volume.h5m one_cadquery_volume.vtk") diff --git a/tests/test_package.py b/tests/test_package.py index ab442bb..99f06cb 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -31,7 +31,9 @@ def transport_particles_on_h5m_geometry( materials.append(mat_dag_material_tag) - # materials.cross_sections='/home/jshim/brep_to_h5m/examples/h5m_from_coords/cross_sections.xml' + materials.cross_sections = ( + "/home/jshim/brep_to_h5m/examples/h5m_from_coords/cross_sections.xml" + ) # downloads the nuclear data and sets the openmc_cross_sections environmental variable odd.just_in_time_library_generator(libraries="ENDFB-7.1-NNDC", materials=materials) @@ -98,7 +100,41 @@ def transport_particles_on_h5m_geometry( my_model.run() -def test_h5m_production_with_single_volume(): +def test_h5m_production_with_single_volume_list(): + """The simplest geometry, a single 4 sided shape with lists instead of np arrays""" + + test_h5m_filename = "single_tet.h5m" + + # a list of xyz coordinates + vertices = [ + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0], + ] + + # the index of the coordinate that make up the corner of a tet, normals need fixing + triangles = [[[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]] + + vertices_to_h5m( + vertices=vertices, + triangles=triangles, + material_tags=["mat1"], + h5m_filename=test_h5m_filename, + ) + + transport_particles_on_h5m_geometry( + h5m_filename=test_h5m_filename, + material_tags=["mat1"], + ) + + assert Path(test_h5m_filename).is_file() + assert di.get_volumes_from_h5m(test_h5m_filename) == [1] + assert di.get_materials_from_h5m(test_h5m_filename) == ["mat1"] + assert di.get_volumes_and_materials_from_h5m(test_h5m_filename) == {1: "mat1"} + + +def test_h5m_production_with_single_volume_numpy(): """The simplest geometry, a single 4 sided shape""" test_h5m_filename = "single_tet.h5m" @@ -106,10 +142,10 @@ def test_h5m_production_with_single_volume(): # a list of xyz coordinates vertices = np.array( [ - [0, 0, 0], - [1, 0, 0], - [0, 1, 0], - [0, 0, 1], + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0], ], dtype="float64", ) @@ -135,7 +171,7 @@ def test_h5m_production_with_single_volume(): assert di.get_volumes_and_materials_from_h5m(test_h5m_filename) == {1: "mat1"} -def test_h5m_production_with_two_touching_volumes(): +def test_h5m_production_with_two_touching_volumes_numpy(): """Two 4 sided shapes that share and edge""" test_h5m_filename = "double_tet.h5m" @@ -143,12 +179,12 @@ def test_h5m_production_with_two_touching_volumes(): # a list of xyz coordinates vertices = np.array( [ - [0, 0, 0], - [1, 0, 0], - [0, 1, 0], - [0, 0, 1], - [1, 1, 1], - [1, 1, 0], + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0], + [1.0, 1.0, 1.0], + [1.0, 1.0, 0.0], ], dtype="float64", ) @@ -180,7 +216,49 @@ def test_h5m_production_with_two_touching_volumes(): } -def test_h5m_production_with_two_touching_vertex(): +def test_h5m_production_with_two_touching_volumes_lists(): + """Two 4 sided shapes that share and edge""" + + test_h5m_filename = "double_tet.h5m" + + # a list of xyz coordinates + vertices = [ + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0], + [1.0, 1.0, 1.0], + [1.0, 1.0, 0.0], + ] + + # the index of the coordinate that make up the corner of a tet, normals need fixing + triangles = [ + [[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]], + [[4, 5, 1], [4, 5, 2], [4, 1, 2], [5, 1, 2]], + ] + + vertices_to_h5m( + vertices=vertices, + triangles=triangles, + material_tags=["mat1", "mat2"], + h5m_filename=test_h5m_filename, + ) + + transport_particles_on_h5m_geometry( + h5m_filename=test_h5m_filename, + material_tags=["mat1", "mat2"], + ) + + assert Path(test_h5m_filename).is_file() + assert di.get_volumes_from_h5m(test_h5m_filename) == [1, 2] + assert di.get_materials_from_h5m(test_h5m_filename) == ["mat1", "mat2"] + assert di.get_volumes_and_materials_from_h5m(test_h5m_filename) == { + 1: "mat1", + 2: "mat2", + } + + +def test_h5m_production_with_two_touching_vertex_numpy(): """Two 4 sided shapes that share an single vertex""" test_h5m_filename = "touching_vertex_tets.h5m" @@ -223,3 +301,45 @@ def test_h5m_production_with_two_touching_vertex(): 1: "mat1", 2: "mat2", } + + +def test_h5m_production_with_two_touching_vertex_list(): + """Two 4 sided shapes that share an single vertex""" + + test_h5m_filename = "touching_vertex_tets.h5m" + + vertices = [ + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0], + [-1.0, 0.0, 0.0], + [0.0, -1.0, 0.0], + [0.0, 0.0, -1.0], + ] + + # the index of the coordinate that make up the corner of a tet, normals need fixing + triangles = [ + [[0, 4, 5], [6, 4, 5], [0, 5, 6], [0, 4, 6]], + [[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]], + ] + + vertices_to_h5m( + vertices=vertices, + triangles=triangles, + material_tags=["mat1", "mat2"], + h5m_filename=test_h5m_filename, + ) + + transport_particles_on_h5m_geometry( + h5m_filename=test_h5m_filename, + material_tags=["mat1", "mat2"], + ) + + assert Path(test_h5m_filename).is_file() + assert di.get_volumes_from_h5m(test_h5m_filename) == [1, 2] + assert di.get_materials_from_h5m(test_h5m_filename) == ["mat1", "mat2"] + assert di.get_volumes_and_materials_from_h5m(test_h5m_filename) == { + 1: "mat1", + 2: "mat2", + }