From d842118994eae513e6d92c16f066bb329f99c0da Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 4 Dec 2018 13:22:59 +0100
Subject: [PATCH] ENH: add cellSet support for volRegion (#1106)

---
 .../functionObjects/volRegion/volRegion.C     | 76 +++++++++++++------
 .../functionObjects/volRegion/volRegion.H     | 35 +++++----
 2 files changed, 74 insertions(+), 37 deletions(-)

diff --git a/src/finiteVolume/functionObjects/volRegion/volRegion.C b/src/finiteVolume/functionObjects/volRegion/volRegion.C
index 1f4779afebd..c39c9cf8c5f 100644
--- a/src/finiteVolume/functionObjects/volRegion/volRegion.C
+++ b/src/finiteVolume/functionObjects/volRegion/volRegion.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -25,6 +25,7 @@ License
 
 #include "volRegion.H"
 #include "volMesh.H"
+#include "cellSet.H"
 #include "globalMeshData.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@@ -44,8 +45,9 @@ const Foam::Enum
 >
 Foam::functionObjects::volRegion::regionTypeNames_
 ({
-    { regionTypes::vrtCellZone, "cellZone" },
     { regionTypes::vrtAll, "all" },
+    { regionTypes::vrtCellSet, "cellSet" },
+    { regionTypes::vrtCellZone, "cellZone" },
 });
 
 
@@ -73,7 +75,7 @@ Foam::functionObjects::volRegion::volRegion
     const dictionary& dict
 )
 :
-    mesh_(mesh),
+    volMesh_(mesh),
     regionType_
     (
         regionTypeNames_.lookupOrDefault
@@ -83,7 +85,7 @@ Foam::functionObjects::volRegion::volRegion
             regionTypes::vrtAll
         )
     ),
-    regionName_(polyMesh::defaultRegion),
+    regionName_(volMesh_.name()),
     regionID_(-1)
 {
     read(dict);
@@ -94,12 +96,6 @@ Foam::functionObjects::volRegion::volRegion
 }
 
 
-// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
-
-Foam::functionObjects::volRegion::~volRegion()
-{}
-
-
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 bool Foam::functionObjects::volRegion::read
@@ -107,19 +103,41 @@ bool Foam::functionObjects::volRegion::read
     const dictionary& dict
 )
 {
+    regionID_ = -1;
+    cellIds_.clear();
+
     switch (regionType_)
     {
+        case vrtCellSet:
+        {
+            dict.readEntry("name", regionName_);
+
+            cellIds_ = cellSet(volMesh_, regionName_).sortedToc();
+
+            if (nCells() == 0)
+            {
+                FatalIOErrorInFunction(dict)
+                    << regionTypeNames_[regionType_]
+                    << "(" << regionName_ << "):" << nl
+                    << "    Region has no cells"
+                    << exit(FatalIOError);
+            }
+
+            break;
+        }
+
         case vrtCellZone:
         {
             dict.readEntry("name", regionName_);
 
-            regionID_ = mesh_.cellZones().findZoneID(regionName_);
+            regionID_ = volMesh_.cellZones().findZoneID(regionName_);
 
             if (regionID_ < 0)
             {
                 FatalIOErrorInFunction(dict)
                     << "Unknown cell zone name: " << regionName_
-                    << ". Valid cell zones are: " << mesh_.cellZones().names()
+                    << ". Valid cell zones    : "
+                    << flatOutput(volMesh_.cellZones().names())
                     << exit(FatalIOError);
             }
 
@@ -137,6 +155,7 @@ bool Foam::functionObjects::volRegion::read
 
         case vrtAll:
         {
+            regionName_= volMesh_.name();
             break;
         }
 
@@ -144,7 +163,7 @@ bool Foam::functionObjects::volRegion::read
         {
             FatalIOErrorInFunction(dict)
                 << "Unknown region type. Valid region types are:"
-                << regionTypeNames_.sortedToc()
+                << flatOutput(regionTypeNames_.names()) << nl
                 << exit(FatalIOError);
         }
     }
@@ -155,14 +174,21 @@ bool Foam::functionObjects::volRegion::read
 
 const Foam::labelList& Foam::functionObjects::volRegion::cellIDs() const
 {
-    if (regionType_ == vrtAll)
-    {
-        return labelList::null();
-    }
-    else
+    switch (regionType_)
     {
-        return mesh_.cellZones()[regionID_];
+        case vrtCellSet:
+            return cellIds_;
+            break;
+
+        case vrtCellZone:
+            return volMesh_.cellZones()[regionID_];
+            break;
+
+        default:
+            break;
     }
+
+    return labelList::null();
 }
 
 
@@ -170,7 +196,7 @@ Foam::label Foam::functionObjects::volRegion::nCells() const
 {
     if (regionType_ == vrtAll)
     {
-        return mesh_.globalData().nTotalCells();
+        return volMesh_.globalData().nTotalCells();
     }
     else
     {
@@ -183,11 +209,17 @@ Foam::scalar Foam::functionObjects::volRegion::V() const
 {
     if (regionType_ == vrtAll)
     {
-        return gSum(mesh_.V());
+        return gSum(volMesh_.V());
     }
     else
     {
-        return gSum(scalarField(mesh_.V(), cellIDs()));
+        scalar vol = 0;
+        for (const label celli : cellIDs())
+        {
+            vol += volMesh_.V()[celli];
+        }
+
+        return returnReduce(vol, sumOp<scalar>());
     }
 }
 
diff --git a/src/finiteVolume/functionObjects/volRegion/volRegion.H b/src/finiteVolume/functionObjects/volRegion/volRegion.H
index f95504684d9..ef33682b94c 100644
--- a/src/finiteVolume/functionObjects/volRegion/volRegion.H
+++ b/src/finiteVolume/functionObjects/volRegion/volRegion.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -54,9 +54,9 @@ Description
 
 Usage
     \table
-        Property     | Description                | Required     | Default value
-        regionType   | cellZone or all              | no | all
-        name         | Name of cellZone if required | no |
+        Property     | Description                          | Required | Default
+        regionType   | Selection type: all/cellSet/cellZone | no | all
+        name         | Name of cellSet/cellZone if required | no |
     \endtable
 
 See also
@@ -78,7 +78,7 @@ SourceFiles
 namespace Foam
 {
 
-// Forward declaration of classes
+// Forward declarations
 class fvMesh;
 
 namespace functionObjects
@@ -90,12 +90,16 @@ namespace functionObjects
 
 class volRegion
 {
-    // Private member data
+    // Private Member Data
 
-        const fvMesh& mesh_;
+        const fvMesh& volMesh_;
+
+        //- The cell ids, from cellSet
+        labelList cellIds_;
 
         // Cache integral properties of the region for writeFileHeader
         label nCells_;
+
         scalar V_;
 
 
@@ -106,8 +110,9 @@ public:
         //- Region type enumeration
         enum regionTypes
         {
-            vrtCellZone,    //!< cell zone
-            vrtAll          //!< all cells
+            vrtAll,             //!< All cells
+            vrtCellSet,         //!< A cellSet
+            vrtCellZone         //!< A cellZone
         };
 
         //- Region type names
@@ -116,15 +121,15 @@ public:
 
 protected:
 
-    // Protected data
+    // Protected Data
 
         //- Region type
         regionTypes regionType_;
 
-        //- Region name (patch, zone, etc.)
+        //- Region name (cellSet, cellZone, ...)
         word regionName_;
 
-        //- Region ID (patch ID, zone ID, etc.)
+        //- Region ID (zone ID, ...)
         label regionID_;
 
 
@@ -147,13 +152,13 @@ public:
 
 
     //- Destructor
-    virtual ~volRegion();
+    virtual ~volRegion() = default;
 
 
     // Public Member Functions
 
         //- Read from dictionary
-        bool read(const dictionary&);
+        bool read(const dictionary& dict);
 
         //- Return the region type
         inline const regionTypes& regionType() const;
@@ -161,7 +166,7 @@ public:
         //- Return the local list of cell IDs
         const labelList& cellIDs() const;
 
-        //- Return the number of cells in the region
+        //- Return the number of cells selected in the region
         label nCells() const;
 
         //- Return total volume of the region
-- 
GitLab