/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "mappedPatchBase.H"
#include "SubField.H"
#include "Time.H"
#include "triPointRef.H"
#include "treeDataPoint.H"
#include "indexedOctree.H"
#include "globalIndex.H"
#include "RemoteData.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(mappedPatchBase, 0);
const scalar mappedPatchBase::defaultMatchTol_ = 1e-4;
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::tmp Foam::mappedPatchBase::patchFaceAreas() const
{
return patch_.primitivePatch::faceAreas();
}
Foam::tmp Foam::mappedPatchBase::patchFaceCentres() const
{
return patch_.primitivePatch::faceCentres();
}
Foam::tmp Foam::mappedPatchBase::patchLocalPoints() const
{
return patch_.localPoints();
}
Foam::tmp Foam::mappedPatchBase::nbrPatchFaceAreas() const
{
return
nbrPatchIsMapped()
? nbrMappedPatch().patchFaceAreas()
: tmp(nbrPolyPatch().primitivePatch::faceAreas());
}
Foam::tmp Foam::mappedPatchBase::nbrPatchFaceCentres() const
{
return
nbrPatchIsMapped()
? nbrMappedPatch().patchFaceCentres()
: tmp(nbrPolyPatch().primitivePatch::faceCentres());
}
Foam::tmp Foam::mappedPatchBase::nbrPatchLocalPoints() const
{
return
nbrPatchIsMapped()
? nbrMappedPatch().patchLocalPoints()
: tmp(nbrPolyPatch().localPoints());
}
bool Foam::mappedPatchBase::mappingIsValid() const
{
const label bothMappingIsValid =
nbrPatchIsMapped()
? min(mappingIsValid_, nbrMappedPatch().nbrMappingIsValid_)
: mappingIsValid_;
return
(bothMappingIsValid == 2)
|| (
bothMappingIsValid == 1
&& !mappedPatchBase::moving
(
patch_,
nbrPolyPatch()
)
);
}
void Foam::mappedPatchBase::calcMapping() const
{
clearOut();
if (sameUntransformedPatch())
{
FatalErrorInFunction
<< "Patch " << patch_.name() << " is mapping itself with no "
<< "transform. Mapping data does not need to be constructed."
<< exit(FatalError);
}
// Calculate the transform as necessary
transform_ =
cyclicTransform
(
patch_.name(),
patchFaceCentres(),
patchFaceAreas(),
transform_,
nbrPolyPatch().name(),
nbrPatchFaceCentres(),
nbrPatchFaceAreas(),
nbrPatchIsMapped()
? nbrMappedPatch().transform_
: cyclicTransform(false),
matchTol_,
true
);
// Build the mapping
if (usingTree_)
{
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()] =
transform_.transform().invTransformPosition
(
patchFaceCentres()
);
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 mapped faces
List> allNearest(patchGlobalIndex.size());
// Find nearest face opposite every face
if (nbrPolyPatch().empty())
{
forAll(allPoints, alli)
{
allNearest[alli].proci = -1;
allNearest[alli].elementi = -1;
allNearest[alli].data = Foam::sqr(great);
}
}
else if (nbrPolyPatch().size() == 1)
{
const pointField nbrPoints(nbrPatchFaceCentres());
forAll(allPoints, alli)
{
const point& p = allPoints[alli];
allNearest[alli].proci = Pstream::myProcNo();
allNearest[alli].elementi = 0;
allNearest[alli].data = magSqr(nbrPoints[0] - p);
}
}
else
{
const pointField nbrPoints(nbrPatchFaceCentres());
const treeBoundBox nbrPointsBb
(
treeBoundBox(nbrPoints).extend(1e-4)
);
const indexedOctree tree
(
treeDataPoint(nbrPoints),
nbrPointsBb, // overall search domain
8, // maxLevel
10, // leafsize
3.0 // duplicity
);
forAll(allPoints, alli)
{
const point& p = allPoints[alli];
const pointIndexHit pih =
tree.findNearest(p, magSqr(nbrPointsBb.span()));
if (pih.hit())
{
allNearest[alli].proci = Pstream::myProcNo();
allNearest[alli].elementi = pih.index();
allNearest[alli].data =
magSqr(nbrPoints[pih.index()] - p);
}
}
}
// Find nearest. Combine on master.
Pstream::listCombineGather
(
allNearest,
RemoteData::smallestEqOp()
);
Pstream::listCombineScatter(allNearest);
// Determine the number of faces for which a nearest neighbouring
// face was not found (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 then fail
if (nNotFound)
{
FatalErrorInFunction
<< "Mapping to patch '" << patch_.name();
if (!sameRegion())
{
FatalErrorInFunction
<< "' in region '"
<< patch_.mesh().name();
}
FatalErrorInFunction
<< "' from patch '" << nbrPatchName();
if (!sameRegion())
{
FatalErrorInFunction
<< "' in region '" << nbrRegionName();
}
FatalErrorInFunction
<< "' failed " << exit(FatalError);
}
// Build lists of samples
DynamicList