From e93a3b43c76f9ddde6251d2d34206c405f41dffb Mon Sep 17 00:00:00 2001
From: Andrew Heather <a.heather@opencfd.co.uk>
Date: Tue, 26 Jun 2018 11:14:57 +0100
Subject: [PATCH] BUG: AMI/ACMI - corrected setting of [src|tgt]MagSf for
 parallel runs.  See #856 #906

---
 .../AMIInterpolation/AMIInterpolation.C       | 20 +++++++++----
 .../AMIMethod/AMIMethod/AMIMethod.H           | 17 ++++++++---
 .../AMIMethod/directAMI/directAMI.C           | 14 ++++++++++
 .../AMIMethod/directAMI/directAMI.H           |  9 ++++++
 .../faceAreaWeightAMI/faceAreaWeightAMI.C     | 15 ++++++++++
 .../faceAreaWeightAMI/faceAreaWeightAMI.H     | 11 +++++++-
 .../AMIMethod/mapNearestAMI/mapNearestAMI.C   | 14 ++++++++++
 .../AMIMethod/mapNearestAMI/mapNearestAMI.H   | 11 +++++++-
 .../partialFaceAreaWeightAMI.C                | 28 +++++++++++++++++++
 .../partialFaceAreaWeightAMI.H                |  9 ++++++
 10 files changed, 137 insertions(+), 11 deletions(-)

diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C
index 9aad249798..910efba4e8 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C
@@ -605,9 +605,11 @@ Foam::AMIInterpolation<SourcePatch, TargetPatch>::AMIInterpolation
     requireMatch_(requireMatch),
     singlePatchProc_(-999),
     lowWeightCorrection_(lowWeightCorrection),
+    srcMagSf_(),
     srcAddress_(),
     srcWeights_(),
     srcWeightsSum_(),
+    tgtMagSf_(),
     tgtAddress_(),
     tgtWeights_(),
     tgtWeightsSum_(),
@@ -636,9 +638,11 @@ Foam::AMIInterpolation<SourcePatch, TargetPatch>::AMIInterpolation
     requireMatch_(requireMatch),
     singlePatchProc_(-999),
     lowWeightCorrection_(lowWeightCorrection),
+    srcMagSf_(),
     srcAddress_(),
     srcWeights_(),
     srcWeightsSum_(),
+    tgtMagSf_(),
     tgtAddress_(),
     tgtWeights_(),
     tgtWeightsSum_(),
@@ -668,9 +672,11 @@ Foam::AMIInterpolation<SourcePatch, TargetPatch>::AMIInterpolation
     requireMatch_(requireMatch),
     singlePatchProc_(-999),
     lowWeightCorrection_(lowWeightCorrection),
+    srcMagSf_(),
     srcAddress_(),
     srcWeights_(),
     srcWeightsSum_(),
+    tgtMagSf_(),
     tgtAddress_(),
     tgtWeights_(),
     tgtWeightsSum_(),
@@ -700,9 +706,11 @@ Foam::AMIInterpolation<SourcePatch, TargetPatch>::AMIInterpolation
     requireMatch_(requireMatch),
     singlePatchProc_(-999),
     lowWeightCorrection_(lowWeightCorrection),
+    srcMagSf_(),
     srcAddress_(),
     srcWeights_(),
     srcWeightsSum_(),
+    tgtMagSf_(),
     tgtAddress_(),
     tgtWeights_(),
     tgtWeightsSum_(),
@@ -727,9 +735,11 @@ Foam::AMIInterpolation<SourcePatch, TargetPatch>::AMIInterpolation
     requireMatch_(fineAMI.requireMatch_),
     singlePatchProc_(fineAMI.singlePatchProc_),
     lowWeightCorrection_(-1.0),
+    srcMagSf_(),
     srcAddress_(),
     srcWeights_(),
     srcWeightsSum_(),
+    tgtMagSf_(),
     tgtAddress_(),
     tgtWeights_(),
     tgtWeightsSum_(),
@@ -884,7 +894,7 @@ void Foam::AMIInterpolation<SourcePatch, TargetPatch>::update
             tgtFaceIDs
         );
 
-        TargetPatch
+        const TargetPatch
             newTgtPatch
             (
                 SubList<face>
@@ -917,11 +927,11 @@ void Foam::AMIInterpolation<SourcePatch, TargetPatch>::update
             tgtWeights_
         );
 
+
         // Note: using patch face areas calculated by the AMI method
-        // - TODO: should move into the calculate method
-        srcMagSf_.transfer(AMIPtr->srcMagSf());
-        tgtMagSf_.transfer(AMIPtr->tgtMagSf());
-        map.reverseDistribute(tgtPatch.size(), tgtMagSf_);
+        // - TODO: move into the calculate or normalise method?
+        AMIPtr->setMagSf(tgtPatch, map, srcMagSf_, tgtMagSf_);
+
 
         // Now
         // ~~~
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/AMIMethod/AMIMethod.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/AMIMethod/AMIMethod.H
index cf3c8289d4..8410c7c68c 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/AMIMethod/AMIMethod.H
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/AMIMethod/AMIMethod.H
@@ -84,11 +84,11 @@ protected:
         const TargetPatch& tgtPatch_;
 
         //- Flag to indicate that the two patches are co-directional and
-        //  that the orientation of the target patch should be reversed
+        //- that the orientation of the target patch should be reversed
         const bool reverseTarget_;
 
         //- Flag to indicate that the two patches must be matched/an overlap
-        //  exists between them
+        //- exists between them
         const bool requireMatch_;
 
         //- Source face areas
@@ -98,7 +98,7 @@ protected:
         List<scalar> tgtMagSf_;
 
         //- Labels of faces that are not overlapped by any target faces
-        //  (should be empty for correct functioning)
+        //- (should be empty for correct functioning)
         labelList srcNonOverlap_;
 
         //- Octree used to find face seeds
@@ -254,8 +254,17 @@ public:
                 label tgtFacei = -1
             ) = 0;
 
+            //- Set the face areas for parallel runs
+            virtual void setMagSf
+            (
+                const TargetPatch& tgtPatch,
+                const mapDistribute& map,
+                scalarList& srcMagSf,
+                scalarList& tgtMagSf
+            ) const = 0;
+
             //- Normalise the weight. Can optionally subset addressing
-            //  (e.g. for mapNearest)
+            //- (e.g. for mapNearest)
             virtual void normaliseWeights
             (
                 const bool verbose,
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.C
index 25bf21baaa..0e503da082 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.C
@@ -313,6 +313,20 @@ void Foam::directAMI<SourcePatch, TargetPatch>::calculate
 }
 
 
+template<class SourcePatch, class TargetPatch>
+void Foam::directAMI<SourcePatch, TargetPatch>::setMagSf
+(
+    const TargetPatch& tgtPatch,
+    const mapDistribute& map,
+    scalarList& srcMagSf,
+    scalarList& tgtMagSf
+) const
+{
+    srcMagSf = std::move(this->srcMagSf_);
+    tgtMagSf = scalarList(tgtPatch.size(), 1.0);
+}
+
+
 template<class SourcePatch, class TargetPatch>
 void Foam::directAMI<SourcePatch, TargetPatch>::normaliseWeights
 (
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.H
index 19544a8fbf..dfdc8edff7 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.H
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/directAMI/directAMI.H
@@ -134,6 +134,15 @@ public:
                 label tgtFacei = -1
             );
 
+            //- Set the face areas for parallel runs
+            virtual void setMagSf
+            (
+                const TargetPatch& tgtPatch,
+                const mapDistribute& map,
+                scalarList& srcMagSf,
+                scalarList& tgtMagSf
+            ) const;
+
             //- Normalise the weight. Can optionally subset addressing
             //  (e.g. for mapNearest)
             virtual void normaliseWeights
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.C
index 1cf13f44be..b382379b5a 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.C
@@ -648,6 +648,21 @@ void Foam::faceAreaWeightAMI<SourcePatch, TargetPatch>::calculate
 }
 
 
+template<class SourcePatch, class TargetPatch>
+void Foam::faceAreaWeightAMI<SourcePatch, TargetPatch>::setMagSf
+(
+    const TargetPatch& tgtPatch,
+    const mapDistribute& map,
+    scalarList& srcMagSf,
+    scalarList& tgtMagSf
+) const
+{
+    srcMagSf = std::move(this->srcMagSf_);
+    tgtMagSf = std::move(this->tgtMagSf_);
+    map.reverseDistribute(tgtPatch.size(), tgtMagSf);
+}
+
+
 template<class SourcePatch, class TargetPatch>
 void Foam::faceAreaWeightAMI<SourcePatch, TargetPatch>::normaliseWeights
 (
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.H
index d27f518e9d..423b6ec807 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.H
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/faceAreaWeightAMI/faceAreaWeightAMI.H
@@ -180,8 +180,17 @@ public:
                 label tgtFacei = -1
             );
 
+            //- Set the face areas for parallel runs
+            virtual void setMagSf
+            (
+                const TargetPatch& tgtPatch,
+                const mapDistribute& map,
+                scalarList& srcMagSf,
+                scalarList& tgtMagSf
+            ) const;
+
             //- Normalise the weight. Can optionally subset addressing
-            //  (e.g. for mapNearest)
+            //- (e.g. for mapNearest)
             virtual void normaliseWeights
             (
                 const bool verbose,
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C
index b2f16391b2..27e455bc56 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C
@@ -347,6 +347,20 @@ void Foam::mapNearestAMI<SourcePatch, TargetPatch>::calculate
 }
 
 
+template<class SourcePatch, class TargetPatch>
+void Foam::mapNearestAMI<SourcePatch, TargetPatch>::setMagSf
+(
+    const TargetPatch& tgtPatch,
+    const mapDistribute& map,
+    scalarList& srcMagSf,
+    scalarList& tgtMagSf
+) const
+{
+    srcMagSf = std::move(this->srcMagSf_);
+    tgtMagSf = scalarList(tgtPatch.size(), 1.0);
+}
+
+
 template<class SourcePatch, class TargetPatch>
 void Foam::mapNearestAMI<SourcePatch, TargetPatch>::normaliseWeights
 (
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.H
index 897ed9ae0c..e8e94ee559 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.H
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.H
@@ -138,8 +138,17 @@ public:
                 label tgtFacei = -1
             );
 
+            //- Set the face areas for parallel runs
+            virtual void setMagSf
+            (
+                const TargetPatch& tgtPatch,
+                const mapDistribute& map,
+                scalarList& srcMagSf,
+                scalarList& tgtMagSf
+            ) const;
+
             //- Normalise the weight. Can optionally subset addressing
-            //  (e.g. for mapNearest)
+            //- (e.g. for mapNearest)
             virtual void normaliseWeights
             (
                 const bool verbose,
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.C
index f25f7cb3c5..f16aa2a8a4 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.C
@@ -150,4 +150,32 @@ void Foam::partialFaceAreaWeightAMI<SourcePatch, TargetPatch>::calculate
 }
 
 
+template<class SourcePatch, class TargetPatch>
+void Foam::partialFaceAreaWeightAMI<SourcePatch, TargetPatch>::setMagSf
+(
+    const TargetPatch& tgtPatch,
+    const mapDistribute& map,
+    scalarList& srcMagSf,
+    scalarList& tgtMagSf
+) const
+{
+    srcMagSf = std::move(this->srcMagSf_);
+
+    scalarList newTgtMagSf(std::move(this->tgtMagSf_));
+    map.reverseDistribute(tgtPatch.size(), newTgtMagSf);
+
+    // Assign default sizes. Override selected values with
+    // calculated values. This is to support ACMI
+    // where some of the target faces are never used (so never get sent
+    // over and hence never assigned to)
+    tgtMagSf = tgtPatch.magFaceAreas();
+
+    for (const labelList& smap : map.subMap())
+    {
+        UIndirectList<scalar>(tgtMagSf, smap) =
+            UIndirectList<scalar>(newTgtMagSf, smap);
+    }
+}
+
+
 // ************************************************************************* //
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.H
index a79f0b6572..67aec398a0 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.H
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/partialFaceAreaWeightAMI/partialFaceAreaWeightAMI.H
@@ -120,6 +120,15 @@ public:
                 label srcFacei = -1,
                 label tgtFacei = -1
             );
+
+            //- Set the face areas for parallel runs
+            virtual void setMagSf
+            (
+                const TargetPatch& tgtPatch,
+                const mapDistribute& map,
+                scalarList& srcMagSf,
+                scalarList& tgtMagSf
+            ) const;
 };
 
 
-- 
GitLab