/*---------------------------------------------------------------------------*\ ========= | \\ / 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 "fvMesh.H" #include "volFields.H" #include "surfaceFields.H" #include "pointFields.H" #include "slicedVolFields.H" #include "slicedSurfaceFields.H" #include "SubField.H" #include "demandDrivenData.H" #include "zonesGenerator.H" #include "fvMeshLduAddressing.H" #include "fvMeshTopoChanger.H" #include "fvMeshDistributor.H" #include "fvMeshMover.H" #include "fvMeshStitcher.H" #include "nonConformalFvPatch.H" #include "polyFacesFvsPatchLabelField.H" #include "polyTopoChangeMap.H" #include "MapFvFields.H" #include "fvMeshMapper.H" #include "pointMesh.H" #include "pointMeshMapper.H" #include "MapPointField.H" #include "meshObjects.H" #include "HashPtrTable.H" #include "CompactListList.H" #include "fvcSurfaceIntegrate.H" #include "fvcReconstruct.H" #include "surfaceInterpolate.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { defineTypeNameAndDebug(fvMesh, 0); } const Foam::HashSet Foam::fvMesh::geometryFields { "Vc", "Vc0", "Vc00", "Sf", "magSf", "Cc", "Cf", "meshPhi", "meshPhi_0" }; const Foam::HashSet Foam::fvMesh::curGeometryFields { "Vc", "Sf", "magSf", "Cc", "Cf" }; // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // void Foam::fvMesh::clearFvGeomNotOldVol() { DebugInFunction << "Clearing current-time FV geometry" << endl; meshObjects::clearUpto < fvMesh, DeletableMeshObject, MoveableMeshObject >(*this); meshObjects::clearUpto < lduMesh, DeletableMeshObject, MoveableMeshObject >(*this); deleteDemandDrivenData(VPtr_); deleteDemandDrivenData(SfSlicePtr_); deleteDemandDrivenData(SfPtr_); deleteDemandDrivenData(magSfSlicePtr_); deleteDemandDrivenData(magSfPtr_); deleteDemandDrivenData(CSlicePtr_); deleteDemandDrivenData(CPtr_); deleteDemandDrivenData(CfSlicePtr_); deleteDemandDrivenData(CfPtr_); } void Foam::fvMesh::clearFvGeom() { DebugInFunction << "Clearing FV Geometry" << endl; clearFvGeomNotOldVol(); deleteDemandDrivenData(phiPtr_); deleteDemandDrivenData(V0Ptr_); deleteDemandDrivenData(V00Ptr_); } void Foam::fvMesh::updateGeomNotOldVol() { bool haveV = (VPtr_ != nullptr); bool haveSf = (SfSlicePtr_ != nullptr || SfPtr_ != nullptr); bool haveMagSf = (magSfSlicePtr_ != nullptr || magSfPtr_ != nullptr); bool haveCP = (CSlicePtr_ != nullptr || CPtr_ != nullptr); bool haveCf = (CfSlicePtr_ != nullptr || CfPtr_ != nullptr); clearFvGeomNotOldVol(); // Now recreate the fields if (haveV) { (void)V(); } if (haveSf) { (void)Sf(); } if (haveMagSf) { (void)magSf(); } if (haveCP) { (void)C(); } if (haveCf) { (void)Cf(); } } void Foam::fvMesh::storeOldTimeFields() { storeOldTimeFields(); storeOldTimeFields(); storeOldTimeFields(); } void Foam::fvMesh::nullOldestTimeFields() { nullOldestTimeFields(); nullOldestTimeFields(); nullOldestTimeFields(); } void Foam::fvMesh::printAllocated() const { polyMesh::printAllocated(); Pout<< "fvMesh allocated :" << endl; if (lduPtr_) { Pout<< " Ldu Addressing" << endl; } if (polyFacesBfPtr_) { Pout<< " Poly-faces boundary field" << endl; } if (polyBFacePatchesPtr_) { Pout<< " Poly-boundary-face to fv-patch and fv-patch-face map" << endl; } if (ownerBfPtr_) { Pout<< " Owner boundary field" << endl; } if (V0Ptr_) { Pout<< " Old-time cell volumes field" << endl; } if (V00Ptr_) { Pout<< " Old-old-time cell volumes field" << endl; } if (SfPtr_) { Pout<< " Non-sliced face areas field" << endl; } if (magSfPtr_) { Pout<< " Non-sliced face area magnitudes field" << endl; } if (CPtr_) { Pout<< " Non-sliced cell centres field" << endl; } if (CfPtr_) { Pout<< " Non-sliced face centres field" << endl; } if (phiPtr_) { Pout<< " Mesh flux field" << endl; } surfaceInterpolation::printAllocated(); } void Foam::fvMesh::clearGeom() { DebugInFunction << "Clearing geometry" << endl; clearFvGeom(); polyMesh::clearGeom(); } void Foam::fvMesh::clearAddressing(const bool isMeshUpdate) { DebugInFunction << "isMeshUpdate: " << isMeshUpdate << endl; if (isMeshUpdate) { // Part of a mesh update. Keep meshObjects that have an topoChange // callback meshObjects::clearUpto < fvMesh, DeletableMeshObject, TopoChangeableMeshObject > ( *this ); meshObjects::clearUpto < lduMesh, DeletableMeshObject, TopoChangeableMeshObject > ( *this ); } else { meshObjects::clear(*this); meshObjects::clear(*this); } deleteDemandDrivenData(lduPtr_); deleteDemandDrivenData(polyFacesBfIOPtr_); deleteDemandDrivenData(polyFacesBfPtr_); deleteDemandDrivenData(polyBFaceOffsetsPtr_); deleteDemandDrivenData(polyBFaceOffsetPatchesPtr_); deleteDemandDrivenData(polyBFaceOffsetPatchFacesPtr_); deleteDemandDrivenData(polyBFacePatchesPtr_); deleteDemandDrivenData(polyBFacePatchFacesPtr_); deleteDemandDrivenData(ownerBfPtr_); } void Foam::fvMesh::clearOut() { clearFvGeom(); surfaceInterpolation::clearOut(); clearAddressing(); polyMesh::clearOut(); } Foam::wordList Foam::fvMesh::polyFacesPatchTypes() const { wordList wantedPatchTypes ( boundary().size(), polyFacesFvsPatchLabelField::typeName ); forAll(boundary(), patchi) { const fvPatch& fvp = boundary()[patchi]; if (isA(fvp)) { wantedPatchTypes[patchi] = refCast(fvp).polyFacesType(); } } return wantedPatchTypes; } Foam::surfaceLabelField::Boundary& Foam::fvMesh::polyFacesBfRef() { if (!polyFacesBfPtr_) { polyFacesBf(); } setPolyFacesBfInstance(time().name()); return *polyFacesBfPtr_; } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::fvMesh::fvMesh ( const IOobject& io, const bool doPost // const bool doZones ) : polyMesh(io), surfaceMesh(*this), surfaceInterpolation(*this), boundary_(*this, poly().boundary()), stitcher_(nullptr), topoChanger_(nullptr), distributor_(nullptr), mover_(nullptr), lduPtr_(nullptr), polyFacesBfIOPtr_(nullptr), polyFacesBfPtr_(nullptr), polyBFaceOffsetsPtr_(nullptr), polyBFaceOffsetPatchesPtr_(nullptr), polyBFaceOffsetPatchFacesPtr_(nullptr), polyBFacePatchesPtr_(nullptr), polyBFacePatchFacesPtr_(nullptr), ownerBfPtr_(nullptr), curTimeIndex_(time().timeIndex()), VPtr_(nullptr), V0Ptr_(nullptr), V00Ptr_(nullptr), SfSlicePtr_(nullptr), SfPtr_(nullptr), magSfSlicePtr_(nullptr), magSfPtr_(nullptr), CSlicePtr_(nullptr), CPtr_(nullptr), CfSlicePtr_(nullptr), CfPtr_(nullptr), phiPtr_(nullptr) { DebugInFunction << "Constructing fvMesh from IOobject" << endl; if (doPost) { postConstruct(true, true, stitchType::geometric); } } Foam::fvMesh::fvMesh ( const IOobject& io, pointField&& points, const cellShapeList& shapes, const faceListList& boundaryFaces, const wordList& boundaryPatchNames, const PtrList& boundaryDicts, const word& defaultBoundaryPatchName, const word& defaultBoundaryPatchType, const bool syncPar ) : polyMesh ( io, std::move(points), shapes, boundaryFaces, boundaryPatchNames, boundaryDicts, defaultBoundaryPatchName, defaultBoundaryPatchType, syncPar ), surfaceMesh(*this), surfaceInterpolation(*this), boundary_(*this, poly().boundary()), stitcher_(nullptr), topoChanger_(nullptr), distributor_(nullptr), mover_(nullptr), lduPtr_(nullptr), polyFacesBfIOPtr_(nullptr), polyFacesBfPtr_(nullptr), polyBFaceOffsetsPtr_(nullptr), polyBFaceOffsetPatchesPtr_(nullptr), polyBFaceOffsetPatchFacesPtr_(nullptr), polyBFacePatchesPtr_(nullptr), polyBFacePatchFacesPtr_(nullptr), ownerBfPtr_(nullptr), curTimeIndex_(time().timeIndex()), VPtr_(nullptr), V0Ptr_(nullptr), V00Ptr_(nullptr), SfSlicePtr_(nullptr), SfPtr_(nullptr), magSfSlicePtr_(nullptr), magSfPtr_(nullptr), CSlicePtr_(nullptr), CPtr_(nullptr), CfSlicePtr_(nullptr), CfPtr_(nullptr), phiPtr_(nullptr) { DebugInFunction << "Constructing fvMesh from shapes" << endl; } Foam::fvMesh::fvMesh ( const IOobject& io, pointField&& points, faceList&& faces, labelList&& allOwner, labelList&& allNeighbour, const bool syncPar ) : polyMesh ( io, std::move(points), std::move(faces), std::move(allOwner), std::move(allNeighbour), syncPar ), surfaceMesh(*this), surfaceInterpolation(*this), boundary_(*this, poly().boundary()), stitcher_(nullptr), topoChanger_(nullptr), distributor_(nullptr), mover_(nullptr), lduPtr_(nullptr), polyFacesBfIOPtr_(nullptr), polyFacesBfPtr_(nullptr), polyBFaceOffsetsPtr_(nullptr), polyBFaceOffsetPatchesPtr_(nullptr), polyBFaceOffsetPatchFacesPtr_(nullptr), polyBFacePatchesPtr_(nullptr), polyBFacePatchFacesPtr_(nullptr), ownerBfPtr_(nullptr), curTimeIndex_(time().timeIndex()), VPtr_(nullptr), V0Ptr_(nullptr), V00Ptr_(nullptr), SfSlicePtr_(nullptr), SfPtr_(nullptr), magSfSlicePtr_(nullptr), magSfPtr_(nullptr), CSlicePtr_(nullptr), CPtr_(nullptr), CfSlicePtr_(nullptr), CfPtr_(nullptr), phiPtr_(nullptr) { DebugInFunction << "Constructing fvMesh from components" << endl; } Foam::fvMesh::fvMesh ( const IOobject& io, pointField&& points, faceList&& faces, cellList&& cells, const bool syncPar ) : polyMesh ( io, std::move(points), std::move(faces), std::move(cells), syncPar ), surfaceMesh(*this), surfaceInterpolation(*this), boundary_(*this), stitcher_(nullptr), topoChanger_(nullptr), distributor_(nullptr), mover_(nullptr), lduPtr_(nullptr), polyFacesBfIOPtr_(nullptr), polyFacesBfPtr_(nullptr), polyBFaceOffsetsPtr_(nullptr), polyBFaceOffsetPatchesPtr_(nullptr), polyBFaceOffsetPatchFacesPtr_(nullptr), polyBFacePatchesPtr_(nullptr), polyBFacePatchFacesPtr_(nullptr), ownerBfPtr_(nullptr), curTimeIndex_(time().timeIndex()), VPtr_(nullptr), V0Ptr_(nullptr), V00Ptr_(nullptr), SfSlicePtr_(nullptr), SfPtr_(nullptr), magSfSlicePtr_(nullptr), magSfPtr_(nullptr), CSlicePtr_(nullptr), CPtr_(nullptr), CfSlicePtr_(nullptr), CfPtr_(nullptr), phiPtr_(nullptr) { DebugInFunction << "Constructing fvMesh from components" << endl; } Foam::fvMesh::fvMesh(fvMesh&& mesh) : polyMesh(Foam::move(mesh)), surfaceMesh(*this), surfaceInterpolation(Foam::move(mesh)), boundary_(Foam::move(mesh.boundary_)), stitcher_(Foam::move(mesh.stitcher_)), topoChanger_(Foam::move(mesh.topoChanger_)), distributor_(Foam::move(mesh.distributor_)), mover_(Foam::move(mesh.mover_)), lduPtr_(Foam::move(mesh.lduPtr_)), polyFacesBfIOPtr_(Foam::move(mesh.polyFacesBfIOPtr_)), polyFacesBfPtr_(Foam::move(mesh.polyFacesBfPtr_)), polyBFaceOffsetsPtr_(Foam::move(mesh.polyBFaceOffsetsPtr_)), polyBFaceOffsetPatchesPtr_(Foam::move(mesh.polyBFaceOffsetPatchesPtr_)), polyBFaceOffsetPatchFacesPtr_ ( Foam::move(mesh.polyBFaceOffsetPatchFacesPtr_) ), polyBFacePatchesPtr_(Foam::move(mesh.polyBFacePatchesPtr_)), polyBFacePatchFacesPtr_(Foam::move(mesh.polyBFacePatchFacesPtr_)), ownerBfPtr_(Foam::move(mesh.ownerBfPtr_)), curTimeIndex_(mesh.curTimeIndex_), VPtr_(Foam::move(mesh.VPtr_)), V0Ptr_(Foam::move(mesh.V0Ptr_)), V00Ptr_(Foam::move(mesh.V00Ptr_)), SfSlicePtr_(Foam::move(mesh.SfSlicePtr_)), SfPtr_(Foam::move(mesh.SfPtr_)), magSfSlicePtr_(Foam::move(mesh.magSfSlicePtr_)), magSfPtr_(Foam::move(mesh.magSfPtr_)), CSlicePtr_(Foam::move(mesh.CSlicePtr_)), CPtr_(Foam::move(mesh.CPtr_)), CfSlicePtr_(Foam::move(mesh.CfSlicePtr_)), CfPtr_(Foam::move(mesh.CfPtr_)), phiPtr_(Foam::move(mesh.phiPtr_)) { DebugInFunction << "Move-constructing fvMesh" << endl; } // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::fvMesh::~fvMesh() { clearOut(); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // void Foam::fvMesh::postConstruct ( const bool changers, const bool zones, const stitchType stitch ) { // Construct the stitcher stitcher_.set(fvMeshStitcher::New(*this, changers).ptr()); // Stitch or Re-stitch if necessary if (stitch != stitchType::none) { stitcher_->connect(false, stitch == stitchType::geometric, true); } // Construct changers if (changers) { topoChanger_.set(fvMeshTopoChanger::New(*this).ptr()); distributor_.set(fvMeshDistributor::New(*this).ptr()); mover_.set(fvMeshMover::New(*this).ptr()); // Check the existence of the cell volumes and read if present // and set the storage of V00 if (fileHandler().isFile(time().timePath()/"Vc0")) { V0Ptr_ = new DimensionedField ( IOobject ( "Vc0", time().name(), *this, IOobject::MUST_READ, IOobject::NO_WRITE, true ), *this, dimensions::volume ); V00(); } // Check the existence of the mesh fluxes and read if present if (fileHandler().isFile(time().timePath()/"meshPhi")) { phiPtr_ = new surfaceScalarField ( IOobject ( "meshPhi", time().name(), *this, IOobject::MUST_READ, IOobject::NO_WRITE, true ), *this, dimensions::volumetricFlux ); } } // Generate the zones after the mesh manipulators have been constructed // to support motion-specific zone generators requiring access to the mover if (zones) { zonesGenerator::New(*this); } } bool Foam::fvMesh::topoChanging() const { return topoChanger_.valid() && topoChanger_->dynamic(); } bool Foam::fvMesh::distributing() const { return distributor_.valid() && distributor_->dynamic(); } bool Foam::fvMesh::dynamic() const { return (topoChanger_.valid() && topoChanger_->dynamic()) || (mover_.valid() && mover_->dynamic()); } bool Foam::fvMesh::update() { if ( stitcher_->stitches() || topoChanger_->dynamic() || distributor_->dynamic() ) { nullOldestTimeFields(); } // Remove the oldest cell volume field if (V00Ptr_) { nullDemandDrivenData(V00Ptr_); } else { nullDemandDrivenData(V0Ptr_); } // Remove the oldest mesh flux field if (phiPtr_) { phiPtr_->nullOldestTime(); } // Set topoChanged_ false before any mesh change. Topo-changing can switch // on and off during a run. topoChanged_ = false; topoChanged_ = topoChanger_->update(); const bool distributed = distributor_->update(); return topoChanged_ || distributed; } bool Foam::fvMesh::move() { // Do not set moving_ false before any mesh motion. Once the mesh starts // moving it is considered to be moving for the rest of the run. const bool moved = mover_->update(); curTimeIndex_ = time().timeIndex(); stitcher_->connect(true, true, false); return moved; } void Foam::fvMesh::addFvPatches ( const List& p, const bool validBoundary ) { if (boundary().size()) { FatalErrorInFunction << " boundary already exists" << abort(FatalError); } // first add polyPatches addPatches(p, validBoundary); boundary_.addPatches(poly().boundary()); } void Foam::fvMesh::removeFvBoundary() { DebugInFunction << "Removing boundary patches." << endl; // Remove fvBoundaryMesh data first. boundary_.clear(); boundary_.setSize(0); polyMesh::removeBoundary(); clearOut(); } void Foam::fvMesh::swap(fvMesh& otherMesh) { // Clear the sliced fields clearFvGeom(); // Clear the current volume and other geometry factors surfaceInterpolation::clearOut(); // Clear any non-updateable addressing clearAddressing(true); polyMesh::swap(otherMesh); auto updatePatches = [] ( const polyPatchList& patches, fvBoundaryMesh& boundaryMesh ) { boundaryMesh.setSize(patches.size()); forAll(patches, patchi) { // Construct new processor patches, as the decomposition may have // changed. Leave other patches as is. if (isA(patches[patchi])) { boundaryMesh.set ( patchi, fvPatch::New ( patches[patchi], boundaryMesh ) ); } } }; updatePatches(poly().boundary(), boundary_); updatePatches(otherMesh.poly().boundary(), otherMesh.boundary_); } Foam::fvMesh::readUpdateState Foam::fvMesh::readUpdate ( const stitchType stitch ) { // Determine if this update moves forward in time. If so, searching back in // time for data files will only go back as far as the previous instance. const fileName instance0 = instance(); const bool forward = readUpdateIsForward(); // Update the polyMesh and the mesh instance const polyMesh::readUpdateState state = polyMesh::readUpdate(); DebugInFunction << "Updating the fvMesh:" << endl; if (debug) { switch (state) { case polyMesh::TOPO_PATCH_CHANGE: Info<< " Boundary and topological change" << endl; break; case polyMesh::TOPO_CHANGE: Info<< " Topological change" << endl; break; case polyMesh::POINTS_MOVED: Info<< " Point motion" << endl; break; default: Info<< " No change" << endl; break; } } const bool reStitch = stitcher_.valid() && stitcher_->stitches() && stitch != stitchType::none && ( !conformal() || time().findInstance ( dbDir()/typeName, "polyFaces", IOobject::READ_IF_PRESENT, forward ? word(instance0) : word::null ) != (forward ? instance0 : polyFacesBfIOPtr_->instance()) ); if (reStitch) { stitcher_->disconnect(false, false); } else if (state != polyMesh::UNCHANGED) { conform(); } switch (state) { case polyMesh::TOPO_PATCH_CHANGE: boundary_.readUpdate(poly().boundary()); clearOut(); break; case polyMesh::TOPO_CHANGE: clearOut(); break; case polyMesh::POINTS_MOVED: clearFvGeom(); break; default: break; } if (reStitch && stitch != stitchType::none) { stitcher_->connect(false, stitch == stitchType::geometric, true); } // Return the corresponding fvMesh read update state switch (state) { case polyMesh::UNCHANGED: return reStitch ? STITCHED : UNCHANGED; case polyMesh::POINTS_MOVED: return reStitch ? STITCHED : POINTS_MOVED; case polyMesh::TOPO_CHANGE: return TOPO_CHANGE; case polyMesh::TOPO_PATCH_CHANGE: return TOPO_PATCH_CHANGE; } return UNCHANGED; } const Foam::fvBoundaryMesh& Foam::fvMesh::boundary() const { return boundary_; } const Foam::lduAddressing& Foam::fvMesh::lduAddr() const { if (!lduPtr_) { lduPtr_ = new fvMeshLduAddressing(*this); } return *lduPtr_; } bool Foam::fvMesh::conformal() const { return !(polyFacesBfPtr_ && SfPtr_); } const Foam::surfaceLabelField::Boundary& Foam::fvMesh::polyFacesBf() const { if (!polyFacesBfPtr_) { polyFacesBfIOPtr_ = new IOobject ( "polyFaces", facesInstance(), typeName, *this, IOobject::NO_READ, IOobject::NO_WRITE, false ); polyFacesBfPtr_ = new surfaceLabelField::Boundary ( boundary(), surfaceLabelField::null(), polyFacesPatchTypes(), poly().boundary().types() ); } return *polyFacesBfPtr_; } const Foam::UCompactListList& Foam::fvMesh::polyBFacePatches() const { if (!polyBFacePatchesPtr_) { const label nPolyBFaces = nFaces() - nInternalFaces(); // Count face-poly-bFaces to get the offsets polyBFaceOffsetsPtr_ = new labelList(nPolyBFaces + 1, 0); labelList& offsets = *polyBFaceOffsetsPtr_; forAll(boundary(), patchi) { forAll(boundary()[patchi], patchFacei) { const label polyBFacei = ( polyFacesBfPtr_ ? (*polyFacesBfPtr_)[patchi][patchFacei] : boundary()[patchi].start() + patchFacei ) - nInternalFaces(); offsets[polyBFacei + 1] ++; } } for (label polyBFacei = 0; polyBFacei < nPolyBFaces; ++ polyBFacei) { offsets[polyBFacei + 1] += offsets[polyBFacei]; } // Set the poly-bFace patches and patch-faces, using the offsets as // counters polyBFaceOffsetPatchesPtr_ = new labelList(offsets.last()); polyBFaceOffsetPatchFacesPtr_ = new labelList(offsets.last()); labelUList& patches = *polyBFaceOffsetPatchesPtr_; labelUList& patchFaces = *polyBFaceOffsetPatchFacesPtr_; forAll(boundary(), patchi) { forAll(boundary()[patchi], patchFacei) { const label polyBFacei = ( polyFacesBfPtr_ ? (*polyFacesBfPtr_)[patchi][patchFacei] : boundary()[patchi].start() + patchFacei ) - nInternalFaces(); patches[offsets[polyBFacei]] = patchi; patchFaces[offsets[polyBFacei]] = patchFacei; offsets[polyBFacei] ++; } } // Restore the offsets by removing the count for ( label polyBFacei = nPolyBFaces - 1; polyBFacei >= 0; -- polyBFacei ) { offsets[polyBFacei + 1] = offsets[polyBFacei]; } offsets[0] = 0; // List-lists polyBFacePatchesPtr_ = new UCompactListList