/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "polyMeshZipUpCells.H"
#include "polyMesh.H"
#include "Time.H"
// #define DEBUG_ZIPUP 1
// #define DEBUG_CHAIN 1
// #define DEBUG_ORDER 1
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
bool Foam::polyMeshZipUpCells(polyMesh& mesh)
{
if (polyMesh::debug)
{
Info<< "bool polyMeshZipUpCells(polyMesh& mesh) const: "
<< "zipping up topologically open cells" << endl;
}
// Algorithm:
// Take the original mesh and visit all cells. For every cell
// calculate the edges of all faces on the cells. A cell is
// correctly topologically closed when all the edges are referenced
// by exactly two faces. If the edges are referenced only by a
// single face, additional vertices need to be inserted into some
// of the faces (topological closedness). If an edge is
// referenced by more that two faces, there is an error in
// topological closedness.
// Point insertion into the faces is done by attempting to create
// closed loops and inserting the intermediate points into the
// defining edge
// Note:
// The algorithm is recursive and changes the mesh faces in each
// pass. It is therefore essential to discard the addressing
// after every pass. The algorithm is completed when the mesh
// stops changing.
label nChangedFacesInMesh = 0;
label nCycles = 0;
labelHashSet problemCells;
do
{
nChangedFacesInMesh = 0;
const cellList& Cells = mesh.cells();
const pointField& Points = mesh.points();
faceList newFaces = mesh.faces();
const faceList& oldFaces = mesh.faces();
const labelListList& pFaces = mesh.pointFaces();
forAll(Cells, celli)
{
const labelList& curFaces = Cells[celli];
const edgeList cellEdges = Cells[celli].edges(oldFaces);
const labelList cellPoints = Cells[celli].labels(oldFaces);
// Find the edges used only once in the cell
labelList edgeUsage(cellEdges.size(), 0);
forAll(curFaces, facei)
{
edgeList curFaceEdges = oldFaces[curFaces[facei]].edges();
forAll(curFaceEdges, faceEdgeI)
{
const edge& curEdge = curFaceEdges[faceEdgeI];
forAll(cellEdges, cellEdgeI)
{
if (cellEdges[cellEdgeI] == curEdge)
{
edgeUsage[cellEdgeI]++;
break;
}
}
}
}
edgeList singleEdges(cellEdges.size());
label nSingleEdges = 0;
forAll(edgeUsage, edgeI)
{
if (edgeUsage[edgeI] == 1)
{
singleEdges[nSingleEdges] = cellEdges[edgeI];
nSingleEdges++;
}
else if (edgeUsage[edgeI] != 2)
{
WarningInFunction
<< "edge " << cellEdges[edgeI] << " in cell " << celli
<< " used " << edgeUsage[edgeI] << " times. " << nl
<< "Should be 1 or 2 - serious error "
<< "in mesh structure. " << endl;
#ifdef DEBUG_ZIPUP
forAll(curFaces, facei)
{
Info<< "face: " << oldFaces[curFaces[facei]]
<< endl;
}
Info<< "Cell edges: " << cellEdges << nl
<< "Edge usage: " << edgeUsage << nl
<< "Cell points: " << cellPoints << endl;
forAll(cellPoints, cpI)
{
Info<< "vertex create \"" << cellPoints[cpI]
<< "\" coordinates "
<< Points[cellPoints[cpI]] << endl;
}
#endif
// Gather the problem cell
problemCells.insert(celli);
}
}
// Check if the cell is already zipped up
if (nSingleEdges == 0) continue;
singleEdges.setSize(nSingleEdges);
#ifdef DEBUG_ZIPUP
Info<< "Cell " << celli << endl;
forAll(curFaces, facei)
{
Info<< "face: " << oldFaces[curFaces[facei]] << endl;
}
Info<< "Cell edges: " << cellEdges << nl
<< "Edge usage: " << edgeUsage << nl
<< "Single edges: " << singleEdges << nl
<< "Cell points: " << cellPoints << endl;
forAll(cellPoints, cpI)
{
Info<< "vertex create \"" << cellPoints[cpI]
<< "\" coordinates "
<< points()[cellPoints[cpI]] << endl;
}
#endif
// Loop through all single edges and mark the points they use
// points marked twice are internal to edge; those marked more than
// twice are corners
labelList pointUsage(cellPoints.size(), 0);
forAll(singleEdges, edgeI)
{
const edge& curEdge = singleEdges[edgeI];
forAll(cellPoints, pointi)
{
if
(
cellPoints[pointi] == curEdge.start()
|| cellPoints[pointi] == curEdge.end()
)
{
pointUsage[pointi]++;
}
}
}
boolList singleEdgeUsage(singleEdges.size(), false);
// loop through all edges and eliminate the ones that are
// blocked out
forAll(singleEdges, edgeI)
{
bool blockedHead = false;
bool blockedTail = false;
label newEdgeStart = singleEdges[edgeI].start();
label newEdgeEnd = singleEdges[edgeI].end();
// check that the edge has not got all ends blocked
forAll(cellPoints, pointi)
{
if (cellPoints[pointi] == newEdgeStart)
{
if (pointUsage[pointi] > 2)
{
blockedHead = true;
}
}
else if (cellPoints[pointi] == newEdgeEnd)
{
if (pointUsage[pointi] > 2)
{
blockedTail = true;
}
}
}
if (blockedHead && blockedTail)
{
// Eliminating edge singleEdges[edgeI] as blocked
singleEdgeUsage[edgeI] = true;
}
}
// Go through the points and start from the point used twice
// check all the edges to find the edges starting from this point
// add the
labelListList edgesToInsert(singleEdges.size());
label nEdgesToInsert = 0;
// Find a good edge
forAll(singleEdges, edgeI)
{
SLList