From f19883c0c60f66a25c4881c62726d696cfe77dbc Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 8 Dec 2021 19:59:16 +0000
Subject: [PATCH] ENH: patchProbes: output patch. Fixes #2291.

---
 src/sampling/probes/patchProbes.C | 47 +++++++++++++++++++------------
 src/sampling/probes/probes.C      | 26 +++++++++++++++--
 src/sampling/probes/probes.H      | 10 ++++++-
 3 files changed, 62 insertions(+), 21 deletions(-)

diff --git a/src/sampling/probes/patchProbes.C b/src/sampling/probes/patchProbes.C
index e638c89f947..c630eeacb91 100644
--- a/src/sampling/probes/patchProbes.C
+++ b/src/sampling/probes/patchProbes.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2016-2020 OpenCFD Ltd.
+    Copyright (C) 2016-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -92,26 +92,26 @@ void Foam::patchProbes::findElements(const fvMesh& mesh)
         overallBb.min() -= point::uniform(ROOTVSMALL);
         overallBb.max() += point::uniform(ROOTVSMALL);
 
+
         const indexedOctree<treeDataFace> boundaryTree
         (
             treeDataFace    // all information needed to search faces
             (
-                false,                      // do not cache bb
+                false,      // do not cache bb
                 mesh,
-                bndFaces                    // patch faces only
+                bndFaces    // patch faces only
             ),
-            overallBb,                      // overall search domain
-            8,                              // maxLevel
-            10,                             // leafsize
-            3.0                             // duplicity
+            overallBb,      // overall search domain
+            8,              // maxLevel
+            10,             // leafsize
+            3.0             // duplicity
         );
 
-
         forAll(probeLocations(), probei)
         {
             const point sample = probeLocations()[probei];
 
-            scalar span = boundaryTree.bb().mag();
+            const scalar span = boundaryTree.bb().mag();
 
             pointIndexHit info = boundaryTree.findNearest
             (
@@ -169,14 +169,15 @@ void Foam::patchProbes::findElements(const fvMesh& mesh)
     Pstream::listCombineGather(nearest, mappedPatchBase::nearestEqOp());
     Pstream::listCombineScatter(nearest);
 
+    oldPoints_.resize(this->size());
 
-    // Update actual probe locations
+    // Update actual probe locations and store old ones
     forAll(nearest, samplei)
     {
+        oldPoints_[samplei] = operator[](samplei);
         operator[](samplei) = nearest[samplei].first().rawPoint();
     }
 
-
     if (debug)
     {
         InfoInFunction << nl;
@@ -193,29 +194,39 @@ void Foam::patchProbes::findElements(const fvMesh& mesh)
         }
     }
 
-
-    // Extract any local faces to sample
+    // Extract any local faces to sample:
+    // - operator[] : actual point to sample (=nearest point on patch)
+    // - oldPoints_ : original provided point (might be anywhere in the mesh)
+    // - elementList_   : cells, not used
+    // - faceList_      : faces (now patch faces)
+    // - patchIDList_   : patch corresponding to faceList
+    // - processor_     : processor
     elementList_.setSize(nearest.size());
     elementList_ = -1;
     faceList_.setSize(nearest.size());
     faceList_ = -1;
     processor_.setSize(nearest.size());
     processor_ = -1;
-
-    processor_.setSize(size());
-    processor_ = -1;
+    patchIDList_.setSize(nearest.size());
+    patchIDList_ = -1;
 
     forAll(nearest, sampleI)
     {
         processor_[sampleI] = nearest[sampleI].second().second();
+
         if (nearest[sampleI].second().second() == Pstream::myProcNo())
         {
             // Store the face to sample
             faceList_[sampleI] = nearest[sampleI].first().index();
-            label facei = faceList_[sampleI];
-            processor_[sampleI] = (facei != -1 ? Pstream::myProcNo() : -1);
+            const label facei = faceList_[sampleI];
+            if (facei != -1)
+            {
+                processor_[sampleI] = Pstream::myProcNo();
+                patchIDList_[sampleI] = bm.whichPatch(facei);
+            }
         }
         reduce(processor_[sampleI], maxOp<label>());
+        reduce(patchIDList_[sampleI], maxOp<label>());
     }
 }
 
diff --git a/src/sampling/probes/probes.C b/src/sampling/probes/probes.C
index 63b0222af6e..cd73a5e7c5c 100644
--- a/src/sampling/probes/probes.C
+++ b/src/sampling/probes/probes.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2015-2020 OpenCFD Ltd.
+    Copyright (C) 2015-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -251,6 +251,28 @@ Foam::label Foam::probes::prepare()
                 {
                     fout<< "  # Not Found";
                 }
+                // Only for patchProbes
+                else if (probei < patchIDList_.size())
+                {
+                    const label patchi = patchIDList_[probei];
+                    if (patchi != -1)
+                    {
+                        const polyBoundaryMesh& bm = mesh_.boundaryMesh();
+                        if
+                        (
+                            patchi < bm.nNonProcessor()
+                         || processor_[probei] == Pstream::myProcNo()
+                        )
+                        {
+                            fout<< " at patch " << bm[patchi].name();
+                        }
+                        fout<< " with a distance of "
+                            << mag(operator[](probei)-oldPoints_[probei])
+                            << " m to the original point "
+                            << oldPoints_[probei];
+                    }
+                }
+
                 fout<< endl;
             }
 
@@ -325,7 +347,7 @@ bool Foam::probes::read(const dictionary& dict)
         {
             WarningInFunction
                 << "Only cell interpolation can be applied when "
-                << "not using fixedLocations.  InterpolationScheme "
+                << "not using fixedLocations. InterpolationScheme "
                 << "entry will be ignored"
                 << endl;
         }
diff --git a/src/sampling/probes/probes.H b/src/sampling/probes/probes.H
index de56fa8f799..0c6639e85c6 100644
--- a/src/sampling/probes/probes.H
+++ b/src/sampling/probes/probes.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2016-2020 OpenCFD Ltd.
+    Copyright (C) 2016-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -188,6 +188,14 @@ protected:
         //- Current open files
         HashPtrTable<OFstream> probeFilePtrs_;
 
+        // Additional fields for patchProbes
+
+            //- Patch IDs on which the new probes are located
+            labelList patchIDList_;
+
+            //- Original probes location (only used for patchProbes)
+            pointField oldPoints_;
+
 
     // Protected Member Functions
 
-- 
GitLab