From 4f1a7ec9db05a9a467496cc535e201f144ffb366 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 23 Feb 2022 15:35:16 +0000
Subject: [PATCH 1/2] ENH: cyclicAMI: extend faceAreaWeight to filter. See
 #2378

---
 .../advancingFrontAMI/advancingFrontAMI.C     | 79 ++++++++++++++++++-
 .../advancingFrontAMI/advancingFrontAMI.H     | 10 +++
 .../faceAreaWeightAMI/faceAreaWeightAMI.C     | 16 +---
 .../faceAreaWeightAMI2D/faceAreaWeightAMI2D.C |  8 +-
 4 files changed, 94 insertions(+), 19 deletions(-)

diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C
index 01f83db8c07..64de10b285d 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C
@@ -81,6 +81,57 @@ void Foam::advancingFrontAMI::checkPatches() const
 }
 
 
+bool Foam::advancingFrontAMI::isCandidate
+(
+    const label srcFacei,
+    const label tgtFacei
+) const
+{
+    const auto& srcPatch = this->srcPatch();
+    const auto& tgtPatch = this->tgtPatch();
+
+    if
+    (
+        (srcMagSf_[srcFacei] < ROOTVSMALL)
+     || (tgtMagSf_[tgtFacei] < ROOTVSMALL)
+    )
+    {
+        return false;
+    }
+
+    if (maxDistance2_ < GREAT)
+    {
+        const point& srcFc = srcPatch.faceCentres()[srcFacei];
+        const point& tgtFc = tgtPatch.faceCentres()[tgtFacei];
+        const vector& srcN = srcPatch.faceNormals()[srcFacei];
+
+        const scalar normalDist((tgtFc-srcFc)&srcN);
+        //if (magSqr(srcFc-tgtFc) >= maxDistance2_)
+        if (sqr(normalDist) >= maxDistance2_)
+        {
+            return false;
+        }
+    }
+
+    if (minCosAngle_ > -1)
+    {
+        const vector& srcN = srcPatch.faceNormals()[srcFacei];
+        vector tgtN = tgtPatch.faceNormals()[tgtFacei];
+        if (!reverseTarget_)
+        {
+            tgtN = -tgtN;
+        }
+
+        if ((srcN & tgtN) <= minCosAngle_)
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
 void Foam::advancingFrontAMI::createExtendedTgtPatch()
 {
     // Create processor map of extended cells. This map gets (possibly
@@ -251,7 +302,7 @@ Foam::label Foam::advancingFrontAMI::findTargetFace
     const pointIndexHit sample =
         treePtr_->findNearest(srcPt, magSqr(bb.max() - bb.centre()), fnOp);
 
-    if (sample.hit())
+    if (sample.hit() && isCandidate(srcFacei, sample.index()))
     {
         targetFacei = sample.index();
 
@@ -375,6 +426,8 @@ Foam::advancingFrontAMI::advancingFrontAMI
 )
 :
     AMIInterpolation(dict, reverseTarget),
+    maxDistance2_(dict.getOrDefault<scalar>("maxDistance2", GREAT)),
+    minCosAngle_(dict.getOrDefault<scalar>("minCosAngle", -1)),
     srcTris_(),
     tgtTris_(),
     extendedTgtPatchPtr_(nullptr),
@@ -404,6 +457,8 @@ Foam::advancingFrontAMI::advancingFrontAMI
 )
 :
     AMIInterpolation(requireMatch, reverseTarget, lowWeightCorrection),
+    maxDistance2_(GREAT),
+    minCosAngle_(-GREAT),
     srcTris_(),
     tgtTris_(),
     extendedTgtPatchPtr_(nullptr),
@@ -419,6 +474,8 @@ Foam::advancingFrontAMI::advancingFrontAMI
 Foam::advancingFrontAMI::advancingFrontAMI(const advancingFrontAMI& ami)
 :
     AMIInterpolation(ami),
+    maxDistance2_(ami.maxDistance2_),
+    minCosAngle_(ami.minCosAngle_),
     srcTris_(),
     tgtTris_(),
     extendedTgtPatchPtr_(nullptr),
@@ -452,6 +509,24 @@ bool Foam::advancingFrontAMI::calculate
         const auto& src = this->srcPatch();
         const auto& tgt = this->tgtPatch();
 
+
+        if (maxDistance2_ < GREAT)
+        {
+            // Early trigger face centre calculation
+            (void)src.faceCentres();
+            (void)tgt.faceCentres();
+            // Early trigger face normals calculation
+            (void)src.faceNormals();
+            (void)tgt.faceNormals();
+        }
+        if (minCosAngle_ > -1)
+        {
+            // Early trigger face normals calculation
+            (void)src.faceNormals();
+            (void)tgt.faceNormals();
+        }
+
+
         // Initialise area magnitudes
         srcMagSf_.setSize(src.size(), 1.0);
         tgtMagSf_.setSize(tgt.size(), 1.0);
@@ -479,6 +554,8 @@ bool Foam::advancingFrontAMI::calculate
 void Foam::advancingFrontAMI::write(Ostream& os) const
 {
     AMIInterpolation::write(os);
+    os.writeEntryIfDifferent<scalar>("maxDistance2", GREAT, maxDistance2_);
+    os.writeEntryIfDifferent<scalar>("minCosAngle", -1, minCosAngle_);
     os.writeEntryIfDifferent<word>
     (
         "triMode",
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.H b/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.H
index 9ba117ecd30..5cfec3e12d2 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.H
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.H
@@ -106,6 +106,12 @@ protected:
 
     // Protected data
 
+        //- Maximum squared distance
+        const scalar maxDistance2_;
+
+        //- Minimum (cos of) angle. 1 for perfectly matching.
+        const scalar minCosAngle_;
+
         //- Storage for src-side triangle decomposition
         List<DynamicList<face>> srcTris_;
 
@@ -148,6 +154,10 @@ protected:
             //- Check AMI patch coupling
             void checkPatches() const;
 
+            //- Is source/target a valid pair (i.e. not too far/different
+            //  orientation). Used for prefiltering before e.g. area overlap
+            bool isCandidate(const label srcFacei, const label tgtFacei) const;
+
             virtual bool calculate
             (
                 const primitivePatch& srcPatch,
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI/faceAreaWeightAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI/faceAreaWeightAMI.C
index 53063cce633..fe29d2c2c5c 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI/faceAreaWeightAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI/faceAreaWeightAMI.C
@@ -379,12 +379,8 @@ void Foam::faceAreaWeightAMI::calcInterArea
 {
     addProfiling(ami, "faceAreaWeightAMI::interArea");
 
-    // Quick reject if either face has zero area
-    if
-    (
-        (srcMagSf_[srcFacei] < ROOTVSMALL)
-     || (tgtMagSf_[tgtFacei] < ROOTVSMALL)
-    )
+    // Quick reject if either face has zero area/too far away/wrong orientation
+    if (!isCandidate(srcFacei, tgtFacei))
     {
         return;
     }
@@ -459,12 +455,8 @@ bool Foam::faceAreaWeightAMI::overlaps
     const scalar threshold
 ) const
 {
-    // Quick reject if either face has zero area
-    if
-    (
-        (srcMagSf_[srcFacei] < ROOTVSMALL)
-     || (tgtMagSf_[tgtFacei] < ROOTVSMALL)
-    )
+    // Quick reject if either face has zero area/too far away/wrong orientation
+    if (!isCandidate(srcFacei, tgtFacei))
     {
         return false;
     }
diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI2D/faceAreaWeightAMI2D.C b/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI2D/faceAreaWeightAMI2D.C
index bec5662ac9a..0e0be21dca9 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI2D/faceAreaWeightAMI2D.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/faceAreaWeightAMI2D/faceAreaWeightAMI2D.C
@@ -133,12 +133,8 @@ void Foam::faceAreaWeightAMI2D::storeInterArea
 {
     addProfiling(ami, "faceAreaWeightAMI2D::calcInterArea");
 
-    // Quick reject if either face has zero area
-    if
-    (
-        (srcMagSf_[srcFacei] < ROOTVSMALL)
-     || (tgtMagSf_[tgtFacei] < ROOTVSMALL)
-    )
+    // Quick reject if either face has zero area/too far away/wrong orientation
+    if (!isCandidate(srcFacei, tgtFacei))
     {
         return;
     }
-- 
GitLab


From 98a8cd58ba01face3cee87c6ba15fec52f22e6eb Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Thu, 24 Feb 2022 11:50:35 +0000
Subject: [PATCH 2/2] STYLE: cyclicAMI: use -1 as switch value. See #2378

---
 .../advancingFrontAMI/advancingFrontAMI.C            | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C
index 64de10b285d..bb434401bcd 100644
--- a/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C
+++ b/src/meshTools/AMIInterpolation/AMIInterpolation/advancingFrontAMI/advancingFrontAMI.C
@@ -99,7 +99,7 @@ bool Foam::advancingFrontAMI::isCandidate
         return false;
     }
 
-    if (maxDistance2_ < GREAT)
+    if (maxDistance2_ > 0)
     {
         const point& srcFc = srcPatch.faceCentres()[srcFacei];
         const point& tgtFc = tgtPatch.faceCentres()[tgtFacei];
@@ -426,7 +426,7 @@ Foam::advancingFrontAMI::advancingFrontAMI
 )
 :
     AMIInterpolation(dict, reverseTarget),
-    maxDistance2_(dict.getOrDefault<scalar>("maxDistance2", GREAT)),
+    maxDistance2_(dict.getOrDefault<scalar>("maxDistance2", -1)),
     minCosAngle_(dict.getOrDefault<scalar>("minCosAngle", -1)),
     srcTris_(),
     tgtTris_(),
@@ -457,8 +457,8 @@ Foam::advancingFrontAMI::advancingFrontAMI
 )
 :
     AMIInterpolation(requireMatch, reverseTarget, lowWeightCorrection),
-    maxDistance2_(GREAT),
-    minCosAngle_(-GREAT),
+    maxDistance2_(-1),
+    minCosAngle_(-1),
     srcTris_(),
     tgtTris_(),
     extendedTgtPatchPtr_(nullptr),
@@ -510,7 +510,7 @@ bool Foam::advancingFrontAMI::calculate
         const auto& tgt = this->tgtPatch();
 
 
-        if (maxDistance2_ < GREAT)
+        if (maxDistance2_ > 0)
         {
             // Early trigger face centre calculation
             (void)src.faceCentres();
@@ -554,7 +554,7 @@ bool Foam::advancingFrontAMI::calculate
 void Foam::advancingFrontAMI::write(Ostream& os) const
 {
     AMIInterpolation::write(os);
-    os.writeEntryIfDifferent<scalar>("maxDistance2", GREAT, maxDistance2_);
+    os.writeEntryIfDifferent<scalar>("maxDistance2", -1, maxDistance2_);
     os.writeEntryIfDifferent<scalar>("minCosAngle", -1, minCosAngle_);
     os.writeEntryIfDifferent<word>
     (
-- 
GitLab