/*---------------------------------------------------------------------------*\ ========= | \\ / 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 "volFields.H" #include "syncTools.H" #include "Time.H" #include "refinementHistory.H" #include "refinementSurfaces.H" #include "refinementFeatures.H" #include "decompositionMethod.H" #include "regionSplit.H" #include "fvMeshDistribute.H" #include "indirectPrimitivePatch.H" #include "polyTopoChange.H" #include "removeCells.H" #include "polyDistributionMap.H" #include "localPointRegion.H" #include "pointMesh.H" #include "pointFields.H" #include "slipPointPatchFields.H" #include "fixedValuePointPatchFields.H" #include "calculatedPointPatchFields.H" #include "cyclicSlipPointPatchFields.H" #include "processorPointPatch.H" #include "globalIndex.H" #include "meshTools.H" #include "OFstream.H" #include "geometric.H" #include "randomGenerator.H" #include "searchableSurfaceList.H" #include "meshSearch.H" #include "fvMeshTools.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { defineTypeNameAndDebug(meshRefinement, 0); } const Foam::NamedEnum Foam::meshRefinement::IOdebugTypeNames { "mesh", "intersections", "featureSeeds", "attraction", "layerInfo" }; const Foam::NamedEnum Foam::meshRefinement::IOoutputTypeNames { "layerInfo" }; const Foam::NamedEnum Foam::meshRefinement::IOwriteTypeNames { "mesh", "noRefinement", "scalarLevels", "layerSets", "layerFields" }; Foam::meshRefinement::writeType Foam::meshRefinement::writeLevel_; Foam::meshRefinement::outputType Foam::meshRefinement::outputLevel_; // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // void Foam::meshRefinement::calcNeighbourData ( labelList& neiLevel, pointField& neiCc ) const { const labelList& cellLevel = meshCutter_.cellLevel(); const pointField& cellCentres = mesh_.cellCentres(); const label nBoundaryFaces = mesh_.nFaces() - mesh_.nInternalFaces(); if (neiLevel.size() != nBoundaryFaces || neiCc.size() != nBoundaryFaces) { FatalErrorInFunction << nBoundaryFaces << " neiLevel:" << neiLevel.size() << abort(FatalError); } const polyBoundaryMesh& patches = mesh_.poly().boundary(); labelHashSet addedPatchIDSet(meshedPatches()); forAll(patches, patchi) { const polyPatch& pp = patches[patchi]; const labelUList& faceCells = pp.faceCells(); const vectorField::subField faceCentres = pp.faceCentres(); const vectorField::subField faceAreas = pp.faceAreas(); label bFacei = pp.start() - mesh_.nInternalFaces(); if (pp.coupled()) { forAll(faceCells, i) { neiLevel[bFacei] = cellLevel[faceCells[i]]; neiCc[bFacei] = cellCentres[faceCells[i]]; bFacei++; } } else if (addedPatchIDSet.found(patchi)) { // Face was introduced from cell-cell intersection. Try to // reconstruct other side cell(centre). Three possibilities: // - cells same size. // - preserved cell smaller. Not handled. // - preserved cell larger. forAll(faceCells, i) { // Extrapolate the face centre. const vector fn = faceAreas[i]/(mag(faceAreas[i]) + vSmall); const label own = faceCells[i]; const label ownLevel = cellLevel[own]; const label faceLevel = meshCutter_.faceLevel(pp.start() + i); // Normal distance from face centre to cell centre scalar d = ((faceCentres[i] - cellCentres[own]) & fn); if (faceLevel > ownLevel) { // Other cell more refined. Adjust normal distance d *= 0.5; } neiLevel[bFacei] = faceLevel; // Calculate other cell centre by extrapolation neiCc[bFacei] = faceCentres[i] + d*fn; bFacei++; } } else { forAll(faceCells, i) { neiLevel[bFacei] = cellLevel[faceCells[i]]; neiCc[bFacei] = faceCentres[i]; bFacei++; } } } // Swap coupled boundaries. Apply separation to cc since is coordinate. syncTools::swapBoundaryFacePositions(mesh_, neiCc); syncTools::swapBoundaryFaceList(mesh_, neiLevel); } void Foam::meshRefinement::updateIntersections(const labelList& changedFaces) { const pointField& cellCentres = mesh_.cellCentres(); // Stats on edges to test. Count proc faces only once. PackedBoolList isMasterFace(syncTools::getMasterFaces(mesh_)); { label nMasterFaces = 0; forAll(isMasterFace, facei) { if (isMasterFace.get(facei) == 1) { nMasterFaces++; } } reduce(nMasterFaces, sumOp()); label nChangedFaces = 0; forAll(changedFaces, i) { if (isMasterFace.get(changedFaces[i]) == 1) { nChangedFaces++; } } reduce(nChangedFaces, sumOp()); Info<< "Edge intersection testing:" << nl << " Number of edges : " << nMasterFaces << nl << " Number of edges to retest : " << nChangedFaces << endl; } // Get boundary face centre and level. Coupled aware. labelList neiLevel(mesh_.nFaces() - mesh_.nInternalFaces()); pointField neiCc(mesh_.nFaces() - mesh_.nInternalFaces()); calcNeighbourData(neiLevel, neiCc); // Collect segments we want to test for pointField start(changedFaces.size()); pointField end(changedFaces.size()); forAll(changedFaces, i) { const label facei = changedFaces[i]; const label own = mesh_.faceOwner()[facei]; start[i] = cellCentres[own]; if (mesh_.isInternalFace(facei)) { end[i] = cellCentres[mesh_.faceNeighbour()[facei]]; } else { end[i] = neiCc[facei-mesh_.nInternalFaces()]; } } // Extend segments a bit { const vectorField smallVec(rootSmall*(end-start)); start -= smallVec; end += smallVec; } // Do tests in one go labelList surfaceHit; { labelList surfaceLevel; surfaces_.findHigherIntersection ( start, end, labelList(start.size(), -1), // accept any intersection surfaceHit, surfaceLevel ); } // Keep just surface hit forAll(surfaceHit, i) { surfaceIndex_[changedFaces[i]] = surfaceHit[i]; } // Make sure both sides have same information. This should be // case in general since same vectors but just to make sure. syncTools::syncFaceList(mesh_, surfaceIndex_, maxEqOp()); const label nHits = countHits(); const label nTotHits = returnReduce(nHits, sumOp()); Info<< " Number of intersected edges : " << nTotHits << endl; // Set files to same time as mesh setInstance(mesh_.facesInstance()); } void Foam::meshRefinement::testSyncPointList ( const string& msg, const polyMesh& mesh, const List& fld ) { if (fld.size() != mesh.nPoints()) { FatalErrorInFunction << msg << endl << "fld size:" << fld.size() << " mesh points:" << mesh.nPoints() << abort(FatalError); } Pout<< "Checking field " << msg << endl; scalarField minFld(fld); syncTools::syncPointList ( mesh, minFld, minEqOp(), great ); scalarField maxFld(fld); syncTools::syncPointList ( mesh, maxFld, maxEqOp(), -great ); forAll(minFld, pointi) { const scalar& minVal = minFld[pointi]; const scalar& maxVal = maxFld[pointi]; if (mag(minVal-maxVal) > small) { Pout<< msg << " at:" << mesh.points()[pointi] << nl << " minFld:" << minVal << nl << " maxFld:" << maxVal << nl << endl; } } } void Foam::meshRefinement::testSyncPointList ( const string& msg, const polyMesh& mesh, const List& fld ) { if (fld.size() != mesh.nPoints()) { FatalErrorInFunction << msg << endl << "fld size:" << fld.size() << " mesh points:" << mesh.nPoints() << abort(FatalError); } Pout<< "Checking field " << msg << endl; pointField minFld(fld); syncTools::syncPointList ( mesh, minFld, minMagSqrEqOp(), point(great, great, great) ); pointField maxFld(fld); syncTools::syncPointList ( mesh, maxFld, maxMagSqrEqOp(), vector::zero ); forAll(minFld, pointi) { const point& minVal = minFld[pointi]; const point& maxVal = maxFld[pointi]; if (mag(minVal-maxVal) > small) { Pout<< msg << " at:" << mesh.points()[pointi] << nl << " minFld:" << minVal << nl << " maxFld:" << maxVal << nl << endl; } } } void Foam::meshRefinement::checkData() { Pout<< "meshRefinement::checkData() : Checking refinement structure." << endl; meshCutter_.checkMesh(); Pout<< "meshRefinement::checkData() : Checking refinement levels." << endl; meshCutter_.checkRefinementLevels(1, labelList(0)); label nBnd = mesh_.nFaces() - mesh_.nInternalFaces(); Pout<< "meshRefinement::checkData() : Checking synchronisation." << endl; // Check face centres { // Boundary face centres pointField::subList boundaryFc ( mesh_.faceCentres(), nBnd, mesh_.nInternalFaces() ); // Get neighbouring face centres pointField neiBoundaryFc(boundaryFc); syncTools::syncBoundaryFacePositions ( mesh_, neiBoundaryFc, eqOp() ); // Compare testSyncBoundaryFaceList ( mergeDistance_, "testing faceCentres : ", boundaryFc, neiBoundaryFc ); } // Check meshRefinement { // Get boundary face centre and level. Coupled aware. labelList neiLevel(nBnd); pointField neiCc(nBnd); calcNeighbourData(neiLevel, neiCc); // Collect segments we want to test for pointField start(mesh_.nFaces()); pointField end(mesh_.nFaces()); forAll(start, facei) { start[facei] = mesh_.cellCentres()[mesh_.faceOwner()[facei]]; if (mesh_.isInternalFace(facei)) { end[facei] = mesh_.cellCentres()[mesh_.faceNeighbour()[facei]]; } else { end[facei] = neiCc[facei-mesh_.nInternalFaces()]; } } // Extend segments a bit { const vectorField smallVec(rootSmall*(end-start)); start -= smallVec; end += smallVec; } // Do tests in one go labelList surfaceHit; { labelList surfaceLevel; surfaces_.findHigherIntersection ( start, end, labelList(start.size(), -1), // accept any intersection surfaceHit, surfaceLevel ); } // Get the coupled hit labelList neiHit ( SubList