From abc8d9619852b11dd32fe210688cfb19e0efaaec Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Tue, 1 Dec 2009 14:42:46 +0100
Subject: [PATCH] sampledSurfaces - handle fields as wordReList

- this allows something like this:
      fields( T U "Ua.*" );
  to select normal fields and all the adjoint velocity fields
---
 .../sampledSurfaces/sampledSurfaces.C         | 131 +++++++++---------
 .../sampledSurfaces/sampledSurfaces.H         |  12 +-
 2 files changed, 76 insertions(+), 67 deletions(-)

diff --git a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C
index d6b9613f6f1..7969b010cdb 100644
--- a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C
+++ b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C
@@ -33,6 +33,9 @@ License
 #include "mergePoints.H"
 #include "volPointInterpolation.H"
 
+#include "IOobjectList.H"
+#include "stringListOps.H"
+
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
 namespace Foam
@@ -70,6 +73,42 @@ Foam::scalar Foam::sampledSurfaces::mergeTol_(1e-10);
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
+Foam::label Foam::sampledSurfaces::appendFieldType
+(
+    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::sampledSurfaces::classifyFieldTypes()
 {
     label nFields = 0;
@@ -80,75 +119,40 @@ Foam::label Foam::sampledSurfaces::classifyFieldTypes()
     symmTensorFields_.clear();
     tensorFields_.clear();
 
-    forAll(fieldNames_, fieldI)
+    // check files for a particular time
+    if (loadFromFiles_)
     {
-        const word& fieldName = fieldNames_[fieldI];
-        word fieldType = "";
+        IOobjectList objects(mesh_, mesh_.time().timeName());
+        wordList allFields = objects.sortedNames();
+
+        labelList indices = findStrings(fieldSelection_, allFields);
 
-        // check files for a particular time
-        if (loadFromFiles_)
+        forAll(indices, fieldI)
         {
-            IOobject io
+            const word& fieldName = allFields[indices[fieldI]];
+
+            nFields += appendFieldType
             (
                 fieldName,
-                mesh_.time().timeName(),
-                mesh_,
-                IOobject::MUST_READ,
-                IOobject::NO_WRITE,
-                false
+                objects.find(fieldName)()->headerClassName()
             );
-
-            if (io.headerOk())
-            {
-                fieldType = io.headerClassName();
-            }
-            else
-            {
-                continue;
-            }
         }
-        else
-        {
-            // check objectRegistry
-            objectRegistry::const_iterator iter = mesh_.find(fieldName);
-
-            if (iter != mesh_.objectRegistry::end())
-            {
-                fieldType = iter()->type();
-            }
-            else
-            {
-                continue;
-            }
-        }
-
+    }
+    else
+    {
+        wordList allFields = mesh_.sortedNames();
+        labelList indices = findStrings(fieldSelection_, allFields);
 
-        if (fieldType == volScalarField::typeName)
+        forAll(indices, fieldI)
         {
-            scalarFields_.append(fieldName);
-            nFields++;
-        }
-        else if (fieldType == volVectorField::typeName)
-        {
-            vectorFields_.append(fieldName);
-            nFields++;
-        }
-        else if (fieldType == volSphericalTensorField::typeName)
-        {
-            sphericalTensorFields_.append(fieldName);
-            nFields++;
-        }
-        else if (fieldType == volSymmTensorField::typeName)
-        {
-            symmTensorFields_.append(fieldName);
-            nFields++;
-        }
-        else if (fieldType == volTensorField::typeName)
-        {
-            tensorFields_.append(fieldName);
-            nFields++;
-        }
+            const word& fieldName = allFields[indices[fieldI]];
 
+            nFields += appendFieldType
+            (
+                fieldName,
+                mesh_.find(fieldName)()->type()
+            );
+        }
     }
 
     return nFields;
@@ -208,7 +212,7 @@ Foam::sampledSurfaces::sampledSurfaces
     mesh_(refCast<const fvMesh>(obr)),
     loadFromFiles_(loadFromFiles),
     outputPath_(fileName::null),
-    fieldNames_(),
+    fieldSelection_(),
     interpolationScheme_(word::null),
     writeFormat_(word::null),
     mergeList_(),
@@ -304,9 +308,10 @@ void Foam::sampledSurfaces::write()
 
 void Foam::sampledSurfaces::read(const dictionary& dict)
 {
-    fieldNames_ = wordList(dict.lookup("fields"));
+    dict.lookup("fields") >> fieldSelection_;
 
-    const label nFields = fieldNames_.size();
+    // might be okay for a size estimate, but we don't really know
+    const label nFields = fieldSelection_.size();
 
     scalarFields_.reset(nFields);
     vectorFields_.reset(nFields);
@@ -348,7 +353,7 @@ void Foam::sampledSurfaces::read(const dictionary& dict)
 
     if (Pstream::master() && debug)
     {
-        Pout<< "sample fields:" << fieldNames_ << nl
+        Pout<< "sample fields:" << fieldSelection_ << nl
             << "sample surfaces:" << nl << "(" << nl;
 
         forAll(*this, surfI)
diff --git a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.H b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.H
index 4ad59c486f7..cd28e1f2ca1 100644
--- a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.H
+++ b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.H
@@ -41,6 +41,7 @@ SourceFiles
 #include "sampledSurface.H"
 #include "surfaceWriter.H"
 #include "volFieldsFwd.H"
+#include "wordReList.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -51,7 +52,7 @@ class fvMesh;
 class dictionary;
 
 /*---------------------------------------------------------------------------*\
-                      Class sampledSurfaces Declaration
+                       Class sampledSurfaces Declaration
 \*---------------------------------------------------------------------------*/
 
 class sampledSurfaces
@@ -100,8 +101,8 @@ class sampledSurfaces
             void reset(const label nElem)
             {
                 formatter.clear();
-                DynamicList<word>::setCapacity(nElem);
-                DynamicList<word>::clear(); 
+                DynamicList<word>::reserve(nElem);
+                DynamicList<word>::clear();
             }
 
             void operator=(const word& writeFormat)
@@ -161,7 +162,7 @@ class sampledSurfaces
         // Read from dictonary
 
             //- Names of fields to sample
-            wordList fieldNames_;
+            wordReList fieldSelection_;
 
             //- Interpolation scheme to use
             word interpolationScheme_;
@@ -191,6 +192,9 @@ class sampledSurfaces
 
     // Private Member Functions
 
+        //- Append fieldName to the appropriate group
+        label appendFieldType(const word& fieldName, const word& fieldType);
+
         //- Classify field types, returns the number of fields
         label classifyFieldTypes();
 
-- 
GitLab