/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "polyMesh.H"
#include "OSspecific.H"
#include "Time.H"
#include "cellIOList.H"
#include "wedgePolyPatch.H"
#include "emptyPolyPatch.H"
#include "globalMeshData.H"
#include "processorPolyPatch.H"
#include "polyMeshTetDecomposition.H"
#include "meshObjects.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(polyMesh, 0);
}
Foam::word Foam::polyMesh::defaultRegion = "region0";
Foam::word Foam::polyMesh::meshSubDir = "polyMesh";
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::fileName Foam::polyMesh::regionDir(const IOobject& io)
{
if (io.name() == defaultRegion)
{
return io.db().dbDir()/io.local();
}
else
{
return io.db().dbDir()/io.local()/io.name();
}
}
void Foam::polyMesh::calcDirections() const
{
for (direction cmpt=0; cmpt(boundary()[patchi]))
{
nEmptyPatches++;
emptyDirVec += sum(cmptMag(boundary()[patchi].faceAreas()));
}
else if (isA(boundary()[patchi]))
{
const wedgePolyPatch& wpp = refCast
(
boundary()[patchi]
);
nWedgePatches++;
wedgeDirVec += cmptMag(wpp.centreNormal());
}
}
}
reduce(nEmptyPatches, maxOp());
reduce(nWedgePatches, maxOp());
if (nEmptyPatches)
{
reduce(emptyDirVec, sumOp());
emptyDirVec /= mag(emptyDirVec);
for (direction cmpt=0; cmpt 1e-6)
{
solutionD_[cmpt] = -1;
}
else
{
solutionD_[cmpt] = 1;
}
}
}
// Knock out wedge directions
geometricD_ = solutionD_;
if (nWedgePatches)
{
reduce(wedgeDirVec, sumOp());
wedgeDirVec /= mag(wedgeDirVec);
for (direction cmpt=0; cmpt 1e-6)
{
geometricD_[cmpt] = -1;
}
else
{
geometricD_[cmpt] = 1;
}
}
}
}
Foam::autoPtr Foam::polyMesh::readTetBasePtIs() const
{
typeIOobject io
(
"tetBasePtIs",
instance(),
meshSubDir,
*this,
IOobject::READ_IF_PRESENT,
IOobject::NO_WRITE
);
if (io.headerOk())
{
return autoPtr(new labelIOList(io));
}
else
{
return autoPtr(nullptr);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::polyMesh::polyMesh(const IOobject& io)
:
objectRegistry(io, regionDir(io)),
primitiveMesh(),
points_
(
IOobject
(
"points",
time().findInstance(meshDir(), "points"),
meshSubDir,
*this,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
),
faces_
(
IOobject
(
"faces",
time().findInstance(meshDir(), "faces"),
meshSubDir,
*this,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
),
owner_
(
IOobject
(
"owner",
faces_.instance(),
meshSubDir,
*this,
IOobject::READ_IF_PRESENT,
IOobject::NO_WRITE
)
),
neighbour_
(
IOobject
(
"neighbour",
faces_.instance(),
meshSubDir,
*this,
IOobject::READ_IF_PRESENT,
IOobject::NO_WRITE
)
),
clearedPrimitives_(false),
boundary_
(
IOobject
(
"boundary",
faces_.instance(),
meshSubDir,
*this,
IOobject::MUST_READ,
IOobject::NO_WRITE
),
*this
),
bounds_(points_),
comm_(UPstream::worldComm),
geometricD_(Zero),
solutionD_(Zero),
tetBasePtIsPtr_(readTetBasePtIs()),
pointZones_
(
IOobject
(
"pointZones",
time().findInstance
(
meshDir(),
"pointZones",
IOobject::READ_IF_PRESENT,
faces_.instance()
),
meshSubDir,
*this,
IOobject::NO_READ, // Delay reading
IOobject::NO_WRITE
),
*this
),
faceZones_
(
IOobject
(
"faceZones",
time().findInstance
(
meshDir(),
"faceZones",
IOobject::READ_IF_PRESENT,
faces_.instance()
),
meshSubDir,
*this,
IOobject::NO_READ, // Delay reading
IOobject::NO_WRITE
),
*this
),
cellZones_
(
IOobject
(
"cellZones",
time().findInstance
(
meshDir(),
"cellZones",
IOobject::READ_IF_PRESENT,
faces_.instance()
),
meshSubDir,
*this,
IOobject::NO_READ, // Delay reading
IOobject::NO_WRITE
),
*this
),
globalMeshDataPtr_(nullptr),
curMotionTimeIndex_(-1),
oldPointsPtr_(nullptr),
oldCellCentresPtr_(nullptr),
storeOldCellCentres_(false),
moving_(false),
topoChanged_(false)
{
if (!owner_.headerClassName().empty())
{
initMesh();
}
else
{
cellCompactIOList cLst
(
IOobject
(
"cells",
faces_.instance(),
meshSubDir,
*this,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);
// Set the primitive mesh
initMesh(cLst);
owner_.write();
neighbour_.write();
}
// Calculate topology for the patches (processor-processor comms etc.)
boundary_.topoChange();
// Calculate the geometry for the patches (transformation tensors etc.)
boundary_.calcGeometry();
// Warn if global empty mesh
if (time().completeCase() && returnReduce(nPoints(), sumOp()) == 0)
{
WarningInFunction
<< "no points in mesh" << endl;
}
if (time().completeCase() && returnReduce(nCells(), sumOp()) == 0)
{
WarningInFunction
<< "no cells in mesh" << endl;
}
// Initialise demand-driven data
calcDirections();
// Read the zones now that the mesh geometry is available to construct them
pointZones_.readIfPresent();
faceZones_.readIfPresent();
cellZones_.readIfPresent();
}
Foam::polyMesh::polyMesh
(
const IOobject& io,
pointField&& points,
faceList&& faces,
labelList&& owner,
labelList&& neighbour,
const bool syncPar
)
:
objectRegistry(io, regionDir(io)),
primitiveMesh(),
points_
(
IOobject
(
"points",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::AUTO_WRITE
),
move(points)
),
faces_
(
IOobject
(
"faces",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::AUTO_WRITE
),
move(faces)
),
owner_
(
IOobject
(
"owner",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::AUTO_WRITE
),
move(owner)
),
neighbour_
(
IOobject
(
"neighbour",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::AUTO_WRITE
),
move(neighbour)
),
clearedPrimitives_(false),
boundary_
(
IOobject
(
"boundary",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::AUTO_WRITE
),
*this,
polyPatchList()
),
bounds_(points_, syncPar),
comm_(UPstream::worldComm),
geometricD_(Zero),
solutionD_(Zero),
tetBasePtIsPtr_(readTetBasePtIs()),
pointZones_
(
IOobject
(
"pointZones",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::NO_WRITE
),
*this
),
faceZones_
(
IOobject
(
"faceZones",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::NO_WRITE
),
*this
),
cellZones_
(
IOobject
(
"cellZones",
instance(),
meshSubDir,
*this,
io.readOpt(),
IOobject::NO_WRITE
),
*this
),
globalMeshDataPtr_(nullptr),
curMotionTimeIndex_(-1),
oldPointsPtr_(nullptr),
oldCellCentresPtr_(nullptr),
storeOldCellCentres_(false),
moving_(false),
topoChanged_(false)
{
// Check if the faces and cells are valid
forAll(faces_, facei)
{
const face& curFace = faces_[facei];
if (min(curFace) < 0 || max(curFace) > points_.size())
{
FatalErrorInFunction
<< "Face " << facei << "contains vertex labels out of range: "
<< curFace << " Max point index = " << points_.size()
<< abort(FatalError);
}
}
// Set the primitive mesh
initMesh();
}
Foam::polyMesh::polyMesh
(
const IOobject& io,
pointField&& points,
faceList&& faces,
cellList&& cells,
const bool syncPar
)
:
objectRegistry(io, regionDir(io)),
primitiveMesh(),
points_
(
IOobject
(
"points",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
move(points)
),
faces_
(
IOobject
(
"faces",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
move(faces)
),
owner_
(
IOobject
(
"owner",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
label(0)
),
neighbour_
(
IOobject
(
"neighbour",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
label(0)
),
clearedPrimitives_(false),
boundary_
(
IOobject
(
"boundary",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
*this,
0
),
bounds_(points_, syncPar),
comm_(UPstream::worldComm),
geometricD_(Zero),
solutionD_(Zero),
tetBasePtIsPtr_(readTetBasePtIs()),
pointZones_
(
IOobject
(
"pointZones",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::NO_WRITE
),
*this
),
faceZones_
(
IOobject
(
"faceZones",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::NO_WRITE
),
*this
),
cellZones_
(
IOobject
(
"cellZones",
instance(),
meshSubDir,
*this,
IOobject::NO_READ,
IOobject::NO_WRITE
),
*this
),
globalMeshDataPtr_(nullptr),
curMotionTimeIndex_(-1),
oldPointsPtr_(nullptr),
oldCellCentresPtr_(nullptr),
storeOldCellCentres_(false),
moving_(false),
topoChanged_(false)
{
// Check if faces are valid
forAll(faces_, facei)
{
const face& curFace = faces_[facei];
if (min(curFace) < 0 || max(curFace) > points_.size())
{
FatalErrorInFunction
<< "Face " << facei << "contains vertex labels out of range: "
<< curFace << " Max point index = " << points_.size()
<< abort(FatalError);
}
}
// transfer in cell list
cellList cLst(move(cells));
// Check if cells are valid
forAll(cLst, celli)
{
const cell& curCell = cLst[celli];
if (min(curCell) < 0 || max(curCell) > faces_.size())
{
FatalErrorInFunction
<< "Cell " << celli << "contains face labels out of range: "
<< curCell << " Max face index = " << faces_.size()
<< abort(FatalError);
}
}
// Set the primitive mesh
initMesh(cLst);
}
Foam::polyMesh::polyMesh(polyMesh&& mesh)
:
objectRegistry(move(mesh)),
primitiveMesh(move(mesh)),
points_(move(mesh.points_)),
faces_(move(mesh.faces_)),
owner_(move(mesh.owner_)),
neighbour_(move(mesh.neighbour_)),
clearedPrimitives_(mesh.clearedPrimitives_),
boundary_(move(mesh.boundary_)),
bounds_(move(mesh.bounds_)),
comm_(mesh.comm_),
geometricD_(mesh.geometricD_),
solutionD_(mesh.solutionD_),
tetBasePtIsPtr_(move(mesh.tetBasePtIsPtr_)),
pointZones_(move(mesh.pointZones_)),
faceZones_(move(mesh.faceZones_)),
cellZones_(move(mesh.cellZones_)),
globalMeshDataPtr_(move(mesh.globalMeshDataPtr_)),
curMotionTimeIndex_(mesh.curMotionTimeIndex_),
oldPointsPtr_(move(mesh.oldPointsPtr_)),
oldCellCentresPtr_(move(mesh.oldCellCentresPtr_)),
storeOldCellCentres_(mesh.storeOldCellCentres_),
moving_(mesh.moving_),
topoChanged_(mesh.topoChanged_)
{}
void Foam::polyMesh::resetPrimitives
(
pointField&& points,
faceList&& faces,
labelList&& owner,
labelList&& neighbour,
const labelList& patchSizes,
const labelList& patchStarts,
const bool validBoundary
)
{
// Clear mesh objects
meshObjects::clear(*this);
// Clear addressing
clearAddressing();
// Take over new primitive data.
// Optimised to avoid overwriting data at all
if (notNull(points))
{
points_ = move(points);
bounds_ = boundBox(points_, validBoundary);
}
if (notNull(faces))
{
faces_ = move(faces);
}
if (notNull(owner))
{
owner_ = move(owner);
}
if (notNull(neighbour))
{
neighbour_ = move(neighbour);
}
// Reset patch sizes and starts
forAll(boundary_, patchi)
{
boundary_[patchi] = polyPatch
(
boundary_[patchi],
boundary_,
patchi,
patchSizes[patchi],
patchStarts[patchi]
);
}
// Flags the mesh files as being changed
setInstance(time().name());
// Check if the faces and cells are valid
forAll(faces_, facei)
{
const face& curFace = faces_[facei];
if (min(curFace) < 0 || max(curFace) > points_.size())
{
FatalErrorInFunction
<< "Face " << facei << " contains vertex labels out of range: "
<< curFace << " Max point index = " << points_.size()
<< abort(FatalError);
}
}
// Set the primitive mesh from the owner_, neighbour_.
// Works out from patch end where the active faces stop.
initMesh();
if (validBoundary)
{
// Note that we assume that all the patches stay the same and are
// correct etc. so we can already use the patches to do
// processor-processor comms.
// Calculate topology for the patches (processor-processor comms etc.)
boundary_.topoChange();
// Calculate the geometry for the patches (transformation tensors etc.)
boundary_.calcGeometry();
// Warn if global empty mesh
if
(
(returnReduce(nPoints(), sumOp()) == 0)
|| (returnReduce(nCells(), sumOp()) == 0)
)
{
FatalErrorInFunction
<< "no points or no cells in mesh"
<< exit(FatalError);
}
}
// Mapping handled by specific mapping function
// meshObjects::reset(*this);
}
void Foam::polyMesh::swap(polyMesh& otherMesh)
{
// Keep meshObjects that have an topoChange callback
meshObjects::clearUpto
<
polyMesh,
DeletableMeshObject,
TopoChangeableMeshObject
>
(
*this
);
// Clear addressing
clearAddressing();
// Keep meshObjects that have an topoChange callback
meshObjects::clearUpto
<
polyMesh,
DeletableMeshObject,
TopoChangeableMeshObject
>
(
otherMesh
);
otherMesh.clearAddressing();
// Swap the primitives
points_.swap(otherMesh.points_);
bounds_ = boundBox(points_, true);
faces_.swap(otherMesh.faces_);
owner_.swap(otherMesh.owner_);
neighbour_.swap(otherMesh.neighbour_);
// Clear the boundary data
boundary_.clearGeom();
boundary_.clearAddressing();
otherMesh.boundary_.clearGeom();
otherMesh.boundary_.clearAddressing();
// Swap the boundaries
auto updatePatches = []
(
const polyPatchList& otherPatches,
polyBoundaryMesh& boundaryMesh
)
{
boundaryMesh.resize(otherPatches.size());
forAll(otherPatches, otherPatchi)
{
// Clone processor patches, as the decomposition may be different
// in the other mesh. Just change the size and start of other
// patches.
if (isA(otherPatches[otherPatchi]))
{
boundaryMesh.set
(
otherPatchi,
otherPatches[otherPatchi].clone(boundaryMesh)
);
}
else
{
boundaryMesh[otherPatchi] = polyPatch
(
boundaryMesh[otherPatchi],
boundaryMesh,
otherPatchi,
otherPatches[otherPatchi].size(),
otherPatches[otherPatchi].start()
);
}
}
};
{
const polyPatchList patches
(
boundary_,
otherMesh.boundary_
);
const polyPatchList otherPatches
(
otherMesh.boundary_,
boundary_
);
updatePatches(otherPatches, boundary_);
updatePatches(patches, otherMesh.boundary_);
}
// Parallel data depends on the patch ordering so force recalculation
globalMeshDataPtr_.clear();
otherMesh.globalMeshDataPtr_.clear();
// Flags the mesh files as being changed
setInstance(time().name());
otherMesh.setInstance(time().name());
// Check if the faces and cells are valid
auto checkFaces = [](const polyMesh& mesh)
{
forAll(mesh.faces_, facei)
{
const face& curFace = mesh.faces_[facei];
if (min(curFace) < 0 || max(curFace) > mesh.points_.size())
{
FatalErrorInFunction
<< "Face " << facei << " contains vertex labels out of "
<< "range: " << curFace << " Max point index = "
<< mesh.points_.size() << abort(FatalError);
}
}
};
checkFaces(*this);
checkFaces(otherMesh);
// Set the primitive mesh from the owner_, neighbour_.
// Works out from patch end where the active faces stop.
initMesh();
otherMesh.initMesh();
// Calculate topology for the patches (processor-processor comms etc.)
boundary_.topoChange();
otherMesh.boundary_.topoChange();
// Calculate the geometry for the patches (transformation tensors etc.)
boundary_.calcGeometry();
otherMesh.boundary_.calcGeometry();
// Reset permanent meshObjects with respect to the updated polyMesh
meshObjects::swap(*this, otherMesh);
// Swap zones
pointZones_.swap(otherMesh.pointZones_);
faceZones_.swap(otherMesh.faceZones_);
cellZones_.swap(otherMesh.cellZones_);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::polyMesh::~polyMesh()
{
clearOut();
resetMotion();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::fileName Foam::polyMesh::meshDirInstance(const IOobject& io)
{
// Create an IO object for the current-time polyMesh directory
const IOobject curDirIo
(
word::null,
io.time().name(),
io.name() == polyMesh::defaultRegion
? fileName(io.local()/meshSubDir)
: fileName(io.local()/io.name()/meshSubDir),
io.time(),
Foam::IOobject::NO_READ
);
// Search back to find the latest-time polyMesh directory
const IOobject latestDirIo =
fileHandler().findInstance(curDirIo, io.time().value(), word::null);
// Return the instance if the polyMesh directory exists, or null if not
return
fileHandler().isDir(fileHandler().objectPath(latestDirIo))
? latestDirIo.instance()
: fileName::null;
}
Foam::fileName Foam::polyMesh::meshDir() const
{
return dbDir()/meshSubDir;
}
const Foam::fileName& Foam::polyMesh::pointsInstance() const
{
return points_.instance();
}
const Foam::fileName& Foam::polyMesh::facesInstance() const
{
return faces_.instance();
}
Foam::IOobject::writeOption Foam::polyMesh::pointsWriteOpt() const
{
return points_.writeOpt();
}
Foam::IOobject::writeOption Foam::polyMesh::facesWriteOpt() const
{
return faces_.writeOpt();
}
const Foam::Vector& Foam::polyMesh::geometricD() const
{
if (geometricD_.x() == 0)
{
calcDirections();
}
return geometricD_;
}
Foam::label Foam::polyMesh::nGeometricD() const
{
return cmptSum(geometricD() + Vector