/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "meshRefinement.H"
#include "refinementSurfaces.H"
#include "faceSet.H"
#include "polyTopoChange.H"
#include "localPointRegion.H"
#include "duplicatePoints.H"
#include "regionSplit.H"
#include "removeCells.H"
#include "OBJstream.H"
#include "patchFaceOrientation.H"
#include "PatchEdgeFaceWave.H"
#include "patchEdgeFaceRegion.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::label Foam::meshRefinement::createBaffle
(
const label facei,
const label ownPatch,
const label nbrPatch,
polyTopoChange& meshMod
) const
{
const face& f = mesh_.faces()[facei];
meshMod.modifyFace
(
f, // modified face
facei, // label of face
mesh_.faceOwner()[facei], // owner
-1, // neighbour
false, // face flip
ownPatch // patch for face
);
label dupFacei = -1;
if (mesh_.isInternalFace(facei))
{
if (nbrPatch == -1)
{
FatalErrorInFunction
<< "No neighbour patch for internal face " << facei
<< " fc:" << mesh_.faceCentres()[facei]
<< " ownPatch:" << ownPatch << abort(FatalError);
}
dupFacei = meshMod.addFace
(
f.reverseFace(), // modified face
mesh_.faceNeighbour()[facei],// owner
-1, // neighbour
facei, // masterFaceID,
true, // face flip
nbrPatch // patch for face
);
}
return dupFacei;
}
void Foam::meshRefinement::getBafflePatches
(
const labelList& globalToMasterPatch,
const labelList& neiLevel,
const pointField& neiCc,
labelList& ownPatch,
labelList& nbrPatch
) const
{
const pointField& cellCentres = mesh_.cellCentres();
// Surfaces that need to be baffled
const labelList surfacesToBaffle
(
surfaceZonesInfo::getUnnamedSurfaces(surfaces_.surfZones())
);
ownPatch.setSize(mesh_.nFaces());
ownPatch = -1;
nbrPatch.setSize(mesh_.nFaces());
nbrPatch = -1;
// Collect candidate faces
// ~~~~~~~~~~~~~~~~~~~~~~~
labelList testFaces(intersectedFaces());
// Collect segments
// ~~~~~~~~~~~~~~~~
pointField start(testFaces.size());
pointField end(testFaces.size());
forAll(testFaces, i)
{
const label facei = testFaces[i];
const label own = mesh_.faceOwner()[facei];
if (mesh_.isInternalFace(facei))
{
start[i] = cellCentres[own];
end[i] = cellCentres[mesh_.faceNeighbour()[facei]];
}
else
{
start[i] = cellCentres[own];
end[i] = neiCc[facei-mesh_.nInternalFaces()];
}
}
// Extend segments a bit
{
const vectorField smallVec(rootSmall*(end-start));
start -= smallVec;
end += smallVec;
}
// Do test for intersections
// ~~~~~~~~~~~~~~~~~~~~~~~~~
labelList surface1;
List hit1;
labelList region1;
labelList surface2;
List hit2;
labelList region2;
surfaces_.findNearestIntersection
(
surfacesToBaffle,
start,
end,
surface1,
hit1,
region1,
surface2,
hit2,
region2
);
forAll(testFaces, i)
{
const label facei = testFaces[i];
if (hit1[i].hit() && hit2[i].hit())
{
// Pick up the patches
ownPatch[facei] = globalToMasterPatch
[
surfaces_.globalRegion(surface1[i], region1[i])
];
nbrPatch[facei] = globalToMasterPatch
[
surfaces_.globalRegion(surface2[i], region2[i])
];
if (ownPatch[facei] == -1 || nbrPatch[facei] == -1)
{
FatalErrorInFunction
<< "problem." << abort(FatalError);
}
}
}
// No need to parallel sync since intersection data (surfaceIndex_ etc.)
// already guaranteed to be synced...
// However:
// - owncc and neicc are reversed on different procs so might pick
// up different regions reversed? No problem. Neighbour on one processor
// might not be owner on the other processor but the neighbour is
// not used when creating baffles from proc faces.
// - tolerances issues occasionally crop up.
syncTools::syncFaceList(mesh_, ownPatch, maxEqOp());
syncTools::syncFaceList(mesh_, nbrPatch, maxEqOp());
}
Foam::Map Foam::meshRefinement::getZoneBafflePatches
(
const bool allowBoundary,
const labelList& globalToMasterPatch,
const labelList& globalToSlavePatch
) const
{
Map bafflePatch(mesh_.nFaces()/1000);
const PtrList& surfZones = surfaces_.surfZones();
const faceZoneList& fZones = mesh_.faceZones();
forAll(surfZones, surfi)
{
const word& faceZoneName = surfZones[surfi].faceZoneName();
if (faceZoneName.size())
{
// Get zone
const label zonei = fZones.findIndex(faceZoneName);
const faceZone& fZone = fZones[zonei];
// Get patch allocated for zone
const label globalRegioni = surfaces_.globalRegion(surfi, 0);
const labelPair zPatches
(
globalToMasterPatch[globalRegioni],
globalToSlavePatch[globalRegioni]
);
Info<< "For zone " << fZone.name() << " found patches "
<< mesh_.poly().boundary()[zPatches[0]].name() << " and "
<< mesh_.poly().boundary()[zPatches[1]].name()
<< endl;
forAll(fZone, i)
{
const label facei = fZone[i];
if (allowBoundary || mesh_.isInternalFace(facei))
{
labelPair patches = zPatches;
if (fZone.flipMap()[i])
{
patches = reverse(patches);
}
if (!bafflePatch.insert(facei, patches))
{
FatalErrorInFunction
<< "Face " << facei
<< " fc:" << mesh_.faceCentres()[facei]
<< " in zone " << fZone.name()
<< " is in multiple zones!"
<< abort(FatalError);
}
}
}
}
}
return bafflePatch;
}
Foam::autoPtr Foam::meshRefinement::createBaffles
(
const labelList& ownPatch,
const labelList& nbrPatch
)
{
if
(
ownPatch.size() != mesh_.nFaces()
|| nbrPatch.size() != mesh_.nFaces()
)
{
FatalErrorInFunction
<< "Illegal size :"
<< " ownPatch:" << ownPatch.size()
<< " nbrPatch:" << nbrPatch.size()
<< ". Should be number of faces:" << mesh_.nFaces()
<< abort(FatalError);
}
if (debug)
{
labelList syncedOwnPatch(ownPatch);
syncTools::syncFaceList(mesh_, syncedOwnPatch, maxEqOp());
labelList syncedNeiPatch(nbrPatch);
syncTools::syncFaceList(mesh_, syncedNeiPatch, maxEqOp());
forAll(syncedOwnPatch, facei)
{
if
(
(ownPatch[facei] == -1 && syncedOwnPatch[facei] != -1)
|| (nbrPatch[facei] == -1 && syncedNeiPatch[facei] != -1)
)
{
FatalErrorInFunction
<< "Non synchronised at face:" << facei
<< " on patch:" << mesh_.poly().boundary().whichPatch(facei)
<< " fc:" << mesh_.faceCentres()[facei] << endl
<< "ownPatch:" << ownPatch[facei]
<< " syncedOwnPatch:" << syncedOwnPatch[facei]
<< " nbrPatch:" << nbrPatch[facei]
<< " syncedNeiPatch:" << syncedNeiPatch[facei]
<< abort(FatalError);
}
}
}
// Topochange container
polyTopoChange meshMod(mesh_);
label nBaffles = 0;
forAll(ownPatch, facei)
{
if (ownPatch[facei] != -1)
{
// Create baffle or repatch face. Return label of inserted baffle
// face.
createBaffle
(
facei,
ownPatch[facei], // owner side patch
nbrPatch[facei], // neighbour side patch
meshMod
);
nBaffles++;
}
}
mesh_.clearOut();
// Change the mesh, parallel sync
autoPtr map = meshMod.changeMesh(mesh_, true);
// Update fields
mesh_.topoChange(map);
// Reset the instance for if in overwrite mode
mesh_.setInstance(name());
//- Redo the intersections on the newly create baffle faces. Note that
// this changes also the cell centre positions.
faceSet baffledFacesSet(mesh_, "baffledFacesSet", 2*nBaffles);
const labelList& reverseFaceMap = map().reverseFaceMap();
const labelList& faceMap = map().faceMap();
// Pick up owner side of baffle
forAll(ownPatch, oldFacei)
{
const label facei = reverseFaceMap[oldFacei];
if (ownPatch[oldFacei] != -1 && facei >= 0)
{
const cell& ownFaces = mesh_.cells()[mesh_.faceOwner()[facei]];
forAll(ownFaces, i)
{
baffledFacesSet.insert(ownFaces[i]);
}
}
}
// Pick up neighbour side of baffle (added faces)
forAll(faceMap, facei)
{
const label oldFacei = faceMap[facei];
if (oldFacei >= 0 && reverseFaceMap[oldFacei] != facei)
{
const cell& ownFaces = mesh_.cells()[mesh_.faceOwner()[facei]];
forAll(ownFaces, i)
{
baffledFacesSet.insert(ownFaces[i]);
}
}
}
baffledFacesSet.sync(mesh_);
topoChange(map, baffledFacesSet.toc());
return map;
}
void Foam::meshRefinement::checkZoneFaces() const
{
const faceZoneList& fZones = mesh_.faceZones();
const polyBoundaryMesh& pbm = mesh_.poly().boundary();
forAll(pbm, patchi)
{
const polyPatch& pp = pbm[patchi];
if (isA(pp))
{
forAll(pp, i)
{
const label facei = pp.start() + i;
const labelList zones = fZones.whichZones(facei);
forAll(zones, zonei)
{
if (zones[zonei] != -1)
{
FatalErrorInFunction
<< "face:" << facei << " on patch " << pp.name()
<< " is in zone " << fZones[zones[zonei]].name()
<< exit(FatalError);
}
}
}
}
}
}
Foam::autoPtr Foam::meshRefinement::createZoneBaffles
(
const labelList& globalToMasterPatch,
const labelList& globalToSlavePatch,
List& baffles
)
{
const labelList zonedSurfaces
(
surfaceZonesInfo::getNamedSurfaces(surfaces_.surfZones())
);
autoPtr map;
// No need to sync; all processors will have all same zonedSurfaces.
if (zonedSurfaces.size())
{
// Split internal faces on interface surfaces
Info<< "Converting zoned faces into baffles ..." << endl;
// Get faces (internal only) to be baffled. Map from face to patch
// label.
Map faceToPatch
(
getZoneBafflePatches
(
false,
globalToMasterPatch,
globalToSlavePatch
)
);
const label nZoneFaces =
returnReduce(faceToPatch.size(), sumOp());
if (nZoneFaces > 0)
{
// Convert into labelLists
labelList ownPatch(mesh_.nFaces(), -1);
labelList nbrPatch(mesh_.nFaces(), -1);
forAllConstIter(Map, faceToPatch, iter)
{
ownPatch[iter.key()] = iter().first();
nbrPatch[iter.key()] = iter().second();
}
// Create baffles. both sides same patch.
map = createBaffles(ownPatch, nbrPatch);
// Get pairs of faces created.
// Just loop over faceMap and store baffle if we encounter a slave
// face.
baffles.setSize(faceToPatch.size());
label baffleI = 0;
const labelList& faceMap = map().faceMap();
const labelList& reverseFaceMap = map().reverseFaceMap();
forAll(faceMap, facei)
{
const label oldFacei = faceMap[facei];
// Does face originate from face-to-patch
Map::const_iterator iter = faceToPatch.find
(
oldFacei
);
if (iter != faceToPatch.end())
{
const label masterFacei = reverseFaceMap[oldFacei];
if (facei != masterFacei)
{
baffles[baffleI++] = labelPair(masterFacei, facei);
}
}
}
if (baffleI != faceToPatch.size())
{
FatalErrorInFunction
<< "Had " << faceToPatch.size() << " patches to create "
<< " but encountered " << baffleI
<< " slave faces originating from patcheable faces."
<< abort(FatalError);
}
if (debug&MESH)
{
const_cast