From 9c26b5ce9fe0728b6b43dbbefc980f4d3ae186e8 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Mon, 23 Nov 2020 19:58:31 +0000
Subject: [PATCH] BUG: subsetMesh: map instead of truncate. See #1558.

This also is to do with redistributePar:
this uses subsetMesh to generate parts to
send to different processors.
2) related to 1558: make sure not to choose 'mapped'
patches to move the processor patches into so
we can use the mapper cloning and correctly
size additional data (e.g. offsets). This should
be generalised to hold for any patch type
holding local data ...
---
 .../fvMeshDistribute/fvMeshDistribute.C       |  8 +-
 src/dynamicMesh/fvMeshSubset/fvMeshSubset.C   | 80 +++++++++++++++----
 2 files changed, 70 insertions(+), 18 deletions(-)

diff --git a/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C b/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C
index 1fe48b0fc13..d69d656f9a2 100644
--- a/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C
+++ b/src/dynamicMesh/fvMeshDistribute/fvMeshDistribute.C
@@ -47,6 +47,7 @@ License
 #include "ListOps.H"
 #include "globalIndex.H"
 #include "cyclicACMIPolyPatch.H"
+#include "mappedPatchBase.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -335,7 +336,12 @@ Foam::label Foam::fvMeshDistribute::findNonEmptyPatch() const
     {
         const polyPatch& pp = patches[patchi];
 
-        if (!isA<emptyPolyPatch>(pp) && !isCoupledPatch(patchi))
+        if
+        (
+           !isA<emptyPolyPatch>(pp)
+        && !isCoupledPatch(patchi)
+        && !isA<mappedPatchBase>(pp)
+        )
         {
             nonEmptyPatchi = patchi;
             break;
diff --git a/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C b/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C
index aa62bf48f3c..98a5fac0608 100644
--- a/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C
+++ b/src/dynamicMesh/fvMeshSubset/fvMeshSubset.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2015-2018 OpenCFD Ltd.
+    Copyright (C) 2015-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -1055,14 +1055,37 @@ void Foam::fvMeshSubset::setCellSubset
     {
         const label newSize = boundaryPatchSizes[globalPatchMap[oldPatchi]];
 
-        // Clone (even if 0 size)
-        newBoundary[nNewPatches] = oldPatches[oldPatchi].clone
-        (
-            fvMeshSubsetPtr_().boundaryMesh(),
-            nNewPatches,
-            newSize,
-            patchStart
-        ).ptr();
+        if (oldInternalPatchID != oldPatchi)
+        {
+            // Pure subset of patch faces (no internal faces added to this
+            // patch). Can use mapping.
+            labelList map(newSize);
+            for (label patchFacei = 0; patchFacei < newSize; patchFacei++)
+            {
+                const label facei = patchStart+patchFacei;
+                const label oldFacei = faceMap_[facei];
+                map[patchFacei] = oldPatches[oldPatchi].whichFace(oldFacei);
+            }
+
+            newBoundary[nNewPatches] = oldPatches[oldPatchi].clone
+            (
+                fvMeshSubsetPtr_().boundaryMesh(),
+                nNewPatches,
+                map,
+                patchStart
+            ).ptr();
+        }
+        else
+        {
+            // Clone (even if 0 size)
+            newBoundary[nNewPatches] = oldPatches[oldPatchi].clone
+            (
+                fvMeshSubsetPtr_().boundaryMesh(),
+                nNewPatches,
+                newSize,
+                patchStart
+            ).ptr();
+        }
 
         patchStart += newSize;
         patchMap_[nNewPatches] = oldPatchi;    // compact patchMap
@@ -1115,14 +1138,37 @@ void Foam::fvMeshSubset::setCellSubset
     {
         const label newSize = boundaryPatchSizes[globalPatchMap[oldPatchi]];
 
-        // Patch still exists. Add it
-        newBoundary[nNewPatches] = oldPatches[oldPatchi].clone
-        (
-            fvMeshSubsetPtr_().boundaryMesh(),
-            nNewPatches,
-            newSize,
-            patchStart
-        ).ptr();
+        if (oldInternalPatchID != oldPatchi)
+        {
+            // Pure subset of patch faces (no internal faces added to this
+            // patch). Can use mapping.
+            labelList map(newSize);
+            for (label patchFacei = 0; patchFacei < newSize; patchFacei++)
+            {
+                const label facei = patchStart+patchFacei;
+                const label oldFacei = faceMap_[facei];
+                map[patchFacei] = oldPatches[oldPatchi].whichFace(oldFacei);
+            }
+
+            newBoundary[nNewPatches] = oldPatches[oldPatchi].clone
+            (
+                fvMeshSubsetPtr_().boundaryMesh(),
+                nNewPatches,
+                map,
+                patchStart
+            ).ptr();
+        }
+        else
+        {
+            // Patch still exists. Add it
+            newBoundary[nNewPatches] = oldPatches[oldPatchi].clone
+            (
+                fvMeshSubsetPtr_().boundaryMesh(),
+                nNewPatches,
+                newSize,
+                patchStart
+            ).ptr();
+        }
 
         //Pout<< "    " << oldPatches[oldPatchi].name() << " : "
         //    << newSize << endl;
-- 
GitLab