From 6217380e9155da7218187a0578c2829dc8a002cb Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Fri, 19 Apr 2024 11:17:43 +0200 Subject: [PATCH] ENH: improve code consistency for {cell,face,point}Mapper (#3147) - robuster and more reliable determination of inserted objects (previous code could produce false positives). Now also determine the number of inserted objects within the constructor (instead of simply storing a bool). This allows reuse in the address calculations to reduce overheads there. BUG: dodgy short-circuit logic for insertedObjectLabels() - as a quick short-circuit it previously created as demand-driven pointer with a zero elements. However, this meant that if this code was called first (before any other addressing), subsequent calls to the addressing would fail. BUG: bad logic and lookup for faAreaMapper (#3147) - was using labelHashSet but returning a bool and set using the addressing using the unfiltered objects! This is a latent bug since interpolated/inserted faces not currently supported anyhow. ENH: use std::unique_ptr for demand-driven data --- .../pointMeshMapper/pointBoundaryMeshMapper.H | 4 +- .../pointMesh/pointMeshMapper/pointMapper.C | 296 +++++++----- .../pointMesh/pointMeshMapper/pointMapper.H | 58 ++- .../pointMeshMapper/pointMeshMapper.H | 17 +- .../pointMeshMapper/pointPatchMapper.C | 54 ++- .../pointMeshMapper/pointPatchMapper.H | 21 +- .../mapPolyMesh/cellMapper/cellMapper.C | 431 +++++++++--------- .../mapPolyMesh/cellMapper/cellMapper.H | 55 ++- .../mapPolyMesh/faceMapper/faceMapper.C | 347 +++++++------- .../mapPolyMesh/faceMapper/faceMapper.H | 54 +-- .../meshes/polyMesh/mapPolyMesh/mapPolyMesh.C | 2 +- .../meshes/polyMesh/mapPolyMesh/mapPolyMesh.H | 94 ++-- .../mapPolyMesh/objectMap/objectMap.H | 49 +- .../mapPolyMesh/objectMap/objectMapI.H | 44 +- .../faMesh/faMeshMapper/faAreaMapper.C | 244 ++++------ .../faMesh/faMeshMapper/faAreaMapper.H | 34 +- .../faMeshMapper/faBoundaryMeshMapper.H | 4 +- .../faMesh/faMeshMapper/faEdgeMapper.C | 25 +- .../faMesh/faMeshMapper/faEdgeMapper.H | 47 +- .../faMesh/faMeshMapper/faMeshMapper.C | 10 +- .../faMesh/faMeshMapper/faMeshMapper.H | 6 +- .../faMesh/faMeshMapper/faPatchMapper.C | 23 +- .../faMesh/faMeshMapper/faPatchMapper.H | 21 +- .../fvMeshMapper/fvBoundaryMeshMapper.H | 4 +- .../fvMesh/fvMeshMapper/fvMeshMapper.H | 8 +- .../fvMesh/fvMeshMapper/fvPatchMapper.C | 93 ++-- .../fvMesh/fvMeshMapper/fvPatchMapper.H | 21 +- .../fvMesh/fvMeshMapper/fvSurfaceMapper.C | 116 ++--- .../fvMesh/fvMeshMapper/fvSurfaceMapper.H | 26 +- 29 files changed, 1059 insertions(+), 1149 deletions(-) diff --git a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointBoundaryMeshMapper.H b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointBoundaryMeshMapper.H index 6c155b54016..f45bc5942a5 100644 --- a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointBoundaryMeshMapper.H +++ b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointBoundaryMeshMapper.H @@ -31,8 +31,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef pointBoundaryMeshMapper_H -#define pointBoundaryMeshMapper_H +#ifndef Foam_pointBoundaryMeshMapper_H +#define Foam_pointBoundaryMeshMapper_H #include "PtrList.H" #include "pointPatchMapper.H" diff --git a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.C b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.C index 3fba1b2c166..2daa81872c5 100644 --- a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.C +++ b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "pointMapper.H" -#include "demandDrivenData.H" #include "pointMesh.H" #include "mapPolyMesh.H" @@ -37,9 +37,9 @@ void Foam::pointMapper::calcAddressing() const if ( directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ - || insertedPointLabelsPtr_ + || insertedObjectsPtr_ ) { FatalErrorInFunction @@ -51,160 +51,226 @@ void Foam::pointMapper::calcAddressing() const { // Direct addressing, no weights - directAddrPtr_ = new labelList(mpm_.pointMap()); - labelList& directAddr = *directAddrPtr_; + directAddrPtr_ = std::make_unique<labelList> + ( + // No retired points, so pointMap().size() == mapperLen_ anyhow + labelList::subList(mpm_.pointMap(), mapperLen_) + ); + auto& directAddr = *directAddrPtr_; - // Not necessary to resize the list as there are no retired points - // directAddr.setSize(pMesh_.size()); + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; - insertedPointLabelsPtr_ = new labelList(pMesh_.size()); - labelList& insertedPoints = *insertedPointLabelsPtr_; - - label nInsertedPoints = 0; - - forAll(directAddr, pointi) + // The nInsertedObjects_ already counted in the constructor + if (nInsertedObjects_) { - if (directAddr[pointi] < 0) + inserted.resize(nInsertedObjects_); + + label nInserted = 0; + forAll(directAddr, i) { - // Found inserted point - directAddr[pointi] = 0; - insertedPoints[nInsertedPoints] = pointi; - nInsertedPoints++; + if (directAddr[i] < 0) + { + // Found inserted + directAddr[i] = 0; + inserted[nInserted] = i; + ++nInserted; + + // TBD: check (nInsertedObjects_ < nInserted)? + #ifdef FULLDEBUG + if (nInsertedObjects_ < nInserted) + { + FatalErrorInFunction + << "Unexpected insert of more than " + << nInsertedObjects_ << " items\n" + << abort(FatalError); + } + #endif + } } + // TBD: check (nInserted < nInsertedObjects_)? + #ifdef FULLDEBUG + if (nInserted < nInsertedObjects_) + { + WarningInFunction + << "Found " << nInserted << " instead of " + << nInsertedObjects_ << " items to insert\n"; + } + #endif + // The resize should be unnecessary + inserted.resize(nInserted); } - - insertedPoints.setSize(nInsertedPoints); } else { // Interpolative addressing - interpolationAddrPtr_ = new labelListList(pMesh_.size()); - labelListList& addr = *interpolationAddrPtr_; + interpAddrPtr_ = std::make_unique<labelListList>(mapperLen_); + auto& addr = *interpAddrPtr_; - weightsPtr_ = new scalarListList(pMesh_.size()); - scalarListList& w = *weightsPtr_; + weightsPtr_ = std::make_unique<scalarListList>(mapperLen_); + auto& wght = *weightsPtr_; - // Points created from other points (i.e. points merged into it). - const List<objectMap>& cfc = mpm_.pointsFromPointsMap(); - forAll(cfc, cfcI) + // Set the addressing and uniform weight + const auto setAddrWeights = [&] + ( + const List<objectMap>& maps, + const char * const nameOfMap + ) { - // Get addressing - const labelList& mo = cfc[cfcI].masterObjects(); - - label pointi = cfc[cfcI].index(); - - if (addr[pointi].size()) + for (const objectMap& map : maps) { - FatalErrorInFunction - << "Master point " << pointi - << " mapped from points " << mo - << " already destination of mapping." << abort(FatalError); + // Get index, addressing + const label pointi = map.index(); + const labelList& mo = map.masterObjects(); + if (mo.empty()) continue; // safety + + if (addr[pointi].size()) + { + FatalErrorInFunction + << "Master point " << pointi + << " already mapped, cannot apply " + << nameOfMap + << flatOutput(mo) << abort(FatalError); + } + + // Map from masters, uniform weights + addr[pointi] = mo; + wght[pointi] = scalarList(mo.size(), 1.0/mo.size()); } + }; - // Map from masters, uniform weights - addr[pointi] = mo; - w[pointi] = scalarList(mo.size(), 1.0/mo.size()); - } + // Points created from other points (i.e. points merged into it). + + setAddrWeights(mpm_.pointsFromPointsMap(), "point points"); - // Do mapped points. Note that can already be set from pointsFromPoints - // so check if addressing size still zero. - const labelList& cm = mpm_.pointMap(); + // Do mapped points. + // - may already have been set, so check if addressing still empty(). - forAll(cm, pointi) { - if (cm[pointi] > -1 && addr[pointi].empty()) + const labelList& map = mpm_.pointMap(); + + for (label pointi = 0; pointi < mapperLen_; ++pointi) { - // Mapped from a single point - addr[pointi] = labelList(1, cm[pointi]); - w[pointi] = scalarList(1, scalar(1)); + const label mappedi = map[pointi]; + + if (mappedi >= 0 && addr[pointi].empty()) + { + // Mapped from a single point + addr[pointi].resize(1, mappedi); + wght[pointi].resize(1, 1.0); + } } } // Grab inserted points (for them the size of addressing is still zero) - insertedPointLabelsPtr_ = new labelList(pMesh_.size()); - labelList& insertedPoints = *insertedPointLabelsPtr_; - - label nInsertedPoints = 0; + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; - forAll(addr, pointi) + // The nInsertedObjects_ already counted in the constructor + if (nInsertedObjects_) { - if (addr[pointi].empty()) - { - // Mapped from a dummy point. Take point 0 with weight 1. - addr[pointi] = labelList(1, Zero); - w[pointi] = scalarList(1, scalar(1)); + inserted.resize(nInsertedObjects_); - insertedPoints[nInsertedPoints] = pointi; - nInsertedPoints++; + label nInserted = 0; + forAll(addr, i) + { + if (addr[i].empty()) + { + // Mapped from dummy point 0 + addr[i].resize(1, 0); + wght[i].resize(1, 1.0); + + inserted[nInserted] = i; + ++nInserted; + + // TBD: check (nInsertedObjects_ < nInserted)? + #ifdef FULLDEBUG + if (nInsertedObjects_ < nInserted) + { + FatalErrorInFunction + << "Unexpected insert of more than " + << nInsertedObjects_ << " items\n" + << abort(FatalError); + } + #endif + } } + // TBD: check (nInserted < nInsertedObjects_)? + #ifdef FULLDEBUG + if (nInserted < nInsertedObjects_) + { + WarningInFunction + << "Found " << nInserted << " instead of " + << nInsertedObjects_ << " items to insert\n"; + } + #endif + // The resize should be unnecessary + inserted.resize(nInserted); } - - insertedPoints.setSize(nInsertedPoints); } } -void Foam::pointMapper::clearOut() -{ - deleteDemandDrivenData(directAddrPtr_); - deleteDemandDrivenData(interpolationAddrPtr_); - deleteDemandDrivenData(weightsPtr_); - deleteDemandDrivenData(insertedPointLabelsPtr_); -} +// void Foam::pointMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// insertedObjectsPtr_.reset(nullptr); +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::pointMapper::pointMapper(const pointMesh& pMesh, const mapPolyMesh& mpm) : - pMesh_(pMesh), mpm_(mpm), - insertedPoints_(true), - direct_(false), - directAddrPtr_(nullptr), - interpolationAddrPtr_(nullptr), - weightsPtr_(nullptr), - insertedPointLabelsPtr_(nullptr) + mapperLen_(pMesh.size()), + nInsertedObjects_(0), + direct_ + ( + // Mapping without interpolation? + mpm.pointsFromPointsMap().empty() + ) { - // Check for possibility of direct mapping - if (mpm_.pointsFromPointsMap().empty()) + const auto& directMap = mpm_.pointMap(); + + if (!mapperLen_) { + // Empty mesh direct_ = true; + nInsertedObjects_ = 0; } - else - { - direct_ = false; - } - - // Check for inserted points - if (direct_ && (mpm_.pointMap().empty() || min(mpm_.pointMap()) > -1)) + else if (direct_) { - insertedPoints_ = false; + // Number of inserted points (-ve values) + nInsertedObjects_ = std::count_if + ( + directMap.cbegin(), + directMap.cbegin(mapperLen_), + [](label i) { return (i < 0); } + ); } else { - //Check if there are inserted points with no owner + // Check if there are inserted points with no owner + // (check all lists) - // Make a copy of the point map, add the entries for points from points - // and check for left-overs - labelList cm(pMesh_.size(), -1); + bitSet unmapped(mapperLen_, true); - const List<objectMap>& cfc = mpm_.pointsFromPointsMap(); + unmapped.unset(directMap); // direct mapped - forAll(cfc, cfcI) + for (const objectMap& map : mpm_.pointsFromPointsMap()) { - cm[cfc[cfcI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - if (min(cm) < 0) - { - insertedPoints_ = true; - } + nInsertedObjects_ = label(unmapped.count()); } } @@ -212,15 +278,14 @@ Foam::pointMapper::pointMapper(const pointMesh& pMesh, const mapPolyMesh& mpm) // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pointMapper::~pointMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::label Foam::pointMapper::size() const { + // OR: return mapperLen_; return mpm_.pointMap().size(); } @@ -266,12 +331,12 @@ const Foam::labelListList& Foam::pointMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } @@ -295,30 +360,19 @@ const Foam::scalarListList& Foam::pointMapper::weights() const const Foam::labelList& Foam::pointMapper::insertedObjectLabels() const { - if (!insertedPointLabelsPtr_) + if (!insertedObjectsPtr_) { - if (!insertedObjects()) + if (!nInsertedObjects_) { - // There are no inserted points - insertedPointLabelsPtr_ = new labelList(0); - } - else - { - calcAddressing(); + // No inserted objects will be created + return labelList::null(); } + + calcAddressing(); } - return *insertedPointLabelsPtr_; + return *insertedObjectsPtr_; } -// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - - // ************************************************************************* // diff --git a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.H b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.H index 1e33711871a..7783e6b3616 100644 --- a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.H +++ b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMapper.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,8 +37,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef pointMapper_H -#define pointMapper_H +#ifndef Foam_pointMapper_H +#define Foam_pointMapper_H #include "morphFieldMapper.H" @@ -46,10 +47,9 @@ SourceFiles namespace Foam { -// Forward declaration of classes -class pointMesh; +// Forward Declarations class mapPolyMesh; -class polyMesh; +class pointMesh; /*---------------------------------------------------------------------------*\ Class pointMapper Declaration @@ -59,38 +59,45 @@ class pointMapper : public morphFieldMapper { - // Private data - - //- Reference to pointMesh - const pointMesh& pMesh_; + // Private Data //- Reference to mapPolyMesh const mapPolyMesh& mpm_; - //- Are there any inserted (unmapped) points - bool insertedPoints_; + //- The size of the mapper = pointMesh::size() + const label mapperLen_; + + //- Number of inserted (unmapped) points + label nInsertedObjects_; //- Is the mapping direct bool direct_; - // Demand-driven private data + // Demand-Driven Data //- Direct addressing (only one for of addressing is used) - mutable labelList* directAddrPtr_; + mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one for of addressing is used) - mutable labelListList* interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights - mutable scalarListList* weightsPtr_; + mutable std::unique_ptr<scalarListList> weightsPtr_; //- Inserted points - mutable labelList* insertedPointLabelsPtr_; + mutable std::unique_ptr<labelList> insertedObjectsPtr_; // Private Member Functions + //- Calculate addressing for mapping with inserted points + void calcAddressing() const; + +public: + + // Generated Methods + //- No copy construct pointMapper(const pointMapper&) = delete; @@ -98,28 +105,20 @@ class pointMapper void operator=(const pointMapper&) = delete; - //- Calculate addressing for mapping with inserted points - void calcAddressing() const; - - //- Clear out local storage - void clearOut(); - - -public: - // Constructors //- Construct from mapPolyMesh pointMapper(const pointMesh&, const mapPolyMesh& mpm); + //- Destructor virtual ~pointMapper(); // Member Functions - //- Return size + //- The mapper size virtual label size() const; //- Return size before mapping @@ -131,8 +130,7 @@ public: return direct_; } - //- Are there unmapped values? I.e. do all size() elements get - // get value + //- Are there unmapped values? i.e. do all size() elements get value virtual bool hasUnmapped() const { return insertedObjects(); @@ -148,9 +146,9 @@ public: virtual const scalarListList& weights() const; //- Are there any inserted points - bool insertedObjects() const + bool insertedObjects() const noexcept { - return insertedPoints_; + return bool(nInsertedObjects_); } //- Return list of inserted points diff --git a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMeshMapper.H b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMeshMapper.H index 54e18c664ea..f6cb2592620 100644 --- a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMeshMapper.H +++ b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointMeshMapper.H @@ -35,8 +35,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef pointMeshMapper_H -#define pointMeshMapper_H +#ifndef Foam_pointMeshMapper_H +#define Foam_pointMeshMapper_H #include "pointMapper.H" #include "pointBoundaryMeshMapper.H" @@ -46,7 +46,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class pointMesh; class mapPolyMesh; @@ -56,7 +56,7 @@ class mapPolyMesh; class pointMeshMapper { - // Private data + // Private Data //- Reference to mesh const pointMesh& mesh_; @@ -92,28 +92,27 @@ public: // Member Functions - //- Return reference to mesh fields belong to - const pointMesh& mesh() const + const pointMesh& mesh() const noexcept { return mesh_; } //- Return reference to objectRegistry storing fields. Can be // removed once fields stored on pointMesh. - const objectRegistry& thisDb() const + const objectRegistry& thisDb() const noexcept { return mesh_(); } //- Return point mapper - const morphFieldMapper& pointMap() const + const morphFieldMapper& pointMap() const noexcept { return pointMap_; } //- Return boundary mapper - const pointBoundaryMeshMapper& boundaryMap() const + const pointBoundaryMeshMapper& boundaryMap() const noexcept { return boundaryMap_; } diff --git a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.C b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.C index e9a18985c0e..2e6a2d7c4ff 100644 --- a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.C +++ b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,7 +30,6 @@ License #include "pointPatch.H" #include "mapPolyMesh.H" #include "faceMapper.H" -#include "demandDrivenData.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -38,7 +38,7 @@ void Foam::pointPatchMapper::calcAddressing() const if ( directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ ) { @@ -52,8 +52,11 @@ void Foam::pointPatchMapper::calcAddressing() const if (direct()) { // Direct mapping. - directAddrPtr_ = new labelList(mpm_.patchPointMap()[patch_.index()]); - labelList& addr = *directAddrPtr_; + directAddrPtr_ = std::make_unique<labelList> + ( + mpm_.patchPointMap()[patch_.index()] + ); + auto& addr = *directAddrPtr_; forAll(addr, i) { @@ -73,11 +76,11 @@ void Foam::pointPatchMapper::calcAddressing() const // patch points. Problem is we don't know what points were in the patch // for points that were merged. - interpolationAddrPtr_ = new labelListList(size()); - labelListList& addr = *interpolationAddrPtr_; + interpAddrPtr_ = std::make_unique<labelListList>(size()); + auto& addr = *interpAddrPtr_; - weightsPtr_ = new scalarListList(addr.size()); - scalarListList& w = *weightsPtr_; + weightsPtr_ = std::make_unique<scalarListList>(addr.size()); + auto& wght = *weightsPtr_; const labelList& ppm = mpm_.patchPointMap()[patch_.index()]; @@ -85,15 +88,15 @@ void Foam::pointPatchMapper::calcAddressing() const { if (ppm[i] >= 0) { - addr[i] = labelList(1, ppm[i]); - w[i] = scalarList(1, scalar(1)); + addr[i].resize(1, ppm[i]); + wght[i].resize(1, 1.0); } else { // Inserted point. ///// Map from point0 (arbitrary choice) - //addr[i] = labelList(1, Zero); - //w[i] = scalarList(1, scalar(1)); + //addr[i].resize(1, 0); + //wght[i].resize(1, 1.0); hasUnmapped_ = true; } } @@ -101,13 +104,13 @@ void Foam::pointPatchMapper::calcAddressing() const } -void Foam::pointPatchMapper::clearOut() -{ - deleteDemandDrivenData(directAddrPtr_); - deleteDemandDrivenData(interpolationAddrPtr_); - deleteDemandDrivenData(weightsPtr_); - hasUnmapped_ = false; -} +// void Foam::pointPatchMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// hasUnmapped_ = false; +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -129,19 +132,14 @@ Foam::pointPatchMapper::pointPatchMapper ? mpm_.oldPatchNMeshPoints()[patch_.index()] : 0 ), - hasUnmapped_(false), - directAddrPtr_(nullptr), - interpolationAddrPtr_(nullptr), - weightsPtr_(nullptr) + hasUnmapped_(false) {} // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pointPatchMapper::~pointPatchMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -173,12 +171,12 @@ const Foam::labelListList& Foam::pointPatchMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } diff --git a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.H b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.H index c79491970f2..e9570dbad3b 100644 --- a/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.H +++ b/src/OpenFOAM/meshes/pointMesh/pointMeshMapper/pointPatchMapper.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,8 +35,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef pointPatchMapper_H -#define pointPatchMapper_H +#ifndef Foam_pointPatchMapper_H +#define Foam_pointPatchMapper_H #include "pointMapper.H" #include "pointPatchFieldMapper.H" @@ -47,7 +48,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class pointPatch; class mapPolyMesh; @@ -59,7 +60,7 @@ class pointPatchMapper : public pointPatchFieldMapper { - // Private data + // Private Data //- Reference to patch const pointPatch& patch_; @@ -74,18 +75,18 @@ class pointPatchMapper const label sizeBeforeMapping_; - // Demand-driven private data + // Demand-driven Data mutable bool hasUnmapped_; //- Direct addressing (only one for of addressing is used) - mutable labelList* directAddrPtr_; + mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one for of addressing is used) - mutable labelListList* interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights - mutable scalarListList* weightsPtr_; + mutable std::unique_ptr<scalarListList> weightsPtr_; // Private Member Functions @@ -100,9 +101,6 @@ class pointPatchMapper //- Calculate addressing for mapping with inserted cells void calcAddressing() const; - //- Clear out local storage - void clearOut(); - public: @@ -156,7 +154,6 @@ public: //- Return interpolaion weights virtual const scalarListList& weights() const; - }; diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.C index fd277b511df..4ac6c44ad10 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "cellMapper.H" -#include "demandDrivenData.H" #include "polyMesh.H" #include "mapPolyMesh.H" @@ -37,9 +37,9 @@ void Foam::cellMapper::calcAddressing() const if ( directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ - || insertedCellLabelsPtr_ + || insertedObjectsPtr_ ) { FatalErrorInFunction @@ -51,129 +51,105 @@ void Foam::cellMapper::calcAddressing() const { // Direct addressing, no weights - directAddrPtr_ = new labelList(mpm_.cellMap()); - labelList& directAddr = *directAddrPtr_; + directAddrPtr_ = std::make_unique<labelList> + ( + // No retired cells, so cellMap().size() == mapperLen_ anyhow + labelList::subList(mpm_.cellMap(), mapperLen_) + ); + auto& directAddr = *directAddrPtr_; - // Not necessary to resize the list as there are no retired cells - // directAddr.setSize(mesh_.nCells()); + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; - insertedCellLabelsPtr_ = new labelList(mesh_.nCells()); - labelList& insertedCells = *insertedCellLabelsPtr_; - - label nInsertedCells = 0; - - forAll(directAddr, celli) + // The nInsertedObjects_ already counted in the constructor + if (nInsertedObjects_) { - if (directAddr[celli] < 0) + inserted.resize(nInsertedObjects_); + + label nInserted = 0; + forAll(directAddr, i) + { + if (directAddr[i] < 0) + { + // Found inserted + directAddr[i] = 0; + inserted[nInserted] = i; + ++nInserted; + + // TBD: check (nInsertedObjects_ < nInserted)? + #ifdef FULLDEBUG + if (nInsertedObjects_ < nInserted) + { + FatalErrorInFunction + << "Unexpected insert of more than " + << nInsertedObjects_ << " items\n" + << abort(FatalError); + } + #endif + } + } + // TBD: check (nInserted < nInsertedObjects_)? + #ifdef FULLDEBUG + if (nInserted < nInsertedObjects_) { - // Found inserted cell - directAddr[celli] = 0; - insertedCells[nInsertedCells] = celli; - nInsertedCells++; + WarningInFunction + << "Found " << nInserted << " instead of " + << nInsertedObjects_ << " items to insert\n"; } + #endif + // The resize should be unnecessary + inserted.resize(nInserted); } - - insertedCells.setSize(nInsertedCells); } else { // Interpolative addressing - interpolationAddrPtr_ = new labelListList(mesh_.nCells()); - labelListList& addr = *interpolationAddrPtr_; + interpAddrPtr_ = std::make_unique<labelListList>(mapperLen_); + auto& addr = *interpAddrPtr_; - weightsPtr_ = new scalarListList(mesh_.nCells()); - scalarListList& w = *weightsPtr_; + weightsPtr_ = std::make_unique<scalarListList>(mapperLen_); + auto& wght = *weightsPtr_; - const List<objectMap>& cfp = mpm_.cellsFromPointsMap(); - forAll(cfp, cfpI) + // Set the addressing and uniform weight + const auto setAddrWeights = [&] + ( + const List<objectMap>& maps, + const char * const nameOfMap + ) { - // Get addressing - const labelList& mo = cfp[cfpI].masterObjects(); - - label celli = cfp[cfpI].index(); - - if (addr[celli].size()) + for (const objectMap& map : maps) { - FatalErrorInFunction - << "Master cell " << celli - << " mapped from point cells " << mo - << " already destination of mapping." << abort(FatalError); - } - - // Map from masters, uniform weights - addr[celli] = mo; - w[celli] = scalarList(mo.size(), 1.0/mo.size()); - } - - const List<objectMap>& cfe = mpm_.cellsFromEdgesMap(); - - forAll(cfe, cfeI) - { - // Get addressing - const labelList& mo = cfe[cfeI].masterObjects(); + // Get index, addressing + const label celli = map.index(); + const labelList& mo = map.masterObjects(); + if (mo.empty()) continue; // safety - label celli = cfe[cfeI].index(); + if (addr[celli].size()) + { + FatalErrorInFunction + << "Master cell " << celli + << " already mapped, cannot apply " + << nameOfMap + << flatOutput(mo) << abort(FatalError); + } - if (addr[celli].size()) - { - FatalErrorInFunction - << "Master cell " << celli - << " mapped from edge cells " << mo - << " already destination of mapping." << abort(FatalError); + // Map from masters, uniform weights + addr[celli] = mo; + wght[celli] = scalarList(mo.size(), 1.0/mo.size()); } + }; - // Map from masters, uniform weights - addr[celli] = mo; - w[celli] = scalarList(mo.size(), 1.0/mo.size()); - } - - const List<objectMap>& cff = mpm_.cellsFromFacesMap(); - - forAll(cff, cffI) - { - // Get addressing - const labelList& mo = cff[cffI].masterObjects(); - - label celli = cff[cffI].index(); - - if (addr[celli].size()) - { - FatalErrorInFunction - << "Master cell " << celli - << " mapped from face cells " << mo - << " already destination of mapping." << abort(FatalError); - } - // Map from masters, uniform weights - addr[celli] = mo; - w[celli] = scalarList(mo.size(), 1.0/mo.size()); - } + setAddrWeights(mpm_.cellsFromPointsMap(), "point cells"); + setAddrWeights(mpm_.cellsFromEdgesMap(), "edge cells"); + setAddrWeights(mpm_.cellsFromFacesMap(), "face cells"); // Volume conservative mapping if possible - const List<objectMap>& cfc = mpm_.cellsFromCellsMap(); - - forAll(cfc, cfcI) - { - // Get addressing - const labelList& mo = cfc[cfcI].masterObjects(); - - label celli = cfc[cfcI].index(); - - if (addr[celli].size()) - { - FatalErrorInFunction - << "Master cell " << celli - << " mapped from cell cells " << mo - << " already destination of mapping." - << abort(FatalError); - } - - // Map from masters - addr[celli] = mo; - } + const List<objectMap>& cellsFromCells = mpm_.cellsFromCellsMap(); + setAddrWeights(cellsFromCells, "cell cells"); if (mpm_.hasOldCellVolumes()) { @@ -185,182 +161,188 @@ void Foam::cellMapper::calcAddressing() const { FatalErrorInFunction << "cellVolumes size " << V.size() - << " is not the old number of cells " << sizeBeforeMapping() + << " != old number of cells " << sizeBeforeMapping() << ". Are your cellVolumes already mapped?" << " (new number of cells " << size() << ")" << abort(FatalError); } - forAll(cfc, cfcI) + for (const auto& map : cellsFromCells) { - const labelList& mo = cfc[cfcI].masterObjects(); - - label celli = cfc[cfcI].index(); + // Get index, addressing + const label celli = map.index(); + const labelList& mo = map.masterObjects(); + if (mo.empty()) continue; // safety - w[celli].setSize(mo.size()); + // wght[celli] is already sized and uniform weighted + auto& wght_cell = wght[celli]; - if (mo.size()) + scalar sumV = 0; + forAll(mo, ci) { - scalar sumV = 0; - forAll(mo, ci) - { - w[celli][ci] = V[mo[ci]]; - sumV += V[mo[ci]]; - } - if (sumV > VSMALL) - { - forAll(mo, ci) - { - w[celli][ci] /= sumV; - } - } - else + wght_cell[ci] = V[mo[ci]]; + sumV += V[mo[ci]]; + } + if (sumV > VSMALL) + { + for (auto& w : wght_cell) { - // Exception: zero volume. Use uniform mapping - w[celli] = scalarList(mo.size(), 1.0/mo.size()); + w /= sumV; } } + else + { + // Exception: zero volume. Use uniform mapping + wght_cell = (1.0/mo.size()); + } } } - else + + + // Do mapped cells. + // - may already have been set, so check if addressing still empty(). + { - // Uniform weighted + const labelList& map = mpm_.cellMap(); - forAll(cfc, cfcI) + // The cellMap.size() == nCells() anyhow + for (label celli = 0; celli < mapperLen_; ++celli) { - const labelList& mo = cfc[cfcI].masterObjects(); - - label celli = cfc[cfcI].index(); + const label mappedi = map[celli]; - w[celli] = scalarList(mo.size(), 1.0/mo.size()); + if (mappedi >= 0 && addr[celli].empty()) + { + // Mapped from a single cell + addr[celli].resize(1, mappedi); + wght[celli].resize(1, 1.0); + } } } - // Do mapped faces. Note that can already be set from cellsFromCells - // so check if addressing size still zero. + // Grab inserted points (for them the size of addressing is still zero) - const labelList& cm = mpm_.cellMap(); + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; - forAll(cm, celli) + // The nInsertedObjects_ already counted in the constructor + if (nInsertedObjects_) { - if (cm[celli] > -1 && addr[celli].empty()) - { - // Mapped from a single cell - addr[celli] = labelList(1, cm[celli]); - w[celli] = scalarList(1, 1.0); - } - } - - // Grab inserted points (for them the size of addressing is still zero) + inserted.resize(nInsertedObjects_); - insertedCellLabelsPtr_ = new labelList(mesh_.nCells()); - labelList& insertedCells = *insertedCellLabelsPtr_; + label nInserted = 0; + forAll(addr, i) + { + if (addr[i].empty()) + { + // Mapped from dummy cell 0 + addr[i].resize(1, 0); + wght[i].resize(1, 1.0); - label nInsertedCells = 0; + inserted[nInserted] = i; + ++nInserted; - forAll(addr, celli) - { - if (addr[celli].empty()) + // TBD: check (nInsertedObjects_ < nInserted)? + #ifdef FULLDEBUG + if (nInsertedObjects_ < nInserted) + { + FatalErrorInFunction + << "Unexpected insert of more than " + << nInsertedObjects_ << " items\n" + << abort(FatalError); + } + #endif + } + } + // TBD: check (nInserted < nInsertedObjects_)? + #ifdef FULLDEBUG + if (nInserted < nInsertedObjects_) { - // Mapped from a dummy cell - addr[celli] = labelList(1, Zero); - w[celli] = scalarList(1, scalar(1)); - - insertedCells[nInsertedCells] = celli; - nInsertedCells++; + WarningInFunction + << "Found " << nInserted << " instead of " + << nInsertedObjects_ << " items to insert\n"; } + #endif + // The resize should be unnecessary + inserted.resize(nInserted); } - - insertedCells.setSize(nInsertedCells); } } -void Foam::cellMapper::clearOut() -{ - deleteDemandDrivenData(directAddrPtr_); - deleteDemandDrivenData(interpolationAddrPtr_); - deleteDemandDrivenData(weightsPtr_); - deleteDemandDrivenData(insertedCellLabelsPtr_); -} +// void Foam::cellMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// insertedObjectsPtr_.reset(nullptr); +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::cellMapper::cellMapper(const mapPolyMesh& mpm) : - mesh_(mpm.mesh()), mpm_(mpm), - insertedCells_(true), - direct_(false), - directAddrPtr_(nullptr), - interpolationAddrPtr_(nullptr), - weightsPtr_(nullptr), - insertedCellLabelsPtr_(nullptr) -{ - // Check for possibility of direct mapping - if + mapperLen_(mpm.mesh().nCells()), + nInsertedObjects_(0), + direct_ ( - mpm_.cellsFromPointsMap().empty() - && mpm_.cellsFromEdgesMap().empty() - && mpm_.cellsFromFacesMap().empty() - && mpm_.cellsFromCellsMap().empty() + // Mapping without interpolation? + mpm.cellsFromPointsMap().empty() + && mpm.cellsFromEdgesMap().empty() + && mpm.cellsFromFacesMap().empty() + && mpm.cellsFromCellsMap().empty() ) +{ + const auto& directMap = mpm_.cellMap(); + + if (!mapperLen_) { + // Empty mesh direct_ = true; + nInsertedObjects_ = 0; } - else - { - direct_ = false; - } - - // Check for inserted cells - if (direct_ && (mpm_.cellMap().empty() || min(mpm_.cellMap()) > -1)) + else if (direct_) { - insertedCells_ = false; + // Number of inserted cells (-ve values) + nInsertedObjects_ = std::count_if + ( + directMap.cbegin(), + directMap.cbegin(mapperLen_), + [](label i) { return (i < 0); } + ); } else { - // Need to check all 3 lists to see if there are inserted cells - // with no owner + // Check if there are inserted cells with no owner + // (check all lists) - // Make a copy of the cell map, add the entried for cells from points, - // cells from edges and cells from faces and check for left-overs - labelList cm(mesh_.nCells(), -1); + bitSet unmapped(mapperLen_, true); - const List<objectMap>& cfp = mpm_.cellsFromPointsMap(); + unmapped.unset(directMap); // direct mapped - forAll(cfp, cfpI) + for (const auto& map : mpm_.cellsFromPointsMap()) { - cm[cfp[cfpI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - const List<objectMap>& cfe = mpm_.cellsFromEdgesMap(); - - forAll(cfe, cfeI) + for (const auto& map : mpm_.cellsFromEdgesMap()) { - cm[cfe[cfeI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - const List<objectMap>& cff = mpm_.cellsFromFacesMap(); - - forAll(cff, cffI) + for (const auto& map : mpm_.cellsFromFacesMap()) { - cm[cff[cffI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - const List<objectMap>& cfc = mpm_.cellsFromCellsMap(); - - forAll(cfc, cfcI) + for (const auto& map : mpm_.cellsFromCellsMap()) { - cm[cfc[cfcI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - if (min(cm) < 0) - { - insertedCells_ = true; - } + nInsertedObjects_ = label(unmapped.count()); } } @@ -368,15 +350,14 @@ Foam::cellMapper::cellMapper(const mapPolyMesh& mpm) // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::cellMapper::~cellMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::label Foam::cellMapper::size() const { + // OR: return mapperLen_; return mpm_.cellMap().size(); } @@ -422,12 +403,12 @@ const Foam::labelListList& Foam::cellMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } @@ -451,20 +432,18 @@ const Foam::scalarListList& Foam::cellMapper::weights() const const Foam::labelList& Foam::cellMapper::insertedObjectLabels() const { - if (!insertedCellLabelsPtr_) + if (!insertedObjectsPtr_) { - if (!insertedObjects()) + if (!nInsertedObjects_) { - // There are no inserted cells - insertedCellLabelsPtr_ = new labelList(0); - } - else - { - calcAddressing(); + // No inserted objects will be created + return labelList::null(); } + + calcAddressing(); } - return *insertedCellLabelsPtr_; + return *insertedObjectsPtr_; } diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.H b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.H index 60eaedd82a3..649b70dab99 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.H +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/cellMapper/cellMapper.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,8 +37,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef cellMapper_H -#define cellMapper_H +#ifndef Foam_cellMapper_H +#define Foam_cellMapper_H #include "morphFieldMapper.H" @@ -46,8 +47,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes -class polyMesh; +// Forward Declarations class mapPolyMesh; /*---------------------------------------------------------------------------*\ @@ -58,60 +58,56 @@ class cellMapper : public morphFieldMapper { - // Private data - - //- Reference to polyMesh - const polyMesh& mesh_; + // Private Data //- Reference to mapPolyMesh const mapPolyMesh& mpm_; - //- Are there any inserted (unmapped) cells - bool insertedCells_; + //- The size of the mapper = polyMesh::nCells() + const label mapperLen_; + + //- Number of inserted (unmapped) cells + label nInsertedObjects_; //- Is the mapping direct bool direct_; - // Demand-driven private data + // Demand-Driven Data //- Direct addressing (only one for of addressing is used) - mutable labelList* directAddrPtr_; + mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one for of addressing is used) - mutable labelListList* interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights - mutable scalarListList* weightsPtr_; + mutable std::unique_ptr<scalarListList> weightsPtr_; //- Inserted cells - mutable labelList* insertedCellLabelsPtr_; + mutable std::unique_ptr<labelList> insertedObjectsPtr_; // Private Member Functions - //- No copy construct - cellMapper(const cellMapper&) = delete; - - //- No copy assignment - void operator=(const cellMapper&) = delete; - - //- Calculate addressing for mapping with inserted cells void calcAddressing() const; - //- Clear out local storage - void clearOut(); +public: + // Generated Methods -public: + //- No copy construct + cellMapper(const cellMapper&) = delete; + + //- No copy assignment + void operator=(const cellMapper&) = delete; - // Static data members // Constructors //- Construct from mapPolyMesh - cellMapper(const mapPolyMesh& mpm); + explicit cellMapper(const mapPolyMesh& mpm); //- Destructor @@ -120,7 +116,7 @@ public: // Member Functions - //- Return size + //- The mapper size virtual label size() const; //- Return size before mapping @@ -132,6 +128,7 @@ public: return direct_; } + //- Are there unmapped values? virtual bool hasUnmapped() const { return insertedObjects(); @@ -149,7 +146,7 @@ public: //- Are there any inserted cells virtual bool insertedObjects() const { - return insertedCells_; + return bool(nInsertedObjects_); } //- Return list of inserted cells diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.C index 7745f659049..d638fe3efc6 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,7 +27,6 @@ License \*---------------------------------------------------------------------------*/ #include "faceMapper.H" -#include "demandDrivenData.H" #include "polyMesh.H" #include "mapPolyMesh.H" @@ -37,9 +37,9 @@ void Foam::faceMapper::calcAddressing() const if ( directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ - || insertedFaceLabelsPtr_ + || insertedObjectsPtr_ ) { FatalErrorInFunction @@ -51,223 +51,240 @@ void Foam::faceMapper::calcAddressing() const { // Direct addressing, no weights - directAddrPtr_ = new labelList(mpm_.faceMap()); - labelList& directAddr = *directAddrPtr_; + // Restrict addressing list to contain only live faces + directAddrPtr_ = std::make_unique<labelList> + ( + labelList::subList(mpm_.faceMap(), mapperLen_) + ); + auto& directAddr = *directAddrPtr_; - // Reset the size of addressing list to contain only live faces - directAddr.setSize(mesh_.nFaces()); + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; - insertedFaceLabelsPtr_ = new labelList(mesh_.nFaces()); - labelList& insertedFaces = *insertedFaceLabelsPtr_; - - label nInsertedFaces = 0; - - forAll(directAddr, facei) + // The nInsertedObjects_ already counted in the constructor + if (nInsertedObjects_) { - if (directAddr[facei] < 0) + inserted.resize(nInsertedObjects_); + + label nInserted = 0; + forAll(directAddr, i) { - // Found inserted face - directAddr[facei] = 0; - insertedFaces[nInsertedFaces] = facei; - nInsertedFaces++; + if (directAddr[i] < 0) + { + // Found inserted + directAddr[i] = 0; + inserted[nInserted] = i; + ++nInserted; + + // TBD: check (nInsertedObjects_ < nInserted)? + #ifdef FULLDEBUG + if (nInsertedObjects_ < nInserted) + { + FatalErrorInFunction + << "Unexpected insert of more than " + << nInsertedObjects_ << " items\n" + << abort(FatalError); + } + #endif + } } + // TBD: check (nInserted < nInsertedObjects_)? + #ifdef FULLDEBUG + if (nInserted < nInsertedObjects_) + { + WarningInFunction + << "Found " << nInserted << " instead of " + << nInsertedObjects_ << " items to insert\n"; + } + #endif + // The resize should be unnecessary + inserted.resize(nInserted); } - - insertedFaces.setSize(nInsertedFaces); } else { // Interpolative addressing - interpolationAddrPtr_ = new labelListList(mesh_.nFaces()); - labelListList& addr = *interpolationAddrPtr_; + interpAddrPtr_ = std::make_unique<labelListList>(mapperLen_); + auto& addr = *interpAddrPtr_; - weightsPtr_ = new scalarListList(mesh_.nFaces()); - scalarListList& w = *weightsPtr_; + weightsPtr_ = std::make_unique<scalarListList>(mapperLen_); + auto& wght = *weightsPtr_; - const List<objectMap>& ffp = mpm_.facesFromPointsMap(); - forAll(ffp, ffpI) + // Set the addressing and uniform weight + const auto setAddrWeights = [&] + ( + const List<objectMap>& maps, + const char * const nameOfMap + ) { - // Get addressing - const labelList& mo = ffp[ffpI].masterObjects(); - - label facei = ffp[ffpI].index(); - - if (addr[facei].size()) + for (const objectMap& map : maps) { - FatalErrorInFunction - << "Master face " << facei - << " mapped from point faces " << mo - << " already destination of mapping." << abort(FatalError); + // Get index, addressing + const label facei = map.index(); + const labelList& mo = map.masterObjects(); + if (mo.empty()) continue; // safety + + if (addr[facei].size()) + { + FatalErrorInFunction + << "Master face " << facei + << " already mapped, cannot apply " + << nameOfMap + << flatOutput(mo) << abort(FatalError); + } + + // Map from masters, uniform weights + addr[facei] = mo; + wght[facei] = scalarList(mo.size(), 1.0/mo.size()); } + }; - // Map from masters, uniform weights - addr[facei] = mo; - w[facei] = scalarList(mo.size(), 1.0/mo.size()); - } - - const List<objectMap>& ffe = mpm_.facesFromEdgesMap(); - - forAll(ffe, ffeI) - { - // Get addressing - const labelList& mo = ffe[ffeI].masterObjects(); - - label facei = ffe[ffeI].index(); - if (addr[facei].size()) - { - FatalErrorInFunction - << "Master face " << facei - << " mapped from edge faces " << mo - << " already destination of mapping." << abort(FatalError); - } + setAddrWeights(mpm_.facesFromPointsMap(), "point faces"); + setAddrWeights(mpm_.facesFromEdgesMap(), "edge faces"); + setAddrWeights(mpm_.facesFromFacesMap(), "face faces"); - // Map from masters, uniform weights - addr[facei] = mo; - w[facei] = scalarList(mo.size(), 1.0/mo.size()); - } - const List<objectMap>& fff = mpm_.facesFromFacesMap(); + // Do mapped faces. + // - may already have been set, so check if addressing still empty(). - forAll(fff, fffI) { - // Get addressing - const labelList& mo = fff[fffI].masterObjects(); - - label facei = fff[fffI].index(); + const labelList& map = mpm_.faceMap(); - if (addr[facei].size()) + // NB: faceMap can be longer than nFaces() + for (label facei = 0; facei < mapperLen_; ++facei) { - FatalErrorInFunction - << "Master face " << facei - << " mapped from face faces " << mo - << " already destination of mapping." << abort(FatalError); - } - - // Map from masters, uniform weights - addr[facei] = mo; - w[facei] = scalarList(mo.size(), 1.0/mo.size()); - } - - - // Do mapped faces. Note that can already be set from facesFromFaces - // so check if addressing size still zero. - const labelList& fm = mpm_.faceMap(); - - forAll(fm, facei) - { - if (fm[facei] > -1 && addr[facei].empty()) - { - // Mapped from a single face - addr[facei] = labelList(1, fm[facei]); - w[facei] = scalarList(1, 1.0); + const label mappedi = map[facei]; + + if (mappedi >= 0 && addr[facei].empty()) + { + // Mapped from a single face + addr[facei].resize(1, mappedi); + wght[facei].resize(1, 1.0); + } } } // Grab inserted faces (for them the size of addressing is still zero) - insertedFaceLabelsPtr_ = new labelList(mesh_.nFaces()); - labelList& insertedFaces = *insertedFaceLabelsPtr_; - - label nInsertedFaces = 0; + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; - forAll(addr, facei) + // The nInsertedObjects_ already counted in the constructor + if (nInsertedObjects_) { - if (addr[facei].empty()) - { - // Mapped from a dummy face - addr[facei] = labelList(1, Zero); - w[facei] = scalarList(1, scalar(1)); + inserted.resize(nInsertedObjects_); - insertedFaces[nInsertedFaces] = facei; - nInsertedFaces++; + label nInserted = 0; + forAll(addr, i) + { + if (addr[i].empty()) + { + // Mapped from dummy face 0 + addr[i].resize(1, 0); + wght[i].resize(1, 1.0); + + inserted[nInserted] = i; + ++nInserted; + + // TBD: check (nInsertedObjects_ < nInserted)? + #ifdef FULLDEBUG + if (nInsertedObjects_ < nInserted) + { + FatalErrorInFunction + << "Unexpected insert of more than " + << nInsertedObjects_ << " items\n" + << abort(FatalError); + } + #endif + } + } + // TBD: check (nInserted < nInsertedObjects_)? + #ifdef FULLDEBUG + if (nInserted < nInsertedObjects_) + { + WarningInFunction + << "Found " << nInserted << " instead of " + << nInsertedObjects_ << " items to insert\n"; } + #endif + // The resize should be unnecessary + inserted.resize(nInserted); } - - insertedFaces.setSize(nInsertedFaces); } } -void Foam::faceMapper::clearOut() -{ - deleteDemandDrivenData(directAddrPtr_); - deleteDemandDrivenData(interpolationAddrPtr_); - deleteDemandDrivenData(weightsPtr_); - deleteDemandDrivenData(insertedFaceLabelsPtr_); -} +// void Foam::faceMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// insertedObjectsPtr_.reset(nullptr); +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::faceMapper::faceMapper(const mapPolyMesh& mpm) : - mesh_(mpm.mesh()), mpm_(mpm), - insertedFaces_(true), - direct_(false), - directAddrPtr_(nullptr), - interpolationAddrPtr_(nullptr), - weightsPtr_(nullptr), - insertedFaceLabelsPtr_(nullptr) -{ - // Check for possibility of direct mapping - if + mapperLen_(mpm.mesh().nFaces()), + nInsertedObjects_(0), + direct_ ( - mpm_.facesFromPointsMap().empty() - && mpm_.facesFromEdgesMap().empty() - && mpm_.facesFromFacesMap().empty() + // Mapping without interpolation? + mpm.facesFromPointsMap().empty() + && mpm.facesFromEdgesMap().empty() + && mpm.facesFromFacesMap().empty() ) +{ + const auto& directMap = mpm_.faceMap(); + + if (!mapperLen_) { + // Empty mesh direct_ = true; + nInsertedObjects_ = 0; } - else - { - direct_ = false; - } - - // Check for inserted faces - if (direct_ && (mpm_.faceMap().empty() || min(mpm_.faceMap()) > -1)) + else if (direct_) { - insertedFaces_ = false; + // Number of inserted faces (-ve values) + nInsertedObjects_ = std::count_if + ( + directMap.cbegin(), + directMap.cbegin(mapperLen_), + [](label i) { return (i < 0); } + ); } else { - // Need to check all 3 lists to see if there are inserted faces - // with no owner + // Check if there are inserted faces with no owner + // (check all lists) - // Make a copy of the face map, add the entries for faces from points - // and faces from edges and check for left-overs - labelList fm(mesh_.nFaces(), -1); + bitSet unmapped(mapperLen_, true); - const List<objectMap>& ffp = mpm_.facesFromPointsMap(); + unmapped.unset(directMap); // direct mapped - forAll(ffp, ffpI) + for (const auto& map : mpm_.facesFromPointsMap()) { - fm[ffp[ffpI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - const List<objectMap>& ffe = mpm_.facesFromEdgesMap(); - - forAll(ffe, ffeI) + for (const auto& map : mpm_.facesFromEdgesMap()) { - fm[ffe[ffeI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - const List<objectMap>& fff = mpm_.facesFromFacesMap(); - - forAll(fff, fffI) + for (const auto& map : mpm_.facesFromFacesMap()) { - fm[fff[fffI].index()] = 0; + if (!map.empty()) unmapped.unset(map.index()); } - if (min(fm) < 0) - { - insertedFaces_ = true; - } + nInsertedObjects_ = label(unmapped.count()); } } @@ -275,16 +292,14 @@ Foam::faceMapper::faceMapper(const mapPolyMesh& mpm) // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::faceMapper::~faceMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::label Foam::faceMapper::size() const { - return mesh_.nFaces(); + return mapperLen_; } @@ -335,12 +350,12 @@ const Foam::labelListList& Foam::faceMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } @@ -364,20 +379,18 @@ const Foam::scalarListList& Foam::faceMapper::weights() const const Foam::labelList& Foam::faceMapper::insertedObjectLabels() const { - if (!insertedFaceLabelsPtr_) + if (!insertedObjectsPtr_) { - if (!insertedObjects()) - { - // There are no inserted faces - insertedFaceLabelsPtr_ = new labelList(0); - } - else + if (!nInsertedObjects_) { - calcAddressing(); + // No inserted objects will be created + return labelList::null(); } + + calcAddressing(); } - return *insertedFaceLabelsPtr_; + return *insertedObjectsPtr_; } diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.H b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.H index b5f43bad230..a1b331835d7 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.H +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/faceMapper/faceMapper.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,8 +37,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef faceMapper_H -#define faceMapper_H +#ifndef Foam_faceMapper_H +#define Foam_faceMapper_H #include "morphFieldMapper.H" #include "HashSet.H" @@ -47,8 +48,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes -class polyMesh; +// Forward Declarations class mapPolyMesh; /*---------------------------------------------------------------------------*\ @@ -59,60 +59,56 @@ class faceMapper : public morphFieldMapper { - // Private data - - //- Reference to polyMesh - const polyMesh& mesh_; + // Private Data //- Reference to mapPolyMesh const mapPolyMesh& mpm_; - //- Are there any inserted (unmapped) faces - bool insertedFaces_; + //- The size of the mapper = polyMesh::nFaces() + const label mapperLen_; + + //- Number of inserted (unmapped) faces + label nInsertedObjects_; //- Is the mapping direct bool direct_; - // Demand-driven private data + // Demand-Driven Data //- Direct addressing (only one for of addressing is used) - mutable labelList* directAddrPtr_; + mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one for of addressing is used) - mutable labelListList* interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights - mutable scalarListList* weightsPtr_; + mutable std::unique_ptr<scalarListList> weightsPtr_; //- Inserted faces - mutable labelList* insertedFaceLabelsPtr_; + mutable std::unique_ptr<labelList> insertedObjectsPtr_; // Private Member Functions - //- No copy construct - faceMapper(const faceMapper&) = delete; - - //- No copy assignment - void operator=(const faceMapper&) = delete; - - //- Calculate addressing for mapping with inserted faces void calcAddressing() const; - //- Clear out local storage - void clearOut(); +public: + // Generated Methods -public: + //- No copy construct + faceMapper(const faceMapper&) = delete; + + //- No copy assignment + void operator=(const faceMapper&) = delete; - // Static data members // Constructors //- Construct from mapPolyMesh - faceMapper(const mapPolyMesh& mpm); + explicit faceMapper(const mapPolyMesh& mpm); //- Destructor @@ -121,7 +117,7 @@ public: // Member Functions - //- Return size + //- The mapper size virtual label size() const; //- Return size of field before mapping @@ -165,7 +161,7 @@ public: //- Are there any inserted faces virtual bool insertedObjects() const { - return insertedFaces_; + return bool(nInsertedObjects_); } //- Return list of inserted faces diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.C index ee7694bb112..5baa1ac33fd 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.C @@ -257,7 +257,7 @@ Foam::mapPolyMesh::mapPolyMesh oldCellVolumesPtr_ = oldCellVolumesPtr.clone(); } - if (oldPatchStarts_.size() > 0) + if (oldPatchStarts_.size()) { // Calculate old patch sizes for (label patchi = 0; patchi < oldPatchStarts_.size() - 1; patchi++) diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.H b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.H index a7b8f955a5e..38bb76e580a 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.H +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapPolyMesh.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -153,6 +153,7 @@ SourceFiles namespace Foam { +// Forward Declarations class polyMesh; /*---------------------------------------------------------------------------*\ @@ -161,7 +162,7 @@ class polyMesh; class mapPolyMesh { - // Private data + // Private Data //- Reference to polyMesh const polyMesh& mesh_; @@ -273,7 +274,9 @@ class mapPolyMesh autoPtr<scalarField> oldCellVolumesPtr_; - // Private Member Functions +public: + + // Generated Methods //- No copy construct mapPolyMesh(const mapPolyMesh&) = delete; @@ -282,14 +285,13 @@ class mapPolyMesh void operator=(const mapPolyMesh&) = delete; -public: - // Constructors //- Construct from mesh mapPolyMesh(const polyMesh& mesh); - //- Construct from components. Copy (except for oldCellVolumes). + //- Copy construct from components, + //- except for oldCellVolumes which is move construct mapPolyMesh ( const polyMesh& mesh, @@ -356,18 +358,19 @@ public: const bool reuse ); + // Member Functions // Access //- Return polyMesh - const polyMesh& mesh() const + const polyMesh& mesh() const noexcept { return mesh_; } //- Number of old points - label nOldPoints() const + label nOldPoints() const noexcept { return nOldPoints_; } @@ -379,13 +382,13 @@ public: } //- Number of old faces - label nOldFaces() const + label nOldFaces() const noexcept { return nOldFaces_; } //- Number of old cells - label nOldCells() const + label nOldCells() const noexcept { return nOldCells_; } @@ -394,13 +397,13 @@ public: // Contains the old point label for all new points. // For preserved points this is the old point label. // For added points this is the master point ID - const labelList& pointMap() const + const labelList& pointMap() const noexcept { return pointMap_; } //- Points originating from points - const List<objectMap>& pointsFromPointsMap() const + const List<objectMap>& pointsFromPointsMap() const noexcept { return pointsFromPointsMap_; } @@ -408,56 +411,56 @@ public: //- Old face map. // Contains a list of old face labels for every new face. // Warning: this map contains invalid entries for new faces - const labelList& faceMap() const + const labelList& faceMap() const noexcept { return faceMap_; } //- Faces inflated from points - const List<objectMap>& facesFromPointsMap() const + const List<objectMap>& facesFromPointsMap() const noexcept { return facesFromPointsMap_; } //- Faces inflated from edges - const List<objectMap>& facesFromEdgesMap() const + const List<objectMap>& facesFromEdgesMap() const noexcept { return facesFromEdgesMap_; } //- Faces originating from faces - const List<objectMap>& facesFromFacesMap() const + const List<objectMap>& facesFromFacesMap() const noexcept { return facesFromFacesMap_; } //- Old cell map. // Contains old cell label for all preserved cells. - const labelList& cellMap() const + const labelList& cellMap() const noexcept { return cellMap_; } //- Cells inflated from points - const List<objectMap>& cellsFromPointsMap() const + const List<objectMap>& cellsFromPointsMap() const noexcept { return cellsFromPointsMap_; } //- Cells inflated from edges - const List<objectMap>& cellsFromEdgesMap() const + const List<objectMap>& cellsFromEdgesMap() const noexcept { return cellsFromEdgesMap_; } //- Cells inflated from faces - const List<objectMap>& cellsFromFacesMap() const + const List<objectMap>& cellsFromFacesMap() const noexcept { return cellsFromFacesMap_; } //- Cells originating from cells - const List<objectMap>& cellsFromCellsMap() const + const List<objectMap>& cellsFromCellsMap() const noexcept { return cellsFromCellsMap_; } @@ -467,7 +470,7 @@ public: //- Reverse point map // Contains new point label for all old and added points - const labelList& reversePointMap() const + const labelList& reversePointMap() const noexcept { return reversePointMap_; } @@ -499,7 +502,7 @@ public: //- Reverse face map // Contains new face label for all old and added faces - const labelList& reverseFaceMap() const + const labelList& reverseFaceMap() const noexcept { return reverseFaceMap_; } @@ -530,7 +533,7 @@ public: //- Reverse cell map // Contains new cell label for all old and added cells - const labelList& reverseCellMap() const + const labelList& reverseCellMap() const noexcept { return reverseCellMap_; } @@ -560,7 +563,7 @@ public: } //- Map of flipped face flux faces - const labelHashSet& flipFaceFlux() const + const labelHashSet& flipFaceFlux() const noexcept { return flipFaceFlux_; } @@ -568,7 +571,7 @@ public: //- Patch point renumbering // For every preserved point on a patch give the old position. // For added points, the index is set to -1 - const labelListList& patchPointMap() const + const labelListList& patchPointMap() const noexcept { return patchPointMap_; } @@ -579,7 +582,7 @@ public: //- Point zone renumbering // For every preserved point in zone give the old position. // For added points, the index is set to -1 - const labelListList& pointZoneMap() const + const labelListList& pointZoneMap() const noexcept { return pointZoneMap_; } @@ -587,7 +590,7 @@ public: //- Face zone point renumbering // For every preserved point in zone give the old position. // For added points, the index is set to -1 - const labelListList& faceZonePointMap() const + const labelListList& faceZonePointMap() const noexcept { return faceZonePointMap_; } @@ -595,7 +598,7 @@ public: //- Face zone face renumbering // For every preserved face in zone give the old position. // For added faces, the index is set to -1 - const labelListList& faceZoneFaceMap() const + const labelListList& faceZoneFaceMap() const noexcept { return faceZoneFaceMap_; } @@ -603,7 +606,7 @@ public: //- Cell zone renumbering // For every preserved cell in zone give the old position. // For added cells, the index is set to -1 - const labelListList& cellZoneMap() const + const labelListList& cellZoneMap() const noexcept { return cellZoneMap_; } @@ -611,49 +614,48 @@ public: //- Pre-motion point positions. // This specifies the correct way of blowing up // zero-volume objects - const pointField& preMotionPoints() const + const pointField& preMotionPoints() const noexcept { return preMotionPoints_; } //- Has valid preMotionPoints? - bool hasMotionPoints() const + bool hasMotionPoints() const noexcept { - return preMotionPoints_.size() > 0; + return !preMotionPoints_.empty(); } //- Return list of the old patch sizes - const labelList& oldPatchSizes() const + const labelList& oldPatchSizes() const noexcept { return oldPatchSizes_; } //- Return list of the old patch start labels - const labelList& oldPatchStarts() const + const labelList& oldPatchStarts() const noexcept { return oldPatchStarts_; } //- Return numbers of mesh points per old patch - const labelList& oldPatchNMeshPoints() const + const labelList& oldPatchNMeshPoints() const noexcept { return oldPatchNMeshPoints_; } - // Geometric mapping data - - bool hasOldCellVolumes() const - { - return bool(oldCellVolumesPtr_); - } + // Geometric mapping data - const scalarField& oldCellVolumes() const - { - return *oldCellVolumesPtr_; - } + bool hasOldCellVolumes() const noexcept + { + return bool(oldCellVolumesPtr_); + } + const scalarField& oldCellVolumes() const + { + return *oldCellVolumesPtr_; + } }; diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMap.H b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMap.H index 676ae137297..4b739eeafee 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMap.H +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMap.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,10 +36,10 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef objectMap_H -#define objectMap_H +#ifndef Foam_objectMap_H +#define Foam_objectMap_H -#include "labelList.H" +#include "List.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -46,7 +47,6 @@ namespace Foam { // Forward Declarations - class objectMap; inline bool operator==(const objectMap& a, const objectMap& b); inline bool operator!=(const objectMap& a, const objectMap& b); @@ -60,38 +60,51 @@ inline Istream& operator>>(Istream&, objectMap&); class objectMap { - // Private data + // Private Data //- Object index label index_; - //- Master object index - labelList masterObjects_; + //- Master object indices + labelList objects_; public: // Constructors - //- Null constructor, with index=-1 and no objects - inline objectMap(); + //- Default construct, with index=-1 and no objects + objectMap() noexcept : index_(-1) {} + + //- Copy construct from components + inline objectMap(const label index, const labelUList& master); - //- Construct from components - inline objectMap(const label index, const UList<label>& master); + //- Move construct from components + inline objectMap(const label index, labelList&& master); //- Construct from Istream - inline objectMap(Istream& is); + inline explicit objectMap(Istream& is); // Member Functions - //- Return object index - inline label& index(); - inline label index() const; + //- True if there are no objects + bool empty() const noexcept { return objects_.empty(); } + + //- The number of objects + label size() const noexcept { return objects_.size(); } + + //- The object index + label& index() noexcept { return index_; } + + //- The object index + label index() const noexcept { return index_; } + + //- The master object indices + labelList& masterObjects() noexcept { return objects_; } - //- Return master object index - inline labelList& masterObjects(); - inline const labelList& masterObjects() const; + //- The master object indices + const labelList& masterObjects() const noexcept { return objects_; } // Friend Operators diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMapI.H b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMapI.H index 43f4dadc5ea..3c5167a8cdf 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMapI.H +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/objectMap/objectMapI.H @@ -29,17 +29,17 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -inline Foam::objectMap::objectMap() +inline Foam::objectMap::objectMap(const label index, const labelUList& master) : - index_(-1), - masterObjects_() + index_(index), + objects_(master) {} -inline Foam::objectMap::objectMap(const label index, const UList<label>& master) +inline Foam::objectMap::objectMap(const label index, labelList&& master) : index_(index), - masterObjects_(master) + objects_(std::move(master)) {} @@ -47,46 +47,20 @@ inline Foam::objectMap::objectMap(Istream& is) { is.readBegin("objectMap"); - is >> index_ >> static_cast<labelList&>(masterObjects_); + is >> index_ >> objects_; is.readEnd("objectMap"); is.check(FUNCTION_NAME); } -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -Foam::label& Foam::objectMap::index() -{ - return index_; -} - - -inline Foam::label Foam::objectMap::index() const -{ - return index_; -} - - -inline Foam::labelList& Foam::objectMap::masterObjects() -{ - return masterObjects_; -} - - -inline const Foam::labelList& Foam::objectMap::masterObjects() const -{ - return masterObjects_; -} - - // * * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * // inline bool Foam::operator==(const objectMap& a, const objectMap& b) { return ( - (a.index_ == b.index_) && (a.masterObjects_ == b.masterObjects_) + (a.index_ == b.index_) && (a.objects_ == b.objects_) ); } @@ -103,7 +77,7 @@ inline Foam::Ostream& Foam::operator<<(Ostream& os, const objectMap& a) { os << token::BEGIN_LIST << a.index_ << token::SPACE - << a.masterObjects_ + << a.objects_ << token::END_LIST; os.check(FUNCTION_NAME); @@ -114,7 +88,7 @@ inline Foam::Ostream& Foam::operator<<(Ostream& os, const objectMap& a) inline Foam::Istream& Foam::operator>>(Istream& is, objectMap& a) { is.readBegin("objectMap"); - is >> a.index_ >> a.masterObjects_; + is >> a.index_ >> a.objects_; is.readEnd("objectMap"); is.check(FUNCTION_NAME); diff --git a/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.C b/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.C index f575d1a6e87..06399811321 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.C +++ b/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.C @@ -27,7 +27,6 @@ License #include "faAreaMapper.H" #include "mapPolyMesh.H" -#include "demandDrivenData.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -38,9 +37,9 @@ void Foam::faAreaMapper::calcAddressing() const newFaceLabelsPtr_ || newFaceLabelsMapPtr_ || directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ - || insertedObjectLabelsPtr_ + || insertedObjectsPtr_ ) { FatalErrorInFunction @@ -73,7 +72,7 @@ void Foam::faAreaMapper::calcAddressing() const mesh_.mesh().nBoundaryFaces(), -1 ); - labelList& newFaceLabelsMap = *newFaceLabelsMapPtr_; + auto& newFaceLabelsMap = *newFaceLabelsMapPtr_; label nNewFaces = 0; Info<< "Old face list size: " << oldFaces.size() @@ -91,7 +90,7 @@ void Foam::faAreaMapper::calcAddressing() const newFaceLabels[nNewFaces] = reverseFaceMap[oldFaces[faceI]]; newFaceLabelsMap[nNewFaces] = faceI; - nNewFaces++; + ++nNewFaces; } } @@ -100,21 +99,20 @@ void Foam::faAreaMapper::calcAddressing() const { Info<< "Direct"<< endl; // Direct mapping: no further faces to add. Resize list - newFaceLabels.setSize(nNewFaces); + newFaceLabels.resize(nNewFaces); - directAddrPtr_ = std::make_unique<labelList>(newFaceLabels.size()); - labelList& addr = *directAddrPtr_; + directAddrPtr_ = std::make_unique<labelList> + ( + labelList::subList(newFaceLabelsMap, nNewFaces) + ); + auto& addr = *directAddrPtr_; // Adjust for creation of a boundary face from an internal face - forAll(addr, faceI) + forAll(addr, facei) { - if (newFaceLabelsMap[faceI] < oldNInternal) - { - addr[faceI] = 0; - } - else + if (addr[facei] < oldNInternal) { - addr[faceI] = newFaceLabelsMap[faceI]; + addr[facei] = 0; } } } @@ -122,20 +120,20 @@ void Foam::faAreaMapper::calcAddressing() const { // There are further faces to add. Prepare interpolation addressing // and weights to full size - interpolationAddrPtr_ = std::make_unique<labelListList> + interpAddrPtr_ = std::make_unique<labelListList> ( newFaceLabels.size() ); - labelListList& addr = *interpolationAddrPtr_; + auto& addr = *interpAddrPtr_; - weightsPtr_ = std::make_unique<scalarListList>(newFaceLabels.size()); - scalarListList& w = *weightsPtr_; + weightsPtr_ = std::make_unique<scalarListList>(addr.size()); + auto& wght = *weightsPtr_; // Insert single addressing and weights for (label addrI = 0; addrI < nNewFaces; ++addrI) { - addr[addrI] = labelList(1, newFaceLabelsMap[addrI]); - w[addrI] = scalarList(1, scalar(1)); + addr[addrI].resize(1, newFaceLabelsMap[addrI]); + wght[addrI].resize(1, 1.0); } // Pick up faces from points, edges and faces where the origin @@ -143,146 +141,87 @@ void Foam::faAreaMapper::calcAddressing() const // fast lookup // Set of faces previously in the mesh - labelHashSet oldFaceLookup(oldFaces); + const labelHashSet oldFaceLookup(oldFaces); - // Go through faces-from lists and add the ones where all - // old face labels belonged to the faMesh + // Check if master objects are in faMesh + DynamicList<label> validMo(128); - const List<objectMap>& ffp = mpm_.facesFromPointsMap(); - - forAll(ffp, ffpI) + const auto addCheckedObjects = [&](const List<objectMap>& maps) { - // Get addressing - const labelList& mo = ffp[ffpI].masterObjects(); - - // Check if master objects are in faMesh - labelList validMo(mo.size()); - label nValidMo = 0; - - forAll(mo, moI) + for (const objectMap& map : maps) { - if (oldFaceLookup.found(mo[moI])) - { - validMo[nValidMo] = oldFaceLookup[mo[moI]]; - nValidMo++; - } - } - - if (nValidMo > 0) - { - // Some objects found: add face and interpolation to list - newFaceLabels[nNewFaces] = ffp[ffpI].index(); - - // No old face available - newFaceLabelsMap[nNewFaces] = -1; + // Get target index, addressing + const label facei = map.index(); + const labelList& mo = map.masterObjects(); + if (mo.empty()) continue; // safety - // Map from masters, uniform weights - addr[nNewFaces] = validMo; - w[nNewFaces] = scalarList(validMo.size(), 1.0/validMo.size()); + validMo.clear(); + validMo.reserve(mo.size()); - nNewFaces++; - } - } - - const List<objectMap>& ffe = mpm_.facesFromEdgesMap(); - - forAll(ffe, ffeI) - { - // Get addressing - const labelList& mo = ffe[ffeI].masterObjects(); - - // Check if master objects are in faMesh - labelList validMo(mo.size()); - label nValidMo = 0; - - forAll(mo, moI) - { - if (oldFaceLookup.found(mo[moI])) + for (const label obji : mo) { - validMo[nValidMo] = oldFaceLookup[mo[moI]]; - nValidMo++; + if (oldFaceLookup.contains(obji)) + { + validMo.push_back(obji); + } } - } - - if (nValidMo > 0) - { - // Some objects found: add face and interpolation to list - newFaceLabels[nNewFaces] = ffe[ffeI].index(); - - // No old face available - newFaceLabelsMap[nNewFaces] = -1; - // Map from masters, uniform weights - addr[nNewFaces] = validMo; - w[nNewFaces] = scalarList(validMo.size(), 1.0/validMo.size()); - - nNewFaces++; - } - } - - const List<objectMap>& fff = mpm_.facesFromFacesMap(); + if (validMo.size()) + { + // Some objects found: add face and interpolation to list + newFaceLabels[nNewFaces] = facei; - forAll(fff, fffI) - { - // Get addressing - const labelList& mo = fff[fffI].masterObjects(); + // No old face available + newFaceLabelsMap[nNewFaces] = -1; - // Check if master objects are in faMesh - labelList validMo(mo.size()); - label nValidMo = 0; + // Map from masters, uniform weights + addr[nNewFaces] = validMo; + wght[nNewFaces] = + scalarList(validMo.size(), 1.0/validMo.size()); - forAll(mo, moI) - { - if (oldFaceLookup.found(mo[moI])) - { - validMo[nValidMo] = oldFaceLookup[mo[moI]]; - nValidMo++; + ++nNewFaces; } } + }; - if (nValidMo > 0) - { - // Some objects found: add face and interpolation to list - newFaceLabels[nNewFaces] = fff[fffI].index(); - - // No old face available - newFaceLabelsMap[nNewFaces] = -1; - // Map from masters, uniform weights - addr[nNewFaces] = validMo; - w[nNewFaces] = scalarList(validMo.size(), 1.0/validMo.size()); + // Go through faces-from lists and add the ones where all + // old face labels belonged to the faMesh - nNewFaces++; - } + { + addCheckedObjects(mpm_.facesFromPointsMap()); + addCheckedObjects(mpm_.facesFromEdgesMap()); + addCheckedObjects(mpm_.facesFromFacesMap()); } // All faces collected. Reset sizes of lists - newFaceLabels.setSize(nNewFaces); - newFaceLabelsMap.setSize(nNewFaces); - addr.setSize(nNewFaces); - w.setSize(nNewFaces); + newFaceLabels.resize(nNewFaces); + newFaceLabelsMap.resize(nNewFaces); + addr.resize(nNewFaces); + wght.resize(nNewFaces); + Info<< "addr: " << addr << nl - << "w: " << w << endl; + << "wght: " << wght << endl; } // Inserted objects cannot appear in the new faMesh as they have no master // HJ, 10/Aug/2011 - insertedObjectLabelsPtr_ = std::make_unique<labelList>(); + insertedObjectsPtr_ = std::make_unique<labelList>(); } -void Foam::faAreaMapper::clearOut() -{ - newFaceLabelsPtr_.reset(nullptr); - newFaceLabelsMapPtr_.reset(nullptr); - - directAddrPtr_.reset(nullptr); - interpolationAddrPtr_.reset(nullptr); - weightsPtr_.reset(nullptr); - - insertedObjectLabelsPtr_.reset(nullptr); - hasUnmapped_ = false; -} +// void Foam::faAreaMapper::clearOut() +// { +// newFaceLabelsPtr_.reset(nullptr); +// newFaceLabelsMapPtr_.reset(nullptr); +// +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// +// insertedObjectsPtr_.reset(nullptr); +// hasUnmapped_ = false; +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -295,25 +234,16 @@ Foam::faAreaMapper::faAreaMapper : mesh_(mesh), mpm_(mpm), - direct_(false), - hasUnmapped_(false), - sizeBeforeMapping_(mesh.nFaces()) -{ - // Check for possibility of direct mapping - if + sizeBeforeMapping_(mesh.nFaces()), + direct_ ( - mpm_.facesFromPointsMap().empty() - && mpm_.facesFromEdgesMap().empty() - && mpm_.facesFromFacesMap().empty() - ) - { - direct_ = true; - } - else - { - direct_ = false; - } - + // Mapping without interpolation? + mpm.facesFromPointsMap().empty() + && mpm.facesFromEdgesMap().empty() + && mpm.facesFromFacesMap().empty() + ), + hasUnmapped_(false) +{ // Inserted objects not supported: no master } @@ -321,9 +251,7 @@ Foam::faAreaMapper::faAreaMapper // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::faAreaMapper::~faAreaMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -377,12 +305,12 @@ const Foam::labelListList& Foam::faAreaMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } @@ -406,12 +334,12 @@ const Foam::scalarListList& Foam::faAreaMapper::weights() const const Foam::labelList& Foam::faAreaMapper::insertedObjectLabels() const { - if (!insertedObjectLabelsPtr_) + if (!insertedObjectsPtr_) { calcAddressing(); } - return *insertedObjectLabelsPtr_; + return *insertedObjectsPtr_; } diff --git a/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.H b/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.H index 9a75decf955..bcf404fe44a 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.H +++ b/src/finiteArea/faMesh/faMeshMapper/faAreaMapper.H @@ -38,8 +38,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef faAreaMapper_H -#define faAreaMapper_H +#ifndef Foam_faAreaMapper_H +#define Foam_faAreaMapper_H #include "morphFieldMapper.H" #include "faMesh.H" @@ -65,17 +65,17 @@ class faAreaMapper //- Reference to mapPolyMesh const mapPolyMesh& mpm_; + //- Size before mapping (old mesh size) + const label sizeBeforeMapping_; + //- Is the mapping direct bool direct_; - // Demand-driven private data + // Demand-Driven Data mutable bool hasUnmapped_; - //- Old mesh size - label sizeBeforeMapping_; - //- New face labels after mapping mutable std::unique_ptr<labelList> newFaceLabelsPtr_; @@ -87,13 +87,13 @@ class faAreaMapper mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one form of addressing is used) - mutable std::unique_ptr<labelListList> interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights mutable std::unique_ptr<scalarListList> weightsPtr_; //- Inserted faces - mutable std::unique_ptr<labelList> insertedObjectLabelsPtr_; + mutable std::unique_ptr<labelList> insertedObjectsPtr_; // Private Member Functions @@ -101,9 +101,6 @@ class faAreaMapper //- Calculate addressing void calcAddressing() const; - //- Clear out local storage - void clearOut(); - public: @@ -116,17 +113,20 @@ public: void operator=(const faAreaMapper&) = delete; - //- Construct from components - faAreaMapper - ( - const faMesh& mesh, - const mapPolyMesh& mpm - ); + // Constructors + + //- Construct from components + faAreaMapper + ( + const faMesh& mesh, + const mapPolyMesh& mpm + ); //- Destructor virtual ~faAreaMapper(); + // Member Functions //- Return new face labels diff --git a/src/finiteArea/faMesh/faMeshMapper/faBoundaryMeshMapper.H b/src/finiteArea/faMesh/faMeshMapper/faBoundaryMeshMapper.H index b60d1a66124..f34280b8bdd 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faBoundaryMeshMapper.H +++ b/src/finiteArea/faMesh/faMeshMapper/faBoundaryMeshMapper.H @@ -31,8 +31,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef faBoundaryMeshMapper_H -#define faBoundaryMeshMapper_H +#ifndef Foam_faBoundaryMeshMapper_H +#define Foam_faBoundaryMeshMapper_H #include "faPatchMapper.H" diff --git a/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.C b/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.C index 2dffd9ed7b2..34f90995a7e 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.C +++ b/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.C @@ -27,7 +27,6 @@ License #include "faEdgeMapper.H" #include "mapPolyMesh.H" -#include "demandDrivenData.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -47,11 +46,11 @@ void Foam::faEdgeMapper::calcAddressing() const } -void Foam::faEdgeMapper::clearOut() -{ - directAddrPtr_.reset(nullptr); - hasUnmapped_ = false; -} +// void Foam::faEdgeMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// hasUnmapped_ = false; +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -62,7 +61,9 @@ Foam::faEdgeMapper::faEdgeMapper const mapPolyMesh& mpm ) : - mesh_(mesh), + // currently unused: mesh_(mesh), + // currently unused: mpm_(mpm), + mapperLen_(mesh.nInternalEdges()), sizeBeforeMapping_(mesh.nInternalEdges()), hasUnmapped_(false) {} @@ -71,13 +72,17 @@ Foam::faEdgeMapper::faEdgeMapper // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::faEdgeMapper::~faEdgeMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +Foam::label Foam::faEdgeMapper::size() const +{ + return mapperLen_; +} + + const Foam::labelUList& Foam::faEdgeMapper::directAddressing() const { if (!directAddrPtr_) diff --git a/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.H b/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.H index 05f0cd0fda9..c50f72f6864 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.H +++ b/src/finiteArea/faMesh/faMeshMapper/faEdgeMapper.H @@ -39,8 +39,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef faEdgeMapper_H -#define faEdgeMapper_H +#ifndef Foam_faEdgeMapper_H +#define Foam_faEdgeMapper_H #include "morphFieldMapper.H" #include "faMesh.H" @@ -63,13 +63,19 @@ class faEdgeMapper // Private Data //- Reference to mesh - const faMesh& mesh_; + // currently unused: const faMesh& mesh_; - //- Old mesh size - label sizeBeforeMapping_; + //- Reference to mapPolyMesh + // currently unused: const mapPolyMesh& mpm_; + //- The size of the mapper = mesh_.nInternalEdges() + const label mapperLen_; - // Demand-driven private data + //- Size before mapping (old mesh size) + const label sizeBeforeMapping_; + + + // Demand-Driven Data mutable bool hasUnmapped_; @@ -82,10 +88,6 @@ class faEdgeMapper //- Calculate addressing void calcAddressing() const; - //- Clear out local storage - void clearOut(); - - public: // Generated Methods @@ -97,12 +99,14 @@ public: void operator=(const faEdgeMapper&) = delete; - //- Construct from components - faEdgeMapper - ( - const faMesh& mesh, - const mapPolyMesh& mpm - ); + // Constructors + + //- Construct from components + faEdgeMapper + ( + const faMesh& mesh, + const mapPolyMesh& mpm + ); //- Destructor @@ -111,11 +115,8 @@ public: // Member Functions - //- Return size - virtual label size() const - { - return mesh_.nInternalEdges(); - } + //- Return mapper size + virtual label size() const; //- Return size of field before mapping virtual label sizeBeforeMapping() const @@ -143,13 +144,13 @@ public: //- Return interpolation weights virtual const scalarListList& weights() const; - //- Are there any inserted faces + //- Are there any inserted edges virtual bool insertedObjects() const { return false; } - //- Return list of inserted faces + //- Return list of inserted edges virtual const labelList& insertedObjectLabels() const { return labelList::null(); diff --git a/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.C b/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.C index aafe6d44f16..44ee8f6beb7 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.C +++ b/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.C @@ -40,9 +40,9 @@ Foam::faMeshMapper::faMeshMapper nOldEdges_(mesh.nEdges()), nOldInternalEdges_(mesh.nInternalEdges()), nOldFaces_(mesh.nFaces()), - oldPatchSizes_(mesh.boundary().size(), 0), - oldPatchStarts_(mesh.boundary().size(), -1), - oldPatchEdgeFaces_(mesh.boundary().size()), + oldPatchSizes_(), + oldPatchStarts_(), + oldPatchEdgeFaces_(), areaMap_(mesh, mpm), edgeMap_(mesh, mpm), boundaryMap_(mesh, mpm) @@ -50,6 +50,10 @@ Foam::faMeshMapper::faMeshMapper // Capture old patch information const faBoundaryMesh& patches = mesh.boundary(); + oldPatchSizes_.resize(patches.size()); + oldPatchStarts_.resize(patches.size()); + oldPatchEdgeFaces_.resize(patches.size()); + forAll(patches, patchI) { oldPatchSizes_[patchI] = patches[patchI].size(); diff --git a/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.H b/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.H index 16760b12e67..fcdc355b8f3 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.H +++ b/src/finiteArea/faMesh/faMeshMapper/faMeshMapper.H @@ -45,8 +45,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef faMeshMapper_H -#define faMeshMapper_H +#ifndef Foam_faMeshMapper_H +#define Foam_faMeshMapper_H #include "faceMapper.H" #include "faAreaMapper.H" @@ -58,7 +58,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class faMesh; class mapPolyMesh; diff --git a/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.C b/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.C index 69aa3177f66..88f8393b54a 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.C +++ b/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.C @@ -27,7 +27,6 @@ License #include "faPatchMapper.H" #include "mapPolyMesh.H" -#include "demandDrivenData.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -44,7 +43,7 @@ void Foam::faPatchMapper::calcAddressing() const hasUnmapped_ = false; directAddrPtr_ = std::make_unique<labelList>(patch_.size(), Foam::zero{}); - labelList& addr = *directAddrPtr_; + auto& addr = *directAddrPtr_; // Make a map of old edgeFaces, giving edge index in patch given the new // face label next to the patch @@ -68,9 +67,11 @@ void Foam::faPatchMapper::calcAddressing() const forAll(ef, efI) { - if (edgeIndexLookup.found(ef[efI])) + const auto iter = edgeIndexLookup.cfind(ef[efI]); + + if (iter.good()) { - addr[efI] = edgeIndexLookup[ef[efI]]; + addr[efI] = iter.val(); } else { @@ -84,11 +85,11 @@ void Foam::faPatchMapper::calcAddressing() const } -void Foam::faPatchMapper::clearOut() -{ - directAddrPtr_.reset(nullptr); - hasUnmapped_ = false; -} +// void Foam::faPatchMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// hasUnmapped_ = false; +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -110,9 +111,7 @@ Foam::faPatchMapper::faPatchMapper // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::faPatchMapper::~faPatchMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // diff --git a/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.H b/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.H index b27bd6874df..cfaf63f44e0 100644 --- a/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.H +++ b/src/finiteArea/faMesh/faMeshMapper/faPatchMapper.H @@ -39,8 +39,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef faPatchMapper_H -#define faPatchMapper_H +#ifndef Foam_faPatchMapper_H +#define Foam_faPatchMapper_H #include "faPatchFieldMapper.H" #include "faceMapper.H" @@ -51,7 +51,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class faPatch; class mapPolyMesh; @@ -78,7 +78,7 @@ class faPatchMapper const labelList oldEdgeFaces_; - // Demand-driven private data + // Demand-Driven Data mutable bool hasUnmapped_; @@ -91,9 +91,6 @@ class faPatchMapper //- Calculate addressing for mapping with inserted cells void calcAddressing() const; - //- Clear out local storage - void clearOut(); - public: @@ -106,12 +103,10 @@ public: void operator=(const faPatchMapper&) = delete; - //- Construct from mappers - faPatchMapper - ( - const faPatch& patch, - const mapPolyMesh& mpm - ); + // Constructors + + //- Construct from mappers + faPatchMapper(const faPatch& patch, const mapPolyMesh& mpm); //- Destructor diff --git a/src/finiteVolume/fvMesh/fvMeshMapper/fvBoundaryMeshMapper.H b/src/finiteVolume/fvMesh/fvMeshMapper/fvBoundaryMeshMapper.H index e533354e4d1..266d104958f 100644 --- a/src/finiteVolume/fvMesh/fvMeshMapper/fvBoundaryMeshMapper.H +++ b/src/finiteVolume/fvMesh/fvMeshMapper/fvBoundaryMeshMapper.H @@ -31,8 +31,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef fvBoundaryMeshMapper_H -#define fvBoundaryMeshMapper_H +#ifndef Foam_fvBoundaryMeshMapper_H +#define Foam_fvBoundaryMeshMapper_H #include "PtrList.H" #include "fvPatchMapper.H" diff --git a/src/finiteVolume/fvMesh/fvMeshMapper/fvMeshMapper.H b/src/finiteVolume/fvMesh/fvMeshMapper/fvMeshMapper.H index 671a8cb59db..6e9deeefbfd 100644 --- a/src/finiteVolume/fvMesh/fvMeshMapper/fvMeshMapper.H +++ b/src/finiteVolume/fvMesh/fvMeshMapper/fvMeshMapper.H @@ -34,8 +34,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef fvMeshMapper_H -#define fvMeshMapper_H +#ifndef Foam_fvMeshMapper_H +#define Foam_fvMeshMapper_H #include "faceMapper.H" #include "cellMapper.H" @@ -47,7 +47,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class fvMesh; class mapPolyMesh; @@ -57,7 +57,7 @@ class mapPolyMesh; class fvMeshMapper { - // Private data + // Private Data //- Reference to mesh const fvMesh& mesh_; diff --git a/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.C b/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.C index 071c3890ad0..ce16d5ce4ed 100644 --- a/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.C +++ b/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,7 +32,6 @@ License #include "fvMesh.H" #include "mapPolyMesh.H" #include "faceMapper.H" -#include "demandDrivenData.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -41,7 +40,7 @@ void Foam::fvPatchMapper::calcAddressing() const if ( directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ ) { @@ -63,14 +62,14 @@ void Foam::fvPatchMapper::calcAddressing() const if (direct()) { // Direct mapping - slice to size - directAddrPtr_ = new labelList + directAddrPtr_ = std::make_unique<labelList> ( patch_.patchSlice ( static_cast<const labelList&>(faceMap_.directAddressing()) ) ); - labelList& addr = *directAddrPtr_; + auto& addr = *directAddrPtr_; // Adjust mapping to manage hits into other patches and into // internal @@ -106,26 +105,25 @@ void Foam::fvPatchMapper::calcAddressing() const else { // Interpolative mapping - interpolationAddrPtr_ = - new labelListList - ( - patch_.patchSlice(faceMap_.addressing()) - ); - labelListList& addr = *interpolationAddrPtr_; + interpAddrPtr_ = std::make_unique<labelListList> + ( + patch_.patchSlice(faceMap_.addressing()) + ); + auto& addr = *interpAddrPtr_; - weightsPtr_ = - new scalarListList - ( - patch_.patchSlice(faceMap_.weights()) - ); - scalarListList& w = *weightsPtr_; + weightsPtr_ = std::make_unique<scalarListList> + ( + patch_.patchSlice(faceMap_.weights()) + ); + auto& wght = *weightsPtr_; // Adjust mapping to manage hits into other patches and into // internal + forAll(addr, facei) { - labelList& curAddr = addr[facei]; - scalarList& curW = w[facei]; + auto& curAddr = addr[facei]; + auto& curWght = wght[facei]; if ( @@ -142,44 +140,42 @@ void Foam::fvPatchMapper::calcAddressing() const else { // Need to recalculate weights to exclude hits into internal - labelList newAddr(curAddr.size()); - scalarField newWeights(curAddr.size()); label nActive = 0; scalar sumWeight = 0; - forAll(curAddr, lfI) + forAll(curAddr, i) { if ( - curAddr[lfI] >= oldPatchStart - && curAddr[lfI] < oldPatchEnd + curAddr[i] >= oldPatchStart + && curAddr[i] < oldPatchEnd ) { - newAddr[nActive] = curAddr[lfI] - oldPatchStart; - newWeights[nActive] = curW[lfI]; + curAddr[nActive] = curAddr[i] - oldPatchStart; + curWght[nActive] = curWght[i]; - sumWeight += curW[lfI]; + sumWeight += curWght[i]; ++nActive; } } - newAddr.resize(nActive); - newWeights.resize(nActive); + // Reset addressing and weights + curAddr.resize(nActive); + curWght.resize(nActive); // Re-scale the weights - if (nActive > 0) + if (nActive) { - newWeights /= sumWeight; + for (auto& w : curWght) + { + w /= sumWeight; + } } else { hasUnmapped_ = true; } - - // Reset addressing and weights - curAddr = newAddr; - curW = newWeights; } } @@ -200,13 +196,13 @@ void Foam::fvPatchMapper::calcAddressing() const } -void Foam::fvPatchMapper::clearOut() -{ - deleteDemandDrivenData(directAddrPtr_); - deleteDemandDrivenData(interpolationAddrPtr_); - deleteDemandDrivenData(weightsPtr_); - hasUnmapped_ = false; -} +// void Foam::fvPatchMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// hasUnmapped_ = false; +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -220,19 +216,14 @@ Foam::fvPatchMapper::fvPatchMapper patch_(patch), faceMap_(faceMap), sizeBeforeMapping_(faceMap.oldPatchSizes()[patch_.index()]), - hasUnmapped_(false), - directAddrPtr_(nullptr), - interpolationAddrPtr_(nullptr), - weightsPtr_(nullptr) + hasUnmapped_(false) {} // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::fvPatchMapper::~fvPatchMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -264,12 +255,12 @@ const Foam::labelListList& Foam::fvPatchMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } diff --git a/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.H b/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.H index 511cae83fe4..babc8d2ac73 100644 --- a/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.H +++ b/src/finiteVolume/fvMesh/fvMeshMapper/fvPatchMapper.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,8 +35,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef fvPatchMapper_H -#define fvPatchMapper_H +#ifndef Foam_fvPatchMapper_H +#define Foam_fvPatchMapper_H #include "fvPatchFieldMapper.H" #include "faceMapper.H" @@ -47,7 +48,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class fvPatch; class mapPolyMesh; @@ -71,18 +72,18 @@ class fvPatchMapper const label sizeBeforeMapping_; - // Demand-driven private data + // Demand-Driven Data mutable bool hasUnmapped_; //- Direct addressing (only one for of addressing is used) - mutable labelList* directAddrPtr_; + mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one for of addressing is used) - mutable labelListList* interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights - mutable scalarListList* weightsPtr_; + mutable std::unique_ptr<scalarListList> weightsPtr_; // Private Member Functions @@ -97,14 +98,9 @@ class fvPatchMapper //- Calculate addressing for mapping with inserted cells void calcAddressing() const; - //- Clear out local storage - void clearOut(); - public: - // Static data members - // Constructors //- Construct from mappers @@ -152,7 +148,6 @@ public: //- Return interpolation weights virtual const scalarListList& weights() const; - }; diff --git a/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.C b/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.C index a03873f01a6..6767d10fa18 100644 --- a/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.C +++ b/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -30,7 +30,6 @@ License #include "fvMesh.H" #include "mapPolyMesh.H" #include "faceMapper.H" -#include "demandDrivenData.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -39,9 +38,9 @@ void Foam::fvSurfaceMapper::calcAddressing() const if ( directAddrPtr_ - || interpolationAddrPtr_ + || interpAddrPtr_ || weightsPtr_ - || insertedObjectLabelsPtr_ + || insertedObjectsPtr_ ) { FatalErrorInFunction @@ -57,12 +56,11 @@ void Foam::fvSurfaceMapper::calcAddressing() const if (direct()) { // Direct mapping - slice to size - directAddrPtr_ = - new labelList - ( - labelList::subList(faceMap_.directAddressing(), size()) - ); - labelList& addr = *directAddrPtr_; + directAddrPtr_ = std::make_unique<labelList> + ( + labelList::subList(faceMap_.directAddressing(), size()) + ); + auto& addr = *directAddrPtr_; // Adjust for creation of an internal face from a boundary face forAll(addr, facei) @@ -76,71 +74,64 @@ void Foam::fvSurfaceMapper::calcAddressing() const else { // Interpolative mapping - slice to size - interpolationAddrPtr_ = - new labelListList - ( - labelListList::subList(faceMap_.addressing(), size()) - ); - labelListList& addr = *interpolationAddrPtr_; - - weightsPtr_ = - new scalarListList - ( - scalarListList::subList(faceMap_.weights(), size()) - ); - scalarListList& w = *weightsPtr_; + interpAddrPtr_ = std::make_unique<labelListList> + ( + labelListList::subList(faceMap_.addressing(), size()) + ); + auto& addr = *interpAddrPtr_; + + weightsPtr_ = std::make_unique<scalarListList> + ( + scalarListList::subList(faceMap_.weights(), size()) + ); + auto& wght = *weightsPtr_; // Adjust for creation of an internal face from a boundary face forAll(addr, facei) { if (max(addr[facei]) >= oldNInternal) { - addr[facei] = labelList(1, Zero); - w[facei] = scalarList(1, scalar(1)); + addr[facei] = labelList(1, Foam::zero{}); + wght[facei] = scalarList(1, scalar(1)); } } } // Inserted objects + insertedObjectsPtr_ = std::make_unique<labelList>(); + auto& inserted = *insertedObjectsPtr_; + // If there are, assemble the labels - if (insertedObjects()) + if (faceMap_.insertedObjects()) { const labelList& insFaces = faceMap_.insertedObjectLabels(); - insertedObjectLabelsPtr_ = new labelList(insFaces.size()); - labelList& ins = *insertedObjectLabelsPtr_; - - label nIns = 0; + inserted.resize(insFaces.size()); - forAll(insFaces, facei) + label count = 0; + for (const label facei : insFaces) { // If the face is internal, keep it here - if (insFaces[facei] < size()) + if (facei < size()) { - ins[nIns] = insFaces[facei]; - nIns++; + inserted[count] = facei; + ++count; } } - ins.setSize(nIns); - } - else - { - // No inserted objects - insertedObjectLabelsPtr_ = new labelList(0); + inserted.resize(count); } } -void Foam::fvSurfaceMapper::clearOut() -{ - deleteDemandDrivenData(directAddrPtr_); - deleteDemandDrivenData(interpolationAddrPtr_); - deleteDemandDrivenData(weightsPtr_); - - deleteDemandDrivenData(insertedObjectLabelsPtr_); -} +// void Foam::fvSurfaceMapper::clearOut() +// { +// directAddrPtr_.reset(nullptr); +// interpAddrPtr_.reset(nullptr); +// weightsPtr_.reset(nullptr); +// insertedObjectsPtr_.reset(nullptr); +// } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -148,24 +139,18 @@ void Foam::fvSurfaceMapper::clearOut() Foam::fvSurfaceMapper::fvSurfaceMapper ( const fvMesh& mesh, - const faceMapper& fMapper + const faceMapper& mapper ) : mesh_(mesh), - faceMap_(fMapper), - directAddrPtr_(nullptr), - interpolationAddrPtr_(nullptr), - weightsPtr_(nullptr), - insertedObjectLabelsPtr_(nullptr) + faceMap_(mapper) {} // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::fvSurfaceMapper::~fvSurfaceMapper() -{ - clearOut(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -197,12 +182,12 @@ const Foam::labelListList& Foam::fvSurfaceMapper::addressing() const << abort(FatalError); } - if (!interpolationAddrPtr_) + if (!interpAddrPtr_) { calcAddressing(); } - return *interpolationAddrPtr_; + return *interpAddrPtr_; } @@ -226,22 +211,13 @@ const Foam::scalarListList& Foam::fvSurfaceMapper::weights() const const Foam::labelList& Foam::fvSurfaceMapper::insertedObjectLabels() const { - if (!insertedObjectLabelsPtr_) + if (!insertedObjectsPtr_) { calcAddressing(); } - return *insertedObjectLabelsPtr_; + return *insertedObjectsPtr_; } -// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // - - // ************************************************************************* // diff --git a/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.H b/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.H index f2f1a0381ce..56b88cfabb0 100644 --- a/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.H +++ b/src/finiteVolume/fvMesh/fvMeshMapper/fvSurfaceMapper.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,15 +28,15 @@ Class Foam::fvSurfaceMapper Description - FV surface mapper. + A finiteVolume surface mapper. SourceFiles fvSurfaceMapper.C \*---------------------------------------------------------------------------*/ -#ifndef fvSurfaceMapper_H -#define fvSurfaceMapper_H +#ifndef Foam_fvSurfaceMapper_H +#define Foam_fvSurfaceMapper_H #include "morphFieldMapper.H" #include "fvMesh.H" @@ -48,8 +49,6 @@ SourceFiles namespace Foam { -// Forward declaration of classes - /*---------------------------------------------------------------------------*\ Class fvSurfaceMapper Declaration \*---------------------------------------------------------------------------*/ @@ -58,7 +57,7 @@ class fvSurfaceMapper : public morphFieldMapper { - // Private data + // Private Data //- Reference to mesh const fvMesh& mesh_; @@ -67,19 +66,19 @@ class fvSurfaceMapper const faceMapper& faceMap_; - // Demand-driven private data + // Demand-Driven Data //- Direct addressing (only one for of addressing is used) - mutable labelList* directAddrPtr_; + mutable std::unique_ptr<labelList> directAddrPtr_; //- Interpolated addressing (only one for of addressing is used) - mutable labelListList* interpolationAddrPtr_; + mutable std::unique_ptr<labelListList> interpAddrPtr_; //- Interpolation weights - mutable scalarListList* weightsPtr_; + mutable std::unique_ptr<scalarListList> weightsPtr_; //- Inserted faces - mutable labelList* insertedObjectLabelsPtr_; + mutable std::unique_ptr<labelList> insertedObjectsPtr_; @@ -95,9 +94,6 @@ class fvSurfaceMapper //- Calculate addressing void calcAddressing() const; - //- Clear out local storage - void clearOut(); - public: @@ -107,7 +103,7 @@ public: fvSurfaceMapper ( const fvMesh& mesh, - const faceMapper& fMapper + const faceMapper& mapper ); -- GitLab