From 42bcc328f33dbb17f2fecf6581e1d560b193b0a6 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 31 Oct 2018 10:36:22 +0000
Subject: [PATCH] ENH: new special purpose bitSet-based topoSet (#1060)

---
 src/meshTools/Make/files                 |   1 +
 src/meshTools/sets/topoSets/cellBitSet.C | 179 +++++++++++++++++++++++
 src/meshTools/sets/topoSets/cellBitSet.H | 160 ++++++++++++++++++++
 src/meshTools/sets/topoSets/topoSet.C    |   6 +
 src/meshTools/sets/topoSets/topoSet.H    |   3 +
 5 files changed, 349 insertions(+)
 create mode 100644 src/meshTools/sets/topoSets/cellBitSet.C
 create mode 100644 src/meshTools/sets/topoSets/cellBitSet.H

diff --git a/src/meshTools/Make/files b/src/meshTools/Make/files
index 5441f3077d..581e919333 100644
--- a/src/meshTools/Make/files
+++ b/src/meshTools/Make/files
@@ -134,6 +134,7 @@ searchableSurfaces/subTriSurfaceMesh/subTriSurfaceMesh.C
 searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C
 
 topoSets = sets/topoSets
+$(topoSets)/cellBitSet.C
 $(topoSets)/cellSet.C
 $(topoSets)/topoSet.C
 $(topoSets)/faceSet.C
diff --git a/src/meshTools/sets/topoSets/cellBitSet.C b/src/meshTools/sets/topoSets/cellBitSet.C
new file mode 100644
index 0000000000..4838b13a60
--- /dev/null
+++ b/src/meshTools/sets/topoSets/cellBitSet.C
@@ -0,0 +1,179 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+\*---------------------------------------------------------------------------*/
+
+#include "cellBitSet.H"
+#include "polyMesh.H"
+#include "Time.H"
+
+// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
+
+namespace Foam
+{
+    defineTypeNameAndDebug(cellBitSet, 0);
+}
+
+
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
+
+Foam::cellBitSet::cellBitSet(const polyMesh& mesh)
+:
+    cellBitSet(mesh, false)
+{}
+
+
+Foam::cellBitSet::cellBitSet(const polyMesh& mesh, const bool val)
+:
+    topoSet
+    (
+        IOobject
+        (
+            "cellBitSet",
+            mesh.time().constant(),
+            mesh,
+            IOobject::NO_READ,
+            IOobject::NO_WRITE,
+            false
+        ),
+        0  // zero-sized (unallocated) labelHashSet
+    ),
+    selected_(mesh.nCells(), val)
+{}
+
+
+// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
+
+bool Foam::cellBitSet::found(const label id) const
+{
+    return selected_.test(id);
+}
+
+
+bool Foam::cellBitSet::set(const label id)
+{
+    return selected_.set(id);
+}
+
+
+bool Foam::cellBitSet::unset(const label id)
+{
+    return selected_.unset(id);
+}
+
+
+void Foam::cellBitSet::set(const labelUList& labels)
+{
+    selected_.set(labels);
+}
+
+
+void Foam::cellBitSet::unset(const labelUList& labels)
+{
+    selected_.unset(labels);
+}
+
+
+void Foam::cellBitSet::invert(const label maxLen)
+{
+    selected_.resize(maxLen);
+    selected_.flip();
+}
+
+
+void Foam::cellBitSet::subset(const topoSet& set)
+{
+    // Only retain entries found in both sets
+    if (isA<cellBitSet>(set))
+    {
+        selected_ &= refCast<const cellBitSet>(set).selected_;
+    }
+    else if (set.empty())
+    {
+        selected_.reset();
+    }
+    else
+    {
+        for (const label id : selected_)
+        {
+            if (!set.found(id))
+            {
+                selected_.unset(id);
+            }
+        }
+    }
+}
+
+
+void Foam::cellBitSet::addSet(const topoSet& set)
+{
+    // Add entries to the set
+    if (isA<cellBitSet>(set))
+    {
+        selected_ |= refCast<const cellBitSet>(set).selected_;
+    }
+    else
+    {
+        for (const label id : set)
+        {
+            selected_.set(id);
+        }
+    }
+}
+
+
+void Foam::cellBitSet::subtractSet(const topoSet& set)
+{
+    // Subtract entries from the set
+    if (isA<cellBitSet>(set))
+    {
+        selected_ -= refCast<const cellBitSet>(set).selected_;
+    }
+    else
+    {
+        for (const label id : set)
+        {
+            selected_.unset(id);
+        }
+    }
+}
+
+
+Foam::label Foam::cellBitSet::maxSize(const polyMesh& mesh) const
+{
+    return mesh.nCells();
+}
+
+
+void Foam::cellBitSet::writeDebug
+(
+    Ostream& os,
+    const primitiveMesh& mesh,
+    const label maxLen
+) const
+{
+    topoSet::writeDebug(os, mesh.cellCentres(), maxLen);
+}
+
+
+// ************************************************************************* //
diff --git a/src/meshTools/sets/topoSets/cellBitSet.H b/src/meshTools/sets/topoSets/cellBitSet.H
new file mode 100644
index 0000000000..ba64b40595
--- /dev/null
+++ b/src/meshTools/sets/topoSets/cellBitSet.H
@@ -0,0 +1,160 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Class
+    Foam::cellBitSet
+
+Description
+    A special purpose topoSet with the cell labels stored as a bitSet.
+    It does not correspond to a cellSet either (no associated IOobject).
+
+SourceFiles
+    cellBitSet.C
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef cellBitSet_H
+#define cellBitSet_H
+
+#include "topoSet.H"
+#include "bitSet.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+
+/*---------------------------------------------------------------------------*\
+                           Class cellBitSet Declaration
+\*---------------------------------------------------------------------------*/
+
+class cellBitSet
+:
+    public topoSet
+{
+    // Private data
+
+        bitSet selected_;
+
+public:
+
+    //- Runtime type information
+    TypeName("cellBitSet");
+
+
+    // Constructors
+
+        //- Construct with nCells elements, all elements unset
+        explicit cellBitSet(const polyMesh& mesh);
+
+        //- Construct with nCells elements, using initial val
+        cellBitSet(const polyMesh& mesh, const bool val);
+
+
+    //- Destructor
+    virtual ~cellBitSet() = default;
+
+
+    // Member Functions
+
+        //- Return the bitSet
+        const bitSet& addressing() const
+        {
+            return selected_;
+        }
+
+        //- Access the bitSet
+        bitSet& addressing()
+        {
+            return selected_;
+        }
+
+        //- Set values to false, leaving the size untouched
+        void reset()
+        {
+            selected_.reset();
+        }
+
+        //- Has the given index?
+        virtual bool found(const label id) const;
+
+        //- Set an index
+        virtual bool set(const label id);
+
+        //- Unset an index
+        virtual bool unset(const label id);
+
+        //- Set multiple indices
+        virtual void set(const labelUList& labels);
+
+        //- Unset multiple indices
+        virtual void unset(const labelUList& labels);
+
+        //- Invert contents.
+        //  Insert all members [0,maxLen) which were not in set.
+        virtual void invert(const label maxLen);
+
+        //- Subset contents. Only elements present in both sets remain.
+        virtual void subset(const topoSet& set);
+
+        //- Add elements present in set.
+        virtual void addSet(const topoSet& set);
+
+        //- Subtract elements present in set.
+        virtual void subtractSet(const topoSet& set);
+
+        //- Sync cellBitSet across coupled patches.
+        virtual void sync(const polyMesh& mesh)
+        {}
+
+        //- Return max index+1.
+        virtual label maxSize(const polyMesh& mesh) const;
+
+        //- Update any stored data for new labels.
+        virtual void updateMesh(const mapPolyMesh& morphMap)
+        {}
+
+        //- Update any stored data for mesh redistribution.
+        virtual void distribute(const mapDistributePolyMesh& map)
+        {}
+
+        //- Write maxLen items with label and coordinates.
+        virtual void writeDebug
+        (
+            Ostream& os,
+            const primitiveMesh& mesh,
+            const label maxLen
+        ) const;
+
+};
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/meshTools/sets/topoSets/topoSet.C b/src/meshTools/sets/topoSets/topoSet.C
index a7ddabcb7b..040eec3c0d 100644
--- a/src/meshTools/sets/topoSets/topoSet.C
+++ b/src/meshTools/sets/topoSets/topoSet.C
@@ -491,6 +491,12 @@ Foam::topoSet::topoSet(const IOobject& io, labelHashSet&& labels)
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
+bool Foam::topoSet::found(const label id) const
+{
+    return static_cast<const labelHashSet&>(*this).found(id);
+}
+
+
 bool Foam::topoSet::set(const label id)
 {
     return static_cast<labelHashSet&>(*this).set(id);
diff --git a/src/meshTools/sets/topoSets/topoSet.H b/src/meshTools/sets/topoSets/topoSet.H
index 7f4e1e9589..3fa955ab7b 100644
--- a/src/meshTools/sets/topoSets/topoSet.H
+++ b/src/meshTools/sets/topoSets/topoSet.H
@@ -313,6 +313,9 @@ public:
 
     // Member Functions
 
+        //- Has the given index?
+        virtual bool found(const label id) const;
+
         //- Set an index
         virtual bool set(const label id);
 
-- 
GitLab