/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2013-2025 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 "nearestCellsToCells.H"
#include "pointIndexHit.H"
#include "meshSearch.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace cellsToCellss
{
defineTypeNameAndDebug(nearest, 0);
addToRunTimeSelectionTable(cellsToCells, nearest, word);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::cellsToCellss::nearest::findInitialSeeds
(
const polyMesh& srcMesh,
const polyMesh& tgtMesh,
const labelList& srcCellIDs,
const boolList& mapFlag,
const label startSeedI,
label& srcSeedI,
label& tgtSeedI
) const
{
const vectorField& srcCcs = srcMesh.cellCentres();
const meshSearch& tgtSearchEngine = meshSearch::New(tgtMesh);
for (label i = startSeedI; i < srcCellIDs.size(); i++)
{
label srcI = srcCellIDs[i];
if (mapFlag[srcI])
{
const point& srcCc = srcCcs[srcI];
pointIndexHit hit =
tgtSearchEngine.cellTree().findNearest(srcCc, great);
if (hit.hit())
{
srcSeedI = srcI;
tgtSeedI = hit.index();
return true;
}
else
{
FatalErrorInFunction
<< "Unable to find nearest target cell"
<< " for source cell " << srcI
<< " with centre " << srcCc
<< abort(FatalError);
}
break;
}
}
if (debug)
{
Pout<< "could not find starting seed" << endl;
}
return false;
}
Foam::scalar Foam::cellsToCellss::nearest::calculateAddressing
(
const polyMesh& srcMesh,
const polyMesh& tgtMesh,
labelListList& srcToTgtCellAddr,
scalarListList& srcToTgtCellWght,
labelListList& tgtToSrcCellAddr,
scalarListList& tgtToSrcCellWght,
const label srcSeedI,
const label tgtSeedI,
const labelList& srcCellIDs,
boolList& mapFlag,
label& startSeedI
)
{
scalar V = 0;
List> srcToTgt(srcMesh.nCells());
List> tgtToSrc(tgtMesh.nCells());
const scalarField& srcVc = srcMesh.cellVolumes();
{
label srcCelli = srcSeedI;
label tgtCelli = tgtSeedI;
do
{
// find nearest tgt cell
findNearestCell(srcMesh, tgtMesh, srcCelli, tgtCelli);
// store src/tgt cell pair
srcToTgt[srcCelli].append(tgtCelli);
tgtToSrc[tgtCelli].append(srcCelli);
// mark source cell srcCelli and tgtCelli as matched
mapFlag[srcCelli] = false;
// accumulate intersection volume
V += srcVc[srcCelli];
// find new source cell
setNextNearestCells
(
srcMesh,
tgtMesh,
startSeedI,
srcCelli,
tgtCelli,
mapFlag,
srcCellIDs
);
}
while (srcCelli >= 0);
}
// for the case of multiple source cells per target cell, select the
// nearest source cell only and discard the others
const vectorField& srcCc = srcMesh.cellCentres();
const vectorField& tgtCc = tgtMesh.cellCentres();
forAll(tgtToSrc, targetCelli)
{
if (tgtToSrc[targetCelli].size() > 1)
{
const vector& tgtC = tgtCc[targetCelli];
DynamicList