From 0e5c9356a035028d89fea8fbba8070ff4f00cc16 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 15 May 2019 11:43:34 +0100
Subject: [PATCH] BUG: overset: out-of-range indexing. Fixes #1321

---
 .../dynamicOversetFvMesh.C                    | 85 +++++++++++--------
 1 file changed, 49 insertions(+), 36 deletions(-)

diff --git a/src/overset/dynamicOversetFvMesh/dynamicOversetFvMesh.C b/src/overset/dynamicOversetFvMesh/dynamicOversetFvMesh.C
index a8530dfba10..074bae9b161 100644
--- a/src/overset/dynamicOversetFvMesh/dynamicOversetFvMesh.C
+++ b/src/overset/dynamicOversetFvMesh/dynamicOversetFvMesh.C
@@ -106,13 +106,14 @@ bool Foam::dynamicOversetFvMesh::updateAddressing() const
     forAll(stencil, celli)
     {
         const labelList& nbrs = stencil[celli];
-        stencilPatches_[celli].setSize(nbrs.size(), -1);
+        stencilPatches_[celli].setSize(nbrs.size());
+        stencilPatches_[celli] = -1;
 
         forAll(nbrs, nbri)
         {
-            const label nbrCelli = nbrs[nbri];
             if (stencilFaces_[celli][nbri] == -1)
             {
+                const label nbrCelli = nbrs[nbri];
                 label globalNbr = globalCellIDs[nbrCelli];
                 label proci = globalNumbering.whichProcID(globalNbr);
                 label remoteCelli = globalNumbering.toLocal(proci, globalNbr);
@@ -164,10 +165,12 @@ bool Foam::dynamicOversetFvMesh::updateAddressing() const
     {
         if (proci < Pstream::myProcNo() && procOwner[proci].size())
         {
-            //Pout<< "Adding interface " << nbri
-            //    << " to receive my " << procOwner[proci]
-            //    << " from " << proci << endl;
-
+            if (debug)
+            {
+                Pout<< "Adding interface " << nbri
+                    << " to receive my " << procOwner[proci].size()
+                    << " from " << proci << endl;
+            }
             procToInterface[proci] = nbri;
             remoteStencilInterfaces_.set
             (
@@ -184,9 +187,12 @@ bool Foam::dynamicOversetFvMesh::updateAddressing() const
         }
         else if (proci > Pstream::myProcNo() && mySendCells[proci].size())
         {
-            //Pout<< "Adding interface " << nbri
-            //    << " to send my " << mySendCells[proci]
-            //    << " to " << proci << endl;
+            if (debug)
+            {
+                Pout<< "Adding interface " << nbri
+                    << " to send my " << mySendCells[proci].size()
+                    << " to " << proci << endl;
+            }
             remoteStencilInterfaces_.set
             (
                 nbri++,
@@ -205,9 +211,12 @@ bool Foam::dynamicOversetFvMesh::updateAddressing() const
     {
         if (proci > Pstream::myProcNo() && procOwner[proci].size())
         {
-            //Pout<< "Adding interface " << nbri
-            //    << " to receive my " << procOwner[proci]
-            //    << " from " << proci << endl;
+            if (debug)
+            {
+                Pout<< "Adding interface " << nbri
+                    << " to receive my " << procOwner[proci].size()
+                    << " from " << proci << endl;
+            }
             procToInterface[proci] = nbri;
             remoteStencilInterfaces_.set
             (
@@ -224,9 +233,12 @@ bool Foam::dynamicOversetFvMesh::updateAddressing() const
         }
         else if (proci < Pstream::myProcNo() && mySendCells[proci].size())
         {
-            //Pout<< "Adding interface " << nbri
-            //    << " to send my " << mySendCells[proci]
-            //    << " to " << proci << endl;
+            if (debug)
+            {
+                Pout<< "Adding interface " << nbri
+                    << " to send my " << mySendCells[proci].size()
+                    << " to " << proci << endl;
+            }
             remoteStencilInterfaces_.set
             (
                 nbri++,
@@ -634,28 +646,13 @@ bool Foam::dynamicOversetFvMesh::writeObject
         const labelIOList& zoneID = overlap.zoneID();
         const labelListList& cellStencil = overlap.cellStencil();
 
+        // Get remote zones
         labelList donorZoneID(zoneID);
         overlap.cellInterpolationMap().distribute(donorZoneID);
 
-        forAll(cellStencil, cellI)
-        {
-            const labelList& stencil = cellStencil[cellI];
-            if (stencil.size())
-            {
-                donorZoneID[cellI] = zoneID[stencil[0]];
-                for (label i = 1; i < stencil.size(); i++)
-                {
-                    if (zoneID[stencil[i]] != donorZoneID[cellI])
-                    {
-                        WarningInFunction << "Mixed donor meshes for cell "
-                            << cellI << " at " << C()[cellI]
-                            << " donors:" << UIndirectList<point>(C(), stencil)
-                            << endl;
-                        donorZoneID[cellI] = -2;
-                    }
-                }
-            }
-        }
+        // Get remote cellCentres
+        pointField cc(C());
+        overlap.cellInterpolationMap().distribute(cc);
 
         volScalarField volDonorZoneID
         (
@@ -672,9 +669,25 @@ bool Foam::dynamicOversetFvMesh::writeObject
             dimensionedScalar("minOne", dimless, scalar(-1)),
             zeroGradientFvPatchScalarField::typeName
         );
-        forAll(donorZoneID, celli)
+
+        forAll(cellStencil, cellI)
         {
-            volDonorZoneID[celli] = donorZoneID[celli];
+            const labelList& stencil = cellStencil[cellI];
+            if (stencil.size())
+            {
+                volDonorZoneID[cellI] = donorZoneID[stencil[0]];
+                for (label i = 1; i < stencil.size(); i++)
+                {
+                    if (donorZoneID[stencil[i]] != volDonorZoneID[cellI])
+                    {
+                        WarningInFunction << "Mixed donor meshes for cell "
+                            << cellI << " at " << C()[cellI]
+                            << " donors:" << UIndirectList<point>(cc, stencil)
+                            << endl;
+                        volDonorZoneID[cellI] = -2;
+                    }
+                }
+            }
         }
         //- Do not correctBoundaryConditions since re-interpolates!
         //volDonorZoneID.correctBoundaryConditions();
-- 
GitLab