From 6c6ebed65d2086f0b72dfae891fb36673b6116c7 Mon Sep 17 00:00:00 2001
From: Jendrik Weise <jewe37@gmail.com>
Date: Sat, 9 Mar 2024 09:42:18 +0100
Subject: [PATCH] Fix: Loading of polyhedral VTU with different size polyhedra

---
 src/meshio/vtu/_vtu.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/meshio/vtu/_vtu.py b/src/meshio/vtu/_vtu.py
index be80904a..b883fe3f 100644
--- a/src/meshio/vtu/_vtu.py
+++ b/src/meshio/vtu/_vtu.py
@@ -53,6 +53,8 @@ def _polyhedron_cells_from_data(offsets, faces, faceoffsets, cell_data_raw):
     # cell. Switch faceoffsets to give start points, not end points
     faceoffsets = np.append([0], faceoffsets[:-1])
 
+    # order of sizes needs to be tracked to preserver cell-cell data correspondence
+    sizes = []
     # Double loop over cells then faces.
     # This will be slow, but seems necessary to cover all cases
     for cell_start in faceoffsets:
@@ -74,6 +76,8 @@ def _polyhedron_cells_from_data(offsets, faces, faceoffsets, cell_data_raw):
         # Done with this cell
         # Find number of nodes for this cell
         num_nodes_this_cell = np.unique(np.hstack([v for v in faces_this_cell])).size
+        if num_nodes_this_cell not in sizes:
+            sizes.append(num_nodes_this_cell)
 
         key = f"polyhedron{num_nodes_this_cell}"
         if key not in cells.keys():
@@ -90,7 +94,7 @@ def _polyhedron_cells_from_data(offsets, faces, faceoffsets, cell_data_raw):
 
     # Loop over all cell sizes, find all cells with this size, and store
     # cell data.
-    for sz in np.unique(size):
+    for sz in sizes:
         # Cells with this number of nodes.
         items = np.where(size == sz)[0]
 
