From fc6a8b648ffd2e95ef95598bee4b2338afb5aa76 Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 17 Aug 2017 10:00:14 +0100 Subject: [PATCH] ENH: shortestPath: new line sampling method ('set') that detects the shortest path between two sets of points. See shortestPathSet.H. Fixes #572. --- src/sampling/Make/files | 1 + .../sampledSet/sampledSet/sampledSet.C | 72 ++++ .../sampledSet/sampledSet/sampledSet.H | 6 +- .../sampledSet/sampledSets/sampledSets.C | 59 +-- .../sampledSet/shortestPath/shortestPathSet.C | 344 ++++++++++++++++++ .../sampledSet/shortestPath/shortestPathSet.H | 149 ++++++++ 6 files changed, 572 insertions(+), 59 deletions(-) create mode 100644 src/sampling/sampledSet/shortestPath/shortestPathSet.C create mode 100644 src/sampling/sampledSet/shortestPath/shortestPathSet.H diff --git a/src/sampling/Make/files b/src/sampling/Make/files index de74ebb8de..3bb94cc39b 100644 --- a/src/sampling/Make/files +++ b/src/sampling/Make/files @@ -16,6 +16,7 @@ sampledSet/sampledSets/sampledSetsGrouping.C sampledSet/triSurfaceMeshPointSet/triSurfaceMeshPointSet.C sampledSet/uniform/uniformSet.C sampledSet/array/arraySet.C +sampledSet/shortestPath/shortestPathSet.C surface/cuttingPlane/cuttingPlane.C surface/isoSurface/isoSurface.C diff --git a/src/sampling/sampledSet/sampledSet/sampledSet.C b/src/sampling/sampledSet/sampledSet/sampledSet.C index 78694160f3..a6da49aad3 100644 --- a/src/sampling/sampledSet/sampledSet/sampledSet.C +++ b/src/sampling/sampledSet/sampledSet/sampledSet.C @@ -29,6 +29,7 @@ License #include "meshSearch.H" #include "writer.H" #include "particle.H" +#include "SortableList.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -394,6 +395,77 @@ void Foam::sampledSet::setSamples } +Foam::autoPtr Foam::sampledSet::gather +( + labelList& indexSet +) const +{ + // Combine sampleSet from processors. Sort by curveDist. Return + // ordering in indexSet. + // Note: only master results are valid + + // Collect data from all processors + List> gatheredPts(Pstream::nProcs()); + gatheredPts[Pstream::myProcNo()] = *this; + Pstream::gatherList(gatheredPts); + + List gatheredSegments(Pstream::nProcs()); + gatheredSegments[Pstream::myProcNo()] = segments(); + Pstream::gatherList(gatheredSegments); + + List gatheredDist(Pstream::nProcs()); + gatheredDist[Pstream::myProcNo()] = curveDist(); + Pstream::gatherList(gatheredDist); + + + // Combine processor lists into one big list. + List allPts + ( + ListListOps::combine> + ( + gatheredPts, accessOp>() + ) + ); + labelList allSegments + ( + ListListOps::combine + ( + gatheredSegments, accessOp() + ) + ); + scalarList allCurveDist + ( + ListListOps::combine + ( + gatheredDist, accessOp() + ) + ); + + + if (Pstream::master() && allCurveDist.size() == 0) + { + WarningInFunction + << "Sample set " << name() + << " has zero points." << endl; + } + + // Sort curveDist and use to fill masterSamplePts + SortableList sortedDist(allCurveDist); + indexSet = sortedDist.indices(); + + return autoPtr + ( + new coordSet + ( + name(), + axis(), + List(UIndirectList(allPts, indexSet)), + sortedDist + ) + ); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::sampledSet::sampledSet diff --git a/src/sampling/sampledSet/sampledSet/sampledSet.H b/src/sampling/sampledSet/sampledSet/sampledSet.H index 1f146c9a86..172f9bfa65 100644 --- a/src/sampling/sampledSet/sampledSet/sampledSet.H +++ b/src/sampling/sampledSet/sampledSet/sampledSet.H @@ -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. @@ -266,6 +266,10 @@ public: //- Output for debugging Ostream& write(Ostream&) const; + + //- Helper: gather onto master and sort. Return (on master) gathered set + // and overall sort order + autoPtr gather(labelList& indexSet) const; }; diff --git a/src/sampling/sampledSet/sampledSets/sampledSets.C b/src/sampling/sampledSet/sampledSets/sampledSets.C index 2115280372..b762de557a 100644 --- a/src/sampling/sampledSet/sampledSets/sampledSets.C +++ b/src/sampling/sampledSet/sampledSets/sampledSets.C @@ -70,67 +70,10 @@ void Foam::sampledSets::combineSampledSets forAll(sampledSets, setI) { - const sampledSet& samplePts = sampledSets[setI]; - - // Collect data from all processors - List> gatheredPts(Pstream::nProcs()); - gatheredPts[Pstream::myProcNo()] = samplePts; - Pstream::gatherList(gatheredPts); - - List gatheredSegments(Pstream::nProcs()); - gatheredSegments[Pstream::myProcNo()] = samplePts.segments(); - Pstream::gatherList(gatheredSegments); - - List gatheredDist(Pstream::nProcs()); - gatheredDist[Pstream::myProcNo()] = samplePts.curveDist(); - Pstream::gatherList(gatheredDist); - - - // Combine processor lists into one big list. - List allPts - ( - ListListOps::combine> - ( - gatheredPts, accessOp>() - ) - ); - labelList allSegments - ( - ListListOps::combine - ( - gatheredSegments, accessOp() - ) - ); - scalarList allCurveDist - ( - ListListOps::combine - ( - gatheredDist, accessOp() - ) - ); - - - if (Pstream::master() && allCurveDist.size() == 0) - { - WarningInFunction - << "Sample set " << samplePts.name() - << " has zero points." << endl; - } - - // Sort curveDist and use to fill masterSamplePts - SortableList sortedDist(allCurveDist); - indexSets[setI] = sortedDist.indices(); - masterSampledSets.set ( setI, - new coordSet - ( - samplePts.name(), - samplePts.axis(), - List(UIndirectList(allPts, indexSets[setI])), - sortedDist - ) + sampledSets[setI].gather(indexSets[setI]) ); } } diff --git a/src/sampling/sampledSet/shortestPath/shortestPathSet.C b/src/sampling/sampledSet/shortestPath/shortestPathSet.C new file mode 100644 index 0000000000..d289f43b97 --- /dev/null +++ b/src/sampling/sampledSet/shortestPath/shortestPathSet.C @@ -0,0 +1,344 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "shortestPathSet.H" +#include "meshSearch.H" +#include "DynamicList.H" +#include "topoDistanceData.H" +#include "addToRunTimeSelectionTable.H" +#include "FaceCellWave.H" +#include "syncTools.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(shortestPathSet, 0); + addToRunTimeSelectionTable(sampledSet, shortestPathSet, word); +} + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +Foam::label Foam::shortestPathSet::findMinFace +( + const polyMesh& mesh, + const label cellI, + const List& allFaceInfo, + const point& origin +) +{ + const cell& cFaces2 = mesh.cells()[cellI]; + + // 1. Get topologically nearest face + + label minDist = labelMax; + label minFaceI = -1; + forAll(cFaces2, i) + { + label faceI = cFaces2[i]; + const topoDistanceData& info = allFaceInfo[faceI]; + if (info.distance() < minDist) + { + minDist = info.distance(); + minFaceI = faceI; + } + } + + // 2. Check all faces with minDist for minimum distance to origin + scalar minDist2 = ROOTVGREAT; + forAll(cFaces2, i) + { + label faceI = cFaces2[i]; + if (allFaceInfo[faceI].distance() == minDist) + { + scalar d2 = magSqr(mesh.faceCentres()[faceI]-origin); + if (d2 < minDist2) + { + minDist2 = d2; + minFaceI = faceI; + } + } + } + + return minFaceI; +} + + +void Foam::shortestPathSet::genSamples(const polyMesh& mesh) +{ + // Storage for sample points + DynamicList samplingPts; + DynamicList