From aeae8e70de170f8b7cc94f5728e6974b14759692 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 17 May 2017 16:25:20 +0200
Subject: [PATCH] ENH: simplify sampling grouping using new IOobjectList
 methods

---
 src/sampling/probes/probes.H                  |  72 +++++----
 src/sampling/probes/probesGrouping.C          | 146 +++++++----------
 .../sampledSet/sampledSets/sampledSets.H      |  42 +++--
 .../sampledSets/sampledSetsGrouping.C         | 147 ++++++++----------
 .../sampledSurfaces/sampledSurfacesGrouping.C |  66 ++++----
 5 files changed, 206 insertions(+), 267 deletions(-)

diff --git a/src/sampling/probes/probes.H b/src/sampling/probes/probes.H
index 28a7ac2e24d..8feb83a229a 100644
--- a/src/sampling/probes/probes.H
+++ b/src/sampling/probes/probes.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -133,45 +133,46 @@ protected:
         //- Load fields from files (not from objectRegistry)
         bool loadFromFiles_;
 
-        // Read from dictonary
 
-            //- Names of fields to probe
-            wordReList fieldSelection_;
+      // Read from dictonary
 
-            //- Fixed locations, default = yes
-            //  Note: set to false for moving mesh calculations where locations
-            //        should move with the mesh
-            bool fixedLocations_;
+        //- Names of fields to probe
+        wordReList fieldSelection_;
 
-            //- Interpolation scheme name
-            //  Note: only possible when fixedLocations_ is true
-            word interpolationScheme_;
+        //- Fixed locations, default = yes
+        //  Note: set to false for moving mesh calculations where locations
+        //        should move with the mesh
+        bool fixedLocations_;
 
+        //- Interpolation scheme name
+        //  Note: only possible when fixedLocations_ is true
+        word interpolationScheme_;
 
-        // Calculated
 
-            //- Categorized scalar/vector/tensor vol fields
-            fieldGroup<scalar> scalarFields_;
-            fieldGroup<vector> vectorFields_;
-            fieldGroup<sphericalTensor> sphericalTensorFields_;
-            fieldGroup<symmTensor> symmTensorFields_;
-            fieldGroup<tensor> tensorFields_;
+      // Calculated
 
-            //- Categorized scalar/vector/tensor surf fields
-            fieldGroup<scalar> surfaceScalarFields_;
-            fieldGroup<vector> surfaceVectorFields_;
-            fieldGroup<sphericalTensor> surfaceSphericalTensorFields_;
-            fieldGroup<symmTensor> surfaceSymmTensorFields_;
-            fieldGroup<tensor> surfaceTensorFields_;
+        //- Categorized scalar/vector/tensor vol fields
+        fieldGroup<scalar> scalarFields_;
+        fieldGroup<vector> vectorFields_;
+        fieldGroup<sphericalTensor> sphericalTensorFields_;
+        fieldGroup<symmTensor> symmTensorFields_;
+        fieldGroup<tensor> tensorFields_;
 
-            // Cells to be probed (obtained from the locations)
-            labelList elementList_;
+        //- Categorized scalar/vector/tensor surf fields
+        fieldGroup<scalar> surfaceScalarFields_;
+        fieldGroup<vector> surfaceVectorFields_;
+        fieldGroup<sphericalTensor> surfaceSphericalTensorFields_;
+        fieldGroup<symmTensor> surfaceSymmTensorFields_;
+        fieldGroup<tensor> surfaceTensorFields_;
 
-            // Faces to be probed
-            labelList faceList_;
+        // Cells to be probed (obtained from the locations)
+        labelList elementList_;
 
-            //- Current open files
-            HashPtrTable<OFstream> probeFilePtrs_;
+        // Faces to be probed
+        labelList faceList_;
+
+        //- Current open files
+        HashPtrTable<OFstream> probeFilePtrs_;
 
 
     // Protected Member Functions
@@ -179,16 +180,13 @@ protected:
         //- Clear old field groups
         void clearFieldGroups();
 
-        //- Append fieldName to the appropriate group
-        label appendFieldGroup(const word& fieldName, const word& fieldType);
-
         //- Classify field types, returns the number of fields
         label classifyFields();
 
         //- Find cells and faces containing probes
-        virtual void findElements(const fvMesh&);
+        virtual void findElements(const fvMesh& mesh);
 
-        //- Classify field type and Open/close file streams,
+        //- Classify field type and open/close file streams,
         //  returns number of fields to sample
         label prepare();
 
@@ -219,10 +217,10 @@ private:
         void sampleAndWriteSurfaceFields(const fieldGroup<Type>&);
 
         //- Disallow default bitwise copy construct
-        probes(const probes&);
+        probes(const probes&) = delete;
 
         //- Disallow default bitwise assignment
-        void operator=(const probes&);
+        void operator=(const probes&) = delete;
 
 
 public:
diff --git a/src/sampling/probes/probesGrouping.C b/src/sampling/probes/probesGrouping.C
index 5d228f1152d..946cc93d304 100644
--- a/src/sampling/probes/probesGrouping.C
+++ b/src/sampling/probes/probesGrouping.C
@@ -47,106 +47,74 @@ void Foam::probes::clearFieldGroups()
 }
 
 
-Foam::label Foam::probes::appendFieldGroup
-(
-    const word& fieldName,
-    const word& fieldType
-)
-{
-    if (fieldType == volScalarField::typeName)
-    {
-        scalarFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volVectorField::typeName)
-    {
-        vectorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volSphericalTensorField::typeName)
-    {
-        sphericalTensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volSymmTensorField::typeName)
-    {
-        symmTensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volTensorField::typeName)
-    {
-        tensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == surfaceScalarField::typeName)
-    {
-        surfaceScalarFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == surfaceVectorField::typeName)
-    {
-        surfaceVectorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == surfaceSphericalTensorField::typeName)
-    {
-        surfaceSphericalTensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == surfaceSymmTensorField::typeName)
-    {
-        surfaceSymmTensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == surfaceTensorField::typeName)
-    {
-        surfaceTensorFields_.append(fieldName);
-        return 1;
-    }
-
-    return 0;
-}
-
-
 Foam::label Foam::probes::classifyFields()
 {
     label nFields = 0;
     clearFieldGroups();
 
-    if (loadFromFiles_)
+    HashTable<wordHashSet> available =
+    (
+        loadFromFiles_
+      ? IOobjectList(mesh_, mesh_.time().timeName()).classes(fieldSelection_)
+      : mesh_.classes(fieldSelection_)
+    );
+
+    forAllConstIters(available, iter)
     {
-        // Check files for a particular time
-        IOobjectList objects(mesh_, mesh_.time().timeName());
-        wordList allFields = objects.sortedNames();
+        const word& fieldType = iter.key();
+        const wordList fieldNames = iter.object().sortedToc();
 
-        labelList indices = findStrings(fieldSelection_, allFields);
+        const label count = fieldNames.size(); // pre-filtered, so non-empty
 
-        forAll(indices, fieldi)
+        if (fieldType == volScalarField::typeName)
         {
-            const word& fieldName = allFields[indices[fieldi]];
-
-            nFields += appendFieldGroup
-            (
-                fieldName,
-                objects.find(fieldName)()->headerClassName()
-            );
+            scalarFields_.append(fieldNames);
+            nFields += count;
         }
-    }
-    else
-    {
-        // Check currently available fields
-        wordList allFields = mesh_.sortedNames();
-        labelList indices = findStrings(fieldSelection_, allFields);
-
-        forAll(indices, fieldi)
+        else if (fieldType == volVectorField::typeName)
         {
-            const word& fieldName = allFields[indices[fieldi]];
-
-            nFields += appendFieldGroup
-            (
-                fieldName,
-                mesh_.find(fieldName)()->type()
-            );
+            vectorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volSphericalTensorField::typeName)
+        {
+            sphericalTensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volSymmTensorField::typeName)
+        {
+            symmTensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volTensorField::typeName)
+        {
+            tensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == surfaceScalarField::typeName)
+        {
+            surfaceScalarFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == surfaceVectorField::typeName)
+        {
+            surfaceVectorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == surfaceSphericalTensorField::typeName)
+        {
+            surfaceSphericalTensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == surfaceSymmTensorField::typeName)
+        {
+            surfaceSymmTensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == surfaceTensorField::typeName)
+        {
+            surfaceTensorFields_.append(fieldNames);
+            nFields += count;
         }
     }
 
diff --git a/src/sampling/sampledSet/sampledSets/sampledSets.H b/src/sampling/sampledSet/sampledSets/sampledSets.H
index 20d8b3c5769..4f64c305fc4 100644
--- a/src/sampling/sampledSet/sampledSets/sampledSets.H
+++ b/src/sampling/sampledSet/sampledSets/sampledSets.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2015-2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2015-2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -104,7 +104,6 @@ class sampledSets
             {
                 formatter = writer<Type>::New(writeFormat);
             }
-
         };
 
 
@@ -173,31 +172,31 @@ class sampledSets
         meshSearch searchEngine_;
 
 
-        // Read from dictonary
+      // Read from dictonary
 
-            //- Names of fields to sample
-            wordReList fieldSelection_;
+        //- Names of fields to sample
+        wordReList fieldSelection_;
 
-            //- Interpolation scheme to use
-            word interpolationScheme_;
+        //- Interpolation scheme to use
+        word interpolationScheme_;
 
-            //- Output format to use
-            word writeFormat_;
+        //- Output format to use
+        word writeFormat_;
 
 
-        // Categorized scalar/vector/tensor fields
+      // Categorized scalar/vector/tensor fields
 
-            fieldGroup<scalar> scalarFields_;
-            fieldGroup<vector> vectorFields_;
-            fieldGroup<sphericalTensor> sphericalTensorFields_;
-            fieldGroup<symmTensor> symmTensorFields_;
-            fieldGroup<tensor> tensorFields_;
+        fieldGroup<scalar> scalarFields_;
+        fieldGroup<vector> vectorFields_;
+        fieldGroup<sphericalTensor> sphericalTensorFields_;
+        fieldGroup<symmTensor> symmTensorFields_;
+        fieldGroup<tensor> tensorFields_;
 
 
-        // Merging structures
+      // Merging structures
 
-            PtrList<coordSet> masterSampledSets_;
-            labelListList indexSets_;
+        PtrList<coordSet> masterSampledSets_;
+        labelListList indexSets_;
 
 
     // Private Member Functions
@@ -205,9 +204,6 @@ class sampledSets
         //- Clear old field groups
         void clearFieldGroups();
 
-        //- Append fieldName to the appropriate group
-        label appendFieldGroup(const word& fieldName, const word& fieldType);
-
         //- Classify field types, returns the number of fields
         label classifyFields();
 
@@ -245,8 +241,8 @@ class sampledSets
 
 
         //- Disallow default bitwise copy construct and assignment
-        sampledSets(const sampledSets&);
-        void operator=(const sampledSets&);
+        sampledSets(const sampledSets&) = delete;
+        void operator=(const sampledSets&) = delete;
 
 
 public:
diff --git a/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C b/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C
index bdf8c18581e..4d57af3ebee 100644
--- a/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C
+++ b/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -27,6 +27,7 @@ License
 #include "volFields.H"
 #include "IOobjectList.H"
 #include "stringListOps.H"
+#include "UIndirectList.H"
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
@@ -40,107 +41,81 @@ void Foam::sampledSets::clearFieldGroups()
 }
 
 
-Foam::label Foam::sampledSets::appendFieldGroup
-(
-    const word& fieldName,
-    const word& fieldType
-)
-{
-    if (fieldType == volScalarField::typeName)
-    {
-        scalarFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volVectorField::typeName)
-    {
-        vectorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volSphericalTensorField::typeName)
-    {
-        sphericalTensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volSymmTensorField::typeName)
-    {
-        symmTensorFields_.append(fieldName);
-        return 1;
-    }
-    else if (fieldType == volTensorField::typeName)
-    {
-        tensorFields_.append(fieldName);
-        return 1;
-    }
-
-    return 0;
-}
-
-
 Foam::label Foam::sampledSets::classifyFields()
 {
     label nFields = 0;
     clearFieldGroups();
 
+    wordList allFields;    // Just needed for warnings
+    HashTable<wordHashSet> available;
+
     if (loadFromFiles_)
     {
         // Check files for a particular time
         IOobjectList objects(mesh_, mesh_.time().timeName());
-        wordList allFields = objects.sortedNames();
 
-        forAll(fieldSelection_, i)
-        {
-            labelList indices = findStrings(fieldSelection_[i], allFields);
-
-            if (indices.size())
-            {
-                forAll(indices, fieldi)
-                {
-                    const word& fieldName = allFields[indices[fieldi]];
-
-                    nFields += appendFieldGroup
-                    (
-                        fieldName,
-                        objects.find(fieldName)()->headerClassName()
-                    );
-                }
-            }
-            else
-            {
-                WarningInFunction
-                    << "Cannot find field file matching "
-                    << fieldSelection_[i] << endl;
-            }
-        }
+        allFields = objects.names();
+        available = objects.classes(fieldSelection_);
     }
     else
     {
         // Check currently available fields
-        wordList allFields = mesh_.sortedNames();
-        labelList indices = findStrings(fieldSelection_, allFields);
+        allFields = mesh_.names();
+        available = mesh_.classes(fieldSelection_);
+    }
+
+    DynamicList<label> missed(fieldSelection_.size());
 
-        forAll(fieldSelection_, i)
+    // Detect missing fields
+    forAll(fieldSelection_, i)
+    {
+        if (findStrings(fieldSelection_[i], allFields).empty())
+        {
+            missed.append(i);
+        }
+    }
+
+    if (missed.size())
+    {
+        WarningInFunction
+            << nl
+            << "Cannot find "
+            << (loadFromFiles_ ? "field file" : "registered field")
+            << " matching "
+            << UIndirectList<wordRe>(fieldSelection_, missed) << endl;
+    }
+
+    forAllConstIters(available, iter)
+    {
+        const word& fieldType = iter.key();
+        const wordList fieldNames = iter.object().sortedToc();
+
+        const label count = fieldNames.size(); // pre-filtered, so non-empty
+
+        if (fieldType == volScalarField::typeName)
+        {
+            scalarFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volVectorField::typeName)
+        {
+            vectorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volSphericalTensorField::typeName)
+        {
+            sphericalTensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volSymmTensorField::typeName)
+        {
+            symmTensorFields_.append(fieldNames);
+            nFields += count;
+        }
+        else if (fieldType == volTensorField::typeName)
         {
-            labelList indices = findStrings(fieldSelection_[i], allFields);
-
-            if (indices.size())
-            {
-                forAll(indices, fieldi)
-                {
-                    const word& fieldName = allFields[indices[fieldi]];
-
-                    nFields += appendFieldGroup
-                    (
-                        fieldName,
-                        mesh_.find(fieldName)()->type()
-                    );
-                }
-            }
-            else
-            {
-                WarningInFunction
-                    << "Cannot find registered field matching "
-                    << fieldSelection_[i] << endl;
-            }
+            tensorFields_.append(fieldNames);
+            nFields += count;
         }
     }
 
diff --git a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C
index 143837a4eb3..233bb65e104 100644
--- a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C
+++ b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.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  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -24,9 +24,9 @@ License
 \*---------------------------------------------------------------------------*/
 
 #include "sampledSurfaces.H"
-#include "volFields.H"
 #include "IOobjectList.H"
 #include "stringListOps.H"
+#include "UIndirectList.H"
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
@@ -34,50 +34,52 @@ Foam::label Foam::sampledSurfaces::classifyFields()
 {
     label nFields = 0;
 
+    wordList allFields;    // Just needed for warnings
+    HashTable<wordHashSet> available;
+
     if (loadFromFiles_)
     {
         // Check files for a particular time
         IOobjectList objects(obr_, obr_.time().timeName());
-        wordList allFields = objects.sortedNames();
 
-        forAll(fieldSelection_, i)
-        {
-            labelList indices = findStrings(fieldSelection_[i], allFields);
-
-            if (indices.size())
-            {
-                nFields += indices.size();
-            }
-            else
-            {
-                WarningInFunction
-                    << "Cannot find field file matching "
-                    << fieldSelection_[i] << endl;
-            }
-        }
+        allFields = objects.names();
+        available = objects.classes(fieldSelection_);
     }
     else
     {
         // Check currently available fields
-        wordList allFields = obr_.sortedNames();
+        allFields = obr_.names();
+        available = obr_.classes(fieldSelection_);
+    }
 
-        forAll(fieldSelection_, i)
+    DynamicList<label> missed(fieldSelection_.size());
+
+    // Detect missing fields
+    forAll(fieldSelection_, i)
+    {
+        if (findStrings(fieldSelection_[i], allFields).empty())
         {
-            labelList indices = findStrings(fieldSelection_[i], allFields);
-
-            if (indices.size())
-            {
-                nFields += indices.size();
-            }
-            else
-            {
-                WarningInFunction
-                    << "Cannot find registered field matching "
-                    << fieldSelection_[i] << endl;
-            }
+            missed.append(i);
         }
     }
 
+    if (missed.size())
+    {
+        WarningInFunction
+            << nl
+            << "Cannot find "
+            << (loadFromFiles_ ? "field file" : "registered field")
+            << " matching "
+            << UIndirectList<wordRe>(fieldSelection_, missed) << endl;
+    }
+
+
+    // Total number selected
+    forAllConstIters(available, iter)
+    {
+        nFields += iter.object().size();
+    }
+
     return nFields;
 }
 
-- 
GitLab