diff --git a/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshFindCell.C b/src/OpenFOAM/meshes/primitiveMesh/primitiveMeshFindCell.C index c26745d96fffa7cdd5e6e85574095a7a3516e3d9..2a450ffebbb250ce0409dba0d62dc4a0ccbf9f6b 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 a7bba62b12104dc1becf19e76ea65cdfe4bb978f..cb10f3a0492b237974627eacbe8fc80c1567ea9f 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 c83340db6d8e37d12f46e30dfc24d0a884b458a9..0f5d134ae9083b74324ae46b297e02773c2832ca 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); } } }