From 22b4c44fe866d55fd7a8bdd30fd37899dafbdacc Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 24 Mar 2021 11:49:38 +0000
Subject: [PATCH] BUG: layerAddition: duplicate faces. Fixes #1988. Fixes
 #2036.

When extruding an edge to a patch face make sure to extrude
all the patch faces connected to that edge and cell. This
will handle cyclicACMI (has duplicate patches - AMI and non-overlap)
---
 .../layerAdditionRemoval/addCellLayer.C       | 67 +++++++++++++++++--
 .../layerAdditionRemoval/removeCellLayer.C    |  8 ++-
 2 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/src/dynamicMesh/layerAdditionRemoval/addCellLayer.C b/src/dynamicMesh/layerAdditionRemoval/addCellLayer.C
index 4ca30ced377..2c6eb27b362 100644
--- a/src/dynamicMesh/layerAdditionRemoval/addCellLayer.C
+++ b/src/dynamicMesh/layerAdditionRemoval/addCellLayer.C
@@ -6,6 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
+    Copyright (C) 2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -423,19 +424,21 @@ void Foam::layerAdditionRemoval::addCellLayer
 
         label patchID = -1;
         label zoneID = -1;
+        label extrudeFaceID = -1;
 
-        forAll(curFaces, facei)
+        forAll(curFaces, curFacei)
         {
-            const label cf = curFaces[facei];
+            extrudeFaceID = curFaces[curFacei];
 
-            if (!mesh.isInternalFace(cf))
+            if (!mesh.isInternalFace(extrudeFaceID))
             {
-                // Face not internal. Check if it is in the zone
-                if (zoneMesh.whichZone(cf) != faceZoneID_.index())
+                // Face not internal. Check if it is not in the zone
+                const label cfZone = zoneMesh.whichZone(extrudeFaceID);
+                if (cfZone != faceZoneID_.index())
                 {
                     // Found the face in a boundary patch which is not in zone
-                    patchID = mesh.boundaryMesh().whichPatch(cf);
-                    zoneID = mesh.faceZones().whichZone(cf);
+                    patchID = mesh.boundaryMesh().whichPatch(extrudeFaceID);
+                    zoneID = cfZone;
 
                     break;
                 }
@@ -474,6 +477,56 @@ void Foam::layerAdditionRemoval::addCellLayer
                 << " own: " << addedCells[edgeFaces[curEdgeID][0]]
                 << endl;
         }
+
+        // Handle duplicate boundary faces (for e.g. cyclicACMI)
+        if (patchID != -1)
+        {
+            for (const label cf : curFaces)
+            {
+                if (cf != extrudeFaceID && !mesh.isInternalFace(cf))
+                {
+                    // Check if it is not in the zone and duplicate of
+                    // extrudeFaceID
+                    const label cfZone = zoneMesh.whichZone(cf);
+                    if
+                    (
+                        cfZone != faceZoneID_.index()
+                     && face::compare(faces[cf], faces[extrudeFaceID]) == 1
+                    )
+                    {
+                        // Found the face in a boundary patch which is not
+                        // in zone (so would not be extruded above)
+                        patchID = mesh.boundaryMesh().whichPatch(cf);
+                        zoneID = cfZone;
+
+                        ref.setAction
+                        (
+                            polyAddFace
+                            (
+                                newFace,                            // face
+                                addedCells[edgeFaces[curEdgeID][0]],// owner
+                                -1,                                 // neighbour
+                                -1,                                 // point
+                                meshEdges[curEdgeID],               // edge
+                                -1,                                 // face
+                                false,                              // flip flux
+                                patchID,                            // patch
+                                zoneID,                             // zone
+                                false                               // zone flip
+                            )
+                        );
+
+                        if (debug > 1)
+                        {
+                            Pout<< "add duplicate boundary face: " << newFace
+                                << " into patch " << patchID
+                                << " own: "
+                                << addedCells[edgeFaces[curEdgeID][0]] << endl;
+                        }
+                    }
+                }
+            }
+        }
     }
 
     // Modify the remaining faces of the master cells to reconnect to the new
diff --git a/src/dynamicMesh/layerAdditionRemoval/removeCellLayer.C b/src/dynamicMesh/layerAdditionRemoval/removeCellLayer.C
index d3f3f2edd88..d3c6c2931f9 100644
--- a/src/dynamicMesh/layerAdditionRemoval/removeCellLayer.C
+++ b/src/dynamicMesh/layerAdditionRemoval/removeCellLayer.C
@@ -396,8 +396,12 @@ void Foam::layerAdditionRemoval::removeCellLayer
                 << " flipFace: " << flipFace
                 << " newPatchID: " << newPatchID
                 << " newZoneID: " << newZoneID << nl
-                << " oldOwn: " << own[mf[facei]]
-                << " oldNei: " << nei[mf[facei]] << endl;
+                << " oldOwn: " << own[mf[facei]];
+            if (newPatchID == -1)
+            {
+                Pout<< " oldNei: " << nei[mf[facei]];
+            }
+            Pout<< endl;
         }
 
         ref.setAction
-- 
GitLab