From ad2baed5af13cd495864d80b0d628d5f162c07da Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 16 Nov 2018 10:30:02 +0100 Subject: [PATCH] ENH update use of bitSet in dynamicRefineFvMesh #1075 --- .../dynamicRefineFvMesh/dynamicRefineFvMesh.C | 473 +++++++----------- .../dynamicRefineFvMesh/dynamicRefineFvMesh.H | 28 +- 2 files changed, 206 insertions(+), 295 deletions(-) diff --git a/src/dynamicFvMesh/dynamicRefineFvMesh/dynamicRefineFvMesh.C b/src/dynamicFvMesh/dynamicRefineFvMesh/dynamicRefineFvMesh.C index 8a85e3d25d..abc72e686f 100644 --- a/src/dynamicFvMesh/dynamicRefineFvMesh/dynamicRefineFvMesh.C +++ b/src/dynamicFvMesh/dynamicRefineFvMesh/dynamicRefineFvMesh.C @@ -33,6 +33,7 @@ License #include "pointFields.H" #include "sigFpe.H" #include "cellSet.H" +#include "HashOps.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -44,32 +45,6 @@ namespace Foam // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // -Foam::label Foam::dynamicRefineFvMesh::count -( - const bitSet& l, - const unsigned int val -) -{ - label n = 0; - forAll(l, i) - { - if (l.get(i) == val) - { - n++; - } - - // debug also serves to get-around Clang compiler trying to optimsie - // out this forAll loop under O3 optimisation - if (debug & 128) - { - Info<< "n=" << n << endl; - } - } - - return n; -} - - void Foam::dynamicRefineFvMesh::calculateProtectedCells ( bitSet& unrefineableCell @@ -88,81 +63,89 @@ void Foam::dynamicRefineFvMesh::calculateProtectedCells // Get neighbouring cell level labelList neiLevel(nFaces()-nInternalFaces()); - for (label facei = nInternalFaces(); facei < nFaces(); facei++) + for (label facei = nInternalFaces(); facei < nFaces(); ++facei) { neiLevel[facei-nInternalFaces()] = cellLevel[faceOwner()[facei]]; } syncTools::swapBoundaryFaceList(*this, neiLevel); + bitSet seedFace; + while (true) { // Pick up faces on border of protected cells - boolList seedFace(nFaces(), false); + seedFace.reset(); + seedFace.resize(nFaces()); - forAll(faceNeighbour(), facei) + for (label facei = 0; facei < nInternalFaces(); ++facei) { - label own = faceOwner()[facei]; - bool ownProtected = unrefineableCell.get(own); - label nei = faceNeighbour()[facei]; - bool neiProtected = unrefineableCell.get(nei); + const label own = faceOwner()[facei]; + const label nei = faceNeighbour()[facei]; - if (ownProtected && (cellLevel[nei] > cellLevel[own])) + if + ( + // Protected owner + ( + unrefineableCell.test(own) + && (cellLevel[nei] > cellLevel[own]) + ) + || + // Protected neighbour + ( + unrefineableCell.test(nei) + && (cellLevel[own] > cellLevel[nei]) + ) + ) { - seedFace[facei] = true; - } - else if (neiProtected && (cellLevel[own] > cellLevel[nei])) - { - seedFace[facei] = true; + seedFace.set(facei); } } for (label facei = nInternalFaces(); facei < nFaces(); facei++) { - label own = faceOwner()[facei]; - bool ownProtected = unrefineableCell.get(own); + const label own = faceOwner()[facei]; + if ( - ownProtected - && (neiLevel[facei-nInternalFaces()] > cellLevel[own]) + // Protected owner + ( + unrefineableCell.test(own) + && (neiLevel[facei-nInternalFaces()] > cellLevel[own]) + ) ) { - seedFace[facei] = true; + seedFace.set(facei); } } - syncTools::syncFaceList(*this, seedFace, orEqOp()); + syncTools::syncFaceList(*this, seedFace, orEqOp()); // Extend unrefineableCell bool hasExtended = false; - for (label facei = 0; facei < nInternalFaces(); facei++) + for (label facei = 0; facei < nInternalFaces(); ++facei) { - if (seedFace[facei]) + if (seedFace.test(facei)) { - label own = faceOwner()[facei]; - if (unrefineableCell.get(own) == 0) + if (unrefineableCell.set(faceOwner()[facei])) { - unrefineableCell.set(own, 1); hasExtended = true; } - - label nei = faceNeighbour()[facei]; - if (unrefineableCell.get(nei) == 0) + if (unrefineableCell.set(faceNeighbour()[facei])) { - unrefineableCell.set(nei, 1); hasExtended = true; } } } - for (label facei = nInternalFaces(); facei < nFaces(); facei++) + for (label facei = nInternalFaces(); facei < nFaces(); ++facei) { - if (seedFace[facei]) + if (seedFace.test(facei)) { - label own = faceOwner()[facei]; - if (unrefineableCell.get(own) == 0) + const label own = faceOwner()[facei]; + + if (unrefineableCell.set(own)) { - unrefineableCell.set(own, 1); hasExtended = true; } } @@ -198,9 +181,9 @@ void Foam::dynamicRefineFvMesh::readDict() // Rework into hashtable. correctFluxes_.resize(fluxVelocities.size()); - forAll(fluxVelocities, i) + for (const auto& pr : fluxVelocities) { - correctFluxes_.insert(fluxVelocities[i][0], fluxVelocities[i][1]); + correctFluxes_.insert(pr.first(), pr.second()); } refineDict.readEntry("dumpLevel", dumpLevel_); @@ -210,6 +193,7 @@ void Foam::dynamicRefineFvMesh::readDict() void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) { dynamicFvMesh::mapFields(mpm); + // Correct the flux for modified/added faces. All the faces which only // have been renumbered will already have been handled by the mapping. { @@ -220,46 +204,42 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) // on the coarse cell that get split into four (or rather the // master face gets modified and three faces get added from the master) // Estimate number of faces created - labelHashSet masterFaces - ( - max - ( - mag(nFaces()-mpm.nOldFaces())/4, - nFaces()/100 - ) - ); + + bitSet masterFaces(nFaces()); forAll(faceMap, facei) { - label oldFacei = faceMap[facei]; + const label oldFacei = faceMap[facei]; if (oldFacei >= 0) { - label masterFacei = reverseFaceMap[oldFacei]; + const label masterFacei = reverseFaceMap[oldFacei]; if (masterFacei < 0) { FatalErrorInFunction << "Problem: should not have removed faces" << " when refining." - << nl << "face:" << facei << abort(FatalError); + << nl << "face:" << facei << endl + << abort(FatalError); } else if (masterFacei != facei) { - masterFaces.insert(masterFacei); + masterFaces.set(masterFacei); } } } + if (debug) { - Pout<< "Found " << masterFaces.size() << " split faces " << endl; + Pout<< "Found " << masterFaces.count() << " split faces " << endl; } HashTable fluxes ( lookupClass() ); - forAllIter(HashTable, fluxes, iter) + forAllIters(fluxes, iter) { if (!correctFluxes_.found(iter.key())) { @@ -282,13 +262,13 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) continue; } + surfaceScalarField& phi = *iter(); + if (UName == "NaN") { Pout<< "Setting surfaceScalarField " << iter.key() << " to NaN" << endl; - surfaceScalarField& phi = *iter(); - sigFpe::fillNan(phi.primitiveFieldRef()); continue; @@ -301,7 +281,6 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) << endl; } - surfaceScalarField& phi = *iter(); const surfaceScalarField phiU ( fvc::interpolate @@ -312,9 +291,9 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) ); // Recalculate new internal faces. - for (label facei = 0; facei < nInternalFaces(); facei++) + for (label facei = 0; facei < nInternalFaces(); ++facei) { - label oldFacei = faceMap[facei]; + const label oldFacei = faceMap[facei]; if (oldFacei == -1) { @@ -329,8 +308,8 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) } // Recalculate new boundary faces. - surfaceScalarField::Boundary& phiBf = - phi.boundaryFieldRef(); + surfaceScalarField::Boundary& phiBf = phi.boundaryFieldRef(); + forAll(phiBf, patchi) { fvsPatchScalarField& patchPhi = phiBf[patchi]; @@ -341,7 +320,7 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) forAll(patchPhi, i) { - label oldFacei = faceMap[facei]; + const label oldFacei = faceMap[facei]; if (oldFacei == -1) { @@ -354,23 +333,21 @@ void Foam::dynamicRefineFvMesh::mapFields(const mapPolyMesh& mpm) patchPhi[i] = patchPhiU[i]; } - facei++; + ++facei; } } // Update master faces - forAllConstIter(labelHashSet, masterFaces, iter) + for (const label facei : masterFaces) { - label facei = iter.key(); - if (isInternalFace(facei)) { phi[facei] = phiU[facei]; } else { - label patchi = boundaryMesh().whichPatch(facei); - label i = facei - boundaryMesh()[patchi].start(); + const label patchi = boundaryMesh().whichPatch(facei); + const label i = facei - boundaryMesh()[patchi].start(); const fvsPatchScalarField& patchPhiU = phiU.boundaryField()[patchi]; @@ -424,9 +401,9 @@ Foam::dynamicRefineFvMesh::refine if (debug) { // Check map. - for (label facei = 0; facei < nInternalFaces(); facei++) + for (label facei = 0; facei < nInternalFaces(); ++facei) { - label oldFacei = map().faceMap()[facei]; + const label oldFacei = map().faceMap()[facei]; if (oldFacei >= nInternalFaces()) { @@ -474,14 +451,17 @@ Foam::dynamicRefineFvMesh::refine forAll(newProtectedCell, celli) { - label oldCelli = map().cellMap()[celli]; - newProtectedCell.set(celli, protectedCell_.get(oldCelli)); + const label oldCelli = map().cellMap()[celli]; + if (protectedCell_.test(oldCelli)) + { + newProtectedCell.set(celli); + } } protectedCell_.transfer(newProtectedCell); } // Debug: Check refinement levels (across faces only) - meshCutter_.checkRefinementLevels(-1, labelList(0)); + meshCutter_.checkRefinementLevels(-1, labelList()); return map; } @@ -508,21 +488,19 @@ Foam::dynamicRefineFvMesh::unrefine Map