From 184d408a7c664c032a6908eaa01fb0302b709cfa Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 29 Jun 2022 11:48:04 +0100 Subject: [PATCH 1/4] added list based tests --- tests/test_package.py | 147 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 133 insertions(+), 14 deletions(-) diff --git a/tests/test_package.py b/tests/test_package.py index ab442bb..fcb3602 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -31,7 +31,7 @@ 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 +98,42 @@ 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.], + [1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.], + ] + + # 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 +141,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.], + [1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.], ], dtype="float64", ) @@ -135,7 +170,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 +178,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.], + [1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.], + [1., 1., 1.], + [1., 1., 0.], ], dtype="float64", ) @@ -180,7 +215,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.], + [1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.], + [1., 1., 1.], + [1., 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]], + [[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 +300,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.], + [1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.], + [-1., 0., 0.], + [0., -1., 0.], + [0., 0., -1.], + ] + + # 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", + } From 9a303a32cd2710609cfcdbbeb01211a26551e73c Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 29 Jun 2022 12:09:18 +0100 Subject: [PATCH 2/4] added cq example --- examples/one_volume_cadquery_tesselation.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 examples/one_volume_cadquery_tesselation.py diff --git a/examples/one_volume_cadquery_tesselation.py b/examples/one_volume_cadquery_tesselation.py new file mode 100644 index 0000000..f6bc475 --- /dev/null +++ b/examples/one_volume_cadquery_tesselation.py @@ -0,0 +1,17 @@ + +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") \ No newline at end of file From d85b55836309cd4d52a6424dfb85ae93ba0ab92f Mon Sep 17 00:00:00 2001 From: shimwell Date: Wed, 29 Jun 2022 11:24:30 +0000 Subject: [PATCH 3/4] [skip ci] Apply formatting changes --- tests/test_package.py | 73 ++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/tests/test_package.py b/tests/test_package.py index fcb3602..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) @@ -105,16 +107,14 @@ def test_h5m_production_with_single_volume_list(): # a list of xyz coordinates vertices = [ - [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], + ] # 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]] - ] + triangles = [[[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]] vertices_to_h5m( vertices=vertices, @@ -133,6 +133,7 @@ def test_h5m_production_with_single_volume_list(): 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""" @@ -141,10 +142,10 @@ def test_h5m_production_with_single_volume_numpy(): # 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", ) @@ -178,12 +179,12 @@ def test_h5m_production_with_two_touching_volumes_numpy(): # 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", ) @@ -215,7 +216,6 @@ def test_h5m_production_with_two_touching_volumes_numpy(): } - def test_h5m_production_with_two_touching_volumes_lists(): """Two 4 sided shapes that share and edge""" @@ -223,13 +223,13 @@ def test_h5m_production_with_two_touching_volumes_lists(): # a list of xyz coordinates vertices = [ - [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], + ] # the index of the coordinate that make up the corner of a tet, normals need fixing triangles = [ @@ -257,6 +257,7 @@ def test_h5m_production_with_two_touching_volumes_lists(): 2: "mat2", } + def test_h5m_production_with_two_touching_vertex_numpy(): """Two 4 sided shapes that share an single vertex""" @@ -308,19 +309,19 @@ def test_h5m_production_with_two_touching_vertex_list(): test_h5m_filename = "touching_vertex_tets.h5m" vertices = [ - [0., 0., 0.], - [1., 0., 0.], - [0., 1., 0.], - [0., 0., 1.], - [-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], + [-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]] + [[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]], ] vertices_to_h5m( From 2cd3107d6d8e9e284eb0d4e19ff5385f2a44e0ee Mon Sep 17 00:00:00 2001 From: shimwell Date: Wed, 29 Jun 2022 11:45:48 +0000 Subject: [PATCH 4/4] [skip ci] Apply formatting changes --- examples/one_volume_cadquery_tesselation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/one_volume_cadquery_tesselation.py b/examples/one_volume_cadquery_tesselation.py index f6bc475..bbe631b 100644 --- a/examples/one_volume_cadquery_tesselation.py +++ b/examples/one_volume_cadquery_tesselation.py @@ -1,4 +1,3 @@ - import cadquery as cq result = cq.Workplane("front").box(2.0, 2.0, 0.5) @@ -14,4 +13,4 @@ import os -os.system("mbconvert one_cadquery_volume.h5m one_cadquery_volume.vtk") \ No newline at end of file +os.system("mbconvert one_cadquery_volume.h5m one_cadquery_volume.vtk")