From 2ff67f8ce407040ce32f8b80b8dd7b3af8c10e05 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Sat, 30 Mar 2024 14:54:42 +0000
Subject: [PATCH] BUG: overlapping zones: incorrect indexing

---
 src/meshTools/polyTopoChange/polyTopoChange.C | 41 +++++++++++++++----
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/src/meshTools/polyTopoChange/polyTopoChange.C b/src/meshTools/polyTopoChange/polyTopoChange.C
index 822ff1efe4a..3c3e20284d3 100644
--- a/src/meshTools/polyTopoChange/polyTopoChange.C
+++ b/src/meshTools/polyTopoChange/polyTopoChange.C
@@ -202,11 +202,33 @@ void Foam::polyTopoChange::writeMeshStats(const polyMesh& mesh, Ostream& os)
         patchStarts[patchi] = patches[patchi].start();
     }
 
+    const auto& czs = mesh.cellZones();
+    labelList cellZoneSizes(czs.size(), 0);
+    for (const auto& cz : czs)
+    {
+        cellZoneSizes[cz.index()] = cz.size();
+    }
+    const auto& fzs = mesh.faceZones();
+    labelList faceZoneSizes(fzs.size(), 0);
+    for (const auto& fz : fzs)
+    {
+        faceZoneSizes[fz.index()] = fz.size();
+    }
+    const auto& pzs = mesh.pointZones();
+    labelList pointZoneSizes(pzs.size(), 0);
+    for (const auto& pz : pzs)
+    {
+        pointZoneSizes[pz.index()] = pz.size();
+    }
+
     os  << "    Points      : " << mesh.nPoints() << nl
         << "    Faces       : " << mesh.nFaces() << nl
         << "    Cells       : " << mesh.nCells() << nl
-        << "    PatchSizes  : " << patchSizes << nl
-        << "    PatchStarts : " << patchStarts << nl
+        << "    PatchSizes  : " << flatOutput(patchSizes) << nl
+        << "    PatchStarts : " << flatOutput(patchStarts) << nl
+        << "    cZoneSizes  : " << flatOutput(cellZoneSizes) << nl
+        << "    fZoneSizes  : " << flatOutput(faceZoneSizes) << nl
+        << "    pZoneSizes  : " << flatOutput(pointZoneSizes) << nl
         << endl;
 }
 
@@ -907,6 +929,8 @@ void Foam::polyTopoChange::reorderCompactFaces
     faceZoneFlip_.setCapacity(newSize);
     if (faceAdditionalZones_.size())
     {
+        // Extend to number of faces so oldToNew can be used
+        faceAdditionalZones_.setSize(faceZone_.size());
         inplaceReorder(oldToNew, faceAdditionalZones_);
         faceAdditionalZones_.setCapacity(newSize);
     }
@@ -3913,13 +3937,6 @@ Foam::autoPtr<Foam::mapPolyMesh> Foam::polyTopoChange::changeMesh
             << endl;
     }
 
-    if (debug)
-    {
-        Pout<< "New mesh:" << nl;
-        writeMeshStats(mesh, Pout);
-    }
-
-
     // Zones
     // ~~~~~
 
@@ -3932,6 +3949,12 @@ Foam::autoPtr<Foam::mapPolyMesh> Foam::polyTopoChange::changeMesh
 
     resetZones(mesh, mesh, pointZoneMap, faceZoneFaceMap, cellZoneMap);
 
+    if (debug)
+    {
+        Pout<< "New mesh:" << nl;
+        writeMeshStats(mesh, Pout);
+    }
+
     // Clear zone info
     {
         pointZone_.clearStorage();
-- 
GitLab