Skip to content

Commit 4d8d7a7

Browse files
committed
Some cleanup/small updates
1 parent 34066e2 commit 4d8d7a7

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

openmc/mesh.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,9 @@ def _write_data_to_vtk_ascii_format(
27522752
n_skipped += 1
27532753
continue
27542754
else:
2755-
raise RuntimeError(f"Invalid element type {elem_type} found")
2755+
raise RuntimeError(
2756+
f"Invalid element type {elem_type} found in mesh {self.id}"
2757+
)
27562758

27572759
for i, c in enumerate(conn):
27582760
if c == -1:
@@ -2809,38 +2811,41 @@ def _write_data_to_vtk_hdf5_format(
28092811
datasets: dict | None = None,
28102812
volume_normalization: bool = True,
28112813
):
2812-
def append_dataset(dset, array):
2813-
"""Convenience function to append data to an HDF5 dataset"""
2814-
origLen = dset.shape[0]
2815-
dset.resize(origLen + array.shape[0], axis=0)
2816-
dset[origLen:] = array
2817-
28182814
# This writer supports linear tetrahedra and linear hexahedra elements
28192815
conn_list = [] # flattened connectivity ids
28202816
cell_sizes = [] # number of points per cell
28212817
vtk_types = [] # VTK cell types per cell (uint8)
2818+
n_skipped = 0
28222819

28232820
for conn, etype in zip(self.connectivity, self.element_types):
28242821
if etype == self._LINEAR_TET:
28252822
ids = conn[:4]
2826-
vtk_types.append(self._VTK_TETRA)
2823+
vtk_types.append(self._VTK_TET)
28272824
elif etype == self._LINEAR_HEX:
28282825
ids = conn[:8]
2829-
vtk_types.append(self._VTK_HEXAHEDRON)
2826+
vtk_types.append(self._VTK_HEX)
2827+
elif etype == self._UNSUPPORTED_ELEM:
2828+
n_skipped += 1
2829+
continue
28302830
else:
28312831
raise RuntimeError(
28322832
f"Invalid element type {etype} found in mesh {self.id}"
28332833
)
28342834
conn_list.extend(ids.tolist())
28352835
cell_sizes.append(len(ids))
28362836

2837+
if n_skipped > 0:
2838+
warnings.warn(
2839+
f"{n_skipped} elements were not written because "
2840+
"they are not of type linear tet/hex"
2841+
)
2842+
28372843
connectivity = np.asarray(conn_list, dtype=np.int64)
28382844

28392845
# Offsets must be length (numCells + 1) with a leading 0 and
28402846
# cumulative end-indices thereafter, per VTK's layout
28412847
cell_sizes_arr = np.asarray(cell_sizes, dtype=np.int64)
2842-
offsets = np.empty(cell_sizes_arr.size + 1, dtype=np.int64)
2843-
offsets[0] = 0
2848+
offsets = np.zeros(cell_sizes_arr.size + 1, dtype=np.int64)
28442849
np.cumsum(cell_sizes_arr, out=offsets[1:])
28452850

28462851
for name, data in datasets.items():
@@ -2880,14 +2885,11 @@ def append_dataset(dset, array):
28802885
cell_data_group = root.create_group("CellData")
28812886

28822887
for name, data in datasets.items():
2883-
2884-
cell_data_group.create_dataset(
2885-
name, (0,), maxshape=(None,), dtype="float64", chunks=True
2886-
)
2887-
28882888
if volume_normalization:
28892889
data /= self.volumes
2890-
append_dataset(cell_data_group[name], data)
2890+
cell_data_group.create_dataset(
2891+
name, data=data, dtype="float64", chunks=True
2892+
)
28912893

28922894
@classmethod
28932895
def from_hdf5(cls, group: h5py.Group, mesh_id: int, name: str):

0 commit comments

Comments
 (0)