From 986e4656ae172bad0794efcd990966edbe2e30e3 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 15 Mar 2017 12:01:23 +0000
Subject: [PATCH] BUG: topoSet: nearestToCell/Point return nearest on all
 processors. Fixes #427.

---
 .../primitiveMesh/primitiveMeshFindCell.C     |  5 ++++
 .../cellSources/nearestToCell/nearestToCell.C | 26 +++++++++++++++--
 .../nearestToPoint/nearestToPoint.C           | 29 ++++++++++++++++---
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshFindCell.C b/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshFindCell.C
index c26745d96ff..2a450ffebbb 100644
--- a/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshFindCell.C
+++ b/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshFindCell.C
@@ -86,6 +86,11 @@ Foam::label Foam::primitiveMesh::findNearestCell(const point& location) const
 {
     const vectorField& centres = cellCentres();
 
+    if (!centres.size())
+    {
+        return -1;
+    }
+
     label nearestCelli = 0;
     scalar minProximity = magSqr(centres[0] - location);
 
diff --git a/src/meshTools/sets/cellSources/nearestToCell/nearestToCell.C b/src/meshTools/sets/cellSources/nearestToCell/nearestToCell.C
index a7bba62b121..cb10f3a0492 100644
--- a/src/meshTools/sets/cellSources/nearestToCell/nearestToCell.C
+++ b/src/meshTools/sets/cellSources/nearestToCell/nearestToCell.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -26,6 +26,7 @@ License
 #include "nearestToCell.H"
 #include "polyMesh.H"
 #include "addToRunTimeSelectionTable.H"
+#include "mappedPatchBase.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -53,9 +54,30 @@ Foam::topoSetSource::addToUsageTable Foam::nearestToCell::usage_
 
 void Foam::nearestToCell::combine(topoSet& set, const bool add) const
 {
+    // All the info for nearest. Construct to miss
+    List<mappedPatchBase::nearInfo> nearest(points_.size());
+
     forAll(points_, pointi)
     {
-        addOrDelete(set, mesh_.findNearestCell(points_[pointi]), add);
+        label celli = mesh_.findNearestCell(points_[pointi]);
+        const point& cc = mesh_.cellCentres()[celli];
+        nearest[pointi].first() = pointIndexHit(true, cc, celli);
+        nearest[pointi].second() = Tuple2<scalar, label>
+        (
+            magSqr(cc-points_[pointi]),
+            Pstream::myProcNo()
+        );
+    }
+
+    Pstream::listCombineGather(nearest, mappedPatchBase::nearestEqOp());
+    Pstream::listCombineScatter(nearest);
+
+    forAll(nearest, pointi)
+    {
+        if (nearest[pointi].second().second() == Pstream::myProcNo())
+        {
+            addOrDelete(set, nearest[pointi].first().index(), add);
+        }
     }
 }
 
diff --git a/src/meshTools/sets/pointSources/nearestToPoint/nearestToPoint.C b/src/meshTools/sets/pointSources/nearestToPoint/nearestToPoint.C
index c83340db6d8..0f5d134ae90 100644
--- a/src/meshTools/sets/pointSources/nearestToPoint/nearestToPoint.C
+++ b/src/meshTools/sets/pointSources/nearestToPoint/nearestToPoint.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -26,6 +26,7 @@ License
 #include "nearestToPoint.H"
 #include "polyMesh.H"
 #include "addToRunTimeSelectionTable.H"
+#include "mappedPatchBase.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -53,12 +54,14 @@ Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
 
 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
 {
+    // All the info for nearest. Construct to miss
+    List<mappedPatchBase::nearInfo> nearest(points_.size());
+
     // Do linear search since usually just a few points.
+    const pointField& pts = mesh_.points();
 
     forAll(points_, pointi)
     {
-        const pointField& pts = mesh_.points();
-
         if (pts.size())
         {
             label minPointi = 0;
@@ -75,7 +78,25 @@ void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
                 }
             }
 
-            addOrDelete(set, minPointi, add);
+            const point& minPt = pts[minPointi];
+            nearest[pointi].first() = pointIndexHit(true, minPt, minPointi);
+            nearest[pointi].second() = Tuple2<scalar, label>
+            (
+                magSqr(minPt-points_[pointi]),
+                Pstream::myProcNo()
+            );
+        }
+    }
+
+
+    Pstream::listCombineGather(nearest, mappedPatchBase::nearestEqOp());
+    Pstream::listCombineScatter(nearest);
+
+    forAll(nearest, pointi)
+    {
+        if (nearest[pointi].second().second() == Pstream::myProcNo())
+        {
+            addOrDelete(set, nearest[pointi].first().index(), add);
         }
     }
 }
-- 
GitLab