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

progress on h5py method #12

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
afe62c3
progress on h5py method
shimwell Oct 21, 2022
fbf45a6
[skip ci] Apply formatting changes
shimwell Oct 21, 2022
5709652
added doc strings
shimwell Oct 21, 2022
2a924ef
merge conflict resolve
shimwell Oct 21, 2022
7bc8ff3
merge conflict resolve
shimwell Oct 21, 2022
9b2c6ce
renamed triangles to triangle_groups
shimwell Oct 21, 2022
319f6ac
[skip ci] Apply formatting changes
shimwell Oct 21, 2022
c432a83
added test scripts to examples
shimwell Oct 21, 2022
c36a1a4
[skip ci] Apply formatting changes
shimwell Oct 21, 2022
9faa4b3
[skip ci] updating arg name
shimwell Oct 21, 2022
7828b06
Merge branch 'writing_files_h5py' of github.com:fusion-energy/vertice…
shimwell Oct 21, 2022
9fd8aa2
added flux values from simulation
shimwell Oct 21, 2022
2554448
pymoab as default method
shimwell Oct 21, 2022
50ac9d2
[skip ci] Apply formatting changes
shimwell Oct 21, 2022
b18c379
checking both methods pymoab and h5py
shimwell Oct 21, 2022
d7ee236
Merge branch 'writing_files_h5py' of github.com:fusion-energy/vertice…
shimwell Oct 21, 2022
2a03c27
[skip ci] Apply formatting changes
shimwell Oct 21, 2022
8a41698
reordered tests
shimwell Oct 25, 2022
945bdf0
[skip ci] Apply formatting changes
shimwell Oct 25, 2022
0b549a6
working progress
shimwell Oct 25, 2022
043088d
working progress
shimwell Oct 25, 2022
ebe9058
added simple test file for example work
shimwell Oct 25, 2022
5194f4d
added more entries to category tag
shimwell Oct 25, 2022
50fb00b
added ref to file format
shimwell Oct 25, 2022
48dac9a
[skip ci] Apply formatting changes
shimwell Oct 25, 2022
8c439cb
[skip ci] renamed groups based on structure
shimwell Oct 25, 2022
aea6497
changed flux value as mesh changed
shimwell Oct 25, 2022
4a9675e
[skip ci] Apply formatting changes
shimwell Oct 25, 2022
a9f9c80
added hex encode function
shimwell Oct 25, 2022
2f41134
[skip ci] Apply formatting changes
shimwell Oct 25, 2022
920ee7f
better h5py h5m
nschloe Nov 9, 2022
37fd026
Merge pull request #13 from nschloe/nschloe-fixes
shimwell Nov 9, 2022
16ee1cc
named groups based on location in tree
shimwell Nov 9, 2022
74bdedb
more work on h5py
nschloe Nov 10, 2022
dd60087
fix order on nodes
nschloe Nov 10, 2022
6e22ffe
Merge pull request #14 from nschloe/more-h5py-fixes
shimwell Nov 10, 2022
66cfa69
[skip ci] Apply formatting changes
shimwell Nov 10, 2022
64a6f81
Comment out version to progress ci
shimwell Nov 10, 2022
aa48640
hard coded version number for now
shimwell Nov 10, 2022
150ef54
Version number as string
shimwell Nov 11, 2022
ed98ee4
[skip ci] Apply formatting changes
shimwell Nov 11, 2022
54f0024
more work in h5py converter
nschloe Nov 28, 2022
ef04400
Merge branch 'writing_files_h5py' into h5m-fixes-3
nschloe Nov 28, 2022
f5495b9
Merge pull request #15 from nschloe/h5m-fixes-3
shimwell Nov 28, 2022
4846831
[skip ci] Apply formatting changes
shimwell Nov 28, 2022
537b3d2
history: use ascii encoding
nschloe Apr 5, 2023
bdefb79
some research for the subarray data type
nschloe Apr 5, 2023
a379893
more clarity for surface/volume/group relations
nschloe Apr 5, 2023
8411926
Merge pull request #17 from nschloe/hdf5-fixes-4
shimwell Apr 5, 2023
296d73c
[skip ci] Apply formatting changes
shimwell Apr 5, 2023
1ca4840
added pymoab core import
shimwell Apr 6, 2023
7bbc7a2
uppdated odd api
shimwell Apr 6, 2023
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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ material tag mat1.
from vertices_to_h5m import vertices_to_h5m
import numpy as np

# these are the x,y,z coordinates of each vertex. Entries should be floats
# these are the x,y,z coordinates of each vertex. Entries should be floats
vertices = np.array(
[
[0.0, 0.0, 0.0],
Expand All @@ -69,15 +69,15 @@ vertices = np.array(


# These are the triangle that connect individual vertices together to form a continious surface and also a closed volume. Entries should be ints
triangles = [
triangle_groups = [
np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]),
]


# This will produce a h5m file called one_volume.h5m ready for use with DAGMC enabled codes.
vertices_to_h5m(
vertices=vertices,
triangles=triangles,
triangle_groups=triangle_groups,
material_tags=["mat1"],
h5m_filename="one_volume.h5m",
)
Expand All @@ -99,19 +99,18 @@ vertices = np.array(
)

# These are the two sets triangle that connect individual vertices together to form a continious surfaces and also two closed volume.
triangles = [
triangle_groups = [
np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]),
np.array([[4, 5, 1], [4, 5, 2], [4, 1, 2], [5, 1, 2]]),
]

# This will produce a h5m file called two_volume_touching_edge.h5m ready for use with DAGMC enabled codes
vertices_to_h5m(
vertices=vertices,
triangles=triangles,
triangle_groups=triangle_groups,
material_tags=["mat1", "mat2"],
h5m_filename="two_volume_touching_edge.h5m",
)

```
![two_volumes](images/two_volumes.png)

Expand Down
70 changes: 70 additions & 0 deletions examples/example_single_volume_h5py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import h5py
import numpy as np

vertices = np.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]
)


triangles = np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]])


f = h5py.File("one_volume.h5m", "w")

tstt = f.create_group("tstt")

elements = tstt.create_group("elements")

global_id = (
1 # not sure if this is need, appears to count both triangles and coordinates
)
mesh_type = "Tri3"
mesh_name = 2

elem_dt = h5py.special_dtype(
enum=(
"i",
{
"Edge": 1,
"Tri": 2,
"Quad": 3,
"Polygon": 4,
"Tet": 5,
"Pyramid": 6,
"Prism": 7,
"Knife": 8,
"Hex": 9,
"Polyhedron": 10,
},
)
)

my_group = elements.create_group(mesh_type)
my_group.attrs.create("element_type", mesh_name, dtype=elem_dt)

# compression="gzip"
# compression_opts=4 #meshio defaults
conn = my_group.create_dataset(
"connectivity",
data=triangles + 1, # node indices are 1 based in h5m
# compression=compression,
# compression_opts=compression_opts,
)

conn.attrs.create("start_id", global_id)
global_id += len(triangles)

nodes = tstt.create_group("nodes")

coords = nodes.create_dataset("coordinates", data=vertices)
coords.attrs.create("start_id", global_id)
global_id += len(vertices)

sets = tstt.create_group("sets")

tags = tstt.create_group("tags")
223 changes: 223 additions & 0 deletions examples/example_two_volume_h5py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import h5py
import numpy as np
import trimesh

"""
Creates a DAGMC compatible h5m file based on the H%M file format layout can be
found here https://sigma.mcs.anl.gov/moab/h5m-file-format/
"""

print("started")

vertices = np.array(
[
[0.42, 0.42, 0.42],
[1.42, 0.42, 0.42],
[0.42, 1.42, 0.42],
[0.42, 0.42, 1.42],
[1.42, 0.42, 1.42],
[0.42, 1.42, 1.42],
]
)

triangle_groups = [
np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]),
np.array([[1, 2, 3], [1, 3, 4], [3, 5, 2], [1, 2, 4], [2, 4, 5], [3, 5, 4]]),
]

# commented out for now as this just reorders the nodes in a specific order so they point outwards
#
# def fix_normals(vertices, triangles_in_each_volume):
# fixed_triangles = []
# for triangles in triangles_in_each_volume:
# fixed_triangles.append(fix_normal(vertices, triangles))
# return fixed_triangles
#
# def fix_normal(vertices, triangles):

# # for triangles in triangles_in_each_volume:
# mesh = trimesh.Trimesh(vertices=vertices, faces=triangles, process=False)

# mesh.fix_normals()

# return mesh.faces
#
# triangles = fix_normals(
# vertices=vertices, triangles_in_each_volume=triangle_groups
# )

f = h5py.File("h5py_two_volumes.h5m", "w")

all_triangles = np.vstack(triangle_groups)


tstt_group = f.create_group("tstt")

group_elements = tstt_group.create_group("elements")

global_id = 1 # appears to count both triangles and coordinates

elem_dt = h5py.special_dtype(
enum=(
"i",
{
# "Edge": 1,
"Tri": 2,
# "Quad": 3,
# "Polygon": 4,
# "Tet": 5,
# "Pyramid": 6,
# "Prism": 7,
# "Knife": 8,
# "Hex": 9,
# "Polyhedron": 10,
},
)
)

# for triangles in triangle_groups:

mesh_name = 2
tri3_group = group_elements.create_group("Tri3")
tri3_group.attrs.create("element_type", mesh_name, dtype=elem_dt)

# compression="gzip"
# compression_opts=4 #meshio defaults
connectivity_group = tri3_group.create_dataset(
"connectivity",
data=all_triangles + 1, # node indices are 1 based in h5m
# compression=compression,
# compression_opts=compression_opts,
)

connectivity_group.attrs.create("start_id", global_id)
global_id += len(all_triangles)


tags_tri3_group = tri3_group.create_group("tags")
tags_tri3_group.create_dataset(
"GLOBAL_ID",
data=[-1] * len(vertices),
# compression=compression,
# compression_opts=compression_opts,
)


nodes_group = tstt_group.create_group("nodes")
tags_nodes_group = nodes_group.create_group("tags")
tags_nodes_group.create_dataset(
"materials",
data=[-1] * len(vertices),
# compression=compression,
# compression_opts=compression_opts,
)

coordinates_group = nodes_group.create_dataset("coordinates", data=vertices)
coordinates_group.attrs.create("start_id", global_id)
global_id += len(vertices)

sets_group = tstt_group.create_group("sets")
sets_group.create_dataset(
"children",
data=[
[
23,
26,
] # TODO not sure where these numbers come from, automate the production of these numbers
],
)
sets_group.create_dataset(
"contents",
data=[
[
1,
6,
13,
4,
24,
7,
6,
17,
6,
27,
1,
28,
] # TODO not sure where these numbers come from
],
)
sets_group.create_dataset(
"list",
data=[
# TODO make an entry like this, not sure what these numbers are based on
# {
# (0,0): 3, -1, 0, 10,
# (1,0): 3, 0, 0, 2,
# (2,0): 4, 0, 0, 2,
# (3,0): 8, 0, 1, 10,
# (4,0): 8, 1, 1, 2,
# (5,0): 9, 1, 1, 2,
# (6,0): 11, 1, 1, 10
# }
],
)
sets_group.create_dataset(
"parents", data=[[24, 27]] # TODO not sure where these numbers come from
)
sets_group.create_dataset(
"GLOBAL_ID",
data=[[1, 1, -1, 2, 2, -1, -1]], # TODO not sure where these numbers come from
)


def hex_encode(string_to_encode: str) -> str:
"""encodes a string in hex format and adds : between characters"""
hex_string = string_to_encode.encode("utf-8").hex()
deliminated_string = ":".join(
hex_string[i : i + 2] for i in range(0, len(hex_string), 2)
)
# TODO pad the string up to the correct number of characters with 00: entries
return deliminated_string


tags_tstt_group = tstt_group.create_group("tags")
cat = tags_tstt_group.create_group("CATEGORY")
cat.create_dataset(
"values",
# appears to repeat twice as we have two volumes
# these words appear in hex format with padding in the h5m file
# for example 53:75:72:66:61:63:65:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
data=[
hex_encode("Surface"),
"Volume",
"Group",
"Surface",
"Volume",
"Group",
],
)

cat = tags_tstt_group.create_group("id_list")
# TODO populate group
cat = tags_tstt_group.create_group("values")
# TODO populate group

# tried these two methods of making a list in the h5m dataset
# metho 1
# arr = np.array(['mat:mat1','mat:mat2'])
# arr=arr.astype(h5py.opaque_dtype(arr.dtype))
# method 2
# binary_blob = b"mat:mat1"
# dset.attrs["attribute_name"] = np.void(binary_blob)
# out = dset.attrs["attribute_name"]

name_group = tags_tstt_group.create_group("NAME")
name_group.create_dataset(
"values",
# TODO needs padding and nesting as pymoab does it
data=[np.void(b"mat:mat1"), np.void(b"mat:mat2")],
)


tstt_group.attrs.create("max_id", global_id)

print("finished")
30 changes: 30 additions & 0 deletions examples/example_two_volume_pymoab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

import numpy as np

from vertices_to_h5m import vertices_to_h5m

vertices = np.array(
[
[0.42, 0.42, 0.42],
[1.42, 0.42, 0.42],
[0.42, 1.42, 0.42],
[0.42, 0.42, 1.42],
[1.42, 0.42, 1.42],
[0.42, 1.42, 1.42],
]
)

triangle_groups = [
np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]),
np.array([[1, 2, 3], [1, 3, 4], [3, 5, 2], [1, 2, 4], [2, 4, 5], [3, 5, 4]]),
]

vertices_to_h5m(
vertices=vertices,
triangle_groups=triangle_groups,
material_tags=["mat1", "mat2"],
h5m_filename="pymoab_two_volumes.h5m",
)

os.system("mbconvert pymoab_two_volumes.h5m pymoab_two_volumes.vtk")
Loading