/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
\\/ 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 "mappedInternalPatchBase.H"
#include "SubField.H"
#include "Time.H"
#include "triPointRef.H"
#include "meshSearch.H"
#include "globalIndex.H"
#include "RemoteData.H"
#include "OBJstream.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(mappedInternalPatchBase, 0);
}
const Foam::NamedEnum
Foam::mappedInternalPatchBase::offsetModeNames_
{
"normal",
"direction"
};
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::mappedInternalPatchBase::offsetMode
Foam::mappedInternalPatchBase::readOffsetMode
(
const dictionary& dict
) const
{
if (dict.found("offsetMode"))
{
return offsetModeNames_.read(dict.lookup("offsetMode"));
}
else
{
const bool haveDistance = dict.found("distance");
const bool haveOffset = dict.found("offset");
if (haveDistance == haveOffset)
{
// Error. Demand "offsetMode" setting to disambiguate.
return offsetModeNames_.read(dict.lookup("offsetMode"));
}
else if (haveDistance)
{
return NORMAL;
}
else
{
return DIRECTION;
}
}
}
void Foam::mappedInternalPatchBase::calcMapping() const
{
if (mapPtr_.valid())
{
FatalErrorInFunction
<< "Mapping already calculated" << exit(FatalError);
}
const polyMesh& nbrMesh = this->nbrMesh();
const meshSearch& nbrSearchEngine = meshSearch::New(nbrMesh);
const globalIndex patchGlobalIndex(patch_.size());
// Find processor and cell/face indices of samples
labelList sampleGlobalPatchFaces, sampleIndices;
{
// Gather the sample points into a single globally indexed list
List allPoints(patchGlobalIndex.size());
{
List procSamplePoints(Pstream::nProcs());
procSamplePoints[Pstream::myProcNo()] = this->samplePoints();
Pstream::gatherList(procSamplePoints);
Pstream::scatterList(procSamplePoints);
forAll(procSamplePoints, proci)
{
forAll(procSamplePoints[proci], procSamplei)
{
allPoints
[
patchGlobalIndex.toGlobal(proci, procSamplei)
] = procSamplePoints[proci][procSamplei];
}
}
}
// List of possibly remote sampling cells
List> allNearest(patchGlobalIndex.size());
// Find containing cell for every sampling point
forAll(allPoints, alli)
{
const point& p = allPoints[alli];
const label celli = nbrSearchEngine.findCell(p);
if (celli != -1)
{
const point& cc = nbrMesh.cellCentres()[celli];
allNearest[alli].proci = Pstream::myProcNo();
allNearest[alli].elementi = celli;
allNearest[alli].data = magSqr(cc - p);
}
}
// Find nearest. Combine on master.
Pstream::listCombineGather
(
allNearest,
RemoteData::smallestEqOp()
);
Pstream::listCombineScatter(allNearest);
// Determine the number of missing samples (no reduction necessary as
// this is computed from synchronised data)
label nNotFound = 0;
forAll(allPoints, alli)
{
if (allNearest[alli].proci == -1)
{
nNotFound ++;
}
}
// If any points were not found within cells then re-search for them
// using a nearest test, which should not fail. Warn that this is
// happening. If any points were not found for some other method, then
// fail.
if (nNotFound)
{
WarningInFunction
<< "Did not find a containing cell for " << nNotFound
<< " out of " << returnReduce(patch_.size(), sumOp())
<< " total faces. Using nearest cell for these faces instead."
<< endl << "On patch " << patch_.name() << " on region "
<< nbrRegionName() << " with offset mode "
<< offsetModeNames_[offsetMode_] << "." << endl;
forAll(allPoints, alli)
{
const point& p = allPoints[alli];
if (allNearest[alli].proci == -1)
{
const pointIndexHit pih =
nbrSearchEngine.cellTree().findNearest(p, sqr(great));
allNearest[alli].proci = Pstream::myProcNo();
allNearest[alli].elementi = pih.index();
allNearest[alli].data = magSqr(pih.hitPoint() - p);
}
}
Pstream::listCombineGather
(
allNearest,
RemoteData::smallestEqOp()
);
Pstream::listCombineScatter(allNearest);
}
// Build lists of samples
DynamicList