ENH: Extended patchSeedSet to generate seed points more uniformly

This commit is contained in:
Andrew Heather
2023-12-02 20:46:36 +00:00
committed by Kutalmış Berçin
parent df71026509
commit ec48e2f309
2 changed files with 149 additions and 39 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd. Copyright (C) 2018-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,13 +28,12 @@ License
#include "patchSeedSet.H" #include "patchSeedSet.H"
#include "polyMesh.H" #include "polyMesh.H"
#include "addToRunTimeSelectionTable.H"
#include "treeBoundBox.H" #include "treeBoundBox.H"
#include "treeDataFace.H" #include "treeDataFace.H"
#include "Time.H"
#include "meshTools.H"
#include "mappedPatchBase.H" #include "mappedPatchBase.H"
#include "indirectPrimitivePatch.H" #include "indirectPrimitivePatch.H"
#include "triangulatedPatch.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -47,8 +46,10 @@ namespace Foam
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::patchSeedSet::calcSamples void Foam::patchSeedSet::calcPatchSamples
( (
const label nAvailable,
const label nPatchPoints,
DynamicList<point>& samplingPts, DynamicList<point>& samplingPts,
DynamicList<label>& samplingCells, DynamicList<label>& samplingCells,
DynamicList<label>& samplingFaces, DynamicList<label>& samplingFaces,
@ -56,39 +57,71 @@ void Foam::patchSeedSet::calcSamples
DynamicList<scalar>& samplingCurveDist DynamicList<scalar>& samplingCurveDist
) )
{ {
DebugInfo << "patchSeedSet : sampling on patches :" << endl; if (nAvailable < 1)
{
return;
}
// Construct search tree for all patch faces. Random& rndGen = *rndGenPtr_;
globalIndex globalSampleNumbers(nAvailable);
label nGlobalPatchPoints = returnReduce(nPatchPoints, sumOp<label>());
point pt;
label facei;
label celli;
const bool perturb = true;
for (const label patchi : patchSet_)
{
const polyPatch& pp = mesh().boundaryMesh()[patchi];
triangulatedPatch tp(pp, perturb);
const label np = nAvailable*pp.size()/scalar(nGlobalPatchPoints);
for (label i = 0; i < np; ++i)
{
tp.randomLocalPoint(rndGen, pt, facei, celli);
samplingPts.append(pt);
samplingCells.append(celli);
samplingFaces.append(pp.start() + facei);
samplingSegments.append(0);
samplingCurveDist.append(globalSampleNumbers.toGlobal(i));
}
}
}
void Foam::patchSeedSet::calcSelectedLocations
(
const label nAvailable,
const label nPatchPoints,
DynamicList<point>& samplingPts,
DynamicList<label>& samplingCells,
DynamicList<label>& samplingFaces,
DynamicList<label>& samplingSegments,
DynamicList<scalar>& samplingCurveDist
)
{
if (nAvailable < 1)
{
return;
}
Random& rndGen = *rndGenPtr_;
labelList patchFaces(nPatchPoints);
label sz = 0; label sz = 0;
for (const label patchi : patchSet_) for (const label patchi : patchSet_)
{ {
const polyPatch& pp = mesh().boundaryMesh()[patchi]; const polyPatch& pp = mesh().boundaryMesh()[patchi];
forAll(pp, localFacei)
sz += pp.size();
DebugInfo << " " << pp.name() << " size " << pp.size() << endl;
}
labelList patchFaces(sz);
sz = 0;
for (const label patchi : patchSet_)
{ {
const polyPatch& pp = mesh().boundaryMesh()[patchi]; patchFaces[sz++] = pp.start() + localFacei;
forAll(pp, i)
{
patchFaces[sz++] = pp.start()+i;
} }
} }
if (!rndGenPtr_)
{
rndGenPtr_.reset(new Random(0));
}
Random& rndGen = *rndGenPtr_;
if (selectedLocations_.size())
{ {
DynamicList<label> newPatchFaces(patchFaces.size()); DynamicList<label> newPatchFaces(patchFaces.size());
@ -98,7 +131,7 @@ void Foam::patchSeedSet::calcSamples
// selectedLocations // selectedLocations
// All the info for nearest. Construct to miss // All the info for nearest. Construct to miss
List<mappedPatchBase::nearInfo> nearest(selectedLocations_.size()); List<mappedPatchBase::nearInfo> nearest(nAvailable);
const indirectPrimitivePatch pp const indirectPrimitivePatch pp
( (
@ -130,7 +163,7 @@ void Foam::patchSeedSet::calcSamples
GREAT GREAT
); );
forAll(selectedLocations_, sampleI) for (label sampleI = 0; sampleI < nAvailable; ++sampleI)
{ {
const auto& treeData = boundaryTree.shapes(); const auto& treeData = boundaryTree.shapes();
const point& sample = selectedLocations_[sampleI]; const point& sample = selectedLocations_[sampleI];
@ -184,7 +217,7 @@ void Foam::patchSeedSet::calcSamples
if (debug) if (debug)
{ {
Pout<< "Found " << newPatchFaces.size() Pout<< "Found " << newPatchFaces.size()
<< " out of " << selectedLocations_.size() << " out of " << nAvailable
<< " on local processor" << endl; << " on local processor" << endl;
} }
@ -193,13 +226,12 @@ void Foam::patchSeedSet::calcSamples
// Shuffle and truncate if in random mode // Shuffle and truncate if in random mode
label totalSize = returnReduce(patchFaces.size(), sumOp<label>()); const label totalSize = returnReduce(patchFaces.size(), sumOp<label>());
if (maxPoints_ < totalSize) if (totalSize > nAvailable)
{ {
// Check what fraction of maxPoints_ I need to generate locally. // Check what fraction of maxPoints_ I need to generate locally.
label myMaxPoints = label myMaxPoints = scalar(patchFaces.size())/totalSize*nAvailable;
label(scalar(patchFaces.size())/totalSize*maxPoints_);
labelList subset = identity(patchFaces.size()); labelList subset = identity(patchFaces.size());
for (label iter = 0; iter < 4; ++iter) for (label iter = 0; iter < 4; ++iter)
@ -210,6 +242,7 @@ void Foam::patchSeedSet::calcSamples
std::swap(subset[i], subset[j]); std::swap(subset[i], subset[j]);
} }
} }
// Truncate // Truncate
subset.setSize(myMaxPoints); subset.setSize(myMaxPoints);
@ -239,7 +272,7 @@ void Foam::patchSeedSet::calcSamples
forAll(patchFaces, i) forAll(patchFaces, i)
{ {
label facei = patchFaces[i]; const label facei = patchFaces[i];
// Slightly shift point in since on warped face face-diagonal // Slightly shift point in since on warped face face-diagonal
// decomposition might be outside cell for face-centre decomposition! // decomposition might be outside cell for face-centre decomposition!
@ -249,7 +282,7 @@ void Foam::patchSeedSet::calcSamples
facei, facei,
polyMesh::FACE_DIAG_TRIS polyMesh::FACE_DIAG_TRIS
); );
label celli = mesh().faceOwner()[facei]; const label celli = mesh().faceOwner()[facei];
if (info.hit()) if (info.hit())
{ {
@ -313,6 +346,59 @@ void Foam::patchSeedSet::genSamples()
} }
void Foam::patchSeedSet::calcSamples
(
DynamicList<point>& samplingPts,
DynamicList<label>& samplingCells,
DynamicList<label>& samplingFaces,
DynamicList<label>& samplingSegments,
DynamicList<scalar>& samplingCurveDist
)
{
DebugInfo << "patchSeedSet : sampling on patches :" << endl;
if (!rndGenPtr_)
{
rndGenPtr_.reset(new Random(0));
}
label nPatchPoints = 0;
for (const label patchi : patchSet_)
{
const polyPatch& pp = mesh().boundaryMesh()[patchi];
nPatchPoints += pp.size();
DebugInfo << " " << pp.name() << " size " << pp.size() << endl;
}
label nAvailable = min(maxPoints_, selectedLocations_.size());
calcSelectedLocations
(
nAvailable,
nPatchPoints,
samplingPts,
samplingCells,
samplingFaces,
samplingSegments,
samplingCurveDist
);
nAvailable = maxPoints_ - nAvailable;
calcPatchSamples
(
nAvailable,
nPatchPoints,
samplingPts,
samplingCells,
samplingFaces,
samplingSegments,
samplingCurveDist
);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::patchSeedSet::patchSeedSet Foam::patchSeedSet::patchSeedSet

View File

@ -82,6 +82,30 @@ class patchSeedSet
// Private Member Functions // Private Member Functions
//- Determine (random) samples on patch
void calcPatchSamples
(
const label nAvailable,
const label nPatchPoints,
DynamicList<point>& samplingPts,
DynamicList<label>& samplingCells,
DynamicList<label>& samplingFaces,
DynamicList<label>& samplingSegments,
DynamicList<scalar>& samplingCurveDist
);
//- Determine samples provided by supplied points
void calcSelectedLocations
(
const label nAvailable,
const label nPatchPoints,
DynamicList<point>& samplingPts,
DynamicList<label>& samplingCells,
DynamicList<label>& samplingFaces,
DynamicList<label>& samplingSegments,
DynamicList<scalar>& samplingCurveDist
);
//- Samples all points in sampleCoords. //- Samples all points in sampleCoords.
void calcSamples void calcSamples
( (