Skip to content

Commit

Permalink
Merge pull request #1 from fusion-energy/numpy_refactor
Browse files Browse the repository at this point in the history
Numpy refactor
  • Loading branch information
shimwell authored Jul 7, 2022
2 parents eef3145 + 2cd3107 commit 0f51886
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 14 deletions.
16 changes: 16 additions & 0 deletions examples/one_volume_cadquery_tesselation.py
Original file line number Diff line number Diff line change
@@ -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")
148 changes: 134 additions & 14 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -98,18 +100,52 @@ 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"

# 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",
)
Expand All @@ -135,20 +171,20 @@ 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"

# 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",
)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",
}

0 comments on commit 0f51886

Please sign in to comment.