From a7e37656e896b078077491026bc6d570d473c2be Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 11 Jul 2018 08:50:38 +0200
Subject: [PATCH] ENH: reduce storage requirement for volumeType

- char instead of integer representation for the enumeration.

- additional constructor as lookupOrDefault from dictionary entry.
---
 .../conformationSurfaces.C                    |  5 ++-
 .../algorithms/indexedOctree/volumeType.C     | 32 ++++++++++++++---
 .../algorithms/indexedOctree/volumeType.H     | 33 +++++++++++------
 .../refinementSurfaces/refinementSurfaces.C   | 26 +++++---------
 .../shellSurfaces/shellSurfaces.C             | 36 ++++++-------------
 .../searchableSurfacesQueries.C               |  3 +-
 6 files changed, 70 insertions(+), 65 deletions(-)

diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformationSurfaces/conformationSurfaces.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformationSurfaces/conformationSurfaces.C
index 761b5a5fea3..97d8b2dc41a 100644
--- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformationSurfaces/conformationSurfaces.C
+++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformationSurfaces/conformationSurfaces.C
@@ -32,7 +32,7 @@ License
 
 namespace Foam
 {
-defineTypeNameAndDebug(conformationSurfaces, 0);
+    defineTypeNameAndDebug(conformationSurfaces, 0);
 }
 
 
@@ -71,8 +71,7 @@ void Foam::conformationSurfaces::hasBoundedVolume
 
             referenceVolumeTypes[s] = vTypes[0];
 
-            Info<< "    is "
-                << volumeType::names[referenceVolumeTypes[s]]
+            Info<< "    is " << referenceVolumeTypes[s].str()
                 << " surface " << surface.name()
                 << endl;
         }
diff --git a/src/OpenFOAM/algorithms/indexedOctree/volumeType.C b/src/OpenFOAM/algorithms/indexedOctree/volumeType.C
index beabdfb6147..6fbc4dcc0a5 100644
--- a/src/OpenFOAM/algorithms/indexedOctree/volumeType.C
+++ b/src/OpenFOAM/algorithms/indexedOctree/volumeType.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -24,6 +24,7 @@ License
 \*---------------------------------------------------------------------------*/
 
 #include "volumeType.H"
+#include "dictionary.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -34,12 +35,33 @@ const Foam::Enum
 Foam::volumeType::names
 {
     { type::UNKNOWN, "unknown" },
-    { type::MIXED, "mixed" },
     { type::INSIDE, "inside" },
     { type::OUTSIDE, "outside" },
+    { type::MIXED, "mixed" },
 };
 
 
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
+
+Foam::volumeType::volumeType
+(
+    const word& key,
+    const dictionary& dict,
+    const type deflt
+)
+:
+    t_(names.lookupOrDefault(key, dict, deflt))
+{}
+
+
+// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
+
+const Foam::word& Foam::volumeType::str() const
+{
+    return names[t_];
+}
+
+
 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
 
 Foam::Istream& Foam::operator>>(Istream& is, volumeType& vt)
@@ -47,10 +69,10 @@ Foam::Istream& Foam::operator>>(Istream& is, volumeType& vt)
     // Read beginning of volumeType
     is.readBegin("volumeType");
 
-    int type;
-    is  >> type;
+    int val;
+    is  >> val;
 
-    vt.t_ = static_cast<volumeType::type>(type);
+    vt.t_ = static_cast<volumeType::type>(val);
 
     // Read end of volumeType
     is.readEnd("volumeType");
diff --git a/src/OpenFOAM/algorithms/indexedOctree/volumeType.H b/src/OpenFOAM/algorithms/indexedOctree/volumeType.H
index 56c6c93f2e2..4e66188fdd3 100644
--- a/src/OpenFOAM/algorithms/indexedOctree/volumeType.H
+++ b/src/OpenFOAM/algorithms/indexedOctree/volumeType.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2014 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2015 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2015-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -25,6 +25,8 @@ Class
     Foam::volumeType
 
 Description
+    An enumeration wrapper for classification of a location as being
+    inside/outside of a volume.
 
 SourceFiles
     volumeType.C
@@ -41,8 +43,8 @@ SourceFiles
 namespace Foam
 {
 
-// Forward declaration of friend functions and operators
-
+// Forward declarations
+class dictionary;
 class volumeType;
 Istream& operator>>(Istream& is, volumeType& vt);
 Ostream& operator<<(Ostream& os, const volumeType& vt);
@@ -56,16 +58,18 @@ class volumeType
 {
 public:
 
-    //- Volume types
-    enum type
+    //- Volume classification types
+    enum type : char
     {
-        UNKNOWN = 0,
-        MIXED   = 1,
-        INSIDE  = 2,
-        OUTSIDE = 3
+        UNKNOWN = 0,    //!< Unknown state
+        INSIDE  = 0x1,  //!< A location inside the volume
+        OUTSIDE = 0x2,  //!< A location outside the volume
+        MIXED   = 0x3   //!< A location that is partly inside and outside
     };
 
     // Static data
+
+    //- Names for the classification enumeration
     static const Enum<volumeType> names;
 
 
@@ -81,18 +85,21 @@ public:
 
     // Constructors
 
-        //- Construct null
+        //- Construct null as UNKNOWN state
         volumeType()
         :
             t_(UNKNOWN)
         {}
 
-        //- Construct from components
+        //- Construct from enumeration
         volumeType(type t)
         :
             t_(t)
         {}
 
+        //- Construct as lookupOrDefault by name from dictionary
+        volumeType(const word& key, const dictionary& dict, const type deflt);
+
         //- Construct from integer
         explicit volumeType(const int t)
         :
@@ -102,11 +109,15 @@ public:
 
     // Member Functions
 
+        //- Return the enumeration
         operator type() const
         {
             return t_;
         }
 
+        //- The string representation of the volume type enumeration
+        const word& str() const;
+
 
     // IOstream operators
 
diff --git a/src/mesh/snappyHexMesh/refinementSurfaces/refinementSurfaces.C b/src/mesh/snappyHexMesh/refinementSurfaces/refinementSurfaces.C
index 3afb8fedb82..f693ca674a8 100644
--- a/src/mesh/snappyHexMesh/refinementSurfaces/refinementSurfaces.C
+++ b/src/mesh/snappyHexMesh/refinementSurfaces/refinementSurfaces.C
@@ -227,14 +227,9 @@ Foam::refinementSurfaces::refinementSurfaces
                 "gapLevel",
                 nullGapLevel
             );
-            globalGapMode[surfI] = volumeType::names
-            [
-                dict.lookupOrDefault<word>
-                (
-                    "gapMode",
-                    volumeType::names[volumeType::MIXED]
-                )
-            ];
+            globalGapMode[surfI] =
+                volumeType("gapMode", dict, volumeType::MIXED);
+
             if
             (
                 globalGapMode[surfI] == volumeType::UNKNOWN
@@ -248,7 +243,7 @@ Foam::refinementSurfaces::refinementSurfaces
                     << "Illegal gapLevel specification for surface "
                     << names_[surfI]
                     << " : gapLevel:" << globalGapLevel[surfI]
-                    << " gapMode:" << volumeType::names[globalGapMode[surfI]]
+                    << " gapMode:" << globalGapMode[surfI].str()
                     << exit(FatalIOError);
             }
 
@@ -327,14 +322,9 @@ Foam::refinementSurfaces::refinementSurfaces
                         regionGapLevel[surfI].insert(regionI, gapSpec);
                         volumeType gapModeSpec
                         (
-                            volumeType::names
-                            [
-                                regionDict.lookupOrDefault<word>
-                                (
-                                    "gapMode",
-                                    volumeType::names[volumeType::MIXED]
-                                )
-                            ]
+                            "gapMode",
+                            regionDict,
+                            volumeType::MIXED
                         );
                         regionGapMode[surfI].insert(regionI, gapModeSpec);
                         if
@@ -350,7 +340,7 @@ Foam::refinementSurfaces::refinementSurfaces
                                 << "Illegal gapLevel specification for surface "
                                 << names_[surfI]
                                 << " : gapLevel:" << gapSpec
-                                << " gapMode:" << volumeType::names[gapModeSpec]
+                                << " gapMode:" << gapModeSpec.str()
                                 << exit(FatalIOError);
                         }
 
diff --git a/src/mesh/snappyHexMesh/shellSurfaces/shellSurfaces.C b/src/mesh/snappyHexMesh/shellSurfaces/shellSurfaces.C
index d6e2cba4923..ea8c00447a1 100644
--- a/src/mesh/snappyHexMesh/shellSurfaces/shellSurfaces.C
+++ b/src/mesh/snappyHexMesh/shellSurfaces/shellSurfaces.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2015-2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2015-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -682,19 +682,9 @@ Foam::shellSurfaces::shellSurfaces
             extendedGapLevel_[shellI].setSize(regionNames.size());
             extendedGapLevel_[shellI] = gapSpec;
 
-            volumeType gapModeSpec
-            (
-                volumeType::names
-                [
-                    dict.lookupOrDefault<word>
-                    (
-                        "gapMode",
-                        volumeType::names[volumeType::MIXED]
-                    )
-                ]
-            );
             extendedGapMode_[shellI].setSize(regionNames.size());
-            extendedGapMode_[shellI] = gapModeSpec;
+            extendedGapMode_[shellI] =
+                volumeType("gapMode", dict, volumeType::MIXED);
 
 
             // Override on a per-region basis?
@@ -721,23 +711,17 @@ Foam::shellSurfaces::shellSurfaces
                         );
                         extendedGapLevel_[shellI][regionI] = gapSpec;
 
-                        volumeType gapModeSpec
-                        (
-                            volumeType::names
-                            [
-                                regionDict.lookupOrDefault<word>
-                                (
-                                    "gapMode",
-                                    volumeType::names[volumeType::MIXED]
-                                )
-                            ]
-                        );
-                        extendedGapMode_[shellI][regionI] = gapModeSpec;
+                        extendedGapMode_[shellI][regionI] =
+                            volumeType
+                            (
+                                "gapMode",
+                                regionDict,
+                                volumeType::MIXED
+                            );
                     }
                 }
             }
 
-
             checkGapLevels(dict, shellI, extendedGapLevel_[shellI]);
 
             shellI++;
diff --git a/src/meshTools/searchableSurfaces/searchableSurfacesQueries/searchableSurfacesQueries.C b/src/meshTools/searchableSurfaces/searchableSurfacesQueries/searchableSurfacesQueries.C
index 4020fb3dc08..f5c48914a27 100644
--- a/src/meshTools/searchableSurfaces/searchableSurfacesQueries/searchableSurfacesQueries.C
+++ b/src/meshTools/searchableSurfaces/searchableSurfacesQueries/searchableSurfacesQueries.C
@@ -644,8 +644,7 @@ void Foam::searchableSurfacesQueries::signedDistance
                             << " point:" << surfPoints[i]
                             << " surface:"
                             << allSurfaces[surfacesToTest[testI]].name()
-                            << " volType:"
-                            << volumeType::names[vT]
+                            << " volType:" << vT.str()
                             << exit(FatalError);
                         break;
                     }
-- 
GitLab